Skip to content

Commit

Permalink
ADD: initial version of pylint static code checker
Browse files Browse the repository at this point in the history
  • Loading branch information
vdmitriyev committed Jan 13, 2025
1 parent fa7c005 commit 1fccf86
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[FORMAT]
max-line-length=240
9 changes: 9 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ tasks:
- echo "running bandit (static code analysis)"
- bandit -x "./.venv,./.tox,./tests" -r .

pylint:
desc: runs pylint
aliases: []
deps: [check-virtualenv]
silent: true
cmds:
- echo "running pylint (static code analysis)"
- pylint pymultissher

tox:
desc: runs tox
silent: true
Expand Down
36 changes: 18 additions & 18 deletions pymultissher/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,35 +271,35 @@ def init(
):
"""Generates initial YAML configuration files with SSH defaults, domains and commands"""

console = Console()
_console = Console()
yml_handler = YAMLEmptyConfigHandler()

try:
yml_handler.generate_empty_configs_domains(filename=file_domains)
console.print(f"Generated config file for domains: ", end=None)
console.print(f"{file_domains}", style="green")
except YAMLConfigExists as ex:
console.print(f"Config file already exists: ", style="red", end=None)
console.print(f"{file_domains}", style="yellow")
_console.print("Generated config file for domains: ", end=None)
_console.print("{file_domains}", style="green")
except YAMLConfigExists:
_console.print("Config file already exists: ", style="red", end=None)
_console.print("{file_domains}", style="yellow")
if not verbose:
console.print(f"Exception: ", end=None)
console.print(f"{ex}", style="red")
_console.print("Exception: ", end=None)
_console.print("{ex}", style="red")
else:
console.print_exception(show_locals=True)
_console.print_exception(show_locals=True)

try:
yml_handler.generate_empty_configs_commands(filename=file_commands)
console.print(f"Generated config file for commands: ", end=None)
console.print(f"{file_commands}", style="green")
_console.print("Generated config file for commands: ", end=None)
_console.print("{file_commands}", style="green")
except YAMLConfigExists as ex:
console.print(f"Config file already exists: ", style="red", end=None)
console.print(f"{file_commands}", style="yellow")
_console.print("Config file already exists: ", style="red", end=None)
_console.print("{file_commands}", style="yellow")

if not verbose:
console.print(f"Exception: ", end=None)
console.print(f"{ex}", style="red")
_console.print("Exception: ", end=None)
_console.print("{ex}", style="red")
else:
console.print_exception(show_locals=True)
_console.print_exception(show_locals=True)


