Add rudimentary support for specifications
This commit is contained in:
parent
0f3b523d68
commit
6d162b2f0e
7 changed files with 82 additions and 18 deletions
|
@ -40,6 +40,33 @@ def main():
|
||||||
arguments, _ = arg_parser.parse_known_args()
|
arguments, _ = arg_parser.parse_known_args()
|
||||||
with open(arguments.config or config_directory.default_config(), mode="rb") as file:
|
with open(arguments.config or config_directory.default_config(), mode="rb") as file:
|
||||||
config = tomllib.load(file)
|
config = tomllib.load(file)
|
||||||
|
|
||||||
|
config["spec"] = config.get("spec", {})
|
||||||
|
for spec_name, spec in config["spec"].items():
|
||||||
|
print(spec_name, spec)
|
||||||
|
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"]}" ]
|
||||||
|
print(flags)
|
||||||
|
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["config"] = config.get("config", {})
|
||||||
config["flags" ] = config.get("flags", {})
|
config["flags" ] = config.get("flags", {})
|
||||||
|
|
||||||
|
@ -69,6 +96,7 @@ def main():
|
||||||
logger.add(command_line)
|
logger.add(command_line)
|
||||||
plugins = PluginManager(
|
plugins = PluginManager(
|
||||||
logger,
|
logger,
|
||||||
|
use_spec,
|
||||||
config["config"],
|
config["config"],
|
||||||
config["flags" ],
|
config["flags" ],
|
||||||
arg_parser,
|
arg_parser,
|
||||||
|
|
|
@ -35,8 +35,9 @@ class PluginLoader:
|
||||||
in ([ "command_line" ] if command_line else []) + [
|
in ([ "command_line" ] if command_line else []) + [
|
||||||
"file",
|
"file",
|
||||||
"stdin",
|
"stdin",
|
||||||
"current_time",
|
"preserve",
|
||||||
"append_type",
|
"time",
|
||||||
|
"extension",
|
||||||
"ssh",
|
"ssh",
|
||||||
"uri",
|
"uri",
|
||||||
"print_location",
|
"print_location",
|
||||||
|
@ -60,7 +61,7 @@ class PluginLoader:
|
||||||
]
|
]
|
||||||
|
|
||||||
class PluginManager:
|
class PluginManager:
|
||||||
def __init__(self, logger, config, flags, arg_parser):
|
def __init__(self, logger, spec, config, flags, arg_parser):
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
self._arg_parser = arg_parser
|
self._arg_parser = arg_parser
|
||||||
|
|
||||||
|
@ -71,14 +72,22 @@ class PluginManager:
|
||||||
for type in Plugin.types():
|
for type in Plugin.types():
|
||||||
setattr(self, type, PluginState())
|
setattr(self, type, PluginState())
|
||||||
|
|
||||||
self._uninitialized = PluginLoader.all(
|
self._uninitialized = []
|
||||||
|
uninitialized = PluginLoader.all(
|
||||||
command_line=False,
|
command_line=False,
|
||||||
logger=logger,
|
logger=logger,
|
||||||
config=config,
|
config=config,
|
||||||
flags=flags,
|
flags=flags,
|
||||||
)
|
)
|
||||||
for plugin in self._uninitialized:
|
for plugin in uninitialized:
|
||||||
plugin.add_args(arg_parser)
|
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):
|
def activate(self, activate_type=None):
|
||||||
args = self._arg_parser.parse_args()
|
args = self._arg_parser.parse_args()
|
||||||
|
|
|
@ -15,7 +15,8 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
class Raw:
|
class Raw:
|
||||||
def __init__(self, type, data):
|
def __init__(self, name, type, data):
|
||||||
|
self.name = name
|
||||||
self.type = type
|
self.type = type
|
||||||
self.data = data
|
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 != None:
|
||||||
|
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():
|
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"
|
plugin_type = "name"
|
||||||
|
|
||||||
config = {
|
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):
|
def get_name(name, source):
|
||||||
|
if name != None:
|
||||||
|
name = name + "_"
|
||||||
return name + _rebase(config.base, time.time_ns())
|
return name + _rebase(config.base, time.time_ns())
|
||||||
|
|
||||||
def _rebase(base, number):
|
def _rebase(base, number):
|
Loading…
Reference in a new issue