def get_posidents_from_db(self, sql=None): """ Get posidents from db Args: sql (str): SQL select statement for filtering or None (if not specified, all ids from db are selected) Raises: WSDPError: SQLite error (Raises when not possible to connect to db) WSDPError: Query has an empty result! (Raises when the response is empty) Returns: ids_dict (dict): pseudo ids from db """ if not sql: sql = "SELECT ID FROM OPSUB ORDER BY ID" try: cur = self.conn.cursor() cur.execute(sql) self.conn.commit() ids = cur.fetchall() except sqlite3.Error as e: raise WSDPError(self.logger, e) # Control if not empty if len(ids) <= 1: msg = "Query has an empty result!" raise WSDPError(self.logger, msg) posidents = [] for i in list(set(ids)): posidents.append(i[0]) return posidents
def _transform_names_dict(self, xml_name): """ Convert names in XML name to name in database based on special dictionary Args: xml_name (str): tag of xml attribute in xml response config_file (str): configuration file (not mandatory) Raises: WSDPError(XML ATTRIBUTE NAME CANNOT BE CONVERTED TO DATABASE COLUMN NAME) Returns: database_name (str): column names in database """ try: # Load dictionary with names of XML tags and their relevant database names mapping_attributes_dict = self._read_mapping_attributes_dir() database_name = mapping_attributes_dict[xml_name] return database_name except Exception as e: raise WSDPError( self.logger, "XML ATTRIBUTE NAME CANNOT BE CONVERTED TO DATABASE COLUMN NAME: {}" .format(e), )
def save_attributes_to_db(self, schema, dictionary): """ Save dictionary attributes to db Args: schema (str): name of schema in db dictionary (nested dictonary): converted DB attributes counter (class): class managing statistics info Raises: WSDPError: SQLite error """ try: cur = self.conn.cursor() cur.execute("BEGIN TRANSACTION") for posident_id, posident_info in dictionary.items(): # Update table OPSUB by database attributes items for dat_name in posident_info: cur.execute( """UPDATE OPSUB SET {0} = ? WHERE id = ?""".format( dat_name), (posident_info[dat_name], posident_id), ) cur.execute("COMMIT TRANSACTION") except self.conn.Error as e: cur.execute("ROLLBACK TRANSACTION") cur.close() raise WSDPError(self.logger, "Transaction failed!: {}".format(e))
def create(self, recipe, logger): """Create service instance based on an input dictionary consisted of service name and parameters""" if not isinstance(recipe, dict): raise WSDPError(self.logger, "Input is not a dictionary!") service_name = list(recipe.keys())[0] parameters = list(recipe.values())[0] cls = self.classes[service_name] return cls._from_recipe(parameters, logger)
def _get_parameters(self): """Method for getting parameters""" json_path = self.parameters file = Path(json_path) if file.exists(): with open(file) as f: data = json.load(f) return data["posidents"] else: raise WSDPError(self.logger, "File is not found!")
def _create_connection(self): """ Create a database connection to the SQLite database specified by the db_path Raises: WSDPError: SQLite error """ try: self.conn = sqlite3.connect(self.db_path) except sqlite3.Error as e: raise WSDPError(self.logger, e)
def uloz_vystup(self, osobni_udaje, vystupni_soubor, format_souboru): """Konvertuje osobni udaje typu slovnik do souboru o definovanem formatu a soubor ulozi do definovaneho vystupniho adresare. """ if format_souboru == OutputFormat.GdalDb: self.ctios._write_output_to_db(osobni_udaje, vystupni_soubor) elif format_souboru == OutputFormat.Json: self.ctios._write_output_to_json(osobni_udaje, vystupni_soubor) elif format_souboru == OutputFormat.Csv: self.ctios._write_output_to_csv(osobni_udaje, vystupni_soubor) else: raise WSDPError( self.ctios.logger, "Format {} is not supported".format(format_souboru))
def _check_db(self, db_path): """ Control path to db Args: db_path (str): Path to vfk db Raises: WSDPError: FileNotFoundError """ my_file = Path(db_path) try: # Control if database file exists my_file.resolve(strict=True) return db_path except FileNotFoundError as e: raise WSDPError(self.logger, e)
def add_column_to_db(self, schema, name, datatype): """ Add column to db Args: schema (str): name of schema in db name (str): name of column datatype (str): column data type Raises: WSDPError: SQLite error """ col_names = self.get_columns_names(schema) try: cur = self.conn.cursor() if name not in col_names: cur.execute("""ALTER TABLE {0} ADD COLUMN {1} {2}""".format( schema, name, datatype)) except sqlite3.Error as e: raise WSDPError(self.logger, e)
def get_columns_names(self, schema): """ Get names of columns in schema Args: schema (str): name of schema in db Raises: WSDPError: SQLite error Returns: col_names (list): column names in given schema """ try: cur = self.conn.cursor() cur.execute("PRAGMA read_committed = true;") cur.execute("""select * from {0}""".format(schema)) col_names = list(map(lambda x: x[0], cur.description)) except sqlite3.Error as e: raise WSDPError(self.logger, e) return col_names
def _write_output_to_db(self, result_dict, output_db): """Write dictionary to db""" file = Path(output_db) if file.exists(): db = CtiOSDbManager(output_db, self.logger) db._create_connection() db.add_column_to_db(self.schema, "OS_ID", "text") columns = db.get_columns_names(self.schema) # Convert xml attributes to db dictionary = CtiOSXml2DbConverter(self.mapping_json_path, self.logger).convert_attributes( columns, result_dict) # Save attributes to db db.save_attributes_to_db(self.schema, dictionary) self.logger.info( "Soubor v ceste {} byl aktualizovan.".format(output_db)) db.close_connection() else: raise WSDPError(self.logger, "File is not found!")
def _get_parameters(self): """Method for getting parameters""" sql = None if isinstance(self.parameters, list): db_path = self.parameters[0] sql = self.parameters[1] else: db_path = self.parameters file = Path(db_path) if file.exists(): db = CtiOSDbManager(db_path, self.logger) db._create_connection() if sql: posidents = db.get_posidents_from_db(sql) else: posidents = db.get_posidents_from_db() db.close_connection() return posidents else: raise WSDPError(self.logger, "File is not found!")
def nacti_parametry_z_json_souboru(self, cesta_k_json_souboru): """Vezme parametry ze souboru typu *.JSON a vytvori instanci sluzby GenerujCenoveUdajeDleKu Vnitrek json souboru je ve tvaru slovniku: {"katastrUzemiKod" : "732630", "rok" : "2020", "mesicOd" : "9", "mesicDo" : "12", "format" : "zip"}, kdy klice jsou pevne dane. """ file = Path(cesta_k_json_souboru) if file.exists(): with open(file) as f: data = json.load(f) dictionary = {self.nazev_modulu: data} else: raise WSDPError( self.logger, "File is not found!" ) dictionary = {self.name: dictionary} self.cen_udaje = pywsdp.create(recipe=dictionary)