Skip to content

Commit 600ff50

Browse files
committed
Improve readability of WITH expressions #45
This add a new render_as_readable() function that will wrap the WITH expression in parens (unless this is only a simple WITH expression) Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent c6826fe commit 600ff50

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/license_expression/__init__.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,19 @@ def render(self, template='{symbol.key}', *args, **kwargs):
827827
"""
828828
return NotImplementedError
829829

830+
def render_as_readable(self, template='{symbol.key}', *args, **kwargs):
831+
"""
832+
Return a formatted string rendering for this expression using the
833+
`template` format string to render each symbol. Add extra parenthesis
834+
around WITH sub-expressions for improved readbility. See `render()` for
835+
other arguments.
836+
"""
837+
if isinstance(self, LicenseWithExceptionSymbol):
838+
return self.render(
839+
template=template, wrap_in_parens=False, *args, **kwargs)
840+
else:
841+
return self.render(template=template, wrap_with_in_parens=True, *args, **kwargs)
842+
830843

831844
class BaseSymbol(Renderable, boolean.Symbol):
832845
"""
@@ -1075,10 +1088,17 @@ def decompose(self):
10751088
yield self.license_symbol
10761089
yield self.exception_symbol
10771090

1078-
def render(self, template='{symbol.key}', *args, **kwargs):
1091+
def render(self, template='{symbol.key}', wrap_with_in_parens=False, *args, **kwargs):
1092+
"""
1093+
Return a formatted WITH expression. If `wrap_in_parens`, wrap in parens.
1094+
"""
10791095
lic = self.license_symbol.render(template, *args, **kwargs)
10801096
exc = self.exception_symbol.render(template, *args, **kwargs)
1081-
return '%(lic)s WITH %(exc)s' % locals()
1097+
if wrap_with_in_parens:
1098+
temp = '(%(lic)s WITH %(exc)s)'
1099+
else:
1100+
temp = '%(lic)s WITH %(exc)s'
1101+
return temp % locals()
10821102

10831103
def __hash__(self, *args, **kwargs):
10841104
return hash((self.license_symbol, self.exception_symbol,))

tests/test_license_expression.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,30 @@ def test_primary_license_symbol_and_primary_license_key(self):
16651665
assert expected == licensing.primary_license_symbol(
16661666
parsed, decompose=False).render('{symbol.key}')
16671667

1668+
def test_render_plain(self):
1669+
l = Licensing()
1670+
result = l.parse('gpl-2.0 WITH exception-gpl-2.0-plus or MIT').render()
1671+
expected = 'gpl-2.0 WITH exception-gpl-2.0-plus OR MIT'
1672+
assert expected == result
1673+
1674+
def test_render_as_readable_does_not_wrap_in_parens_single_with(self):
1675+
l = Licensing()
1676+
result = l.parse('gpl-2.0 WITH exception-gpl-2.0-plus').render_as_readable()
1677+
expected = 'gpl-2.0 WITH exception-gpl-2.0-plus'
1678+
assert expected == result
1679+
1680+
def test_render_as_readable_wraps_in_parens_with_and_other_subexpressions(self):
1681+
l = Licensing()
1682+
result = l.parse('mit AND gpl-2.0 WITH exception-gpl-2.0-plus').render_as_readable()
1683+
expected = 'mit AND (gpl-2.0 WITH exception-gpl-2.0-plus)'
1684+
assert expected == result
1685+
1686+
def test_render_as_readable_does_not_wrap_in_parens_if_no_with(self):
1687+
l = Licensing()
1688+
result1 = l.parse('gpl-2.0 and exception OR that').render_as_readable()
1689+
result2 = l.parse('gpl-2.0 and exception OR that').render()
1690+
assert result1 == result2
1691+
16681692

16691693
class SplitAndTokenizeTest(TestCase):
16701694

0 commit comments

Comments
 (0)