Exemplo n.º 1
0
	def onClickConectar(self, widget, data):		
		self.log("Conectando a " + data["ip"] + 
				" con el usuario " + data["usuario"] + 
				" ["+data["nombre"]+"]")

		t = Terminal(data)
		t.show_all()
Exemplo n.º 2
0
    def __init__(self, text, generatorFunc = None):
        Terminal.__init__(self, text)

        if generatorFunc is None:
            generatorFunc = lambda _: text

        self.generator = generatorFunc
Exemplo n.º 3
0
def render_log_frames(golog_path, rows, cols, limit=None):
    """
    Returns the frames of *golog_path* as a list of HTML-encoded strings that
    can be used with the playback_log.html template.  It accomplishes this task
    by running the frames through the terminal emulator and capturing the HTML
    output from the `Terminal.dump_html` method.

    If *limit* is given, only return that number of frames (e.g. for preview)
    """
    out_frames = []
    from terminal import Terminal
    term = Terminal(
        # 14/7 for the em_height should be OK for most browsers to ensure that
        # images don't always wind up at the bottom of the screen.
        rows=rows, cols=cols, em_dimensions={'height':14, 'width':7})
    for i, frame in enumerate(get_frames(golog_path)):
        if limit and i == limit:
            break
        if len(frame) > 14:
            if i == 0 and frame[14:15] == b'{':
                # This is just the metadata frame.  Skip it
                continue
            frame_time = int(float(frame[:13]))
            frame_screen = frame[14:] # Skips the colon
            # Emulate how a real shell would output newlines:
            frame_screen = frame_screen.replace(b'\n', b'\r\n')
            term.write(frame_screen)
            # Ensure we're not in the middle of capturing a file.  Otherwise
            # it might get cut off and result in no image being shown.
            if term.capture:
                continue
            scrollback, screen = term.dump_html()
            out_frames.append({'screen': screen, 'time': frame_time})
    del term # Ensures any file capture file descriptors are cleaned up
    return out_frames # Skip the first frame which is the metadata
Exemplo n.º 4
0
class Theatre(object):
   
  def __init__(self, here, manager_loc, core_store, store_lock, current_id, id_lock, current_creator, creator_lock, port_range, f = None, args = None, kwds = None, ps = None):
    log.debug(self, 'starting')
    self.__here = here
    port = ids.port_from_loc(here)
    self.__shared_data = Shared(port, current_id, id_lock, core_store, store_lock, current_creator, creator_lock, port_range)    
    self.__actor_store = LocalActorStore(self.__here, self.__shared_data)
    self.__messenger = Messenger(here, self.__actor_store)
    self.__migrator = Migrator(here, self.__actor_store, self.__shared_data)
    self.__manager_loc = manager_loc
    self.__port_range = port_range
    self.__term = None
    self.__processes = ps
 
    if (port == port_range[0]):
      self.__term = Terminal(self, here)
    self.valve = rpc.RPCValve(port, self.external_interface(), log)
    set_local_theatre(self.internal_interface())
    self.valve.listen()
    print('Theatre created on port ' + str(port))
    
    vis.add_host(here)
              
    if (port != port_range[0]):
      while True:
        try:
          time.sleep(5)
        except:
          pass

    else:
      self.f = f
      self.f(*args, **kwds)
      self.__term.shell()
   
  def external_interface(self):
     return TheatreExternalInterface(self.__messenger, self.__migrator, self.__term, self, self.__actor_store)
   
  def internal_interface(self):
    return TheatreInternalInterface(self.__here, self.__messenger, self.__actor_store, self.__migrator, self.__manager_loc, self.__shared_data)
  
  def shutdown(self):
    for port in self.__port_range:
      if (port != self.__port_range[0]):
        network_locator = rpc.RPCConnector("127.0.0.1:" + str(port))
        locator = network_locator.connect()
        locator.shutdown()
        locator.disconnect()
    self.valve.shutdown()
    time.sleep(2)
    for p in self.__processes:
      p.terminate()
    time.sleep(2)        
    sys.exit()
    
  def __str__(self):
    return "Theatre" 
Exemplo n.º 5
0
 def _style(self, obj):
     f, b = None, None
     if obj.color:
         f = Terminal.fcolor(obj.color, obj.bright)
     if obj.bg_color:
         b = Terminal.bcolor(obj.bg_color, obj.bg_bright)
     self.styles.append((f, b))
     obj.enter(self.selector)
     self.styles.pop()
Exemplo n.º 6
0
	def add_terminal (self):
		terminal = Terminal (self.config)
		terminal.connect ("key-release", self.__on_key_release)
		terminal.connect ("exited", self.__on_terminal_exited)

		number = self.notebook.append_page (terminal, None)
		self.tabs[number] = terminal
		self.notebook.set_current_page (number)

		return terminal
Exemplo n.º 7
0
    def enter_status_line(self, mode, enter):
        if not enter:
            status_line = ''.join(self.status_line)
            if len(status_line) > 0:
                self.process_status_line(mode, status_line)
        else:
            self.status_line = []
            self.status_line_mode = mode

        Terminal.enter_status_line(self, mode, enter)
Exemplo n.º 8
0
    def on_data(self, data):
        try:
            # self._data_lock.acquire()
            Terminal.on_data(self, data)
        except:
            LOGGER.exception('on data')
        finally:
            # self._data_lock.release()
            pass

        self.refresh_display()
Exemplo n.º 9
0
def _retrieve_log_flat(queue, settings):
    """
    Writes the given *log_filename* to *queue* in a flat format equivalent to::

        ./logviewer.py --flat log_filename

    *settings* - A dict containing the *log_filename*, *colors*, and *theme* to
    use when generating the HTML output.
    """
    out_dict = {
        'result': "",
        'log': "",
        'metadata': {},
    }
    # Local variables
    out = []
    spanstrip = re.compile(r'\s+\<\/span\>$')
    gateone_dir = settings['gateone_dir']
    user = settings['user']
    users_dir = settings['users_dir']
    container = settings['container']
    prefix = settings['prefix']
    log_filename = settings['log_filename']
    theme = "%s.css" % settings['theme']
    colors = "%s.css" % settings['colors']
    logs_dir = os.path.join(users_dir, "logs")
    log_path = os.path.join(logs_dir, log_filename)
    if os.path.exists(log_path):
        out_dict['metadata'] = get_or_update_metadata(log_path, user)
        out_dict['metadata']['filename'] = log_filename
        out_dict['result'] = "Success"
        import StringIO
        # Use the terminal emulator to create nice HTML-formatted output
        from terminal import Terminal
        term = Terminal(rows=100, cols=300)
        io_obj = StringIO.StringIO()
        flatten_log(log_path, io_obj)
        io_obj.seek(0)
        # Needed to emulate an actual term
        flattened_log = io_obj.read().replace('\n', '\r\n')
        term.write(flattened_log)
        scrollback, screen = term.dump_html()
        # Join them together
        log_lines = scrollback + screen
        # rstrip the lines
        log_lines = [a.rstrip() for a in log_lines]
        # Fix things like "<span>whatever [lots of whitespace]    </span>"
        for i, line in enumerate(log_lines):
            out.append(spanstrip.sub("</span>", line))
        out_dict['log'] = out
    else:
        out_dict['result'] = _("ERROR: Log not found")
    message = {'logging_log_flat': out_dict}
    queue.put(message)
Exemplo n.º 10
0
def _retrieve_log_flat(queue, settings):
    """
    Writes the given *log_filename* to *queue* in a flat format equivalent to::

        ./logviewer.py --flat log_filename

    *settings* - A dict containing the *log_filename*, *colors_css*, and
    *theme_css* to use when generating the HTML output.
    """
    out_dict = {
        'result': "",
        'log': "",
        'metadata': {},
    }
    # Local variables
    out = []
    spanstrip = re.compile(r'\s+\<\/span\>$')
    user = settings['user']
    users_dir = settings['users_dir']
    log_filename = settings['log_filename']
    logs_dir = os.path.join(users_dir, "logs")
    log_path = os.path.join(logs_dir, log_filename)
    if os.path.exists(log_path):
        out_dict['metadata'] = get_or_update_metadata(log_path, user)
        out_dict['metadata']['filename'] = log_filename
        out_dict['result'] = "Success"
        from io import BytesIO
        # Use the terminal emulator to create nice HTML-formatted output
        from terminal import Terminal
        term = Terminal(rows=100, cols=300, em_dimensions=0)
        io_obj = BytesIO()
        flatten_log(log_path, io_obj)
        io_obj.seek(0)
        # Needed to emulate an actual term
        flattened_log = io_obj.read().replace(b'\n', b'\r\n')
        # NOTE: Using chunking below to emulate how a stream might actually be
        # written to the terminal emulator.  This is to prevent the emulator
        # from thinking that any embedded files (like PDFs) are never going to
        # end.
        def chunker(s, n):
            """Produce `n`-character chunks from `s`."""
            for start in range(0, len(s), n):
                yield s[start:start+n]
        for chunk in chunker(flattened_log, 499):
            term.write(chunk)
        scrollback, screen = term.dump_html()
        # Join them together
        log_lines = scrollback + screen
        # rstrip the lines
        log_lines = [a.rstrip() for a in log_lines]
        # Fix things like "<span>whatever [lots of whitespace]    </span>"
        for i, line in enumerate(log_lines):
            out.append(spanstrip.sub("</span>", line))
        out_dict['log'] = out
        term.clear_screen() # Ensure the function below works...
        term.close_captured_fds() # Force clean up open file descriptors
    else:
        out_dict['result'] = _("ERROR: Log not found")
    message = {'terminal:logging_log_flat': out_dict}
    queue.put(message)
Exemplo n.º 11
0
    def _styleoverride(self, obj):
        height, width, x_off, y_off = self.box_stack[-1]
        if obj.absolute:
            x_off = obj.pos_x
            y_off = obj.pos_y
        x, y = x_off + obj.margin_left, y_off + obj.margin_top

        val, fg, bg, z_index = self.screen.get(x, y)
        if obj.color:
            fg = Terminal.fcolor(obj.color, obj.bright)
        if obj.bg_color:
            bg = Terminal.bcolor(obj.bg_color, obj.bg_bright)

        self.screen.set(x, y, fg=fg, bg=bg, z_index=z_index+10)
Exemplo n.º 12
0
    def has_directories(self, directories, auto_create=True):
        try:
            self.ssh_client.connect(self.address, username=self.user)

            for directory in directories:
                if not self._has_file(directory):
                    if auto_create:
                        Terminal.print_warn('Missing directory "%s", attempting to create.' % directory)
                        if not self._create_directory(directory):
                            raise ServerError('Could not create directory "%s"' % directory)
                    else:
                        raise ServerError('Missing directory "%s".' % directory)

        except IOError, e:
            raise ServerError('An error occurred with the ssh connection.\n\t> %s' % e, base=e)
Exemplo n.º 13
0
Arquivo: urd.py Projeto: nycz/urd
    def __init__(self, file_to_open=""):
        super().__init__()
        self.setWindowTitle("New file")

        self.force_quit_flag = False

        self.config = read_config()
        self.setStyleSheet("background: " + self.config["theme"]["background"])

        layout = QtGui.QVBoxLayout(self)
        common.kill_theming(layout)

        scene_container = QtGui.QScrollArea(self)
        layout.addWidget(scene_container, stretch=1)

        self.scene = Scene(
            self.config, scene_container.horizontalScrollBar().value, scene_container.verticalScrollBar().value
        )

        scene_container.setWidget(self.scene)

        self.terminal = Terminal(self, lambda: self.scene.file_path)
        layout.addWidget(self.terminal)

        self.connect_signals(self.scene, self.terminal)

        common.set_hotkey("Escape", self, self.terminal.toggle)

        if file_to_open:
            self.scene.open_file(file_to_open)

        self.show()
Exemplo n.º 14
0
  def __init__(self, here, manager_loc, core_store, store_lock, current_id, id_lock, current_creator, creator_lock, port_range, f = None, args = None, kwds = None, ps = None):
    log.debug(self, 'starting')
    self.__here = here
    port = ids.port_from_loc(here)
    self.__shared_data = Shared(port, current_id, id_lock, core_store, store_lock, current_creator, creator_lock, port_range)    
    self.__actor_store = LocalActorStore(self.__here, self.__shared_data)
    self.__messenger = Messenger(here, self.__actor_store)
    self.__migrator = Migrator(here, self.__actor_store, self.__shared_data)
    self.__manager_loc = manager_loc
    self.__port_range = port_range
    self.__term = None
    self.__processes = ps
 
    if (port == port_range[0]):
      self.__term = Terminal(self, here)
    self.valve = rpc.RPCValve(port, self.external_interface(), log)
    set_local_theatre(self.internal_interface())
    self.valve.listen()
    print('Theatre created on port ' + str(port))
    
    vis.add_host(here)
              
    if (port != port_range[0]):
      while True:
        try:
          time.sleep(5)
        except:
          pass

    else:
      self.f = f
      self.f(*args, **kwds)
      self.__term.shell()
Exemplo n.º 15
0
    def cleanup(self):
        if self.oldattrs is not None:
            termios.tcsetattr(sys.stdin, termios.TCSANOW, self.oldattrs)
        sys.stdout.write(Terminal.cursor_show())
        sys.stdout.flush()
        self.disable_alternate()

        fl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
        fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, fl & ~os.O_NONBLOCK)
