From 371faaf6453b07f5316e356e68d7f26ae44b0df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 21 Nov 2019 12:42:50 +0100 Subject: Add Clause.prettyprint() for debug. This function allows developers to preview the structure of the resulting clause parsed from a spec, usable with `print(spec.clause.prettyprint())`. Apply typical PEP8 indentation rules. --- semantic_version/base.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/semantic_version/base.py b/semantic_version/base.py index 078d83d..4327fa1 100644 --- a/semantic_version/base.py +++ b/semantic_version/base.py @@ -673,6 +673,19 @@ class Clause(object): def __eq__(self, other): raise NotImplementedError() + def prettyprint(self, indent='\t'): + """Pretty-print the clause. + """ + return '\n'.join(self._pretty()).replace('\t', indent) + + def _pretty(self): + """Actual pretty-printing logic. + + Yields: + A list of string. Indentation is performed with \t. + """ + yield repr(self) + def __ne__(self, other): return not self == other @@ -733,6 +746,15 @@ class AnyOf(Clause): def __repr__(self): return 'AnyOf(%s)' % ', '.join(sorted(repr(c) for c in self.clauses)) + def _pretty(self): + yield 'AnyOF(' + for clause in self.clauses: + lines = list(clause._pretty()) + for line in lines[:-1]: + yield '\t' + line + yield '\t' + lines[-1] + ',' + yield ')' + class AllOf(Clause): __slots__ = ['clauses'] @@ -789,6 +811,15 @@ class AllOf(Clause): def __repr__(self): return 'AllOf(%s)' % ', '.join(sorted(repr(c) for c in self.clauses)) + def _pretty(self): + yield 'AllOF(' + for clause in self.clauses: + lines = list(clause._pretty()) + for line in lines[:-1]: + yield '\t' + line + yield '\t' + lines[-1] + ',' + yield ')' + class Matcher(Clause): __slots__ = [] -- cgit v1.2.1