def __init__(self, controller):
"""Initialize Report class.
Args:
controller (Controller): controller object which will
store all the data required by each segment of the
application.
:Attributes:
:v_link (dict): Link to access variables in controller.
:conn (sqlite3.Connection): Database object
:cur (sqlite3.Cursor): Database cursor.
:data_dirs (dict[str, str]): Data directories for the
application.
"""
self.controller = controller
self.v_link = self.controller.app_data
""":type : dict[str, any]"""
# Create necessary application folders in
# C:\Users\User\AppData\Local
user_dirs = appdirs.AppDirs("Weather_App", "")
local_app_dir = user_dirs.user_data_dir
self.data_dirs = {"Database": "",
"Debug": ""}
for sub_dir in self.data_dirs:
path = os.path.join(local_app_dir, sub_dir)
self.data_dirs[sub_dir] = path
os.makedirs(path, exist_ok=True)
# Establish database connection.
self.conn = sqlite3.connect(os.path.join(self.data_dirs["Database"],
"locations.db"))
self.cur = self.conn.cursor()
self.cur.execute("CREATE TABLE IF NOT EXISTS locations("
"Id INTEGER NOT NULL PRIMARY KEY , "
"Location TEXT NOT NULL UNIQUE, "
"Num_of_calls INTEGER NOT NULL DEFAULT 0, "
"Units TEXT)")
# Read the last used units from the database and set in
# controller.
try:
self.cur.execute("SELECT Units FROM locations WHERE Id=1")
last_units = self.cur.fetchall()[0][0]
if last_units != "":
self.v_link["var_units"].set(last_units)
# In case of the initial run with an empty database, default
# units are metric.
except IndexError:
self.v_link["var_units"].set("metric")
self.conn.commit()
# Initial list of locations from previous use of the app for
# loc_combobox ordered by amount of previous calls.
self.combo_drop_menu()
评论列表
文章目录