Exemplo n.º 16
0
    def get_supervisor_config(self, project, auto_create=True):
        try:
            self.ssh_client.connect(self.address, username=self.user)

            ubuntu_config_dir = '/etc/supervisor/conf.d'
            rehl_config_dir = '/etc/supervisord.d'
            config_path = ''

            if self._has_file(ubuntu_config_dir):
                config_path = "%s/%s.conf" % (ubuntu_config_dir, project.lower())
            elif self._has_file(rehl_config_dir):
                config_path = '%s/%s.conf' % (rehl_config_dir, project.lower())
            else:
                raise ServerError('Could not find supervisor include dir')

            if not self._has_file(config_path):
                if auto_create:
                    Terminal.print_warn(
                        'Missing supervisor config for %s in "%s", attempting to create.' % (project, config_path))

                    if self.user != 'root' and self.password is None:
                        self._prompt_superuser_pwd(
                            'Missing file "%s", Please enter password to proceed to creation.' % config_path)

                    out, err = self._execute_sudo_cmd('touch %s' % config_path)

                    if err != '':
                        self.password = None
                        raise ServerError('Could not create supervisor config in "%s" : %s' % (config_path, err))

                    return ''
                else:
                    raise ServerError('Missing supervisor config for %s in "%s".' % (project, config_path))

            stdin, stdout, stderr = self.ssh_client.exec_command('cat %s' % config_path)

            out, err = stdout.read().rstrip(), stderr.read().rstrip()

            if err != '':
                raise ServerError('Could not read supervisor config "%s"' % config_path)

            return out
        except IOError, e:
            raise ServerError('An error occurred with the ssh connection.\n\t> %s' % e, base=e)
Exemplo n.º 17
0
def main():
	try:
		os.environ["TERM"] = "xterm-256color"

		screen = init_curses()
		terminal = Terminal(screen)

		timer = Stopwatch()

		#Application Loop
		while True:
			#Determine if user input should be used this cycle
			if timer.get_elapsed() >= PERIOD:
				terminal.use_key = True
				timer.reset()
			#Generate and display the terminal frame
			terminal.application_frame()

	finally:
		exit_curses(screen)
Exemplo n.º 18
0
    def has_git_repositories(self, bare_repo_directory, src_repo_directory, auto_create=True):
        try:
            self.ssh_client.connect(self.address, username=self.user)

            if not self._is_repo(bare_repo_directory, bare=True):
                if auto_create:
                    Terminal.print_warn('No bare git repository in "%s", attempting to create.' % bare_repo_directory)
                    if not self._init_repo(bare_repo_directory, bare=True):
                        raise ServerError('Could not create bare git repository in "%s"' % bare_repo_directory)
                else:
                    raise ServerError('Missing bare git repository in "%s".' % bare_repo_directory)

            if not self._is_repo(src_repo_directory, bare=False):
                if auto_create:
                    Terminal.print_warn('No src git repository in "%s", attempting to create.' % src_repo_directory)
                    if not self._clone_repo(bare_repo_directory, src_repo_directory):
                        raise ServerError('Could not create src git repository in "%s"' % src_repo_directory)
                else:
                    raise ServerError('Missing src git repository in "%s".' % src_repo_directory)
        except IOError, e:
            raise ServerError('An error occurred with the ssh connection.\n\t> %s' % e, base=e)
Exemplo n.º 19
0
 def __init__(self, user_config, reactor=reactor):
     super(UI, self).__init__()
     self.user_config = user_config
     self.reactor = reactor
     self.player = Player(initial_volume=user_config.persistent.volume)
     self.progress_bar = ProgressBar(
         self.user_config.appearance.progress_bar)
     self.time_check = TimeCheck()
     self.status_bar = HorizontalContainer(
         (self.progress_bar, self.time_check))
     self.terminal = Terminal()
     self.key_bindings = self._create_key_bindings()
Exemplo n.º 20
0
    def __init__(self, cfg):
        Terminal.__init__(self, cfg)

        self.term_widget = None
        self.session = None

        self.col = 0
        self.row = 0

        self.remain_buffer = []

        self.cur_line_option = get_default_text_attribute()
        self.saved_screen_buffer, self.saved_cursor, \
            self.saved_cur_line_option = \
            ScreenBuffer(), (0, 0), get_default_text_attribute()

        self.status_line = []
        self.status_line_mode = 0

        self.charset_modes_translate = [None, None]
        self.charset_mode = 0

        self._saved_charset_modes_translate = [None, None]
        self._saved_charset_mode = 0

        self._data_lock = threading.RLock()
        self._screen_buffer = ScreenBuffer()

        self._dec_mode = False
        self._force_column = False
        self._force_column_count = 80

        self._origin_mode = False
        self._saved_origin_mode = False

        self._tab_stops = {}
        self._set_default_tab_stops()

        self._cursor_visible = True
Exemplo n.º 21
0
    def __init__(self, workspace_, resubmit=False, terminal=None):
        DAServer.__init__(self, "dispatch")

        self._workspace = workspace_
        self._webDir = ""
        self._jobInfo = {}
        self._resubmit = resubmit
        self._readyJobs = dict([(k, []) for k in DADispatcher.CLUSTERS])
        self._activeJobs = dict([(k, []) for k in DADispatcher.CLUSTERS])
        self._lock = threading.Lock()
        self._stateChanged = threading.Event()
        if terminal:
            self._terminal = terminal
        else:
            self._terminal = Terminal(TERMNODE, verbose=True)

        self.options = dict([(k, "") for k in DADispatcher.CLUSTERS])
Exemplo n.º 22
0
Arquivo: urd.py Projeto: cefyr/urd
    def __init__(self, file_to_open=''):
        super().__init__()
        self.setWindowTitle('New file')

        self.force_quit_flag = False

        self.config = read_config()
        self.setStyleSheet('background: '+self.config['theme']['background'])

        layout = QtGui.QVBoxLayout(self)
        common.kill_theming(layout)

        self.scene_container = QtGui.QScrollArea(self)
        layout.addWidget(self.scene_container, stretch=1)

        self.scene = Scene(self.config,
                self.scene_container.horizontalScrollBar().value,
                self.scene_container.verticalScrollBar().value)

        self.scene_container.setWidget(self.scene)
        self.scene_container.setDisabled(True)

        self.terminal = Terminal(self, lambda: self.scene.file_path)
        layout.addWidget(self.terminal)

        self.connect_signals(self.scene, self.terminal)

#        common.set_hotkey('Escape', self, self.terminal.toggle)
        common.set_hotkey('Ctrl+N', self, self.scene.request_new_file)
        common.set_hotkey('Ctrl+O', self, lambda:self.terminal.prompt('o '))
        common.set_hotkey('Ctrl+S', self, self.scene.request_save_file)
        common.set_hotkey('Ctrl+Shift+S', self,
                          lambda:self.terminal.prompt('s '))

        if file_to_open:
            self.scene.open_file(file_to_open)

        self.show()
Exemplo n.º 23
0
class UI(PyampBase):
    def __init__(self, user_config, reactor=reactor):
        super(UI, self).__init__()
        self.user_config = user_config
        self.reactor = reactor
        self.player = Player(initial_volume=user_config.persistent.volume)
        self.progress_bar = ProgressBar(
            self.user_config.appearance.progress_bar)
        self.time_check = TimeCheck()
        self.status_bar = HorizontalContainer(
            (self.progress_bar, self.time_check))
        self.terminal = Terminal()
        self.key_bindings = self._create_key_bindings()

    def _create_bindable_funcs_map(self):
        bindable_funcs = {}
        for obj in self, self.player:
            for name in dir(obj):
                func = getattr(obj, name)
                if is_bindable(func):
                    bindable_funcs[name] = func
        return bindable_funcs

    def _create_key_bindings(self):
        bindable_funcs = self._create_bindable_funcs_map()
        key_bindings = {}
        for func_name, keys in self.user_config.key_bindings:
            if isinstance(keys, basestring):
                keys = [keys]
            for key in keys:
                key = ' '.join(key.split())
                if func_name in bindable_funcs:
                    key_bindings[key] = bindable_funcs[func_name]
                else:
                    self.log.warning(
                        'Warning: {} is not a bindable pyamp function'.format(
                            func_name))
        return key_bindings

    def update(self):
        try:
            self.player.update()
        except StopIteration:
            self.quit()
        self.draw()

    def draw(self):
        #print self.terminal.clear()
        if self.player.playing:
            position = (self.player.get_position() or 0) / gst.SECOND
            duration = (self.player.get_duration() or 0) / gst.SECOND
            if duration:
                self.progress_bar.fraction = position / duration
            self.time_check.position = position
            self.time_check.duration = duration
        total_width = self.terminal.width - 2
        with self.terminal.location(0, self.terminal.height - 2):
            print self.player.tags['title'].center(self.terminal.width)
            print self.status_bar.draw(total_width, 1).center(
                self.terminal.width),
        sys.stdout.flush()

    def _handle_sigint(self, signal, frame):
        self.quit()

    def handle_input(self, char):
        action = self.key_bindings.get(char, lambda: None)
        action()

    @bindable
    @gst_log_calls
    def quit(self):
        def clean_up():
            self.player.stop()
            self.reactor.stop()
        if self.player.playing:
            fade_out_time = 1
            self.player.fade_out(fade_out_time)
            reactor.callLater(fade_out_time + 0.1, clean_up)
        else:
            clean_up()

    def run(self):
        with self.terminal.fullscreen():
            with self.terminal.hidden_cursor():
                with self.terminal.unbuffered_input():
                    signal.signal(signal.SIGINT, self._handle_sigint)
                    signal.signal(signal.SIGTSTP, self.terminal.handle_sigtstp)
                    self.looping_call = task.LoopingCall(self.update)
                    self.looping_call.start(1 / 20)
                    stdio.StandardIO(InputReader(self))
                    self.reactor.run()
Exemplo n.º 24
0
class Program:
    def __init__(self):
        self.settings = Settings()
        self.adapter = Adapter(self)
        self.project = Project(self)
        self.gui = Gui(self)
        self.file_manager = FileManager(self)
        self.terminal = Terminal(self)

        self.function_queue = queue.Queue()
        self.work_thread = threading.Thread(target=self.execute_functions)
        self.work_thread.daemon = True
        self.work_thread.start()
        self.measuring = False
        self.gui_thread_is_free = True

    def execute_functions(self):
        while True:
            try:
                function = "self." + self.function_queue.get()
                exec(function)
            except queue.Empty:
                time.sleep(0.1)

    def queue_function(self, function):
        self.function_queue.put(function)

    # should be executed by self.work_thread only
    def connect(self, address):
        if self.adapter.connect(address):
            self.gui.gpib.update_button_connected()
            self.gui.info.change_connect_label()
        else:
            tk.messagebox.showerror(
                title="Connection error",
                message="Can't connect to the device on that address.")

    # should be executed by self.work_thread only
    def disconnect(self):
        if self.terminal.is_open():
            self.close_terminal()
        self.adapter.disconnect()
        self.gui.gpib.update_button_disconnected()
        self.gui.info.change_connect_label()

    # should be executed by self.work_thread only
    def save_state(self):
        state = self.adapter.get_state()
        if state is None:
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()
        elif not state:
            pass
        else:
            self.project.set_state(state)
            self.gui.info.change_state_label()

    # should be executed by self.work_thread only
    def recall_state(self):
        return_code = self.adapter.set_state(self.project.get_state())
        if return_code is None:
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()
        elif not return_code:
            pass
        else:
            tk.messagebox.showinfo(
                title="Recalled State",
                message="State sent to the device successfully!")

    # should be executed by self.work_thread only
    def preset(self):
        return_code = self.adapter.preset()
        if return_code is None:
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()
        elif not return_code:
            pass
        else:
            tk.messagebox.showinfo(title="PRESET",
                                   message="Preset successful!")

    def save_calib(self):
        calib = self.adapter.get_calibration()
        if calib is None:
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()
        elif type(calib) == str and not calib:
            tk.messagebox.showwarning(title="Empty calibration",
                                      message="The device is not calibrated.")
        elif not calib:
            pass
        else:
            self.project.set_calibration(calib)
            self.gui.info.change_calibration_label()

    def load_calib(self):
        return_code = self.adapter.set_calibration(
            self.project.get_calibration())
        if return_code is None:
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()
        elif not return_code:
            pass
        else:
            tk.messagebox.showinfo(
                title="Calibration Loaded",
                message="Calibration sent to the device successfully!")

    # should be executed by self.work_thread only
    def prepare_measurement(self):
        parameters = self.settings.get_parameters()
        parameters = parameters.upper().split()
        s11 = s12 = s21 = s22 = False
        if "S11" in parameters:
            s11 = True
        if "S12" in parameters:
            s12 = True
        if "S21" in parameters:
            s21 = True
        if "S22" in parameters:
            s22 = True
        self.project.reset_data(s11, s12, s21, s22)

    # should be executed by self.work_thread only
    def measure(self, autosave=False):
        self.prepare_measurement()
        if self.project.data is None:  # ak neboli vybrate ziadne S-parametre, vymazu sa data z pameti
            self.gui.sweep.change_run()
            self.gui.window.after_idle(self.gui.sweep.refresh_frame)
            self.gui.sweep.enable_measurement_checkboxes()
            self.gui.sweep.run_button["state"] = tk.NORMAL
            return

        data = self.adapter.measure()
        self.gui.window.after_idle(self.gui.sweep.refresh_points)

        if data == "":
            tk.messagebox.showwarning(
                title="No Data",
                message="No data received.\n"
                "The device may be in Remote mode, try reconnecting.")
            self.gui.sweep.enable_measurement_checkboxes()
            self.gui.sweep.run_button["state"] = tk.NORMAL
            return
        if not data:
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()
            self.project.data = None
            self.gui.sweep.enable_measurement_checkboxes()
            self.gui.sweep.run_button["state"] = tk.NORMAL
            return

        data = data.strip()
        self.project.data.add_measurement(data)
        self.gui.window.after_idle(self.gui.sweep.refresh_frame)
        if autosave:
            self.file_manager.save_last_measurement()
        self.gui.sweep.run_button["state"] = tk.NORMAL
        self.gui.sweep.enable_measurement_checkboxes()

    # should be executed by self.work_thread only
    def start_measurement(self, autosave=False):
        self.prepare_measurement()
        if self.project.data is None:  # ak neboli vybrate ziadne S-parametre
            self.gui.sweep.reset_frame()
            self.gui.sweep.change_run()
            self.gui.window.after_idle(self.gui.sweep.refresh_frame)
            self.gui.sweep.enable_measurement_checkboxes()
            return

        return_code = self.adapter.start_measurement()
        self.gui.window.after_idle(self.gui.sweep.refresh_points)
        if return_code is None:
            self.gui.sweep.change_run()
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()
            self.gui.sweep.enable_measurement_checkboxes()
            self.project.data = None
            return
        if not return_code:  # ak nebol konektnuty
            self.gui.sweep.change_run()
            self.project.data = None
            self.gui.sweep.enable_measurement_checkboxes()
            return

        self.measuring = True
        waited = False
        while True:
            data = self.adapter.retrieve_measurement_data()
            if data is None or data == "":
                if waited:
                    break
                if not self.measuring and not waited:  # malo by byt thread-safe
                    waited = True
                    continue
                if data == "":
                    tk.messagebox.showwarning(title="No Data",
                                              message="No data received.\n"
                                              "The device may be in "
                                              "Remote mode, try reconnecting.")
                    self.gui.sweep.change_run()
                    self.gui.sweep.enable_measurement_checkboxes()
                    return
                self.measuring = False
                self.gui.gpib.update_button_disconnected()
                self.gui.info.change_connect_label()
                self.gui.sweep.change_run()
                self.gui.sweep.enable_measurement_checkboxes()
                return

            data = data.strip()
            self.project.data.add_measurement(data)
            if self.gui_thread_is_free:  # aby gui reagoval na klikanie pocas merania
                self.gui.window.after(1, self.toggle_gui_free)
                self.gui.window.after(1, self.gui_graph_refresh)
            if autosave:
                self.file_manager.save_last_measurement()
            if waited:
                break
            if not self.measuring:  # malo by byt thread-safe
                waited = True
        self.gui.window.after(1, self.gui.sweep.refresh_frame)
        self.gui.sweep.change_run()
        self.gui.sweep.enable_measurement_checkboxes()

        # executed by GUI thread
    def toggle_gui_free(self):
        self.gui_thread_is_free = not self.gui_thread_is_free

        # executed by GUI thread
    def gui_graph_refresh(self):
        self.gui.sweep.refresh_frame()
        self.gui.window.after(200, self.toggle_gui_free)

    # executed by GUI thread
    def end_measurement(self):
        def finish():
            self.adapter.end_measurement()
            time.sleep(
                0.2
            )  # chcem pockat kym HPCTRL vyprintuje meranie do std_out ak uz zacal
            self.measuring = False

        if not self.measuring:
            return
        finish_thread = threading.Thread(target=finish)
        finish_thread.daemon = True
        finish_thread.start()

    def get_data_for_graph(self, measurement_index, s_param):
        if self.project.data is None:
            return None
        measurement = self.project.data.get_measurement(
            s_param, measurement_index)
        if measurement is None:
            return None
        freq = measurement.keys()
        value1 = []
        value2 = []
        for v1, v2 in measurement.values():
            value1.append(v1)
            value2.append(v2)
        return freq, value1, value2

    def open_terminal(self):
        return_code = self.adapter.enter_cmd_mode()
        if return_code is None:
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()
        elif not return_code:
            pass
        else:
            self.terminal.open_new_window()

    def close_terminal(self):
        return_code = self.adapter.exit_cmd_mode()
        if return_code is None:
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()

    def terminal_send(self, message):
        return_code = self.adapter.cmd_send(message)
        if return_code is None:
            self.gui.gpib.update_button_disconnected()
            self.gui.info.change_connect_label()
        elif not return_code:
            pass
        else:
            if type(return_code) != bool:
                self.terminal.print_message(return_code)

    # should be executed by self.work_thread only
    def adjust_calibration(self, port1, port2, vel_fact):
        self.settings.set_port1(float(port1))
        self.settings.set_port2(float(port2))
        self.settings.set_vel_factor(float(vel_fact))

        functions = [
            self.adapter.set_port1_length, self.adapter.set_port2_length,
            self.adapter.set_velocity_factor
        ]
        for i in range(len(functions)):
            return_code = functions[i]()
            if return_code is None:
                self.gui.gpib.update_button_disconnected()
                self.gui.info.change_connect_label()

    def quit_program(self):
        self.gui.window.quit()
        self.gui.window.destroy()
        self.adapter.kill_hpctrl()
