-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Move CachedData and HTTPSource into separate files
TODO: * CI * split-off tests * cleanup everything and every reference to rss2irc
- Loading branch information
Showing
18 changed files
with
206 additions
and
153 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
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,4 @@ | ||
#!/usr/bin/env python3 | ||
"""FIXME.""" | ||
from .cached_data import CachedData # noqa: F401 | ||
from .http_source import HTTPSource # noqa: F401 |
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,45 @@ | ||
#!/usr/bin/env python3 | ||
"""Code related to Cache. | ||
I love how black and reorder-python-imports play nicely together and no | ||
workarounds are needed. | ||
""" | ||
import time | ||
from dataclasses import dataclass | ||
from dataclasses import field | ||
|
||
from .config_options import DATA_SOURCE_EXPIRATION | ||
from .http_source import HTTPSource | ||
|
||
|
||
@dataclass | ||
class CachedData: | ||
"""CachedData represents locally cached data and state.""" | ||
|
||
data_sources: dict = field(default_factory=dict) | ||
items: dict = field(default_factory=dict) | ||
|
||
def get_source_by_url(self, url: str) -> HTTPSource: | ||
"""Return source by URL. | ||
If source doesn't exist, it will be created. | ||
""" | ||
source = self.data_sources.get(url, None) | ||
if source: | ||
source.last_used_ts = int(time.time()) | ||
return source | ||
|
||
self.data_sources[url] = HTTPSource( | ||
last_used_ts=int(time.time()), url=url | ||
) | ||
return self.get_source_by_url(url) | ||
|
||
def scrub_data_sources( | ||
self, expiration: int = DATA_SOURCE_EXPIRATION | ||
) -> None: | ||
"""Delete expired data sources.""" | ||
now = int(time.time()) | ||
for key in list(self.data_sources.keys()): | ||
diff = now - self.data_sources[key].last_used_ts | ||
if int(diff) > expiration: | ||
self.data_sources.pop(key) |
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,9 @@ | ||
#!/usr/bin/env python3 | ||
"""Common configuration options. | ||
I love how black and reorder-python-imports play nicely together and no | ||
workarounds are needed. | ||
""" | ||
CACHE_EXPIRATION = 86400 # seconds | ||
DATA_SOURCE_EXPIRATION = 30 * 86400 # seconds | ||
HTTP_TIMEOUT = 30 # seconds |
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,41 @@ | ||
#!/usr/bin/env python3 | ||
"""Code related to HTTP Source. | ||
I love how black and reorder-python-imports play nicely together and no | ||
workarounds are needed. | ||
""" | ||
from dataclasses import dataclass | ||
from dataclasses import field | ||
from typing import Dict | ||
|
||
|
||
@dataclass | ||
class HTTPSource: | ||
"""Class represents HTTP data source.""" | ||
|
||
http_etag: str = field(default_factory=str) | ||
http_last_modified: str = field(default_factory=str) | ||
last_used_ts: int = 0 | ||
url: str = field(default_factory=str) | ||
|
||
def extract_caching_headers(self, headers: Dict[str, str]) -> None: | ||
"""Extract cache related headers from given dict.""" | ||
self.http_etag = "" | ||
self.http_last_modified = "" | ||
for key, value in headers.items(): | ||
key = key.lower() | ||
if key == "etag": | ||
self.http_etag = value | ||
elif key == "last-modified": | ||
self.http_last_modified = value | ||
|
||
def make_caching_headers(self) -> Dict[str, str]: | ||
"""Return cache related headers as a dict.""" | ||
headers = {} | ||
if self.http_etag: | ||
headers["if-none-match"] = self.http_etag | ||
|
||
if self.http_last_modified: | ||
headers["if-modified-since"] = self.http_last_modified | ||
|
||
return headers |
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
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
Oops, something went wrong.