def call(self, user_input): """Call appropriate method Params: * user_input -> string Split user command into parts, find appropriate method to call from parts[0], and call it with arguments parts[1:] """ cmd_parts = user_input.split(" ") cmd = cmd_parts[0] arguments = [] if len(cmd_parts) == 1 else cmd_parts[1:] if cmd.strip().lower() in self.cmds: for potential_handler in self.method_handlers: funct = getattr( potential_handler, "cmd_{command}".format(command=cmd), None # default if attr not existent ) if funct is not None: funct(arguments) return helper_methods.log(1, "Command registered but unexistent") else: helper_methods.log(3, "The command doesn't exist")
def stop(self): """Stop the session with current time""" if self.is_finished() is False: self.end_time = datetime.today() helper_methods.log(3, "Session stopped!") else: helper_methods.log(2, "Trying to stop already stopped session!")
def start(self): """Start the session with current time""" if self.is_started() is False: self.start_time = datetime.today() helper_methods.log(3, "Session started!") else: helper_methods.log(2, "Trying to start already started session")
def current_time(self): """Return time difference from now to start of session""" if self.is_started(): return helper_methods.chop_microseconds( datetime.now() - self.start_time ) else: helper_methods.log(1, "This session is unstarted!")
def stop_session(self): """Stop a session If there is a session running then stop it """ if self.current_session is not None: self.current_session.stop() self.current_session = None else: helper_methods.log(2, "No session is started")
def start_session(self): """Start the session Before starting the session make sure that no session is started. otherwise log a notice Register session to a sessions dictionary """ if self.current_session is None: self.current_session = Session(self) self.current_session.start() ses_date = self.current_session.date() if ses_date in self.sessions: self.sessions[ses_date].append(self.current_session) else: self.sessions[ses_date] = [self.current_session] else: helper_methods.log(2, "One session is currently active")
def load(self): try: decrypted = MyCrypto.read_from_file( self.ses_file, self.password ) self.deserialize(json.loads(decrypted)) helper_methods.log(3, "Loaded config from file") except ValueError as e: # noqa self.password_tries -= 1 if self.password_tries == 0: exit("File you try to open is corrupt or password " "is incorrect") print(str(e) + (" Please type password again," "you can do it {tries} more times").format( tries=self.password_tries )) self.password = getpass(prompt) self.load()
def main(): """Initialize the work manager """ try: WM = WorkManager() CP = CommandProcessor([WM]) while 1: try: cmd = input(prompt) if cmd == "exit": raise KeyboardInterrupt() CP.call(cmd) except KeyboardInterrupt: print() if WM.saved == 0: WM.save() helper_methods.log(3, "\nBye bye") break except ValueError as e: print(e) print("Wrong input type.")
def cmd_ims(self, arguments): for date, session in self.session_manager.sessions.items(): helper_methods.log(3, date + "===" + str([ str(ses) for ses in session ]))
def date(self): """Get session date""" if self.is_started(): return self.start_time.strftime(date_format) else: helper_methods.log(1, "This session is unstarted")