MCPcopy Create free account
hub / github.com/SySS-Research/hallucinate / create_script_source

Function create_script_source

hallucinate/script.py:30–77  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

28
29
30def create_script_source(args):
31 scriptsrc = ''
32 options = {
33 'debug': False,
34 'replaceBuffer': args.replaceBuffer,
35 'map': {}
36 }
37 if args.verbose >= 2:
38 options['debug'] = True
39
40 if args.mapfile is not None:
41 with open(args.mapfile, 'rb') as f:
42 options['map'] = json.load(f)
43
44 scriptsrc += 'const options = ' + json.dumps(options) + ';'
45 mypath = os.path.realpath(os.path.dirname(__file__))
46 with open(mypath + os.path.sep + 'utils.js', 'r') as f:
47 scriptsrc += f.read()
48
49 # Avoid scope collisions in scripts
50 def wrap_module(modname, source):
51 return """
52 // from module %s
53 (function() {
54 %s
55 }());
56 """ % (modname, source)
57
58 def add_dir(root, predicate):
59 for name in os.listdir(root):
60 p = os.path.join(root, name)
61 if not os.path.isfile(p):
62 continue
63 if not predicate(name):
64 logging.debug('Disabled module %s', name)
65 continue
66
67 logging.debug('Adding module %s', name)
68 with open(p, 'r') as f:
69 yield wrap_module(name, f.read())
70
71 for source in add_dir(mypath + os.path.sep + 'modules/', lambda x: x not in args.disable):
72 scriptsrc += source
73
74 for source in add_dir(mypath + os.path.sep + 'modules/optional', lambda x: x in args.enable):
75 scriptsrc += source
76
77 return scriptsrc

Callers 1

create_scriptFunction · 0.85

Calls 1

add_dirFunction · 0.85

Tested by

no test coverage detected