Skip to content

Commit

Permalink
state & proxy: various fixes
Browse files Browse the repository at this point in the history
* load state provider from env if fromenv, plugins need to define load_provider
* type check readwrite / readonly lists (common mistake to give just str of single path)
* generic load_proxy function for async proxies
  • Loading branch information
lukasheinrich committed Oct 3, 2017
1 parent bfbf162 commit 650d079
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
7 changes: 5 additions & 2 deletions packtivity/backendutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import packtivity.asyncbackends as asyncbackends
import packtivity.syncbackends as syncbackends

def proxy_from_json(jsondata, best_effort_backend = True):
def proxy_from_json(jsondata, best_effort_backend = True, raise_on_unknown = False):
proxy, backend = None, None
if jsondata['proxyname'] == 'CeleryProxy':
from .asyncbackends import CeleryProxy
proxy = CeleryProxy.fromJSON(jsondata)
Expand All @@ -25,6 +26,8 @@ def proxy_from_json(jsondata, best_effort_backend = True):
proxyclass = getattr(module,proxyclass)
proxy = proxyclass.fromJSON(jsondata)
_, backend = backend_from_string('fromenv')
if not proxy and raise_on_unknown:
raise RuntimeError('unknown proxy type: %s', jsondata['proxyname'])
if best_effort_backend:
return proxy, backend
return proxy
Expand Down Expand Up @@ -62,4 +65,4 @@ def backend_from_string(backendstring):
module = importlib.import_module(module)
backendclass = getattr(module,backend)
return is_async, backendclass(**ctor_kwargs)
raise RuntimeError('Unknown Backend')
raise RuntimeError('Unknown Backend')
6 changes: 4 additions & 2 deletions packtivity/statecontexts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ def load_state(jsondata):
if 'PACKTIVITY_STATEPROVIDER' in os.environ:
module = importlib.import_module(os.environ['PACKTIVITY_STATEPROVIDER'])
return module.load_state(jsondata)


raise TypeError('unknown state type {}'.format(jsondata['state_type']))

def load_provider(jsondata):
if jsondata == None:
return None
if jsondata['state_provider_type'] == 'localfs_provider':
return LocalFSProvider.fromJSON(jsondata)
if 'PACKTIVITY_STATEPROVIDER' in os.environ:
module = importlib.import_module(os.environ['PACKTIVITY_STATEPROVIDER'])
return module.load_provider(jsondata)
raise TypeError('unknown provider type {}'.format(jsondata['state_provider_type']))
11 changes: 7 additions & 4 deletions packtivity/statecontexts/posixfs_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ 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'):
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)))
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 [])
Expand Down Expand Up @@ -66,7 +71,7 @@ def contextualize_data(self,data):
contextualizes string data by string interpolation.
replaces '{workdir}' placeholder with first readwrite directory
'''
try:
try:
workdir = self.readwrite[0]
return data.format(workdir = workdir)
except AttributeError:
Expand Down Expand Up @@ -146,7 +151,7 @@ def new_state(self,name):
newstate = LocalFSState(readwrite = new_readwrites, readonly = new_readonlies, identifier = new_identifier)

if self.ensure:
newstate.ensure()
newstate.ensure()
return newstate

def json(self):
Expand All @@ -160,5 +165,3 @@ def json(self):
@classmethod
def fromJSON(cls,jsondata):
return cls(LocalFSState.fromJSON(jsondata['base_state']), nest = jsondata['nest'], ensure = jsondata['ensure'])


0 comments on commit 650d079

Please sign in to comment.