This commit is contained in:
Waylon Walker 2023-03-13 09:42:39 -05:00
commit 72672d6709
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
32 changed files with 1651 additions and 0 deletions

4
pyflybygen/__about__.py Normal file
View file

@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2023-present Waylon S. Walker <waylon@waylonwalker.com>
#
# SPDX-License-Identifier: MIT
__version__ = "0.0.0.dev1"

3
pyflybygen/__init__.py Normal file
View file

@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2023-present Waylon S. Walker <waylon@waylonwalker.com>
#
# SPDX-License-Identifier: MIT

View file

59
pyflybygen/cli/app.py Normal file
View file

@ -0,0 +1,59 @@
from pathlib import Path
import more_itertools
import typer
from pyflybygen.cli.common import verbose_callback
from pyflybygen.cli.config import config_app
from pyflybygen.cli.tui import tui_app
from pyflybygen.pyflybygen import get_imports
app = typer.Typer(
name="pyflybygen",
help="A rich terminal report for coveragepy.",
)
app.add_typer(config_app)
app.add_typer(tui_app)
def version_callback(value: bool) -> None:
"""Callback function to print the version of the pyflybygen package.
Args:
value (bool): Boolean value to determine if the version should be printed.
Raises:
typer.Exit: If the value is True, the version will be printed and the program will exit.
Example:
version_callback(True)
"""
if value:
from pyflybygen.__about__ import __version__
typer.echo(f"{__version__}")
raise typer.Exit()
@app.callback(invoke_without_command=True)
def main(
version: bool = typer.Option(
None,
"--version",
callback=version_callback,
is_eager=True,
),
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
) -> None:
imports = more_itertools.flatten(
[get_imports(p.read_text()) for p in Path(".").glob("**/*.py")]
)
print("\n".join(set(imports)))
if __name__ == "__main__":
typer.run(main)

6
pyflybygen/cli/common.py Normal file
View file

@ -0,0 +1,6 @@
from pyflybygen.console import console
def verbose_callback(value: bool) -> None:
if value:
console.quiet = False

29
pyflybygen/cli/config.py Normal file
View file

