From: Frank Brehm Date: Tue, 6 Feb 2018 14:37:26 +0000 (+0100) Subject: Decreasing complexity of method import_records() X-Git-Tag: 0.1.2~6^2~32 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=aa8ee08235bb3c10a8564bb56bbdd4d11dc77fd7;p=pixelpark%2Fadmin-tools.git Decreasing complexity of method import_records() --- diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index 9294768..ff9c064 100644 --- a/pp_lib/import_pdnsdata.py +++ b/pp_lib/import_pdnsdata.py @@ -31,7 +31,7 @@ from .cfg_app import PpCfgAppError, PpConfigApplication from .pdns_record import PdnsSoaData -__version__ = '0.10.2' +__version__ = '0.10.3' LOG = logging.getLogger(__name__) # ============================================================================= @@ -71,6 +71,15 @@ class ImportPdnsdataApp(PpConfigApplication): VALUES (%(domain_id)s, %(kind)s, %(content)s) ''').strip() + sql_insert_record = textwrap.dedent('''\ + INSERT INTO records (id, domain_id, name, type, content, + ttl, prio, change_date, disabled, + ordername, auth) + VALUES (%(id)s, %(domain_id)s, %(name)s, %(type)s, %(content)s, + %(ttl)s, %(prio)s, %(change_date)s, %(disabled)s, + %(ordername)s, %(auth)s) + ''').strip() + # ------------------------------------------------------------------------- def __init__(self, appname=None, version=__version__): @@ -757,6 +766,49 @@ class ImportPdnsdataApp(PpConfigApplication): LOG.debug("Commiting changes ...") self.tgt_connection.commit() + # ------------------------------------------------------------------------- + def _import_record(self, record, tgt_cursor): + + if self.tgt_db_type == 'mysql': + record['disabled'] = 0 + else: + record['disabled'] = False + if record['auth'] is None: + record['auth'] = True + else: + if record['auth']: + record['auth'] = True + else: + record['auth'] = False + if record['ordername'] is None: + dom_id = record['domain_id'] + if dom_id in self.domain_ids: + dom_name = self.domain_ids[dom_id] + if record['name'] == dom_name: + record['ordername'] = '' + else: + idx = record['name'].rfind('.' + dom_name) + if idx >= 0: + record['ordername'] = record['name'][:idx] + else: + record['ordername'] = '' + else: + record['ordername'] = '' + if record['type'] in ('NS', 'MX'): + record['content'] = RE_DOT_AT_END.sub('', record['content']) + elif record['type'] == 'SOA': + soa = PdnsSoaData.init_from_data( + record['content'], appname=self.appname, + verbose=self.verbose, base_dir=self.base_dir) + soa.primary = RE_DOT_AT_END.sub('', soa.primary) + soa.email = RE_DOT_AT_END.sub('', soa.email) + record['content'] = soa.data + if self.verbose > 3: + LOG.debug("SQL for insert record:\n{}".format( + to_str(tgt_cursor.mogrify(self.sql_insert_record, record)))) + if not self.simulate: + tgt_cursor.execute(self.sql_insert_record, record) + # ------------------------------------------------------------------------- def import_records(self): @@ -773,16 +825,8 @@ class ImportPdnsdataApp(PpConfigApplication): if self.verbose > 1: LOG.debug("Source SQL:\n{}".format(src_sql)) - tgt_sql = textwrap.dedent('''\ - INSERT INTO records (id, domain_id, name, type, content, - ttl, prio, change_date, disabled, - ordername, auth) - VALUES (%(id)s, %(domain_id)s, %(name)s, %(type)s, %(content)s, - %(ttl)s, %(prio)s, %(change_date)s, %(disabled)s, - %(ordername)s, %(auth)s) - ''').strip() if self.verbose > 1: - LOG.debug("Target SQL:\n{}".format(tgt_sql)) + LOG.debug("Target SQL:\n{}".format(self.sql_insert_record)) with self.tgt_connection.cursor() as tgt_cursor: with self.src_connection.cursor() as src_cursor: @@ -802,42 +846,8 @@ class ImportPdnsdataApp(PpConfigApplication): for result in results: i += 1 - if self.tgt_db_type == 'mysql': - result['disabled'] = 0 - else: - result['disabled'] = False - if result['auth'] is None: - result['auth'] = True - else: - if result['auth']: - result['auth'] = True - else: - result['auth'] = False - if result['ordername'] is None: - dom_id = result['domain_id'] - if dom_id in self.domain_ids: - dom_name = self.domain_ids[dom_id] - if result['name'] == dom_name: - result['ordername'] = '' - else: - idx = result['name'].rfind('.' + dom_name) - if idx >= 0: - result['ordername'] = result['name'][:idx] - else: - result['ordername'] = '' - else: - result['ordername'] = '' - if result['type'] in ('NS', 'MX'): - result['content'] = RE_DOT_AT_END.sub('', result['content']) - elif result['type'] == 'SOA': - soa = PdnsSoaData.init_from_data( - result['content'], appname=self.appname, - verbose=self.verbose, base_dir=self.base_dir) - soa.primary = RE_DOT_AT_END.sub('', soa.primary) - soa.email = RE_DOT_AT_END.sub('', soa.email) - result['content'] = soa.data - if not self.simulate: - tgt_cursor.execute(tgt_sql, result) + self._import_record(result, tgt_cursor) + LOG.info("Imported {} records.".format(i)) if self.tgt_db_type != 'mysql': @@ -853,7 +863,7 @@ class ImportPdnsdataApp(PpConfigApplication): sql = "SELECT SETVAL('records_id_seq', %s)" LOG.debug("Setting curval of records_id_seq to {} ...".format(max_id)) if self.verbose > 1: - LOG.debug("SQL: {}".format(sql)) + LOG.debug("SQL: {}".format(to_str(tgt_cursor.mogrify(sql, (max_id, ))))) if not self.simulate: tgt_cursor.execute(sql, (max_id, ))