corrade-nucleus-nucleons – Blame information for rev 4

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 office 1 #
2 # General code for JSBeautifier unpackers infrastructure. See README.specs
3 # written by Stefano Sanfilippo <a.little.coder@gmail.com>
4 #
5  
6 """General code for JSBeautifier unpackers infrastructure."""
7  
8 import pkgutil
9 import re
10 from jsbeautifier.unpackers import evalbased
11  
12 # NOTE: AT THE MOMENT, IT IS DEACTIVATED FOR YOUR SECURITY: it runs js!
13 BLACKLIST = ['jsbeautifier.unpackers.evalbased']
14  
15 class UnpackingError(Exception):
16 """Badly packed source or general error. Argument is a
17 meaningful description."""
18 pass
19  
20 def getunpackers():
21 """Scans the unpackers dir, finds unpackers and add them to UNPACKERS list.
22 An unpacker will be loaded only if it is a valid python module (name must
23 adhere to naming conventions) and it is not blacklisted (i.e. inserted
24 into BLACKLIST."""
25 path = __path__
26 prefix = __name__ + '.'
27 unpackers = []
28 interface = ['unpack', 'detect', 'PRIORITY']
29 for _importer, modname, _ispkg in pkgutil.iter_modules(path, prefix):
30 if 'tests' not in modname and modname not in BLACKLIST:
31 try:
32 module = __import__(modname, fromlist=interface)
33 except ImportError:
34 raise UnpackingError('Bad unpacker: %s' % modname)
35 else:
36 unpackers.append(module)
37  
38 return sorted(unpackers, key = lambda mod: mod.PRIORITY)
39  
40 UNPACKERS = getunpackers()
41  
42 def run(source, evalcode=False):
43 """Runs the applicable unpackers and return unpacked source as a string."""
44 for unpacker in [mod for mod in UNPACKERS if mod.detect(source)]:
45 source = unpacker.unpack(source)
46 if evalcode and evalbased.detect(source):
47 source = evalbased.unpack(source)
48 return source
49  
50 def filtercomments(source):
51 """NOT USED: strips trailing comments and put them at the top."""
52 trailing_comments = []
53 comment = True
54  
55 while comment:
56 if re.search(r'^\s*\/\*', source):
57 comment = source[0, source.index('*/') + 2]
58 elif re.search(r'^\s*\/\/', source):
59 comment = re.search(r'^\s*\/\/', source).group(0)
60 else:
61 comment = None
62  
63 if comment:
64 source = re.sub(r'^\s+', '', source[len(comment):])
65 trailing_comments.append(comment)
66  
67 return '\n'.join(trailing_comments) + source