Exemple #1
0
class PdeForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.port = None
        self.ui.setupUi(self)

        self.conf = ConfTool("conf.json")
        if self.conf.fileStatus == FileStatus.ISOPEN:
            self.addTextToBox("Config file: LOAD")
            self.ui.labelID.setText(str(self.conf.getIdNumber()))
        else:
            self.ui.startButton.setEnabled(False)
            self.addTextToBox("Config file: ERROR")

        for portName in listPorts.enumerate_serial_ports():
            self.ui.portComboBox.addItem(portName)

        QtCore.QObject.connect(self.ui.startButton, QtCore.SIGNAL("clicked()"),
                               self.startButtonClicked)
        QtCore.QObject.connect(self.ui.resetButton, QtCore.SIGNAL("clicked()"),
                               self.resetButtonClicked)
        QtCore.QObject.connect(self.ui.aboutButton, QtCore.SIGNAL("clicked()"),
                               self.aboutClicked)
        self.ui.resetButton.setEnabled(False)

    def startButtonClicked(self):

        self.stateManager = StateManager(self.conf, self)
        self.stateManager.updateText.connect(self.updateTextBox)
        self.port = PortTool(str(self.ui.portComboBox.currentText()),
                             self.stateManager)
        status = self.port.portOpen()
        if status == PortStatus.OPEN:
            self.addTextToBox("Port " + self.ui.portComboBox.currentText() +
                              " is open")
            self.ui.resetButton.setEnabled(True)
        else:
            self.ui.boxTextEdit.setText("Port " +
                                        self.ui.portComboBox.currentText() +
                                        ": error")

    def resetButtonClicked(self):
        self.ui.boxTextEdit.clear()
        self.addTextToBox("Port " + self.ui.portComboBox.currentText() +
                          " is open")
        self.addTextToBox("----> Reset <----")
        self.stateManager.reset()

    def updateTextBox(self, text):
        self.addTextToBox(text)

    def addTextToBox(self, newText):
        oldText = self.ui.boxTextEdit.toPlainText()
        self.ui.boxTextEdit.setText(newText + "\n" + oldText)

    def aboutClicked(self):
        QtGui.QMessageBox.information(
            self, "About",
            "Serial Device Emulator\n Autor: P.Penar [ppenar[at]prz.edu.pl]")
Exemple #2
0
    def __init__(self,
                 activeImage,
                 selectedImage,
                 offset=(0, 0),
                 size=(200, 200),
                 initialState=(0, 0),
                 wlan='192.168.1.3',
                 dT=1,
                 pings=[
                     '192.168.1.1', '192.168.1.2', '192.168.1.3',
                     '192.168.1.4', '192.168.1.5', '192.168.1.105',
                     '192.168.1.102', '192.168.1.103'
                 ]):
        StateManager.__init__(self, activeImage, selectedImage, offset, size,
                              initialState)
        self.wlan = wlan
        self.wlan1 = meter.SignalMeter(wlan, 1, dT)
        self.wlan1.start()
        self.wlan2 = meter.SignalMeter(wlan, 2, dT)
        self.wlan2.start()
        pygame.font.init()
        self.pingers = []
        for ping in pings:
            tmp = pinger.Pinger(ping, dT=3)
            tmp.start()
            self.pingers.append(tmp)
            self.ips[ping] = self.pingers[-1]

        self.myfont = pygame.font.SysFont("Arial", 18)
        self.myfont2 = pygame.font.SysFont("Courier", 10)
Exemple #3
0
 def __init__(self, path):
     self.state_manager = StateManager()
     self.path = path
     self.zip_r = None
     self.zip_w = None
     self.open_read()
     self.open_write()
Exemple #4
0
    def __init__(self, username, password, host, path):
        ftp = FTP(host)
        try:
            ftp.login(user=username, passwd=password)
        except:
            log('Incorrect credentials')
            exit()

        log('Connected to {} with username {} and password {} on path {}'.
            format(host, username, password, path))
        self.ftp = ftp
        self.state_manager = StateManager()
        self.path = path
