-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasc_db_setup.py
139 lines (117 loc) · 5.53 KB
/
asc_db_setup.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
137
138
## Copyright 2023 by Thomas Kocourek, N4FWD
import os
import sqlite3
import ve_utilities as ut
import global_var as gv
from tkinter import messagebox as mb
def setup():
## get the correct path for working directory
## get_environment() only sets up the path
## and returns it
basic_dir = ut.set_environment()
## Change to correct directory
try:
os.chdir(basic_dir)
except Exception:
txt = "basic dir: "+basic_dir+'\n'
print(txt)
log_it(txt)
#os.abort()
## set the flag
db_result_flag = True
#glaarg_update = False
## set path to databases
tmp_dir = os.path.join(basic_dir,gv.asc_dir)
gv.asc_database_path = os.path.join(tmp_dir,gv.asc_database_name)
gv.temp_database_path = os.path.join(tmp_dir,gv.temp_database_name)
gv.temp_fetch_path = os.path.join(tmp_dir,gv.ARRL_fetch_state)
if os.path.exists(gv.asc_database_path): ## Do we have an existing database?
## Yes.
## do a check if old style or new glaarg style
## if the database is old style or corrupted, then reset the database
##
## sqlite_master is the root definition holding the structure of the database
##
## In the next line, we are referencing the database root asking about a
## particular table structure
##
sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'glaarg_count'"
db_connection = sqlite3.connect(gv.asc_database_path)
db_cursor = db_connection.cursor()
db_cursor.execute(sql)
tested_value = db_cursor.fetchone()
if tested_value is None: ## No table labeled as 'glaarg_count'
## remove old table and remind user
os.remove(gv.asc_database_path)
mb.showinfo("Alert","Database has been flushed.\nExiting the ASC-DB program to rebuild the database.\nRemember to run Set Defaults.\n")
if os.path.exists(gv.temp_fetch_path):
os.remove(gv.temp_fetch_path)
db_result_flag = False
return db_result_flag
else:
## no existing database
try:
## create a new database
##
with open(gv.asc_database_path,mode="w"):
pass
db_connection = sqlite3.connect(gv.asc_database_path)
db_cursor = db_connection.cursor()
## will be populated by data from websites
sql = """
CREATE TABLE IF NOT EXISTS ve_count (id INTEGER PRIMARY KEY AUTOINCREMENT,
call TEXT NOT NULL UNIQUE, county TEXT NOT NULL, accredit TEXT NOT NULL, scount INTEGER NOT NULL, state TEXT NOT NULL, tag TEXT);
"""
#sql_result = db_cursor.execute(sql)
db_cursor.execute(sql)
sql = """
CREATE TABLE IF NOT EXISTS glaarg_count (id INTEGER PRIMARY KEY AUTOINCREMENT,
ve_num TEXT NOT NULL UNIQUE, csign TEXT NOT NULL, ve_name TEXT NOT NULL, sess_ct INTEGER NOT NULL, helped INTEGER NOT NULL, overseen INTEGER NOT NULL,
new_lic INTEGER NOT NULL, upgrades INTEGER NOT NULL, tag TEXT);
"""
#sql_result = db_cursor.execute(sql)
db_cursor.execute(sql)
## 'date' is for recording the last date the database was updated
## ditto for glaarg via 'gl_date'
##
## Add in entry for glaarg initial/update 'date'
##
sql = """
CREATE TABLE IF NOT EXISTS settings (id INTEGER PRIMARY KEY AUTOINCREMENT, yourcall TEXT ,
date TEXT , gl_date TEXT, defaultState TEXT, autoflag TEXT , cronMin TEXT , cronHr TEXT , cronDom TEXT , cronMon TEXT ,
cronDow TEXT);
"""
#sql_result = db_cursor.execute(sql)
db_cursor.execute(sql)
## commit both sql statements and create tables
db_connection.commit()
## Inserting default values into the 'settings' table
rec_cols = ', '.join(gv.settings_field_list_glaarg)
q_marks = ','.join(list('?'*len(gv.settings_field_list_glaarg)))
values = tuple(gv.settings_default_values_glaarg)
sql = "INSERT INTO settings ("+rec_cols+") VALUES ("+q_marks+")"
#sql_result = db_cursor.execute(sql,values)
db_cursor.execute(sql,values)
## commit insert
db_connection.commit()
db_cursor.execute("SELECT * FROM settings;")
#sql_records = db_cursor.fetchall()
db_cursor.fetchall()
except sqlite3.Error as er: ## in case of a SQL error, let's get more info
print('SQLite error: %s' % (' '.join(er.args)))
print("Exception class is: ", er.__class__)
#print('SQLite traceback: ')
#exc_type, exc_value, exc_tb = sys.exc_info()
#print(traceback.format_exception(exc_type, exc_value, exc_tb))
db_result_flag = False
## And close the new database
db_connection.close()
## whether we were successful or not, return a flag
return db_result_flag
def log_it(text): ## a quick logging dump
logging = os.path.join(gv.base_rpt_dir,"a_log.txt")
with open(logging,'a') as li:
## make sure we are dealing with a string
if not text.isalpha():
text = str(text)
li.write(text)