Ejemplo n.º 1
0
    def test_store_and_deserialize_keys(self):
        ecc_key, eg_key, rsa_key = KeyHandler.parse_keyfile(KEYFILE)

        self.assertIsNotNone(ecc_key)
        self.assertIsNotNone(eg_key)
        self.assertIsNotNone(rsa_key)

        self.assertEqual({"_name", "_n", "_e", "_diff", "_salt"}, rsa_key.__dict__.keys(), )
        self.assertEqual({"_g", "_p", "_diff", "_n", "_salt", "_name"}, ecc_key.__dict__.keys())
        self.assertEqual({"_g", "_p", "_diff", "_n", "_salt", "_name", "_k"}, eg_key.__dict__.keys())
Ejemplo n.º 2
0
 def initKeyHandler(self):
   self.keyHandler = KeyHandler()
   self.keyHandler.addKey(K_LEFT , True, 120, 12)
   self.keyHandler.addKey(K_RIGHT, True, 120, 12)
   self.keyHandler.addKey(K_DOWN , True, 12 , 12)
   self.keyHandler.addKey(K_UP   , False)
   self.keyHandler.addKey(K_x    , False)
   self.keyHandler.addKey(K_z    , False)
   self.keyHandler.addKey(K_SPACE, False)
   self.keyHandler.addKey(K_c    , False)
   self.keyHandler.addKey(K_r    , False)
   self.keyHandler.addKey(K_q    , False)
Ejemplo n.º 3
0
    def __init__(self, function_name, request, kwargs, descriptors=None):
        self.global_lock = threading.Lock()
        self.render_name = function_name
        self.request = request
        self.action_name = kwargs.get('action')
        self.args_for_action = None
        self.parser = ExecFileParser()
        self.keyhandler = KeyHandler()
        self.conf = Config()

        if descriptors is None:
            self.stdin = sys.stdin
            self.stdout = sys.stdout
            self.stderr = sys.stderr
        else:
            self.stdin = descriptors['stdin']
            self.stdout = descriptors['stdout']
            self.stderr = descriptors['stderr']
Ejemplo n.º 4
0
class Kunai(object):

    RE_DESPICTION_DELAY = 0.05

    def __init__(self, function_name, request, kwargs, descriptors=None):
        self.global_lock = threading.Lock()
        self.render_name = function_name
        self.request = request
        self.action_name = kwargs.get('action')
        self.args_for_action = None
        self.parser = ExecFileParser()
        self.keyhandler = KeyHandler()
        self.conf = Config()

        if descriptors is None:
            self.stdin = sys.stdin
            self.stdout = sys.stdout
            self.stderr = sys.stderr
        else:
            self.stdin = descriptors['stdin']
            self.stdout = descriptors['stdout']
            self.stderr = descriptors['stderr']

    def __enter__(self):
        self.stdscr = curses.initscr()
        curses.curs_set(0)

        display = Display()
        screen = Screen(self.stdscr, self.request)
        self.view = View(display, screen)

        # Invalidation Ctrl + z
        signal.signal(signal.SIGINT, lambda signum, frame: None)
        self.stdscr.keypad(True)

        curses.raw()
        curses.noecho()
        curses.cbreak()
        curses.nonl()

        return self

    def __exit__(self, exc_type, exc_value, traceback):
        """Exit and action or call next kunai action
        """
        curses.nl()
        curses.endwin()
        if self.action_name:
            # next action
            self.parser.pick_command(self.action_name)
            for const in self.parser.code_obj.co_consts:
                # XXX get only action method code object
                if isinstance(const, types.CodeType):
                    self.parser.set_importmodule_code(const)
                    exec(self.parser.code_obj, {
                        # set globals
                        self.render_name: self.args_for_action,
                    })
        else:
            # finish
            self.execute_command()

    def loop(self):
        # initialize
        self.view.refresh_display()
        self.updating_timer = None

        def despiction():
            self.view.search_query(self.keyhandler.current_key)

        while True:
            self.view.refresh_display()
            try:
                key = self.stdscr.getch()
                self.keyhandler.handle_key(key)
                if self.keyhandler.has_query:
                    with self.global_lock:

                        if self.updating_timer is not None:
                            # clear timer
                            self.updating_timer.cancel()
                            self.updating_timer = None
                        timer = threading.Timer(self.RE_DESPICTION_DELAY,
                                                despiction)
                        self.updating_timer = timer
                        timer.start()

                Command(self)
            except TerminateLoop as e:
                return e.value

    def finish(self, value=0):
        raise TerminateLoop(self.finish_with_exit_code(value))

    def cancel(self):
        raise TerminateLoop(self.cancel_with_exit_code())

    def finish_with_exit_code(self, value):
        if isinstance(self.request, dict):
            self.args_for_action = self.request[self.view.select_line.strip().decode('utf-8')]
        else:
            self.args_for_action = self.view.select_line
        return value

    def cancel_with_exit_code(self):
        return 1

    def execute_command(self):
        """Execute action

        kunai default action
        """
        p = subprocess.Popen(
            self.args_for_action,
            stdout=subprocess.PIPE,
            shell=True,
            executable=self.conf.shell
        )
        (output, err) = p.communicate()
        self.stdout.write(output)
Ejemplo n.º 5
0
class Game:
  def __init__(self):
    self.screen = pygame.display.get_surface()
    self.clock = pygame.time.Clock()
    # default: standard
    self.field = Field(10, 20)
    self.initKeyHandler()

  def initKeyHandler(self):
    self.keyHandler = KeyHandler()
    self.keyHandler.addKey(K_LEFT , True, 120, 12)
    self.keyHandler.addKey(K_RIGHT, True, 120, 12)
    self.keyHandler.addKey(K_DOWN , True, 12 , 12)
    self.keyHandler.addKey(K_UP   , False)
    self.keyHandler.addKey(K_x    , False)
    self.keyHandler.addKey(K_z    , False)
    self.keyHandler.addKey(K_SPACE, False)
    self.keyHandler.addKey(K_c    , False)
    self.keyHandler.addKey(K_r    , False)
    self.keyHandler.addKey(K_q    , False)

  def routine(self):
    # for exit
    for event in pygame.event.get():
      if event.type == pygame.QUIT: sys.exit()
    # key presses
    time = self.clock.tick(120)
    quit, restart = False, False
    for key in self.keyHandler.getTrigger(time):
      if key == K_LEFT    : self.field.moveMino((-1, 0))
      elif key == K_RIGHT : self.field.moveMino((1, 0))
      elif key == K_DOWN  : self.field.moveMino((0, -1))
      elif key == K_UP    : self.field.turnMino(False)
      elif key == K_x     : self.field.turnMino(False)
      elif key == K_z     : self.field.turnMino(True)
      elif key == K_SPACE : self.field.dropMino()
      elif key == K_c     : self.field.holdMino()
      elif key == K_r     : restart = True
      elif key == K_q     : quit = True
    return quit, restart