Exemple #5
0
 def __init__(self,
              activeImage,
              selectedImage,
              offset=(0, 0),
              size=(200, 200),
              initialState=(0, 0),
              addr='192.168.1.5',
              port=6001,
              login='******',
              password='******'):
     StateManager.__init__(self, activeImage, selectedImage, offset, size,
                           initialState)
     self.camera = CamRunner([addr, port, login, password])
     self.camera.start()
Exemple #6
0
    def startButtonClicked(self):

        self.stateManager = StateManager(self.conf, self)
        self.stateManager.updateText.connect(self.updateTextBox)
        self.port = PortTool(str(self.ui.portComboBox.currentText()),
                             self.stateManager)
        status = self.port.portOpen()
        if status == PortStatus.OPEN:
            self.addTextToBox("Port " + self.ui.portComboBox.currentText() +
                              " is open")
            self.ui.resetButton.setEnabled(True)
        else:
            self.ui.boxTextEdit.setText("Port " +
                                        self.ui.portComboBox.currentText() +
                                        ": error")
Exemple #7
0
 def __init__(self,
              activeImage,
              selectedImage,
              offset=(0, 0),
              size=(200, 200),
              initialState=(0, 0),
              winnum=0,
              stream=None,
              winoffset=0):
     StateManager.__init__(self, activeImage, selectedImage, offset, size,
                           initialState)
     self.winnum = winnum
     self.winoffset = winoffset
     self.stream = stream
     self.quad()
Exemple #8
0
 def __init__(self,
              activeImage,
              selectedImage,
              offset=(0, 0),
              size=(200, 200),
              initialState=(0, 0),
              winnum=0,
              addr=None,
              winoffset=0):
     StateManager.__init__(self, activeImage, selectedImage, offset, size,
                           initialState)
     self.camera = QuadRunner(addr)
     self.addr = addr
     self.quad()
     self.camera.start()
Exemple #9
0
 def OnStateActivated(self):
     StateManager.OnStateActivated(self)
     winoffset = self.winoffset
     if self._activeState == (0, 0):
         self.quad()
     if self._activeState == (-1, -1):
         self.stream.players[0].Activate(self.winnum)
         self.stream.players[0].MoveResize(self.winnum, 0 + winoffset, 0,
                                           1400, 1104)
     if self._activeState == (-1, 1):
         self.stream.players[1].Activate(self.winnum)
         self.stream.players[1].MoveResize(self.winnum, 0 + winoffset, 0,
                                           1400, 1104)
     if self._activeState == (1, -1):
         self.stream.players[2].Activate(self.winnum)
         self.stream.players[2].MoveResize(self.winnum, 0 + winoffset, 0,
                                           1400, 1104)
     if self.winnum == 0:
         if self._activeState == (1, 1):
             self.stream.players[3].Activate(self.winnum)
             self.stream.players[3].MoveResize(self.winnum, 0, 0, 1400,
                                               1104)
     else:
         if self._activeState == (1, 1):
             self.stream.players[4].Activate(0)
             self.stream.players[4].MoveResize(0, 0 + winoffset, 0, 1400,
                                               1104)
             self.stream.players[4].Activate(1)
             self.stream.players[4].MoveResize(1, 0 + winoffset, 0, 1400,
                                               1104)
Exemple #10
0
    def __init__(self, game, player_turn=None, simulations=500):
        self.game = game
        self.state_manager = StateManager(game=game)

        # Set player to random if starting player is not defined
        self.player_turn = player_turn if player_turn == 0 or player_turn == 1 else randint(0,1)
        self.static_player = (player_turn == 0 or player_turn == 1)
        
        print(simulations)
        # Initialize agent to ask for actions
        self.agent = MCTS(state_manager=self.state_manager, simulations=simulations)
