Skip to content

Commit 4e35653

Browse files
committed
Format code with yapf
1 parent 33ad730 commit 4e35653

File tree

7 files changed

+101
-74
lines changed

7 files changed

+101
-74
lines changed

.landscape.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ test-warnings: yes
33
strictness: veryhigh
44
max-line-length: 80
55
pep257:
6-
disable: [D203, D401]
6+
disable: [D406, D413, D407, D213, D202, D203, D401]
77
pep8:
88
full: true
99
mccabe:

.style.yapf

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[style]
2+
based_on_style = google

conftest.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
#
1313
# You should have received a copy of the GNU General Public License
1414
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15-
1615
"""Configuration facilities for cppdep tests with pytest."""
1716

1817
from cppdep.cppdep import Include
1918

19+
2020
#pylint: disable=invalid-name
2121
def pytest_assertrepr_compare(op, left, right):
2222
"""Custom assertion messages for cppdep classes."""
2323
if isinstance(left, Include) and isinstance(right, Include):
2424
if op in ('==', '!='):
25-
return ['Comparing Include directives:',
26-
' vals: %s %s %s' % (str(left),
27-
{'==': '!=', '!=': '=='}[op],
28-
str(right))]
25+
return [
26+
'Comparing Include directives:',
27+
' vals: %s %s %s' % (str(left), {
28+
'==': '!=',
29+
'!=': '=='
30+
}[op], str(right))
31+
]

cppdep/__main__.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18-
1918
"""The command-line entry point for the package."""
2019

2120
from __future__ import print_function, absolute_import
@@ -32,17 +31,29 @@
3231

3332
def main(argv=None):
3433
"""Runs the dependency analysis and prints results and graphs."""
35-
parser = ap.ArgumentParser(description=cppdep.__doc__,
36-
formatter_class=ap.ArgumentDefaultsHelpFormatter)
37-
parser.add_argument('--version', action='store_true', default=False,
38-
help='show the version information and exit')
39-
parser.add_argument('-c', '--config', default='.cppdep.yml',
40-
help="""a YAML file which describes
41-
the source code structure of a C/C++ project""")
42-
parser.add_argument('-l', action='store_true', default=False,
43-
help='list reduced dependencies of nodes')
44-
parser.add_argument('-L', action='store_true', default=False,
45-
help='list unreduced dependencies of nodes')
34+
parser = ap.ArgumentParser(
35+
description=cppdep.__doc__,
36+
formatter_class=ap.ArgumentDefaultsHelpFormatter)
37+
parser.add_argument(
38+
'--version',
39+
action='store_true',
40+
default=False,
41+
help='show the version information and exit')
42+
parser.add_argument(
43+
'-c',
44+
'--config',
45+
default='.cppdep.yml',
46+
help='a YAML file describing the C/C++ project structure')
47+
parser.add_argument(
48+
'-l',
49+
action='store_true',
50+
default=False,
51+
help='list reduced dependencies of nodes')
52+
parser.add_argument(
53+
'-L',
54+
action='store_true',
55+
default=False,
56+
help='list unreduced dependencies of nodes')
4657
parser.add_argument('-o', '--output', metavar='path', help='output file')
4758
args = parser.parse_args(argv)
4859
if args.version:

cppdep/cppdep.py

