caricamento nomi e tipi
This commit is contained in:
@@ -23,6 +23,19 @@ from pyftpdlib.authorizers import DummyAuthorizer, AuthenticationFailed
|
||||
def conn_db(cfg):
|
||||
return psycopg2.connect(dbname=cfg.dbname, user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport )
|
||||
|
||||
def extract_value(patterns, primary_source, secondary_source, default='Not Defined'):
|
||||
"""
|
||||
Extracts the first match for a list of patterns from the primary source.
|
||||
Falls back to the secondary source if no match is found.
|
||||
"""
|
||||
for source in (primary_source, secondary_source):
|
||||
for pattern in patterns:
|
||||
matches = re.findall(pattern, source, re.IGNORECASE)
|
||||
if matches:
|
||||
return matches[0] # Return the first match immediately
|
||||
return default # Return default if no matches are found
|
||||
|
||||
|
||||
class DummySha256Authorizer(DummyAuthorizer):
|
||||
def __init__(self, cfg):
|
||||
# Initialize the DummyAuthorizer and add the admin user
|
||||
@@ -88,12 +101,19 @@ class ASEHandler(FTPHandler):
|
||||
if (fileExtension.upper() in (cfg.fileext)):
|
||||
with open(file, 'r') as csvfile:
|
||||
lines = csvfile.readlines()
|
||||
|
||||
unit_types = cfg.headers.keys()
|
||||
unit_type = extract_value(unit_types, filename, str(lines[0:500]))
|
||||
unit_name = extract_value(cfg.unit_names, filename, str(lines[0:500]))
|
||||
tool_name = extract_value(cfg.tool_names, filename, str(lines[0:500]))
|
||||
tool_type = extract_value(cfg.tool_types, filename, str(lines[0:500]))
|
||||
|
||||
conn = conn_db(cfg)
|
||||
|
||||
# Crea un cursore
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute("INSERT INTO received (filename, content) VALUES (%s,%s)" , (filename, lines))
|
||||
cur.execute("INSERT INTO received (filename, unit_name, unit_type, tool_name, tool_type, tool_data) VALUES (%s,%s,%s,%s,%s,%s)" , (filename, unit_name.upper(), unit_type.upper(), tool_name.upper(), tool_type.upper(), lines))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
except:
|
||||
|
||||
Reference in New Issue
Block a user