Exemple #11
0
 def OnStateActivated(self):
     StateManager.OnStateActivated(self)
     if self._activeState == (0, 0):
         self.quad()
     if self._activeState == (-1, -1):
         self.camera.commands.append(1)
     if self._activeState == (1, -1):
         self.camera.commands.append(2)
     if self._activeState == (-1, 1):
         self.camera.commands.append(3)
     if self._activeState == (1, 1):
         self.camera.commands.append(4)
	def __init__(self):
		# create main game window
		super(GameController, self).__init__(1280, 720, "Panda Runner")

		# setup various managers
		self.screenManager = ScreenManager(self)
		self.stateManager = StateManager(self)
		self.spriteManager = SpriteManager()

		# load spritesheet and probably sounds in the future
		self.loadResources()
		self.start()
class GameController(pyglet.window.Window):
	"""docstring for GameController"""
	def __init__(self):
		# create main game window
		super(GameController, self).__init__(1280, 720, "Panda Runner")

		# setup various managers
		self.screenManager = ScreenManager(self)
		self.stateManager = StateManager(self)
		self.spriteManager = SpriteManager()

		# load spritesheet and probably sounds in the future
		self.loadResources()
		self.start()

	def start(self):
		# schedule update function to be called every frame
		# I need to add some sort of FPS setting here
		clock.schedule(self.update)

		# instantiate the main game instance
		# this will be changed to the menu instance when it's implemented
		gameState = Game(self)
		self.stateManager.addState('game', gameState)
		self.stateManager.setState('game')

	# prepare various resources such as sprite sheets and sounds
	def loadResources(self):
		self.spriteManager.newSheet('main', 'sprites.png')

	# Main update function
	def update(self, dt):
		self.screenManager.update(dt)
		self.draw()

	#main draw function
	def draw(self):
		self.screenManager.draw()
Exemple #14
0
 def OnStateSelected(self, newState):
     if newState == (1, -1): newState = (0, 0)
     if newState == (1, 1): newState = (0, 0)
     #if l==(-1,0):l=(0,0)
     #if l==(1,0):l=(0,0)
     if newState == (0, 0):
         self.camera.Stop()
     if newState == (-1, 0):
         self.camera.Left()
     if newState == (-1, -1):
         self.camera.ZoomIn()
     if newState == (-1, 1):
         self.camera.ZoomOut()
     if newState == (1, 0):
         self.camera.Right()
     if newState == (0, -1):
         self.camera.Down()
     if newState == (0, 1):
         self.camera.Up()
     StateManager.OnStateSelected(self, newState)