+45-40
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
1716
"""C/C++ dependency analyzer.
1817
1918
Physical dependency analyzer
@@ -36,17 +35,15 @@
3635

3736
from .graph import Graph
3837

39-
4038
VERSION = '0.2.4' # The latest release version.
4139

42-
_SCHEMA_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
43-
'config_schema.yml')
40+
_SCHEMA_FILE = os.path.join(
41+
os.path.dirname(os.path.abspath(__file__)), 'config_schema.yml')
4442
assert os.path.isfile(_SCHEMA_FILE), 'The cppdep schema file is missing.'
4543
assert safe_load(open(_SCHEMA_FILE)) # Will throw if invalid.
4644

4745
_FILE_OPEN_FLAGS = {} if sys.version[0] == '2' else {'errors': 'replace'}
4846

49-
5047
# Allowed common abbreviations in the code:
5148
# ccd - Cumulative Component Dependency (CCD)
5249
# nccd - Normalized CCD
@@ -218,8 +215,8 @@ def _find_in(include_dir):
218215
if any(x.match(self.hfile) for x in patterns):
219216
return self.hfile, package
220217

221-
if any(_find_in(x) for x in
222-
(iter if self.with_quotes else reversed)(include_dirs)):
218+
direction = iter if self.with_quotes else reversed
219+
if any(_find_in(x) for x in direction(include_dirs)):
223220
return self.hpath, None
224221

225222
return None, None
@@ -274,6 +271,7 @@ def dependencies(self):
274271

275272
def __sanitize_includes(self):
276273
"""Sanitizes and checks includes."""
274+
277275
def _check_duplicates(path, includes):
278276
unique_includes = set()
279277
for include in includes:
@@ -301,14 +299,14 @@ def _remove_redundant():
301299

302300
if self.hpath and self.cpath:
303301
hfile = os.path.basename(self.hpath)
304-
if hfile not in (os.path.basename(x.hfile)
305-
for x in self.includes_in_c):
302+
if hfile not in (
303+
os.path.basename(x.hfile) for x in self.includes_in_c):
306304
warn('include issues: missing include: '
307305
'%s does not include %s.' % (self.cpath, hfile))
308306
elif hfile != os.path.basename(self.includes_in_c[0].hfile):
309307
warn('include issues: include order: '
310-
'%s should be the first include in %s.' %
311-
(hfile, self.cpath))
308+
'%s should be the first include in %s.' % (hfile,
309+
self.cpath))
312310
_remove_duplicates()
313311
_remove_redundant()
314312

@@ -386,6 +384,7 @@ def __str__(self):
386384

387385
def __init_paths(self, src_paths, include_paths, alias_paths, ignore_paths):
388386
"""Initializes package paths."""
387+
389388
def _update(path_container, arg_paths, check_dir=True):
390389
for path in arg_paths:
391390
path = os.path.normpath(path)
@@ -440,9 +439,9 @@ def _select_src_file(root, filename):
440439
return
441440
src_match = Package._RE_SRC.match(filename)
442441
if src_match:
443-
(hpaths if src_match.group('h')
444-
else cpaths)[strip_ext(filename)].append(
445-
file_type(_reverse(full_path), full_path))
442+
src_container = hpaths if src_match.group('h') else cpaths
443+
src_container[strip_ext(filename)].append(
444+
file_type(_reverse(full_path), full_path))
446445

447446
def _gather_files(dir_path):
448447
for root, _, files in os.walk(dir_path):
@@ -462,6 +461,7 @@ def _gather_files(dir_path):
462461

463462
def __pair_files(self, hpaths, cpaths):
464463
"""Pairs header and implementation files into components."""
464+
465465
# This should probably be solved with a graph algorithm.
466466
# Find the nodes with the longest matching consecutive ancestors
467467
# starting from the node (not the root!).
@@ -472,17 +472,21 @@ def __pair_files(self, hpaths, cpaths):
472472
# Therefore, the algorithm to find
473473
# the lowest common ancestor seems to lead to false answers.
474474
def _num_consecutive_ancestors(file_one, file_two):
475-
return sum(1 for _ in itertools.takewhile(lambda x: x[0] == x[1],
476-
zip(file_one.rev_path,
477-
file_two.rev_path)))
475+
return sum(1
476+
for _ in itertools.takewhile(lambda x: x[0] == x[1],
477+
zip(file_one.rev_path,
478+
file_two.rev_path)))
478479

479480
def _pair(hfiles, cfiles):
480481
assert hfiles and cfiles # Expected to have few elements.
481-
candidates = [(x, sorted(((_num_consecutive_ancestors(x, y), y)
482-
for y in hfiles), reverse=True))
482+
candidates = [(x,
483+
sorted(
484+
((_num_consecutive_ancestors(x, y), y)
485+
for y in hfiles),
486+
reverse=True))
483487
for x in cfiles]
484-
candidates.sort(reverse=True,
485-
key=lambda x: tuple(y for y, _ in x[1]))
488+
candidates.sort(
489+
reverse=True, key=lambda x: tuple(y for y, _ in x[1]))
486490
for cfile, hfile_candidates in candidates:
487491
for _, hfile in hfile_candidates:
488492
if hfile in hfiles:
@@ -555,8 +559,8 @@ def dependencies(self):
555559
if self.__dep_groups is None:
556560
self.__dep_groups = set()
557561
for package in self.packages.values():
558-
self.__dep_groups.update(x.group for x in package.dependencies()
559-
if x.group != self)
562+
self.__dep_groups.update(
563+
x.group for x in package.dependencies() if x.group != self)
560564
return self.__dep_groups
561565

562566
def add_package(self, package):
@@ -572,8 +576,8 @@ def add_package(self, package):
572576
"""
573577
if package.name in self.packages:
574578
raise InvalidArgumentError(
575-
'%s is a duplicate package in %s group.' %
576-
(package.name, self.name))
579+
'%s is a duplicate package in %s group.' % (package.name,
580+
self.name))
577581
self.packages[package.name] = package
578582

