-
Notifications
You must be signed in to change notification settings - Fork 0
/
luddite.py
executable file
·403 lines (348 loc) · 15.4 KB
/
luddite.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env python
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is Komodo code.
#
# The Initial Developer of the Original Code is ActiveState Software Inc.
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
# ActiveState Software Inc. All Rights Reserved.
#
# Contributor(s):
# ActiveState Software Inc
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
r"""A command-line interface for managing UDL (User-Defined Language)
files for Komodo.
"""
import os
from os.path import basename, dirname, join, exists, abspath, splitext
import sys
import re
import logging
from glob import glob
from pprint import pprint
import webbrowser
def _set_lib_path():
if exists(join(dirname(__file__), "ludditelib")): # dev layout
pass
else: # install layout (in Komodo SDK)
sys.path.insert(0, join(dirname(dirname(abspath(__file__))), "pylib"))
_set_lib_path()
from ludditelib import cmdln
from ludditelib import parser, gen, constants, commands
from ludditelib.common import LudditeError, __version__, \
guid_pat, norm_guid, generate_guid
log = logging.getLogger("luddite")
#---- command line interface
class Shell(cmdln.Cmdln):
"""
luddite: build and package Komodo language extensions
usage:
${name} SUBCOMMAND [ARGS...]
${name} help SUBCOMMAND
Language syntax-highlighting in Komodo requires a lexer. For most of
Komodo's core supported languages these lexers are written in C++.
However, as of Komodo 4, you can define custom lexers for languages that
Komodo doesn't support out of the box. This system is called UDL -- for
User-Defined Languages.
Luddite is a tool for building and packaging a Komodo language
extension. The typical process is:
1. Use the 'koext' tool (also part of the Komodo SDK) to start a Komodo
extension source tree and create stub files for a new Komodo language.
koext startext fuzzy_language
cd fuzzy_language/
koext startlang fuzzy
2. Author the 'udl/LANG-mainlex.udl' file defining syntax highlighting
rules for your language and the 'components/koFuzzy_UDL_Language.py'
language service as appropriate.
The 'luddite lex' and 'luddite lexhtml' commands can help you debug
your .udl code.
3. Build the extension. 'koext' knows how to compile your UDL code into
the '.lexres' files that Komodo uses at runtime.
koext build
4. Upload and announce your new extension on Komodo's add-ons site:
http://community.activestate.com/addons
For more information on writing .udl files see Komodo's UDL
documentation.
${option_list}
${command_list}
${help_list}
"""
name = "luddite"
version = __version__
helpindent = ' '*2
def get_optparser(self):
parser = cmdln.Cmdln.get_optparser(self)
parser.add_option("-v", "--verbose", dest="log_level",
action="store_const", const=logging.DEBUG,
help="more verbose output")
parser.add_option("-q", "--quiet", dest="log_level",
action="store_const", const=logging.WARNING,
help="quieter output")
parser.set_defaults(log_level=logging.INFO)
return parser
def postoptparse(self):
global log
log.setLevel(self.options.log_level)
@cmdln.option("-d", "--output-dir",
help="specify the output dir for the .lexres file (by "
"default the same dir as the input .udl path")
@cmdln.option("-I", dest="include_dirs", action="append",
help="extra dirs to search for include'd UDL files")
def do_just_compile(self, subcmd, opts, udl_path):
"""${cmd_name}: compile a .udl file into a .lexres file
${cmd_usage}
${cmd_option_list}
Note: This is a replacement for the deprecated `luddite compile'. In
a subsequent release this will get the name `luddite compile'.
"""
return commands.compile(
udl_path, output_dir=opts.output_dir,
include_path=opts.include_dirs, log=log)
@cmdln.option("--ext",
help="specify a default file extension for this language")
@cmdln.option("-g", "--guid",
help="specify an XPCOM component GUID to use, or a path "
"to GUIDs text file")
@cmdln.option("-G", dest="new_guid", action="store_true",
help="create a new XPCOM component GUID for this build")
@cmdln.option("--skel", action="store_true",
help="also build skeleton Language Service and template files")
@cmdln.option("--add-missing", action="store_true",
help="only add in missing files for skeleton")
@cmdln.option("-f", "--force", action="store_true",
help="allow build outputs to overwrite existing files")
@cmdln.alias("compile")
def do_deprecated_compile(self, subcmd, opts, udl_path):
"""${cmd_name}: compile a .udl file into lang resources
Note: This has been deprecated in favour of `luddite just_compile'
and the more generic functionality of the 'koext' tool.
${cmd_usage}
${cmd_option_list}
If you specify '--skel' to build a skeleton Language Service
then you must also specify one of the -G or -g|--guid options.
The Language Service is a Python file that controls language
support in Komodo. It requires a unique GUID (for the XPCOM
component's class id).
If you are just building the skeleton language service for every
build (you can get away with this for minimal language support)
the using a GUIDs text file to ensure the same GUID is used for
your component from build to build is recommended. This file
must be of the form (one entry per line):
<lang> <guid>
"""
log.warn("the 'skel' generation facilities of 'luddite compile' "
"have been deprecated in favour of (a) the simpler "
"'luddite just_compile' and (b) the more generic support "
"of the 'koext' tool")
guid = guid_from_lang = None
if not opts.skel:
if opts.add_missing:
raise Luddite("cannot specify --add-missing without --skel")
elif opts.new_guid and opts.guid:
raise LudditeError("cannot specify both -G and -g|--guid "
"options at the same time")
elif opts.new_guid:
guid = None
elif opts.guid:
if guid_pat.match(opts.guid):
guid = norm_guid(opts.guid)
else:
if not exists(opts.guid):
raise LudditeError("`%s' is not a GUID and does not "
"exist" % opts.guid)
guid_from_lang = {} # dict((lang, g) for lang, g in)
for line in file(opts.guid):
if line.startswith("#"):
continue
if not line.strip():
continue
lang, g = line.strip().split(None, 1)
guid_from_lang[lang] = norm_guid(g)
else:
raise LudditeError("must specify one of -G or -g|--guid")
if opts.force and opts.add_missing:
raise LudditeError("cannot specify both -f|--force and "
"--add-missing options at the same time")
return commands.deprecated_compile(
udl_path, skel=opts.skel, guid=guid,
guid_from_lang=guid_from_lang, ext=opts.ext,
force=opts.force, add_missing=opts.add_missing, log=log)
def _do_parse(self, subcmd, opts, *udl_paths):
"""${cmd_name}: parse the given .udl file (for debugging)
${cmd_usage}
${cmd_option_list}
"""
for udl_path in udl_paths:
tree = commands.parse(udl_path, log=log)
pprint(tree)
@cmdln.option("-f", "--force", action="store_true",
help="allow build outputs to overwrite existing files")
@cmdln.option("--id", action="store",
help="the internal identifier for this extension (a short string)")
@cmdln.option("--name", action="store",
help="the extension name")
@cmdln.option("--description", action="store",
help="a short description of the extension")
@cmdln.option("-c", "--creator", action="store",
help="the name of the creator/maintainer of this extension")
@cmdln.option("-V", "--version", action="store",
help="the extension version")
@cmdln.alias("package")
def do_deprecated_package(self, subcmd, opts, lang):
"""${cmd_name}: package Komodo lang resources into an extension
Note: This has been deprecated in favour of `luddite just_compile'
and the more generic functionality of the 'koext' tool.
${cmd_usage}
${cmd_option_list}
You must first have run '${name} compile ...' to build the Komodo
resources for this language.
Typical usage should specify the "creator" and "version". The other
values have reasonable defaults. For example:
${name} package -c "Santa Claus" --version "2.0.1" Toy
${name} package -c "Larry Wall" --version "0.8.0" Perl
(However, Komodo already has a Perl lexer so creating your own
UDL-based one for Perl is just for the masochists.)
"""
return commands.deprecated_package(
lang, version=opts.version,
creator=opts.creator, name=opts.name,
description=opts.description, id=opts.id,
force=opts.force, log=log)
def do_lex(self, subcmd, opts, lang, path):
"""${cmd_name}: lex the given file (for debugging)
${cmd_usage}
${cmd_option_list}
Lex the given path with the UDL-based lexer for the given language,
printing a summary to stdout. This is for debugging a .udl file
during development.
"""
from ludditelib.debug import lex
content = open(path, 'r').read()
lex(content, lang)
@cmdln.option("-o", "--output",
help="path to which to write HTML output (instead of "
"PATH.html, use '-' for stdout)")
@cmdln.option("-b", "--browse", action="store_true",
help="open output file in browser")
def do_lexhtml(self, subcmd, opts, lang, path):
"""${cmd_name}: lex the given file to styled HTML (for debugging)
${cmd_usage}
${cmd_option_list}
"""
from ludditelib.debug import lex_to_html
content = open(path, 'r').read()
html = lex_to_html(content, lang)
if opts.output == '-':
output_path = None
output_file = sys.stdout
else:
if opts.output:
output_path = opts.output
else:
output_path = path+".html"
if exists(output_path):
os.remove(output_path)
output_file = open(output_path, 'w')
if output_file:
output_file.write(html)
if output_path:
output_file.close()
if opts.browse:
if not output_path:
raise LudditeError("cannot open in browser if stdout used "
"for output")
import webbrowser
url = _url_from_local_path(output_path)
webbrowser.open_new(url)
#---- internal support stuff
# Recipe: pretty_logging (0.1) in C:\trentm\tm\recipes\cookbook
class _PerLevelFormatter(logging.Formatter):
"""Allow multiple format string -- depending on the log level.
A "fmtFromLevel" optional arg is added to the constructor. It can be
a dictionary mapping a log record level to a format string. The
usual "fmt" argument acts as the default.
"""
def __init__(self, fmt=None, datefmt=None, fmtFromLevel=None):
logging.Formatter.__init__(self, fmt, datefmt)
if fmtFromLevel is None:
self.fmtFromLevel = {}
else:
self.fmtFromLevel = fmtFromLevel
def format(self, record):
record.levelname = record.levelname.lower()
if record.levelno in self.fmtFromLevel:
# XXX This is a non-threadsafe HACK. Really the base Formatter
# class should provide a hook accessor for the _fmt
# attribute. *Could* add a lock guard here (overkill?).
_saved_fmt = self._fmt
self._fmt = self.fmtFromLevel[record.levelno]
try:
return logging.Formatter.format(self, record)
finally:
self._fmt = _saved_fmt
else:
return logging.Formatter.format(self, record)
def _setup_logging():
hdlr = logging.StreamHandler()
defaultFmt = "%(name)s: %(levelname)s: %(message)s"
infoFmt = "%(name)s: %(message)s"
fmtr = _PerLevelFormatter(fmt=defaultFmt,
fmtFromLevel={logging.INFO: infoFmt})
hdlr.setFormatter(fmtr)
logging.root.addHandler(hdlr)
log.setLevel(logging.INFO)
# log.setLevel(logging.DEBUG)
def _url_from_local_path(local_path):
# HACKy: This isn't super-robust.
from os.path import abspath, normpath
url = normpath(abspath(local_path))
if sys.platform == "win32":
url = "file:///" + url.replace('\\', '/')
else:
url = "file://" + url
return url
#---- mainline
if __name__ == "__main__":
_setup_logging() # defined in recipe:pretty_logging
try:
shell = Shell()
retval = shell.main(sys.argv)
except KeyboardInterrupt:
sys.exit(1)
except:
exc_info = sys.exc_info()
if hasattr(exc_info[0], "__name__"):
log.error("%s: %s", exc_info[0].__name__, exc_info[1])
else: # string exception
log.error(exc_info[0])
if log.isEnabledFor(logging.DEBUG):
import traceback
print
traceback.print_exception(*exc_info)
sys.exit(1)
else:
sys.exit(retval)