class Filesystem:
    """
    Handles filesystem files
    """
    def __init__(self, target_path):
        self.state_manager = StateManager()
        if target_path[-1] != path.sep:
            target_path = target_path + path.sep
        self.path = target_path

    @staticmethod
    def selector_matches(selector):
        """
        Checks if the specified selector is in the right format
        :param selector: Mode selection string
        :return: True if the specified string is in the correct format
        """
        regex = r"folder:(.*)"
        x = re.match(regex, selector)
        if not x:
            return None
        return x.group(1)

    @handle_failure(log)
    def create_state(self):
        """
        Creates a state of the files
        :return: Tuple of the current state and the previous state
        """
        # set current state for class_a
        state = []
        file_list = list(walk(self.path))
        for (index, (dirname, _, filenames)) in enumerate(file_list):
            dirname = dirname.replace(self.path, '')

            if index > 0:
                state.append({'path': dirname, 'is_directory': True})
            for filename in filenames:
                path_with_file = path.join(dirname, filename)
                state.append({
                    'path':
                    path_with_file,
                    'is_directory':
                    False,
                    'size':
                    self.get_file_size(path_with_file),
                    'last_modified':
                    self.get_last_modified_time(
                        path.join(self.path, path_with_file))
                })
        self.state_manager.set_state(state)
        return self.state_manager.get_current_state(
        ), self.state_manager.get_previous_state()

    @handle_failure(log)
    def read(self, filename):
        """
        Read the contents of a file
        :param filename: File path
        :return: The contents of the specified file
        """
        filename = path.normpath(self.path + filename)
        f = open(filename, "rb")
        content = f.read()
        f.close()
        log('Reading ', filename, 'content', content)
        return content

    @handle_failure(log)
    def is_directory(self, filename):
        """
        Checks to see whether the file specified is a directory
        :param filename: File path
        :return: True if the specified file is a directory
        """
        filename = path.normpath(self.path + filename)
        return path.isdir(filename)

    @handle_failure(log)
    def get_last_modified_time(self, filepath):
        """
        get the last modified time of a file
        """
        return math.floor(path.getmtime(path.abspath(filepath)) / 2)

    @handle_failure(log)
    def create_directory(self, filename):
        """
        Create a new directory
        :param filename: Directory path
        """
        filename = path.normpath(self.path + filename)
        mkdir(filename)

    @handle_failure(log)
    def file_exists(self, filename):
        """
        Check the existence of a file
        :param filename: File path
        :return: True if the file exists
        """
        return path.exists(path.join(self.path, filename))

    @handle_failure(log)
    def delete(self, filename):
        """
        Delete a file or directory
        :param filename: File pth to be deleted
        """
        file = path.abspath(path.normpath(self.path + filename + path.sep))
        log('Delete from ', file, self.is_directory(filename))
        if self.is_directory(filename):
            rmtree(file)
        else:
            remove(file)

    @handle_failure(log)
    def copy_from(self, class_b, filename):
        """
        Copy filename from class_b
        :param class_b: Class instance that will provide the contents of the file
        :param filename: File path
        """
        target_path = filename
        from_path = filename
        log('reading ', from_path, 'from class b')
        if class_b.is_directory(from_path):
            log('Create dir from ', from_path, 'to', target_path)
            self.create_directory(target_path)
        else:
            log('Copy from ', class_b.path + from_path, 'to',
                path.normpath(self.path + target_path))
            contents = class_b.read(from_path)
            f = open(path.normpath(self.path + target_path), 'wb')
            f.write(contents)
            f.close()

    def create_file_hash(self, filename):
        """
        create a hash of a file
        """
        return create_hash(path.join(self.path, filename))

    def get_file_size(self, filename):
        """
        get the size of a file
        """
        return path.getsize(path.normpath(self.path + filename))
 def __init__(self, target_path):
     self.state_manager = StateManager()
     if target_path[-1] != path.sep:
         target_path = target_path + path.sep
     self.path = target_path
Exemple #17
0
 def _init_state_manager(self, bot, user_id):
     self.logger.debug("create state Manager for user %d" % user_id)
     state_manager = StateManager(self.message_sender, user_id, self.config)
     self._users_id[user_id] = state_manager
     return state_manager
Exemple #18
0
 def setup(self):
     self.grammar.setup("grammar.txt")
     stateManager = StateManager(self.grammar)
     stateManager.setup()
     self.table.setup(stateManager)
Exemple #19
0
class DecosInteractionStates:
    services_connected = None

    def __init__(self, application):
        # Getting a session that will be reused everywhere
        self.application = application
        self.session = application.session
        self.service_name = self.__class__.__name__

        # Getting a logger. Logs will be in /var/log/naoqi/servicemanager/{application id}.{service name}
        self.logger = qi.Logger(self.service_name)

        # Do some initializations before the service is registered to NAOqi
        self.logger.info("Initializing...")
        self.connect_services()

        self.logger.info("Initialized!")


    @qi.nobind
    def start_service(self):
        self.logger.info("Starting service...")

        self.state_manager = StateManager(self.session)

        self.logger.info("Started!")

    @qi.nobind
    def stop_service(self):
        # probably useless, unless one method needs to stop the service from inside.
        # external naoqi scripts should use ALServiceManager.stopService if they need to stop it.
        self.logger.info("Stopping service...")
        self.application.stop()
        self.logger.info("Stopped!")

    @qi.nobind
    def connect_services(self):
        # connect all services required by your module
        # done in async way over 30s,
        # so it works even if other services are not yet ready when you start your module
        # this is required when the service is autorun as it may start before other modules...
        self.logger.info('Connecting services...')
        self.services_connected = qi.Promise()
        services_connected_fut = self.services_connected.future()

        def get_services():
            try:
                self.memory = self.session.service('ALMemory')


                self.logger.info('All services are now connected')
                self.services_connected.setValue(True)
            except RuntimeError as e:
                self.logger.warning('Still missing some service:\n {}'.format(e))

        get_services_task = qi.PeriodicTask()
        get_services_task.setCallback(get_services)
        get_services_task.setUsPeriod(int(2*1000000))  # check every 2s
        get_services_task.start(True)
        try:
            services_connected_fut.value(30*1000)  # timeout = 30s
            get_services_task.stop()
        except RuntimeError:
            get_services_task.stop()
            self.logger.error('Failed to reach all services after 30 seconds')
            raise RuntimeError


    ### Utility functions ###
    def change_state(self,name):
        self.state_manager.change_state(name)

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

    def cleanup(self):
        # called when your module is stopped
        self.logger.info("Cleaning...")
        # do something
        self.logger.info("End!")
Exemple #20
0
 def OnStateActivated(self):
     StateManager.OnStateActivated(self)
Exemple #21
0
if __name__ == "__main__":
    board_size = config.BOARD_SIZE
    exploration_bonus = config.EXPLORATION_BONUS
    num_simulations = config.NUM_SIMULATIONS
    num_episodes = config.NUM_EPISODES
    save_every = config.SAVE_EVERY
    folder_results = config.FOLDER_RESULTS
    lr = config.LEARNING_RATE

    utils.create_directory(folder_results)

    time_start = time.time()
    board = HexBoard(board_size)
    mapper = Mapper(board_size=board_size)
    state_manager = StateManager(board, mapper, verbose=config.VISUALIZE_MOVES)

    #anet_nn = ANET_NN_COMPLEX(input_size=board_size*board_size, output_size = board_size*board_size).float()
    anet_nn = ANET_NN_GENERAL(input_size=board_size * board_size,
                              output_size=board_size * board_size,
                              hidden_layers=config.HIDDEN_LAYERS,
                              activation=config.ACTIVATION).float()

    anet = ANET_MODEL(anet_nn,
                      max_cases_in_buffer=config.REPLAY_BUFFER_MAX_SIZE)
    optimizer = config.OPTIMIZERS[config.OPTIMIZER](anet_nn.parameters(),
                                                    lr=lr)

    for episode in range(num_episodes):
        while state_manager.game_is_on():
            random_number = random.uniform(0, 1)