579583

@@ -658,8 +662,7 @@ def __add_package_group(pkg_group_config, pkg_groups):
658662
package_group = PackageGroup(group_name, group_path)
659663

660664
for pkg_config in pkg_group_config['packages']:
661-
Package(pkg_config['name'],
662-
package_group,
665+
Package(pkg_config['name'], package_group,
663666
yaml_optional_list(pkg_config, 'src'),
664667
yaml_optional_list(pkg_config, 'include'),
665668
yaml_optional_list(pkg_config, 'alias'),
@@ -670,22 +673,24 @@ def __add_package_group(pkg_group_config, pkg_groups):
670673

671674
def __gather_include_dirs(self):
672675
"""Gathers include directories from packages."""
676+
673677
def _add_from(groups):
674678
for group in groups.values():
675679
for package in group.packages.values():
676680
self.include_dirs.extend(package.include_paths)
681+
677682
_add_from(self.internal_groups)
678683
_add_from(self.external_groups)
679684

680685
def __gather_aliases(self):
681686
"""Gathers aliases for *external* packages lazy include search."""
682687
for group in self.external_groups.values():
683688
for package in group.packages.values():
684-
self.__package_aliases.extend((x, package)
685-
for x in package.alias_paths)
689+
self.__package_aliases.extend(
690+
(x, package) for x in package.alias_paths)
686691
self.__package_aliases.sort()
687-
assert (len(set(x for x, _ in self.__package_aliases)) ==
688-
len(self.__package_aliases)), "Ambiguous aliases to packages"
692+
assert (len(set(x for x, _ in self.__package_aliases)) == len(
693+
self.__package_aliases)), "Ambiguous aliases to packages"
689694

690695
def __gather_include_patterns(self):
691696
"""Gathers and compiles include patterns into regex objects."""
@@ -708,16 +713,16 @@ def locate(self, include, component):
708713
Raises:
709714
AnalysisError: Failure to associate a header to a component.
710715
"""
716+
711717
def _find_external_package(hpath):
712718
for path, package in reversed(self.__package_aliases):
713719
if path_isancestor(path, hpath):
714720
return package
715721
raise AnalysisError('include error: Cannot associate '
716722
'%s file with any component.' % hpath)
717723

718-
hpath, package = include.locate(component.working_dir,
719-
self.include_dirs,
720-
self.__include_patterns)
724+
hpath, package = include.locate(
725+
component.working_dir, self.include_dirs, self.__include_patterns)
721726

722727
if hpath is None:
723728
return False
@@ -727,11 +732,10 @@ def _find_external_package(hpath):
727732
component.dep_components.add(dep_component)
728733
else:
729734
if hpath in self._external_components:
730-
component.dep_components.add(
731-
self._external_components[hpath])
735+
component.dep_components.add(self._external_components[hpath])
732736
else:
733-
dep_component = ExternalComponent(
734-
hpath, package or _find_external_package(hpath))
737+
dep_component = ExternalComponent(hpath, package or
738+
_find_external_package(hpath))
735739
component.dep_components.add(dep_component)
736740
self._external_components[hpath] = dep_component
737741
return True
@@ -768,6 +772,7 @@ def make_components(self):
768772

769773
def analyze(self, printer, args):
770774
"""Runs the analysis."""
775+
771776
def _analyze(graph_name, digraph):
772777
digraph.analyze()
773778
digraph.print_cycles(printer)
@@ -803,8 +808,8 @@ def _analyze(graph_name, digraph):
803808
continue
804809
printer('\n' + '#' * 80)
805810
printer('analyzing dependencies among components in '
806-
'the specified package %s.%s ...' %
807-
(group_name, pkg_name))
811+
'the specified package %s.%s ...' % (group_name,
812+
pkg_name))
808813
_analyze('_'.join((group_name, pkg_name)),
809814
Graph(package.components,
810815
lambda x: (i if i.package == package

0 commit comments

Comments
 (0)