comparison env/lib/python3.7/site-packages/planemo/conda_verify/utils.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 import collections
2 import sys
3
4 from planemo.conda_verify.const import (
5 DLL_TYPES,
6 MAGIC_HEADERS,
7 )
8
9
10 def get_object_type(data):
11 head = data[:4]
12 if head not in MAGIC_HEADERS:
13 return None
14 lookup = MAGIC_HEADERS.get(head)
15 if lookup == 'DLL':
16 pos = data.find('PE\0\0')
17 if pos < 0:
18 return "<no PE header found>"
19 i = ord(data[pos + 4]) + 256 * ord(data[pos + 5])
20 return "DLL " + DLL_TYPES.get(i)
21 elif lookup.startswith('MachO'):
22 return lookup
23 elif lookup == 'ELF':
24 return "ELF" + {'\x01': '32', '\x02': '64'}.get(data[4])
25
26
27 def get_bad_seq(s):
28 for seq in ('--', '-.', '-_',
29 '.-', '..', '._',
30 '_-', '_.'): # but '__' is fine
31 if seq in s:
32 return seq
33 return None
34
35
36 def all_ascii(data, allow_CR=False):
37 newline = [10] # LF
38 if allow_CR:
39 newline.append(13) # CF
40 for c in data:
41 n = ord(c) if sys.version_info[0] == 2 else c
42 if not (n in newline or 32 <= n < 127):
43 return False
44 return True
45
46
47 class memoized(object):
48 """Decorator. Caches a function's return value each time it is called.
49 If called later with the same arguments, the cached value is returned
50 (not reevaluated).
51 """
52 def __init__(self, func):
53 self.func = func
54 self.cache = {}
55
56 def __call__(self, *args):
57 if not isinstance(args, collections.Hashable):
58 # uncacheable. a list, for instance.
59 # better to not cache than blow up.
60 return self.func(*args)
61 if args in self.cache:
62 return self.cache[args]
63 else:
64 value = self.func(*args)
65 self.cache[args] = value
66 return value
67
68
69 if __name__ == '__main__':
70 print(sys.version)
71 print(all_ascii(b'Hello\x00'), all_ascii(b"Hello World!"))