34 import simplejson
as json
40 from classes.logger
import log
41 from classes
import info
44 path_regex = re.compile(
r'\"(image|path)\":.*?\"(.*?)\"', re.UNICODE)
58 self.data_type =
"json data"
66 if type(self._data) == list:
70 for item
in self._data:
71 if "setting" in item
and "value" in item:
72 user_values[item[
"setting"].lower()] = item[
"value"]
75 return copy.deepcopy(user_values.get(key,
None))
78 return copy.deepcopy(self._data.get(key,
None))
82 def set(self, key, value):
86 if type(self._data) == list:
90 for item
in self._data:
91 if "setting" in item
and "value" in item:
92 user_values[item[
"setting"].lower()] = item
95 user_values[key][
"value"] = value
99 self._data[key] = value
104 def merge_settings(self, default, user):
107 if type(default) == list:
112 if "setting" in item
and "value" in item:
113 user_values[item[
"setting"]] = item[
"value"]
117 user_value = user_values.get(item[
"setting"],
None)
118 if user_value !=
None:
119 item[
"value"] = user_value
129 user[key] = default[key]
136 def read_from_file(self, file_path, path_mode="ignore"):
138 with open(file_path,
'r') as f:
141 if path_mode ==
"absolute":
143 contents = self.convert_paths_to_absolute(file_path, contents)
144 return json.loads(contents, strict=
False)
145 except Exception
as ex:
146 msg = (
"Couldn't load {} file: {}".format(self.data_type, ex))
149 msg = (
"Couldn't load {} file, no data.".format(self.data_type))
155 def write_to_file(self, file_path, data, path_mode="ignore", previous_path=None):
157 contents = json.dumps(data, indent=4)
158 if path_mode ==
"relative":
160 contents = self.convert_paths_to_relative(file_path, previous_path, contents)
161 with open(file_path,
'w')
as f:
163 except Exception
as ex:
164 msg = (
"Couldn't save {} file:\n{}\n{}".format(self.data_type, file_path, ex))
170 def replace_string_to_absolute(self, match):
171 key = match.groups(0)[0]
172 path = match.groups(0)[1]
175 utf_path = json.loads(
'"%s"' % path, encoding=
"utf-8")
176 if "@transitions" not in utf_path:
178 new_path = os.path.abspath(os.path.join(path_context.get(
"existing_project_folder",
""), utf_path))
179 new_path = json.dumps(new_path)
180 return '"%s": %s' % (key, new_path)
184 new_path = path.replace(
"@transitions", os.path.join(info.PATH,
"transitions"))
185 new_path = json.dumps(new_path)
186 return '"%s": %s' % (key, new_path)
190 def convert_paths_to_absolute(self, file_path, data):
193 path_context[
"new_project_folder"] = os.path.dirname(file_path)
194 path_context[
"existing_project_folder"] = os.path.dirname(file_path)
197 data = re.sub(path_regex, self.replace_string_to_absolute, data)
199 except Exception
as ex:
200 log.error(
"Error while converting relative paths to absolute paths: %s" % str(ex))
206 def replace_string_to_relative(self, match):
207 key = match.groups(0)[0]
208 path = match.groups(0)[1]
209 utf_path = json.loads(
'"%s"' % path, encoding=
"utf-8")
210 folder_path, file_path = os.path.split(os.path.abspath(utf_path))
213 if info.THUMBNAIL_PATH
in folder_path:
215 new_path = os.path.join(
"thumbnail", file_path).replace(
"\\",
"/")
216 new_path = json.dumps(new_path)
217 return '"%s": %s' % (key, new_path)
220 elif os.path.join(info.PATH,
"transitions")
in folder_path:
222 folder_path, category_path = os.path.split(folder_path)
225 new_path = os.path.join(
"@transitions", category_path, file_path).replace(
"\\",
"/")
226 new_path = json.dumps(new_path)
227 return '"%s": %s' % (key, new_path)
232 orig_abs_path = os.path.abspath(utf_path)
235 orig_abs_folder = os.path.split(orig_abs_path)[0]
238 new_rel_path_folder = os.path.relpath(orig_abs_folder, path_context.get(
"new_project_folder",
""))
239 new_rel_path = os.path.join(new_rel_path_folder, file_path).replace(
"\\",
"/")
240 new_rel_path = json.dumps(new_rel_path)
241 return '"%s": %s' % (key, new_rel_path)
245 def convert_paths_to_relative(self, file_path, previous_path, data):
248 path_context[
"new_project_folder"] = os.path.dirname(file_path)
249 path_context[
"existing_project_folder"] = os.path.dirname(file_path)
251 path_context[
"existing_project_folder"] = os.path.dirname(previous_path)
254 data = re.sub(path_regex, self.replace_string_to_relative, data)
256 except Exception
as ex:
257 log.error(
"Error while converting absolute paths to relative paths: %s" % str(ex))