diff options
| author | Roman Haritonov <reclosedev@gmail.com> | 2013-01-12 18:01:00 +0400 |
|---|---|---|
| committer | Roman Haritonov <reclosedev@gmail.com> | 2013-01-12 18:01:00 +0400 |
| commit | d2a89822e050f6cfdf3675e5bcb8a48deb570aab (patch) | |
| tree | 3a0182b20d743e7d0678a1e8f305a2a0b62f22a4 | |
| parent | 92decc4507ba996c7d36d205f9827192312bab33 (diff) | |
| download | requests-cache-d2a89822e050f6cfdf3675e5bcb8a48deb570aab.tar.gz | |
move storage implementations to package
| -rw-r--r-- | requests_cache/backends/mongo.py | 2 | ||||
| -rw-r--r-- | requests_cache/backends/sqlite.py | 2 | ||||
| -rw-r--r-- | requests_cache/backends/storage/__init__.py | 0 | ||||
| -rw-r--r-- | requests_cache/backends/storage/dbdict.py (renamed from requests_cache/backends/dbdict.py) | 0 | ||||
| -rw-r--r-- | requests_cache/backends/storage/mongodict.py (renamed from requests_cache/backends/mongodict.py) | 148 | ||||
| -rw-r--r-- | tests/test_dbdict.py | 2 | ||||
| -rw-r--r-- | tests/test_mongodict.py | 2 |
7 files changed, 78 insertions, 78 deletions
diff --git a/requests_cache/backends/mongo.py b/requests_cache/backends/mongo.py index 2a78f46..084d2ac 100644 --- a/requests_cache/backends/mongo.py +++ b/requests_cache/backends/mongo.py @@ -7,7 +7,7 @@ ``mongo`` cache backend """ from .base import BaseCache -from .mongodict import MongoDict, MongoPickleDict +from .storage.mongodict import MongoDict, MongoPickleDict class MongoCache(BaseCache): diff --git a/requests_cache/backends/sqlite.py b/requests_cache/backends/sqlite.py index cd99eb1..5e56b22 100644 --- a/requests_cache/backends/sqlite.py +++ b/requests_cache/backends/sqlite.py @@ -7,7 +7,7 @@ ``sqlite3`` cache backend """ from .base import BaseCache -from .dbdict import DbDict, DbPickleDict +from .storage.dbdict import DbDict, DbPickleDict class DbCache(BaseCache): diff --git a/requests_cache/backends/storage/__init__.py b/requests_cache/backends/storage/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/requests_cache/backends/storage/__init__.py diff --git a/requests_cache/backends/dbdict.py b/requests_cache/backends/storage/dbdict.py index ffc7b18..ffc7b18 100644 --- a/requests_cache/backends/dbdict.py +++ b/requests_cache/backends/storage/dbdict.py diff --git a/requests_cache/backends/mongodict.py b/requests_cache/backends/storage/mongodict.py index 06877c7..172d9a0 100644 --- a/requests_cache/backends/mongodict.py +++ b/requests_cache/backends/storage/mongodict.py @@ -1,74 +1,74 @@ -#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
- requests_cache.backends.mongodict
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
- Dictionary-like objects for saving large data sets to ``mongodb`` database
-"""
-from collections import MutableMapping
-try:
- import cPickle as pickle
-except ImportError:
- import pickle
-
-from pymongo import Connection
-
-
-class MongoDict(MutableMapping):
- """ MongoDict - a dictionary-like interface for ``mongo`` database
- """
- def __init__(self, db_name,
- collection_name='mongo_dict_data', connection=None):
- """
- :param db_name: database name (be careful with production databases)
- :param collection_name: collection name (default: mongo_dict_data)
- :param connection: ``pymongo.Connection`` instance. If it's ``None``
- (default) new connection with default options will
- be created
- """
- if connection is not None:
- self.connection = connection
- else:
- self.connection = Connection()
- self.db = self.connection[db_name]
- self.collection = self.db[collection_name]
-
- def __getitem__(self, key):
- result = self.collection.find_one({'_id': key})
- if result is None:
- raise KeyError
- return result['data']
-
- def __setitem__(self, key, item):
- self.collection.save({'_id': key, 'data': item})
-
- def __delitem__(self, key):
- spec = {'_id': key}
- if self.collection.find_one(spec, fields=['_id']):
- self.collection.remove(spec)
- else:
- raise KeyError
-
- def __len__(self):
- return self.collection.count()
-
- def __iter__(self):
- for d in self.collection.find(fields=['_id']):
- yield d['_id']
-
- def clear(self):
- self.collection.drop()
-
- def __str__(self):
- return str(dict(self.items()))
-
-
-class MongoPickleDict(MongoDict):
- """ Same as :class:`MongoDict`, but pickles values before saving
- """
- def __setitem__(self, key, item):
- super(MongoPickleDict, self).__setitem__(key, pickle.dumps(item))
-
- def __getitem__(self, key):
- return pickle.loads(bytes(super(MongoPickleDict, self).__getitem__(key)))
+#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" + requests_cache.backends.mongodict + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + Dictionary-like objects for saving large data sets to ``mongodb`` database +""" +from collections import MutableMapping +try: + import cPickle as pickle +except ImportError: + import pickle + +from pymongo import Connection + + +class MongoDict(MutableMapping): + """ MongoDict - a dictionary-like interface for ``mongo`` database + """ + def __init__(self, db_name, + collection_name='mongo_dict_data', connection=None): + """ + :param db_name: database name (be careful with production databases) + :param collection_name: collection name (default: mongo_dict_data) + :param connection: ``pymongo.Connection`` instance. If it's ``None`` + (default) new connection with default options will + be created + """ + if connection is not None: + self.connection = connection + else: + self.connection = Connection() + self.db = self.connection[db_name] + self.collection = self.db[collection_name] + + def __getitem__(self, key): + result = self.collection.find_one({'_id': key}) + if result is None: + raise KeyError + return result['data'] + + def __setitem__(self, key, item): + self.collection.save({'_id': key, 'data': item}) + + def __delitem__(self, key): + spec = {'_id': key} + if self.collection.find_one(spec, fields=['_id']): + self.collection.remove(spec) + else: + raise KeyError + + def __len__(self): + return self.collection.count() + + def __iter__(self): + for d in self.collection.find(fields=['_id']): + yield d['_id'] + + def clear(self): + self.collection.drop() + + def __str__(self): + return str(dict(self.items())) + + +class MongoPickleDict(MongoDict): + """ Same as :class:`MongoDict`, but pickles values before saving + """ + def __setitem__(self, key, item): + super(MongoPickleDict, self).__setitem__(key, pickle.dumps(item)) + + def __getitem__(self, key): + return pickle.loads(bytes(super(MongoPickleDict, self).__getitem__(key))) diff --git a/tests/test_dbdict.py b/tests/test_dbdict.py index 615ce55..5201311 100644 --- a/tests/test_dbdict.py +++ b/tests/test_dbdict.py @@ -6,7 +6,7 @@ sys.path.insert(0, os.path.abspath('..')) from threading import Thread import unittest -from requests_cache.backends.dbdict import DbDict, DbPickleDict +from requests_cache.backends.storage.dbdict import DbDict, DbPickleDict DB_NAME = 'test' diff --git a/tests/test_mongodict.py b/tests/test_mongodict.py index 60f381d..66907de 100644 --- a/tests/test_mongodict.py +++ b/tests/test_mongodict.py @@ -5,7 +5,7 @@ import os, sys sys.path.insert(0, os.path.abspath('..')) import unittest -from requests_cache.backends.mongodict import MongoDict, MongoPickleDict +from requests_cache.backends.storage.mongodict import MongoDict, MongoPickleDict DB_NAME = 'requests-cache-temporary-db-test-will-be-deleted' |
