-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
1103 lines (840 loc) · 32.2 KB
/
Copy pathcommon.py
File metadata and controls
1103 lines (840 loc) · 32.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###########################################################
#
# Copyright (c) 2005, Southpaw Technology
# All Rights Reserved
#
# PROPRIETARY INFORMATION. This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
__all__ = ["Common", "Marshaller", "jsondumps", "jsonloads"]
import os, sys, time, string, re, random, types, new, pprint, traceback
import thread, threading, zipfile
import hashlib, StringIO, urllib
import datetime
import colorsys
from base import *
try:
from cjson import encode as jsondumps
from cjson import decode as jsonloads
except ImportError:
try:
# Python 2.6 ships with json
from json import dumps as xjsondumps
from json import loads as jsonloads
import json
except ImportError:
try:
from simplejson import dumps as xjsondumps
from simplejson import loads as jsonloads
import simplejson as json
except ImportError:
print "ERROR: no json library found"
print
raise
class JSONDateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (datetime.date, datetime.datetime)):
return obj.isoformat()
else:
return json.JSONEncoder.default(self, obj)
def jsondumps(obj):
return xjsondumps(obj, cls=JSONDateTimeEncoder)
class Common(Base):
def get_full_class_name(object):
name = "%s.%s" % (object.__module__, object.__class__.__name__)
return name
get_full_class_name = staticmethod(get_full_class_name)
def breakup_class_path(class_path):
'''breaks up a class path into a module and class_name'''
parts = string.split(class_path,".")
module_name = string.join(parts[0:len(parts)-1],".")
class_name = parts[len(parts)-1]
return (module_name, class_name)
breakup_class_path = staticmethod(breakup_class_path)
def create_from_class_path(class_path, args=[], kwargs={}):
'''dynamically creats an object from a string class path.'''
assert class_path
marshaller = Marshaller()
marshaller.set_class(class_path)
for arg in args:
marshaller.add_arg(arg)
marshaller.set_kwargs(kwargs)
# instantiate the object
object = marshaller.get_object()
return object
create_from_class_path = staticmethod(create_from_class_path)
def create_from_method(class_path, method):
'''dynamically creates an object from a string class path and its
method.
Note: this assumes an empty constructor'''
assert class_path and class_path != ""
# instantiate the object
obj = Common.create_from_class_path(class_path)
obj = eval('obj.%s()' %method)
return obj
create_from_method = staticmethod(create_from_method)
def add_func_to_class(func, instance, cls, method_name=None):
''' add a function to a class, if an instance is given
it will be bound to it. '''
# if the func is wrapped in a method, extract it
if isinstance(func, types.MethodType):
func = func.im_func
method = new.instancemethod(func, instance, cls)
if not method_name:
method_name=func.__name__
if instance:
# and the method to the instance dict to be readily callable
instance.__dict__[method_name]=method
return method
add_func_to_class = staticmethod(add_func_to_class)
def get_import_from_class_path(class_path):
'''dynamically executes a an import statement
Note: this assumes an empty constructor'''
assert class_path != None
assert class_path != ""
(module_name, class_name) = Common.breakup_class_path(class_path)
if not module_name:
return ''
return "import %s" % module_name
get_import_from_class_path = staticmethod(get_import_from_class_path)
def relative_dir(dir_a, dir_b):
'''get the relative directory between two directories'''
if dir_a == dir_b:
return "."
# strip out any ending /'s
if dir_a.endswith("/"):
dir_a = dir_a.rstrip("/")
if dir_b.endswith("/"):
dir_b = dir_b.rstrip("/")
parts_a = dir_a.split("/")
parts_b = dir_b.split("/")
len_a = len(parts_a)
len_b = len(parts_b)
if len_a < len_b:
length = len_a
else:
length = len_b
# remove parts that are similar
for i in range(0, length):
if parts_a[0] != parts_b[0]:
break
parts_a = parts_a[1:]
parts_b = parts_b[1:]
# figure out the relative path
len_a = len(parts_a)
len_b = len(parts_b)
relative = [".."] * len_a
relative.extend(parts_b)
relative = "/".join(relative)
return relative
relative_dir = staticmethod(relative_dir)
def relative_path(cls, path_a, path_b):
'''get the relative path between two paths. These will include
file names and should be handled differently'''
dir_a = os.path.dirname(path_a)
#file_a = os.path.dirname(file_a)
dir_b = os.path.dirname(path_b)
file_b = os.path.basename(path_b)
rel_dir = cls.relative_dir(dir_a, dir_b)
return "%s/%s" % (rel_dir, file_b)
relative_path = classmethod(relative_path)
def generate_random_key():
# generate a random key
random.seed()
random_key = ""
for i in range(0,19):
random_key += chr(random.randint(0,255))
#random_key = md5.new(random_key).hexdigest()
random_key = hashlib.md5(random_key).hexdigest()
return random_key
generate_random_key = staticmethod(generate_random_key)
def generate_alphanum_key(num_digits=10, mode="alpha"):
if mode == "alpha":
low = 65
high = 90
else:
log = 48
high = 90
# generate a random key
key = ""
for i in range(0, num_digits):
idx = 58
while idx >= 58 and idx <= 64:
idx = random.randint(low, high)
key += chr(idx)
return key
generate_alphanum_key = staticmethod(generate_alphanum_key)
def extract_keywords(data):
is_ascii = Common.is_ascii(data)
data = re.sub(r'([_|,])+', ' ', data)
if is_ascii:
# other non ASCII languages don't need these
data = re.sub(r'([^\s\w\'])+', '', data)
# lowercase is still needed for a mix of ASCII and non-ASCII like a french word
data = data.lower().split(" ")
data = [x for x in data if x]
return data
extract_keywords = staticmethod(extract_keywords)
def is_ascii(value):
'''check if a value is ASCII'''
is_ascii = True
try:
value.encode('ascii')
except UnicodeEncodeError:
is_ascii = False
else:
is_ascii = True
return is_ascii
is_ascii = staticmethod(is_ascii)
def download(url, to_dir=".", filename='', md5_checksum=""):
'''Download a file from a given url
@param:
url - the url source location of the file
@keyparam:
to_dir - the directory to download to
filename - the filename to download to, defaults to original filename
md5_checksum - an md5 checksum to match the file against
@return:
string - path of the file donwloaded
'''
# use url filename by default
if not filename:
filename = os.path.basename(url)
# make sure the directory exists
if not os.path.exists(to_dir):
os.makedirs(to_dir)
to_path = "%s/%s" % (to_dir, filename)
# check if this file is already downloaded. if so, skip
if os.path.exists(to_path):
# if it exists, check the MD5 checksum
if md5_checksum:
if my._md5_check(to_path, md5_checksum):
print "skipping '%s', already exists" % to_path
return to_path
else:
# always download if no md5_checksum available
pass
f = urllib.urlopen(url)
file = open(to_path, "wb")
file.write( f.read() )
file.close()
f.close()
return to_path
download = staticmethod(download)
def get_user_name():
'''win32 code to get the user'''
if os.name == "nt":
# Taken from:
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66314
import win32api
#import win32net
#import win32netcon
#dc=win32net.NetServerEnum(None,100,win32netcon.SV_TYPE_DOMAIN_CTRL)
user=win32api.GetUserName()
"""
if dc[0]:
dcname=dc[0][0]['name']
info = win32net.NetUserGetInfo("\\\\"+dcname,user,1)
else:
info = win32net.NetUserGetInfo(None,user,1)
return info['name']
"""
return user
else:
return os.getlogin()
get_user_name = staticmethod(get_user_name)
def get_os():
return os.name
get_os = staticmethod(get_os)
def escape_quote(function):
p = re.compile("('|\")")
return p.sub(r"\'", function)
escape_quote = staticmethod(escape_quote)
def escape_tag(function):
function = function.replace('<','<')
function = function.replace('>','>')
return function
escape_tag = staticmethod(escape_tag)
def sort_dict(dct, reverse=False):
''' sort a dictionary based on its keys,
a list of sorted values is returned '''
keys = dct.keys()
keys.sort(reverse=reverse)
return map(dct.get, keys)
sort_dict = staticmethod(sort_dict)
def get_dict_list(dct):
'''get a tuple sorted list given a dictionary'''
keys = dct.keys()
keys.sort()
# value is str() to remove the u' in front of unicode str
return [(x, dct[x]) for x in keys]
get_dict_list = staticmethod(get_dict_list)
def subset_dict(dct, keys):
return dict([ (i, dct[i] ) for i in keys])
subset_dict = staticmethod(subset_dict)
def get_unique_list(list):
''' get a unique list, order preserving'''
seen = set()
return [ x for x in list if x not in seen and not seen.add(x)]
get_unique_list = staticmethod(get_unique_list)
"""
def dump_trace(stacktrace):
'''The argument 'tb' traceback is found by calling:
tb = sys.exc_info()[2]
stacktrace = traceback.format_tb(tb)
This, unfortunately, must be called in the except clause to get
a proper traceback
'''
stacktrace_str = "".join(stacktrace)
print "-"*50
print stacktrace_str
print "-"*50
dump_trace = staticmethod(dump_trace)
"""
def match_ip(a_ip, b_ip, mask):
a_ip = a_ip.split(".")
b_ip = b_ip.split(".")
mask = mask.split(".")
for count in range(0, 4):
a_part = int(a_ip[count])
b_part = int(b_ip[count])
mask_part = int(mask[count])
if (a_part & mask_part) != b_part:
break
else:
return True
return False
match_ip = staticmethod(match_ip)
def get_dir_info(dir, skip_dir_details=False):
'''Finds the disk size of a path'''
info = {}
count = 0
dir_size = 0
if dir.find("#") != -1:
dir_size = 0
file_type = 'sequence'
elif not os.path.exists(dir):
dir_size = 0
file_type = 'missing'
elif os.path.islink(dir):
count = 0
dir_size = 0
dir_size += os.path.getsize(dir)
file_type = 'link'
elif os.path.isdir(dir):
# this part is too slow
if not skip_dir_details:
for (path, dirs, files) in os.walk(dir):
for file in files:
filename = os.path.join(path, file)
if os.path.islink(filename):
dir_size = 0;
else:
dir_size += os.path.getsize(filename)
count += 1
file_type = 'directory'
else:
dir_size = os.path.getsize(dir)
count = 1
file_type = 'file'
info['size'] = dir_size
info['count'] = count
info['file_type'] = file_type
return info
get_dir_info = staticmethod(get_dir_info)
def unzip_file(file_path, dir=None):
if not zipfile.is_zipfile(file_path):
from environment import Environment
Environment.add_warning('invalid zip file', file_path)
if dir:
os.mkdir(dir, 0777)
else:
dir, filename = os.path.split(file_path)
zf_obj = zipfile.ZipFile(file_path)
files = []
for name in zf_obj.namelist():
if name.endswith('/'):
os.mkdir(os.path.join(dir, name))
else:
outfile_path = os.path.join(dir, name)
outfile = open(outfile_path, 'wb')
outfile.write(zf_obj.read(name))
outfile.close()
files.append(outfile_path)
return files
unzip_file = staticmethod(unzip_file)
def get_filesystem_name(filename):
'''take a name and converts it to a name that can be saved in
the filesystem. This is different from File.get_filesystem_name()'''
# handle python style
p = re.compile("^__(\w+)__.py$")
if p.match(filename):
return filename
processed = []
# remove unwanted mixed delimiters. Double dlimiters of -- and __ are
# ok, but not double ..
last_char = None
length = len(filename)
for i, char in enumerate(filename):
if char == ' ':
char = '_'
if i == length - 1:
next_char = None
else:
next_char = filename[i+1]
if char == '.' and last_char == '.':
continue
if char in ['_','-']:
if last_char == '.'or next_char == '.':
continue
elif char == '-' and (last_char == '_' or next_char == '_'):
continue
elif char == '-' and last_char == '!':
continue
elif char == '_' and last_char == '!':
continue
else:
processed.append(char)
elif char == '%' and next_char == '0':
processed.append(char)
elif char in ':' and i == 1:
# handle windows C:
processed.append(char)
elif char in '''!@$%^&*()={}[]:"|;'\\<>?''':
pass
else:
processed.append(char)
last_char = char
# remove trailing . if any
if processed and processed[-1] == '.':
processed.pop()
filename = "".join(processed)
return filename
get_filesystem_name = staticmethod(get_filesystem_name)
#
# String manipulation functions
#
def expand_env(base_dir):
'''expand the environment variables in a string
e.g. $(CLASSPATH)/admin/'''
pat = re.compile('\$\(([^)]*)\)')
# find one or more $()
s = pat.finditer(base_dir)
expanded = []
iter_s = []
for i in s:
temp_str = base_dir[i.start()+2:i.end()-1]
expanded_temp_str = os.getenv(temp_str)
expanded.append(expanded_temp_str)
iter_s.append(i.group())
for idx, i in enumerate(iter_s):
base_dir = base_dir.replace(i, expanded[idx])
base_dir = base_dir.replace('\\', '/')
return base_dir
expand_env = staticmethod(expand_env)
def get_display_title(title):
title = title.strip()
if title.find("_") == -1:
# camelcase
p = re.compile("([A-Z])")
replace = " \\1"
return p.sub(replace, title).strip().title()
else:
title = title.replace("_", " ")
return title.title()
get_display_title = staticmethod(get_display_title)
def process_unicode_string( in_string ):
if isinstance(in_string, basestring):
return in_string.encode('utf-8')
else:
# for integer and floats
return str(in_string)
process_unicode_string = staticmethod(process_unicode_string)
def convert_to_strings(my, array):
new = []
for x in array:
if not isinstance(array, basestring):
x = str(array)
new.append(x)
return new
def convert_to_json(data):
regex = re.compile( r'\n\s*' )
dict_str = jsondumps(data)
dict_str = dict_str.replace('"', '"')
return dict_str
convert_to_json = staticmethod(convert_to_json)
def pretty_print(data):
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)
pretty_print = staticmethod(pretty_print)
def get_pretty_print(data):
data_str = StringIO.StringIO()
pprint.pprint(data, indent=4, stream=data_str)
return data_str.getvalue()
get_pretty_print = staticmethod(get_pretty_print)
def modify_color(color, modifier):
if not modifier:
return color
if not color:
return None
color = color.replace("#", '')
if color in ['grey', 'blue', 'red']:
return color
if len(color) == 3:
first = "%s%s" % (color[0], color[0])
second = "%s%s" % (color[1], color[1])
third = "%s%s" % (color[2], color[2])
elif len(color) == 6:
first = "%s" % color[0:2]
second = "%s" % color[2:4]
third = "%s" % color[4:6]
else:
return color
first = float(int(first, 16) ) / 256
second = float(int(second, 16) ) / 256
third = float(int(third, 16) ) / 256
if type(modifier) == types.ListType:
rgb = []
rgb.append( 0.01*modifier[0] + first )
rgb.append( 0.01*modifier[1] + second )
rgb.append( 0.01*modifier[2] + third )
else:
hsv = colorsys.rgb_to_hsv(first, second, third)
value = 0.01*modifier + hsv[2]
if value < 0:
value = 0
if value > 1:
value = 1
hsv = (hsv[0], hsv[1], value )
rgb = colorsys.hsv_to_rgb(*hsv)
first = hex(int(rgb[0]*256))[2:]
if len(first) == 1:
first = "0%s" % first
second = hex(int(rgb[1]*256))[2:]
if len(second) == 1:
second = "0%s" % second
third = hex(int(rgb[2]*256))[2:]
if len(third) == 1:
third = "0%s" % third
if len(first) == 3:
first = "FF"
if len(second) == 3:
second = "FF"
if len(third) == 3:
third = "FF"
color = "#%s%s%s" % (first, second, third)
return color
modify_color = staticmethod(modify_color)
def extract_dict(value, expression):
'''Given a string and an expression, return a dictionary for the corresponding arg name:value'''
re_expression = expression
token = []
args = {}
args_keys = []
index = 0
for char in expression:
if char == "{":
token = "".join(token)
token = []
elif char == "}":
token = "".join(token)
args_keys.append(token)
re_expression = re_expression.replace("{%s}"%token, "([\w/=\?&\.-]+)")
token = []
else:
token.append(char)
#print re_expression
p = re.compile(re_expression)
m = p.search(value)
if m:
matches = m.groups()
for key, match in zip(args_keys, matches):
args[key] = match
return args
extract_dict = staticmethod(extract_dict)
def get_next_code(code, min_padding=3):
'''function to get the next code with a minimum padding
For example: chr001 -> chr002
'''
p = re.compile("(.*?)(\d+)$")
m = p.findall(code)
if m:
match = m[0]
base = match[0]
number = match[1]
padding = len(number)
number = int(number)
else:
number = 0
padding = 1
base = code
if padding < min_padding:
padding = min_padding
next = number + 1
next_str = "%s%s" % (base, "%%0.%sd" % padding % next)
return next_str
get_next_code = staticmethod(get_next_code)
def total_size(o, handlers={}, verbose=False):
# Taken from:
#http://code.activestate.com/recipes/577504-compute-memory-footprint-of-an-object-and-its-cont/
""" Returns the approximate memory footprint an object and all of its contents.
Automatically finds the contents of the following builtin containers and
their subclasses: tuple, list, deque, dict, set and frozenset.
To search other containers, add handlers to iterate over their contents:
handlers = {SomeContainerClass: iter,
OtherContainerClass: OtherContainerClass.get_elements}
"""
from sys import getsizeof
from itertools import chain
from collections import deque
dict_handler = lambda d: chain.from_iterable(d.items())
all_handlers = {tuple: iter,
list: iter,
deque: iter,
dict: dict_handler,
set: iter,
frozenset: iter,
}
all_handlers.update(handlers) # user handlers take precedence
seen = set() # track which object id's have already been seen
default_size = getsizeof(0) # estimate sizeof object without __sizeof__
def sizeof(o):
from sys import getsizeof, stderr
try:
from reprlib import repr
except ImportError:
pass
if id(o) in seen: # do not double count the same object
return 0
seen.add(id(o))
s = getsizeof(o, default_size)
if verbose:
print(s, type(o), repr(o))
for typ, handler in all_handlers.items():
if isinstance(o, typ):
s += sum(map(sizeof, handler(o)))
break
return s
return sizeof(o)
total_size = staticmethod(total_size)
# take from: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
which = staticmethod(which)
def kill(pid=None):
'''Kills the current program.'''
import sys
if not pid:
pid = os.getpid()
pid = int(pid)
if os.name =='nt':
# for windows
python = sys.executable
python = python.replace('\\','/')
import subprocess
subprocess.Popen([python, sys.argv])
kill = KillProcessThread(pid)
kill.start()
else:
os.kill(pid, 1)
kill = staticmethod(kill)
class KillProcessThread(threading.Thread):
'''Kill a Windows process'''
def __init__(my, pid):
super(KillProcessThread, my).__init__()
my.pid = pid
def run(my):
"""kill function for Win32 prior to Python2.7"""
import time
# pause for the DbConfigSaveCbk to finish first
time.sleep(2)
import ctypes
kernel32 = ctypes.windll.kernel32
handle = kernel32.OpenProcess(1, 0, my.pid)
return (0 != kernel32.TerminateProcess(handle, 0))
gl = globals()
lc = locals()
import binascii, pickle
try:
import zlib
HAS_ZLIB = True
except ImportError:
HAS_ZLIB = False
class Marshaller:
'''Marshals object creation to send over the web for dynamic creation.
The problem with pickling is that it is not really web friendly. Nor
is it encrypted (not that encryption is all that important or secure here).
The detail of the communication protocol, is completely
encapsulated in this class and should never be exposed externally
NOTE: this could be replaced by AJAX which uses a standard XML interface
to marshal function calls to the server
'''
def __init__(my, class_path=None):
my.set_class(class_path)
my.options = {}
my.args = []
my.kwargs = {}
def set_class(my, class_path):
if not class_path:
my.class_path = None
elif type(class_path) in types.StringTypes:
my.class_path = class_path
elif type(class_path) == types.TypeType:
# do some wonky stuff
p = re.compile(r"<class '(.*)'>")
m = p.findall(str(class_path))
if not m:
raise Exception("Cannot find class name for: %s" % str(class_path))
my.class_path = m[0]
else:
my.class_path = Common.get_full_class_name(class_path)
def set_option(my, name, value):
my.options[name] = value
def add_arg(my, arg):
my.args.append(arg)
def add_kwarg(my, name, value):
my.kwargs[name] = value
def set_kwargs(my, kwargs):
# ensure all keywords are strings
my.kwargs = {}
for name, value in kwargs.items():
my.kwargs[str(name)] = value
#my.kwargs = kwargs
def get_object(my):
# dynamic creation using a string argument like the command line
(module_name, class_name) = Common.breakup_class_path(my.class_path)
unique_class_name = "%s%s" % (module_name.replace(".",""), class_name)
args = ",".join(["my.args[%s]" % i for i in range(0,len(my.args))] )
try:
if module_name.startswith("tactic.plugins."):
import tactic.plugins
parts = module_name.split(".")
plugin = parts[2]
parts.append(class_name)
rest = ".".join( parts[3:] )
module = tactic.plugins.import_plugin(plugin)
unique_class_name = "module.%s" % rest
if my.kwargs and args:
object = eval("%s(%s, **my.kwargs)" % (unique_class_name, args))
elif my.kwargs:
object = eval("%s(**my.kwargs)" % (unique_class_name) )
else:
object = eval("%s(%s)" % (unique_class_name, args) )
except NameError:
if module_name != "":
try:
#print( "from %s import %s as %s" % (module_name,class_name, unique_class_name))
exec( "from %s import %s as %s" % (module_name,class_name, unique_class_name), gl, lc )
except:
print ImportError("Cannot import [%s] from module [%s]" % (class_name, module_name) )
raise
else:
# standard libraries to import for dynamic loading
# TODO: This dynamic loading of all of these libraries imparts
# significant overhead when ihooks are used. It is probably
# better to be explicit with all class names
try:
exec("from pyasm.widget import %s" % class_name, gl, lc)
except ImportError:
try:
exec("from pyasm.command import %s" % class_name, gl, lc)
except ImportError:
try:
exec("from pyasm.web import %s" % class_name, gl, lc)
except ImportError:
raise ImportError("Could not find '%s' for import" % my.class_path)
# now with module loaded, instantiate again
try:
if my.kwargs and args:
object = eval("%s(%s, **my.kwargs)" % (unique_class_name, args) )
elif my.kwargs: