Skip to content

Commit

Permalink
Get version using AST
Browse files Browse the repository at this point in the history
Incorrectly installed package will crash during import
  • Loading branch information
penguinolog committed Apr 18, 2017
1 parent 251f329 commit c1b31dc
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGELOG
=========
Version 2.4.1
-------------
* Use ast for version extraction and do not use danger of if's in `__init__.py` - installed without dependencies package should crash on import.

Version 2.4.0
-------------
* PyPy3 correct support and python 3.3 external asyncio.
Expand Down
45 changes: 20 additions & 25 deletions logwrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,24 @@

from __future__ import absolute_import

try:
import six

from ._repr_utils import PrettyFormat, pretty_repr, pretty_str

# pylint: disable=no-name-in-module
if six.PY3: # pragma: no cover
from ._log_wrap3 import logwrap, LogWrap
else: # pragma: no cover
from ._log_wrap2 import logwrap, LogWrap
# pylint: enable=no-name-in-module

__all__ = (
'LogWrap',
'logwrap',
'PrettyFormat',
'pretty_repr',
'pretty_str'
)

except ImportError: # pragma: no cover
# Package is not installed
six = PrettyFormat = pretty_repr = pretty_str = logwrap = LogWrap = None

__version__ = '2.4.0'
import six

from ._repr_utils import PrettyFormat, pretty_repr, pretty_str

# pylint: disable=no-name-in-module
if six.PY3: # pragma: no cover
from ._log_wrap3 import logwrap, LogWrap
else: # pragma: no cover
from ._log_wrap2 import logwrap, LogWrap
# pylint: enable=no-name-in-module

__all__ = (
'LogWrap',
'logwrap',
'PrettyFormat',
'pretty_repr',
'pretty_str'
)

__version__ = '2.4.1'
__author__ = "Alexey Stepanov <penguinolog@gmail.com>"
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[metadata]
name = logwrap
version = attr: logwrap.__version__
url = https://github.com/penguinolog/logwrap
author = Alexey Stepanov
author_email = penguinolog@gmail.com
Expand Down
59 changes: 59 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,69 @@

"""logwrap decorator for human-readable logging of command arguments."""

import ast
import collections
import os.path

import setuptools
import six

with open(
os.path.join(
os.path.dirname(__file__),
'logwrap', '__init__.py'
)
) as f:
source = f.read()


# noinspection PyUnresolvedReferences
def get_simple_vars_from_src(src):
"""Get simple (string/number/boolean and None) assigned values from source.
:param src: Source code
:type src: str
:rtype: collections.OrderedDict
"""
ast_string = (ast.Str,)
if six.PY3:
ast_string += (ast.Bytes,)

tree = ast.parse(src)

result = collections.OrderedDict()

for node in ast.iter_child_nodes(tree):
if not isinstance(node, ast.Assign): # We parse assigns only
continue
for tgt in node.targets:
if isinstance(
tgt, ast.Name
) and isinstance(
tgt.ctx, ast.Store
):
if isinstance(node.value, ast.Num): # Number, incl. complex
result[tgt.id] = node.value.n
elif isinstance(node.value, ast_string): # String, incl bytes
result[tgt.id] = node.value.s
elif isinstance( # NameConstant in python < 3.4
node.value, ast.Name
) and isinstance(
node.value.ctx, ast.Load # Read constant
):
# pylint: disable=eval-used
result[tgt.id] = eval(node.value.id, {}, {}) in {} # nosec
# pylint: enable=eval-used
elif six.PY34 and isinstance(node.value, ast.NameConstant):
result[tgt.id] = node.value.value
return result


variables = get_simple_vars_from_src(source)

setuptools.setup(
name='logwrap',
version=variables['__version__'],
extras_require={
':python_version == "2.7"': [
'funcsigs>=1.0',
Expand Down

0 comments on commit c1b31dc

Please sign in to comment.