Skip to content

Commit 24865ab

Browse files
committed
added config import and export helpers
1 parent c5a01f0 commit 24865ab

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

qgist/config.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,38 @@ def _save(self):
179179

180180
if backup_fn is not None:
181181
os.unlink(backup_fn)
182+
183+
@staticmethod
184+
def import_config(fn):
185+
186+
if not isinstance(fn, str):
187+
raise QgistTypeError(translate('global', '"fn" must be str. (config import)'))
188+
if not os.path.exists(fn):
189+
raise QgistValueError(translate('global', '"fn" must exists. (config import)'))
190+
if not os.path.isfile(fn):
191+
raise QgistValueError(translate('global', '"fn" must be a file. (config import)'))
192+
193+
with open(fn, 'r', encoding = 'utf-8') as f:
194+
raw = f.read()
195+
196+
try:
197+
value = json.loads(raw)
198+
except:
199+
raise QgistValueError(translate('global', '"fn" does not contain valid JSON. (config import)'))
200+
201+
return value
202+
203+
@staticmethod
204+
def export_config(fn, value):
205+
206+
if not isinstance(fn, str):
207+
raise QgistTypeError(translate('global', '"fn" must be str. (config export)'))
208+
if not os.path.exists(os.path.dirname(fn)):
209+
raise QgistValueError(translate('global', 'Parent of "fn" must exists. (config export)'))
210+
if not os.path.isdir(os.path.dirname(fn)):
211+
raise QgistValueError(translate('global', 'Parent of "fn" must be a directory. (config export)'))
212+
if not config_class._check_value(value):
213+
raise QgistTypeError(translate('global', '"value" contains not allowed types. (config export)'))
214+
215+
with open(fn, 'w', encoding = 'utf-8') as f:
216+
f.write(json.dumps(svalue, indent = 4, sort_keys = True))

0 commit comments

Comments
 (0)