예제 #1
0
    def __init__(self, configFile = "conf.yml"):
        self.logInstance = Logging.Logging()
        self.config = {}
        self.configFile = configFile

        try:
            with open(self.configFile, 'r') as stream:
                try:
                    self.config = yaml.safe_load(stream)
                except yaml.YAMLError as exc:
                    self.logInstance.logError("Failed to parse config")
                    print(exc)
                    exit(101)
        except FileNotFoundError as exc:
            self.logInstance.logError("Failed to open config")
            print(exc)
            exit(100)
        
        try:
            Path(self.config["varPath"] + "/log/").mkdir(parents=True, exist_ok=True)
            Path(self.config["varPath"] + "/finding/").mkdir(parents=True, exist_ok=True)
            Path(self.config["varPath"] + "/trace/").mkdir(parents=True, exist_ok=True)
        except:
            self.logInstance.logError("Failed to create directories in " + self.config["varPath"])
            exit(3)

        try:
            self.logInstance.logfile = self.config["varPath"] + "/log/pserver.log"
            self.logInstance.writeStart()
        except KeyError as exc:
            self.logInstance.logError("Invalid logfile config at key " + exc.args[0])
            exit(102)

        try:
            self.redisServer = RedisConnection(self.config["redis"]["ip"],self.config["redis"]["port"])
        except KeyError as exc:
            self.logInstance.logError("Invalid redis config at key " + exc.args[0])
            exit(102)
        pass

        self.providers = DNSProviders(self)
        self.cliServer = DNSCliServer.runCliServer(self)
        self.proxyServer = None
예제 #2
0
 def __init__(self,
              screen_size=13.3,
              camera_number=0,
              calib_ratio=1,
              mean_pixels=1,
              logs=False):
     self.camera_number = int(camera_number)
     self.__calibration_manager = CalibrationManager(
         camera_number=self.camera_number, screen_size=screen_size)
     self.__shots_defined = False
     self.__thread = None
     self.logs = Logging() if logs else None
     self.__stop_running = False
     self.calib_real_ratio = int(calib_ratio)
     self.screen_width = self.__calibration_manager.width_px * self.calib_real_ratio
     self.screen_height = self.__calibration_manager.height_px * self.calib_real_ratio
     self.__with_webcam = False
     self.__mean_pixels = int(mean_pixels)
     self.get_pixel = self.get_pixel_mean if self.__mean_pixels > 1 else self.get_pixel_uno
     self.pixel_linear = (0, 0)
     self.pixel_trig = (0, 0)
