Permetre executar dry runs al table-parser
Ara es pot executar el table-parser per fer proves amb l'opciĆ³ --dryrun
sense que tot el parsejat s'afegeixi a la base de dades.
diff --git a/cron-parse-tables.py b/cron-parse-tables.py
index 7ed1dee..f98816a 100644
--- a/cron-parse-tables.py
+++ b/cron-parse-tables.py
@@ -9,47 +9,59 @@
config = configparser.ConfigParser()
config.read('config.ini')
- db_host = config['db']['host']
- db_database = config['db']['database']
- db_user = config['db']['user']
- db_password = config['db']['password']
-
- connection_string = (
- 'DRIVER=MySQL ODBC 8.0 ANSI Driver;'
- 'SERVER=' + db_host + ';'
- 'DATABASE=' + db_database + ';'
- 'UID=' + db_user + ';'
- 'PWD=' + db_password + ';'
- 'charset=utf8mb4;'
- )
-
- db = pyodbc.connect(connection_string)
- db.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
- db.setencoding(encoding='utf-8')
+ loadDB = True
try:
- opts, args = getopt.getopt(argv, 'o:', ['offset='])
+ opts, args = getopt.getopt(argv, 'o:d', ['offset=', 'dryrun'])
except getopt.GetoptError:
print('Usage: python cron-parse-tables.py -o <offset_in_days>')
print(' The offset is the day relative to today that wants to be')
print(' parsed. For instance, -1 is yesterday and 1 is tomorrow.')
+ print('')
+ print('You can also use the option -d to perform a dry run (doesn\'t')
+ print('save anything to the database)')
sys.exit(2)
offset = 1
for opt, arg in opts:
if opt in ('-o', '--offset'):
offset = arg
+ elif opt in ('-d', '--dryrun'):
+ loadDB = False
else:
print('Parameter \'' + opt + '\' not recognized.')
print('')
print('Run python cron-parse-tables.py to get help on how to use')
print('this script.')
+ db = None
+
+ if loadDB:
+ db_host = config['db']['host']
+ db_database = config['db']['database']
+ db_user = config['db']['user']
+ db_password = config['db']['password']
+
+ connection_string = (
+ 'DRIVER=MySQL ODBC 8.0 ANSI Driver;'
+ 'SERVER=' + db_host + ';'
+ 'DATABASE=' + db_database + ';'
+ 'UID=' + db_user + ';'
+ 'PWD=' + db_password + ';'
+ 'charset=utf8mb4;'
+ )
+
+ db = pyodbc.connect(connection_string)
+ db.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
+ db.setencoding(encoding='utf-8')
+
offset = int(offset)
print('Parsejant les classes ' +
(('de fa ' if offset < 0 else 'de dins de ') +
str(abs(offset)) + (' dia' if abs(offset) == 1 else ' dies') if offset != 0 else 'd\'avui'))
+ if not loadDB:
+ print('NOTA: Dry run')
tomorrow = datetime.date.today() + datetime.timedelta(days=offset)