|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +import re |
| 5 | +from importlib import import_module |
| 6 | +try: |
| 7 | + import simplejson as json |
| 8 | +except ImportError: |
| 9 | + import json |
| 10 | + |
| 11 | +from pygithub3.exceptions import (DoesNotExists, UriInvalid, ValidationError, |
| 12 | + InvalidBodySchema) |
| 13 | +from pygithub3.resources.base import Raw |
| 14 | + |
| 15 | +ABS_IMPORT_PREFIX = 'pygithub3.requests' |
| 16 | + |
| 17 | + |
| 18 | +class Body(object): |
| 19 | + |
| 20 | + def __init__(self, content, schema, required): |
| 21 | + self.content = content |
| 22 | + self.schema = schema |
| 23 | + self.required = required |
| 24 | + |
| 25 | + def dumps(self): |
| 26 | + if not self.schema: |
| 27 | + return self.content or None |
| 28 | + return json.dumps(self.parse()) |
| 29 | + |
| 30 | + def parse(self): |
| 31 | + if not hasattr(self.content, 'items'): |
| 32 | + raise ValidationError("'%s' needs a content dictionary" |
| 33 | + % self.__class__.__name__) |
| 34 | + parsed = {key: self.content[key] for key in self.schema |
| 35 | + if key in self.content} |
| 36 | + for attr_required in self.required: |
| 37 | + if attr_required not in parsed: |
| 38 | + raise ValidationError("'%s' attribute is required" % |
| 39 | + attr_required) |
| 40 | + if not parsed[attr_required]: |
| 41 | + raise ValidationError("'%s' attribute can't be empty" % |
| 42 | + attr_required) |
| 43 | + return parsed |
| 44 | + |
| 45 | + |
| 46 | +class Request(object): |
| 47 | + """ """ |
| 48 | + |
| 49 | + uri = '' |
| 50 | + resource = Raw |
| 51 | + body_schema = {} |
| 52 | + |
| 53 | + def __init__(self, **kwargs): |
| 54 | + """ """ |
| 55 | + self.body = kwargs.pop('body', None) |
| 56 | + self.args = kwargs |
| 57 | + self.clean() |
| 58 | + |
| 59 | + def clean(self): |
| 60 | + self.uri = self.clean_uri() or self.uri |
| 61 | + self.body = Body(self.clean_body(), **self.clean_valid_body()) |
| 62 | + |
| 63 | + def clean_body(self): |
| 64 | + return self.body |
| 65 | + |
| 66 | + def clean_uri(self): |
| 67 | + return None |
| 68 | + |
| 69 | + def clean_valid_body(self): |
| 70 | + schema = set(self.body_schema.get('schema', ())) |
| 71 | + required = set(self.body_schema.get('required', ())) |
| 72 | + if not required.issubset(schema): |
| 73 | + raise InvalidBodySchema( |
| 74 | + "'%s:valid_body' attribute is invalid. " |
| 75 | + "'%s required' isn't a subset of '%s schema'" % ( |
| 76 | + self.__class__.__name__, required, schema)) |
| 77 | + return dict(schema=schema, required=required) |
| 78 | + |
| 79 | + def __getattr__(self, name): |
| 80 | + return self.args.get(name) |
| 81 | + |
| 82 | + def __str__(self): |
| 83 | + return self.populate_uri() |
| 84 | + |
| 85 | + def populate_uri(self): |
| 86 | + try: |
| 87 | + populated_uri = self.uri.format(**self.args) |
| 88 | + except KeyError: |
| 89 | + raise ValidationError( |
| 90 | + "'%s' request wasn't be able to populate the uri '%s' with " |
| 91 | + "'%s' args" % (self.__class__.__name__, self.uri, self.args)) |
| 92 | + return str(populated_uri).strip('/') |
| 93 | + |
| 94 | + def get_body(self): |
| 95 | + return self.body.dumps() |
| 96 | + |
| 97 | + |
| 98 | +class Factory(object): |
| 99 | + """ """ |
| 100 | + |
| 101 | + import_pattern = re.compile(r'^(\w+\.)+\w+$') |
| 102 | + |
| 103 | + def validate(func): |
| 104 | + """ """ |
| 105 | + |
| 106 | + def wrapper(self, request_uri, **kwargs): |
| 107 | + if not Factory.import_pattern.match(request_uri): |
| 108 | + raise UriInvalid("'%s' isn't valid form" % request_uri) |
| 109 | + return func(self, request_uri.lower(), **kwargs) |
| 110 | + return wrapper |
| 111 | + |
| 112 | + def dispatch(func): |
| 113 | + """ """ |
| 114 | + |
| 115 | + def wrapper(self, request_uri, **kwargs): |
| 116 | + module_chunk, s, request_chunk = request_uri.rpartition('.') |
| 117 | + try: |
| 118 | + # TODO: CamelCase and under_score support, now only Class Name |
| 119 | + module = import_module('%s.%s' |
| 120 | + % (ABS_IMPORT_PREFIX, module_chunk)) |
| 121 | + request = getattr(module, request_chunk.capitalize()) |
| 122 | + except ImportError: |
| 123 | + raise DoesNotExists("'%s' module does not exists" |
| 124 | + % module_chunk) |
| 125 | + except AttributeError: |
| 126 | + raise DoesNotExists( |
| 127 | + "'%s' request doesn't exists into '%s' module" |
| 128 | + % (request_chunk.capitalize(), module_chunk)) |
| 129 | + return func(self, request, **kwargs) |
| 130 | + return wrapper |
| 131 | + |
| 132 | + @validate |
| 133 | + @dispatch |
| 134 | + def __call__(self, request='', **kwargs): |
| 135 | + request = request(**kwargs) |
| 136 | + assert isinstance(request, Request) |
| 137 | + return request |
0 commit comments