Exemplo n.º 25
0
    if not resubmit and (len(args) < 2 or not options.workspace):
        parser.print_usage()
        sys.exit(1)

    ### CREATE SERVER ###

    taskID = string.join(random.sample(string.ascii_lowercase, 4), "")

    tcpServer = DelegatingTCPServer()

    ### OPEN TERMINAL ###

    if DEBUG:
        print "opening terminal"

    terminal = Terminal(options.terminal, verbose=True)

    ### CONFIGURATION ###

    if resubmit:
        workspace = os.path.realpath(args[0])
        if not os.path.exists(workspace) or not os.path.exists(workspace + "/jobconfig.py"):
            raise RuntimeError("{0} is not a valid workspace".format(workspace))

        sys.path.append(workspace)
        import jobconfig

        jobConfig = copy.copy(jobconfig.jobConfig)

        if not options.recover:
            ilog = 1
Exemplo n.º 26
0
from node import Node
from terminal import Terminal
from relay import Relay
from radio import Radio
from event import Event
import copy
import cfg

#initialize model
radio = Radio()
t1 = Terminal("t1", 100, 40, "00:00:00:00:00:01", "192.168.0.1", radio, 1, 2)
radio.add_node(t1)
t2 = Terminal("t2", 100, -40, "00:00:00:00:00:02", "192.168.0.2", radio, 1, 1)
radio.add_node(t2)
t3 = Terminal("t3", -100, 40, "00:00:00:00:00:03", "192.168.0.3", radio, 1, 1)
radio.add_node(t3)
t4 = Terminal("t4", -100, -40, "00:00:00:00:00:04", "192.168.0.4", radio, 1, 1)
radio.add_node(t4)

r1 = Relay("r1", 40, 0, "00:00:00:00:10:01", "192.168.10.1", radio, 1, 2)
radio.add_node(r1)
r1.add_entry_to_routing_table("192.168.0.1", "00:00:00:00:00:01")
r1.add_entry_to_routing_table("192.168.0.2", "00:00:00:00:00:02")
r1.add_entry_to_routing_table("192.168.0.3", "00:00:00:00:10:02")
r1.add_entry_to_routing_table("192.168.0.4", "00:00:00:00:10:02")

r2 = Relay("r2", -40, 0, "00:00:00:00:10:02", "192.168.10.2", radio, 1, 2)
radio.add_node(r2)
r2.add_entry_to_routing_table("192.168.0.1", "00:00:00:00:10:01")
r2.add_entry_to_routing_table("192.168.0.2", "00:00:00:00:10:02")
r2.add_entry_to_routing_table("192.168.0.3", "00:00:00:00:00:03")
Exemplo n.º 27
0
def flatten_log(log_path, file_like, preserve_renditions=True, show_esc=False):
    """
    Given a log file at *log_path*, write a string of log lines contained
    within to *file_like*.  Where *file_like* is expected to be any file-like
    object with write() and flush() methods.

    If *preserve_renditions* is True, CSI escape sequences for renditions will
    be preserved as-is (e.g. font color, background, etc).  This is to make the
    output appear as close to how it was originally displayed as possible.
    Besides that, it looks really nice =)

    If *show_esc* is True, escape sequences and control characters will be
    visible in the output.  Trailing whitespace and escape sequences will not be
    removed.

    ..note::

        Converts our standard recording-based log format into something that
        can be used with grep and similar search/filter tools.
    """
    from terminal import Terminal, SPECIAL
    metadata = get_log_metadata(log_path)
    rows = metadata.get('rows', 24)
    cols = metadata.get('columns', None)
    if not cols:
        # Try the old metadata format which used 'cols':
        cols = metadata.get('cols', 80)
    term = Terminal(rows=rows, cols=cols, em_dimensions=0)
    out_line = u""
    cr = False
    # We skip the first frame, [1:] because it holds the recording metadata
    for count, frame in enumerate(get_frames(log_path)):
        if count == 0:
            # Skip the first frame (it's just JSON-encoded metadata)
            continue
        # First 13 chars is the timestamp:
        frame_time = float(frame.decode('UTF-8', 'ignore')[:13])
        # Convert to datetime object
        frame_time = datetime.fromtimestamp(frame_time / 1000)
        if show_esc:
            frame_time = frame_time.strftime(u'\x1b[0m%b %d %H:%M:%S')
        else:  # Renditions preserved == I want pretty.  Make the date bold:
            frame_time = frame_time.strftime(u'\x1b[0;1m%b %d %H:%M:%S\x1b[m')
        if not show_esc:
            term.write(frame[14:])
        if term.capture:
            # Capturing a file...  Keep feeding it frames until complete
            continue
        elif term.captured_files:
            for line in term.screen:
                # Find all the characters that come before/after the capture
                for char in line:
                    if ord(char) >= SPECIAL:
                        adjusted = escape_escape_seq(out_line, rstrip=True)
                        adjusted = frame_time + u' %s\n' % adjusted
                        file_like.write(adjusted.encode('utf-8'))
                        out_line = u""
                        if char in term.captured_files:
                            captured_file = term.captured_files[char].file_obj
                            captured_file.seek(0)
                            file_like.write(captured_file.read())
                            file_like.write(b'\n')
                            del captured_file
                            term.clear_screen()
                            term.close_captured_fds()  # Instant cleanup
                    else:
                        out_line += char
            if not out_line:
                continue
            adjusted = frame_time + u' %s\n' % out_line.strip()
            file_like.write(adjusted.encode('utf-8'))
            out_line = u""
            continue
        else:
            term.clear_screen()
        frame = frame.decode('UTF-8', 'ignore')
        for char in frame[14:]:
            if '\x1b[H\x1b[2J' in out_line:  # Clear screen sequence
                # Handle the clear screen (usually ctrl-l) by outputting
                # a new log entry line to avoid confusion regarding what
                # happened at this time.
                out_line += u"^L"  # Clear screen is a ctrl-l or equivalent
                if show_esc:
                    adjusted = raw(out_line)
                else:
                    adjusted = escape_escape_seq(out_line, rstrip=True)
                adjusted = frame_time + u' %s\n' % adjusted
                file_like.write(adjusted.encode('utf-8'))
                out_line = u""
                continue
            if char == u'\n':
                if show_esc:
                    adjusted = raw(out_line)
                else:
                    adjusted = escape_escape_seq(out_line, rstrip=True)
                if not adjusted:
                    out_line = u""  # Skip empty lines
                    continue
                adjusted = frame_time + u' %s\n' % adjusted
                file_like.write(adjusted.encode('utf-8'))
                out_line = u""
                cr = False
            elif char == u'\r':
                # Carriage returns need special handling.  Make a note of it
                cr = True
            else:
                # \r without \n means that characters were (likely)
                # overwritten.  This usually happens when the user gets to
                # the end of the line (which would create a newline in the
                # terminal but not necessarily the log), erases their
                # current line (e.g. ctrl-u), or an escape sequence modified
                # the line in-place.  To clearly indicate what happened we
                # insert a '^M' and start a new line so as to avoid
                # confusion over these events.
                if cr:
                    out_line += "^M"
                    file_like.write((frame_time + u' ').encode('utf-8'))
                    if show_esc:
                        adjusted = raw(out_line)
                    else:
                        adjusted = escape_escape_seq(out_line, rstrip=True)
                    file_like.write((adjusted + u'\n').encode('utf-8'))
                    out_line = u""
                out_line += char
                cr = False
        file_like.flush()
    del term
Exemplo n.º 28
0
class TestTerminal(unittest.TestCase):

    def setUp(self):
        self.terminal = Terminal()
        self.flight = Flight()
        self.flight2 = Flight(start_time=Date(day=29,
                              month=11, year=2016, hour='17:30'),
                              from_dest="Vancouver", to_dest="New York",
                              end_time=Date(day=29, month=11,
                              year=2016, hour='20:40'))
        self.terminal.add_flight(self.flight)
        self.terminal.add_flight(self.flight2)

    def test_init(self):
        self.assertEqual(self.terminal.number, 1)
        self.assertEqual(self.terminal.max_flights, 20)

    def test_add_flight(self):
        flight = Flight()
        self.assertEqual(self.terminal.add_flight(flight),
                         [self.flight, self.flight2, flight])

    def test_str(self):
        self.assertEqual(str(self.terminal), "Terminal number 1")

    def test_get_terminal_flights_on(self):
        date = Date()
        self.assertEqual(self.terminal.get_terminal_flights_on(date),
                         self.terminal.flights)

    def test_get_terminal_flights(self):
        self.assertEqual(self.terminal.get_terminal_flights(),
                         self.terminal.flights)

    def test_terminal_flights_to_dest(self):
        destination = "London"
        self.assertEqual(self.terminal.terminal_flights_to_dest(destination),
                         [self.flight])

    def test_get_flights_for(self):  # passed
        date = Date()
        self.assertEqual(self.terminal.get_flights_for(date), 2)

    def test_get_flights_before(self):
        date = Date()
        hour = '16:30'
        self.assertEqual(self.terminal.get_flights_before(date, hour),
                         [self.flight])

    def test_get_flight_from(self):
        destination = self.flight.from_dest
        self.assertEqual(self.terminal.get_flight_from(destination),
                         [self.flight])

    def test_get_flight_to(self):
        destination = self.flight.to_dest
        self.assertEqual(self.terminal.get_flight_to(destination),
                         [self.flight])

    def test_get_flight_to_hour(self):
        destination = self.flight.to_dest
        date = Date()
        hour = '12:31'
        self.assertEqual(self.terminal.
                         get_flight_to_hour(destination, date, hour),
                         [self.flight])

    def test_get_flight_from_hour(self):
        destination = self.flight.from_dest
        date = Date()
        hour = '12:31'
        self.assertEqual(self.terminal.
                         get_flight_from_hour(destination, date, hour),
                         [self.flight])

    def test_flights_on_date_lt_hours(self):
        hours = 3.0
        date = Date()
        self.assertEqual(self.terminal.flights_on_date_lt_hours(date, hours),
                         [self.flight])

    def test_flights_within_duration(self):
        start_time = Date(day=29, month=11, year=2016, hour='17:30')
        end_time = Date(day=29, month=11, year=2016, hour='20:40')
        self.assertEqual(self.terminal.flights_within_duration(start_time,
                                                               end_time),
                         [self.flight2])

    def test_passengers_to_dest(self):
        destination = self.flight.to_dest
        passenger = Passenger(first_name="Georgi", second_name="Atanasov",
                              age=20, flight=self.flight)
        passenger2 = Passenger(flight=self.flight2)
        self.flight.add_passenger(passenger)
        self.flight2.add_passenger(passenger2)
        self.assertEqual(self.terminal.passengers_to_dest(destination),
                         [passenger])

    def test_flights_with_passengers_gt(self):
        size = 0
        passenger = Passenger(first_name="Georgi", second_name="Atanasov",
                              age=20, flight=self.flight)
        passenger2 = Passenger(flight=self.flight2)
        self.flight.add_passenger(passenger)
        self.flight2.add_passenger(passenger2)
        self.assertEqual(self.terminal.flights_with_passengers_gt(size),
                         [self.flight, self.flight2])

    def test_reservations_to_destination(destination):
        destination = "London"
        pass
