-
Notifications
You must be signed in to change notification settings - Fork 22
Added Python 3 support #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ pip-log.txt | |
|
|
||
| # Unit test / coverage reports | ||
| .coverage | ||
| .cache | ||
|
|
||
| # Translations | ||
| *.mo | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| language: python | ||
|
|
||
| sudo: required | ||
| dist: trusty | ||
|
|
||
| python: | ||
| - 2.7 | ||
| - 3.6 | ||
|
|
||
| install: | ||
| - pip install pytest coveralls | ||
|
|
||
| script: | ||
| - PYTHONPATH=$(pwd)/source pytest test | ||
|
|
||
| after_success: | ||
| - coveralls |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,127 +2,31 @@ | |
| # :copyright: Copyright (c) 2013 Martin Pengelly-Phillips | ||
| # :license: See LICENSE.txt. | ||
|
|
||
| import os | ||
| import uuid | ||
| import imp | ||
|
|
||
| from ._version import __version__ | ||
| from .template import Template, Resolver | ||
| from .error import ParseError, FormatError, NotFound | ||
|
|
||
|
|
||
| def discover_templates(paths=None, recursive=True): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These were moved into template.py for consistency.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually had these in So I don't really see a benefit to changing this at this stage. |
||
| '''Search *paths* for mount points and load templates from them. | ||
|
|
||
| *paths* should be a list of filesystem paths to search for mount points. | ||
| If not specified will try to use value from environment variable | ||
| :envvar:`LUCIDITY_TEMPLATE_PATH`. | ||
|
|
||
| A mount point is a Python file that defines a 'register' function. The | ||
| function should return a list of instantiated | ||
| :py:class:`~lucidity.template.Template` objects. | ||
|
|
||
| If *recursive* is True (the default) then all directories under a path | ||
| will also be searched. | ||
|
|
||
| ''' | ||
| templates = [] | ||
|
|
||
| if paths is None: | ||
| paths = os.environ.get('LUCIDITY_TEMPLATE_PATH', '').split(os.pathsep) | ||
|
|
||
| for path in paths: | ||
| for base, directories, filenames in os.walk(path): | ||
| for filename in filenames: | ||
| _, extension = os.path.splitext(filename) | ||
| if extension != '.py': | ||
| continue | ||
|
|
||
| module_path = os.path.join(base, filename) | ||
| module_name = uuid.uuid4().hex | ||
| module = imp.load_source(module_name, module_path) | ||
| try: | ||
| registered = module.register() | ||
| except AttributeError: | ||
| pass | ||
| else: | ||
| if registered: | ||
| templates.extend(registered) | ||
|
|
||
| if not recursive: | ||
| del directories[:] | ||
|
|
||
| return templates | ||
|
|
||
|
|
||
| def parse(path, templates): | ||
| '''Parse *path* against *templates*. | ||
|
|
||
| *path* should be a string to parse. | ||
|
|
||
| *templates* should be a list of :py:class:`~lucidity.template.Template` | ||
| instances in the order that they should be tried. | ||
|
|
||
| Return ``(data, template)`` from first successful parse. | ||
|
|
||
| Raise :py:class:`~lucidity.error.ParseError` if *path* is not | ||
| parseable by any of the supplied *templates*. | ||
|
|
||
| ''' | ||
| for template in templates: | ||
| try: | ||
| data = template.parse(path) | ||
| except ParseError: | ||
| continue | ||
| else: | ||
| return (data, template) | ||
|
|
||
| raise ParseError( | ||
| 'Path {0!r} did not match any of the supplied template patterns.' | ||
| .format(path) | ||
| ) | ||
|
|
||
|
|
||
| def format(data, templates): # @ReservedAssignment | ||
| '''Format *data* using *templates*. | ||
|
|
||
| *data* should be a dictionary of data to format into a path. | ||
|
|
||
| *templates* should be a list of :py:class:`~lucidity.template.Template` | ||
| instances in the order that they should be tried. | ||
|
|
||
| Return ``(path, template)`` from first successful format. | ||
|
|
||
| Raise :py:class:`~lucidity.error.FormatError` if *data* is not | ||
| formattable by any of the supplied *templates*. | ||
|
|
||
|
|
||
| ''' | ||
| for template in templates: | ||
| try: | ||
| path = template.format(data) | ||
| except FormatError: | ||
| continue | ||
| else: | ||
| return (path, template) | ||
|
|
||
| raise FormatError( | ||
| 'Data {0!r} was not formattable by any of the supplied templates.' | ||
| .format(data) | ||
| ) | ||
|
|
||
|
|
||
| def get_template(name, templates): | ||
| '''Retrieve a template from *templates* by *name*. | ||
|
|
||
| Raise :py:exc:`~lucidity.error.NotFound` if no matching template with | ||
| *name* found in *templates*. | ||
|
|
||
| ''' | ||
| for template in templates: | ||
| if template.name == name: | ||
| return template | ||
|
|
||
| raise NotFound( | ||
| '{0} template not found in specified templates.'.format(name) | ||
| ) | ||
| from .error import ( | ||
| ParseError, | ||
| FormatError, | ||
| NotFound | ||
| ) | ||
|
|
||
| from .template import ( | ||
| Template, | ||
| Resolver, | ||
| discover_templates, | ||
| parse, | ||
| format, | ||
| get_template | ||
| ) | ||
|
|
||
| __all__ = [ | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was added for PEP08, and the "unused variables" warning.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. My understanding is that As I have no need to control this (the default behaviour is fine), I am not clear on the benefit of adding this here. Could you explain further perhaps? Also, I don't get any "unused variables" warning - what are you using to lint / check the code? |
||
| "__version__", | ||
| "ParseError", | ||
| "FormatError", | ||
| "NotFound", | ||
| "Template", | ||
| "Resolver", | ||
| "discover_templates", | ||
| "parse", | ||
| "format", | ||
| "get_template", | ||
| ] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was created by unit tests in Python 3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