Created
January 22, 2012 23:43
-
-
Save DamianZaremba/1659407 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
""" | |
I was thinking the usage of this to be something more like | |
ah = actionHandler() | |
try: | |
action = ah.getAction("test.something.bob") | |
except InvalidController: | |
pass | |
except InvalidClass: | |
pass | |
except InvalidAction: | |
pass | |
try: | |
result = action() | |
except: | |
pass # Something uber bad happended | |
""" | |
class InvalidController(Exception): | |
def __int__(self, message=""): | |
self.message = message | |
def __str__(self): | |
return repr(self.message) | |
class InvalidClass(Exception): | |
def __int__(self, message=""): | |
self.message = message | |
def __str__(self): | |
return repr(self.message) | |
class InvalidAction(Exception): | |
def __int__(self, message=""): | |
self.message = message | |
def __str__(self): | |
return repr(self.message) | |
class ClassError(Exception): | |
def __int__(self, message=""): | |
self.message = message | |
def __str__(self): | |
return repr(self.message) | |
import traceback | |
class actionHandler: | |
""" | |
Simple wrapper around the module loading - saves code dublication | |
""" | |
def __init__(self): | |
""" | |
Sets up this namespace | |
@brief Class init | |
@return Boolean | |
Returns True if successful or False if not successful. | |
""" | |
self.data = {} | |
self.logger = False | |
def fromServer(self, client_uid): | |
""" | |
Takes client id and loads the relevent stuff from the datastore | |
@brief Loads task info from the datastore | |
@param client_uid String | |
Client UID - used to load the relevent data from the datastore | |
@return Boolean | |
Returns True if successful or False if not successful. | |
""" | |
self.client_data = sdata.get_cdata(client_uid) | |
# TODO | |
# Add stuff to self.data as needed | |
self.data = { | |
'options': self.client_data['request_data']['request_args'], | |
} | |
def fromQueue(self, queue_id): | |
""" | |
Takes queue id and loads the relevent stuff from the datastore | |
@brief Loads task info from the datastore | |
@param queue_id String | |
Queue ID - used to load the relevent data from the datastore | |
@return Boolean | |
Returns True if successful or False if not successful. | |
""" | |
# TODO | |
# Add stuff to self.data as needed | |
self.data = { | |
'options': [], | |
} | |
def getAction(self, module_name): | |
""" | |
Returns the action object or throws the relevent exception | |
@brief Returns action | |
@param module_name String | |
@return Object | |
Action as an object | |
""" | |
mparts = module_name.split(".") | |
try: | |
rmodule = "modules.%s" % '.'.join(mparts[:-1]) | |
rcontroller = "%s%s" % (mparts[-2][0].upper(), mparts[-2][1:].lower()) | |
raction = str(mparts[-1]) | |
except IndexError: | |
raise InvalidController() | |
# Try and load the modules namespace | |
try: | |
_modules = __import__(rmodule) | |
except ImportError: | |
raise InvalidController() | |
# Try and load the requested controller | |
try: | |
_controller = getattr(_modules, "test") | |
except AttributeError as e: | |
raise InvalidController() | |
# Try and get the class from the controller | |
try: | |
_class = getattr(_controller, rcontroller) | |
except AttributeError: | |
raise InvalidClass() | |
try: | |
_class = _class(self.logger, self.data) | |
except: | |
raise ClassError() | |
# Try and get the function from the class | |
try: | |
_action = getattr(_class, raction) | |
except AttributeError: | |
raise InvalidAction() | |
return _action |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment