Skip to content

Commit

Permalink
updated tests for main. using imp.find_module() vs import_module
Browse files Browse the repository at this point in the history
  • Loading branch information
miki725 committed May 3, 2017
1 parent e0338da commit 9ff7b13
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 390 deletions.
1 change: 1 addition & 0 deletions .importanizerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"exclude": [
"*/.tox/*",
"*/test_data/*.py"
],
"groups": [
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: clean-pyc clean-build docs clean

NOSE_FLAGS=-sv --with-doctest --rednose
NOSE_FLAGS=-sv --with-doctest --rednose --exclude=test_data
COVER_CONFIG_FLAGS=--with-coverage --cover-package=importanize,tests --cover-tests --cover-erase
COVER_REPORT_FLAGS=--cover-html --cover-html-dir=htmlcov
COVER_FLAGS=${COVER_CONFIG_FLAGS} ${COVER_REPORT_FLAGS}
Expand Down Expand Up @@ -45,6 +45,7 @@ clean-test-all: clean-test

lint:
flake8 importanize tests
python -m importanize tests/ importanize/ tests/ --ci

test:
nosetests ${NOSE_FLAGS} tests/
Expand Down
78 changes: 40 additions & 38 deletions importanize/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import json
import logging
import os
import pathlib
import sys
from fnmatch import fnmatch
from stat import S_ISFIFO

import pathlib2 as pathlib
import six

from . import __description__, __version__, formatters
Expand Down Expand Up @@ -63,24 +63,19 @@


def find_config():
path = os.getcwd()
path = pathlib.Path.cwd()
default_config = None
found_default = ''

while path != os.sep:
config_path = os.path.join(path, IMPORTANIZE_CONFIG)
if os.path.exists(config_path):
default_config = config_path
found_default = (' Found configuration file at {}'
''.format(default_config))

while path != pathlib.Path(path.root):
config_path = path / IMPORTANIZE_CONFIG
if config_path.exists():
default_config = six.text_type(config_path)
break
else:
path = os.path.dirname(path)

return default_config, found_default
path = path.parent

return default_config

default_config, found_default = find_config()

parser = argparse.ArgumentParser(
description=__description__,
Expand All @@ -94,15 +89,13 @@ def find_config():
)
parser.add_argument(
'-c', '--config',
type=six.text_type,
default=default_config,
type=argparse.FileType('rb'),
help='Path to importanize config json file. '
'If "{}" is present in either current folder '
'or any parent folder, that config '
'will be used. Otherwise crude default pep8 '
'config will be used.{}'
''.format(IMPORTANIZE_CONFIG,
found_default),
'config will be used.'
''.format(IMPORTANIZE_CONFIG),
)
parser.add_argument(
'-f', '--formatter',
Expand Down Expand Up @@ -208,6 +201,11 @@ def run_importanize_on_text(text, config, args):

def run(source, config, args, path=None):
if isinstance(source, six.string_types):
msg = 'About to importanize'
if path:
msg += ' {}'.format(path)
log.debug(msg)

try:
organized = run_importanize_on_text(source, config, args)

Expand All @@ -221,7 +219,7 @@ def run(source, config, args, path=None):
else:
if args.print and args.header and path:
print('=' * len(six.text_type(path)))
print(path)
print(six.text_type(path))
print('-' * len(six.text_type(path)))

if args.print:
Expand All @@ -237,10 +235,10 @@ def run(source, config, args, path=None):
else:
path.write_text(organized)

msg = 'Successfully importanized'
if path:
msg += ' {}'.format(path)
log.info(msg)
msg = 'Successfully importanized'
if path:
msg += ' {}'.format(path)
log.info(msg)

return organized

Expand Down Expand Up @@ -271,38 +269,43 @@ def run(source, config, args, path=None):
raise CIFailure()


def main():
args = parser.parse_args()
def main(args=None):
args = args if args is not None else sys.argv[1:]
args = parser.parse_args(args=args)

# adjust logging level
(logging.getLogger('')
.setLevel(VERBOSITY_MAPPING.get(args.verbose, 0)))

log.debug('Running importanize with {}'.format(args))

config_path = getattr(args.config, 'name', '') or find_config()

if args.version:
msg = (
'importanize\n'
'===========\n'
'{}\n\n'
'version: {}\n'
'python: {}\n'
'{description}\n\n'
'version: {version}\n'
'python: {python}\n'
'config: {config}\n'
'source: https://github.com/miki725/importanize'
)
print(msg.format(__description__, __version__, sys.executable))
print(msg.format(
description=__description__,
version=__version__,
python=sys.executable,
config=config_path or '<default pep8>',
))
return 0

if args.config is None:
config = PEP8_CONFIG
else:
config = json.loads(read(args.config))
config = json.loads(read(config_path)) if config_path else PEP8_CONFIG

to_importanize = [pathlib.Path(i) for i in (args.path or ['.'])]

if S_ISFIFO(os.fstat(0).st_mode):
if args.path:
parser.error('Cant supply any paths when piping input')
return 1
return parser.error('Cant supply any paths when piping input')

to_importanize = [force_text(sys.stdin.read())]
args.print = True
Expand All @@ -326,5 +329,4 @@ def main():
return int(not all_successes)


if __name__ == '__main__':
sys.exit(main())
sys.exit(main()) if __name__ == '__main__' else None
16 changes: 15 additions & 1 deletion importanize/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,19 @@ class ImportStatement(ComparatorMixin):

def __init__(self, line_numbers, stem, leafs=None,
comments=None, **kwargs):
as_name = None

if ' as ' in stem:
stem, as_name = list_strip(stem.split(' as '))
if leafs:
as_name = None

if stem == as_name:
as_name = None

self.line_numbers = line_numbers
self.stem = stem
self.as_name = as_name
self.leafs = leafs or []
self.comments = comments or []
self.file_artifacts = kwargs.get('file_artifacts', {})
Expand All @@ -126,7 +137,10 @@ def root_module(self):

def as_string(self):
if not self.leafs:
return 'import {}'.format(self.stem)
data = 'import {}'.format(self.stem)
if self.as_name:
data += ' as {}'.format(self.as_name)
return data
else:
return (
'from {} import {}'
Expand Down
55 changes: 11 additions & 44 deletions importanize/utils.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,17 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import imp
import operator
import os
import sys
from contextlib import contextmanager
from importlib import import_module


@contextmanager
def ignore_site_packages_paths():
paths = sys.path[:]
def _get_module_path(module_name):
try:
# remove working directory so that all
# local imports fail
if os.getcwd() in sys.path:
sys.path.remove(os.getcwd())
# remove all third-party paths
# so that only stdlib imports will succeed
sys.path = list(set(filter(
None,
filter(lambda i: all(('site-packages' not in i,
'python' in i or 'pypy' in i)),
map(operator.methodcaller('lower'), sys.path))
)))
yield
finally:
sys.path = paths


def _safe_import_module(module_name):
# remove module and submodules
# removing submodules is necessary in cases when module
# imports an attribute from submodule
# if parent module is removed from sys.modules
# but not removing submodule will result in AttributeError
# when attempting to re-import parent module again
imported_modules = {
k: sys.modules.pop(k)
for k in list(sys.modules.keys())
if k == module_name or k.startswith(module_name + '.')
}
try:
return import_module(module_name)
# TODO deprecated in Py3.
# TODO Find better way for py2 and py3 compatibility.
return imp.find_module(module_name)[1]
except ImportError:
return None
finally:
sys.modules.update(imported_modules)
return ''


def is_std_lib(module_name):
Expand All @@ -55,16 +21,17 @@ def is_std_lib(module_name):
if module_name in sys.builtin_module_names:
return True

with ignore_site_packages_paths():
return bool(_safe_import_module(module_name))
module_path = _get_module_path(module_name)
if 'site-packages' in module_path:
return False
return 'python' in module_path or 'pypy' in module_path


def is_site_package(module_name):
if not module_name:
return False

module = _safe_import_module(module_name)
module_path = getattr(module, "__file__", "")
module_path = _get_module_path(module_name)
if "site-packages" not in module_path:
return False
return "python" in module_path or "pypy" in module_path
Expand Down
16 changes: 16 additions & 0 deletions tests/test_data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"groups": [
{
"type": "stdlib"
},
{
"type": "sitepackages"
},
{
"type": "remainder"
},
{
"type": "local"
}
]
}
1 change: 1 addition & 0 deletions tests/test_data/input.txt → tests/test_data/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ..othermodule import rainbows
from a import b
from a.b import c
import flake8 as lint # in site-package
from a.b import d

import z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from os import path as ospath

import coverage # in site-packages
import flake8 as lint # in site-package

import something # with comment
import z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from os import path as ospath

import coverage # in site-packages
import flake8 as lint # in site-package

import something # with comment
import z
Expand Down

0 comments on commit 9ff7b13

Please sign in to comment.