-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Add needimport
caching and needs_import_cache_size
configuration
#1297
Open
chrisjsewell
wants to merge
2
commits into
master
Choose a base branch
from
neeimport-cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -3,7 +3,9 @@ | |
import json | ||
import os | ||
import re | ||
from typing import Sequence | ||
import threading | ||
from copy import deepcopy | ||
from typing import Any, OrderedDict, Sequence | ||
from urllib.parse import urlparse | ||
|
||
import requests | ||
|
@@ -52,7 +54,8 @@ | |
|
||
@measure_time("needimport") | ||
def run(self) -> Sequence[nodes.Node]: | ||
# needs_list = {} | ||
needs_config = NeedsSphinxConfig(self.config) | ||
|
||
version = self.options.get("version") | ||
filter_string = self.options.get("filter") | ||
id_prefix = self.options.get("id_prefix", "") | ||
|
@@ -111,21 +114,34 @@ | |
raise ReferenceError( | ||
f"Could not load needs import file {correct_need_import_path}" | ||
) | ||
mtime = os.path.getmtime(correct_need_import_path) | ||
|
||
try: | ||
with open(correct_need_import_path) as needs_file: | ||
needs_import_list = json.load(needs_file) | ||
except (OSError, json.JSONDecodeError) as e: | ||
# TODO: Add exception handling | ||
raise SphinxNeedsFileException(correct_need_import_path) from e | ||
|
||
errors = check_needs_data(needs_import_list) | ||
if errors.schema: | ||
logger.info( | ||
f"Schema validation errors detected in file {correct_need_import_path}:" | ||
) | ||
for error in errors.schema: | ||
logger.info(f' {error.message} -> {".".join(error.path)}') | ||
if ( | ||
needs_import_list := _FileCache.get(correct_need_import_path, mtime) | ||
) is None: | ||
try: | ||
with open(correct_need_import_path) as needs_file: | ||
needs_import_list = json.load(needs_file) | ||
except (OSError, json.JSONDecodeError) as e: | ||
# TODO: Add exception handling | ||
raise SphinxNeedsFileException(correct_need_import_path) from e | ||
|
||
errors = check_needs_data(needs_import_list) | ||
if errors.schema: | ||
logger.info( | ||
f"Schema validation errors detected in file {correct_need_import_path}:" | ||
) | ||
for error in errors.schema: | ||
logger.info(f' {error.message} -> {".".join(error.path)}') | ||
else: | ||
_FileCache.set( | ||
correct_need_import_path, | ||
mtime, | ||
needs_import_list, | ||
needs_config.import_cache_size, | ||
) | ||
|
||
self.env.note_dependency(correct_need_import_path) | ||
|
||
if version is None: | ||
try: | ||
|
@@ -141,17 +157,17 @@ | |
f"Version {version} not found in needs import file {correct_need_import_path}" | ||
) | ||
|
||
needs_config = NeedsSphinxConfig(self.config) | ||
data = needs_import_list["versions"][version] | ||
|
||
# TODO this is not exactly NeedsInfoType, because the export removes/adds some keys | ||
needs_list: dict[str, NeedsInfoType] = data["needs"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't know how to feel about calling a dictionary a list :) |
||
|
||
if ids := self.options.get("ids"): | ||
id_list = [i.strip() for i in ids.split(",") if i.strip()] | ||
data["needs"] = { | ||
needs_list = { | ||
key: data["needs"][key] for key in id_list if key in data["needs"] | ||
} | ||
|
||
# TODO this is not exactly NeedsInfoType, because the export removes/adds some keys | ||
needs_list: dict[str, NeedsInfoType] = data["needs"] | ||
if schema := data.get("needs_schema"): | ||
# Set defaults from schema | ||
defaults = { | ||
|
@@ -160,7 +176,8 @@ | |
if "default" in value | ||
} | ||
needs_list = { | ||
key: {**defaults, **value} for key, value in needs_list.items() | ||
key: {**defaults, **value} # type: ignore[typeddict-item] | ||
for key, value in needs_list.items() | ||
} | ||
|
||
# Filter imported needs | ||
|
@@ -169,7 +186,8 @@ | |
if filter_string is None: | ||
needs_list_filtered[key] = need | ||
else: | ||
filter_context = need.copy() | ||
# we deepcopy here, to ensure that the original data is not modified | ||
filter_context = deepcopy(need) | ||
|
||
# Support both ways of addressing the description, as "description" is used in json file, but | ||
# "content" is the sphinx internal name for this kind of information | ||
|
@@ -185,7 +203,9 @@ | |
location=(self.env.docname, self.lineno), | ||
) | ||
|
||
needs_list = needs_list_filtered | ||
# note we need to deepcopy here, as we are going to modify the data, | ||
# but we want to ensure data referenced from the cache is not modified | ||
needs_list = deepcopy(needs_list_filtered) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe there's a code logic options that combines the 2 deepcopy |
||
|
||
# tags update | ||
if tags := [ | ||
|
@@ -265,6 +285,41 @@ | |
return self.env.docname | ||
|
||
|
||
class _ImportCache: | ||
"""A simple cache for imported needs, | ||
mapping a (path, mtime) to a dictionary of needs. | ||
It's thread safe, | ||
and has a maximum size when adding new items. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self._cache: OrderedDict[tuple[str, float], dict[str, Any]] = OrderedDict() | ||
self._need_count = 0 | ||
self._lock = threading.Lock() | ||
|
||
def set( | ||
self, path: str, mtime: float, value: dict[str, Any], max_size: int | ||
) -> None: | ||
with self._lock: | ||
self._cache[(path, mtime)] = value | ||
self._need_count += len(value) | ||
max_size = max(max_size, 0) | ||
while self._need_count > max_size: | ||
_, value = self._cache.popitem(last=False) | ||
self._need_count -= len(value) | ||
|
||
def get(self, path: str, mtime: float) -> dict[str, Any] | None: | ||
with self._lock: | ||
return self._cache.get((path, mtime), None) | ||
|
||
def __repr__(self) -> str: | ||
with self._lock: | ||
return f"{self.__class__.__name__}({list(self._cache)})" | ||
|
||
|
||
_FileCache = _ImportCache() | ||
|
||
|
||
class VersionNotFound(BaseException): | ||
pass | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ImportCache
storesself._cache: OrderedDict[tuple[str, float], dict[str, Any]] = OrderedDict()
where
tuple[str, float]
is path and mtime.Items are popped until the dict size is less than
needs_import_cache_size
.So not the number of needs is relevant, but the number of distinct needs.json paths (+mtime combination).
If I'm right, please rephrase to make that clearer.