From 56dc9c74328c3401c6ee7e4c63ca339f9a5dc337 Mon Sep 17 00:00:00 2001 From: Robert Moggach Date: Wed, 15 Feb 2012 22:37:29 -0500 Subject: [PATCH] made introspection less project specific --- MANIFEST | 6 ++-- README.rst | 6 ++-- dirtt/__init__.py | 3 +- dirtt/data/dirtt.dtd | 1 - dirtt/scripts/mkdirtt.py | 42 ---------------------- dirtt/scripts/{mkproj.py => mkproject.py} | 9 ++--- dirtt/scripts/mktemplate.py | 43 +++++++++++++++++++++++ dirtt/scripts/{mkdirt.py => mktree.py} | 10 +++--- dirtt/util/introspection.py | 2 +- dirtt/util/template.py | 21 +++++++++-- docs/index.rst | 6 ++-- setup.py | 1 + 12 files changed, 85 insertions(+), 65 deletions(-) delete mode 100755 dirtt/scripts/mkdirtt.py rename dirtt/scripts/{mkproj.py => mkproject.py} (95%) create mode 100755 dirtt/scripts/mktemplate.py rename dirtt/scripts/{mkdirt.py => mktree.py} (93%) diff --git a/MANIFEST b/MANIFEST index 7b511b9..f3aa5c1 100755 --- a/MANIFEST +++ b/MANIFEST @@ -19,9 +19,9 @@ dirtt/data/templates/project_sequence.xml dirtt/data/templates/project_shot.xml dirtt/data/templates/project_work.xml dirtt/data/templates/workspace.mel -dirtt/scripts/mkdirt -dirtt/scripts/mkdirtt -dirtt/scripts/mkproj +dirtt/scripts/mkproject.py +dirtt/scripts/mktemplate.py +dirtt/scripts/mktree.py docs/index.rst docs/news.rst tests/__init__.py diff --git a/README.rst b/README.rst index 819ae8e..d544a82 100755 --- a/README.rst +++ b/README.rst @@ -71,12 +71,12 @@ Forking through Bitbucket ~~~~~~~~~~~~~~~~~~~~~~~~~ First of all, you need to fork from the official repository, which is -`https://bitbucket.org/mogga/python-dirtt `_. +`https://github.com/dshng/python-dirtt `_. Log in to Bitbucket, go to the dirtt repository page, follow the fork link, wait for Bitbucket to copy the repository and then clone your fork, like: - ``git clone https://bitbucket.org/YOUR_USER_NAME/python-dirtt`` + ``git clone https://github.com/YOUR_USER_NAME/python-dirtt`` Now you can change whatever you want, commit, push to your fork and when your contribution is done, follow the pull request link and send us a @@ -221,5 +221,5 @@ Links Here's the links: -`Bitbucket `_ +`Bitbucket `_ diff --git a/dirtt/__init__.py b/dirtt/__init__.py index 9c4dd20..08bbeb8 100755 --- a/dirtt/__init__.py +++ b/dirtt/__init__.py @@ -35,7 +35,6 @@ def get_version(): import os - from xml.etree import ElementTree #from xml.sax import make_parser from xml.sax import parseString @@ -51,6 +50,7 @@ def get_version(): TEMPLATES_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data', 'templates') + def list_available_templates(): print "\n These are the available templates. Reference using the full path provided." template_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data', 'templates') @@ -69,6 +69,7 @@ def list_available_templates(): # template_list.append(os.path.join(root,file)) # print " %s" % os.path.join(root, file) + class DirectoryTreeHandler(ContentHandler): """ Main SAX Interface for handling directory tree XML templates diff --git a/dirtt/data/dirtt.dtd b/dirtt/data/dirtt.dtd index c7b6f5c..e2746ea 100755 --- a/dirtt/data/dirtt.dtd +++ b/dirtt/data/dirtt.dtd @@ -61,4 +61,3 @@ idref IDREF #IMPLIED ref CDATA #IMPLIED dirname CDATA #IMPLIED> - diff --git a/dirtt/scripts/mkdirtt.py b/dirtt/scripts/mkdirtt.py deleted file mode 100755 index a969920..0000000 --- a/dirtt/scripts/mkdirtt.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python - -""" -python-dirtt - Directory Tree Templater -(c) 2012 Dashing Collective Inc. and contributors -Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php - - btdirt - - This is a generic command line tool to create a project template from an - existing source tree -""" -import os -import sys -from optparse import OptionParser -from dirtt.util.introspection import * - - -def main(): - usage = "usage: %prog [-d PROJECT_ROOT]" - version=__import__('dirtt').get_version() - description="""Create a project template from an existing source tree.""" - parser = OptionParser(usage=usage, version=version, description=description) - parser.add_option("-d", "--project-path", dest="project_path", help="Full path to project location.") - (options, args) = parser.parse_args() - if options.project_path: - project_path = options.project_path - else: - project_path = None - print "You must specify a project path with -d or --project-path to run this script." - sys.exit(-6) - - pbuilder = ProjectBuilder(project_path) - pbuilder.build_template() - sys.exit(0) - - -if __name__ == "__main__": - main() - - - diff --git a/dirtt/scripts/mkproj.py b/dirtt/scripts/mkproject.py similarity index 95% rename from dirtt/scripts/mkproj.py rename to dirtt/scripts/mkproject.py index 3bf35a2..92e3ec8 100755 --- a/dirtt/scripts/mkproj.py +++ b/dirtt/scripts/mkproject.py @@ -1,11 +1,11 @@ #!/usr/bin/python """ -python-dirtt - Directory Tree Templater -(c) 2012 Dashing Collective Inc. and contributors -Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php + python-dirtt - Directory Tree Templater + (c) 2012 Dashing Collective Inc. and contributors + Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php - mkproj.py + mkproject.py This is a generic command line tool that prompts for template variables in a given template and renders the tree. @@ -31,6 +31,7 @@ SEQUENCE_TEMPLATE=os.path.join(TEMPLATE_DIR,"project_sequence.xml") SHOT_TEMPLATE=os.path.join(TEMPLATE_DIR,"project_shot.xml") + def main(): usage = "usage: %prog [-t TEMPLATE]" version=__import__('dirtt').get_version() diff --git a/dirtt/scripts/mktemplate.py b/dirtt/scripts/mktemplate.py new file mode 100755 index 0000000..956da59 --- /dev/null +++ b/dirtt/scripts/mktemplate.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +""" + python-dirtt - Directory Tree Templater + (c) 2012 Dashing Collective Inc. and contributors + Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php + + mktemplate.py + + This is a generic command line tool to create a tree template from an + existing source path + +""" + +import os +import sys +from optparse import OptionParser +from dirtt.util.introspection import * + + +def main(): + usage = "usage: %prog [-p SOURCE_PATH]" + version=__import__('dirtt').get_version() + description="""Return an XML directory template from an existing directory tree.""" + parser = OptionParser(usage=usage, version=version, description=description) + parser.add_option("-p", "--path", dest="source_path", help="Absolute path to source tree.") + (options, args) = parser.parse_args() + if options.project_path: + project_path = options.project_path + else: + project_path = None + print "\n You must specify a source path to introspect\n with -p or --path to run this script." + sys.exit(-6) + introspected_tree = TreeIntrospector(project_path) + introspected_tree.build_template() + sys.exit(0) + + +if __name__ == "__main__": + main() + + + diff --git a/dirtt/scripts/mkdirt.py b/dirtt/scripts/mktree.py similarity index 93% rename from dirtt/scripts/mkdirt.py rename to dirtt/scripts/mktree.py index 815b693..3aa1a55 100755 --- a/dirtt/scripts/mkdirt.py +++ b/dirtt/scripts/mktree.py @@ -1,11 +1,11 @@ #!/usr/bin/python """ -python-dirtt - Directory Tree Templater -(c) 2012 Dashing Collective Inc. and contributors -Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php + python-dirtt - Directory Tree Templater + (c) 2012 Dashing Collective Inc. and contributors + Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php - mkdirt.py + mktree.py This is a generic command line tool that prompts for template variables in a given template and renders the tree. @@ -15,12 +15,12 @@ import os import sys from optparse import OptionParser - from dirtt import DirectoryTreeHandler, list_available_templates from dirtt.util.template import return_placeholders ENABLED_USERS = [0,1111,1003] + def main(): usage = "usage: %prog [-t TEMPLATE]" version=__import__('dirtt').get_version() diff --git a/dirtt/util/introspection.py b/dirtt/util/introspection.py index 3fe0f89..040b33a 100644 --- a/dirtt/util/introspection.py +++ b/dirtt/util/introspection.py @@ -15,7 +15,7 @@ from xml.dom.minidom import Document from stat import * -class ProjectBuilder: +class TreeIntrospector: def __init__(self, base="."): self.base = os.path.realpath(base) diff --git a/dirtt/util/template.py b/dirtt/util/template.py index b753f9e..0bf2a1f 100755 --- a/dirtt/util/template.py +++ b/dirtt/util/template.py @@ -33,14 +33,15 @@ def foo(bar): If there are syntax errors ``TemplateError`` will be raised. """ + import re import sys import cgi import urllib import looper -__all__ = ['TemplateError', 'Template', 'sub', 'HTMLTemplate', - 'sub_html', 'html', 'bunch'] + +__all__ = ['TemplateError', 'Template', 'sub', 'HTMLTemplate', 'sub_html', 'html', 'bunch'] token_re = re.compile(r'\{\{|\}\}') in_re = re.compile(r'\s+in\s+') @@ -311,6 +312,7 @@ def __repr__(self): self.__class__.__name__, ' '.join(['%s=%r' % (k, v) for k, v in items])) + ############################################################ ## HTML Templating ############################################################ @@ -324,6 +326,7 @@ def __repr__(self): return '<%s %r>' % ( self.__class__.__name__, self.value) + def html_quote(value): if value is None: return '' @@ -337,6 +340,7 @@ def html_quote(value): value = value.encode('ascii', 'xmlcharrefreplace') return value + def url(v): if not isinstance(v, basestring): if hasattr(v, '__unicode__'): @@ -347,6 +351,7 @@ def url(v): v = v.encode('utf8') return urllib.quote(v) + def attr(**kw): kw = kw.items() kw.sort() @@ -359,6 +364,7 @@ def attr(**kw): parts.append('%s="%s"' % (html_quote(name), html_quote(value))) return html(' '.join(parts)) + class HTMLTemplate(Template): default_namespace = Template.default_namespace.copy() @@ -375,6 +381,7 @@ def _repr(self, value, pos): else: return html_quote(plain) + def sub_html(content, **kw): name = kw.get('__name') tmpl = HTMLTemplate(content, name=name) @@ -390,6 +397,7 @@ def f7(seq): S,L = set, list return S(L(seq)) + def return_placeholders(s, name=None): in_expr = False vars = [] @@ -478,6 +486,7 @@ def lex(s, name=None, trim_whitespace=True): trail_whitespace_re = re.compile(r'\n[\t ]*$') lead_whitespace_re = re.compile(r'^[\t ]*\n') + def trim_lex(tokens): r""" Takes a lexed set of tokens, and removes whitespace when there is @@ -527,6 +536,7 @@ def find_position(string, index): leading = string[:index].splitlines() return (len(leading), len(leading[-1])+1) + def parse(s, name=None): r""" Parses a string into a kind of AST @@ -584,6 +594,7 @@ def parse(s, name=None): result.append(next) return result + def parse_expr(tokens, name, context=()): if isinstance(tokens[0], basestring): return tokens[0], tokens[1:] @@ -628,6 +639,7 @@ def parse_expr(tokens, name, context=()): return ('comment', pos, tokens[0][0]), tokens[1:] return ('expr', pos, tokens[0][0]), tokens[1:] + def parse_cond(tokens, name, context): start = tokens[0][1] pieces = [] @@ -643,6 +655,7 @@ def parse_cond(tokens, name, context): next, tokens = parse_one_cond(tokens, name, context) pieces.append(next) + def parse_one_cond(tokens, name, context): (first, pos), tokens = tokens[0], tokens[1:] content = [] @@ -669,6 +682,7 @@ def parse_one_cond(tokens, name, context): next, tokens = parse_expr(tokens, name, context) content.append(next) + def parse_for(tokens, name, context): first, pos = tokens[0] tokens = tokens[1:] @@ -703,6 +717,7 @@ def parse_for(tokens, name, context): next, tokens = parse_expr(tokens, name, context) content.append(next) + def parse_default(tokens, name, context): first, pos = tokens[0] assert first.startswith('default ') @@ -731,6 +746,7 @@ def parse_default(tokens, name, context): strings. """ + def fill_command(args=None): import sys, optparse, pkg_resources, os if args is None: @@ -793,6 +809,7 @@ def fill_command(args=None): else: sys.stdout.write(result) + if __name__ == '__main__': from dirtt.util.template import fill_command fill_command() diff --git a/docs/index.rst b/docs/index.rst index da5ba8b..59b37fc 100755 --- a/docs/index.rst +++ b/docs/index.rst @@ -73,12 +73,12 @@ Forking through Bitbucket ~~~~~~~~~~~~~~~~~~~~~~~~~ First of all, you need to fork from the official repository, which is -`https://bitbucket.org/mogga/python-dirtt `_. +`https://github.com/dshng/python-dirtt `_. Log in to Bitbucket, go to the dirtt repository page, follow the fork link, wait for Bitbucket to copy the repository and then clone your fork, like: - ``git clone https://bitbucket.org/YOUR_USER_NAME/python-dirtt`` + ``git clone https://github.com/YOUR_USER_NAME/python-dirtt`` Now you can change whatever you want, commit, push to your fork and when your contribution is done, follow the pull request link and send us a @@ -224,5 +224,5 @@ Links Here's the links: -`Bitbucket `_ +`Bitbucket `_ diff --git a/setup.py b/setup.py index fc3372c..e53bfb0 100755 --- a/setup.py +++ b/setup.py @@ -60,6 +60,7 @@ def fullsplit(path, result=None): root_dir = os.path.dirname(__file__) if root_dir != '': os.chdir(root_dir) + dirtt_dir = 'dirtt' for dirpath, dirnames, filenames in os.walk(dirtt_dir):