comparison frontends/src/jp/cmd_blog.py @ 1876:1088bf7b28e7

jp (blog/edit): metadata file is reused if it already exists
author Goffi <goffi@goffi.org>
date Fri, 04 Mar 2016 10:33:28 +0100
parents 658824755a0c
children a97db84c048d
comparison
equal deleted inserted replaced
1875:c55220fc03e4 1876:1088bf7b28e7
115 @param mb_data(dict, None): microblog metadata (for existing items) 115 @param mb_data(dict, None): microblog metadata (for existing items)
116 @return (tuple[dict, str]): merged metadata put originaly in metadata file 116 @return (tuple[dict, str]): merged metadata put originaly in metadata file
117 and path to temporary metadata file 117 and path to temporary metadata file
118 """ 118 """
119 # we first construct metadata from edited item ones and CLI argumments 119 # we first construct metadata from edited item ones and CLI argumments
120 mb_data = {} if mb_data is None else mb_data.copy() 120 # or re-use the existing one if it exists
121 meta_file_path = os.path.splitext(content_file_path)[0] + METADATA_SUFF
122 if os.path.exists(meta_file_path):
123 self.disp(u"Metadata file already exists, we re-use it")
124 try:
125 with open(meta_file_path, 'rb') as f:
126 mb_data = json.load(f)
127 except (OSError, IOError, ValueError) as e:
128 self.disp(u"Can't read existing metadata file at {path}, aborting: {reason}".format(
129 path=meta_file_path, reason=e), error=True)
130 self.host.quit(1)
131 else:
132 mb_data = {} if mb_data is None else mb_data.copy()
133
134 # in all cases, we want to remove unwanted keys
121 for key in KEY_TO_REMOVE_METADATA: 135 for key in KEY_TO_REMOVE_METADATA:
122 try: 136 try:
123 del mb_data[key] 137 del mb_data[key]
124 except KeyError: 138 except KeyError:
125 pass 139 pass
140 # and override metadata with command-line arguments
126 mb_data['allow_comments'] = C.boolConst(not self.args.no_comment) 141 mb_data['allow_comments'] = C.boolConst(not self.args.no_comment)
127 if self.args.tag: 142 if self.args.tag:
128 common.iter2dict('tag', self.args.tag, mb_data) 143 common.iter2dict('tag', self.args.tag, mb_data)
129 if self.args.title is not None: 144 if self.args.title is not None:
130 mb_data['title'] = self.args.title 145 mb_data['title'] = self.args.title
131 146
132 # the we create the file and write metadata there, as JSON dict 147 # then we create the file and write metadata there, as JSON dict
133 meta_file_path = os.path.splitext(content_file_path)[0] + METADATA_SUFF
134 # XXX: if we port jp one day on Windows, O_BINARY may need to be added here 148 # XXX: if we port jp one day on Windows, O_BINARY may need to be added here
135 if os.path.exists(meta_file_path): 149 if os.path.exists(meta_file_path):
136 self.disp(u"metadata file {} already exists, this should not happen! Cancelling...", error=True) 150 self.disp(u"metadata file {} already exists, this should not happen! Cancelling...", error=True)
137 with os.fdopen(os.open(meta_file_path, os.O_RDWR | os.O_CREAT ,0o600), 'w+b') as f: 151 with os.fdopen(os.open(meta_file_path, os.O_RDWR | os.O_CREAT ,0o600), 'w+b') as f:
138 # we need to use an intermediate unicode buffer to write to the file unicode without escaping characters 152 # we need to use an intermediate unicode buffer to write to the file unicode without escaping characters