Exemple #22
0
class Zip:
    """
    Handles zip files
    """
    def __init__(self, path):
        self.state_manager = StateManager()
        self.path = path
        self.zip_r = None
        self.zip_w = None
        self.open_read()
        self.open_write()

    @staticmethod
    def selector_matches(selector):
        """
        Checks if the specified selector is in the right format
        :param selector: Mode selection string
        :return: True if the specified string is in the correct format
        """
        regex = r"zip:(.*)$"
        x = re.match(regex, selector)
        if not x:
            return None
        return x.group(1)

    @handle_failure(log)
    def open_read(self):
        """
        Open a zip file in read mode
        :return:
        """
        if self.zip_r:
            self.zip_r.close()
        self.zip_r = ZipFile(self.path, 'r')

    @handle_failure(log)
    def open_write(self):
        """
        Open a zip file in write mode
        :return:
        """
        if self.zip_w:
            self.zip_w.close()
        self.zip_w = ZipFile(self.path, 'a')

    @handle_failure(log)
    def create_state(self):
        """
        Creates a state of the files
        :return: Tuple of the current state and the previous state
        """
        state = []
        self.open_read()

        name_list = self.zip_r.namelist()
        # add nested folders if they doesn't exist
        for file in name_list:
            folder_path = str(Path(file).parent) + path.sep
            if folder_path not in name_list and not self.zip_r.getinfo(
                    file).is_dir() and folder_path != '.' + path.sep:
                state.append({'path': folder_path[:-1], 'is_directory': True})
        # set current state for class_a
        for file in name_list:
            info = self.zip_r.getinfo(file)
            if info.is_dir():
                state.append({'path': file[:-1], 'is_directory': True})
            else:
                state.append({
                    'path': file,
                    'is_directory': False,
                    'size': info.file_size,
                    'last_modified': self.get_last_modified_time(info)
                })
        # # add top level folder
        # if len(name_list) > 0 and name_list[0].split(path.sep)[0] + path.sep not in name_list:
        #     state.insert(0, {'path':   name_list[0].split(
        #         path.sep)[0], 'is_directory': True})
        self.state_manager.set_state(state)
        return self.state_manager.get_current_state(
        ), self.state_manager.get_previous_state()

    @handle_failure(log)
    def get_last_modified_time(self, zip_info):
        """
        Get the last modified time
        :param zip_info: ZipInfo object
        :return: Last modified time
        """
        # (year, month, day, hour, minute, second) = zip_info.date_time
        date_string = ''
        for item in zip_info.date_time:
            date_string = date_string + str(item)
        date_time_obj = datetime.datetime.strptime(date_string, '%Y%m%d%H%M%S')
        return math.floor(date_time_obj.timestamp() / 2)

    @handle_failure(log)
    def read(self, filename):
        """
        Read the contents of a file
        :param filename: File path
        :return: The contents of the specified file
        """
        for item in self.zip_r.infolist():
            if item.filename == filename:
                return self.zip_r.read(item.filename)

    @handle_failure(log)
    def file_exists(self, filename):
        """
        Check the existence of a file
        :param filename: File path
        :return: True if the file exists
        """
        for item in self.zip_r.infolist():
            if item.filename == filename:
                return True
        return False

    @handle_failure(log)
    def is_directory(self, filename):
        """
        Checks to see whether the file specified is a directory
        :param filename: File path
        :return: True if the specified file is a directory
        """
        current_state = self.state_manager.get_current_state()
        for item in current_state:
            if item['path'] == filename:
                return item['is_directory']

    @handle_failure(log)
    def create_directory(self, filename):
        """
        Create a new directory
        :param filename: Directory path
        """
        zfi = ZipInfo(filename + path.sep)
        self.zip_w.writestr(zfi, '')
        log('Creating directory', filename)
        self.open_read()
        self.open_write()

    @handle_failure(log)
    def delete(self, filename):
        """
       Delete a file or directory
       :param filename: File pth to be deleted
       """
        is_directory = self.is_directory(filename)
        log("Delete ", is_directory, filename)
        zout = ZipFile('./temp.zip', 'w')
        for item in self.zip_r.infolist():
            buff = self.zip_r.read(item.filename)
            if item.filename != filename and (
                    is_directory and not item.filename.startswith(filename)):
                zout.writestr(item, buff)
        zout.close()
        rename("./temp.zip", self.path)
        self.open_read()

    @handle_failure(log)
    def write(self, filename, content):
        """
        Write contents to a file
        :param filename: File
        :param content: Contents to be written
        """

        # zout = ZipFile('./temp.zip', 'w')
        # did_write = False
        # for item in self.zip_r.infolist():
        #     buff = self.zip_r.read(item.filename)
        #     if item.filename == filename:
        #         zout.writestr(item, content)
        #         did_write = True
        #     else:
        #         zout.writestr(item, buff)
        # if not did_write:
        #     zout.writestr(filename, content)
        # zout.close()
        # rename("./temp.zip",
        #        self.path)
        # self.open_read()
        with ZipFile(self.path, 'w') as zipped_f:
            zipped_f.writestr(filename, content)

    @handle_failure(log)
    def copy_from(self, class_b, filename):
        """
        Copy filename from class_b
        :param class_b: Class instance that will provide the contents of the file
        :param filename: File path
        """
        target_path = filename
        from_path = filename
        if class_b.is_directory(from_path):
            log('Copy from ', from_path, 'to', target_path)
            self.create_directory(target_path)
        else:
            log('Copy from ', from_path, 'to', target_path)
            contents = class_b.read(from_path)
            self.write(filename, contents)

    def create_file_hash(self, filename):
        """
        create a hash of a file
        """
        with tempfile.TemporaryDirectory() as dirpath:
            tempfilename = path.join(dirpath, filename)
            with open(tempfilename, 'wb') as f:
                f.write(self.read(filename))
            return create_hash(tempfilename)