예제 #3
0
class Parse:
    transcationFlag = False
    newDB = True
    count = 0
    log = logging.Logging()

    def __init__(self, database, query, queryProcessor):
        self.queryProcessor = queryProcessor
        database = database.replace(";", "")
        database = database.strip()
        self.database = database.lower()
        query = query.replace(";", "")
        query = query.strip()
        self.query = query.lower()

    def check_query(self):
        if constants.select in self.query.lower():
            if self.database == "":
                self.log.pushLog(self.query, constants.db_missing)
                print(constants.db_missing)
                return
            self.select()
        elif constants.delete in self.query.lower():
            if self.database == "":
                self.log.pushLog(self.query, constants.db_missing)
                print("Database not selected")
                return
            self.delete()
        elif constants.insert in self.query.lower():
            if self.database == "":
                self.log.pushLog(self.query, constants.db_missing)
                print("Database not selected")
                return
            self.insert()
        elif constants.create in self.query.lower():
            self.create()
        elif constants.update in self.query.lower():
            if self.database == "":
                self.log.pushLog(self.query, constants.db_missing)
                print("Database not selected")
                return
            self.update()
        elif constants.drop in self.query.lower():
            self.drop()
        elif "describe" in self.query.lower() or "desc" in self.query.lower():
            self.describe()
        elif "begin transaction" in self.query.lower():
            if self.database == "":
                self.log.pushLog(self.query, constants.db_missing)
                print("Database not selected")
                return
            Parse.transcationFlag = True
            self.beginTransaction()
            return
        elif "quit" in self.query.lower():
            if Parse.transcationFlag:
                Parse.transcationFlag = False
                self.queryProcessor.rollback()
            self.count = 0
            return 0
        elif "use" in self.query.lower():
            Parse.newDB = True
            return
        elif "commit" in self.query.lower():
            if Parse.transcationFlag:
                self.queryProcessor.commit()
                print("commited")
                Parse.transcationFlag = False
                self.count = 0
                return 0
            else:
                print("No transaction")
                return
        elif "rollback" in self.query.lower():
            if Parse.transcationFlag:
                check = self.rollback()
                if check == 0:
                    Parse.transcationFlag = False
                    self.count = 0
                    return 0
                elif check == 1:
                    return
            else:
                print("No transaction")
                return
        elif "savepoint" in self.query.lower():
            try:
                if Parse.transcationFlag:
                    savepoint = re.compile(r'savepoint\s*(.*).*',
                                           re.IGNORECASE).findall(self.query)
                    savePointName = savepoint[0]
                    savePointName = savePointName.rstrip().lstrip()
                    self.queryProcessor.setSavePoint(savePointName)
                else:
                    print("No transaction")
                    return
            except IndexError as e:
                print("Error in Query Syntax")
            except Exception as e:
                print(e)

        else:
            return -1

    def select(self):
        try:
            if Parse.newDB:
                self.queryProcessor.useDb(self.database)
                Parse.newDB = False
            condition_column = ''
            condition_value = ''

            # with condition
            if constants.where_clause in self.query:
                raw_columns = re.compile(constants.select_column_RE,
                                         re.IGNORECASE).findall(self.query)
                columns = self.format(raw_columns[0])
                tableName = re.compile(constants.select_table_condition_RE,
                                       re.IGNORECASE).findall(self.query)

                condition_column = re.compile(
                    constants.select_condition_column_RE,
                    re.IGNORECASE).findall(self.query)
                condition_value = re.compile(
                    constants.select_condition_value_RE,
                    re.IGNORECASE).findall(self.query)

                condition = self.formatCondition(condition_column,
                                                 condition_value)
                table = tableName[0]
                table = table.lstrip().rstrip()
                if Parse.transcationFlag:
                    if self.count == 0:
                        self.count += 1
                        self.queryProcessor.startTransaction(
                            self.database, table)
                self.queryProcessor.selectQuery(table,
                                                condition=condition,
                                                columnListToDisplay=columns)
                self.log.pushLog(
                    self.query, "Selected " + table + " from " + " database " +
                    self.database)
            # without condition
            else:
                raw_columns = re.compile(constants.select_column_RE,
                                         re.IGNORECASE).findall(self.query)
                columns = self.format(raw_columns[0])

                tableName = re.compile(constants.select_table_no_condition_RE,
                                       re.IGNORECASE).findall(self.query)
                table = tableName[0]
                table = table.lstrip().rstrip()
                if Parse.transcationFlag:
                    if self.count == 0:
                        self.count += 1
                        self.queryProcessor.startTransaction(
                            self.database, table)
                self.queryProcessor.selectQuery(table,
                                                columnListToDisplay=columns)
                self.log.pushLog(
                    self.query, "Selected " + table + " from " + " database " +
                    self.database)

        except IndexError as e:
            self.log.pushLog(self.query, "Error in select query syntax")
            print("Error in Query Syntax")
        except Exception as e:
            self.log.pushLog(self.query, str(e))
            print(e)

    def delete(self):
        try:
            if Parse.newDB:
                self.queryProcessor.useDb(self.database)
                Parse.newDB = False

            if constants.where_clause in self.query:
                tableName = re.compile(constants.delete_table_RE,
                                       re.IGNORECASE).findall(self.query)
                condition_column = re.compile(
                    constants.delete_condition_column_RE,
                    re.IGNORECASE).findall(self.query)
                condition_value = re.compile(
                    constants.delete_condition_value_RE,
                    re.IGNORECASE).findall(self.query)
                condition = self.formatCondition(condition_column,
                                                 condition_value)
                table = tableName[0]
                table = table.lstrip().rstrip()
                if Parse.transcationFlag:
                    if self.count == 0:
                        self.count += 1
                        self.queryProcessor.startTransaction(
                            self.database, table)
                self.queryProcessor.deleteQuery(table, condition)
                self.log.pushLog(
                    self.query, "Data deleted from table " + table +
                    " of database " + self.database)
            elif constants.where_clause not in self.query:
                tableName = re.compile(r'from\s(.*)\s*',
                                       re.IGNORECASE).findall(self.query)
                table = tableName[0]
                table = table.lstrip().rstrip()
                if Parse.transcationFlag:
                    if self.count == 0:
                        self.count += 1
                        self.queryProcessor.startTransaction(
                            self.database, table)
                self.queryProcessor.deleteQuery(table)
                self.log.pushLog(
                    self.query, "Data successfully deleted from table " +
                    table + " of database " + self.database)
        except IndexError as e:
            self.log.pushLog(self.query, "Error in select query syntax")
            print("Error in Query Syntax")
        except Exception as e:
            self.log.pushLog(self.query, str(e))
            print(e)

    def insert(self):
        try:
            if Parse.newDB:
                self.queryProcessor.useDb(self.database)
                Parse.newDB = False
            table = re.compile(constants.insert_table_RE,
                               re.IGNORECASE).findall(self.query)
            table = table[0]
            table = table[:table.find('(')]
            table = table.lstrip().rstrip()
            raw_columns = re.compile(constants.insert_columns_RE,
                                     re.IGNORECASE).findall(self.query)
            raw_values = re.compile(constants.insert_values_RE,
                                    re.IGNORECASE).findall(self.query)
            values = raw_values[0].split(",")
            columns = raw_columns[0].split(",")
            if Parse.transcationFlag:
                if self.count == 0:
                    self.count += 1
                    self.queryProcessor.startTransaction(self.database, table)
            self.queryProcessor.insertQuery(table,
                                            valueList=values,
                                            colList=columns)
            self.log.pushLog(
                self.query, "Data successfully inserted into table " + table +
                " of database " + self.database)
        except IndexError as e:
            self.log.pushLog(self.query, "Error in select query syntax")
            print("Error in Query Syntax")
        except Exception as e:
            self.log.pushLog(self.query, str(e))
            print(e)

    def create(self):
        try:
            if "database" in self.query:
                database = re.compile(r'create database\s(.*)\s*',
                                      re.IGNORECASE).findall(self.query)
                self.queryProcessor.createDB(database[0])
                self.log.pushLog(
                    self.query,
                    "Successfully created database " + self.database)
            elif "table" in self.query:
                if Parse.newDB:
                    self.queryProcessor.useDb(self.database)
                    Parse.newDB = False
                raw_values = re.compile(r'\((.*)\)',
                                        re.IGNORECASE).findall(self.query)
                foreignKeyList = []
                primaryKeyList = []
                if "primary" in raw_values[0].lower():
                    tableName = re.compile(r'create table\s*(.*)\s*\(+?',
                                           re.IGNORECASE).findall(self.query)
                    tableName = re.compile(r'\s*(.*)\s\(',
                                           re.IGNORECASE).findall(
                                               str(tableName[0]))
                    foreignKeyList = []
                    primaryKeyList = re.compile(r'primary key \((.*)\)',
                                                re.IGNORECASE).findall(
                                                    raw_values[0])

                    raw_values[0] = re.sub(r'primary key \((.*)\)', "",
                                           raw_values[0], re.IGNORECASE)
                    raw_values[0] = re.sub(r'foreign key (.*)', "",
                                           raw_values[0], re.IGNORECASE)

                    value_list = raw_values[0].split(",")
                    value_list.remove('')
                else:
                    tableName = re.compile(r'create table\s*(.*)\s*\(+?',
                                           re.IGNORECASE).findall(self.query)
                    value_list = raw_values[0].split(",")
                columnDict = {}
                for val in value_list:
                    temp = val.split()
                    column = str(temp[0])
                    clm_type = str(temp[1])
                    columnDict[column] = clm_type
                table = tableName[0]
                table = table.lstrip().rstrip()
                self.queryProcessor.createTable(table, columnDict,
                                                primaryKeyList, foreignKeyList)
                self.log.pushLog(
                    self.query, "Successfully created table " + table +
                    " from database " + self.database)
        except IndexError as e:
            self.log.pushLog(self.query, "Error in select query syntax")
            print("Error in Query Syntax")
        except Exception as e:
            self.log.pushLog(self.query, str(e))
            print(e)

    def update(self):
        try:
            if Parse.newDB:
                self.queryProcessor.useDb(self.database)
                Parse.newDB = False
            tableName = re.compile(r'update\s(.*)\sset',
                                   re.IGNORECASE).findall(self.query)
            raw_values = re.compile(r'set\s(.*)\swhere',
                                    re.IGNORECASE).findall(self.query)
            val = format(raw_values[0])
            val_list = re.split('=|,', val)
            update_values = {}
            for x in range(0, len(val_list), 2):
                column = val_list[x].lstrip().rstrip()
                clm_value = val_list[x + 1].lstrip().rstrip()
                update_values[column] = clm_value.lstrip().rstrip()
            #condition
            condition_str = self.query[self.query.find('where'):]
            condition_column = re.compile(r'where\s*(.*)[=|>|<|<=|>=]\s*',
                                          re.IGNORECASE).findall(condition_str)
            condition_value = re.compile(r'\s*(=|>|<|<=|>=)\s*(.*).*',
                                         re.IGNORECASE).findall(condition_str)
            condition = self.formatCondition(condition_column, condition_value)

            table = tableName[0]
            table = table.lstrip().rstrip()
            if Parse.transcationFlag:
                if self.count == 0:
                    self.count += 1
                    self.queryProcessor.startTransaction(self.database, table)
            self.queryProcessor.updateQuery(table, update_values, condition)
            self.log.pushLog(
                self.query, "Data successfully update in table " + table +
                " from database " + self.database)
        except IndexError as e:
            self.log.pushLog(self.query, "Error in select query syntax")
            print("Error in Query Syntax")
        except Exception as e:
            self.log.pushLog(self.query, str(e))
            print(e)

    def drop(self):
        try:
            if "database" in self.query:
                database_drop = re.compile(r'drop database\s*(.*)\s*',
                                           re.IGNORECASE).findall(self.query)
                database = database_drop[0]
                database = database.lstrip().rstrip()
                self.queryProcessor.dropDb(database)
                self.log.pushLog(
                    self.query,
                    "Successfully dropped database " + self.database)
            elif "table" in self.query:
                if Parse.newDB:
                    self.queryProcessor.useDb(self.database)
                    Parse.newDB = False
                if self.database == "":
                    self.log.pushLog(self.query, "Database Not Selected")
                    print("Database Not Selected")
                    return
                tableName = re.compile(r'drop table\s*(.*)\s*',
                                       re.IGNORECASE).findall(self.query)
                table = tableName[0]
                table = table.lstrip().rstrip()
                self.queryProcessor.useDb(self.database)
                self.queryProcessor.dropTable(table)
                self.log.pushLog(
                    self.query, "Successfully dropped table " + table +
                    " from database " + self.database)
        except IndexError as e:
            self.log.pushLog(self.query, "Error in select query syntax")
            print("Error in Query Syntax")
        except Exception as e:
            self.log.pushLog(self.query, str(e))
            print(e)

    def describe(self):
        try:
            if Parse.newDB:
                self.queryProcessor.useDb(self.database)
                Parse.newDB = False
            if "describe" in self.query.lower():
                tableName = re.compile(r'describe\s*(.*).*',
                                       re.IGNORECASE).findall(self.query)
            elif "desc" in self.query.lower():
                tableName = re.compile(r'desc\s*(.*).*',
                                       re.IGNORECASE).findall(self.query)
            table = tableName[0]
            table = table.lstrip().rstrip()
            self.queryProcessor.describeTable(table)
            self.log.pushLog(
                self.query,
                "Describe table " + table + " from database " + self.database)
        except IndexError as e:
            print(e)

    def format(self, raw_columns):
        columns = raw_columns.split(",")
        return columns

    # returns condition dictionary
    def formatCondition(self, column, condition_data):
        condition_op = condition_data[0][0]
        condition_val = condition_data[0][1]
        return {
            "columnName": column[0].lstrip().rstrip(),
            "operator": condition_op.lstrip().rstrip(),
            "value": condition_val.lstrip().rstrip()
        }

    def beginTransaction(self):

        print("\n--------------------------------------------------------")
        print("tranasction started")
        query = ""
        while not query.lower() == "quit":
            query = input()
            self.query = query
            val = self.check_query()
            if val == -1:
                print(colored("Incorrect Query", 'red'))
            elif val == 0:
                break
        print("transaction ended")
        print("\n--------------------------------------------------------")

    def rollback(self):
        try:
            self.query = self.query.replace(";", "")
            self.query = self.query.strip()
            check = self.query.split()
            if len(check) > 2:
                savepoint = re.compile(r'rollback to\s(.*).*',
                                       re.IGNORECASE).findall(self.query)
                savePointName = savepoint[0]
                savePointName = savePointName.lstrip().rstrip()
                self.queryProcessor.rollback(savePointName)
                return 1
            elif len(check) == 1:
                if check[0].lower() == 'rollback':
                    self.queryProcessor.rollback()
                    return 0
                else:
                    raise Exception("Invalid Query")
        except IndexError as e:
            print("Error in Query Syntax")
        except Exception as e:
            print(e)
예제 #4
0
 def __init__(self):
     super(MelsecWorker, self).__init__()
     self.existing = True
     self.logging = Logging()
     self.Parameter = DataRead.readParameter()
     self.TrigReset.connect(self.SetTrigReset)
예제 #5
0
dqn.processor = train_processor

experiment_name = "NoseTip"

history_train = dqn.fit(train_env,
                        nb_steps=500,
                        nb_max_episode_steps=100,
                        log_interval=30000,
                        visualize=False,
                        verbose=2)

dqn.save_weights(experiment_name, overwrite=True)

print("******", train_env.wander)

L = Logging()
episode_count = L.log_train_history(history_train, experiment_name)

test_env = ImageEnvironment(images=test_images,
                            landmarks=test_landmarks,
                            state_size=state_window_size,
                            seed=6,
                            step_size=step_size)
test_env.debug = False

dqn.load_weights(experiment_name)
# Need to update the agent to have the test environment processor
dqn.processor = ImageProcessor(test_env)

# Test on one of the episodes
nb_episodes = 300