diff options
author | 2012-02-06 20:19:42 +0100 | |
---|---|---|
committer | 2012-02-06 20:19:42 +0100 | |
commit | 29d7c5641ce8a4099e23f50e965f1c483e993f39 (patch) | |
tree | 17c3e03c153beea88f213e3ae2fde80d4a67a473 /pygithub3/requests/__init__.py | |
parent | User service complete (diff) | |
download | python-github3-29d7c5641ce8a4099e23f50e965f1c483e993f39.tar.xz python-github3-29d7c5641ce8a4099e23f50e965f1c483e993f39.zip |
Rename ghrequest to requests
I think it's a redundant name. Only needs to move out from core module
to avoid imports problems with `requests` library
* Also change some in setup.py
Diffstat (limited to 'pygithub3/requests/__init__.py')
-rw-r--r-- | pygithub3/requests/__init__.py | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/pygithub3/requests/__init__.py b/pygithub3/requests/__init__.py new file mode 100644 index 0000000..725ea12 --- /dev/null +++ b/pygithub3/requests/__init__.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +import re +from importlib import import_module +try: + import simplejson as json +except ImportError: + import json + +ABS_IMPORT_PREFIX = 'pygithub3.requests' + + +class RequestNotFound(Exception): + pass + + +class RequestUriInvalid(Exception): + pass + + +class RequestValidationError(Exception): + pass + + +class Request(object): + """ """ + + def __init__(self, args): + """ """ + self.args = args + self.validate() + self.uri = self.set_uri() + + def validate(self): + raise NotImplementedError + + def set_uri(self): + raise NotImplementedError + + def get_data(self): + raise NotImplementedError + + def get_uri(self): + return str(self.uri).strip('/') + + def get_resource(self): + return getattr(self, 'resource', '') + + def __getattr__(self, name): + return self.args.get(name) + + def __str__(self): + return self.get_uri() + + def _parse_simple_dict(self, to_parse): + if not hasattr(to_parse, 'items'): + raise RequestValidationError("'%s' needs a data dictionary" + % self.__class__.__name__) + update_params = { + valid_key: to_parse[valid_key] + for valid_key in self.valid + if valid_key in to_parse} + return update_params + + +class Factory(object): + """ """ + + import_pattern = re.compile(r'^(\w+\.)+\w+$') + + def __init__(self): + """ """ + self.args = {} + + def config_with(self, **kwargs): + self.args = kwargs + + def clear_config(self): + self.args = {} + + def __validate(func): + """ """ + + def wrapper(self, request_uri): + if not Factory.import_pattern.match(request_uri): + raise RequestUriInvalid("'%s' isn't valid form" % request_uri) + return func(self, request_uri.lower()) + return wrapper + + def __dispatch(func): + """ """ + + def wrapper(self, request_uri): + module_chunk, s, request_chunk = request_uri.rpartition('.') + try: + # TODO: CamelCase and under_score support, now only Class Name + module = import_module('%s.%s' + % (ABS_IMPORT_PREFIX, module_chunk)) + request = getattr(module, request_chunk.capitalize()) + except ImportError: + raise RequestNotFound("'%s' module does not exists" + % module_chunk) + except AttributeError: + raise RequestNotFound( + "'%s' request doesn't exists into '%s' module" + % (request_chunk.capitalize(), module_chunk)) + return func(self, request) + return wrapper + + @__validate + @__dispatch + def __call__(self, request=''): + request = request(self.args) + self.clear_config() + assert isinstance(request, Request) + return request |