Skip to content

Commit

Permalink
style(ruff): fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pythoninja committed Oct 12, 2023
1 parent a36d07f commit 6f4c7e2
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ select = [
"UP", # pyupgrade
"YTT" # flake8-2020
]
ignore = []
ignore = ["ANN101", "ANN204"]
line-length = 120
target-version = "py312"
preview = true
Expand Down
2 changes: 1 addition & 1 deletion sshgen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sshgen.cli import app


def main():
def main() -> None:
app()


Expand Down
16 changes: 8 additions & 8 deletions sshgen/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env python
from typing import Optional
from typing import Annotated

import typer
from typing_extensions import Annotated

from sshgen import __app_name__, __version__
from sshgen.utils.app import AppUtils
Expand All @@ -11,9 +10,9 @@
app = typer.Typer(no_args_is_help=True)


@app.command("generate")
@app.command('generate')
def generate_hosts_file(hosts_file: Annotated[str, typer.Option('--hosts-file', '-h')] = './hosts.yml',
output: Annotated[str, typer.Option('--output', '-o')] = "./config"):
output: Annotated[str, typer.Option('--output', '-o')] = './config') -> None:
"""
Command to generate SSH configuration file.
By default, it uses hosts.yml file placed in your working directory and outputs to file named as "config"
Expand All @@ -27,17 +26,18 @@ def generate_hosts_file(hosts_file: Annotated[str, typer.Option('--hosts-file',
AppUtils(hosts_file, output_file).generate_ssh_config()


def _version_callback(value: bool):
def _version_callback(value: bool) -> None:
if value:
typer.echo(f'{__app_name__} v{__version__}')
raise typer.Exit()


# noinspection PyUnusedLocal
@app.callback()
def callback(
version: Annotated[Optional[bool],
typer.Option('-v', '--version', callback=_version_callback, is_eager=True)] = None
):
version: Annotated[bool | None, # noqa: ARG001
typer.Option('-v', '--version', callback=_version_callback, is_eager=True)] = None,
) -> None:
"""
sshgen generates SSH configuration file based on an Ansible hosts file.
"""
Expand Down
5 changes: 3 additions & 2 deletions sshgen/generators/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@


class MapToHost:
def convert(self, parsed_hosts: CommentedMap) -> list[HostModel]:
@staticmethod
def convert(parsed_hosts: CommentedMap) -> list[HostModel]:
host_models = []

for host_group, hosts in parsed_hosts.items():
Expand All @@ -15,7 +16,7 @@ def convert(self, parsed_hosts: CommentedMap) -> list[HostModel]:
host=host,
host_group=host_group,
ansible_host=host_details['ansible_host'],
ansible_user=host_details['ansible_user']
ansible_user=host_details['ansible_user'],
)

meta = host_details.get('_meta', {})
Expand Down
5 changes: 3 additions & 2 deletions sshgen/generators/sshconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ def _open_template(self) -> str:
def _save_config(self, templates: list[str]) -> None:
self._create_output_file()
self.output_file.write_text('\n'.join(templates))
log.info("Generated SSH config file was saved to %s", self.output_file)
log.info('Generated SSH config file was saved to %s', self.output_file)

def _create_output_file(self) -> None:
self.output_file.touch(exist_ok=True)

def _get_fallback_auth(self) -> list[str]:
@staticmethod
def _get_fallback_auth() -> list[str]:
parameter = 'IdentitiesOnly yes'
width = len(parameter) + 4
return ['IdentityFile ~/.ssh/ssh_key', f'{parameter: >{width}}']
2 changes: 1 addition & 1 deletion sshgen/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def init_logger(module_name: str) -> Logger:
logger: Logger = logging.getLogger(module_name)
logger.setLevel(logging.INFO)

console_formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] - %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
console_formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
console_handler = logging.StreamHandler()
console_handler.setFormatter(console_formatter)

Expand Down
1 change: 0 additions & 1 deletion sshgen/parsers/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class AnsibleHostsParser:

def __init__(self, hosts_file_path: Path | None = None):
self._yaml = YAML()
self._default_hosts_file = 'examples/hosts.yml'
Expand Down
3 changes: 1 addition & 2 deletions sshgen/run.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/env python

from sshgen.utils.app import AppUtils
from sshgen.utils.file import FileUtils


def run():
def run() -> None:
hosts_file = FileUtils.as_file(file_path='../examples/hosts.yml')
output_file = FileUtils.as_file(file_path='../config')

Expand Down
2 changes: 1 addition & 1 deletion sshgen/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ def _create_file(file_path: Path) -> None:
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.touch(exist_ok=True)
except OSError as e:
log.error("Failed to create file or directory: %s, reason: %s", file_path, e.strerror)
log.exception('Failed to create file or directory: %s, reason: %s', file_path, e.strerror)
sys.exit(1)

0 comments on commit 6f4c7e2

Please sign in to comment.