From b6f1902c7c767104700ea86896eed9b27d389344 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Tue, 14 Feb 2017 16:36:41 +0800 Subject: manifest.py: Add parameter 'splitlist' to gets() for CSV export --- scripts/collect_yaml.py | 2 +- scripts/manifest.py | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) (limited to 'scripts') diff --git a/scripts/collect_yaml.py b/scripts/collect_yaml.py index 0fb5ee6..05d4684 100755 --- a/scripts/collect_yaml.py +++ b/scripts/collect_yaml.py @@ -50,7 +50,7 @@ def main(): results = [] for fp in args.infile: manifest = Manifest(fp) - res = manifest.gets(keys) + res = manifest.gets(keys, splitlist=True) if args.verbose: print("FILE:{0}: {1}".format(fp, list(res.values())), file=sys.stderr) diff --git a/scripts/manifest.py b/scripts/manifest.py index 937cb43..74e737e 100755 --- a/scripts/manifest.py +++ b/scripts/manifest.py @@ -66,14 +66,21 @@ class Manifest: else: raise KeyError("manifest doesn't have item: '%s'" % key) - def gets(self, keys, default=None): + def gets(self, keys, default=None, splitlist=False): """ Get the value of the specified item in the manifest. + TODO: splitlist + Parameters ---------- keys : list[str] A list of keys specifying the items to be requested. + default : optional + The default value to return if the item not exists. + splitlist : bool, optional + Split the item value if it is a list, making it is easier + to export as CSV format. Returns ------- @@ -86,6 +93,16 @@ class Manifest: data = OrderedDict([ (key, self.manifest.get(key, default)) for key in keys ]) + if splitlist: + ds = OrderedDict() + for k, v in data.items(): + if isinstance(v, list): + for i, vi in enumerate(v): + ki = "{0}[{1}]".format(k, i) + ds[ki] = vi + else: + ds[k] = v + data = ds return data def getpath(self, key): @@ -155,7 +172,13 @@ class Manifest: try: v = float(value) except ValueError: - v = value + # string/boolean + if value.lower() in ["true", "yes"]: + v = True + elif value.lower() in ["false", "no"]: + v = False + else: + v = value # string parsed_values.append(v) # if len(parsed_values) == 1: -- cgit v1.2.2