__version__ = '1.0.6'

This commit is contained in:
Sergey
2024-08-29 10:54:55 +03:00
parent 8e1624d898
commit 3ef9a6c819

35
app.py
View File

@@ -1,14 +1,15 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# __author__ = 'szhdanoff@gmail.com' # __author__ = 'szhdanoff@gmail.com'
__version__ = '1.0.5' __version__ = '1.0.6'
import os import os
import time import time
import csv import csv
import phonenumbers import phonenumbers
import apscheduler
# #
from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler import version as apscheduler_version
from email_validator import validate_email, EmailNotValidError from email_validator import validate_email, EmailNotValidError
from dotenv import load_dotenv from dotenv import load_dotenv
# local imports # local imports
@@ -66,12 +67,19 @@ def run_import():
if file_extension == '.csv': if file_extension == '.csv':
files.append(os.path.join(r, file)) files.append(os.path.join(r, file))
print('Scan .scv files: ', files)
for f in files: for f in files:
with open(f + f'-{time.strftime("%Y%m%d-%H%M%S", time.localtime())}.log', "w", encoding="utf-8") as log_file: log_file_name = f'{f}-{time.strftime("%Y%m%d-%H%M%S", time.localtime())}.log'
print('Log file: ', log_file_name)
with open(log_file_name, "w", encoding="utf-8") as log_file:
with open(f, "r", encoding="utf-8") as csv_file: with open(f, "r", encoding="utf-8") as csv_file:
file_name = os.path.basename(f) file_name = os.path.basename(f)
print('Importing', file_name) print('Importing', file_name)
# =====================================================
# USERS file # USERS file
# =====================================================
start_time = time.time() start_time = time.time()
if 'users' in file_name: if 'users' in file_name:
print(f'Processing "users" file: {f}') print(f'Processing "users" file: {f}')
@@ -86,7 +94,8 @@ def run_import():
print(f'Processing line {line_count}: {row}') print(f'Processing line {line_count}: {row}')
nickname, full_name, card, phone, email, gender = row nickname, full_name, card, phone, email, gender = row
print(nickname, full_name, card, phone, email, gender) print(nickname, full_name, card, phone, email, gender)
log_file.write(f'String: [{line_count}]- {nickname}, {full_name}, {card}, {phone}, {email}, {gender}\n') log_file.write(
f'String: [{line_count}]- {nickname}, {full_name}, {card}, {phone}, {email}, {gender}\n')
# strip whitespaces # strip whitespaces
nickname = nickname.strip() nickname = nickname.strip()
@@ -152,7 +161,8 @@ def run_import():
print('User created with', data['ID'], data['DIN']) print('User created with', data['ID'], data['DIN'])
log_file.write(f'Line: [{line_count}]- User created with: {data}\n') log_file.write(f'Line: [{line_count}]- User created with: {data}\n')
else: else:
log_file.write(f'error creating user in line: [{line_count}]- Invalid user data: {data}\n') log_file.write(
f'error creating user in line: [{line_count}]- Invalid user data: {data}\n')
else: else:
log_file.write(f'Line: [{line_count}]- User found with: {data}\n') log_file.write(f'Line: [{line_count}]- User found with: {data}\n')
print('User found with ', user_card, data) print('User found with ', user_card, data)
@@ -160,8 +170,9 @@ def run_import():
end_time = time.time() end_time = time.time()
print(f'Elapsed time of USERS file processing : {end_time - start_time} seconds') print(f'Elapsed time of USERS file processing : {end_time - start_time} seconds')
# =====================================================
# TRANSACTIONS files # TRANSACTIONS files
# =====================================================
start_time = time.time() start_time = time.time()
if 'transaction' in file_name: if 'transaction' in file_name:
print(f'Processing "transaction" file: {f}') print(f'Processing "transaction" file: {f}')
@@ -179,6 +190,7 @@ def run_import():
if card.strip() != '': if card.strip() != '':
user_found_by_card, din_id, _, _, _ = get_user(card, get_type='auto') user_found_by_card, din_id, _, _, _ = get_user(card, get_type='auto')
if not user_found_by_card: if not user_found_by_card:
print('----------------------------------------------------------------')
print(f'error in line: [{line_count}]- Invalid card: {card}') print(f'error in line: [{line_count}]- Invalid card: {card}')
log_file.write(f'error in line: [{line_count}]- Invalid card: {card}\n') log_file.write(f'error in line: [{line_count}]- Invalid card: {card}\n')
else: else:
@@ -242,9 +254,14 @@ scheduler = BackgroundScheduler()
scheduler.start() scheduler.start()
try: try:
job = scheduler.add_job(run_import, 'interval', minutes=1) job = scheduler.add_job(
print('Import running every 1 minute. Place *.csv files in "csv" directory') run_import, 'interval', minutes=1, max_instances=1, coalesce=True, misfire_grace_time=60
print('Press Ctrl+{0} to exit'.format('Break' if apscheduler.__version__ >= '3.0.0' else 'C')) )
print('#######################################################################')
print('# Import running every 1 minute. Place *.csv files in "csv" directory #')
print('#######################################################################')
print('Press Ctrl+{0} to exit'.format('Break' if apscheduler_version >= '3.0.0' else 'C'))
# This is here to simulate application activity (which keeps the main thread alive). # This is here to simulate application activity (which keeps the main thread alive).
while True: while True: