-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.py
67 lines (62 loc) · 2.06 KB
/
parse.py
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
import sys
from script import Script
import re
import jsmin
import os
import config
def parse_parents(src):
for parent in src.parents:
parse_file(parent)
def parse_file(src):
"""
find file in config and output to dest dir
"""
#clear the stack between parses
if config.dest_dir == None:
dest = src.dir
else:
dest = config.dest_dir
output = get_output(src)
output_file = dest + '/' + src.basename + '.min.js'
f = open(output_file,'w')
f.write(jsmin.jsmin(output))
f.close()
print "Wrote combined and minified file to: %s" % (output_file)
def get_output(src):
"""
parse lines looking for commands
"""
output = ''
lines = open(src.path, 'rU').readlines()
for line in lines:
m = re.match(config.import_regex,line)
if m:
include_path = os.path.abspath(src.dir + '/' + m.group('script'));
if include_path not in config.sources:
script = Script(include_path)
config.sources[script.path] = script
include_file = config.sources[include_path]
#require statements dont include if the file has already been included
if include_file not in config.stack or m.group('command') == 'import':
config.stack.append(include_file)
output += get_output(include_file)
else:
output += line
return output
def execute(cmd, line):
"""
get output from command
"""
return
def parse_dependencies(script,context):
lines = open(script.path).read()
matches = re.finditer(config.import_regex,lines)
for match in matches:
# determine file path, recurse
src = Script(os.path.abspath(script.dir + '/' + match.group('script')))
if (src.path not in config.sources.keys()):
src.parents.append(context)
config.sources[src.path] = src
parse_dependencies(src,context)
elif (context not in config.sources[src.path].parents):
config.sources[src.path].parents.append(context)