From 865f66b79df693616e02bef9e555a25d84200baf Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Sat, 14 Sep 2024 18:14:28 +0000 Subject: [PATCH 1/2] Oops --- test_in.py => _test_in.py | 0 test_out.py => _test_out.py | 0 pyproject.toml | 5 +++++ src/sshare/plugin/plugin.py | 7 +++++-- src/sshare/validator.py | 2 +- tests/test_test.py | 4 ++++ 6 files changed, 15 insertions(+), 3 deletions(-) rename test_in.py => _test_in.py (100%) rename test_out.py => _test_out.py (100%) create mode 100644 tests/test_test.py diff --git a/test_in.py b/_test_in.py similarity index 100% rename from test_in.py rename to _test_in.py diff --git a/test_out.py b/_test_out.py similarity index 100% rename from test_out.py rename to _test_out.py diff --git a/pyproject.toml b/pyproject.toml index acb06ef..3fb26fb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,3 +29,8 @@ sshare-validate = "sshare.validator:main" [tool.setuptools_scm] version_file = "src/sshare/version.py" + +[tool.pytest.ini_options] +addopts = [ + "--import-mode=importlib" +] diff --git a/src/sshare/plugin/plugin.py b/src/sshare/plugin/plugin.py index 3d75c6b..558f2c2 100644 --- a/src/sshare/plugin/plugin.py +++ b/src/sshare/plugin/plugin.py @@ -14,6 +14,9 @@ import importlib import importlib.util +import sys + +from pathlib import Path from sshare.plugin.config import Flag from sshare.plugin.config import NoDefault @@ -49,9 +52,9 @@ class PluginLoader: ] @staticmethod - def at(logger=None, config=dict(), flags=dict(), *args): + def at(*args, logger=None, config=dict(), flags=dict()): return [ - Plugin.external(plugin, logger, config, flags) + Plugin.external(Path(plugin), logger, config, flags) for plugin in args ] diff --git a/src/sshare/validator.py b/src/sshare/validator.py index f4e21fd..b003ba2 100644 --- a/src/sshare/validator.py +++ b/src/sshare/validator.py @@ -113,7 +113,7 @@ def main(): arg_parser.add_argument( "plugins", nargs="*", - help="plugin(s) to be validated", + help="plugin(s) to be validated (Default: all external plugins)", ) arguments = arg_parser.parse_args() if arguments.dev: diff --git a/tests/test_test.py b/tests/test_test.py new file mode 100644 index 0000000..3c71169 --- /dev/null +++ b/tests/test_test.py @@ -0,0 +1,4 @@ +from src.sshare.plugin.config import Argument + +def test_foo(): + assert True From 82b4d8de7c8835f5c97d80e7cf5867550405f758 Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Tue, 24 Sep 2024 17:51:37 +0000 Subject: [PATCH 2/2] WIP command wrapper. Kind of works....but also janky. Not sure how to make it better :/ --- _test_out.py | 5 +-- src/sshare/plugin/config.py | 18 ++++++++--- src/sshare/plugin/plugin.py | 1 + src/sshare/plugins/stdin.py | 2 +- src/sshare/plugins/wrap_command.py | 50 ++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 src/sshare/plugins/wrap_command.py diff --git a/_test_out.py b/_test_out.py index f89bd5d..94711d4 100755 --- a/_test_out.py +++ b/_test_out.py @@ -1,4 +1,5 @@ #!/usr/bin/python -for i in range(0, 5): - print("\033[91mHello World!") +for i in range(0, 10000): + print(f"\033[{(i % 7) + 30}mHello World!") + print(f"\033[{(i % 7) + 90}mHello World!") diff --git a/src/sshare/plugin/config.py b/src/sshare/plugin/config.py index 6df34ed..b12f099 100644 --- a/src/sshare/plugin/config.py +++ b/src/sshare/plugin/config.py @@ -12,6 +12,8 @@ # You should have received a copy of the GNU General Public License along with # SSHare. If not, see . +import argparse + class NoDefault: pass def Flag(name=None, help=None): @@ -33,6 +35,8 @@ class Argument: self._short = None self._long = name + self._is_remainder = kwargs.get("nargs", None) == argparse.REMAINDER + if not "default" in kwargs: kwargs["default"] = NoDefault kwargs["default"] = _None(kwargs["default"]) @@ -43,6 +47,8 @@ class Argument: if self._long == None: self._long = argument self._kwargs["metavar"] = argument + if self._is_remainder: + self._long = self.dest() def set_flags(self, short, long): if short != None: @@ -75,11 +81,13 @@ class Argument: def add(self, arg_parser): flags = [] - if self._short != None: flags.append(f"-{self._short}") - if self._long != None: flags.append(f"--{self._long}") - kwargs = self._kwargs | { - "dest": self.dest() - } + kwargs = self._kwargs + if self._is_remainder: + flags.append(self._long) + else: + if self._short != None: flags.append(f"-{self._short}") + if self._long != None: flags.append(f"--{self._long}") + kwargs["dest"] = self.dest() arg_parser.add_argument( *flags, **kwargs diff --git a/src/sshare/plugin/plugin.py b/src/sshare/plugin/plugin.py index 558f2c2..dc413c6 100644 --- a/src/sshare/plugin/plugin.py +++ b/src/sshare/plugin/plugin.py @@ -35,6 +35,7 @@ class PluginLoader: in ([ "command_line" ] if command_line else []) + [ "file", "stdin", + "wrap_command", "current_time", "append_type", "ssh", diff --git a/src/sshare/plugins/stdin.py b/src/sshare/plugins/stdin.py index 7b219b6..d4edc64 100644 --- a/src/sshare/plugins/stdin.py +++ b/src/sshare/plugins/stdin.py @@ -21,7 +21,7 @@ plugin_type = "source" activate = { "stdin" } config = { - "suffix": "txt" + "suffix": "txt", } args = { "stdin": Flag(help="Upload from stdin") diff --git a/src/sshare/plugins/wrap_command.py b/src/sshare/plugins/wrap_command.py new file mode 100644 index 0000000..410874b --- /dev/null +++ b/src/sshare/plugins/wrap_command.py @@ -0,0 +1,50 @@ +# This file is part of SSHare. +# +# SSHare is free software: you can redistribute it and/or modify it under the terms of +# the GNU General Public License as published by the Free Software Foundation, +# either version 3 of the License, or (at your option) any later version. +# +# SSHare is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# SSHare. If not, see . + +import argparse +import locale +import subprocess + +from sshare.plugin.config import Argument +from sshare.plugin.source import Raw + +plugin_type = "source" + +activate = { "command" } +config = { + "suffix": "txt", +} +args = { + "command": Argument( + nargs=argparse.REMAINDER, + help="Upload the contents of the wrapped command", + ) +} + +def init(): + config.command = config.command[1:] + +def get_source(): + output = b"" + with subprocess.Popen( + config.command, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + bufsize=0, + ) as process: + for line in process.stdout: + print(line.decode(locale.getpreferredencoding()), end="") + output += line + + return Raw(config.suffix, output)