Skip to content

Commit

Permalink
readonly is a @Property
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasheinrich committed Mar 27, 2018
1 parent b980c4b commit e3c3c2f
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packtivity/statecontexts/posixfs_context.py
Expand Up @@ -11,21 +11,32 @@ class LocalFSState(object):
'''
Local Filesyste State consisting of a number of readwrite and readonly directories
'''
def __init__(self,readwrite = None,readonly = None, dependencies = None, identifier = 'unidentified_state'):
def __init__(self,readwrite = None, readonly = None, dependencies = None, identifier = 'unidentified_state'):
try:
assert type(readwrite) in [list, type(None)]
assert type(readonly) in [list, type(None)]
except AssertionError:
raise TypeError('readwrite and readonly must be None or a list {} {}'.format(type(readwrite), type(readonly)))
raise TypeError('readwrite and readonly must be None or a list {} {}'.format(type(readonly)))
self._identifier = identifier
self.readwrite = list(map(os.path.realpath,readwrite) if readwrite else [])
self.readonly = list(map(os.path.realpath,readonly) if readonly else [])
self.add_readonly = list(map(os.path.realpath,readonly) if readonly else [])
self.dependencies = dependencies or []
self.datamodel = None

def __repr__(self):
return '<LocalFSState rw: {}, ro: {}>'.format(self.readwrite,self.readonly)

@property
def readonly(self):
readonlies = [x for x in self.add_readonly]
for d in self.dependencies or []:
if d.readwrite:
readonlies += d.readwrite # if dep has readwrite add those
else:
readonlies += d.readonly # else add the readonlies
return list(map(os.path.realpath,readonlies))


@property
def metadir(self):
if self.readwrite:
Expand All @@ -42,7 +53,7 @@ def reset(self):
'''
resets state by deleting readwrite directory contents (deletes tree and re-creates)
'''
for rw in self.readwrite:
for rw in self.readwrite + [self.metadir]:
if os.path.exists(rw):
shutil.rmtree(rw)
self.ensure()
Expand Down Expand Up @@ -94,15 +105,15 @@ def json(self):
'state_type': 'localfs',
'identifier': self.identifier(),
'readwrite': self.readwrite,
'readonly': self.readonly,
'add_readonly': self.add_readonly,
'dependencies': [x.json() for x in self.dependencies]
}

@classmethod
def fromJSON(cls,jsondata):
return cls(
readwrite = jsondata['readwrite'],
readonly = jsondata['readonly'],
readonly = jsondata['add_readonly'],
identifier = jsondata['identifier'],
dependencies = [LocalFSState.fromJSON(x) for x in jsondata['dependencies']]
)

0 comments on commit e3c3c2f

Please sign in to comment.