# Pyglet is a slightly lower-level take on Python graphics than PyGame as
# I understand it.
import pyglet
from pyglet import clock
from Entity import *
from HUD import HUD
from pyglet.window import key
from StateManager import StateManager

# If true, program will save a series of PNG images for FFMPEG or similar
# To create videos, and use frame as a variable to index the files.
RECORD = False
frame = 0
# Flag to change for gamequitting event
quit = False
state = StateManager()

# Clock is a pyglet object that controls framerate
clock.set_fps_limit(30)
while (not quit):
        dt = clock.tick()
        state.window.switch_to()
        state.window.dispatch_events()
        state.loop(dt)
        quit = state.is_quit()
        state.window.flip()
        if RECORD:
            # This long snippet just takes a copy of the frame buffer, and dumps it to a file.
            # With software like ffmpeg, I can stitch these stills together to recreate the game.
            # Source: Pyglet's documentation
            pyglet.image.get_buffer_manager().get_color_buffer().save("video/frame{:05}.png".format(frame))
Exemple #24
0
 def OnStateSelected(self, l):
     if l == (0, -1): l = (0, 0)
     if l == (0, 1): l = (0, 0)
     if l == (-1, 0): l = (0, 0)
     if l == (1, 0): l = (0, 0)
     StateManager.OnStateSelected(self, l)
Exemple #25
0
 def setup(self, StateManager):
     self.action = StateManager.getActionFunction()
     self.goto = StateManager.getGotoFunction()
     print(self.action)
     print(self.goto)
Exemple #26
0
    def start_service(self):
        self.logger.info("Starting service...")

        self.state_manager = StateManager(self.session)

        self.logger.info("Started!")
Exemple #27
0
 def OnStateSelected(self, newState):
     StateManager.OnStateSelected(self, newState)
