Options defaults, small fixes, update readme

This commit is contained in:
2023-03-14 00:55:14 +01:00
parent 02dea55518
commit d6fa3d25db
4 changed files with 25 additions and 27 deletions

View File

@@ -9,16 +9,14 @@ Use cases:
## Installation
*TODO*
### With pip from Github
<!-- ### pip
Requirements: git, python, pip
1. Install python
2. `py -m pip install dyn2py`
```
pip install "dyn2py @ git+https://github.com/infeeeee/dyn2py"
```
### github releases
-->
## Usage
@@ -35,6 +33,7 @@ positional arguments:
options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-l LOGLEVEL, --loglevel LOGLEVEL
set log level, possible options: CRITICAL, ERROR, WARNING, INFO, DEBUG
-n, --dry-run do not modify files, only show log

View File

@@ -23,10 +23,15 @@ __all__ = [
"PythonNode",
"DynamoFileException",
"PythonNodeNotFoundException",
"PythonNodeException",
"PythonFileException"
]
def __dir__():
return __all__
def run(options: Options | None = None) -> None:
"""Run an extraction as from the command line
@@ -49,6 +54,11 @@ def run(options: Options | None = None) -> None:
""")
)
parser.add_argument("-v", "--version",
action="version",
version=f'{METADATA["Name"]} {METADATA["Version"]}'
)
parser.add_argument("-l", "--loglevel",
metavar="LOGLEVEL",
choices=LOGLEVELS,

View File

@@ -110,21 +110,18 @@ class File():
"""
return bool(self.extension == ".py")
def write(self, options: Options | None = None) -> None:
def write(self, options: Options = Options()) -> None:
"""Prepare writing file to the disk:
create backup, process dry-run, call filetype specific write_file() methods
Should be called on subclasses!
Args:
options(Options | None, optional): Run options. Defaults to None.
options(Options, optional): Run options. Defaults to Options().
Raises:
TypeError: If called on a File object
"""
if not options:
options = Options()
# This should only work on subclasses:
if type(self).__name__ == "File":
raise TypeError("This method shouldn't be called on File objects!")
@@ -169,16 +166,13 @@ class DynamoFile(File):
open_files: set[DynamoFile] = set()
"""A set of open Dynamo files, before saving. Self added by read()"""
def extract_python(self, options: Options | None = None) -> None:
def extract_python(self, options: Options = Options()) -> None:
"""Extract and write python files
Args:
options(Options | None, optional): Run options. Defaults to None.
options(Options, optional): Run options. Defaults to Options().
"""
if not options:
options = Options()
logging.info(f"Extracting from file: {self.filepath}")
# Go through nodes in the file:
@@ -314,17 +308,15 @@ class DynamoFile(File):
with open(self.filepath, "w", encoding="utf-8", newline="") as output_file:
json.dump(self.full_dict, output_file, indent=2, use_decimal=True)
def get_related_python_files(self, options: Options | None = None) -> list["PythonFile"]:
def get_related_python_files(self, options: Options = Options()) -> list["PythonFile"]:
"""Get python files exported from this Dynamo file
Args:
options(Options | None, optional): Run options. Defaults to None.
options(Options, optional): Run options. Defaults to Options().
Returns:
list[PythonFile]: A list of PythonFile objects
"""
if not options:
options = Options()
# Find the folder of the python files
if options.python_folder:
@@ -483,16 +475,13 @@ class PythonFile(File):
logging.debug(f"Header data from python file: {self.header_data}")
# logging.debug(f"Code from python file: {self.code}")
def update_dynamo(self, options: Options | None = None) -> None:
def update_dynamo(self, options: Options = Options()) -> None:
"""Update a the source Dynamo graph from this python script
Args:
options (Options | None, optional): Run options. Defaults to None.
options (Options, optional): Run options. Defaults to Options().
"""
if not options:
options = Options()
dynamo_file = self.get_source_dynamo_file()
new_python_node = PythonNode(python_file=self)

View File

@@ -1,6 +1,6 @@
[project]
name = "dyn2py"
version = "0.2.0"
version = "0.2.1"
description = "Extract python code from Dynamo graphs"
readme = "README.md"
requires-python = ">=3.8"