# This script tests the Terminal class on computers which are running
# a Windows operating system

from terminal import Terminal

testTerminal = Terminal("test-terminal", "cmd.exe")
print("The current directory is " + testTerminal.currentDirectory)
terminalOutput = testTerminal.executeCommand("dir")
print(
    "The command '" + terminalOutput['executedCommand'] 
    + "' was executed in terminal '" + terminalOutput['terminalName']
    + "'.\n\n" + "The terminal returned the output:\n'"
    + terminalOutput['outputString'] + "'.\n\n"
    + "If there was an error message, it reads: '"
    + terminalOutput['errorString'] + "'.\n"
)
Exemplo n.º 30
0
# This script tests the Terminal class on computers which are running
# a Linux operating system

from terminal import Terminal

testTerminal = Terminal("test-terminal", "/bin/bash")
terminalOutput = testTerminal.executeCommand("ls")
print("The command '" + terminalOutput['executedCommand'] +
      "' was executed in terminal '" + terminalOutput['terminalName'] +
      "'.\n\n" + "The terminal returned the output:\n'" +
      terminalOutput['outputString'] + "'.\n\n" +
      "If there was an error message, it reads: '" +
      terminalOutput['errorString'] + "'.\n")
Exemplo n.º 31
0
# MAIN control of the whole program

# IMPORTS
import os
import colored
import yfinance as yf
import functions as funcs
from terminal import Terminal
from stock_data import Stock


#Initiate Terminal controller class
terminal = Terminal()




stock = Stock("TSLA", start=None, end=None)
print(stock.get_equity_information())

# etf = Stock("DGRO", start=None, end=None)
# terminal.show_company_info(etf)


# if __name__ == "__main__":

#     terminal.introduction()
#     testTrackedStock = Stock("AAPL", start=None, end=None)

#     choice = 0
#     while choice != 8:
Exemplo n.º 32
0
 def __init__(self, term=None):
     self.term = Terminal() if term is None else term
     self._stream = self.term.stream
     self._saved = False
Exemplo n.º 33
0
from terminal import Terminal
import json
import os
import sys


if __name__ == "__main__":

    if len(sys.argv) > 1:
        host = sys.argv[1]
        
    else:
        with open(os.path.join("config","config.json")) as file:
            data = json.loads(file.read())
            host = data.get("host", "localhost")
            
    terminal = Terminal(host = host)
    terminal.run()
def lesson3():
    """ draw some lines! """
    term = Terminal()

    term.clear_screen();
    sys.stdout.write(term.get_code(term.FOREGROUND_RED))
    Line(term, Point(4, 4), Point(term.size.x - 4, term.size.y - 4)).draw('r')
    sys.stdout.write(term.get_code(term.FOREGROUND_GREEN))
    Line(term, Point(term.size.x - 4, 4),\
         Point(term.size.x - 20, term.size.y - 4)).draw('r')

    sys.stdout.write(term.get_bgfg_code(term.BACKGROUND_BLUE,
                                        term.FOREGROUND_WHITE))
    Line(term, Point(0, 0), Point(0, term.size.y - 2)).draw(' ')
    Line(term, Point(term.size.x - 1, 0), \
         Point(term.size.x - 1, term.size.y - 2)).draw(' ')
    Line(term, Point(0, 0), Point(term.size.x - 1, 0)).draw(' ')
    Line(term, Point(0, term.size.y - 2), \
         Point(term.size.x - 1, term.size.y - 2)).draw(' ')

    term.set_cursor_bottom_left()
    sys.stdout.write(term.get_code(term.RESET))
    sys.stdout.write(term.get_code(term.FOREGROUND_GREEN))
    print("Python: Line draw demo")
    sys.stdout.write(term.get_code(term.RESET))
Exemplo n.º 35
0
        tmpfile = os.path.expanduser('~') + '\\' + tmpfile
    if os.path.exists(tmpfile):
        print "An urader testbed server is already running at port", server_port
        sys.exit(0)
    open(tmpfile, 'a').close()
    server = Server()
    server.server_func(server_port)
    os.remove(tmpfile)
elif sys.argv[1] == "terminal":
    from terminal import Terminal
    if len(sys.argv) < 4 or sys.argv[2] != "-s":
        print_usage()
        sys.exit(1)
    server_ip = sys.argv[3]
    if len(sys.argv) >= 6 and sys.argv[4] == '-p':
        try:
            server_port = int(sys.argv[5])
            if server_port < 0 or server_port > 65534:
                raise BaseException
        except:
            print "Usage error: invalid server port"
            print_usage()
            sys.exit(1)
    if os.path.exists('terminal') == False:
        os.mkdir('terminal')
    terminal = Terminal()
    terminal.terminal_func(server_ip, server_port)
else:
    print_usage()
    sys.exit(0)
Exemplo n.º 36
0
    def __init__(self, cfg):
	    Terminal.__init__(self, cfg)
Exemplo n.º 37
0
Arquivo: urd.py Projeto: cefyr/urd
class MainWindow(QtGui.QFrame):
    def __init__(self, file_to_open=''):
        super().__init__()
        self.setWindowTitle('New file')

        self.force_quit_flag = False

        self.config = read_config()
        self.setStyleSheet('background: '+self.config['theme']['background'])

        layout = QtGui.QVBoxLayout(self)
        common.kill_theming(layout)

        self.scene_container = QtGui.QScrollArea(self)
        layout.addWidget(self.scene_container, stretch=1)

        self.scene = Scene(self.config,
                self.scene_container.horizontalScrollBar().value,
                self.scene_container.verticalScrollBar().value)

        self.scene_container.setWidget(self.scene)
        self.scene_container.setDisabled(True)

        self.terminal = Terminal(self, lambda: self.scene.file_path)
        layout.addWidget(self.terminal)

        self.connect_signals(self.scene, self.terminal)

#        common.set_hotkey('Escape', self, self.terminal.toggle)
        common.set_hotkey('Ctrl+N', self, self.scene.request_new_file)
        common.set_hotkey('Ctrl+O', self, lambda:self.terminal.prompt('o '))
        common.set_hotkey('Ctrl+S', self, self.scene.request_save_file)
        common.set_hotkey('Ctrl+Shift+S', self,
                          lambda:self.terminal.prompt('s '))

        if file_to_open:
            self.scene.open_file(file_to_open)

        self.show()

    # Override
    def closeEvent(self, event):
        if not self.scene.is_modified() or self.force_quit_flag:
            event.accept()
        else:
            self.terminal.error('Unsaved changes! Force quit with q! or save first.')
            event.ignore()

    def quit(self, force):
        if force:
            self.force_quit_flag = True
            self.close()
        else:
            self.force_quit_flag = False
            self.close()

    def connect_signals(self, scene, term):
        connect = (
            (term.add_plotline, scene.add_plotline),
            (term.add_timeslot, scene.add_timeslot),
            (term.insert_plotline, scene.insert_plotline),
            (term.insert_timeslot, scene.insert_timeslot),
            (term.move_plotline, scene.move_plotline),
            (term.move_timeslot, scene.move_timeslot),
            (term.copy_plotline, scene.copy_plotline),
            (term.copy_timeslot, scene.copy_timeslot),
            (term.remove_plotline, scene.remove_plotline),
            (term.remove_timeslot, scene.remove_timeslot),
            (term.move_cell, scene.move_cell),
            (term.copy_cell, scene.copy_cell),
            (term.edit_cell, scene.edit_cell),
            (term.clear_cell, scene.clear_cell),
            (term.undo, scene.undo),
            (term.set_time_orientation, scene.set_time_orientation),
            (scene.window_title_changed, self.setWindowTitle),

            # Terminal actions
            (scene.prompt_sig, term.prompt),
            (scene.error_sig, term.error),
            (scene.print_, term.print_),
            (term.give_up_focus, scene.setFocus),
            (term.input_term.scroll_index, self.scene_container.event),

            # File operations
            (term.request_new_file, scene.request_new_file),
            (term.request_save_file, scene.request_save_file),
            (term.request_open_file, scene.request_open_file),
            (term.request_quit, self.quit)
        )
        for signal, slot in connect:
            signal.connect(slot)
def save_num_cat_2( df_cat_num, df_md, project_relative_root_path, local_project):

    for field_name_num in df_md.columns:
        field_name_cat   = field_name_num + '_cat'
        categories_list  = []
        indexes_list     = []
        df_filtered      = df_cat_num[ [field_name_num, field_name_cat] ]
        categories       = list(df_filtered[field_name_cat].unique())

        cleaned_categories = [c for c in categories if str(c) != 'nan']
        
        #print(cleaned_categories)
        for category in cleaned_categories:
        
        index = df_filtered[ df_filtered[field_name_cat] == category][field_name_num].iloc[0]
            try:
            if int(category ) == int(index) :
                print('It is a number')
                return [], []
            except:
            pass    
            
            print(f'category: {category} - index: {int(index)}' )
            categories_list.append(category)
            indexes_list.append(index)

        pass
# ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
# method: def save_data_folder( local_group, local_project):
# ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
def save_data_folder( local_group, local_project):
    Terminal.print_box( [f'{local_group} - {local_project}'], font_color = FontColor.Blue) 

    project_relative_root_path = f'./data/{local_group}/{local_project}/'

    df_selected_columns_original, df_selected_columns     = save_relevant_features( 
        project_relative_root_path, 
        local_project )
    
    df_finished_responses   = save_only_completed_responses( 
        df                         = df_selected_columns, 
        project_relative_root_path = project_relative_root_path, 
        local_project              = local_project ) 
    
    df_md = save_metadata_alternative ( 
        df_selected_columns         = df_selected_columns_original,  # because wee need the first line (column description)
        df_finished_responses       = df_finished_responses, 
        project_relative_root_path  = project_relative_root_path, 
        local_project               = local_project )

    df_all = save_num_cat( 
        project_relative_root_path = project_relative_root_path, 
        local_project              = local_project )    

    save_num_cat_2(
        df_all = df_all, 
        df_md = df_md,
        project_relative_root_path  = project_relative_root_path, 
        local_project               = local_project )
    )    

# ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
# __main__
# ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
if __name__ == '__main__':
    from terminal import Terminal, FontColor

    Terminal.print_box( ['PREPROCESSING STARTED'], font_color = FontColor.Green) 

    option = 'FRI_20_VIC' #'THU_20_FER-1'
    run_all = False 

    # MONDAY 14 FER -------------------------------------------------------------------------------------------------
    if option == 'MON_14_FER' or run_all:
        save_data_folder("MON_14_FER", 'ca2a')
        save_data_folder("MON_14_FER", 'nova_pets')
        save_data_folder("MON_14_FER", 'social_snap')

        save_data_folder("MON_14_FER", 'skelp')
        save_data_folder("MON_14_FER", 'cozinha_alma')
        save_data_folder("MON_14_FER", 'compal')
        save_data_folder("MON_14_FER", 'budee_meals')
        save_data_folder("MON_14_FER", 'refood')
        
    # THURSDAY 20 FER -------------------------------------------------------------------------------------------------    
    if option == 'THU_20_FER' or run_all:
        save_data_folder('THU_20_FER', 'netflix')
        save_data_folder('THU_20_FER', 'catch_volunteering')
        save_data_folder('THU_20_FER', 'ajuda_portugal')
        save_data_folder('THU_20_FER', 'organibag')

        #save_data_folder('THU_20_FER', 'ajuda_portugal')
        #save_data_folder('THU_20_FER', 'ajuda_portugal')
        #save_data_folder('THU_20_FER', 'ajuda_portugal')
        #save_data_folder('THU_20_FER', 'ajuda_portugal')


    # THURsDAY 20 VIC -------------------------------------------------------------------------------------------------
    if option == 'THU_20_VIC' or run_all:
        save_data_folder('THU_20_VIC', 'micolet')
        save_data_folder('THU_20_VIC', 'refood')
        save_data_folder('THU_20_VIC', 'share_me')
        save_data_folder('THU_20_VIC', 'sustainable_u')


    # FRIDAY 18 FER ---------------------------------------------------------------------------------------------------   
    if option == 'FRI_18_FER' or run_all:            
        save_data_folder('FRI_18_FER', 'live_smart')
        save_data_folder('FRI_18_FER', 'lost_abroad')
        save_data_folder('FRI_18_FER', 'ma_beauty')
        save_data_folder('FRI_18_FER', 'naturally')
    
    # FRIDAY 18 VIC ---------------------------------------------------------------------------------------------------
    if option == 'FRI_18_VIC' or run_all:    
        save_data_folder('FRI_18_VIC', 'food_donations')
        save_data_folder('FRI_18_VIC', 'green_care')
        save_data_folder('FRI_18_VIC', 'kid_kid_store')
        save_data_folder('FRI_18_VIC', 'meduse')
        save_data_folder('FRI_18_VIC', 'ocean_alive')

    # FRIDAY 20 FER ---------------------------------------------------------------------------------------------------
    if option == 'FRI_20_FER' or run_all:    
        save_data_folder('FRI_20_FER', 'chef4u')
        save_data_folder('FRI_20_FER', 'fake_news')
        save_data_folder('FRI_20_FER', 'startup_helpe')
    
    # FRIDAY 20 VIC ---------------------------------------------------------------------------------------------------    
    if option == 'FRI_20_VIC' or run_all:    
        save_data_folder('FRI_20_VIC', 'gap_year')
        save_data_folder('FRI_20_VIC', 'social_app')
        save_data_folder('FRI_20_VIC', 'tie_trip')
        
            
