-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
136 lines (111 loc) · 4.79 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# database.py
import os
import random
import string
import datetime
import openpyxl
import pandas as pd
from pesel_utils import (
validate,
is_valid,
rotate_identifier,
date_to_letters,
format_pesel,
compact,
)
from logger import log_error, log_summary
# Database file path
db_file_path = 'database/pesel_results.txt'
# Add a set to keep track of generated PESEL numbers
generated_pesels = set()
# Function to check if an ID already exists in the database
def is_id_in_database(id_code):
try:
with open(db_file_path, 'r') as file:
for line in file:
if id_code in line:
return True
except FileNotFoundError:
# Handle the case where the database file does not exist
print("Database file not found.")
return False
# Function to get the existing ID from the database for a given PESEL
def get_existing_id_from_database(pesel):
if not os.path.exists(db_file_path):
return None
pesel = compact(pesel)
with open(db_file_path, 'r') as file:
for line in file:
parts = line.strip().split(": ")
if len(parts) == 2 and parts[0] == "PESEL" and compact(parts[1]) == pesel:
return parts[1]
# Function to check if a PESEL exists in the database
def is_pesel_in_database(pesel):
try:
with open(db_file_path, 'r') as file:
for line in file:
if f"PESEL: {pesel}:" in line:
return True
except FileNotFoundError:
# Handle the case where the database file does not exist
print("Database file not found.")
return False
# Function to add an entry to the database
def add_entry_to_database(identifier_name, identifier, final_id):
with open(db_file_path, 'a') as file:
file.write(f"{identifier_name}: {identifier}: {final_id}\n")
# Placeholder for import_pesel_from_excel function
def import_pesel_from_excel(excel_file_path):
# Implement your logic for importing PESEL numbers from Excel here
global log_file_path
log_file_path = datetime.datetime.now().strftime("%Y_%m_%d") + "_Logs.txt"
if not os.path.exists(excel_file_path):
print("Excel file not found.")
return
imported_count = 0
error_count = 0
df = pd.read_excel(excel_file_path, header=None)
for index, row in df.iterrows():
if index == 0:
if df.shape[1] < 4:
df.loc[index, 3] = "Comment"
continue
pesel = str(row[1])
if not pesel.isdigit() or len(pesel) != 11:
print(f"Error: Invalid PESEL number: {pesel}")
log_error(f"Invalid PESEL number: {pesel}")
error_count += 1
df.loc[index, 2] = "ERROR"
df.loc[index, 3] = "Invalid PESEL number"
continue
existing_id = get_existing_id_from_database(pesel)
if existing_id:
print(f"PESEL {pesel} already has an ID code in the database.")
df.loc[index, 2] = existing_id # Update column C with the existing ID code
df.loc[index, 3] = "Already Exists in Database"
else:
rotated_pesel = rotate_identifier(pesel)
date_letters = date_to_letters()
formatted_identifier = format_pesel(rotated_pesel)
final_id = date_letters + formatted_identifier + str(random.randint(0, 9))
# Check if the pesel already exists in the database or in generated_pesels set
if is_pesel_in_database(pesel) or pesel in generated_pesels:
print(f"PESEL {pesel} already exists in the database or has been generated before.")
df.loc[index, 2] = final_id # Update column C with the generated ID
df.loc[index, 3] = "Already Exists in Database"
else:
with open(db_file_path, 'a') as file:
file.write(f"PESEL: {pesel}: {final_id}\n")
print(f"PESEL {pesel} successfully created and saved in the database. ID code: {final_id}")
imported_count += 1
df.loc[index, 2] = final_id
df.loc[index, 3] = "Create and save in the database"
# Add the generated PESEL to the set
generated_pesels.add(pesel)
#result_excel_file_path = "../load_pesel_excel_file.xlsx"
# Update the file path for the result Excel file
result_excel_file_path = "load_pesel_excel_file.xlsx"
with pd.ExcelWriter(result_excel_file_path, engine='openpyxl') as writer:
df.to_excel(writer, sheet_name='Arkusz1', header=False, index=False)
print("Validation results added to the Excel file.")
log_summary(imported_count, error_count)