@ -0,0 +1,29 @@
from rich.console import Console
import typer
from pyflybygen.cli.common import verbose_callback
from pyflybygen.config import config as configuration
config_app = typer.Typer()
@config_app.callback()
def config(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
"configuration cli"
@config_app.command()
def show(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
Console().print(configuration)

18
pyflybygen/cli/tui.py Normal file
View file

@ -0,0 +1,18 @@
import typer
from pyflybygen.cli.common import verbose_callback
from pyflybygen.tui.app import run_app
tui_app = typer.Typer()
@tui_app.callback(invoke_without_command=True)
def i(
verbose: bool = typer.Option(
False,
callback=verbose_callback,
help="show the log messages",
),
):
"interactive tui"
run_app()

3
pyflybygen/config.py Normal file
View file

@ -0,0 +1,3 @@
from pyflybygen.standard_config import load
config = load("pyflybygen")

4
pyflybygen/console.py Normal file
View file

@ -0,0 +1,4 @@
from rich.console import Console
console = Console()
console.quiet = True

53
pyflybygen/pyflybygen.py Normal file
View file

@ -0,0 +1,53 @@
from tree_sitter_languages import get_language, get_parser
class CapturePoint:
def __init__(self, node):
self.start_point = node[0].start_point[0]
self.end_point = node[0].end_point[0]
def __eq__(self, o):
return self.start_point == o.start_point and self.end_point == o.end_point
def __repr__(self):
return f"{self.start_point}:{self.end_point}"
def __hash__(self):
return hash(repr(self))
def get_imports(content):
language = get_language("python")
content = content
content_lines = content.split("\n")
parser = get_parser("python")
tree = parser.parse(content.encode())
wild_imports = set(
CapturePoint(n)
for n in language.query(
"(import_from_statement (wildcard_import) @wildcard) @import_wild"
).captures(tree.root_node)
)
import_froms = list(
set(
CapturePoint(n)
for n in language.query("(import_from_statement) @import_from").captures(
tree.root_node
)
)
- wild_imports
)
imports = list(
set(
CapturePoint(n)
for n in language.query("(import_statement) @import").captures(
tree.root_node
)
)
)
import_lines = [
(" ".join(content_lines[s.start_point : s.end_point + 1])).strip()
for s in imports + import_froms
]
return import_lines

View file

@ -0,0 +1,239 @@
"""Standard Config.
A module to load tooling config from a users project space.
Inspired from frustrations that some tools have a tool.ini, .tool.ini,
setup.cfg, or pyproject.toml. Some allow for global configs, some don't. Some
properly follow the users home directory, others end up in a weird temp
directory. Windows home directory is only more confusing. Some will even
respect the users `$XDG_HOME` directory.
This file is for any project that can be configured in plain text such as `ini`
or `toml` and not requiring a .py file. Just name your tool and let users put
config where it makes sense to them, no need to figure out resolution order.
## Usage:
``` python
from standard_config import load
# Retrieve any overrides from the user
overrides = {'setting': True}
config = load('my_tool', overrides)
```
## Resolution Order
* First global file with a tool key
* First local file with a tool key
* Environment variables prefixed with `TOOL`
* Overrides
### Tool Specific Ini files
Ini file formats must include a `<tool>` key.
``` ini
[my_tool]
setting = True
```
### pyproject.toml
Toml files must include a `tool.<tool>` key
``` toml
[tool.my_tool]
setting = True
```
### setup.cfg
setup.cfg files must include a `tool:<tool>` key
``` ini
[tool:my_tool]
setting = True
```
### global files to consider
* <home>/tool.ini
* <home>/.tool
* <home>/.tool.ini
* <home>/.config/tool.ini
* <home>/.config/.tool
* <home>/.config/.tool.ini
### local files to consider
* <project_home>/tool.ini
* <project_home>/.tool
* <project_home>/.tool.ini
* <project_home>/pyproject.toml
* <project_home>/setup.cfg
"""
import os
from pathlib import Path
from typing import Dict, List, Union
import anyconfig
# path_spec_type = List[Dict[str, Union[Path, str, List[str\}\}\}\}
path_spec_type = List
def _get_global_path_specs(tool: str) -> path_spec_type:
"""
Generate a list of standard pathspecs for global config files.
Args:
tool (str): name of the tool to configure
"""
try:
home = Path(os.environ["XDG_HOME"])
except KeyError:
home = Path.home()
return [
{"path_specs": home / f"{tool}.ini", "ac_parser": "ini", "keys": [tool]},
{"path_specs": home / f".{tool}", "ac_parser": "ini", "keys": [tool]},
{"path_specs": home / f".{tool}.ini", "ac_parser": "ini", "keys": [tool]},
{
"path_specs": home / ".config" / f"{tool}.ini",
"ac_parser": "ini",
"keys": [tool],
},
{
"path_specs": home / ".config" / f".{tool}",
"ac_parser": "ini",
"keys": [tool],
},
{
"path_specs": home / ".config" / f".{tool}.ini",
"ac_parser": "ini",
"keys": [tool],
},
]
def _get_local_path_specs(tool: str, project_home: Union[str, Path]) -> path_spec_type:
"""
Generate a list of standard pathspecs for local, project directory config files.
Args:
tool (str): name of the tool to configure
"""
return [
{
"path_specs": Path(project_home) / f"{tool}.ini",
"ac_parser": "ini",
"keys": [tool],
},
{
"path_specs": Path(project_home) / f".{tool}",
"ac_parser": "ini",
"keys": [tool],
},
{
"path_specs": Path(project_home) / f".{tool}.ini",
"ac_parser": "ini",
"keys": [tool],
},
{
"path_specs": Path(project_home) / f"{tool}.yml",
"ac_parser": "yaml",
"keys": [tool],
},
{
"path_specs": Path(project_home) / f".{tool}.yml",
"ac_parser": "yaml",
"keys": [tool],
},
{
"path_specs": Path(project_home) / f"{tool}.toml",
"ac_parser": "toml",
"keys": [tool],
},
{
"path_specs": Path(project_home) / f".{tool}.toml",
"ac_parser": "toml",
"keys": [tool],
},
{
"path_specs": Path(project_home) / "pyproject.toml",
"ac_parser": "toml",
"keys": ["tool", tool],
},
{
"path_specs": Path(project_home) / "setup.cfg",
"ac_parser": "ini",
"keys": [f"tool.{tool}"],
},
]
def _get_attrs(attrs: list, config: Dict) -> Dict:
"""Get nested config data from a list of keys.
specifically written for pyproject.toml which needs to get `tool` then `<tool>`
"""
for attr in attrs:
config = config[attr]
return config
def _load_files(config_path_specs: path_spec_type) -> Dict:
"""Use anyconfig to load config files stopping at the first one that exists.
config_path_specs (list): a list of pathspecs and keys to load
"""
for file in config_path_specs:
if file["path_specs"].exists():
config = anyconfig.load(**file)
else:
# ignore missing files
continue
try:
return _get_attrs(file["keys"], config)
except KeyError:
# ignore incorrect keys
continue
return {}
def _load_env(tool: str) -> Dict:
"""Load config from environment variables.
Args:
tool (str): name of the tool to configure
"""
vars = [var for var in os.environ.keys() if var.startswith(tool.upper())]
return {
var.lower().strip(tool.lower()).strip("_").strip("-"): os.environ[var]
for var in vars
}
def load(tool: str, project_home: Union[Path, str] = ".", overrides: Dict = {}) -> Dict:
"""Load tool config from standard config files.
Resolution Order
* First global file with a tool key
* First local file with a tool key
* Environment variables prefixed with `TOOL`
* Overrides
Args:
tool (str): name of the tool to configure
"""
global_config = _load_files(_get_global_path_specs(tool))
local_config = _load_files(_get_local_path_specs(tool, project_home))
env_config = _load_env(tool)
return {**global_config, **local_config, **env_config, **overrides}

18
pyflybygen/tui/app.css Normal file
View file

@ -0,0 +1,18 @@
Screen {
align: center middle;
layers: main footer;
}
Sidebar {
height: 100vh;
width: auto;
min-width: 20;
background: $secondary-background-darken-2;
dock: left;
margin-right: 1;
layer: main;
}
Footer {
layer: footer;
}

62
pyflybygen/tui/app.py Normal file
View file

@ -0,0 +1,62 @@
from pathlib import Path
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.css.query import NoMatches
from textual.widgets import Footer, Static
from pyflybygen.config import config
config["tui"] = {}
config["tui"]["bindings"] = {}
class Sidebar(Static):
def compose(self) -> ComposeResult:
yield Container(
Static("sidebar"),
id="sidebar",
)
class Tui(App):
"""A Textual app to manage requests."""
CSS_PATH = Path("__file__").parent / "app.css"
BINDINGS = [tuple(b.values()) for b in config["tui"]["bindings"]]
def compose(self) -> ComposeResult:
"""Create child widgets for the app."""
yield Container(Static("hello world"))
yield Footer()
def action_toggle_dark(self) -> None:
"""An action to toggle dark mode."""
self.dark = not self.dark
def action_toggle_sidebar(self):
try:
self.query_one("PromptSidebar").remove()
except NoMatches:
self.mount(Sidebar())
def run_app():
import os
import sys
from textual.features import parse_features
dev = "--dev" in sys.argv
features = set(parse_features(os.environ.get("TEXTUAL", "")))
if dev:
features.add("debug")
features.add("devtools")
os.environ["TEXTUAL"] = ",".join(sorted(features))
app = Tui()
app.run()
if __name__ == "__main__":
run_app()