-
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.
- Loading branch information
1 parent
08f400c
commit 343cd14
Showing
18 changed files
with
244 additions
and
122 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
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 +1 @@ | ||
__version__ = '0.7' | ||
__version__ = '0.8' |
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 @@ | ||
from .values import EXAMPLE_DIR, PYTHONPATH_FOLDERS, SET_UTF8_ENCODING, WORKING_DIR |
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,44 @@ | ||
from typing import Any, Final, Generic, Optional, Tuple, Type, TypeVar, Union | ||
|
||
from sphinx.application import Sphinx as SphinxApp | ||
from sphinx.errors import ConfigError | ||
|
||
from sphinx_exec_code.__const__ import log | ||
|
||
TYPE_VALUE = TypeVar('TYPE_VALUE') | ||
|
||
|
||
class SphinxConfigValue(Generic[TYPE_VALUE]): | ||
SPHINX_TYPE: Union[Tuple[Type[Any], ...], Type[Any]] | ||
|
||
def __init__(self, sphinx_name: str, initial_value: Optional[TYPE_VALUE] = None): | ||
self.sphinx_name: Final = sphinx_name | ||
self._value: Optional[TYPE_VALUE] = initial_value | ||
|
||
@property | ||
def value(self) -> TYPE_VALUE: | ||
if self._value is None: | ||
raise ConfigError(f'{self.sphinx_name} is not set!') | ||
return self._value | ||
|
||
def transform_value(self, app: SphinxApp, value): | ||
return value | ||
|
||
def validate_value(self, value) -> TYPE_VALUE: | ||
return value | ||
|
||
def from_app(self, app: SphinxApp) -> TYPE_VALUE: | ||
# load value | ||
value = self.transform_value(app, getattr(app.config, self.sphinx_name)) | ||
|
||
# log transformed value | ||
assert self.sphinx_name.startswith('exec_code_') | ||
name = self.sphinx_name[10:].replace('_', ' ').capitalize() | ||
log.debug(f'[exec-code] {name:s}: {value}') | ||
|
||
# additional validation | ||
self._value = self.validate_value(value) | ||
return self._value | ||
|
||
def add_config_value(self, app: SphinxApp, sphinx_default): | ||
app.add_config_value(self.sphinx_name, sphinx_default, 'env', self.SPHINX_TYPE) |
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,10 @@ | ||
from sphinx.application import Sphinx as SphinxApp | ||
|
||
from sphinx_exec_code.configuration.base import SphinxConfigValue | ||
|
||
|
||
class SphinxConfigFlag(SphinxConfigValue[bool]): | ||
SPHINX_TYPE = bool | ||
|
||
def transform_value(self, app: SphinxApp, value) -> bool: | ||
return bool(value) |
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,68 @@ | ||
from pathlib import Path | ||
from typing import Tuple | ||
|
||
from sphinx.application import Sphinx as SphinxApp | ||
|
||
from sphinx_exec_code.__const__ import log | ||
from sphinx_exec_code.configuration.base import SphinxConfigValue, TYPE_VALUE | ||
|
||
|
||
class SphinxConfigPath(SphinxConfigValue[TYPE_VALUE]): | ||
SPHINX_TYPE = (str, Path) | ||
|
||
def make_path(self, app: SphinxApp, value) -> Path: | ||
try: | ||
path = Path(value) | ||
except Exception: | ||
raise ValueError(f'Could not create Path from "{value}" (type {type(value).__name__}) ' | ||
f'(configured by {self.sphinx_name:s})') from None | ||
|
||
if not path.is_absolute(): | ||
path = (Path(app.confdir) / path).resolve() | ||
return path | ||
|
||
def check_folder_exists(self, folder: Path) -> Path: | ||
if not folder.is_dir(): | ||
raise FileNotFoundError(f'Directory "{folder}" not found! (configured by {self.sphinx_name:s})') | ||
return folder | ||
|
||
|
||
class SphinxConfigFolder(SphinxConfigPath[Path]): | ||
def transform_value(self, app: SphinxApp, value) -> Path: | ||
return self.make_path(app, value) | ||
|
||
def validate_value(self, value: Path) -> Path: | ||
return self.check_folder_exists(value) | ||
|
||
|
||
class SphinxConfigMultipleFolderStr(SphinxConfigPath[Tuple[str, ...]]): | ||
SPHINX_TYPE = () | ||
|
||
def transform_value(self, app: SphinxApp, value) -> Tuple[Path, ...]: | ||
return tuple(self.make_path(app, p) for p in value) | ||
|
||
def validate_value(self, value: Tuple[Path, ...]) -> Tuple[str, ...]: | ||
# check that folders exist | ||
for f in value: | ||
self.check_folder_exists(f) | ||
|
||
# Search for a python package and print a warning if we find none | ||
# since this is the only reason to specify additional folders | ||
for f in value: | ||
package_found = False | ||
for _f in f.iterdir(): | ||
if not _f.is_dir(): | ||
continue | ||
|
||
# log warning if we don't find a python package | ||
for file in _f.glob('__init__.py'): | ||
if file.name == '__init__.py': | ||
package_found = True | ||
break | ||
if package_found: | ||
break | ||
|
||
if not package_found: | ||
log.warning(f'[exec-code] No Python packages found in {f}') | ||
|
||
return tuple(map(str, value)) |
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 @@ | ||
from .flag_config import SphinxConfigFlag | ||
from .path_config import SphinxConfigFolder, SphinxConfigMultipleFolderStr | ||
|
||
EXAMPLE_DIR = SphinxConfigFolder('exec_code_example_dir') | ||
|
||
# Options for code execution | ||
WORKING_DIR = SphinxConfigFolder('exec_code_working_dir') | ||
PYTHONPATH_FOLDERS = SphinxConfigMultipleFolderStr('exec_code_source_folders') | ||
SET_UTF8_ENCODING = SphinxConfigFlag('exec_code_set_utf8_encoding') |
Oops, something went wrong.