-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
774 additions
and
813 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
__version__ = "1.0" | ||
|
||
from .benchlingapi import BenchlingAPI, BenchlingAPIException, AquariumLoginError, BenchlingLoginError | ||
from .convert import * | ||
#from .benchlingportal import BenchlingPortal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import inflection | ||
from benchlingapi.exceptions import ModelNotFoundError | ||
|
||
|
||
class ModelRegistry(type): | ||
"""Stores a list of models that can be accessed by name.""" | ||
models = {} | ||
|
||
def __init__(cls, name, bases, selfdict): | ||
"""Saves model to the registry""" | ||
super().__init__(name, bases, selfdict) | ||
if not name == "ModelBase": | ||
ModelRegistry.models[name] = cls | ||
|
||
@staticmethod | ||
def get_model(model_name): | ||
"""Gets model by model_name""" | ||
if model_name not in ModelRegistry.models: | ||
raise ModelNotFoundError( | ||
"Model \"{}\" not found in ModelRegistry.".format(model_name)) | ||
else: | ||
return ModelRegistry.models[model_name] | ||
|
||
def __getattr__(cls, item): | ||
""" | ||
Special warning for attribute errors. | ||
Its likely that user may have wanted to use a model interface instead of | ||
the Base class. | ||
""" | ||
raise AttributeError("'{0}' has no attribute '{1}'. Method may be a ModelInterface method." | ||
" Did you mean '<yoursession>.{0}.{1}'?" | ||
.format(cls.__name__, item)) | ||
|
||
|
||
class ModelBase(object): | ||
class ModelBase(object, metaclass=ModelRegistry): | ||
def __init__(self): | ||
self.http = None | ||
self.data = None | ||
|
||
@classmethod | ||
def path(cls): | ||
name = inflection.underscore(cls.__name__) | ||
name = inflection.dasherize(name) | ||
name = inflection.pluralize(name) | ||
return name | ||
|
||
@classmethod | ||
def all(cls, http): | ||
return http.post(cls.path()) | ||
|
||
@classmethod | ||
def find(cls, http, model_id): | ||
return http.get(f"{cls.path()}/{model_id}") | ||
|
||
@classmethod | ||
def delete(cls, http, model_id): | ||
return http.delete(f"{cls.path()}/{model_id}") | ||
|
||
@classmethod | ||
def create(cls, http, json_data): | ||
return http.post(f"{cls.path()}", json_data=json_data) | ||
|
||
|
||
class ModelInterface(object): | ||
|
||
def __init__(self, model_name, http): | ||
self.model_name = model_name | ||
self.http = http | ||
self.model = ModelRegistry.get_model(model_name) | ||
|
||
def all(self): | ||
return self.model.post(self.http) | ||
|
||
def find(self, model_id): | ||
return self.model.find(self.http, model_id) | ||
|
||
def delete(self, model_id): | ||
return self.model.delete(self.http, model_id) | ||
|
||
def create(self, json_data): | ||
return self.model.create(self.http, json_data) |
Oops, something went wrong.