-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch marvelous to python3, revamp caching approach to be more flexi…
…ble.
- Loading branch information
1 parent
6729cc5
commit 2ae86ca
Showing
10 changed files
with
108 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
*.sqlite | ||
*.db | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,12 @@ | ||
from session import Session | ||
from exceptions import AuthenticationError, LibraryError | ||
from . import session, exceptions | ||
from .sqlite_cache import SqliteCache | ||
|
||
|
||
def api(public_key=None, private_key=None, cache=False): | ||
def api(public_key=None, private_key=None, cache=None): | ||
if public_key is None: | ||
raise AuthenticationError("Missing public_key.") | ||
raise exceptions.AuthenticationError("Missing public_key.") | ||
|
||
if private_key is None: | ||
raise AuthenticationError("Missing private_key.") | ||
raise exceptions.AuthenticationError("Missing private_key.") | ||
|
||
if cache: | ||
try: | ||
from requests_cache import CachedSession | ||
|
||
cache_defaults = { | ||
'backend': 'sqlite', | ||
'expire_after': 60*60*24, # 24 hours | ||
'ignored_parameters': ['hash', 'ts', 'apikey'] | ||
} | ||
|
||
# Override the name and kwargs of the cache by passing a dict into | ||
# the cache kwarg | ||
if isinstance(cache, dict): | ||
cache_defaults.update(cache) | ||
|
||
cache = CachedSession( | ||
cache_defaults.get('name', 'marvelous'), **cache_defaults) | ||
except: | ||
raise LibraryError( | ||
"Marvelous only supports cache with requests-cache >= 0.4.11, " | ||
"not yet on pypi. To install the newest requests-cache, run " | ||
"`pip install requests-cache`.") | ||
|
||
return Session(public_key, private_key, cached_requests=cache) | ||
return session.Session(public_key, private_key, cache=cache) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import itertools | ||
|
||
import comic | ||
import exceptions | ||
from . import comic, exceptions | ||
|
||
|
||
class ComicsList(): | ||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import json | ||
import sqlite3 | ||
|
||
|
||
class SqliteCache: | ||
def __init__(self, db_name="marvelous_cache.db"): | ||
self.con = sqlite3.connect(db_name) | ||
self.cur = self.con.cursor() | ||
self.cur.execute("CREATE TABLE IF NOT EXISTS responses (key, json)") | ||
|
||
def get(self, key): | ||
self.cur.execute("SELECT json FROM responses WHERE key = ?", (key,)) | ||
result = self.cur.fetchone() | ||
|
||
if result: | ||
return json.loads(result[0]) | ||
|
||
return None | ||
|
||
def store(self, key, value): | ||
self.cur.execute( | ||
"INSERT INTO responses(key, json) VALUES(?, ?)", | ||
(key, json.dumps(value))) | ||
self.con.commit() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
marshmallow==2.6.0 | ||
nose==1.3.7 | ||
requests==2.9.1 | ||
requests-cache==0.4.12 | ||
sphinx-rtd-theme==0.1.9 |
This file contains 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