From 5d377c822c82da3a5f2c620c52310970e256bc6b Mon Sep 17 00:00:00 2001 From: "vladimir.buzalka" Date: Sat, 4 Oct 2025 12:54:54 +0200 Subject: [PATCH] Z230 --- FunkceWhereIsDropbox.py | 54 +++++++++++++++++++++++++++++++++++++++++ PDF - doplnění jména.py | 13 +++++----- s03soubory.py | 33 ++++++++++++++++--------- 3 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 FunkceWhereIsDropbox.py diff --git a/FunkceWhereIsDropbox.py b/FunkceWhereIsDropbox.py new file mode 100644 index 0000000..6f60ecf --- /dev/null +++ b/FunkceWhereIsDropbox.py @@ -0,0 +1,54 @@ +from pathlib import Path +import json, os, socket,fdb + +def get_dropbox_path() -> Path: + r""" + Return the absolute Dropbox folder path. + Works across your computers (U:, Z:, etc.). + Always reads from LOCALAPPDATA\Dropbox\info.json. + Falls back to searching for a folder literally named 'Dropbox'. + """ + info_path = Path(os.environ["LOCALAPPDATA"]) / "Dropbox" / "info.json" + if info_path.exists(): + try: + with open(info_path, encoding="utf-8") as f: + info = json.load(f) + return Path(info["personal"]["path"]) + except Exception as e: + print(f"⚠️ Could not read Dropbox info.json: {e}") + + # fallback if JSON missing + for drive in "CDEFGHIJKLMNOPQRSTUVWXYZ": + candidate = Path(f"{drive}:\\Dropbox") + if candidate.exists(): + return candidate + + raise FileNotFoundError("Dropbox folder not found on any drive.") + +print(get_dropbox_path()) + + +def get_medicus_connection(): + """ + Connect to Firebird 'medicus.fdb' depending on computer name. + Returns fdb.Connection or raises RuntimeError if unknown or connection fails. + """ + computer_name = socket.gethostname().upper() + try: + if computer_name == "Z230": + print("Computer name is Z230") + return fdb.connect(dsn=r"localhost:c:\medicus 3\data\medicus.fdb", user="SYSDBA", password="masterkey", charset="win1250") + elif computer_name == "LEKAR": + print("Computer name is SESTRALEKAR") + return fdb.connect(dsn=r"localhost:m:\medicus\data\medicus.fdb", user="SYSDBA", password="masterkey", charset="win1250") + elif computer_name in ("SESTRA", "POHODA"): + print("Computer name is SESTRA or POHODA") + return fdb.connect(dsn=r"192.168.1.40:m:\medicus\data\medicus.fdb", user="SYSDBA", password="masterkey", charset="win1250") + else: + raise RuntimeError(f"❌ Unknown computer name: {computer_name}") + + except Exception as e: + print(f"⚠️ Error connecting to Medicus on {computer_name}: {e}") + raise + +print(get_medicus_connection()) diff --git a/PDF - doplnění jména.py b/PDF - doplnění jména.py index ad7c412..73dcfdf 100644 --- a/PDF - doplnění jména.py +++ b/PDF - doplnění jména.py @@ -1,5 +1,6 @@ import os,re,fdb,time import logging +from FunkceWhereIsDropbox import get_dropbox_path,get_medicus_connection logging.basicConfig(level=logging.DEBUG) @@ -16,14 +17,12 @@ def set_single_page_view (filepdfin): # Connect to the Firebird database -conn = fdb.connect( - dsn=r'192.168.1.10:m:\medicus\data\medicus.fdb', # Database path - user='SYSDBA', # Username - password="masterkey", # Password, - charset="win1250") -# cur = conn.cursor() +conn = get_medicus_connection() -cesta=r"z:\dropbox\ordinace\Dokumentace_ke_zpracování" +#Get cesta to Dropbox on working computer +dropbox=get_dropbox_path() +cesta=dropbox/"Ordinace"/"Dokumentace_ke_zpracování" +cestazpracovana=dropbox/"Ordinace"/"Dokumentace_zpracovaná" for file in os.listdir(cesta): if file.upper().endswith((".PDF")) and os.path.isfile(os.path.join(cesta,file)): diff --git a/s03soubory.py b/s03soubory.py index 94182a3..677caa8 100644 --- a/s03soubory.py +++ b/s03soubory.py @@ -1,17 +1,25 @@ import os,shutil,fdb,time import re,datetime,funkce +from FunkceWhereIsDropbox import get_dropbox_path,get_medicus_connection # Connect to the Firebird database -conn = fdb.connect( - dsn=r'localhost:u:\medicus 3\data\medicus.fdb', # Database path - user='SYSDBA', # Username - password="masterkey", # Password, - charset="win1250") +conn = get_medicus_connection() -# cesta=r"u:\Dropbox\!!!Days\Downloads Z230\Dokumentace" -cesta=r"u:\NextcloudOrdinace\Dokumentace_ke_zpracování" -# cestazpracovana=r"u:\Dropbox\!!!Days\Downloads Z230\Dokument" -cestazpracovana=r"z:\NextcloudOrdinace\Dokumentace_zpracovaná" +#Get cesta to Dropbox on working computer +dropbox=get_dropbox_path() +# Convert to Windows-style strings (backslashes) +cesta = (dropbox / "Ordinace" / "Dokumentace_ke_zpracování").resolve().as_posix().replace('/', '\\') +cestazpracovana = (dropbox / "Ordinace" / "Dokumentace_zpracovaná").resolve().as_posix().replace('/', '\\') + + +print(conn) +print(dropbox) +print(cesta) +print(cestazpracovana) +# exit(0) +# cesta=r"u:\NextcloudOrdinace\Dokumentace_ke_zpracování" +# # cestazpracovana=r"u:\Dropbox\!!!Days\Downloads Z230\Dokument" +# cestazpracovana=r"z:\NextcloudOrdinace\Dokumentace_zpracovaná" def restore_files_for_import(retezec): @@ -169,13 +177,14 @@ BOOKMARKSTEXT pacid=row[1] filename=row[6] - + Problem_s_ulozenim=True for attempt in range(3): try: # Replace this with the command that might raise an error if not os.path.exists(os.path.join(cestazpracovana,row[6])): shutil.move(os.path.join(cesta,row[6]), os.path.join(cestazpracovana,row[6])) print("Command succeeded!") + Problem_s_ulozenim = False break # Exit the loop if the command succeeds else: now = datetime.datetime.now() @@ -183,6 +192,7 @@ BOOKMARKSTEXT print(os.path.join(cestazpracovana,row[6][:-4]+" "+datetime_string+".pdf")) shutil.move(os.path.join(cesta,row[6]),os.path.join(cestazpracovana,row[6][:-4]+" "+datetime_string+".pdf")) print("Command succeeded!") + Problem_s_ulozenim = False break # Exit the loop if the command succeeds except Exception as e: print(f"Attempt {attempt + 1} failed: {e}") @@ -195,10 +205,11 @@ BOOKMARKSTEXT if Problem_s_ulozenim: print("Problem s ulozenim",row[6]) + exit(0) break fileid=funkce.zapis_file(vstupconnection=conn, idpac=row[1], - cesta=cesta, souborname=row[6], prvnizavorka=row[4], + cesta=cestazpracovana, souborname=row[6], prvnizavorka=row[4], soubordate=row[2], souborfiledate=row[7], poznamka=row[5]) filename=funkce.convert_to1250(filename)