From ba7734aaa527df2939ceb8e7189a13baef69dd39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 31 Mar 2016 11:36:45 +0200 Subject: [PATCH] Added CloudPath class for easier cloud browsing. This allows us to have a structured, well-defined way to point at a node in Pillar (and store its parent nodes). --- blender_cloud/pillar.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/blender_cloud/pillar.py b/blender_cloud/pillar.py index 70174be..902139b 100644 --- a/blender_cloud/pillar.py +++ b/blender_cloud/pillar.py @@ -4,6 +4,7 @@ import os import functools import logging from contextlib import closing, contextmanager +import pathlib import requests import requests.structures @@ -37,6 +38,34 @@ class PillarError(RuntimeError): """ +class CloudPath(pathlib.PurePosixPath): + """Cloud path, in the form of /project uuid/node uuid/node uuid/... + + The components are: + - the root '/' + - the project UUID + - zero or more node UUIDs. + """ + + @property + def project_uuid(self) -> str: + assert self.parts[0] == '/' + return self.parts[1] + + @property + def node_uuids(self) -> list: + assert self.parts[0] == '/' + return self.parts[2:] + + @property + def node_uuid(self) -> str: + node_uuids = self.node_uuids + + if not node_uuids: + return None + return node_uuids[-1] + + @contextmanager def with_existing_dir(filename: str, open_mode: str, encoding=None): """Opens a file, ensuring its directory exists."""