Skip to content

Commit

Permalink
Implement archan provider
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jun 5, 2017
1 parent 1fe1b28 commit 66edb5b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
3 changes: 3 additions & 0 deletions setup.py
Expand Up @@ -72,6 +72,9 @@ def read(*names, **kwargs):
entry_points={
'console_scripts': [
'dependenpy = dependenpy.cli:main',
],
'archan': [
'dependenpy.ArchanProvider = dependenpy.dsm:ArchanProvider'
]
},
)
4 changes: 2 additions & 2 deletions src/dependenpy/__init__.py
Expand Up @@ -8,8 +8,8 @@
be able to build a dependency matrix and use it for other purposes.
"""

from .dsm import DSM, Dependency, Module, Package
from .dsm import ArchanProvider, DSM, Dependency, Module, Package
from .structures import Matrix, TreeMap

__all__ = ('DSM', 'Dependency', 'Matrix', 'Module', 'Package', 'TreeMap')
__all__ = ('ArchanProvider', 'DSM', 'Dependency', 'Matrix', 'Module', 'Package', 'TreeMap')
__version__ = '3.1.0'
7 changes: 2 additions & 5 deletions src/dependenpy/cli.py
Expand Up @@ -21,7 +21,7 @@
import sys

from . import __version__
from .dsm import DSM
from .dsm import DSM, guess_depth
from .printer import CSV, FORMAT, JSON

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -98,10 +98,7 @@ def main(args=None):
# guess convenient depth
depth = args.depth
if depth is None:
if len(packages) == 1:
depth = packages[0].count('.') + 2
else:
depth = min(p.count('.') for p in packages) + 1
depth = guess_depth(packages)

# open file if not stdout
output = args.output
Expand Down
48 changes: 48 additions & 0 deletions src/dependenpy/dsm.py
Expand Up @@ -17,6 +17,13 @@
import sys
from os.path import isdir, isfile, join, splitext

try:
from archan import Provider, Argument
except ImportError:
from collections import namedtuple
Provider = object
Argument = lambda *a, **k: None

from .finder import Finder, PackageSpec
from .node import LeafNode, NodeMixin, RootNode
from .printer import PrintMixin
Expand Down Expand Up @@ -425,3 +432,44 @@ def __str__(self):
def external(self):
"""Property to tell if the dependency's target is a valid node."""
return isinstance(self.target, str)


def guess_depth(packages):
if len(packages) == 1:
return packages[0].count('.') + 2
return min(p.count('.') for p in packages) + 1


class ArchanProvider(Provider):
name = 'Dependency matrix provider for Archan'
description = 'Provide matrix data about internal dependencies ' \
'in a set of packages.'
arguments = (
Argument('packages', list, 'the list of packages to check for'),
Argument(
'enforce_init', bool,
' whether to assert presence of __init__.py files in directories'),
Argument('depth', int, 'the depth of the matrix to generate'),
)

def get_dsm(self, packages, enforce_init=True, depth=None):
"""
Provide matrix data about internal dependencies in a set of packages.
Args:
*packages (list): the list of packages to check for.
enforce_init (bool):
whether to assert presence of __init__.py files in directories.
depth (int): the depth of the matrix to generate.
Returns:
dict: keys and data in a dictionary (no categories for now).
"""
dsm = DSM(*packages, enforce_init=enforce_init)
if depth is None:
depth = guess_depth(packages)
matrix = dsm.as_matrix(depth=depth)
return {
'keys': matrix.keys,
'data': matrix.data
}

0 comments on commit 66edb5b

Please sign in to comment.