refactory
This commit is contained in:
@@ -2,9 +2,8 @@
|
||||
|
||||
"""
|
||||
from configparser import ConfigParser
|
||||
import json
|
||||
|
||||
class config:
|
||||
class Config:
|
||||
def __init__(self):
|
||||
c = ConfigParser()
|
||||
c.read(["/etc/aseftp/ftpcsvreceiver.ini", "./ftpcsvreceiver.ini",
|
||||
12
utils/config/parser.py
Normal file
12
utils/config/parser.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import re
|
||||
|
||||
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
|
||||
@@ -3,7 +3,7 @@ import mysql.connector
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def conn_db(cfg):
|
||||
def connetti_db(cfg):
|
||||
"""Establishes a connection to the MySQL database.
|
||||
|
||||
Args:
|
||||
@@ -15,6 +15,7 @@ def conn_db(cfg):
|
||||
try:
|
||||
conn = mysql.connector.connect(user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport)
|
||||
conn.autocommit = True
|
||||
logger.info("Connected")
|
||||
return conn
|
||||
except mysql.connector.Error as e:
|
||||
logger.error(f'{e}')
|
||||
96
utils/database/get_nodes_type.py
Normal file
96
utils/database/get_nodes_type.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import logging
|
||||
import datetime
|
||||
import os
|
||||
import mysql.connector
|
||||
|
||||
from utils.database.connection import connetti_db
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def get_timestamp(log_type):
|
||||
"""Generates a timestamp string for logging."""
|
||||
now = datetime.datetime.now()
|
||||
return now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
def get_nodes_type(db_lar, server, username, password, tool, unit, channels, nodetype, ain, din):
|
||||
"""
|
||||
Retrieves node type, Ain, Din, and channels from the database for a specific tool and unit.
|
||||
|
||||
Args:
|
||||
db_lar (str): The name of the MySQL database.
|
||||
server (str): The hostname or IP address of the MySQL server.
|
||||
username (str): The MySQL username.
|
||||
password (str): The MySQL password.
|
||||
tool (str): The name of the tool.
|
||||
unit (str): The name of the unit.
|
||||
channels (list): An empty list to store the 'channels' values.
|
||||
nodetype (list): An empty list to store the 'type' values.
|
||||
ain (list): An empty list to store the 'ain' values.
|
||||
din (list): An empty list to store the 'din' values.
|
||||
"""
|
||||
|
||||
|
||||
try:
|
||||
conn = connetti_db(cfg)
|
||||
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
query = f"""
|
||||
SELECT t.name AS name, n.seq AS seq, n.num AS num, n.channels AS channels, y.type AS type, n.ain AS ain, n.din AS din
|
||||
FROM nodes AS n
|
||||
INNER JOIN tools AS t ON t.id = n.tool_id
|
||||
INNER JOIN units AS u ON u.id = t.unit_id
|
||||
INNER JOIN nodetypes AS y ON n.nodetype_id = y.id
|
||||
WHERE y.type NOT IN ('Anchor Link', 'None') AND t.name = '{tool}' AND u.name = '{unit}'
|
||||
ORDER BY n.num;
|
||||
"""
|
||||
|
||||
cursor.execute(query)
|
||||
results = cursor.fetchall()
|
||||
|
||||
print(f"{get_timestamp('log')} - pid {os.getpid()} >> {unit} - {tool}: {cursor.rowcount} rows selected to get node type/Ain/Din/channels.")
|
||||
|
||||
if not results:
|
||||
print(f"{get_timestamp('log')} - pid {os.getpid()} >> Node/Channels/Ain/Din not defined.")
|
||||
print(f"{get_timestamp('log')} - pid {os.getpid()} >> Execution ended.")
|
||||
exit()
|
||||
else:
|
||||
for row in results:
|
||||
channels.append(row['channels'])
|
||||
nodetype.append(row['type'])
|
||||
ain.append(row['ain'])
|
||||
din.append(row['din'])
|
||||
|
||||
cursor.close()
|
||||
dbh.close()
|
||||
|
||||
except mysql.connector.Error as err:
|
||||
error_message = f"{get_timestamp('log')} - pid {os.getpid()} >> Could not connect to database: {err}"
|
||||
print(error_message)
|
||||
raise # Re-raise the exception if you want the calling code to handle it
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Example usage (replace with your actual database credentials and values)
|
||||
db_name = "your_database_name"
|
||||
db_server = "your_server_address"
|
||||
db_user = "your_username"
|
||||
db_password = "your_password"
|
||||
current_tool = "your_tool_name"
|
||||
current_unit = "your_unit_name"
|
||||
|
||||
node_channels = []
|
||||
node_types = []
|
||||
node_ains = []
|
||||
node_dins = []
|
||||
|
||||
try:
|
||||
get_nodes_type(db_name, db_server, db_user, db_password, current_tool, current_unit, node_channels, node_types, node_ains, node_dins)
|
||||
|
||||
print("\nRetrieved Data:")
|
||||
print(f"Channels: {node_channels}")
|
||||
print(f"Node Types: {node_types}")
|
||||
print(f"Ains: {node_ains}")
|
||||
print(f"Dins: {node_dins}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
52
utils/ftp/file_management.py
Normal file
52
utils/ftp/file_management.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import os
|
||||
import logging
|
||||
|
||||
import mysql.connector
|
||||
|
||||
from utils.database.connection import connetti_db
|
||||
|
||||
from utils.config.parser import extract_value
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def on_file_received(self, file):
|
||||
"""Handles the event when a file is successfully received.
|
||||
|
||||
Args:
|
||||
file: The path to the received file.
|
||||
"""
|
||||
if not os.stat(file).st_size:
|
||||
os.remove(file)
|
||||
logging.info(f'File {file} is empty: removed.')
|
||||
else:
|
||||
cfg = self.cfg
|
||||
path, filenameExt = os.path.split(file)
|
||||
filename, fileExtension = os.path.splitext(filenameExt)
|
||||
if (fileExtension.upper() in (cfg.fileext)):
|
||||
with open(file, 'r') as csvfile:
|
||||
lines = csvfile.readlines()
|
||||
|
||||
unit_name = extract_value(cfg.units_name, filename, str(lines[0:9]))
|
||||
unit_type = extract_value(cfg.units_type, filename, str(lines[0:9]))
|
||||
tool_name = extract_value(cfg.tools_name, filename, str(lines[0:9]))
|
||||
tool_type = extract_value(cfg.tools_type, filename, str(lines[0:9]))
|
||||
|
||||
try:
|
||||
conn = connetti_db(cfg)
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error: {e}")
|
||||
logging.error(f'{e}')
|
||||
|
||||
# Create a cursor
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute(f"INSERT INTO {cfg.dbname}.{cfg.dbrectable} (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(), ''.join(lines)))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f'File {file} not loaded. Held in user path.')
|
||||
logging.error(f'{e}')
|
||||
else:
|
||||
os.remove(file)
|
||||
logging.info(f'File {file} loaded: removed.')
|
||||
140
utils/ftp/user_admin.py
Normal file
140
utils/ftp/user_admin.py
Normal file
@@ -0,0 +1,140 @@
|
||||
import os
|
||||
import mysql.connector
|
||||
import logging
|
||||
|
||||
from hashlib import sha256
|
||||
from pathlib import Path
|
||||
|
||||
from utils.database.connection import connetti_db
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def ftp_SITE_ADDU(self, line):
|
||||
"""Adds a virtual user, creates their directory, and saves their details to the database.
|
||||
"""
|
||||
cfg = self.cfg
|
||||
try:
|
||||
parms = line.split()
|
||||
user = os.path.basename(parms[0]) # Extract the username
|
||||
password = parms[1] # Get the password
|
||||
hash = sha256(password.encode("UTF-8")).hexdigest() # Hash the password
|
||||
except IndexError:
|
||||
self.respond('501 SITE ADDU failed. Command needs 2 arguments')
|
||||
else:
|
||||
try:
|
||||
# Create the user's directory
|
||||
Path(cfg.virtpath + user).mkdir(parents=True, exist_ok=True)
|
||||
except Exception as e:
|
||||
self.respond(f'551 Error in create virtual user path: {e}')
|
||||
else:
|
||||
try:
|
||||
# Add the user to the authorizer
|
||||
self.authorizer.add_user(str(user),
|
||||
hash, cfg.virtpath + "/" + user, perm=cfg.defperm)
|
||||
# Save the user to the database
|
||||
# Define the database connection
|
||||
try:
|
||||
conn = connetti_db(cfg)
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error: {e}")
|
||||
logging.error(f'{e}')
|
||||
|
||||
# Create a cursor
|
||||
cur = conn.cursor()
|
||||
cur.execute(f"INSERT INTO {cfg.dbname}.{cfg.dbusertable} (ftpuser, hash, virtpath, perm) VALUES ('{user}', '{hash}', '{cfg.virtpath + user}', '{cfg.defperm}')")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
logging.info(f"User {user} created.")
|
||||
self.respond('200 SITE ADDU successful.')
|
||||
except Exception as e:
|
||||
self.respond(f'501 SITE ADDU failed: {e}.')
|
||||
print(e)
|
||||
|
||||
def ftp_SITE_DISU(self, line):
|
||||
"""Removes a virtual user from the authorizer and marks them as deleted in the database."""
|
||||
cfg = self.cfg
|
||||
parms = line.split()
|
||||
user = os.path.basename(parms[0]) # Extract the username
|
||||
try:
|
||||
# Remove the user from the authorizer
|
||||
self.authorizer.remove_user(str(user))
|
||||
# Delete the user from database
|
||||
try:
|
||||
conn = connetti_db(cfg)
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error: {e}")
|
||||
logging.error(f'{e}')
|
||||
|
||||
# Crea un cursore
|
||||
cur = conn.cursor()
|
||||
cur.execute(f"UPDATE {cfg.dbname}.{cfg.dbusertable} SET disabled_at = now() WHERE ftpuser = '{user}'")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
logging.info(f"User {user} deleted.")
|
||||
self.respond('200 SITE DISU successful.')
|
||||
except Exception as e:
|
||||
self.respond('501 SITE DISU failed.')
|
||||
print(e)
|
||||
|
||||
def ftp_SITE_ENAU(self, line):
|
||||
"""Restores a virtual user by updating their status in the database and adding them back to the authorizer."""
|
||||
cfg = self.cfg
|
||||
parms = line.split()
|
||||
user = os.path.basename(parms[0]) # Extract the username
|
||||
try:
|
||||
# Restore the user into database
|
||||
try:
|
||||
conn = connetti_db(cfg)
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error: {e}")
|
||||
logging.error(f'{e}')
|
||||
|
||||
# Crea un cursore
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute(f"UPDATE {cfg.dbname}.{cfg.dbusertable} SET disabled_at = null WHERE ftpuser = '{user}'")
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
logging.error(f"Update DB failed: {e}")
|
||||
|
||||
cur.execute(f"SELECT ftpuser, hash, virtpath, perm FROM {cfg.dbname}.{cfg.dbusertable} WHERE ftpuser = '{user}'")
|
||||
|
||||
ftpuser, hash, virtpath, perm = cur.fetchone()
|
||||
self.authorizer.add_user(ftpuser, hash, virtpath, perm)
|
||||
try:
|
||||
Path(cfg.virtpath + ftpuser).mkdir(parents=True, exist_ok=True)
|
||||
except Exception as e:
|
||||
self.responde(f'551 Error in create virtual user path: {e}')
|
||||
|
||||
conn.close()
|
||||
|
||||
logging.info(f"User {user} restored.")
|
||||
self.respond('200 SITE ENAU successful.')
|
||||
|
||||
except Exception as e:
|
||||
self.respond('501 SITE ENAU failed.')
|
||||
print(e)
|
||||
|
||||
def ftp_SITE_LSTU(self, line):
|
||||
"""Lists all virtual users from the database."""
|
||||
cfg = self.cfg
|
||||
users_list = []
|
||||
try:
|
||||
# Connect to the SQLite database to fetch users
|
||||
try:
|
||||
conn = connetti_db(cfg)
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error: {e}")
|
||||
logging.error(f'{e}')
|
||||
|
||||
# Crea un cursore
|
||||
cur = conn.cursor()
|
||||
self.push("214-The following virtual users are defined:\r\n")
|
||||
cur.execute(f'SELECT ftpuser, perm, disabled_at FROM {cfg.dbname}.{cfg.dbusertable}')
|
||||
[users_list.append(f'Username: {ftpuser}\tPerms: {perm}\tDisabled: {disabled_at}\r\n') for ftpuser, perm, disabled_at in cur.fetchall()]
|
||||
self.push(''.join(users_list))
|
||||
self.respond("214 LSTU SITE command successful.")
|
||||
|
||||
except Exception as e:
|
||||
self.respond(f'501 list users failed: {e}')
|
||||
@@ -1 +0,0 @@
|
||||
locals
|
||||
3
utils/parsers/g801_mums.py
Normal file
3
utils/parsers/g801_mums.py
Normal file
@@ -0,0 +1,3 @@
|
||||
def chi_sono(unit, tool):
|
||||
print(f'g801_mums: {unit} - {tool}')
|
||||
return f'g801_mums: {unit} - {tool}'
|
||||
3
utils/parsers/g801_mux.py
Normal file
3
utils/parsers/g801_mux.py
Normal file
@@ -0,0 +1,3 @@
|
||||
def chi_sono(unit, tool):
|
||||
print(f'g801_mux: {unit} - {tool}')
|
||||
return f'g801_mux: {unit} - {tool}'
|
||||
@@ -1 +0,0 @@
|
||||
"""Utilità per i formati timestamp"""
|
||||
@@ -1,28 +0,0 @@
|
||||
"""Funzioni per formato data
|
||||
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from re import search
|
||||
|
||||
|
||||
def dateFmt(date):
|
||||
t = date.replace("/", "-")
|
||||
if search('^\d\d\d\d-\d\d-\d\d$', t):
|
||||
d = datetime.strptime(t, "%Y-%m-%d")
|
||||
elif search('^\d\d-\d\d-\d\d$', t):
|
||||
d = datetime.strptime(t, "%y-%m-%d")
|
||||
elif search('^\d\d-\d\d-\d\d\d\d$', t):
|
||||
d = datetime.strptime(t, "%d-%m-%Y")
|
||||
return datetime.strftime(d, "%Y-%m-%d")
|
||||
|
||||
|
||||
def dateTimeFmt(date):
|
||||
t = date.replace("/", "-")
|
||||
if search('^\d\d\d\d-\d\d-\d\d$', t):
|
||||
d = datetime.strptime(t, "%Y-%m-%d %H:%M:%S")
|
||||
elif search('^\d\d-\d\d-\d\d$', t):
|
||||
d = datetime.strptime(t, "%y-%m-%d %H:%M:%S")
|
||||
elif search('^\d\d-\d\d-\d\d\d\d$', t):
|
||||
d = datetime.strptime(t, "%d-%m-%Y %H:%M:%S")
|
||||
return datetime.strftime(d, "%Y-%m-%d")
|
||||
@@ -1,25 +0,0 @@
|
||||
"""Funzioni per convertire formato data
|
||||
|
||||
"""
|
||||
|
||||
import datetime
|
||||
|
||||
|
||||
def dateFmt(date):
|
||||
t = date.replace("/", "-")
|
||||
try:
|
||||
datetime.datetime.strptime(t, "%Y-%m-%d")
|
||||
return t
|
||||
except ValueError:
|
||||
d = datetime.datetime.strptime(t, "%d-%m-%Y")
|
||||
return datetime.datetime.strftime(d, "%Y-%m-%d")
|
||||
|
||||
|
||||
def dateTimeFmt(date):
|
||||
t = date.replace("/", "-")
|
||||
try:
|
||||
datetime.datetime.strptime(t, "%Y-%m-%d %H:%M:%S")
|
||||
return t
|
||||
except ValueError:
|
||||
d = datetime.datetime.strptime(t, "%d-%m-%Y %H:%M:%S")
|
||||
return datetime.datetime.strftime(d, "%Y-%m-%d %H:%M:%S")
|
||||
@@ -1,10 +0,0 @@
|
||||
"""Funzioni per timestamp
|
||||
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def timestamp(t):
|
||||
fmt = {"log": "%Y-%m-%d %H:%M:%S", "tms": "%Y%m%d%H%M%S"}
|
||||
return datetime.now().strftime(fmt[t])
|
||||
0
utils/timestamp/__init__.py
Normal file
0
utils/timestamp/__init__.py
Normal file
Reference in New Issue
Block a user