Implement appending command aliases to bashrc to avoid command not found

This commit is contained in:
Kristóf Tóth 2018-04-12 11:07:56 +02:00
parent 8caf879ffd
commit 16c1fe7b50

View File

@ -7,10 +7,18 @@ LOG = logging.getLogger(__name__)
class TerminalCommands(ABC): class TerminalCommands(ABC):
def __init__(self): def __init__(self, bashrc=None):
self._command_method_regex = r'^command_(.+)$' self._command_method_regex = r'^command_(.+)$'
self.command_implemetations = {self._parse_command_name(fun): getattr(self, fun) for fun in dir(self) self.command_implemetations = {self._parse_command_name(fun): getattr(self, fun) for fun in dir(self)
if callable(getattr(self, fun)) and self._is_command_implementation(fun)} if callable(getattr(self, fun)) and self._is_command_implementation(fun)}
if bashrc is not None:
self._setup_bashrc_aliases(bashrc)
def _setup_bashrc_aliases(self, bashrc):
with open(bashrc, 'a') as ofile:
alias_template = 'alias {0}="{0} > /dev/null 2>&1"\n'
for command in self.command_implemetations.keys():
ofile.write(alias_template.format(command))
def _is_command_implementation(self, method_name): def _is_command_implementation(self, method_name):
return bool(self._match_command_regex(method_name)) return bool(self._match_command_regex(method_name))