aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDafydd Harries <daf@rhydd.org>2013-03-18 02:21:13 -0400
committerDafydd Harries <daf@rhydd.org>2013-03-18 02:21:13 -0400
commitf1df716a1ac376d04aaea4666b9ab2aa37214699 (patch)
tree544f85a69b70fe0c5b29dfa10d47b48ddda3ea9b
parent642bdf9672d1572354d27ddea5c5ba8b92f218ca (diff)
license.py: allow for traversal and flattening
-rw-r--r--license.py43
1 files changed, 37 insertions, 6 deletions
diff --git a/license.py b/license.py
index 853cdc0..e52d3f6 100644
--- a/license.py
+++ b/license.py
@@ -4,6 +4,9 @@ class License(object):
self.name = name
def __iter__(self):
+ yield self
+
+ def flatten(self):
yield self.name
def __str__(self, depth=0):
@@ -18,7 +21,11 @@ class Licenses(object):
def __iter__(self):
for x in self.xs:
- for y in iter(x):
+ yield x
+
+ def flatten(self):
+ for x in self.xs:
+ for y in x.flatten():
yield y
def __str__(self, depth=0):
@@ -42,18 +49,42 @@ class AnyLicense(Licenses):
def parse_licenses(s):
"""
- >>> print parse_licenses("X")
+ >>> ls = parse_licenses("X")
+ >>> ls
+ <License X>
+ >>> print ls
X
- >>> print parse_licenses("X or Y or Z")
+ >>> list(ls)
+ [<License X>]
+ >>> list(ls.flatten())
+ ['X']
+
+ >>> ls = parse_licenses("X or Y or Z")
+ >>> ls
+ <AnyLicense [<License X>, <License Y>, <License Z>]>
+ >>> print ls
X | Y | Z
- >>> print parse_licenses("X and Y and Z")
+ >>> list(ls)
+ [<License X>, <License Y>, <License Z>]
+ >>> list(ls.flatten())
+ ['X', 'Y', 'Z']
+
+ >>> ls = parse_licenses("X and Y and Z")
+ >>> ls
+ <AllLicenses [<License X>, <License Y>, <License Z>]>
+ >>> print ls
X & Y & Z
- >>> parse_licenses("X or Y and Z")
- <AnyLicense [<License X>, <AllLicenses [<License Y>, <License Z>]>]>
+ >>> list(ls)
+ [<License X>, <License Y>, <License Z>]
+ >>> list(ls.flatten())
+ ['X', 'Y', 'Z']
+
>>> print parse_licenses("X or Y and Z")
X | (Y & Z)
+
>>> print parse_licenses("X and Y or Z")
(X & Y) | Z
+
>>> print parse_licenses("X, and Y or Z")
X & (Y | Z)
"""