class Capsule: def __init__(self, host, port): self.ns = NameService() self.marshaller_client = Marshaller() self.c = ClientRequestHandler() self.requestor = Requestor(self.marshaller_client, self.c) self.server = threading.Thread(target = self.createServer, args = (host, port) ) self.server.daemon = True self.server.start() def resetNS(self): self.ns.reset() def createServer(self, host, port): self.marshaller_server = Marshaller() self.invoker = Invoker(self.marshaller_server) self.s = ServerRequestHandler(host, port, self.invoker) self.invoker.addServer(self.s); def registerRemoteObject(self, id, name, obj, host, port): self.ns.register(id, name, host, port) self.invoker.register(id, name, obj) def invoke(self, obj, method, args): result = self.requestor.invoke(obj, method, args) return result
def lint(self, save_to=None): """Lints all files in this commit. BUG: Lints the file from the cache, not the one from the index. TODO: Create lint class """ if save_to is not None: if isinstance(save_to, str): f = open(save_to, 'a+') f.close() cpp_lint_cmd = self._ri.get_path() + self.CPP_LINT_PATH cpp_files = self.get_cpp_files() if len(cpp_files) == 0: print('[SUCCESS] Nothing to lint!') return cmd = [cpp_lint_cmd] cmd.extend(cpp_files) print('Linting', len(cpp_files), ' cpp files...') invoker = Invoker(cmd) invoker.invoke() print(invoker.stdout) print('[SUCCESS] Lint complete!\n') if save_to is not None: if isinstance(save_to, file): save_to.write(invoker.stdout) else: f = open(save_to, "w") f.write(invoker.stdout)
def verify(self): """Raises exception if something isn't as expected.""" invoker = Invoker(self.VERIFY_CMD.format(self.commit_hash)) invoker.invoke() if invoker.status != 0: raise CommitException('Revision ' + self.commit_hash + ' does not exist!') return True
def amend(self): """This amends the current commit. WARNING: current commit should be the last one. """ f = tempfile.NamedTemporaryFile() f.write(self.message.get_raw_message().encode()) f.seek(0, os.SEEK_SET) cmd = self.AMEND_STR.format(f.name) invoker = Invoker(cmd) invoker.invoke() if invoker.status != 0: message = '{0}\n{1}\n{0}\n{2}\n{0}\n[FAIL] Amending failed!'.format( '-'*80, invoker.stdout, invoker.stderr) raise CommitException(message)
def invoke(self): self.prepare() invoker = Invoker(self.command) invoker.invoke() print(invoker.stdout) print(invoker.stderr) if invoker.status != 0: print("[WARNING] post-review failed!!!") self.reviewboard_id = -1 return # Get the ReviewBoard ID from the message pattern = re.compile('Review request #(?P<id>\d+) posted.') match = pattern.match(invoker.stdout) if match is None: print("[ABORT] post-review failed!!!") print("See ", file_name, " for details") sys.exit(1) self.reviewboard_id = int(match.group('id'))
def get_commit(self, commit_id): # Get the data from git log log_cmd = self.GIT_LOG_STR.format(commit_id) invoker = Invoker(log_cmd) invoker.invoke() if invoker.status != 0: raise RepositoryException('Something bad happened!\n' + 'Command = ' + str(invoker.cmd)) match = self.LOG_REGEX_C.match(invoker.stdout) if match is None: raise RepositoryException('Unknown log format!\n' + 'Command = ' + str(invoker.cmd)) commit_hash = match.group('hash') author = match.group('author') time = match.group('time') message = match.group('message') # Get the files from git diff files_cmd = self.GIT_FILES_STR.format(commit_id) invoker = Invoker(files_cmd) invoker.invoke() if invoker.status != 0: raise RepositoryException('Something bad happened!\n' + 'Command = ' + str(invoker.cmd)) files = [self.info.get_path() + '/' + x for x in invoker.stdout.split('\n')] return Commit(commit_hash, author, time, message, files)
def createServer(self, host, port): self.marshaller_server = Marshaller() self.invoker = Invoker(self.marshaller_server) self.s = ServerRequestHandler(host, port, self.invoker) self.invoker.addServer(self.s);
from invoker import Invoker if __name__ == "__main__": invoker = Invoker() invoker.invoke()
# The client asks for a command to be executed # The invoker takes the command, encapsulates it and places it in a queue, in case # there is something else to do first. # The command that is in charge of the requested command sends the result to the receiver. # The receiver knows how to perform the operations. # Example: Meal ordering at a restaurant. # The client is the customer. He sends his request to the receiver through the waiter. # who is the Invoker. # The waiter encapsulates the command (the order in this case) by writing it on the check # and then places it, creating the Command object. # The Receiver will be the cook that, after completing work on all the orders that were # sent to him before the command in question, starts working on it. from invoker import Invoker from simple_command import SimpleCommand from complex_command import ComplexCommand from receiver import Receiver if __name__ == "__main__": invoker = Invoker() invoker.set_on_start(SimpleCommand("Say Hi!")) receiver = Receiver() invoker.set_on_finish(ComplexCommand(receiver, "Send email", "Save report")) invoker.do_something_important()
from receiver import Receiver from command import Command, ConcreteCommand from invoker import Invoker if __name__ == "__main__": recv = Receiver() cmd = ConcreteCommand(recv) invoker = Invoker() invoker.command(cmd) invoker.execute()
from invoker import Invoker, InvokerGeneral from commands import LightOffCommand, LightOnCommand from light_bulb import LightBulb light_bulb = LightBulb light_invoker = Invoker(light_on_command=LightOnCommand(light_bulb), light_off_command=LightOffCommand(light_bulb)) light_invoker.on_button() light_invoker.off_button() # ------------------------- general_invoker = InvokerGeneral() general_invoker.set_command(LightOnCommand.CMD_NAME, LightOnCommand(light_bulb)) general_invoker.set_command(LightOffCommand.CMD_NAME, LightOffCommand(light_bulb)) general_invoker.execute(LightOnCommand.CMD_NAME) general_invoker.execute(LightOffCommand.CMD_NAME)
from configparser import ConfigParser from os.path import exists from command import getip # Get the config if not exists('config.ini'): raise RuntimeError( '`config.ini` file not found. Must exist next to `main.py`') config = ConfigParser() config.read('config.ini') # Invoker invoker = Invoker() # Parser setup parser = Parser() # Slack settings slack = SlackWrapper(config) # Command creation/adding parser.add_command(getip.GetIp(slack)) # Finally, start watching for new messages (pattern is a regex string for initial message filtering)
from command import FirstPrimesCommand, SortAndCombineCommand from invoker import Invoker if __name__ == '__main__': print('Please select:') print('1. First N prime numbers') print('2. Sort and combine') try: invoker = Invoker() user_input = int(input('>> ')) if user_input != 1 and user_input != 2: raise ValueError elif user_input == 1: n = int(input('N = ')) invoker.set_command(FirstPrimesCommand(n)) invoker.execute_command() else: first = input('First array (separated by comma without square brackets): ').split(',') second = input('Second array (separated by comma without square brackets): ').split(',') invoker.set_command(SortAndCombineCommand(first, second)) invoker.execute_command() except ValueError: print('Invalid input')