from json import load, dump from os import path import os #BASEPATH = "C:/dev/pshell/config/bernina_robot/adjustables_fs/" BASEPATH = "/sf/bernina/config/src/python/bernina_robot/adjustables_fs/" class AdjustableFS: def __init__(self, name=None, default_value=None, file_path=None): if file_path is None: file_path = BASEPATH + name self.file_path = file_path if not path.exists(self.file_path): if not path.exists(path.dirname(self.file_path)): os.mkdir(path.dirname(self.file_path)) self.write_value(default_value) self.name = name def get_current_value(self): with open(self.file_path, "r") as f: res = load(f) return res["value"] def write_value(self, value): with open(self.file_path, "w") as f: dump({"value": value}, f, indent=4) def __call__(self, value=None): if not value is None: self.write_value(value) else: return self.get_current_value() def __repr__(self): return "Current value of {} at: ".format(self.name) + str(self.get_current_value())