Skip to content

Commit cf3b3f2

Browse files
authored
Merge pull request #28 from pedohorse/dev
Dev
2 parents e77036f + 9a2fc6c commit cf3b3f2

File tree

4 files changed

+168
-143
lines changed

4 files changed

+168
-143
lines changed

python2.7libs/hpaste/hpasteweb.py

Lines changed: 90 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hou # for ui only
12
import hpastewebplugins
23
import widcacher
34
import random # to shuffle plugins
@@ -7,91 +8,103 @@
78

89

910
def webPack(asciiText, pluginList=None, maxChunkSize=None):
10-
allPackids=[]
11-
done = False
11+
allPackids=[]
12+
done = False
1213

13-
while not done:
14-
packid = None
15-
if pluginList is None:
16-
pluginClasses = [x for x in hpastewebplugins.pluginClassList]
17-
random.shuffle(pluginClasses)
18-
pluginClasses.sort(reverse=True, key=lambda x: x.speedClass())
19-
else:
20-
pluginClasses = []
21-
for pname in pluginList:
22-
pluginClasses += [x for x in hpastewebplugins.pluginClassList if x.__name__ == pname]
23-
cls = None
24-
for cls in pluginClasses:
25-
try:
26-
packer = cls()
27-
chunklen = min(packer.maxStringLength(), len(asciiText))
28-
if maxChunkSize is not None:
29-
chunklen = min(chunklen, maxChunkSize)
30-
chunk = asciiText[:chunklen]
31-
packid = packer.webPackData(chunk)
32-
asciiText = asciiText[chunklen:]
33-
break
34-
except Exception as e:
35-
print("error: %s" % str(e.message))
36-
print("failed to pack with plugin %s, looking for alternatives..." % cls.__name__)
37-
continue
38-
if packid is None or cls is None:
39-
print("all web packing methods failed, sorry :(")
40-
raise RuntimeError("couldnt web pack data")
14+
# sanity check
15+
if len(asciiText) > 2 ** 23:
16+
if hou.isUIAvailable():
17+
if hou.ui.displayMessage('you are about to save %d Mb into a snippet. This is HIGHLY DISCOURAGED!' % (len(asciiText) // 2 ** 20,),
18+
buttons=('Proceed', 'Cancel'), default_choice=1, close_choice=1,
19+
severity=hou.severityType.Warning) != 0:
20+
raise RuntimeError('snippet too big. cancelled')
21+
else:
22+
raise RuntimeError('snippet too big. cancelled')
23+
#
4124

42-
allPackids.append('@'.join((packid, cls.__name__)))
43-
done = len(asciiText) == 0
44-
# just a failsafe:
45-
if len(allPackids) > 128:
46-
raise RuntimeError("Failsafe triggered: for some reason too many chunks. plz check the chunkSize, or your plugins for too small allowed string sizes, or your data for sanity.")
25+
while not done:
26+
packid = None
27+
if pluginList is None:
28+
pluginClasses = [x for x in hpastewebplugins.pluginClassList]
29+
random.shuffle(pluginClasses)
30+
pluginClasses.sort(reverse=True, key=lambda x: x.speedClass())
31+
else:
32+
pluginClasses = []
33+
for pname in pluginList:
34+
pluginClasses += [x for x in hpastewebplugins.pluginClassList if x.__name__ == pname]
4735

48-
return '#'.join(allPackids)
36+
cls = None
37+
for cls in pluginClasses:
38+
try:
39+
packer = cls()
40+
chunklen = min(packer.maxStringLength(), len(asciiText))
41+
if maxChunkSize is not None:
42+
chunklen = min(chunklen, maxChunkSize)
43+
chunk = asciiText[:chunklen]
44+
packid = packer.webPackData(chunk)
45+
asciiText = asciiText[chunklen:]
46+
break
47+
except Exception as e:
48+
print("error: %s" % str(e.message))
49+
print("failed to pack with plugin %s, looking for alternatives..." % cls.__name__)
50+
continue
51+
if packid is None or cls is None:
52+
print("all web packing methods failed, sorry :(")
53+
raise RuntimeError("couldnt web pack data")
54+
55+
allPackids.append('@'.join((packid, cls.__name__)))
56+
done = len(asciiText) == 0
57+
# just a failsafe:
58+
if len(allPackids) > 128:
59+
raise RuntimeError("Failsafe triggered: for some reason too many chunks. plz check the chunkSize, or your plugins for too small allowed string sizes, or your data for sanity.")
60+
61+
return '#'.join(allPackids)
4962

5063

5164
def webUnpack(wid, useCached=True, cache=None):
52-
#just a bit cleanup the wid first, in case it was copied with extra spaces
53-
# strip witespaces, just to be sure
54-
wid = wid.strip()
55-
# strip comments if there are
56-
wid = re.sub(r'^\S+\s+', '', wid)
57-
#
58-
if useCached:
59-
if cache is None: # default cacher
60-
cache = widcacher.WidCacher.globalInstance()
61-
if wid in cache:
62-
return cache[wid]
65+
#just a bit cleanup the wid first, in case it was copied with extra spaces
66+
# strip witespaces, just to be sure
67+
wid = wid.strip()
68+
# strip comments if there are
69+
wid = re.sub(r'^\S+\s+', '', wid)
70+
#
71+
if useCached:
72+
if cache is None: # default cacher
73+
cache = widcacher.WidCacher.globalInstance()
74+
if wid in cache:
75+
return cache[wid]
6376

64-
allPackids = wid.split('#')
65-
asciiTextParts = []
66-
for awid in allPackids:
67-
if awid.count('@') != 1:
68-
raise RuntimeError('given wid is not a valid wid')
69-
id, cname = awid.split('@')
77+
allPackids = wid.split('#')
78+
asciiTextParts = []
79+
for awid in allPackids:
80+
if awid.count('@') != 1:
81+
raise RuntimeError('given wid is not a valid wid')
82+
id, cname = awid.split('@')
7083

71-
pretendents = [x for x in hpastewebplugins.pluginClassList if x.__name__ == cname]
72-
if len(pretendents) == 0:
73-
raise RuntimeError("No plugins that can process this wid found")
84+
pretendents = [x for x in hpastewebplugins.pluginClassList if x.__name__ == cname]
85+
if len(pretendents) == 0:
86+
raise RuntimeError("No plugins that can process this wid found")
7487

75-
asciiText = None
76-
for cls in pretendents:
77-
try:
78-
unpacker = cls()
79-
asciiText = unpacker.webUnpackData(id)
80-
break
81-
except WebClipBoardWidNotFound as e:
82-
raise RuntimeError('item "%s" does not exist. it may have expired' % e.wid)
83-
except Exception as e:
84-
print("error: %s: %s" % (str(type(e)), str(e.message)))
85-
print("Exception: %s" % repr(e))
86-
print("keep trying...")
87-
continue
88-
if asciiText is None:
89-
print("failed")
90-
raise RuntimeError("couldn't web unpack data")
91-
asciiTextParts.append(asciiText)
88+
asciiText = None
89+
for cls in pretendents:
90+
try:
91+
unpacker = cls()
92+
asciiText = unpacker.webUnpackData(id)
93+
break
94+
except WebClipBoardWidNotFound as e:
95+
raise RuntimeError('item "%s" does not exist. it may have expired' % e.wid)
96+
except Exception as e:
97+
print("error: %s: %s" % (str(type(e)), str(e.message)))
98+
print("Exception: %s" % repr(e))
99+
print("keep trying...")
100+
continue
101+
if asciiText is None:
102+
print("failed")
103+
raise RuntimeError("couldn't web unpack data")
104+
asciiTextParts.append(asciiText)
92105

93-
finalText = ''.join(asciiTextParts)
94-
if useCached and cache is not None:
95-
cache[wid] = finalText
106+
finalText = ''.join(asciiTextParts)
107+
if useCached and cache is not None:
108+
cache[wid] = finalText
96109

97-
return finalText
110+
return finalText

python2.7libs/hpaste/hpastewebplugins/houhpaste.py

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,68 @@
66

77

88
class HPaste(WebClipBoardBase):
9-
def __init__(self):
10-
self.__headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
11-
12-
@classmethod
13-
def speedClass(cls):
14-
return opt.getOption('hpasteweb.plugins.%s.speed_class'%cls.__name__, 10)
15-
16-
@classmethod
17-
def maxStringLength(cls):
18-
return 400000
19-
20-
@classmethod
21-
def urlopen(cls, url, timeout=30):
22-
try:
23-
rep = urllib2.urlopen(url, timeout=timeout)
24-
except urllib2.URLError as e:
25-
try:
26-
import certifi
27-
rep = urllib2.urlopen(url, timeout=timeout, cafile=certifi.where())
28-
except ImportError:
29-
import ssl
30-
rep = urllib2.urlopen(url, timeout=timeout, context=ssl._create_unverified_context())
31-
print "WARNING: connected with unverified context"
32-
return rep
33-
34-
def webPackData(self, s):
35-
if (not isinstance(s, str)):
36-
s = str(s)
37-
if (len(s) > self.maxStringLength()): raise RuntimeError("len of s it too big for web clipboard currently")
38-
39-
try:
40-
req = urllib2.Request(r"https://hou-hpaste.herokuapp.com/documents", s, headers=self.__headers)
41-
rep = self.urlopen(req, timeout=30)
42-
repstring = rep.read()
43-
except Exception as e:
44-
raise RuntimeError("error/timeout connecting to web clipboard: " + str(e.message))
45-
46-
if (rep.getcode() != 200): raise RuntimeError("error code from web clipboard")
47-
48-
try:
49-
repson = json.loads(repstring)
50-
id=repson['key']
51-
except Exception as e:
52-
raise RuntimeError("Unknown Server responce: "+str(e.message))
53-
54-
return str(id)
55-
56-
57-
def webUnpackData(self, id):
58-
id=str(id)
59-
try:
60-
req = urllib2.Request(r"https://hou-hpaste.herokuapp.com/raw/" + id, headers=self.__headers)
61-
rep = self.urlopen(req, timeout=30)
62-
except urllib2.HTTPError as e:
63-
if e.code == 404:
64-
raise WebClipBoardWidNotFound(id)
65-
raise RuntimeError("error connecting to web clipboard: " + e.message)
66-
except Exception as e:
67-
raise RuntimeError("error/timeout connecting to web clipboard: " + e.message)
68-
69-
if (rep.getcode() != 200): raise RuntimeError("error code from web clipboard")
70-
71-
repstring = rep.read()
72-
73-
return repstring
9+
def __init__(self):
10+
self.__headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
11+
12+
@classmethod
13+
def speedClass(cls):
14+
return opt.getOption('hpasteweb.plugins.%s.speed_class'%cls.__name__, 10)
15+
16+
@classmethod
17+
def maxStringLength(cls):
18+
return 2**20
19+
20+
@classmethod
21+
def urlopen(cls, url, timeout=30):
22+
try:
23+
rep = urllib2.urlopen(url, timeout=timeout)
24+
except urllib2.URLError as e:
25+
try:
26+
import certifi
27+
rep = urllib2.urlopen(url, timeout=timeout, cafile=certifi.where())
28+
except ImportError:
29+
import ssl
30+
rep = urllib2.urlopen(url, timeout=timeout, context=ssl._create_unverified_context())
31+
print "WARNING: connected with unverified context"
32+
return rep
33+
34+
def webPackData(self, s):
35+
if (not isinstance(s, str)):
36+
s = str(s)
37+
if (len(s) > self.maxStringLength()): raise RuntimeError("len of s it too big for web clipboard currently")
38+
39+
try:
40+
req = urllib2.Request(r"https://hou-hpaste.herokuapp.com/documents", s, headers=self.__headers)
41+
rep = self.urlopen(req, timeout=30)
42+
repstring = rep.read()
43+
except Exception as e:
44+
raise RuntimeError("error/timeout connecting to web clipboard: " + str(e.message))
45+
46+
if (rep.getcode() != 200): raise RuntimeError("error code from web clipboard")
47+
48+
try:
49+
repson = json.loads(repstring)
50+
id=repson['key']
51+
except Exception as e:
52+
raise RuntimeError("Unknown Server responce: "+str(e.message))
53+
54+
return str(id)
55+
56+
57+
def webUnpackData(self, id):
58+
id=str(id)
59+
try:
60+
req = urllib2.Request(r"https://hou-hpaste.herokuapp.com/raw/" + id, headers=self.__headers)
61+
rep = self.urlopen(req, timeout=30)
62+
except urllib2.HTTPError as e:
63+
if e.code == 404:
64+
raise WebClipBoardWidNotFound(id)
65+
raise RuntimeError("error connecting to web clipboard: " + e.message)
66+
except Exception as e:
67+
raise RuntimeError("error/timeout connecting to web clipboard: " + e.message)
68+
69+
if (rep.getcode() != 200): raise RuntimeError("error code from web clipboard")
70+
71+
repstring = rep.read()
72+
73+
return repstring

python3.7libs/hpaste/hpasteweb.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hou # for ui only
12
from . import hpastewebplugins
23
from . import widcacher
34
import random # to shuffle plugins
@@ -10,6 +11,17 @@ def webPack(asciiText: str, pluginList=None, maxChunkSize=None):
1011
allPackids = []
1112
done = False
1213

14+
# sanity check
15+
if len(asciiText) > 2 ** 23:
16+
if hou.isUIAvailable():
17+
if hou.ui.displayMessage('you are about to save %d Mb into a snippet. This is HIGHLY DISCOURAGED!' % (len(asciiText) // 2 ** 20,),
18+
buttons=('Proceed', 'Cancel'), default_choice=1, close_choice=1,
19+
severity=hou.severityType.Warning) != 0:
20+
raise RuntimeError('snippet too big. cancelled')
21+
else:
22+
raise RuntimeError('snippet too big. cancelled')
23+
#
24+
1325
while not done:
1426
packid = None
1527
if pluginList is None:

python3.7libs/hpaste/hpastewebplugins/houhpaste.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def speedClass(cls):
1616

1717
@classmethod
1818
def maxStringLength(cls):
19-
return 400000
19+
return 2**20
2020

2121
@classmethod
2222
def urlopen(cls, url, timeout=30):

0 commit comments

Comments
 (0)