Skip to content

Commit

Permalink
Various small improvements.
Browse files Browse the repository at this point in the history
* More type checks/assertions.
* Fix MemoryCache.inspect.
  • Loading branch information
Anteru committed Jan 3, 2024
1 parent a0ec826 commit 382d802
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 12 deletions.
9 changes: 8 additions & 1 deletion liara/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,14 @@ def __setup_cache(self) -> None:
dir = self.__configuration.get('build.cache.db.directory')
if dir and cache_directory is None:
cache_directory = pathlib.Path(dir)
assert cache_directory
self.__cache = Sqlite3Cache(cache_directory)
case 'fs':
self.__log.debug('Using FilesystemCache')
dir = self.__configuration.get('build.cache.fs.directory')
if dir and cache_directory is None:
cache_directory = pathlib.Path(dir)
assert cache_directory
self.__cache = FilesystemCache(cache_directory)
case 'redis':
self.__log.debug('Using RedisCache')
Expand Down Expand Up @@ -738,7 +740,7 @@ def _load_plugins(self, folder):
self.__registered_plugins[plugin] = module

def _load_module(self, path, name=''):
import importlib
import importlib.util
self.__log.debug(f'Initializing plugin from module: "{path}"')
# Prevent modules from being loaded twice
if module := self.__registered_plugins.get(path):
Expand All @@ -748,7 +750,12 @@ def _load_module(self, path, name=''):
if not name:
name = path.stem
spec = importlib.util.spec_from_file_location(name, path)
assert spec
assert spec.loader

module = importlib.util.module_from_spec(spec)
assert module

spec.loader.exec_module(module)
return module

Expand Down
5 changes: 5 additions & 0 deletions liara/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ def _extract_links(document: DocumentNode):
This assumes the document has been already processed into valid Html.
"""
from bs4 import BeautifulSoup
assert document.content
soup = BeautifulSoup(document.content, 'lxml')

for item in soup.find_all(['img', 'a']):
target = None

if item.name == 'img':
target = item.attrs.get('src', None)
elif item.name == 'a':
Expand Down Expand Up @@ -104,6 +107,8 @@ def _check_external_link(url: str):
if url.startswith('mailto'):
return (True, url, None,)

error = 'Unknown error'

try:
r = requests.get(url, timeout=5)
if r.status_code == 200:
Expand Down
4 changes: 2 additions & 2 deletions liara/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CacheInfo:
as getting the exact numbers may be costly."""

size: int = 0
"""Approximate number of objects stored in the cache."""
"""Approximate number of bytes stored in the cache."""

entry_count: int = 0
"""Approximate number of objects stored in the cache."""
Expand Down Expand Up @@ -234,7 +234,7 @@ def inspect(self):
size = sys.getsizeof(self.__index)

size += sum([sys.getsizeof(k) + sys.getsizeof(v)
for k, v in self.__index.value()])
for k, v in self.__index.items()])

count = len(self.__index)

Expand Down
10 changes: 6 additions & 4 deletions liara/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Environment:
This provides access to global variables that are useful for command line
commands, as well as a global liara instance."""
__liara : Liara | None

def __init__(self):
self.verbose = False
self.debug = False
Expand All @@ -21,7 +23,7 @@ def __init__(self):
self.log = logging.getLogger('liara.cmdline')

@property
def liara(self):
def liara(self) -> Liara:
if not self.__liara:
self.__liara = _create_liara(self.config)
return self.__liara
Expand All @@ -30,7 +32,7 @@ def liara(self):
pass_environment = click.make_pass_decorator(Environment, ensure=True)


def _setup_logging(debug, verbose):
def _setup_logging(debug: bool, verbose: bool):
if debug:
logging.basicConfig(
level=logging.DEBUG,
Expand Down Expand Up @@ -61,7 +63,7 @@ def _setup_logging(debug, verbose):
@click.option('--date', default=None, help='Override the current date.')
@click.version_option()
@pass_environment
def cli(env, debug, verbose, config, date):
def cli(env, debug: bool, verbose: bool, config, date: str):
_setup_logging(debug, verbose)

if date:
Expand Down Expand Up @@ -96,7 +98,7 @@ def _create_liara(config):
@click.option('--parallel/--no-parallel', default=True,
help='Enable or disable parallel processing.')
@pass_environment
def build(env, profile, profile_file, cache, parallel):
def build(env, profile, profile_file, cache: bool, parallel: bool):
"""Build a site."""
if profile:
pr = cProfile.Profile()
Expand Down
4 changes: 3 additions & 1 deletion liara/feeds.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .nodes import GeneratedNode, NodeKind
from .nodes import GeneratedNode, NodeKind, DocumentNode
from .site import Site
from . import __version__
from .util import local_now
Expand Down Expand Up @@ -50,6 +50,7 @@ def generate(self):
)

for item in items:
assert isinstance(item, DocumentNode)
e = E.item(
E.title(item.metadata['title']),
E.link(meta['base_url'] + str(item.path)),
Expand Down Expand Up @@ -89,6 +90,7 @@ def generate(self):

result_items = []
for item in items:
assert isinstance(item, DocumentNode)
result_items.append({
'id': meta['base_url'] + str(item.path),
'title': item.metadata['title'],
Expand Down
8 changes: 6 additions & 2 deletions liara/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,11 @@ class StaticNode(Node):
Static nodes are suitable for large static data which never changes, for
instance, binary files, videos, images etc.
"""
def __init__(self, src, path: pathlib.PurePosixPath, metadata_path=None):
src: pathlib.Path # Unlike a generic Node, a StaticNode always has a
# source

def __init__(self, src: pathlib.Path, path: pathlib.PurePosixPath,
metadata_path=None):
super().__init__()
self.kind = NodeKind.Static
self.src = src
Expand Down Expand Up @@ -861,7 +865,7 @@ def publish(self, publisher: Publisher) -> pathlib.Path:
class _AsyncThumbnailTask(_AsyncTask):
__log = logging.getLogger(f'{__name__}.{__qualname__}')

def __init__(self, cache_key, src: pathlib.Path,
def __init__(self, cache_key: bytes, src: pathlib.Path,
size: Dict[str, int],
format: Optional[str]):
self.__src = src
Expand Down
1 change: 1 addition & 0 deletions liara/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def publish_generated(self, generated: GeneratedNode):
if isinstance(generated.content, bytes):
file_path.write_bytes(generated.content)
else:
assert isinstance(generated.content, str)
file_path.write_text(generated.content, encoding='utf-8')
return file_path

Expand Down
1 change: 1 addition & 0 deletions liara/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def clear(self):
def load_bytecode(self, bucket):
key = bucket.key.encode('utf-8')
if content := self.__cache.get(key):
assert isinstance(content, bytes)
s = io.BytesIO(content)
bucket.load_bytecode(s)

Expand Down
17 changes: 15 additions & 2 deletions test/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,18 @@

def test_put_retrieve_memory_cache():
c = MemoryCache()
c.put(1, 23)
assert c.get(1) == 23
key = bytes(1)
c.put(key, 23)
assert c.get(key) == 23

def test_memory_cache_inspect():
import sys

c = MemoryCache()
key = bytes(1)
value = bytes([1, 2, 3, 4])

c.put(key, value)
ci = c.inspect()
assert ci.entry_count == 1
assert ci.size >= sys.getsizeof(value)

0 comments on commit 382d802

Please sign in to comment.