jp (blog/edit): metadata file is reused if it already exists

This commit is contained in:
Goffi 2016-03-04 10:33:28 +01:00
commit f9d691fd26

View file

@ -117,20 +117,34 @@ class Edit(base.CommandBase):
and path to temporary metadata file
"""
# we first construct metadata from edited item ones and CLI argumments
mb_data = {} if mb_data is None else mb_data.copy()
# or re-use the existing one if it exists
meta_file_path = os.path.splitext(content_file_path)[0] + METADATA_SUFF
if os.path.exists(meta_file_path):
self.disp(u"Metadata file already exists, we re-use it")
try:
with open(meta_file_path, 'rb') as f:
mb_data = json.load(f)
except (OSError, IOError, ValueError) as e:
self.disp(u"Can't read existing metadata file at {path}, aborting: {reason}".format(
path=meta_file_path, reason=e), error=True)
self.host.quit(1)
else:
mb_data = {} if mb_data is None else mb_data.copy()
# in all cases, we want to remove unwanted keys
for key in KEY_TO_REMOVE_METADATA:
try:
del mb_data[key]
except KeyError:
pass
# and override metadata with command-line arguments
mb_data['allow_comments'] = C.boolConst(not self.args.no_comment)
if self.args.tag:
common.iter2dict('tag', self.args.tag, mb_data)
if self.args.title is not None:
mb_data['title'] = self.args.title
# the we create the file and write metadata there, as JSON dict
meta_file_path = os.path.splitext(content_file_path)[0] + METADATA_SUFF
# then we create the file and write metadata there, as JSON dict
# XXX: if we port jp one day on Windows, O_BINARY may need to be added here
if os.path.exists(meta_file_path):
self.disp(u"metadata file {} already exists, this should not happen! Cancelling...", error=True)