Mercurial > hg > MakeItSo
comparison makeitso/makeitso.py @ 138:169e02e190ef
peg to tempita version and eliminate cruft
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 21 Feb 2012 21:01:28 -0800 |
parents | 0107f95376b3 |
children | cfd4f1e91090 |
comparison
equal
deleted
inserted
replaced
137:2ffadba77bb6 | 138:169e02e190ef |
---|---|
14 import urllib | 14 import urllib |
15 # TODO: may have to use urllib2.urlopen to get reasonable timeouts | 15 # TODO: may have to use urllib2.urlopen to get reasonable timeouts |
16 | 16 |
17 from ConfigParser import RawConfigParser | 17 from ConfigParser import RawConfigParser |
18 from optparse import OptionParser | 18 from optparse import OptionParser |
19 | |
20 | 19 |
21 # URL of -this file- | 20 # URL of -this file- |
22 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py' | 21 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py' |
23 | 22 |
24 ### import tempita | 23 ### import tempita |
63 import tempita | 62 import tempita |
64 except: | 63 except: |
65 cleanup() | 64 cleanup() |
66 raise NotImplementedError('This should say something like youre not connected to the net') | 65 raise NotImplementedError('This should say something like youre not connected to the net') |
67 | 66 |
68 # does tempita support delimeters? | |
69 has_delimeters = 'delimeters' in inspect.getargspec(tempita.Template.__init__).args | |
70 | |
71 # regular expressions for finding the shebang | 67 # regular expressions for finding the shebang |
72 shebang_re = '#!.*makeitso.*' | 68 shebang_re = '#!.*makeitso.*' |
73 shebang_re = re.compile(shebang_re) | 69 shebang_re = re.compile(shebang_re) |
74 | 70 |
75 ### URIs | 71 ### URIs |
76 | 72 |
77 def parent_uri(uri): | 73 def parent_uri(uri): |
78 """parent resource of a URI""" | 74 """parent resource of a URI""" |
79 | 75 |
80 if '://' in uri: | 76 if '://' in uri: |
81 return uri.rsplit('/', 1)[0] + '/' | 77 return uri.rsplit('/', 1)[0] + '/' |
82 else: | 78 else: |
83 here = os.path.dirname(os.path.abspath(uri)) | 79 here = os.path.dirname(os.path.abspath(uri)) |
84 here = here.rstrip(os.path.sep) + os.path.sep | 80 here = here.rstrip(os.path.sep) + os.path.sep |
376 os.makedirs(output) | 372 os.makedirs(output) |
377 | 373 |
378 # do the substitution | 374 # do the substitution |
379 for template in self.templates: | 375 for template in self.templates: |
380 template.substitute(vars, output) | 376 template.substitute(vars, output) |
381 | 377 |
382 ### command line interface | 378 ### command line interface |
383 | 379 |
384 def read_config(config_files, templates=()): | 380 def read_config(config_files, templates=()): |
385 """read variables from a set of .ini files""" | 381 """read variables from a set of .ini files""" |
386 retval = {} | 382 retval = {} |
388 parser.read(config_files) | 384 parser.read(config_files) |
389 retval.update(dict(parser.items('DEFAULT'))) | 385 retval.update(dict(parser.items('DEFAULT'))) |
390 for template in templates: | 386 for template in templates: |
391 if template in parser.sections(): | 387 if template in parser.sections(): |
392 retval.update(dict(parser.items(template))) | 388 retval.update(dict(parser.items(template))) |
393 | |
394 return retval | 389 return retval |
395 | 390 |
396 def invocation(url, **variables): | 391 def invocation(url, **variables): |
397 """returns a string appropriate for TTW invocation""" | 392 """returns a string appropriate for TTW invocation""" |
398 variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()]) | 393 variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()]) |
409 if 'HOME' in os.environ: | 404 if 'HOME' in os.environ: |
410 dotfile = os.path.join(os.environ['HOME'], '.makeitso') | 405 dotfile = os.path.join(os.environ['HOME'], '.makeitso') |
411 if not (os.path.exists(dotfile) and os.path.isfile(dotfile)): | 406 if not (os.path.exists(dotfile) and os.path.isfile(dotfile)): |
412 dotfile = None | 407 dotfile = None |
413 | 408 |
414 # delimeters | 409 parser.add_option('-[', '--start-braces', dest='start_braces', |
415 # XXX needs tempita trunk | 410 help='starting delimeter') |
416 if has_delimeters: | 411 parser.add_option('-]', '--end-braces', dest='end_braces', |
417 parser.add_option('-[', '--start-braces', dest='start_braces', | 412 help='starting delimeter') |
418 help='starting delimeter') | |
419 parser.add_option('-]', '--end-braces', dest='end_braces', | |
420 help='starting delimeter') | |
421 | 413 |
422 # options about where to put things | 414 # options about where to put things |
423 parser.add_option('-o', '--output', dest='output', | 415 parser.add_option('-o', '--output', dest='output', |
424 help='where to put the output (filename or directory)') | 416 help='where to put the output (filename or directory)') |
425 | 417 |
452 if entry_points: | 444 if entry_points: |
453 parser.add_option('--list-templates', dest='list_templates', | 445 parser.add_option('--list-templates', dest='list_templates', |
454 action='store_true', default=False, | 446 action='store_true', default=False, |
455 help="list installed MakeItSo! templates") | 447 help="list installed MakeItSo! templates") |
456 | 448 |
457 | |
458 options, args = parser.parse_args(args) | 449 options, args = parser.parse_args(args) |
459 | 450 |
460 # list the templates | 451 # list the templates |
461 if entry_points and options.list_templates: | 452 if entry_points and options.list_templates: |
462 for key in sorted(entry_points.keys()): | 453 for key in sorted(entry_points.keys()): |
525 template = ContentTemplate(sys.stdin.read(), variables=variables) | 516 template = ContentTemplate(sys.stdin.read(), variables=variables) |
526 print template.substitute() | 517 print template.substitute() |
527 | 518 |
528 # cleanup | 519 # cleanup |
529 cleanup() | 520 cleanup() |
530 | 521 |
531 if __name__ == '__main__': | 522 if __name__ == '__main__': |
532 main() | 523 main() |
533 | 524 |
534 | 525 |
535 # get templates from pkg_resources | 526 # get templates from pkg_resources |