From f975ea34fb31cac518ee2fa32da685884d8db273 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Sat, 9 Dec 2023 05:14:06 +0530 Subject: [PATCH 1/2] =?UTF-8?q?Code=20Cleanup=20and=20version=20bump=20?= =?UTF-8?q?=F0=9F=95=BA=F0=9F=8F=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 4 +-- src/bitssh/__init__.py | 3 +- src/bitssh/__main__.py | 23 ++++++------- src/bitssh/argument_parser.py | 14 ++++++++ src/bitssh/core.py | 42 ---------------------- src/bitssh/prompt.py | 26 ++++++++++++++ src/bitssh/ui.py | 20 +++++++++++ src/bitssh/utils.py | 65 ++++++++++++++++++++++------------- 8 files changed, 116 insertions(+), 81 deletions(-) create mode 100644 src/bitssh/argument_parser.py delete mode 100644 src/bitssh/core.py create mode 100644 src/bitssh/prompt.py create mode 100644 src/bitssh/ui.py diff --git a/pyproject.toml b/pyproject.toml index 4eaecab..62bc7f0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "bitssh" -version = "2.0.0" +version = "2.5.0" description = "A command-line tool for managing SSH connections" readme = "README.md" authors = [{ name = "Kanishk Pachauri", email = "itskanishkp.py@gmail.com" }] @@ -32,7 +32,7 @@ Docs = "https://github.com/Mr-Sunglasses/bitssh/blob/master/docs/docs.md" bitssh = "bitssh.__main__:main" [tool.bumpver] -current_version = "2.0.0" +current_version = "2.5.0" version_pattern = "MAJOR.MINOR.PATCH" commit_message = "Bump version {old_version} -> {new_version}" commit = true diff --git a/src/bitssh/__init__.py b/src/bitssh/__init__.py index ae72c01..63e3f9c 100644 --- a/src/bitssh/__init__.py +++ b/src/bitssh/__init__.py @@ -1,5 +1,4 @@ from importlib import resources - try: import tomllib except ModuleNotFoundError: @@ -7,4 +6,4 @@ import tomli as tomllib -__version__ = "2.0.0" +__version__ = "2.5.0" diff --git a/src/bitssh/__main__.py b/src/bitssh/__main__.py index 8e87c50..26d3149 100644 --- a/src/bitssh/__main__.py +++ b/src/bitssh/__main__.py @@ -1,19 +1,18 @@ -import inquirer -from inquirer.themes import GreenPassion import os -from . import core +from .prompt import Prompt +from .ui import UI +from .argument_parser import Config +from bitssh import __version__ def main(): - answers = inquirer.prompt(questions=core.questions, theme=GreenPassion()) - cmd = answers["host"] - cmd = cmd[6::] - cmd = f"ssh {cmd}" - core.console.print( - "Please Wait While Your System is Connecting to the Remote Server", - style="green", - ) - os.system(cmd) + config = Config() + if config.version: + print(f"bitssh {__version__}") + else: + UI.draw_table() + Prompt.ask_host_prompt() + if __name__ == "__main__": main() diff --git a/src/bitssh/argument_parser.py b/src/bitssh/argument_parser.py new file mode 100644 index 0000000..6330e4e --- /dev/null +++ b/src/bitssh/argument_parser.py @@ -0,0 +1,14 @@ +import argparse + +class Config: + def __init__(self) -> None: + parser = argparse.ArgumentParser(description="A New and Modern SSH connector written in Python.") + parser.add_argument( + "--version", + action="store_true", + default=False, + help="Show the bitssh version.", + ) + + args, _ = parser.parse_known_args() + self.version = args.version \ No newline at end of file diff --git a/src/bitssh/core.py b/src/bitssh/core.py deleted file mode 100644 index c518add..0000000 --- a/src/bitssh/core.py +++ /dev/null @@ -1,42 +0,0 @@ -from rich.console import Console -from rich.table import Table -import inquirer -from . import utils -try: - content = utils.get_config_content(config_file=utils.get_config_path()) -except FileNotFoundError: - raise Exception( - "Config file is not Found in ~/.ssh/config Please See the Docs of bitssh.") -ROWS = [] - -for host, attributes in content.items(): - row = (attributes["Hostname"], host, - attributes["port"], attributes["User"]) - ROWS.append(row) - - -table = Table(title="SSH Servers in Config File") - -table.add_column("Hostname", justify="left", style="cyan", no_wrap=True) -table.add_column("Host", style="magenta") -table.add_column("Port", justify="right", style="green") -table.add_column("User", justify="right", style="yellow") - -for i in ROWS: - table.add_row(i[0], i[1], i[2], i[3]) - - -console = Console() -console.print(table) - -HOST = [] - -for hosts in ROWS: - HOST.append(f"Host: {hosts[1]}") - - -questions = [ - inquirer.List( - name="host", message="Select the Host Given in the Above List", choices=HOST - ), -] diff --git a/src/bitssh/prompt.py b/src/bitssh/prompt.py new file mode 100644 index 0000000..fc67f62 --- /dev/null +++ b/src/bitssh/prompt.py @@ -0,0 +1,26 @@ +import inquirer +from inquirer.themes import GreenPassion +from .utils import ConfigPathUtility +from .ui import UI +import os + +class Prompt(): + @classmethod + def ask_host_prompt(cls): + HOST = ConfigPathUtility.get_config_file_host_data() + questions = [ + inquirer.List( + name="host", message="Select the Host Given in the Above List", choices=HOST + ), + ] + + answers = inquirer.prompt(questions=questions, theme=GreenPassion()) + + cmd = answers["host"] + cmd = cmd[6::] + cmd = f"ssh {cmd}" + UI.console.print( + "Please Wait While Your System is Connecting to the Remote Server", + style="green", + ) + os.system(cmd) diff --git a/src/bitssh/ui.py b/src/bitssh/ui.py new file mode 100644 index 0000000..d5cc41d --- /dev/null +++ b/src/bitssh/ui.py @@ -0,0 +1,20 @@ +from rich.console import Console +from rich.table import Table +from .utils import ConfigPathUtility + + +class UI: + console = Console() + + @classmethod + def draw_table(cls): + table = Table(title="SSH Servers in Config File") + table.add_column("Hostname", justify="left", style="cyan", no_wrap=True) + table.add_column("Host", style="magenta") + table.add_column("Port", justify="right", style="green") + table.add_column("User", justify="right", style="yellow") + + for i in ConfigPathUtility.get_config_file_row_data(): + table.add_row(i[0], i[1], i[2], i[3]) + + cls.console.print(table) \ No newline at end of file diff --git a/src/bitssh/utils.py b/src/bitssh/utils.py index b458d64..836a464 100644 --- a/src/bitssh/utils.py +++ b/src/bitssh/utils.py @@ -1,32 +1,51 @@ import os import re +import pprint -def get_config_path(): - try: - config_path = os.path.expanduser("~/.ssh/config") - return config_path - except FileNotFoundError: - raise f"Config File Not Found" +class ConfigPathUtility: + config_file_path = os.path.expanduser("~/.ssh/config") + if not os.path.exists(config_file_path): + raise FileNotFoundError("Config file is not Found in ~/.ssh/config Please See the Docs of bitssh.") + @classmethod + def get_config_content(cls): + config = {} + current_host = None -def get_config_content(config_file): - config = {} - current_host = None + with open(cls.config_file_path, "r") as f: + lines = f.readlines() - with open(config_file, "r") as f: - lines = f.readlines() + for line in lines: + line = line.strip() - for line in lines: - line = line.strip() + if re.match(r"^Host\s+", line): + current_host = re.sub(r"^Host\s+", "", line) + config[current_host] = {} + else: + match = re.match(r"^\s*([\w-]+)\s+(.*)", line) + if match: + key = match.group(1) + value = match.group(2) + config[current_host][key] = value - if re.match(r"^Host\s+", line): - current_host = re.sub(r"^Host\s+", "", line) - config[current_host] = {} - else: - match = re.match(r"^\s*([\w-]+)\s+(.*)", line) - if match: - key = match.group(1) - value = match.group(2) - config[current_host][key] = value + return config - return config \ No newline at end of file + @classmethod + def get_config_file_row_data(cls): + config_content = cls.get_config_content() + + ROWS = [] + + for host, attributes in config_content.items(): + row = (attributes["Hostname"], host, + attributes["port"], attributes["User"]) + ROWS.append(row) + + return ROWS + + @classmethod + def get_config_file_host_data(cls): + HOST = [] + for hosts in cls.get_config_file_row_data(): + HOST.append(f"Host: {hosts[1]}") + return HOST \ No newline at end of file From e8b6d5077d22463735ab2b46257de4fd308cfa1c Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Sat, 9 Dec 2023 05:15:15 +0530 Subject: [PATCH 2/2] =?UTF-8?q?Formated=20with=20black=20=20=F0=9F=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bitssh/__init__.py | 1 + src/bitssh/argument_parser.py | 7 +++++-- src/bitssh/prompt.py | 7 +++++-- src/bitssh/ui.py | 2 +- src/bitssh/utils.py | 12 +++++++----- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/bitssh/__init__.py b/src/bitssh/__init__.py index 63e3f9c..95b247d 100644 --- a/src/bitssh/__init__.py +++ b/src/bitssh/__init__.py @@ -1,4 +1,5 @@ from importlib import resources + try: import tomllib except ModuleNotFoundError: diff --git a/src/bitssh/argument_parser.py b/src/bitssh/argument_parser.py index 6330e4e..b3a9223 100644 --- a/src/bitssh/argument_parser.py +++ b/src/bitssh/argument_parser.py @@ -1,8 +1,11 @@ import argparse + class Config: def __init__(self) -> None: - parser = argparse.ArgumentParser(description="A New and Modern SSH connector written in Python.") + parser = argparse.ArgumentParser( + description="A New and Modern SSH connector written in Python." + ) parser.add_argument( "--version", action="store_true", @@ -11,4 +14,4 @@ def __init__(self) -> None: ) args, _ = parser.parse_known_args() - self.version = args.version \ No newline at end of file + self.version = args.version diff --git a/src/bitssh/prompt.py b/src/bitssh/prompt.py index fc67f62..62f3791 100644 --- a/src/bitssh/prompt.py +++ b/src/bitssh/prompt.py @@ -4,13 +4,16 @@ from .ui import UI import os -class Prompt(): + +class Prompt: @classmethod def ask_host_prompt(cls): HOST = ConfigPathUtility.get_config_file_host_data() questions = [ inquirer.List( - name="host", message="Select the Host Given in the Above List", choices=HOST + name="host", + message="Select the Host Given in the Above List", + choices=HOST, ), ] diff --git a/src/bitssh/ui.py b/src/bitssh/ui.py index d5cc41d..fb19384 100644 --- a/src/bitssh/ui.py +++ b/src/bitssh/ui.py @@ -17,4 +17,4 @@ def draw_table(cls): for i in ConfigPathUtility.get_config_file_row_data(): table.add_row(i[0], i[1], i[2], i[3]) - cls.console.print(table) \ No newline at end of file + cls.console.print(table) diff --git a/src/bitssh/utils.py b/src/bitssh/utils.py index 836a464..5514596 100644 --- a/src/bitssh/utils.py +++ b/src/bitssh/utils.py @@ -2,11 +2,14 @@ import re import pprint -class ConfigPathUtility: +class ConfigPathUtility: config_file_path = os.path.expanduser("~/.ssh/config") if not os.path.exists(config_file_path): - raise FileNotFoundError("Config file is not Found in ~/.ssh/config Please See the Docs of bitssh.") + raise FileNotFoundError( + "Config file is not Found in ~/.ssh/config Please See the Docs of bitssh." + ) + @classmethod def get_config_content(cls): config = {} @@ -37,8 +40,7 @@ def get_config_file_row_data(cls): ROWS = [] for host, attributes in config_content.items(): - row = (attributes["Hostname"], host, - attributes["port"], attributes["User"]) + row = (attributes["Hostname"], host, attributes["port"], attributes["User"]) ROWS.append(row) return ROWS @@ -48,4 +50,4 @@ def get_config_file_host_data(cls): HOST = [] for hosts in cls.get_config_file_row_data(): HOST.append(f"Host: {hosts[1]}") - return HOST \ No newline at end of file + return HOST