Skip to content

Commit

Permalink
search resource to actual resource
Browse files Browse the repository at this point in the history
  • Loading branch information
nanorepublica committed Nov 4, 2015
1 parent ec3abeb commit 441f25b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
50 changes: 50 additions & 0 deletions duedil/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
# DuedilApiClient v3 Pro + Credit
# @copyright 2014 Christian Ledermann
# @copyright 2015 Andrew Miller
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand All @@ -15,7 +16,56 @@
# License for the specific language governing permissions and limitations
# under the License.
#
#

import hashlib
import six

from collections import OrderedDict


class Cache(object):
"""
Basic in-memory cache implementation.
The key-values are stored internally as a dictionary. An existing
dictionary can be passed to the constructor to initialise.
"""

_raw_dict = {}

def __init__(self, raw_dict=None):
if not raw_dict:
self._raw_dict = {}
else:
self._raw_dict = raw_dict

def _cache_key(self, key, url_params):
cache_key = key
if url_params:
cache_key += six.text_type(OrderedDict(url_params))
return hashlib.md5(six.b(key)).hexdigest()

def get_url(self, url, url_params=None):
"""
get the value in cache or None if URL doesn't match any
:param url: The URL to query
:return: the value in cache or None
"""
cache_key = self._cache_key(url, url_params)
return self._raw_dict.get(cache_key, None)

def set_url(self, url, data, url_params=None):
"""
Store the data for given URL in cache, override any previously present value
:param url: The URL as key
:param data: The value to store
:return: Nothing
"""
cache_key = self._cache_key(url, url_params)
self._raw_dict[cache_key] = data


from dogpile.cache import make_region
Expand Down
7 changes: 5 additions & 2 deletions duedil/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ class Resource(object):
path = None
client_class = LiteClient

def __init__(self, rid, api_key=None, locale='uk', load=False, **kwargs):
def __init__(self, rid, api_key=None, locale='uk', load=False, client=None, **kwargs):
if not self.attribute_names:
raise NotImplementedError(
"Resources must include a list of allowed attributes")

self.rid = rid
assert(locale in ['uk', 'roi'])
self.locale = locale
self.client = self.client_class(api_key, sandbox=kwargs.pop('sandbox', False))
if client:
self.client = client
else:
self.client = self.client_class(api_key, sandbox=kwargs.pop('sandbox', False))

if load:
self.load()
Expand Down
18 changes: 16 additions & 2 deletions duedil/search/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

from importlib import import_module

class SearchResource(object):
attribute_names = None
locale = 'uk'
rid = None
path = None
result_obj = {}

def __init__(self, client, rid=None, locale='uk', **kwargs):
def __init__(self, client, id=None, locale='uk', load=False, **kwargs):
if not self.attribute_names:
raise NotImplementedError(
"Resources must include a list of allowed attributes")

self.rid = rid
self.rid = id
assert(locale in ['uk', 'roi'])
self.locale = locale
self.client = client
self.should_load = True if load else False
if load:
self.load()

if kwargs:
self._set_attributes(**kwargs)
Expand Down Expand Up @@ -43,5 +48,14 @@ def __getattr__(self, name):
if name in self.attribute_names:
self.load()
return super(SearchResource, self).__getattribute__(name)
elif name in self.result_obj.keys():
mod_path, klass_str = self.result_obj[name].rsplit('.', 1)
mod = import_module(mod_path)
klass = getattr(mod, klass_str)
return klass(self.rid, client=self.client, locale=self.locale, load=self.should_load)
else:
raise

# def get_object(self, object_cls):
# # this should get the id and create the appropriate resource and return it
# return object_cls(self.id, client=self.client, locale=self.locale, load=self.should_load)
3 changes: 3 additions & 0 deletions duedil/search/pro/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ class CompanySearchResult(SearchResource):
'name',
'company_url'
]
result_obj = {
'company': 'duedil.resources.pro.company.Company'
}
5 changes: 5 additions & 0 deletions duedil/search/pro/director.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ class DirectorSearchResult(SearchResource):
'directorships_url',
'companies_url',
]

result_obj = {
# this import path is incorrect.
'director': 'duedil.resources.pro.director.Director'
}

0 comments on commit 441f25b

Please sign in to comment.