Exemplo n.º 39
0
def flatten_log(log_path, file_like, preserve_renditions=True, show_esc=False):
    """
    Given a log file at *log_path*, write a string of log lines contained
    within to *file_like*.  Where *file_like* is expected to be any file-like
    object with write() and flush() methods.

    If *preserve_renditions* is True, CSI escape sequences for renditions will
    be preserved as-is (e.g. font color, background, etc).  This is to make the
    output appear as close to how it was originally displayed as possible.
    Besides that, it looks really nice =)

    If *show_esc* is True, escape sequences and control characters will be
    visible in the output.  Trailing whitespace and escape sequences will not be
    removed.

    ..note::

        Converts our standard recording-based log format into something that
        can be used with grep and similar search/filter tools.
    """
    from terminal import Terminal, SPECIAL
    metadata = get_log_metadata(log_path)
    rows = metadata.get('rows', 24)
    cols = metadata.get('columns', None)
    if not cols:
        # Try the old metadata format which used 'cols':
        cols = metadata.get('cols', 80)
    term = Terminal(rows=rows, cols=cols, em_dimensions=0)
    out_line = u""
    cr = False
    # We skip the first frame, [1:] because it holds the recording metadata
    for count, frame in enumerate(get_frames(log_path)):
        if count == 0:
            # Skip the first frame (it's just JSON-encoded metadata)
            continue
        # First 13 chars is the timestamp:
        frame_time = float(frame.decode('UTF-8', 'ignore')[:13])
        # Convert to datetime object
        frame_time = datetime.fromtimestamp(frame_time/1000)
        if show_esc:
            frame_time = frame_time.strftime(u'\x1b[0m%b %m %H:%M:%S')
        else: # Renditions preserved == I want pretty.  Make the date bold:
            frame_time = frame_time.strftime(u'\x1b[0;1m%b %m %H:%M:%S\x1b[m')
        if not show_esc:
            term.write(frame[14:])
        if term.capture:
            # Capturing a file...  Keep feeding it frames until complete
            continue
        elif term.captured_files:
            for line in term.screen:
                # Find all the characters that come before/after the capture
                for char in line:
                    if ord(char) >= SPECIAL:
                        adjusted = escape_escape_seq(out_line, rstrip=True)
                        adjusted = frame_time + u' %s\n' % adjusted
                        file_like.write(adjusted.encode('utf-8'))
                        out_line = u""
                        if char in term.captured_files:
                            captured_file = term.captured_files[char].file_obj
                            captured_file.seek(0)
                            file_like.write(captured_file.read())
                            file_like.write(b'\n')
                            del captured_file
                            term.clear_screen()
                            term.close_captured_fds() # Instant cleanup
                    else:
                        out_line += char
            if not out_line:
                continue
            adjusted = frame_time + u' %s\n' % out_line.strip()
            file_like.write(adjusted.encode('utf-8'))
            out_line = u""
            continue
        else:
            term.clear_screen()
        frame = frame.decode('UTF-8', 'ignore')
        for char in frame[14:]:
            if '\x1b[H\x1b[2J' in out_line: # Clear screen sequence
                # Handle the clear screen (usually ctrl-l) by outputting
                # a new log entry line to avoid confusion regarding what
                # happened at this time.
                out_line += u"^L" # Clear screen is a ctrl-l or equivalent
                if show_esc:
                    adjusted = raw(out_line)
                else:
                    adjusted = escape_escape_seq(out_line, rstrip=True)
                adjusted = frame_time + u' %s\n' % adjusted
                file_like.write(adjusted.encode('utf-8'))
                out_line = u""
                continue
            if char == u'\n':
                if show_esc:
                    adjusted = raw(out_line)
                else:
                    adjusted = escape_escape_seq(out_line, rstrip=True)
                if not adjusted:
                    out_line = u"" # Skip empty lines
                    continue
                adjusted = frame_time + u' %s\n' % adjusted
                file_like.write(adjusted.encode('utf-8'))
                out_line = u""
                cr = False
            elif char == u'\r':
                # Carriage returns need special handling.  Make a note of it
                cr = True
            else:
                # \r without \n means that characters were (likely)
                # overwritten.  This usually happens when the user gets to
                # the end of the line (which would create a newline in the
                # terminal but not necessarily the log), erases their
                # current line (e.g. ctrl-u), or an escape sequence modified
                # the line in-place.  To clearly indicate what happened we
                # insert a '^M' and start a new line so as to avoid
                # confusion over these events.
                if cr:
                    out_line += "^M"
                    file_like.write((frame_time + u' ').encode('utf-8'))
                    if show_esc:
                        adjusted = raw(out_line)
                    else:
                        adjusted = escape_escape_seq(out_line, rstrip=True)
                    file_like.write((adjusted + u'\n').encode('utf-8'))
                    out_line = u""
                out_line += char
                cr = False
        file_like.flush()
    del term
Exemplo n.º 40
0
 def test_iter(self):
     cfg = CFG([Variable("S"),
                Variable("A"),
                Variable("B"),
                Variable("C")],
               [Terminal("a"), Terminal("b")], [
                   CFGRule(Variable("S"),
                           [Variable("A"), Variable("B")]),
                   CFGRule(Variable("S"), [Terminal("a")]),
                   CFGRule(Variable("A"), [Terminal("b")]),
                   CFGRule(Variable("A"), [Variable("C")])
               ], Variable("S"))
     words = [x for x in cfg]
     self.assertEqual(words, [[Terminal("a")]])
     cfg = CFG([Variable("S"), Variable("A"),
                Variable("C")],
               [Terminal("a"), Terminal("b")], [
                   CFGRule(Variable("S"), [Variable("A")]),
                   CFGRule(Variable("S"), [Terminal("a")]),
                   CFGRule(Variable("A"), [Terminal("b")]),
                   CFGRule(Variable("A"), [Variable("C")])
               ], Variable("S"))
     words = [x for x in cfg]
     self.assertIn([Terminal("a")], words)
     self.assertIn([Terminal("b")], words)
     self.assertEqual(len(words), 2)
     cfg = CFG([Variable("S"), Variable("A"),
                Variable("B")],
               [Terminal("a"), Terminal("b")], [
                   CFGRule(Variable("S"),
                           [Variable("S"), Variable("S")]),
                   CFGRule(Variable("S"), [Terminal("a")]),
                   CFGRule(Variable("A"), [Terminal("b")])
               ], Variable("S"))
     it = iter(cfg)
     word = next(it)
     self.assertEqual(set(word), set([Terminal("a")]))
     word = next(it)
     self.assertEqual(set(word), set([Terminal("a")]))
     word = next(it)
     self.assertEqual(set(word), set([Terminal("a")]))
     word = next(it)
     self.assertEqual(set(word), set([Terminal("a")]))
     word = next(it)
     self.assertEqual(set(word), set([Terminal("a")]))
     cfg = CFG([Variable("S"), Variable("A"),
                Variable("B")],
               [Terminal("a"), Terminal("b")], [
                   CFGRule(Variable("S"),
                           [Variable("A"), Variable("S")]),
                   CFGRule(Variable("S"), [Terminal("a")]),
                   CFGRule(Variable("A"), [Terminal("b")])
               ], Variable("S"))
     it = iter(cfg)
     temp = [next(it) for _ in range(100)]
     self.assertIn(
         [Terminal("b"),
          Terminal("b"),
          Terminal("b"),
          Terminal("a")], temp)
Exemplo n.º 41
0
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1052, 664)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)

        self.nav_widget = QtWidgets.QWidget(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum,
                                           QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(
            self.nav_widget.sizePolicy().hasHeightForWidth())
        self.nav_widget.setSizePolicy(sizePolicy)
        self.nav_widget.setStyleSheet("background-color:#057EAB")
        self.nav_widget.setObjectName("nav_widget")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.nav_widget)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.nav_menu_label_2 = QtWidgets.QLabel(self.nav_widget)
        font = QtGui.QFont()
        font.setPointSize(21)
        font.setBold(False)
        font.setWeight(50)
        self.nav_menu_label_2.setFont(font)
        self.nav_menu_label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.nav_menu_label_2.setObjectName("nav_menu_label_2")
        self.verticalLayout_2.addWidget(self.nav_menu_label_2)
        spacerItem = QtWidgets.QSpacerItem(40, 20,
                                           QtWidgets.QSizePolicy.Expanding,
                                           QtWidgets.QSizePolicy.Minimum)
        self.verticalLayout_2.addItem(spacerItem)
        self.nav_home_btn = QtWidgets.QPushButton(self.nav_widget)
        font = QtGui.QFont()
        font.setPointSize(15)
        self.nav_home_btn.setFont(font)
        self.nav_home_btn.setCursor(QtGui.QCursor(
            QtCore.Qt.PointingHandCursor))
        self.nav_home_btn.setStyleSheet("border:none")
        self.nav_home_btn.setObjectName("nav_home_btn")
        self.verticalLayout_2.addWidget(self.nav_home_btn)
        self.nav_line1_2 = QtWidgets.QFrame(self.nav_widget)
        self.nav_line1_2.setFrameShape(QtWidgets.QFrame.HLine)
        self.nav_line1_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.nav_line1_2.setObjectName("nav_line1_2")
        self.verticalLayout_2.addWidget(self.nav_line1_2)
        self.nav_scan_btn = QtWidgets.QPushButton(self.nav_widget)
        font = QtGui.QFont()
        font.setPointSize(15)
        self.nav_scan_btn.setFont(font)
        self.nav_scan_btn.setCursor(QtGui.QCursor(
            QtCore.Qt.PointingHandCursor))
        self.nav_scan_btn.setStyleSheet("border:none")
        self.nav_scan_btn.setObjectName("nav_scan_btn")
        self.verticalLayout_2.addWidget(self.nav_scan_btn)
        self.nav_line2_2 = QtWidgets.QFrame(self.nav_widget)
        self.nav_line2_2.setFrameShape(QtWidgets.QFrame.HLine)
        self.nav_line2_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.nav_line2_2.setObjectName("nav_line2_2")
        self.verticalLayout_2.addWidget(self.nav_line2_2)
        self.nav_ip_btn = QtWidgets.QPushButton(self.nav_widget)
        font = QtGui.QFont()
        font.setPointSize(15)
        self.nav_ip_btn.setFont(font)
        self.nav_ip_btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.nav_ip_btn.setStyleSheet("border:none")
        self.nav_ip_btn.setObjectName("nav_ip_btn")
        self.verticalLayout_2.addWidget(self.nav_ip_btn)
        self.nav_line3_2 = QtWidgets.QFrame(self.nav_widget)
        self.nav_line3_2.setFrameShape(QtWidgets.QFrame.HLine)
        self.nav_line3_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.nav_line3_2.setObjectName("nav_line3_2")
        self.verticalLayout_2.addWidget(self.nav_line3_2)
        self.nav_terminal_btn = QtWidgets.QPushButton(self.nav_widget)
        font = QtGui.QFont()
        font.setPointSize(15)
        self.nav_terminal_btn.setFont(font)
        self.nav_terminal_btn.setCursor(
            QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.nav_terminal_btn.setStyleSheet("border:none")
        self.nav_terminal_btn.setObjectName("nav_terminal_btn")
        self.verticalLayout_2.addWidget(self.nav_terminal_btn)
        spacerItem1 = QtWidgets.QSpacerItem(20, 40,
                                            QtWidgets.QSizePolicy.Minimum,
                                            QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout_2.addItem(spacerItem1)
        self.horizontalLayout.addWidget(self.nav_widget)
        self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding,
                                           QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(
            self.stackedWidget.sizePolicy().hasHeightForWidth())

        self.stackedWidget.setSizePolicy(sizePolicy)
        self.stackedWidget.setObjectName("stackedWidget")

        ################################# Home #################################
        """ 
        self.home.setWidgetResizable(True) """
        self.home = QtWidgets.QWidget()
        self.home_ui = Home(self.home)
        self.stackedWidget.addWidget(self.home)
        #########################################################################

        ################################# Port Scan #############################
        self.port_scan = QtWidgets.QWidget()
        self.port_scan_ui = Port_scan(self.port_scan)
        self.stackedWidget.addWidget(self.port_scan)
        #########################################################################

        self.ip_scan = QtWidgets.QWidget()
        self.ip_scan.setObjectName("ip_scan")
        self.label_7 = QtWidgets.QLabel(self.ip_scan)
        self.label_7.setGeometry(QtCore.QRect(300, 10, 67, 17))
        self.label_7.setObjectName("label_7")
        self.stackedWidget.addWidget(self.ip_scan)

        #################################### Terminal ##########################
        self.terminal = QtWidgets.QWidget()
        self.terminal_ui = Terminal(self.terminal)
        self.stackedWidget.addWidget(self.terminal)
        #######################################################################

        ################################### Menu Bar ############################
        self.horizontalLayout.addWidget(self.stackedWidget)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1052, 22))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuSave_as = QtWidgets.QMenu(self.menuFile)
        self.menuSave_as.setObjectName("menuSave_as")
        self.menuSave_as.setTitle('Save As')
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.actionJson = QtWidgets.QAction(MainWindow)
        self.actionJson.setObjectName("actionJson")
        self.actionJson.setText('JSON')
        self.actionJson.triggered.connect(self.port_scan_ui.saveAsJson)

        self.actionPdf = QtWidgets.QAction(MainWindow)
        self.actionPdf.setObjectName("actionPdf")
        self.actionPdf.setText('PDF')
        self.actionPdf.triggered.connect(self.port_scan_ui.saveAsPdf)

        self.menuSave_as.addAction(self.actionJson)
        self.menuSave_as.addAction(self.actionPdf)

        self.menuFile.addAction(self.menuSave_as.menuAction())

        self.menubar.addAction(self.menuFile.menuAction())

        #########################################################################

        self.retranslateUi(MainWindow)
        self.stackedWidget.setCurrentIndex(1)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
