From 3ef9a6c81990abb3471d00c54c013f586e642ac0 Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 29 Aug 2024 10:54:55 +0300 Subject: [PATCH] __version__ = '1.0.6' --- app.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 8e1586f..ed070f6 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,15 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # __author__ = 'szhdanoff@gmail.com' -__version__ = '1.0.5' +__version__ = '1.0.6' + import os import time import csv import phonenumbers -import apscheduler # from apscheduler.schedulers.background import BackgroundScheduler +from apscheduler import version as apscheduler_version from email_validator import validate_email, EmailNotValidError from dotenv import load_dotenv # local imports @@ -66,12 +67,19 @@ def run_import(): if file_extension == '.csv': files.append(os.path.join(r, file)) + print('Scan .scv files: ', 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: file_name = os.path.basename(f) print('Importing', file_name) + # ===================================================== # USERS file + # ===================================================== start_time = time.time() if 'users' in file_name: print(f'Processing "users" file: {f}') @@ -86,7 +94,8 @@ def run_import(): print(f'Processing line {line_count}: {row}') nickname, full_name, card, phone, email, gender = row 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 nickname = nickname.strip() @@ -152,16 +161,18 @@ def run_import(): print('User created with', data['ID'], data['DIN']) log_file.write(f'Line: [{line_count}]- User created with: {data}\n') 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: 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) # print('User found with', data['DIN'], user_card) end_time = time.time() print(f'Elapsed time of USERS file processing : {end_time - start_time} seconds') - + # ===================================================== # TRANSACTIONS files + # ===================================================== start_time = time.time() if 'transaction' in file_name: print(f'Processing "transaction" file: {f}') @@ -179,6 +190,7 @@ def run_import(): if card.strip() != '': user_found_by_card, din_id, _, _, _ = get_user(card, get_type='auto') if not user_found_by_card: + print('----------------------------------------------------------------') print(f'error in line: [{line_count}]- Invalid card: {card}') log_file.write(f'error in line: [{line_count}]- Invalid card: {card}\n') else: @@ -242,9 +254,14 @@ scheduler = BackgroundScheduler() scheduler.start() try: - job = scheduler.add_job(run_import, 'interval', minutes=1) - print('Import running every 1 minute. Place *.csv files in "csv" directory') - print('Press Ctrl+{0} to exit'.format('Break' if apscheduler.__version__ >= '3.0.0' else 'C')) + job = scheduler.add_job( + run_import, 'interval', minutes=1, max_instances=1, coalesce=True, misfire_grace_time=60 + ) + + 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). while True: