Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
build:
environment:
python: 2.7.7
nodes:
my-tests:
dependencies:
before:
- pip install 'setuptools==44.0.0'
- pip install -e '.[dev]'
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ addons:
- graphviz

install:
- pip install -r requirements.txt
- pip install '.[dev]'
- pip install coveralls

script:
Expand Down
43 changes: 9 additions & 34 deletions pydeps/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,12 @@
import sys
import textwrap
from . import __version__


def error(*args, **kwargs): # pragma: nocover
"""Print an error message and exit.
"""
kwargs['file'] = sys.stderr
print("\n\tERROR:", *args, **kwargs)
sys.exit(1)


#: the (will become) verbose function
verbose = None


def _not_verbose(*args, **kwargs): # pragma: nocover
pass


def _mkverbose(level):
def _verbose(n, *args, **kwargs):
if not isinstance(n, int): # we're only interested in small integers
# this allows the simpler usage cli.verbose(msg)
args = (n,) + args
n = 1
if 0 < level <= n:
print(*args, **kwargs)
return _verbose
from . import clilog


def base_argparser(argv=()):
"""Initial parser that can set values for the rest of the parsing process.
"""
global verbose
verbose = _not_verbose

_p = argparse.ArgumentParser(add_help=False)
_p.add_argument('--debug', action='store_true', help="turn on all the show and verbose options (mainly for debugging pydeps itself)")
_p.add_argument('--config', help="specify config file", metavar="FILE")
Expand All @@ -61,7 +32,8 @@ def base_argparser(argv=()):
if _args.log:
loglevels = "CRITICAL DEBUG ERROR FATAL INFO WARN"
if _args.log not in loglevels: # pragma: nocover
error('legal values for the -L parameter are:', loglevels)
clilog.error('legal values for the -L parameter are:', loglevels)
sys.exit(1)
loglevel = getattr(logging, _args.log)
else:
loglevel = None
Expand Down Expand Up @@ -134,6 +106,9 @@ def parse_args(argv=()):

_args = args.parse_args(argv)

clilog.set_level("verbose", _args.verbose)
clilog.set_level("debug", _args.debug)

if _args.externals:
return dict(
T='svg', config=None, debug=False, display=None, exclude=[], externals=True,
Expand All @@ -151,7 +126,8 @@ def parse_args(argv=()):
if _args.noshow:
_args.show = False
if _args.nodot and _args.show_cycles:
error("Can't use --nodot and --show-cycles together") # pragma: nocover
clilog.error("Can't use --nodot and --show-cycles together") # pragma: nocover
sys.exit(1)
if _args.nodot:
_args.show_dot = False
if _args.max_bacon == 0:
Expand All @@ -161,8 +137,7 @@ def parse_args(argv=()):

_args.format = getattr(_args, 'T', getattr(_args, 'format', None))

verbose = _mkverbose(max(_args.verbose, int(_args.debug)))
verbose(2, _args, '\n')
clilog.verbose(2, _args, '\n')
if _args.debug: # pragma: nocover
_args.verbose = 1
_args.show = True
Expand Down
49 changes: 49 additions & 0 deletions pydeps/clilog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
"""
messages for command line interface (cli)
"""
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from builtins import *
import sys

levels = {"verbose": 0, "debug": 0}

def reset_levels():
set_level("verbose", 0)
set_level("debug", 0)

def set_level(name, lvl):
"""set the log level for verbose/debug logging"""
if name not in levels:
raise KeyError("unkown level: " + name)
levels[name] = int(lvl)


def error(*args, **kwargs): # pragma: nocover
"""Print an error message."""
if kwargs.get("file", None) is None:
kwargs["file"] = sys.stderr

print("ERROR:", *args, **kwargs)


def _get_effective_level():
"""by combining debug and verbose,
figure out which level we have"""
return max(levels.values())


def verbose(n, *args, **kwargs):
"""log a verbose message with level n"""
lvl = _get_effective_level()
if not lvl:
return

if not isinstance(n, int):
# this allows the simpler usage cli.verbose(msg)
args = (n,) + args
n = 1

if 0 < n <= lvl:
print(*args, **kwargs)
14 changes: 7 additions & 7 deletions pydeps/depgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import re
import enum

from . import colors, cli
from . import colors, clilog
import sys
import logging
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -260,7 +260,7 @@ def __init__(self, depgraf, types, **args):
self.add_source(src)

self.module_count = len(self.sources)
cli.verbose(1, "there are", self.module_count, "total modules")
clilog.verbose(1, "there are", self.module_count, "total modules")

self.connect_generations()
if self.args['show_cycles']:
Expand All @@ -276,15 +276,15 @@ def __init__(self, depgraf, types, **args):
excluded = [v for v in list(self.sources.values()) if v.excluded]
# print "EXCLUDED:", excluded
self.skip_count = len(excluded)
cli.verbose(1, "skipping", self.skip_count, "modules")
clilog.verbose(1, "skipping", self.skip_count, "modules")
for module in excluded:
# print 'exclude:', module.name
cli.verbose(2, " ", module.name)
clilog.verbose(2, " ", module.name)

self.remove_excluded()

if not self.args['show_deps']:
cli.verbose(3, self)
clilog.verbose(3, self)

# def verbose(self, n, *args):
# if self.args['verbose'] >= n:
Expand Down Expand Up @@ -321,7 +321,7 @@ def visit(src):
# for _src in list(self.sources.values()):
for _src in self.sources.values():
for source in visit(_src):
cli.verbose(4, "Yielding", source[0], source[1])
clilog.verbose(4, "Yielding", source[0], source[1])
yield source

def __repr__(self):
Expand Down Expand Up @@ -388,7 +388,7 @@ def exclude_noise(self):
if src.excluded:
continue
if src.is_noise():
cli.verbose(2, "excluding", src, "because it is noisy:", src.degree)
clilog.verbose(2, "excluding", src, "because it is noisy:", src.degree)
src.excluded = True
# print "Exluding noise:", src.name
self._add_skip(src.name)
Expand Down
8 changes: 4 additions & 4 deletions pydeps/dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import subprocess
import shlex

from . import cli
from . import clilog

win32 = sys.platform == 'win32'

Expand Down Expand Up @@ -75,7 +75,7 @@ def call_graphviz_dot(src, fmt):
svg = dot(src, T=fmt)
except OSError as e: # pragma: nocover
if e.errno == 2:
cli.error("""
clilog.error("""
cannot find 'dot'

pydeps calls dot (from graphviz) to create svg diagrams,
Expand All @@ -90,12 +90,12 @@ def display_svg(kw, fname): # pragma: nocover
"""Try to display the svg file on this platform.
"""
if kw['display'] is None:
cli.verbose("Displaying:", fname)
clilog.verbose("Displaying:", fname)
if sys.platform == 'win32':
os.startfile(fname)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, fname])
else:
cli.verbose(kw['display'] + " " + fname)
clilog.verbose(kw['display'] + " " + fname)
os.system(kw['display'] + " " + fname)
8 changes: 4 additions & 4 deletions pydeps/dummymodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import textwrap
import logging

from . import cli
from . import clilog

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -56,7 +56,7 @@ def __init__(self, target, **args):
log.debug("dummy-filename: %r (%s)", self.fname, self.absname)

if target.is_module:
cli.verbose(1, "target is a PACKAGE")
clilog.verbose(1, "target is a PACKAGE")
with open(self.fname, 'w') as fp:
for fname in python_sources_below(target.package_root):
modname = fname2modname(fname, target.syspath_dir)
Expand All @@ -65,15 +65,15 @@ def __init__(self, target, **args):
elif target.is_dir:
# FIXME?: not sure what the intended semantics was here, as it is
# this will almost certainly not do the right thing...
cli.verbose(1, "target is a DIRECTORY")
clilog.verbose(1, "target is a DIRECTORY")
with open(self.fname, 'w') as fp:
for fname in os.listdir(target.dirname):
if is_pysource(fname):
self.print_import(fp, fname2modname(fname, ''))

else:
assert target.is_pysource
cli.verbose(1, "target is a FILE")
clilog.verbose(1, "target is a FILE")
with open(self.fname, 'w') as fp:
self.print_import(fp, target.modpath)

Expand Down
8 changes: 4 additions & 4 deletions pydeps/pydeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import pprint
import sys
from . import py2depgraph, cli, dot, target
from . import py2depgraph, cli, dot, target, clilog
from .depgraph2dot import dep2dot, cycles2dot
import logging
from . import colors
Expand All @@ -32,14 +32,14 @@ def _pydeps(trgt, **kw):
dep_graph = py2depgraph.py2dep(trgt, **kw)

if kw.get('show_deps'):
cli.verbose("DEPS:")
clilog.verbose("DEPS:")
pprint.pprint(dep_graph)

dotsrc = depgraph_to_dotsrc(trgt, dep_graph, **kw)

if not nodot:
if kw.get('show_dot'):
cli.verbose("DOTSRC:")
clilog.verbose("DOTSRC:")
print(dotsrc)

if not no_output:
Expand All @@ -48,7 +48,7 @@ def _pydeps(trgt, **kw):
svg = svg.replace(b'</title>', b'</title><style>.edge>path:hover{stroke-width:8}</style>')

with open(output, 'wb') as fp:
cli.verbose("Writing output to:", output)
clilog.verbose("Writing output to:", output)
fp.write(svg)

if show_svg:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build-system]
requires = ["setuptools<45"]
9 changes: 0 additions & 9 deletions requirements.txt

This file was deleted.

Loading