fix issue about name for csv refactoring to use only one file and one directory
This commit is contained in:
parent
7f0b9c1d04
commit
53fbeda6d9
@ -482,20 +482,38 @@ def export_recipes(config, csv_path=None, logger=None):
|
|||||||
|
|
||||||
def backup_current_recipes(config, logger=None):
|
def backup_current_recipes(config, logger=None):
|
||||||
"""
|
"""
|
||||||
Back up current recipes to a timestamped CSV file in the predefined backup directory.
|
Back up current recipes to a CSV file in a single common folder. The file name equals the
|
||||||
|
[machine]/description from the active machine config (sanitized). Saving overwrites any
|
||||||
|
previous backup with the same machine description.
|
||||||
"""
|
"""
|
||||||
# Get the machine's hostname to use for the directory name
|
# Get machine description from config; fall back to hostname if missing
|
||||||
machine_name = socket.gethostname()
|
machine_desc = None
|
||||||
# Define the backup directory and file name
|
try:
|
||||||
backup_dir = os.path.join('config', 'csv_import', 'backup_csv', machine_name)
|
machine_desc = (config.get("machine", {}) or {}).get("description")
|
||||||
timestamp = datetime.now().strftime("%d%m%y%H%M%S")
|
except Exception:
|
||||||
backup_file = f"backup_{timestamp}.csv"
|
machine_desc = None
|
||||||
backup_path = os.path.join(backup_dir, backup_file)
|
if not machine_desc:
|
||||||
|
machine_desc = socket.gethostname()
|
||||||
|
|
||||||
|
# Sanitize description to be safe as a file name
|
||||||
|
safe_name = str(machine_desc).strip()
|
||||||
|
# Replace path separators and common forbidden characters on Windows/Unix
|
||||||
|
for ch in ['\\', '/', ':', '*', '?', '"', '<', '>', '|']:
|
||||||
|
safe_name = safe_name.replace(ch, '_')
|
||||||
|
# Also collapse consecutive spaces
|
||||||
|
safe_name = ' '.join(safe_name.split())
|
||||||
|
# Ensure .csv extension
|
||||||
|
if not safe_name.lower().endswith('.csv'):
|
||||||
|
safe_name = f"{safe_name}.csv"
|
||||||
|
|
||||||
|
# Define the single backup directory and file path
|
||||||
|
backup_dir = os.path.join('config', 'csv_import', 'backup_csv')
|
||||||
|
backup_path = os.path.join(backup_dir, safe_name)
|
||||||
|
|
||||||
# Ensure the backup directory exists
|
# Ensure the backup directory exists
|
||||||
os.makedirs(backup_dir, exist_ok=True)
|
os.makedirs(backup_dir, exist_ok=True)
|
||||||
|
|
||||||
# Export current recipes to the backup path
|
# Export current recipes to the backup path (export_recipes overwrites the file)
|
||||||
export_recipes(config=config, csv_path=backup_path, logger=logger)
|
export_recipes(config=config, csv_path=backup_path, logger=logger)
|
||||||
|
|
||||||
if logger:
|
if logger:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user