diff options
-rw-r--r-- | lesana/collection.py | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/lesana/collection.py b/lesana/collection.py index 2ade0be..50e8299 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -49,28 +49,26 @@ class Entry(object): return d def empty_data(self): - data = '' - for field in self.collection.settings['fields']: - if not field.get('help', None) is None: - data += "# {name} ({type}): {help}\n".format(**field) - t = field['type'] - if field.get('default', None): - data += "{name}: '{default}'\n".format(**field) - elif t == 'string': - data += "{name}: ''\n".format(**field) - elif t == 'text': - data += "{name}: |\n .\n".format(**field) - elif t == 'integer': - data += "{name}: 0\n".format(**field) - elif t == 'float': - data += "{name}: 0.0\n".format(**field) - elif t == 'decimal': - data += "{name}: 0.00\n".format(**field) - elif t == 'list': - data += "{name}: []\n".format(**field) + data = self.collection.yaml.load("{}") + for name, field in self.collection.fields.items(): + if field.field.get('default', None): + data[name] = field.field['default'] else: - data += "{name}: \n".format(**field) - return self.collection.yaml.load(data) + data[name] = field.empty() + if field.field.get('help', None) is not None: + comment = "# {name} ({type}): {help}\n".format(**field.field) + try: + data.yaml_set_comment_before_after_key( + key=name, + before=comment, + indent=0 + ) + except AttributeError: + logging.warning( + "Not adding comments because they are not" + "supported by the yaml loader." + ) + return data @property def yaml_data(self): @@ -81,8 +79,9 @@ class Entry(object): for field in self.collection.settings['fields']: if field['type'] == 'decimal': v = to_dump.get(field['name'], '') - if v: + if v is not None: to_dump[field['name']] = str(v) + s_io = io.StringIO() self.collection.yaml.dump(to_dump, s_io) return s_io.getvalue() @@ -136,6 +135,7 @@ class Collection(object): self.itemdir = os.path.join(self.basedir, itemdir) self.yaml = ruamel.yaml.YAML() self.yaml.preserve_quotes = True + self.yaml.typ = 'rt' try: with open(os.path.join(self.basedir, 'settings.yaml')) as fp: self.settings = self.yaml.load(fp) |