* Refactored onionrcommands py to be linting compliant (started using mypy)

+ git ignore mypy cache
This commit is contained in:
Kevin Froman 2019-12-19 04:34:19 -06:00
parent 6d123b93fc
commit 906219fe30
20 changed files with 617 additions and 322 deletions

View file

@ -1,7 +1,6 @@
"""
Onionr - Private P2P Communication
"""Onionr - Private P2P Communication.
This module loads in the Onionr arguments and their help messages
This module loads in the Onionr arguments and their help messages
"""
import sys
import os
@ -28,10 +27,12 @@ from . import arguments, recommend
def plugin_command(cmd):
"""Build a plugin command function name."""
return f'on_{cmd}_cmd'
def register_plugin_commands(cmd) -> bool:
"""Find a plugin command hook and execute it for a given cmd."""
plugin_cmd = plugin_command(cmd)
for pl in onionrplugins.get_enabled_plugins():
pl = onionrplugins.get_plugin(pl)
@ -46,11 +47,10 @@ def _show_term(msg: str):
def register():
"""Registers commands and handles help command processing"""
"""Register commands and handles help command processing."""
def get_help_message(cmd: str,
default: str = 'No help available for this command'):
"""Return help message for a given command, supports plugin commands"""
"""Print help message for a given command, supports plugin commands."""
pl_cmd = plugin_command(cmd)
for pl in onionrplugins.get_enabled_plugins():
pl = onionrplugins.get_plugin(pl)
@ -61,7 +61,7 @@ def register():
pass
for i in arguments.get_arguments():
for alias in i:
for _ in i:
try:
return arguments.get_help(cmd)
except AttributeError:
@ -78,16 +78,19 @@ def register():
return
is_help_cmd = False
if cmd.replace('--', '').lower() == 'help': is_help_cmd = True
if cmd.replace('--', '').lower() == 'help':
is_help_cmd = True
try:
try:
if cmd not in ('start', 'details', 'show-details'):
os.chdir(os.environ['ORIG_ONIONR_RUN_DIR'])
except KeyError: pass
except KeyError:
pass
try:
arguments.get_func(cmd)()
except KeyboardInterrupt: pass
except KeyboardInterrupt:
pass
except onionrexceptions.NotFound:
if not register_plugin_commands(cmd) and not is_help_cmd:
recommend.recommend()

View file

@ -1,7 +1,6 @@
"""
Onionr - Private P2P Communication
"""Onionr - Private P2P Communication.
Sets CLI arguments for Onionr
Sets CLI arguments for Onionr
"""
from typing import Callable
@ -38,8 +37,11 @@ from onionrutils import importnewblocks # func to import new blocks
def get_arguments() -> dict:
"""This is a function because we need to be able
to dynamically modify them with plugins"""
"""Return command argument dict, minus plugin cmds.
This is a function because we need to be able to
dynamically modify them with plugins
"""
args = {
('blacklist', 'blacklist-block', 'remove-block',
'removeblock', 'banblock', 'ban-block'): banblocks.ban_block,
@ -76,7 +78,7 @@ def get_arguments() -> dict:
def get_help(arg: str) -> str:
"""Returns the help info string from a given command"""
"""Return the help info string from a given command."""
arguments = get_arguments()
# Iterate the command alias tuples
for argument in arguments:
@ -87,7 +89,7 @@ def get_help(arg: str) -> str:
def get_func(argument: str) -> Callable:
"""Returns the function for a given command argument"""
"""Return the function for a given command argument."""
argument = argument.lower()
args = get_arguments()

View file

@ -1,10 +1,29 @@
"""Onionr - Private P2P Communication.
Try to provide recommendations for invalid Onionr commands
"""
import sys
from difflib import SequenceMatcher
import logger
from . import arguments
"""
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.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
def recommend(print_default: bool = True):
"""Print out a recommendation for argv cmd if one is available."""
tried = sys.argv[1]
args = arguments.get_arguments()
print_message = 'Command not found:'
@ -15,5 +34,5 @@ def recommend(print_default: bool = True):
+ 'did you mean "{word}"?',
terminal=True)
return
if print_default: logger.error('%s "%s"' %
(print_message, tried), terminal=True)
if print_default:
logger.error('%s "%s"' % (print_message, tried), terminal=True)