Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: b7488e2f33
Fetching contributors…

Cannot retrieve contributors at this time

85 lines (67 sloc) 2.69 KB
from ConfigParser import ConfigParser
from ConfigParser import NoSectionError
from jstools import utils, merge
from memoize import memoizedproperty
import logging
import pkg_resources
DIST = pkg_resources.Requirement.parse("jstools")
logger = logging.getLogger('jstools.deps')
class DepMap(ConfigParser):
dependency mapper
template = dict(require='// @requires %s\n',
include='// @include %s\n')
def __init__(self, defaults=None, printer=logger):
ConfigParser.__init__(self, defaults)
self.printer = printer
def alias_map(self):
return utils.SectionMap(self, 'alias')
def from_resource(cls, resource_name, dist=DIST, defaults=None, printer=logger):
conf = pkg_resources.resource_stream(dist, resource_name)
dmap = cls(defaults=defaults, printer=printer)
return dmap
def from_path(cls, path, defaults=None, printer=logger):
dmap = cls(defaults=defaults, printer=printer)
if isinstance(path, basestring):
path = path,
paths =
assert paths, ValueError("No valid config files: %s" %paths)
return dmap
def reverse_alias_map(self):
return dict((v, k) for k, v in self.alias_map.items())
def get_dependencies_by_filename(self, filename):
alias = self.reverse_alias_map[filename]
return self.get_dependencies_by_alias(alias)
def get_dependencies_by_alias(self, alias):
return utils.SectionMap(self, alias)
except NoSectionError:
return None
get_deps = get_dependencies_by_alias
def guess_alias_by_filename(self, filename, sorter=sorted, single=True):
guesses = (self.reverse_alias_map.get(fn) for fn in sorter(self.reverse_alias_map.keys()) if fn.endswith(filename))
if single:
except StopIteration:
return None
return guesses
def formatted_dependencies(self, alias):
for directive, aliases in self.get_deps(alias).items():
for alias in aliases.split():
yield self.template[directive] %self.alias_map[alias]
except KeyError:
raise AliasNotFound("Alias '%s' not found in %s. "
"Check dependency cfg for alias mismatch." \
%(alias, self.alias_map.keys()))
class AliasNotFound(KeyError):
Exception for any time an alias is not found
Jump to Line
Something went wrong with that request. Please try again.