Onionr/src/onionrcommands/parser/arguments.py

99 lines
3.9 KiB
Python
Raw Normal View History

"""Onionr - Private P2P Communication.
2019-08-29 09:25:21 +00:00
Sets CLI arguments for Onionr
2019-12-19 10:32:40 +00:00
"""
from typing import Callable
2022-01-12 00:13:19 +00:00
from .. import onionrstatistics, version, daemonlaunch
2019-12-19 10:32:40 +00:00
from .. import openwebinterface
from .. import banblocks # Command to blacklist a block by its hash
from .. import filecommands # commands to share files with onionr
from .. import exportblocks # commands to export blocks
from .. import pubkeymanager # commands to add or change id
from .. import resetplugins # command to reinstall default plugins
from .. import softreset # command to delete onionr blocks
from .. import restartonionr # command to restart Onionr
2019-12-29 17:21:18 +00:00
from .. import runtimetestcmd # cmd to execute the runtime integration tests
from .. import sitecreator # cmd to create multi-page sites
2020-02-05 07:49:24 +00:00
from ..listsites import print_site_list # cmd to list list ids
2019-12-19 10:32:40 +00:00
import onionrexceptions
from onionrutils import importnewblocks # func to import new blocks
"""
2022-01-12 00:13:19 +00:00
This program 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.
2019-08-29 09:25:21 +00:00
2022-01-12 00:13:19 +00:00
This program 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.
2019-08-29 09:25:21 +00:00
2022-01-12 00:13:19 +00:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-12-19 10:32:40 +00:00
"""
2019-12-19 10:32:40 +00:00
def get_arguments() -> dict:
"""Return command argument dict, minus plugin cmds.
This is a function because we need to be able to
dynamically modify them with plugins
"""
2019-07-31 05:10:28 +00:00
args = {
2019-12-19 10:32:40 +00:00
('blacklist', 'blacklist-block', 'remove-block',
'removeblock', 'banblock', 'ban-block'): banblocks.ban_block,
2019-07-31 05:10:28 +00:00
('details', 'info'): onionrstatistics.show_details,
2019-08-05 23:09:04 +00:00
('stats', 'statistics'): onionrstatistics.show_stats,
2019-08-29 22:17:47 +00:00
('version',): version.version,
2019-08-04 04:52:57 +00:00
('start', 'daemon'): daemonlaunch.start,
('stop', 'kill'): daemonlaunch.kill_daemon,
2019-09-20 16:25:12 +00:00
('restart',): restartonionr.restart,
2019-12-19 10:32:40 +00:00
('openhome', 'gui', 'openweb',
'open-home', 'open-web'): openwebinterface.open_home,
2019-09-30 03:03:55 +00:00
('get-url', 'url', 'get-web'): openwebinterface.get_url,
('addhtml', 'add-html'): filecommands.add_html,
2019-12-19 10:32:40 +00:00
('addsite', 'add-site',
'update-site', 'updatesite'): sitecreator.create_multipage_site,
('listsites', 'list-sites'): print_site_list,
2019-08-05 23:09:04 +00:00
('addfile', 'add-file'): filecommands.add_file,
('get-file', 'getfile'): filecommands.get_file,
('export-block', 'exportblock'): exportblocks.export_block,
2019-12-19 10:32:40 +00:00
('importblocks',
'import-blocks', 'import-block'): importnewblocks.import_new_blocks,
2019-08-05 23:09:04 +00:00
('addid', 'add-id'): pubkeymanager.add_ID,
('changeid', 'change-id'): pubkeymanager.change_ID,
2019-09-09 08:23:09 +00:00
('add-vanity', 'addvanity'): pubkeymanager.add_vanity,
2019-09-01 09:13:46 +00:00
('resetplugins', 'reset-plugins'): resetplugins.reset,
2019-09-26 07:43:03 +00:00
('soft-reset', 'softreset'): softreset.soft_reset,
2022-01-12 00:13:19 +00:00
('runtime-test', 'runtimetest'): runtimetestcmd.do_runtime_test
2019-07-31 05:10:28 +00:00
}
return args
2019-12-19 10:32:40 +00:00
2019-08-29 22:17:47 +00:00
def get_help(arg: str) -> str:
"""Return the help info string from a given command."""
2019-08-29 22:17:47 +00:00
arguments = get_arguments()
# Iterate the command alias tuples
for argument in arguments:
# Return the help message if its found in a command alias tuple
2019-12-19 10:32:40 +00:00
if arg in argument:
return arguments[argument].onionr_help
2019-08-29 22:17:47 +00:00
raise KeyError
2019-07-31 05:10:28 +00:00
2019-12-19 10:32:40 +00:00
2019-09-14 06:29:31 +00:00
def get_func(argument: str) -> Callable:
"""Return the function for a given command argument."""
2019-07-31 05:10:28 +00:00
argument = argument.lower()
args = get_arguments()
2019-12-19 10:32:40 +00:00
for arg in args.keys(): # Iterate command alias sets
"""
If our argument is in the current alias set,
return the command function
"""
if argument in arg:
2019-07-31 05:10:28 +00:00
return args[arg]
2019-08-29 22:17:47 +00:00
raise onionrexceptions.NotFound