Exemple #28
0
class Ftp:
    """
    Handles ftp files
    """
    def __init__(self, username, password, host, path):
        ftp = FTP(host)
        try:
            ftp.login(user=username, passwd=password)
        except:
            log('Incorrect credentials')
            exit()

        log('Connected to {} with username {} and password {} on path {}'.
            format(host, username, password, path))
        self.ftp = ftp
        self.state_manager = StateManager()
        self.path = path

    @staticmethod
    def selector_matches(selector):
        """
        Checks if the specified selector is in the right format
        :param selector: Mode selection string
        :return: True if the specified string is in the correct format
        """
        regex = r"ftp:(\S*):([\S]+)@([\S^.]+?)([~.\/].*)"
        x = re.search(regex, selector)
        if not x:
            return None
        return {
            "user": x.group(1),
            "password": x.group(2),
            "host": x.group(3),
            "path": x.group(4),
        }

    @handle_failure(log)
    def listdir(self, _path):
        """
        return files and directory names within a path (directory)
        """

        file_list, dirs, nondirs = [], [], []
        try:
            self.ftp.cwd(_path)
        except Exception as exp:
            log("the current path is : ", self.ftp.pwd(), exp.__str__(), _path)
            return [], []
        else:

            self.ftp.retrlines('LIST', lambda x: file_list.append(x.split()))
            # parse each file info result
            for info in file_list:
                ls_type, name, size = info[0], ' '.join(info[8:]), info[4]
                if ls_type.startswith('d'):
                    dirs.append(name)
                else:
                    modification_time = self.parse_ftp_date(
                        self.ftp.voidcmd(f"MDTM " + name).split()[-1])

                    nondirs.append((name, modification_time, int(size)))
            return dirs, nondirs

    @handle_failure(log)
    def walk(self, filepath=path.sep):
        """
        Walk through FTP server's directory tree
        """
        dirs, nondirs = self.listdir(filepath)
        yield filepath, dirs, nondirs
        for name in dirs:
            filepath = path.join(filepath, name)
            yield from self.walk(filepath)
            self.ftp.cwd('..')
            filepath = path.dirname(filepath)

    def generate_filedata(self):
        """
        generat a file state
        """
        state = []
        files = list(self.walk(self.path))
        for (index, (dirname, _, filenames)) in enumerate(files):
            dirname = dirname.replace(self.path, '')

            # ignore the first directory
            if index > 0:
                state.append({'path': dirname, 'is_directory': True})
            for (filename, last_modified, size) in filenames:
                path_with_file = path.join(dirname, filename)
                state.append({
                    'path': path_with_file,
                    'is_directory': False,
                    'last_modified': last_modified,
                    'size': size
                })
        return state

    @handle_failure(log)
    def create_state(self):
        """
        Creates a state of the files
        :return: Tuple of the current state and the previous state
        """
        state = self.generate_filedata()
        self.state_manager.set_state(state)
        return self.state_manager.get_current_state(
        ), self.state_manager.get_previous_state()

    @handle_failure(log)
    @retry_function(2)
    def parse_ftp_date(self, date_string):
        """
        Parse a date string
        :param date_string: Date string
        :return: Date timestamp
        """
        date_time_obj = datetime.datetime.strptime(date_string, '%Y%m%d%H%M%S')
        return math.floor(date_time_obj.timestamp() / 2)

    @handle_failure(log)
    @retry_function(2)
    def read(self, filepath):
        """
        Read the contents of a file
        :param filepath: File path
        :return: The contents of the specified file
        """
        directory_name, file_name = path.split(
            path.normpath(self.path + filepath))
        r = BytesIO()
        self.ftp.cwd(directory_name)
        self.ftp.retrbinary('RETR ' + path.join(directory_name, file_name),
                            r.write)
        self.ftp.cwd(self.path)

        contents = r.getvalue()
        return contents

    @handle_failure(log)
    @retry_function(2)
    def file_exists(self, filepath):
        """
        Check the existence of a file
        :param filename: File path
        :return: True if the file exists
        """
        files = self.generate_filedata()
        for file in files:
            if filepath == file['path']:
                return True
        return False

    @handle_failure(log)
    @retry_function(2)
    def create_directory(self, filename):
        """
        Create a new directory
        :param filename: Directory path
        """
        log('Create directory ', filename)
        try:
            self.ftp.mkd(filename)
        except Exception:
            # silent fail, directory already exists
            return

    @handle_failure(log)
    @retry_function(2)
    def delete(self, filename):
        """
       Delete a file or directory
       :param filename: File pth to be deleted
       """
        if self.is_directory(filename):
            return self.ftp.rmd(filename)
        return self.ftp.delete(filename)

    @handle_failure(log)
    def is_directory(self, filename):
        """
        Checks to see whether the file specified is a directory
        :param filename: File path
        :return: True if the specified file is a directory
        """
        current_state = self.state_manager.get_current_state()
        for item in current_state:
            if item['path'] == filename:
                return item['is_directory']

    @handle_failure(log)
    @retry_function(2)
    def write(self, filename, content):
        """
        Write contents to a file
        :param filename: File path
        :param content: Content to be written
        """
        bio = BytesIO(content)
        directory_name, file_name = path.split(path.join(self.path, filename))
        self.ftp.cwd(directory_name)
        log('Writing', content, ' to', directory_name)

        self.ftp.storbinary('STOR ' + file_name, bio)

    @handle_failure(log)
    @retry_function(2)
    def copy_from(self, class_b, filename):
        """
        Copy filename from class_b
        :param class_b: Class instance that will provide the contents of the file
        :param filename: File path
        """
        target_path = filename
        from_path = filename

        if target_path[0] == path.sep:
            target_path = '.' + target_path
        log('Copy', path.join(class_b.path, from_path), 'to',
            path.join(self.path, target_path))

        if class_b.is_directory(from_path):
            self.create_directory(target_path)
        else:
            self.write(target_path, class_b.read(from_path))

    @handle_failure(log)
    @retry_function(2)
    def create_file_hash(self, filename):
        """
        create a hash of a file
        """
        m = hashlib.sha1()
        self.ftp.retrbinary('RETR ' + self.path + filename, m.update)
        return m.hexdigest()