-
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.
Version 3.2.0 - Stubber Update ### Added - Stub generators from descriptor config files where you structure out your config file and give each value the Python type you expect the config to yield in order to generate type-hinting stub-classes that match the structure of your config file(s). - CLI commands for running the stub generation: `alviss-stubber` - CLI commands for running the config rendering: `alviss-render` - A bunch of Alviss specific Error Exceptions that are raised e.g. when files aren't found or when Fidelius is required but isn't installed ### Changed - The rendering of static single file configs from Alviss config files and expressions has now been moved into its own sub-module with a standard API
- Loading branch information
1 parent
f8da238
commit 1f40c0a
Showing
34 changed files
with
1,204 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = '3.1.0' | ||
__version__ = '3.2.0' | ||
|
||
__author__ = 'Thordur Matthiasson <[email protected]>' | ||
__license__ = 'MIT License' | ||
|
Empty file.
Empty file.
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,66 @@ | ||
import argparse | ||
|
||
from alviss import __version__ as version | ||
from alviss.renderers import SimpleStaticRenderer | ||
from alviss.structs.errors import * | ||
|
||
import sys | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Renders a single static config from an Alviss formatted file and ' | ||
'its extends, includes and other expressions.', | ||
epilog=f'Alviss version {version}') | ||
|
||
parser.add_argument('file', help='The Alviss formatted config file to read, parse and render') | ||
parser.add_argument('-o', '--output', help='File to write the results to (otherwise its just printed to stdout)', | ||
default='', nargs='?') | ||
parser.add_argument('-f', '--force-overwrite', help='Overwrite existing output file if it exists', | ||
action='store_true') | ||
|
||
loudness_group = parser.add_mutually_exclusive_group() | ||
loudness_group.add_argument('-s', '--silent', action='store_true', | ||
help='Only outputs the resulting rendered config (and errors print to stderr)') | ||
loudness_group.add_argument('-v', '--verbose', action="store_true", | ||
help='Spits out DEBUG level logs') | ||
|
||
args = parser.parse_args() | ||
|
||
if args.verbose: | ||
import logging | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
if not args.silent: | ||
print(f'Reading and parsing file: {args.file}...') | ||
|
||
try: | ||
if args.output: | ||
if not args.silent: | ||
print(f'Writing output to: {args.output}...') | ||
|
||
SimpleStaticRenderer().render_static_config_to_file(input_file=args.file, | ||
output_file=args.output, | ||
overwrite_existing=args.force_overwrite) | ||
|
||
else: | ||
if not args.silent: | ||
print(f'Printing results:') | ||
print(f'==================================================') | ||
print(SimpleStaticRenderer().render_static_config_from_file(args.file)) | ||
|
||
if not args.silent: | ||
print(f'==================================================') | ||
|
||
if not args.silent: | ||
print(f'Done!') | ||
|
||
except AlvissError as e: | ||
if not args.silent: | ||
print(f'An error occurred: {e!r}') | ||
else: | ||
print(f'An error occurred: {e!r}', file=sys.stderr) | ||
sys.exit(3) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Empty file.
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,62 @@ | ||
import argparse | ||
|
||
from alviss import __version__ as version | ||
from alviss import stubber | ||
from alviss.structs.errors import * | ||
import sys | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Generates Python dataclass stubs based on the given alviss type descriptor file.', | ||
epilog=f'Alviss version {version}') | ||
|
||
parser.add_argument('file', help='The Alviss config type descriptor file to generate strubs from.') | ||
parser.add_argument('-o', '--output', help='File to write the generated stub code to (otherwise its just printed to stdout)', | ||
default='', nargs='?') | ||
parser.add_argument('-f', '--force-overwrite', help='Overwrite existing output file if it exists', action='store_true') | ||
|
||
loudness_group = parser.add_mutually_exclusive_group() | ||
loudness_group.add_argument('-s', '--silent', action='store_true', | ||
help='Only outputs the resulting rendered code (and errors print to stderr)') | ||
loudness_group.add_argument('-v', '--verbose', action="store_true", | ||
help='Spits out DEBUG level logs') | ||
|
||
args = parser.parse_args() | ||
|
||
if args.verbose: | ||
import logging | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
if not args.silent: | ||
print(f'Reading and stubbing file: {args.file}...') | ||
|
||
try: | ||
if args.output: | ||
if not args.silent: | ||
print(f'Writing output to: {args.output}...') | ||
|
||
stubber.SimpleStubMaker().render_stub_classes_to_file(input_file=args.file, | ||
output_file=args.output, | ||
overwrite_existing=args.force_overwrite) | ||
else: | ||
if not args.silent: | ||
print(f'Printing results:') | ||
print(f'==================================================') | ||
print(stubber.SimpleStubMaker().render_stub_classes_from_descriptor_file(args.file)) | ||
|
||
if not args.silent: | ||
print(f'==================================================') | ||
|
||
if not args.silent: | ||
print(f'Done!') | ||
|
||
except AlvissError as e: | ||
if not args.silent: | ||
print(f'An error occurred: {e!r}') | ||
else: | ||
print(f'An error occurred: {e!r}', file=sys.stderr) | ||
sys.exit(3) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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 .static import * |
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,2 @@ | ||
from .interface import * | ||
from ._simple import * |
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,34 @@ | ||
__all__ = [ | ||
'SimpleStaticRenderer', | ||
] | ||
|
||
from .interface import * | ||
from alviss import quickloader | ||
from alviss.structs.errors import * | ||
|
||
import os | ||
import pathlib | ||
|
||
import logging | ||
log = logging.getLogger(__file__) | ||
|
||
|
||
class SimpleStaticRenderer(IStaticRenderer): | ||
def render_static_config_from_file(self, file: str) -> str: | ||
return quickloader.render_load(file) | ||
|
||
def render_static_config_to_file(self, input_file: str, output_file: str, overwrite_existing: bool = False): | ||
out = pathlib.Path(output_file).absolute() | ||
if out.exists() and not overwrite_existing: | ||
raise AlvissFileAlreadyExistsError('Output file already exists', file_name=output_file) | ||
|
||
results = self.render_static_config_from_file(input_file) | ||
|
||
if not out.parent.exists(): | ||
log.debug(f'Creating output path: {out.parent}') | ||
os.makedirs(out.parent, exist_ok=True) | ||
|
||
with open(output_file, 'w') as fin: | ||
fin.write(results) | ||
|
||
return |
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,21 @@ | ||
__all__ = [ | ||
'IStaticRenderer', | ||
] | ||
import abc | ||
|
||
|
||
class IStaticRenderer(abc.ABC): | ||
@abc.abstractmethod | ||
def render_static_config_from_file(self, file: str) -> str: | ||
"""Renders a single static configuration file from an Alviss formatted | ||
file, including all included and/or extended files and resolving all | ||
expressions, variables and internal references and such. | ||
""" | ||
pass | ||
|
||
@abc.abstractmethod | ||
def render_static_config_to_file(self, input_file: str, output_file: str, overwrite_existing: bool = False): | ||
"""Writer the results of the `render_static_config_from_file` call to | ||
the given output file. | ||
""" | ||
pass |
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,2 @@ | ||
from ccptools.structs import * | ||
from .errors import * |
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,12 @@ | ||
__all__ = [ | ||
'_BaseCfgStub', | ||
] | ||
from ._base import * | ||
|
||
|
||
class _BaseCfgStub(Protocol): | ||
"""This is the base Protocol for all generated Stubs | ||
""" | ||
def __getattr__(self, item) -> Union[Any, Empty]: | ||
... |
Oops, something went wrong.