diff options
author | Aaron LI <aaronly.me@outlook.com> | 2017-02-14 16:36:41 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2017-02-17 01:16:46 +0800 |
commit | b6f1902c7c767104700ea86896eed9b27d389344 (patch) | |
tree | 889d6cbad5b5df3efa0ec06cc0dc8d6aa4695fa9 | |
parent | 9556e0d27b8d592d9d6bba0b423405bf9a44b2b8 (diff) | |
download | chandra-acis-analysis-b6f1902c7c767104700ea86896eed9b27d389344.tar.bz2 |
manifest.py: Add parameter 'splitlist' to gets() for CSV export
-rwxr-xr-x | scripts/collect_yaml.py | 2 | ||||
-rwxr-xr-x | scripts/manifest.py | 27 |
2 files changed, 26 insertions, 3 deletions
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: |