Add rudimentary support for specifications
This commit is contained in:
parent
0f3b523d68
commit
92f5e1f47a
8 changed files with 81 additions and 19 deletions
2
examples
2
examples
|
@ -1 +1 @@
|
|||
Subproject commit 3273aa4e4c1898c484e989340a8525edb343cebe
|
||||
Subproject commit 99f2e9c3d2fbab02b0582dc8dcf9ed05df7789a6
|
|
@ -40,6 +40,31 @@ def main():
|
|||
arguments, _ = arg_parser.parse_known_args()
|
||||
with open(arguments.config or config_directory.default_config(), mode="rb") as file:
|
||||
config = tomllib.load(file)
|
||||
|
||||
config["spec"] = config.get("spec", {})
|
||||
for spec_name, spec in config["spec"].items():
|
||||
if spec_name != "default":
|
||||
flags = []
|
||||
if spec.get("flag", {}).get("short") != None:
|
||||
flags = flags + [ f"-{spec["flag"]["short"]}" ]
|
||||
if spec.get("flag", {}).get("long") != None:
|
||||
flags = flags + [ f"--{spec["flag"]["long"]}" ]
|
||||
arg_parser.add_argument(
|
||||
*flags,
|
||||
action="store_const",
|
||||
const=True,
|
||||
default=False,
|
||||
help=spec.get(help, f"Use {spec_name} spec"),
|
||||
dest=spec_name,
|
||||
)
|
||||
|
||||
use_spec = config["spec"].get("default", {})
|
||||
arguments, _ = arg_parser.parse_known_args()
|
||||
for spec_name, spec in config["spec"].items():
|
||||
if spec_name != "default":
|
||||
if getattr(arguments, spec_name):
|
||||
use_spec = spec
|
||||
|
||||
config["config"] = config.get("config", {})
|
||||
config["flags" ] = config.get("flags", {})
|
||||
|
||||
|
@ -69,6 +94,7 @@ def main():
|
|||
logger.add(command_line)
|
||||
plugins = PluginManager(
|
||||
logger,
|
||||
use_spec,
|
||||
config["config"],
|
||||
config["flags" ],
|
||||
arg_parser,
|
||||
|
|
|
@ -35,8 +35,9 @@ class PluginLoader:
|
|||
in ([ "command_line" ] if command_line else []) + [
|
||||
"file",
|
||||
"stdin",
|
||||
"current_time",
|
||||
"append_type",
|
||||
"preserve",
|
||||
"time",
|
||||
"extension",
|
||||
"ssh",
|
||||
"uri",
|
||||
"print_location",
|
||||
|
@ -60,7 +61,7 @@ class PluginLoader:
|
|||
]
|
||||
|
||||
class PluginManager:
|
||||
def __init__(self, logger, config, flags, arg_parser):
|
||||
def __init__(self, logger, spec, config, flags, arg_parser):
|
||||
self._logger = logger
|
||||
self._arg_parser = arg_parser
|
||||
|
||||
|
@ -71,14 +72,22 @@ class PluginManager:
|
|||
for type in Plugin.types():
|
||||
setattr(self, type, PluginState())
|
||||
|
||||
self._uninitialized = PluginLoader.all(
|
||||
self._uninitialized = []
|
||||
uninitialized = PluginLoader.all(
|
||||
command_line=False,
|
||||
logger=logger,
|
||||
config=config,
|
||||
flags=flags,
|
||||
)
|
||||
for plugin in self._uninitialized:
|
||||
for plugin in uninitialized:
|
||||
plugin.add_args(arg_parser)
|
||||
if not "name" in plugin.plugin_type:
|
||||
self._uninitialized.append(plugin)
|
||||
|
||||
for name in spec.get("name", []):
|
||||
for plugin in uninitialized:
|
||||
if plugin.name == name:
|
||||
self._uninitialized.append(plugin)
|
||||
|
||||
def activate(self, activate_type=None):
|
||||
args = self._arg_parser.parse_args()
|
||||
|
|
|
@ -15,7 +15,8 @@
|
|||
from pathlib import Path
|
||||
|
||||
class Raw:
|
||||
def __init__(self, type, data):
|
||||
def __init__(self, name, type, data):
|
||||
self.name = name
|
||||
self.type = type
|
||||
self.data = data
|
||||
|
||||
|
|
32
src/sshare/plugins/preserve.py
Normal file
32
src/sshare/plugins/preserve.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
# 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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
from sshare.plugin.source import File
|
||||
from sshare.plugin.source import Raw
|
||||
|
||||
plugin_type = "name"
|
||||
|
||||
def get_name(name, source):
|
||||
if name != "":
|
||||
name = name + "_"
|
||||
else:
|
||||
name = ""
|
||||
if isinstance(source, File):
|
||||
components = source.path.name.split(".")
|
||||
if components[0] == "":
|
||||
return name + "." + components[1]
|
||||
else:
|
||||
return name + components[0]
|
||||
else:
|
||||
return name + source.name
|
|
@ -28,4 +28,4 @@ args = {
|
|||
}
|
||||
|
||||
def get_source():
|
||||
return Raw(config.suffix, sys.stdin.buffer.read())
|
||||
return Raw("stdin", config.suffix, sys.stdin.buffer.read())
|
||||
|
|
|
@ -20,18 +20,12 @@ from sshare.plugin.source import File
|
|||
plugin_type = "name"
|
||||
|
||||
config = {
|
||||
"base": 62,
|
||||
"format": 62,
|
||||
}
|
||||
|
||||
def init():
|
||||
if not isinstance(config.base, int):
|
||||
logger.fatal("Error: 'base' must be an integer")
|
||||
elif config.base < 2:
|
||||
logger.fatal("Error: 'base' cannot be less than 2")
|
||||
elif config.base > 62:
|
||||
logger.fatal("Error: 'base' cannot be greater than 62")
|
||||
|
||||
def get_name(name, source):
|
||||
if name != "":
|
||||
name = name + "_"
|
||||
return name + _rebase(config.base, time.time_ns())
|
||||
|
||||
def _rebase(base, number):
|
Loading…
Reference in a new issue