Effortlessly run scripts when certain files change.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.4 KiB

#!/usr/bin/env python3.9
import sys
import time
from utils.config import Config
from utils.interface import Interface
from utils.parser import Parser
from utils.logger import Logger
from utils.watcher import Watcher
class Miru:
def __init__(self):
self.config = Config()
self.interface = Interface()
self.parser = Parser()
self.logger = Logger()
self.watcher = Watcher(self.logger)
self._recursive_walks = False
self.config_value = None
# I could have done the traditional CLI
# route and made this a little more efficient,
# but you simply can't get more explicit than this.
self.argument_shortcuts = {
'-c': '--create',
'-r': '--reset',
'-d': '--delete',
'-e': '--edit',
'-v': '--view',
'-h': '--help',
'-p': '--pass'
}
self.arguments = {
'--create': self.config.create,
'--reset': self.config.reset,
'--delete': self.config.delete,
'--edit': self.config.edit,
'--view': self.config.echo,
'--help': self.interface.help,
'--pass': self.watcher.pass_argument
}
@property
def _parsed_config(self):
# If not invoked with -cl, will use config
# file.
# Example:
# ['modified', '/home/me/Downloads', '/path/to/script.sh']
if not self.config_value:
self.config_value = self.config._get_config()
return self.parser.parse(self.config_value)
def _walk_arguments(self):
for arg in sys.argv:
if self.arguments.get(arg, False):
self.arguments[arg]()
if arg != "--pass":
quit()
elif self.argument_shortcuts.get(arg, False):
self.arguments[self.argument_shortcuts[arg]]()
if arg != "-p":
quit()
# if no matching argument is found, miru will start watching.
def _handle_special_arguments(self):
if '-l' in sys.argv or '--log' in sys.argv:
self.logger.should_output = True
self.logger.log_to_file('==========')
print('Logging enabled.')
if '-re' in sys.argv or '--recursive' in sys.argv:
self._recursive_walks = True
print('Recursive walking enabled.')
if '-p' in sys.argv or '--pass' in sys.argv:
print('Argument passing enabled.')
if '-cl' in sys.argv or '--command-line' in sys.argv:
self.config_value = [sys.argv[2]] # exempli gratia: miru -cl any:::/path/:::command
def __call__(self):
self._walk_arguments() # arguments are expected to quit if there's a problem
self._handle_special_arguments()
self.watcher(self._parsed_config, self._recursive_walks)
if __name__ == '__main__':
miru = Miru()
miru()