-
Notifications
You must be signed in to change notification settings - Fork 2
/
ah.py
executable file
·96 lines (84 loc) · 2.85 KB
/
ah.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
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
#!/usr/bin/python
from autohighlight import Autohighlight
import hotshot, hotshot.stats
from memoize import memoize
import sys
import getopt
import re
from symbol import Symbol
from vimoutputter import VimOutputter
from EmacsOutputter import EmacsOutputter
from utils import Set
def usage():
print """Usage: python ah.py [OPTION]... [FILE]
Generates the specified syntax highlighting files from the given input FILE.
Options:
-h, --help Prints this help
--vim Generates a vim syntax highlighting file
--emacs Generates an emacs font locking file
--error-checking Highlight all symbols not currently being colored as errors (currently works for vim only)
"""
def strip_filename(filename):
return re.sub('\.ah$','',filename)
def generate_vim(filename,error_checking):
output_filename = strip_filename(filename) + '.vim'
print 'Generating vim file %s\n' % output_filename
ah = Autohighlight(filename)
ah.parse()
outputter = VimOutputter(error_checking)
open(output_filename,'w').write(ah.output(outputter))
def generate_emacs(filename):
import os.path
output_filename = strip_filename(filename) + '.el'
ah = Autohighlight(filename)
ah.parse()
outputter = EmacsOutputter(os.path.basename(strip_filename(filename)))
open(output_filename,'w').write(ah.output(outputter))
print "Wrote %s\n" % output_filename
def main_interface():
profile = False
help, vim, emacs, error_checking = False, False, False, False
opts, args = getopt.getopt(sys.argv[1:-1], "h", \
["help", "vim", "emacs", "error-checking", \
"memoize", "profile"])
for opt, arg in opts:
if opt in ("-h", "--help"):
help = True
elif opt == '--vim':
vim = True
elif opt == '--memoize':
memoize(Symbol.get_terminal_equivalent_regexes)
memoize(Symbol.getRightRegexes)
memoize(Symbol.getLeftRegexes)
memoize(Symbol.get_contexts)
elif opt == '--profile':
profile = True
elif opt == '--emacs':
emacs = True
elif opt == '--error-checking':
error_checking = True
filename=sys.argv[-1]
def do_it():
if(help):
usage()
sys.exit()
if(vim):
generate_vim(filename,error_checking)
if(emacs):
generate_emacs(filename)
if(not emacs and not vim):
usage()
sys.exit(2)
if profile:
fn = "sample%s.prof" % "".join(sys.argv[1:-1])
prof = hotshot.Profile(fn)
prof.runcall(do_it)
prof.close()
stats = hotshot.stats.load(fn)
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats()
else:
do_it()
if __name__ == "__main__":
main_interface()