1+ import hou # for ui only
12import hpastewebplugins
23import widcacher
34import random # to shuffle plugins
78
89
910def 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
5164def 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
0 commit comments