@app.command()
Expand All @@ -312,8 +312,8 @@ def version(
"""Shows package version"""

if not verbose:
console.print(f"Version: ", style="white", end=None)
console.print(f"{package_version()}", style="yellow")
console.print("Version: ", style="white", end=None)
console.print("{package_version()}", style="yellow")
else:
table = Table()
table.add_column("Field", justify="right", style="cyan", no_wrap=True)
Expand Down
2 changes: 2 additions & 0 deletions pymultissher/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Provides module specific constants."""

import os

BASEDIR = os.path.abspath(os.path.dirname(__file__))
Expand Down
27 changes: 9 additions & 18 deletions pymultissher/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
class MultiSSHerException(Exception):
"""Generic exception for MultiSSHer"""
"""Provides module specific exceptions."""


pass
class MultiSSHerException(Exception):
"""Generic exception for MultiSSHer."""


class MultiSSHerNotSupported(Exception):
"""Exception for non supported features"""

pass
"""Exception for non supported features."""


class MultiSSHerCreateClient(Exception):
"""MultiSSHer was not able to create SSH client"""

pass
"""MultiSSHer was not able to create SSH client."""


class YAMLGenericException(Exception):
"""MultiSSHer YAML validation error"""

pass
"""MultiSSHer YAML validation error."""


class YAMLValidationError(Exception):
"""MultiSSHer YAML validation error"""

pass
"""MultiSSHer YAML validation error."""


class YAMLConfigExists(Exception):
"""MultiSSHer YAML already exists error"""

pass
"""MultiSSHer YAML already exists error."""
7 changes: 5 additions & 2 deletions pymultissher/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Provides module specific helper functions."""


def handle_dict_keys(data: dict, key: str) -> None:
"""Check if key exists in dict and add it, if it doesn't exist.
Expand All @@ -13,10 +16,10 @@ def handle_dict_keys(data: dict, key: str) -> None:
verbose = False


def is_verbose():
def is_verbose() -> bool:
return verbose


def set_verbose(value):
def set_verbose(value: bool) -> None:
global verbose
verbose = value
24 changes: 17 additions & 7 deletions pymultissher/pymultissher.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import logging
import os
import traceback
from dataclasses import dataclass
Expand All @@ -12,13 +11,15 @@

from pymultissher.constants import SUPPORTED_SSH_KEY_TYPES, VIEW_CONSOLE_FORMATS
from pymultissher.exceptions import MultiSSHerException, MultiSSHerNotSupported
from pymultissher.helpers import is_verbose, set_verbose
from pymultissher.helpers import is_verbose
from pymultissher.logger import get_logger
from pymultissher.yamlhandler import YAMLHandler


@dataclass
class SSHCredentials:
"""Handles SSH credentials as a class"""

domain: str = "localhost"
port: int = 22
username: str = "root"
Expand All @@ -28,6 +29,7 @@ class SSHCredentials:


class MultiSSHer:
"""Main class to handle all SSH interactions"""

def __init__(self, logger=None):
if logger is None:
Expand All @@ -38,8 +40,15 @@ def __init__(self, logger=None):
self.data = {}
self.ssh_defaults = SSHCredentials()
self.console = Console()
self.domains = []
self.client = None

def verbose_print(self, msg: str) -> None:
"""Prints additional message, if verbose flag has been set.
Args:
msg (str): Message to print
"""
if is_verbose():
self.console.print(msg, style="yellow")

Expand Down Expand Up @@ -85,7 +94,7 @@ def load_domains(self, filename: str):
yml_handler.load_data()

if "domains" not in yml_handler.data:
raise MultiSSHerException(f"Do domains were found")
raise MultiSSHerException("Domains have been found")

self.domains = yml_handler.data["domains"]

Expand All @@ -96,6 +105,7 @@ def create_client(self, ssh_host: SSHCredentials) -> None:
ssh_host(SSHCredentials): data class that contains all necessary parameters for SSH connection
"""

key = None
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # nosec B507

Expand Down Expand Up @@ -183,13 +193,13 @@ def execute_cmd_and_read_response(self, cmd: str) -> str:
transport = self.client.get_transport()

if transport is None:
self.logger.error(f"Something wrong with transport: client.get_transport()")
self.logger.error("Something went wrong with transport: client.get_transport()")
return None

if transport.is_active():
try:
transport.send_ignore()
except Exception as _e:
except Exception:
self.logger.error(traceback.format_exc())
else:
self.logger.error(f"Something wrong with transport. transport.is_active(): {transport.is_active()}")
Expand Down Expand Up @@ -277,8 +287,8 @@ def apply_filter_on_domains(self, filter: str = None) -> list:

filter = filter.lower()

self.console.print(f"Filter domains based on: ", style="white", end=None)
self.console.print(f"{filter}", style="cyan")
self.console.print("Filter domains based on: ", style="white", end=None)
self.console.print("{filter}", style="cyan")

filtered_domains = []
for item in self.domains:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
name = "pymultissher"
description = "pymultissher is a simple CLI tool that runs commands on multiple servers at once using SSH"
readme = "README.md"
version = "0.3.13"
version = "0.3.14"
authors = [
{name = "vdmitriyev"}
]
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pytest-docker==3.1.1
docker==7.1.0
pre-commit==4.0.1
bandit==1.8.0
pylint==3.3.3

0 comments on commit 1fccf86

Please sign in to comment.