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).
This commit is contained in:
Sybren A. Stüvel 2016-03-31 11:36:45 +02:00
parent 364cc75548
commit ba7734aaa5

View File

@ -4,6 +4,7 @@ import os
import functools import functools
import logging import logging
from contextlib import closing, contextmanager from contextlib import closing, contextmanager
import pathlib
import requests import requests
import requests.structures 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 @contextmanager
def with_existing_dir(filename: str, open_mode: str, encoding=None): def with_existing_dir(filename: str, open_mode: str, encoding=None):
"""Opens a file, ensuring its directory exists.""" """Opens a file, ensuring its directory exists."""