Exemplo n.º 42
0
 def test_emptiness(self):
     cfg = CFG([Variable("S"), Variable("A"),
                Variable("B")],
               [Terminal("a"), Terminal("b")], [
                   CFGRule(Variable("S"),
                           [Variable("A"), Variable("B")]),
                   CFGRule(Variable("S"), [Terminal("a")]),
                   CFGRule(Variable("A"), [Terminal("b")])
               ], Variable("S"))
     self.assertEqual(
         set([Variable("S"),
              Variable("A"),
              Terminal("a"),
              Terminal("b")]), cfg.get_reachable())
     self.assertFalse(cfg.is_empty())
     cfg = CFG([Variable("S"), Variable("A"),
                Variable("B")],
               [Terminal("a"), Terminal("b")], [
                   CFGRule(Variable("S"),
                           [Variable("A"), Variable("B")]),
                   CFGRule(Variable("S"), [Terminal("a")]),
                   CFGRule(Variable("A"), [Terminal("b")])
               ], Variable("B"))
     self.assertEqual(
         set([Variable("S"),
              Variable("A"),
              Terminal("a"),
              Terminal("b")]), cfg.get_reachable())
     self.assertTrue(cfg.is_empty())
Exemplo n.º 43
0
    observer = Observer()
    collection = JamCollection()
    player = JamPlayer(collection)
    
    valid_filepaths = []
    for dirpath, dirnames, filenames in os.walk(JAM_DIR):
        files_with_paths = [os.path.join(dirpath, filename) for filename in filenames if JamCollection.valid_file_check(filename)]
        if len(files_with_paths) > 0:
            collection.batch_add_tracks(files_with_paths)

    event_handler = JamFileHandler(collection)

    observer.schedule(event_handler, JAM_DIR, recursive=True)
    observer.start()

    term = Terminal(collection, player)
    intro = \
    """
      ___           ___           ___           ___           ___     
     /\  \         /\  \         /\__\         /\  \         /\  \    
    /::\  \        \:\  \       /:/ _/_        \:\  \       |::\  \   
   /:/\:\__\        \:\  \     /:/ /\  \        \:\  \      |:|:\  \  
  /:/ /:/  /    ___  \:\  \   /:/ /::\  \   ___ /::\  \   __|:|\:\  \ 
 /:/_/:/__/___ /\  \  \:\__\ /:/_/:/\:\__\ /\  /:/\:\__\ /::::|_\:\__\\
 \:\/:::::/  / \:\  \ /:/  / \:\/:/ /:/  / \:\/:/  \/__/ \:\~~\  \/__/
  \::/~~/~~~~   \:\  /:/  /   \::/ /:/  /   \::/__/       \:\  \      
   \:\~~\        \:\/:/  /     \/_/:/  /     \:\  \        \:\  \     
    \:\__\        \::/  /        /:/  /       \:\__\        \:\__\    
     \/__/         \/__/         \/__/         \/__/         \/__/    
      ___           ___           ___           ___           ___     
     /\  \         /\  \         /\__\         /\__\         /\  \    
Exemplo n.º 44
0
 def test_paper(self):
     cfg = CFG([Variable("S"), Variable("C")], [
         Terminal("a"),
         Terminal("b"),
         Terminal("c"),
         Terminal("q"),
         Terminal("am"),
         Terminal("bm"),
         Terminal("cm"),
         Terminal("qm")
     ], [
         CFGRule(
             Variable("S"),
             [Variable("C"), Terminal("q"),
              Variable("C")]),
         CFGRule(
             Variable("C"),
             [Terminal("a"), Variable("C"),
              Terminal("am")]),
         CFGRule(
             Variable("C"),
             [Terminal("b"), Variable("C"),
              Terminal("bm")]),
         CFGRule(
             Variable("C"),
             [Terminal("c"), Variable("C"),
              Terminal("cm")]),
         CFGRule(
             Variable("C"),
             [Terminal("q"), Variable("C"),
              Terminal("qm")]),
         CFGRule(Variable("C"),
                 [Variable("C"), Variable("C")]),
         CFGRule(Variable("C"), [])
     ], Variable("S"))
     regex = RegexTree(Node("(a,b)|(bm,c)|(cm,am,q)"))
     fsm = regex.to_fsm()
     fsm.close()
     cfg_temp = cfg.intersect(fsm)
     self.assertFalse(cfg_temp.is_empty())
     regex = RegexTree(Node("(a,b)|(b,c)|(cm,am,q)"))
     fsm = regex.to_fsm()
     fsm.close()
     cfg_temp = cfg.intersect(fsm)
     self.assertTrue(cfg_temp.is_empty())
Exemplo n.º 45
0
class DADispatcher(DAServer):
    """
    Service for job status bookkeeping.
    Protocol:
      SRV: send READY
      CLT: send START, EXIT, or DONE
    """

    STATES = ["UNKNOWN", "CREATED", "PENDING", "RUNNING", "DONE", "FAILED", "EXITED"]

    CLUSTERS = ["lsf", "interactive", "local"]
    FALLBACK = {"lsf": "interactive", "interactive": "", "local": ""}
    MAXACTIVE = {"lsf": 40, "interactive": 10, "local": 20}

    class JobInfo(object):
        """
        Struct for job information.
        """

        def __init__(self, name_):
            self.name = name_
            self.key = string.join(random.sample(string.ascii_lowercase, 4), "")
            self.cluster = ""
            self.proc = None
            self.node = ""
            self.state = "CREATED"
            self.lastHB = 0

    def __init__(self, workspace_, resubmit=False, terminal=None):
        DAServer.__init__(self, "dispatch")

        self._workspace = workspace_
        self._webDir = ""
        self._jobInfo = {}
        self._resubmit = resubmit
        self._readyJobs = dict([(k, []) for k in DADispatcher.CLUSTERS])
        self._activeJobs = dict([(k, []) for k in DADispatcher.CLUSTERS])
        self._lock = threading.Lock()
        self._stateChanged = threading.Event()
        if terminal:
            self._terminal = terminal
        else:
            self._terminal = Terminal(TERMNODE, verbose=True)

        self.options = dict([(k, "") for k in DADispatcher.CLUSTERS])

    def __del__(self):
        for cluster in DADispatcher.CLUSTERS:
            for jobInfo in self._activeJobs[cluster]:
                self.kill(jobInfo)

    def authenticate(self, jobName_, key_):
        try:
            return key_ == self._jobInfo[jobName_].key
        except:
            return False

    def canServe(self, jobName_):
        if jobName_ in self._jobInfo:
            return 1
        else:
            return -1

    def serve(self, request_, jobName_):
        jobInfo = self._jobInfo[jobName_]

        jobInfo.lastHB = time.time()

        try:
            response = request_.recv(1024)
            request_.send("OK")
            if response == "HB":
                self.log("Heart beat from", jobName_)
                return
            elif response in DADispatcher.STATES:
                self.log("Set state", jobName_, response)
            else:
                raise Exception()
        except:
            response = "UNKNOWN"

        with self._lock:
            try:
                jobInfo.state = response

                finished = False

                if jobInfo.state == "DONE":
                    self._activeJobs[jobInfo.cluster].remove(jobInfo)
                    finished = True
                elif jobInfo.state == "FAILED":
                    self._activeJobs[jobInfo.cluster].remove(jobInfo)
                    if self._resubmit:
                        self._readyJobs[jobInfo.cluster].append(jobInfo)

                    finished = True

                if finished:
                    if jobInfo.cluster == "interactive":
                        jobInfo.proc.close()
                    elif jobInfo.cluster == "local":
                        jobInfo.proc.communicate()

            except:
                self.log("Exception while serving", jobName_, "\n", excDump())

            if jobInfo.state == "FAILED":
                with open(self._workspace + "/logs/" + jobName_ + ".fail", "w") as failLog:
                    pass

        self._stateChanged.set()

    def createJob(self, jobName_, cluster_, append=True):
        jobInfo = DADispatcher.JobInfo(jobName_)
        jobInfo.cluster = cluster_
        self._jobInfo[jobName_] = jobInfo
        if append:
            self._readyJobs[cluster_].append(jobInfo)
        if DEBUG:
            self.log("Created", jobName_)
        return jobInfo

    def submitOne(self, cluster, logdir=""):
        if len(self._activeJobs[cluster]) >= DADispatcher.MAXACTIVE[cluster] or len(self._readyJobs[cluster]) == 0:
            return False

        with self._lock:
            try:
                jobInfo = self._readyJobs[cluster].pop(0)
            except IndexError:
                return False

        if DEBUG:
            self.log("submit", jobInfo.name)

        if self.submit(jobInfo, logdir):
            with self._lock:
                self._activeJobs[cluster].append(jobInfo)
            return True
        else:
            with self._lock:
                self._readyJobs[cluster].append(jobInfo)
            return False

    def submit(self, jobInfo_, logdir=""):
        self.log("Submitting job ", jobInfo_.name)

        if not logdir:
            logdir = self._workspace + "/logs"

        try:
            if jobInfo_.cluster == "lsf":
                command = "bsub -J {jobName} -o {log} -cwd '$TMPDIR' {options} 'source {environment};darun.py {workspace} {jobName} {key}'".format(
                    jobName=jobInfo_.name,
                    log=logdir + "/" + jobInfo_.name + ".log",
                    options=self.options["lsf"],
                    environment=self._workspace + "/environment",
                    workspace=self._workspace,
                    key=jobInfo_.key,
                )

                self.log(command)

                bsubout = self._terminal.communicate(command)

                success = False
                if len(bsubout) != 0 and "submitted" in bsubout[0]:
                    matches = re.search("<([0-9]+)>", bsubout[0])
                    if matches:
                        success = True

                if not success:
                    self.log("bsub failed")
                    raise Exception

                self.log("lxbatch job ID for {0} is {1}".format(jobInfo_.name, matches.group(1)))

                proc = matches.group(1)
                node = ""

            elif jobInfo_.cluster == "interactive":
                node = TERMNODE

                if LOADBALANCE:
                    hostProc = subprocess.Popen(["host", TERMNODE], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    out, err = hostProc.communicate()
                    for line in out.split("\n"):
                        if "has address" in line:
                            addr = line.split()[3]
                            for term in Terminal.OPENTERMS:
                                if term.addr == addr:
                                    break
                            else:
                                node = addr
                                break

                command = "cd $TMPDIR;source {environment};darun.py -p {workspace} {jobName} {key} >> {log} 2>&1;exit".format(
                    environment=self._workspace + "/environment",
                    workspace=self._workspace,
                    jobName=jobInfo_.name,
                    key=jobInfo_.key,
                    log=logdir + "/" + jobInfo_.name + ".log",
                )

                self.log(node + ":", command)

                term = Terminal(node)
                term.write(command)

                self.log("Command issued to", term.node)

                proc = term
                node = term.node

            elif jobInfo_.cluster == "local":
                command = "cd {tmpdir};source {environment};darun.py -p {workspace} {jobName} {key} >> {log} 2>&1".format(
                    tmpdir=TMPDIR,
                    environment=self._workspace + "/environment",
                    workspace=self._workspace,
                    jobName=jobInfo_.name,
                    key=jobInfo_.key,
                    log=logdir + "/" + jobInfo_.name + ".log",
                )

                self.log(command)

                proc = subprocess.Popen(
                    command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
                )  # stdout will be redirected to a log file within the job

                self.log("Subprocess started")

                node = "localhost"

        except:
            return False

        with self._lock:
            jobInfo_.proc = proc
            jobInfo_.state = "PENDING"
            jobInfo_.node = node
            jobInfo_.lastHB = time.time()

        self._stateChanged.set()

        return True

    def kill(self, jobInfo_):
        if jobInfo_.cluster == "lsf":
            response = self._terminal.communicate("bkill {0}".format(jobInfo_.proc))
            for line in response:
                self.log(line)

        elif jobInfo_.cluster == "interactive":
            if jobInfo_.proc.isOpen():
                jobInfo_.proc.close(force=True)

        elif jobInfo_.cluster == "local":
            if jobInfo_.proc.poll() is None:
                jobInfo_.proc.terminate()

        try:
            self._jobInfo.pop(jobInfo_.name)
        except:
            self.log("Exception while trying to remove", jobInfo_.name)

    def dispatch(self, logdir=""):
        monitorTerminate = threading.Event()
        monitorThread = threading.Thread(target=self.monitor, args=(monitorTerminate,), name="monitor")
        monitorThread.daemon = True
        monitorThread.start()

        while True:
            submitted = False
            for cluster in DADispatcher.CLUSTERS:
                if self.submitOne(cluster, logdir):
                    submitted = True

            if submitted:
                continue

            with self._lock:
                nReady = reduce(lambda x, y: x + y, map(len, self._readyJobs.values()))
                nActive = reduce(lambda x, y: x + y, map(len, self._activeJobs.values()))

            if nReady == 0 and nActive == 0:
                break

            self._stateChanged.wait(60.0)

            if not self._stateChanged.isSet():  # timeout
                self.log("No job changed state in the last 60 seconds. Now checking for stale jobs..")
                exited = []
                longpend = []

                if len(self._activeJobs["lsf"]) != 0:
                    lsfNodes = {}
                    try:
                        response = self._terminal.communicate("bjobs")[1:]
                        if DEBUG:
                            print "bjobs", response

                        for line in response:
                            id = line.split()[0]
                            node = line.split()[5]
                            lsfNodes[id] = node

                    except:
                        self.log("Job status query failed")

                with self._lock:
                    for jobInfo in self._activeJobs["lsf"]:
                        # Two different tasks - if id is in the list of ids, set the node name.
                        # If not, the job may have exited abnormally - check state.
                        if jobInfo.proc in lsfNodes:
                            if not jobInfo.node:
                                jobInfo.node = lsfNodes[jobInfo.proc]

                            if jobInfo.lastHB < time.time() - 120:
                                self.log("No heartbeat from", jobInfo.name, "for 120 seconds")
                                if jobInfo.state == "PENDING":
                                    longpend.append(jobInfo)
                                elif jobInfo.state == "RUNNING":
                                    exited.append(jobInfo)
                        else:
                            self.log(jobInfo.name, "disappeared from LSF job list")
                            exited.append(jobInfo)

                    for jobInfo in self._activeJobs["interactive"]:
                        if not jobInfo.proc.isOpen():
                            exited.append(jobInfo)
                        elif jobInfo.lastHB < time.time() - 120:
                            self.log("No heartbeat from", jobInfo.name, "for 120 seconds")
                            if jobInfo.state == "PENDING":
                                longpend.append(jobInfo)
                            elif jobInfo.state == "RUNNING":
                                exited.append(jobInfo)

                    for jobInfo in self._activeJobs["local"]:
                        if jobInfo.proc.poll() is not None:
                            exited.append(jobInfo)
                        elif jobInfo.lastHB < time.time() - 120:
                            self.log("No heartbeat from", jobInfo.name, "for 120 seconds")
                            if jobInfo.state == "PENDING":
                                longpend.append(jobInfo)
                            elif jobInfo.state == "RUNNING":
                                exited.append(jobInfo)

                    for jobInfo in exited:
                        self.log("Set state", jobInfo.name, "EXITED")
                        self.kill(jobInfo)  # removes from self._jobInfo
                        jobInfo.state = "EXITED"
                        self._activeJobs[jobInfo.cluster].remove(jobInfo)

                for jobInfo in exited:
                    with open(self._workspace + "/logs/" + jobInfo.name + ".fail", "w") as failLog:
                        pass

                resubmit = []
                if self._resubmit:
                    resubmit += exited

                available = dict(
                    [(c, DADispatcher.MAXACTIVE[c] - len(self._activeJobs[c])) for c in DADispatcher.CLUSTERS]
                )
                for jobInfo in longpend:
                    fallback = DADispatcher.FALLBACK[jobInfo.cluster]
                    if fallback and available[fallback] > 0:
                        # This job did not start in time and there is a space in the fallback queue
                        with self._lock:
                            self.kill(jobInfo)  # removes from self._jobInfo
                            jobInfo.state = "EXITED"
                            self._activeJobs[jobInfo.cluster].remove(jobInfo)
                            jobInfo.cluster = fallback
                            resubmit.append(jobInfo)
                            available[fallback] -= 1

                for jobInfo in resubmit:
                    newJobInfo = self.createJob(jobInfo.name, jobInfo.cluster, append=False)

                    if self.submit(newJobInfo, logdir):
                        with self._lock:
                            self._activeJobs[jobInfo.cluster].append(newJobInfo)
                    else:
                        with self._lock:
                            self._readyJobs[jobInfo.cluster].append(newJobInfo)

                    self._stateChanged.set()

            time.sleep(1)  # allow the monitor thread to catch up
            self._stateChanged.clear()

        monitorTerminate.set()
        self._stateChanged.set()
        monitorThread.join()

    def monitor(self, _terminate):
        self.printStatus()
        self.printStatusWeb()
        lastWebUpdate = time.time()

        while True:
            self._stateChanged.wait(10.0)
            if _terminate.isSet():
                break

            self.printStatus()
            if time.time() > lastWebUpdate + 60.0:
                self.printStatusWeb()
                lastWebUpdate = time.time()

    def countJobs(self):
        jobCounts = dict((key, 0) for key in DADispatcher.STATES)

        with self._lock:
            for jobInfo in self._jobInfo.values():
                jobCounts[jobInfo.state] += 1

        return jobCounts

    def setWebDir(self, dir_):
        self._webDir = dir_
        try:
            os.mkdir(self._webDir)
        except OSError:
            pass

    def printStatus(self):
        jobCounts = self.countJobs()

        line = ""
        for state in DADispatcher.STATES:
            line += " {state}: {n}".format(state=state, n=jobCounts[state])

        line = "\r" + line
        line += " " * 10
        sys.stdout.write(line)
        if DEBUG:
            sys.stdout.write("\n")
        sys.stdout.flush()

    def printStatusWeb(self, copyLogs=False):
        if not self._webDir:
            return

        if copyLogs:
            logDir = self._webDir + "/logs"
            if not os.path.exists(logDir):
                os.mkdir(logDir)

            for fileName in os.listdir(self._workspace + "/logs"):
                sourceName = self._workspace + "/logs/" + fileName
                destName = logDir + "/" + fileName
                if not os.path.exists(destName) or os.stat(sourceName).st_mtime > os.stat(destName).st_mtime:
                    shutil.copy(sourceName, destName)

        allJobs = self._jobInfo.keys()

        summaryName = self._webDir + "/summary.dat"
        if not os.path.exists(summaryName):
            with open(summaryName, "w") as summaryFile:
                # add more info in the future?
                summaryFile.write("workspace = " + self._workspace)

        statusDir = self._webDir + "/status"

        if not os.path.exists(statusDir):
            os.makedirs(statusDir)
            for job in allJobs:
                open(statusDir + "/" + job + ".UNKNOWN", "w").close()

        with self._lock:
            for statusFile in os.listdir(statusDir):
                jobName = statusFile[: statusFile.rfind(".")]
                if jobName not in self._jobInfo:
                    continue
                current = statusFile[statusFile.rfind(".") + 1 :]
                actual = self._jobInfo[jobName].state
                if current != actual:
                    os.rename(statusDir + "/" + statusFile, statusDir + "/" + jobName + "." + actual)
Exemplo n.º 46
0
 def test_to_pda(self):
     cfg = CFG([Variable("E"), Variable("I")], [
         Terminal("a"),
         Terminal("b"),
         Terminal("0"),
         Terminal("1"),
         Terminal("+"),
         Terminal("*"),
         Terminal("("),
         Terminal(")")
     ], [
         CFGRule(Variable("I"), [Terminal("a")]),
         CFGRule(Variable("I"), [Terminal("b")]),
         CFGRule(Variable("I"),
                 [Variable("I"), Terminal("a")]),
         CFGRule(Variable("I"),
                 [Variable("I"), Terminal("b")]),
         CFGRule(Variable("I"),
                 [Variable("I"), Terminal("0")]),
         CFGRule(Variable("I"),
                 [Variable("I"), Terminal("1")]),
         CFGRule(Variable("E"), [Variable("I")]),
         CFGRule(
             Variable("E"),
             [Variable("E"), Terminal("*"),
              Variable("E")]),
         CFGRule(
             Variable("E"),
             [Variable("E"), Terminal("+"),
              Variable("E")]),
         CFGRule(
             Variable("E"),
             [Terminal("("), Variable("E"),
              Terminal(")")])
     ], Variable("E"))
     pda = cfg.to_PDA()
     self.assertIsInstance(pda, PDA)
     self.assertIn(
         PDATransitionFunction(PDAState("q"), "epsilon", Variable("I"),
                               PDAState("q"), [Terminal("a")]),
         pda.transition_function)
     self.assertIn(
         PDATransitionFunction(PDAState("q"), "epsilon", Variable("I"),
                               PDAState("q"),
                               [Variable("I"), Terminal("0")]),
         pda.transition_function)
     self.assertIn(
         PDATransitionFunction(PDAState("q"), "epsilon", Variable("E"),
                               PDAState("q"), [Variable("I")]),
         pda.transition_function)
     self.assertEqual(18, len(pda.transition_function))
     self.assertTrue(pda.accepts_by_empty_stack([Terminal("a")], 100))
     self.assertTrue(pda.accepts_by_empty_stack([Terminal("b")], 100))
     self.assertFalse(
         pda.accepts_by_empty_stack([Terminal(x) for x in "b0"], 100))
     self.assertTrue(
         pda.accepts_by_empty_stack([Terminal(x) for x in "b0"], 1000))
     self.assertTrue(
         pda.accepts_by_empty_stack([Terminal(x) for x in "b00"], 10000))
Exemplo n.º 47
0
    def submit(self, jobInfo_, logdir=""):
        self.log("Submitting job ", jobInfo_.name)

        if not logdir:
            logdir = self._workspace + "/logs"

        try:
            if jobInfo_.cluster == "lsf":
                command = "bsub -J {jobName} -o {log} -cwd '$TMPDIR' {options} 'source {environment};darun.py {workspace} {jobName} {key}'".format(
                    jobName=jobInfo_.name,
                    log=logdir + "/" + jobInfo_.name + ".log",
                    options=self.options["lsf"],
                    environment=self._workspace + "/environment",
                    workspace=self._workspace,
                    key=jobInfo_.key,
                )

                self.log(command)

                bsubout = self._terminal.communicate(command)

                success = False
                if len(bsubout) != 0 and "submitted" in bsubout[0]:
                    matches = re.search("<([0-9]+)>", bsubout[0])
                    if matches:
                        success = True

                if not success:
                    self.log("bsub failed")
                    raise Exception

                self.log("lxbatch job ID for {0} is {1}".format(jobInfo_.name, matches.group(1)))

                proc = matches.group(1)
                node = ""

            elif jobInfo_.cluster == "interactive":
                node = TERMNODE

                if LOADBALANCE:
                    hostProc = subprocess.Popen(["host", TERMNODE], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    out, err = hostProc.communicate()
                    for line in out.split("\n"):
                        if "has address" in line:
                            addr = line.split()[3]
                            for term in Terminal.OPENTERMS:
                                if term.addr == addr:
                                    break
                            else:
                                node = addr
                                break

                command = "cd $TMPDIR;source {environment};darun.py -p {workspace} {jobName} {key} >> {log} 2>&1;exit".format(
                    environment=self._workspace + "/environment",
                    workspace=self._workspace,
                    jobName=jobInfo_.name,
                    key=jobInfo_.key,
                    log=logdir + "/" + jobInfo_.name + ".log",
                )

                self.log(node + ":", command)

                term = Terminal(node)
                term.write(command)

                self.log("Command issued to", term.node)

                proc = term
                node = term.node

            elif jobInfo_.cluster == "local":
                command = "cd {tmpdir};source {environment};darun.py -p {workspace} {jobName} {key} >> {log} 2>&1".format(
                    tmpdir=TMPDIR,
                    environment=self._workspace + "/environment",
                    workspace=self._workspace,
                    jobName=jobInfo_.name,
                    key=jobInfo_.key,
                    log=logdir + "/" + jobInfo_.name + ".log",
                )

                self.log(command)

                proc = subprocess.Popen(
                    command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
                )  # stdout will be redirected to a log file within the job

                self.log("Subprocess started")

                node = "localhost"

        except:
            return False

        with self._lock:
            jobInfo_.proc = proc
            jobInfo_.state = "PENDING"
            jobInfo_.node = node
            jobInfo_.lastHB = time.time()

        self._stateChanged.set()

        return True
def run_game():

    # Initialize game and create a screen object.

    state = 'none'
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode(
        (settings.screen_width, settings.screen_height))
    pygame.display.set_caption("Cyber Game")

    player = Player(settings, screen)

    terminal = Terminal(settings, screen, '')
    room1 = Room(settings, screen)
    room1.add_terminal(
        75, 639, 130, 105, '',
        'You are at the Computer. Try to get the safe code from it.', False)
    room1.terminals[0].add_response('help', [
        'Commands:', 'help', 'echo', 'safe code', '',
        'Type in unfamiliar words or phrase to search them (ex. brute force)',
        'Come back here whenever you are stuck'
    ])
    room1.terminals[0].add_response('echo', ['echo'])
    room1.terminals[0].add_response('safe code', ['5902'])
    room1.terminals[0].add_response('brute force', [
        'Brute force can be helpful for finding simple password.',
        'Computers can be helpful to speed this up',
        'Common Bad Passwords: 123456, Password, qwerty, abc123'
    ])
    room1.terminals[0].add_response(
        'the answer to the life the universe and everything',
        ['Password from part 3: 42'])
    room1.terminals[0].add_response(
        'what is the answer to the life the universe and everything',
        ['Password from part 3: 42'])
    room1.terminals[0].add_response("Hichicker's guide to the Galaxy",
                                    ['Password from part 3: 42'])
    room1.terminals[0].add_response('cat',
                                    ['Linux command to read the given file'])
    room1.terminals[0].add_response(
        'ls', ['Linux command to list everything in the current folder'])
    room1.terminals[0].add_response(
        'cd',
        ['Linux command to chnage directory (you move into a new folder)'])
    room1.terminals[0].add_response('caesar shift', [
        'A cipher where every letter is moved down n letters in the alphabet',
        "For Example: 'abc' rot 3 would become 'def'",
        'Your command line has a caesar shift decoder',
        'just type "shift <text> <n>"'
    ])
    room1.terminals[0].add_response('shift', [
        'A cipher where every letter is moved down n letters in the alphabet',
        "For Example: 'abc' rot 3 would become 'def'",
        'Your command line has a caesar shift decoder',
        'just type "shift <text> <n>"'
    ])
    room1.terminals[0].add_response('hexadecimal', [
        'Hexidecimal is base 16, meaning that in addition to the',
        'digits 0-9, it also has "digits" A-F, where A = 10,',
        'B=11, etc. As in base ten, each digit to the left has a greater',
        'power of the base. In hexadecimal, 26 would be represented as 1A',
        'where the 1 represents 16 and A represents 1',
        "Type 'hex to dec <hex num>' in your terminal to convert a",
        'hexadecimal number to a base ten number'
    ])
    room1.terminals[0].add_response(
        "Three Cups of Tea",
        ['Password from part 4: is the number in the title'])

    room1.add_terminal(640, 624, 102, 120, '5902', 'Open the Safe.', True)
    room1.terminals[1].add_response('help', [
        'Commands:', 'ls -> examines safe',
        'cat <text name> -> reads the chosen text', '',
        '(Hint) people often choose password numbers from their interests'
    ])
    room1.terminals[1].add_response(
        'ls', ['You see two books, book1.txt and book2.txt'])
    room1.terminals[1].add_response('cat book1.txt', [
        "Hichicker's guide to the Galaxy",
        "...the answer to the Life, the Universe, and Everything is..."
    ])
    room1.terminals[1].add_response('cat book2.txt', ["Three Cups of Tea"])

    room1.add_terminal(440, 35, 300, 70, 'Password',
                       'Get more info about Brute force and search the desk.',
                       True)
    room1.terminals[2].add_response('help', [
        'You can use the ls command to see where you are',
        'and cd <filename> to move through files',
        'cd .. will move you up a folder'
    ])

    room1.add_terminal(623, 510, 138, 106, '',
                       'Phone. Dial the secret number.', False)
    room1.terminals[3].add_response('help', 'Phone. Dial the secret number.')
    room1.terminals[3].add_response('9', ['mvezmzuzmztyz'])
    room1.terminals[3].add_response('2078037452',
                                    ['A trapdoor opens from beneath the rug'])

    room1.add_terminal(8, 165, 124, 338, '',
                       'You are at the Sofa. What do you want to do?', False)
    room1.terminals[4].add_response(
        'help', ["You are at the Sofa. What do you want to do?"])
    room1.terminals[4].add_response(
        'search', ["You find a slip of paper with the following text: '7E3'"])
    room1.terminals[4].add_response(
        'search sofa',
        ["You find a slip of paper with the following text: '7E3'"])
    room1.terminals[4].add_response(
        'look', ["You find a slip of paper with the following text: '7E3'"])
    room1.terminals[4].add_response(
        'ls', ["You find a slip of paper with the following text: '7E3'"])
    room1.terminals[4].add_response('sleep', ["You sleep for some time"])
    room1.terminals[4].add_response('sit', ["You sit for some time"])

    room1.add_terminal(465, 639, 161, 125, '',
                       'Get the right station in the Radio.', False)
    room1.terminals[5].add_response('2019', ['Password from part 1: HackCU'])

    room1.add_terminal(140, 215, 136, 240, '', 'Just a carpet. Or is it?',
                       False)
    room1.terminals[6].add_response('help', ['Just a carpet. Or is it?'])
    room1.terminals[6].add_response('roll',
                                    ['A trapdoor is revealed underneath'])
    room1.terminals[6].add_response('enter', [
        'Good job!', 'You found the back entrance, the one that is often',
        'forgotten about. Remember, no matter how strong your',
        "encryption is, you're still suceptible to a personal password attack"
    ])
    room1.terminals[6].add_response('enter trapdoor', [
        'Good job!', 'You found the back entrance, the one that is often',
        'forgotten about. Remember, no matter how strong your',
        "encryption is, you're still suceptible to a personal password attack"
    ])
    room1.terminals[6].add_response('enter door', [
        'Good job!', 'You found the back entrance, the one that is often',
        'forgotten about. Remember, no matter how strong your',
        "encryption is, you're still suceptible to a personal password attack"
    ])
    room1.terminals[6].add_response('open trapdoor', [
        'Good job!', 'You found the back entrance, the one that is often',
        'forgotten about. Remember, no matter how strong your',
        "encryption is, you're still suceptible to a personal password attack"
    ])

    room1.add_terminal(243, 1, 72, 104, 'HackCUvenividivichi423',
                       'Combine the 4 passwords to open the lock', True)
    room1.terminals[7].add_response(
        'help', ['Open the lock with the password you have obtained.'])
    room1.terminals[7].add_response('open door', ['Good job', 'You escaped'])
    room1.terminals[7].add_response('open', ['Good job', 'You escaped'])
    room1.terminals[7].add_response('enter door', ['Good job', 'You escaped'])
    room1.terminals[7].add_response('enter', ['Good job', 'You escaped'])

    # Start the main loop for the game.
    while True:
        check_events(player, terminal, state, room1)

        # Redraw the screen during each pass through the loop.
        screen.fill(settings.bg_color)

        player.update()

        terminal.draw()
        room1.draw()

        player.blitme()

        state = get_terminal_collision(player, room1)
        if state == -1:
            terminal.update_header(
                "No Signal.  Type help in any terminal to get more info.")
        else:
            terminal.update_header(room1.terminals[state].terminal_name)

        pygame.display.flip()
Exemplo n.º 49
0
 def __init__(self):
     self.url = ''
     self.format = ' video and audio '
     self.path = '~/Downloads'
     self.terminal = Terminal()
Exemplo n.º 50
0
 def __init__(self):
     self.path_save_file = "./"
     self.connexion = sck.socket(sck.AF_INET, sck.SOCK_STREAM)
     self.term = Terminal(self)
Exemplo n.º 51
0
valid_commands = [
    command for command in commands.__dict__.keys()
    if not command.startswith("__")
]


# Given the query text and the current state, attempt to provide
# an autocompletion. This function will use the contents of the current
# directory as well as all valid commands as the possible results.
def completer(text, state):
    complete_corpus = valid_commands + os.listdir(".")
    options = [option for option in complete_corpus if option.startswith(text)]
    return options[state] if state < len(options) else None


def get_command():
    command = raw_input()
    return command


if __name__ == "__main__":
    print("Welcome to pylinux")

    readline.set_completer(completer)
    readline.parse_and_bind("tab: complete")
    readline.parse_and_bind("bind ^I rl_complete")

    terminal = Terminal()
    terminal.play()
Exemplo n.º 52
0
class Client:
    def __init__(self):
        self.path_save_file = "./"
        self.connexion = sck.socket(sck.AF_INET, sck.SOCK_STREAM)
        self.term = Terminal(self)

    def setPathToSaveFile(self, path):
        self.path_save_file = path

    def connect(self, host, port):
        try:
            self.connexion.connect((str(host), int(port)))
            print("Connected")
        except ConnectionRefusedError:
            print("No server responding.")
        except OSError:
            print("Invalid arguments.")

    def runterminal(self):
        self.term.run()

    def recv(self, file_name=""):
        metadata_tmp = self.connexion.recv(4096).decode()

        metadata = json.loads(metadata_tmp)

        if file_name != "":
            metadata["name"] = file_name

        print("Do you want to receive this " + metadata["type"] + ":\n" +
              metadata["name"])

        test = False

        while not test:
            ans = input("\n[Y/n]")
            if ans.lower() == "yes" or ans.lower() == "y":
                if os.path.exists(
                        os.path.join(self.path_save_file, metadata["name"])):
                    print("The received " + metadata["type"] +
                          " already exists. Do you want to delete it ?")

                    self.askconfirmdel(metadata)

                self.connexion.send(b"Y")
                test = True

                if metadata["type"] == "file":
                    self.recvfile(
                        metadata,
                        os.path.join(self.path_save_file, metadata["name"]))
                    print("File received!")

                elif metadata["type"] == "dir":
                    self.recvdir(metadata, self.path_save_file)
                    print("Folder received!")

                else:
                    print("Error: Wrong data type sent")

            elif ans.lower() == "no" or ans.lower() == "n":
                self.connexion.send(b"N")
                test = True

                return

    def askconfirmdel(self, metadata):
        test2 = False

        while not test2:
            ans = input("\n[Y/n]")

            if ans.lower() == "yes" or ans.lower() == "y":
                self.deldir(os.path.join(self.path_save_file,
                                         metadata["name"]))
                print("Directory deleted!")
                test2 = True

            elif ans.lower() == "no" or ans.lower() == "n":
                self.connexion.send(b"N")
                test2 = True

                return

    def deldir(self, path):
        if os.path.isdir(path):
            for name in os.listdir(path):
                self.deldir(os.path.join(path, name))
            os.rmdir(path)
        else:
            os.remove(path)

    def recvfile(self, metadata, path):
        f = open(path, "wb")
        data = b" "

        file_size = int(metadata["size"])
        count = 0
        tmp = 0

        while count < file_size:
            if tmp % 100 == 0:
                printprogress(count, file_size)

            data = self.connexion.recv(16384)
            count += len(data)
            tmp += 1

            f.write(data)

        printprogress(count, file_size)
        print("")

        f.close()

    def recvdir(self, metadata, path):
        if metadata != {}:
            if metadata["type"] == "dir":
                content = metadata["content"]

                path = os.path.join(path, metadata["name"])
                os.makedirs(path)

                for o in content:
                    self.recvdir(content[o], path)
            elif metadata["type"] == "file":
                path = os.path.join(path, metadata["name"])
                self.recvfile(metadata, path)
            else:
                print("Error: Wrong data type sent")
    print('{:.<20} {:<15}'.format('Upgrade firmware',
                                  'Yes' if upgrade else 'No'))
    print()


try:
    print(
        '\nThe script will configure the switch d-link with received parameters.'
    )
    print(
        'All that you need is specify management and users VLAN\'s and choice a model.\n'
    )

    network = NetworkBase()

    mgm_vlan = Terminal.get_from_user_management_vlan()
    user_vlan = Terminal.get_from_user_users_vlan()

    if mgm_vlan == user_vlan:
        print('Management VLAN and user VLAN can not be the same!')
        exit(0)

    ip_address = Terminal.get_from_user_ip_address(
    ) or network.get_next_ip_address(mgm_vlan)

    username = network.get_username()
    password = network.get_password()
    ro_snmp_community = network.get_ro_snmp_community()
    rw_snmp_community = network.get_rw_snmp_community()

    print('\nChoice the model of switch that we will configure.\n')
    print(df_2_wmv.shape)

    url = project_relative_root_path + local_project + DATA_ANALYSIS_SUFFIX
    print(f'url (save {DATA_WMV_SUFFIX}): {url}')
    df_2_wmv.to_excel(url, index=False, encoding='utf-8')

    return df_2_wmv


# ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
# __main__
# ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
if __name__ == '__main__':
    from terminal import Terminal, FontColor

    Terminal.print_box(['PREPROCESSING STARTED'], font_color=FontColor.Green)

    option = 'SPECIFIC'  #'THU_20_FER-1'
    run_all = False

    if option == 'SPECIFIC':
        local_group = 'THU_20_FER'
        local_project = 'netflix'
        Terminal.print_box([f'{local_group} - {local_project}'],
                           font_color=FontColor.Blue)

        project_relative_root_path = f'./data/{local_group}/{local_project}/'

        df = save_metadata(project_relative_root_path, local_project)
        df = save_data_selected_columns(df, project_relative_root_path,
                                        local_project)
Exemplo n.º 55
0
class Venda():
    def __init__(self, terminal, produtos=[]):
        self.terminal = terminal
        self.produtos = produtos

    def adicionar_produto(self, produto, init_ticket=False):
        mensagem_venda = MensagemVenda(self.terminal)
        xml = mensagem_venda.obter_mensagem_produto(produto, 1, 20,
                                                    init_ticket)

        return ConsultaPromo().consultar_promocao(xml)

    def remover_produto(self, produto):
        if produto:
            self.produtos.remove(produto)

    def finalizar_venda(self):
        pass


if __name__ == "__main__":
    terminal = Terminal('psts', '9906', 'MGA10196')  # socket.gethostname()
    venda = Venda(terminal)
    promo = venda.adicionar_produto(Produto('Produto 01', 5.5), True)
    print(promo)
    promo = venda.adicionar_produto(Produto('Produto 01', 5.5))
    print(promo)
    promo = venda.adicionar_produto(Produto('Produto 01', 5.5))
    print(promo)
Exemplo n.º 56
0
import datetime
import random

from game import Game
from terminal import Ticket, Terminal

# create Keno game
date1 = datetime.date(2019, 10, 19)
game1 = Game(date1)

# create ticket terminal
term = Terminal(100)

# We will sell 3 tickets for game1
ticket_pool = []
for i in range(3):
    ticket = term.generate_ticket(game1)
    ticket_pool.append(ticket)

# play Keno game and get lucky numbers
game1.play()

# Next we validate tickets
print(game1)  # all
for ticket in ticket_pool:
    res = term.validate_ticket(game1, ticket)
    ncorrect = len(res)
    ntotal = len(ticket.nums)
    print(ticket)
    print("Lucky Numbers: {0}".format(str(game1.lucky_numbers).strip('[]')))
    print("Matched Numbers: {0}".format(str(res).strip('[]')))
Exemplo n.º 57
0
 def __init__(self, start_dir, end_dir, validation, hints=[""]):
     Terminal.__init__(self, start_dir, end_dir, validation, hints=[""])
    df_finished_responses = save_only_completed_responses(
        df=df_selected_columns,
        project_relative_root_path=project_relative_root_path,
        local_project=local_project)

    save_metadata_alternative(
        df_selected_columns=
        df_selected_columns_original,  # because wee need the first line (column description)
        df_finished_responses=df_finished_responses,
        project_relative_root_path=project_relative_root_path,
        local_project=local_project)

    save_num_cat(project_relative_root_path=project_relative_root_path,
                 local_project=local_project)


# ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
# __main__
# ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
if __name__ == '__main__':
    from terminal import Terminal, FontColor

    Terminal.print_box(['PREPROCESSING STARTED'], font_color=FontColor.Green)

    option = 'SPECIFIC'  #'THU_20_FER-1'
    run_all = False

    if option == 'SPECIFIC':
        save_data_folder('THU_20_FER', 'netflix')
        #save_data_folder('FRI_18_FER', 'live_smart')
Exemplo n.º 59
0
 def runterminal(self):
     term = Terminal(self)
     term.run()
Exemplo n.º 60
0
from steganography import Steganography
from enum import Enum
from errors import TooLongTextError, TooShortTextError, ExtensionError, EncodeError


class Operation(Enum):
    ENCODE = 1
    DECODE = 2

    EXIT = 99


if __name__ == '__main__':

    try:
        clear_com = Terminal.get_clear_command()
        term_width, _ = Terminal.get_terminal_size()

        os.system(clear_com)
        SystemMessages.banner(term_width)

        print('\n\n\n\n')

        SystemMessages.hard_warning('Current working directory: {}'.format(
            os.getcwd()))
        SystemMessages.hard_warning(
            'Filename must not contain any space or the program will not work...\n\n'
        )

        try:
            image_path = input(