def save(self, indent=False): """Saves file to disk as json :param indent: If True format the json nicely (indent=2) :type indent: bool :return fullPath: The full path to the saved .json file :rtype fullPath: str """ root = self.root if not root: return path.Path() fullPath = root / self.relativePath filesystem.ensureFolderExists(os.path.dirname(str(fullPath))) output = copy.deepcopy(self) del output["root"] del output["relativePath"] exts = fullPath.getExtension(True) if not exts: fullPath = fullPath.setExtension("json", True) if not indent: filesystem.saveJson(output, str(fullPath)) else: filesystem.saveJson(output, str(fullPath), indent=2) return self.path()
def findSetting(self, relativePath, root=None, extension=None): """Finds a settings object by searching the roots in reverse order. The first path to exist will be the one to be resolved. If a root is specified and the root+relativePath exists then that will be returned instead :param relativePath: :type relativePath: str :param root: The Root name to search if root is None then all roots in reverse order will be search until a \ settings is found. :type root: str or None :return: :rtype: :class:`SettingObject` """ relativePath = path.Path(relativePath) if not relativePath.getExtension(True): relativePath = relativePath.setExtension(extension or self.extension) if root is not None: rootPath = self.roots.get(root) if rootPath is not None: fullpath = rootPath / relativePath if not fullpath.exists(): return SettingObject(rootPath, relativePath) return self.open(rootPath, relativePath) else: for name, p in reversed(self.roots.items()): # we're working with an ordered dict fullpath = p / relativePath if not fullpath.exists(): continue return self.open(p, relativePath) return SettingObject("", relativePath)
def __init__(self, root, relativePath=None, **kwargs): relativePath = path.Path(relativePath or "") if not relativePath.getExtension(True): relativePath = relativePath.setExtension(".json") kwargs["relativePath"] = relativePath kwargs["root"] = root super(SettingObject, self).__init__(**kwargs)
def open(self, root, relativePath, extension=None): relativePath = path.Path(relativePath) if not relativePath.getExtension(True): relativePath = relativePath.setExtension(extension or self.extension) fullPath = root / relativePath if not os.path.exists(fullPath): raise InvalidSettingsPath(fullPath) data = filesystem.loadJson(fullPath) return SettingObject(root, relativePath, **data)
def save(self): root = self.root if not root: return path.Path() fullPath = root / self.relativePath filesystem.ensureFolderExists(os.path.dirname(str(fullPath))) output = copy.deepcopy(self) del output["root"] del output["relativePath"] exts = fullPath.getExtension(True) if not exts: fullPath = fullPath.setExtension("json", True) filesystem.saveJson(output, str(fullPath)) return self.path()
def addRoot(self, fullPath, name): if name in self.roots: raise RootAlreadyExistsError("Root already exists: {}".format(name)) self.roots[name] = path.Path(fullPath)
def rootPath(self): if self.root: return self.root return path.Path()