Skip to content

Commit e1f666c

Browse files
committed
Started work on persistent user data and settings
1 parent f4a303b commit e1f666c

1 file changed

Lines changed: 57 additions & 7 deletions

File tree

pypdfbuilder.py

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/python
22

33
import os
4+
import appdirs
5+
import json
46
from pathlib import Path as plPath
57
from operator import itemgetter
68
from settings import *
@@ -11,8 +13,57 @@
1113

1214
from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter
1315

16+
APPNAME = 'pypdfbuilder'
1417
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
1518
USER_DIR = str(plPath.home())
19+
CONFIG_DIR = appdirs.user_config_dir(APPNAME)
20+
DATA_DIR = appdirs.user_data_dir(APPNAME)
21+
22+
23+
class UserData:
24+
'''Class for storing current user's application data'''
25+
26+
def __init__(self):
27+
self.__user_data = {}
28+
self.__user_data_path = os.path.join(DATA_DIR, 'data.json')
29+
30+
@property
31+
def filedialog_path(self):
32+
'''The last directory the user visited while opening or saving a file
33+
using a Tk File Dialog.
34+
35+
The getter will first try to return the value stored in the state of the
36+
instance, then try to read it out of the user data file, and if all else fails,
37+
set it to the user's home directory and return that value.
38+
39+
The setter will set the according class instance property and save that property to
40+
a user data file. If no such file exists yet, one will be created.
41+
'''
42+
return self.__user_data.get('filedialog_path') or self.__get_user_data()['filedialog_path']
43+
44+
@filedialog_path.setter
45+
def filedialog_path(self, val):
46+
self.__user_data['filedialog_path'] = val
47+
self.__save_user_data()
48+
49+
def __get_user_data(self):
50+
try:
51+
with (open(self.__user_data_path, 'r')) as datafile:
52+
self.__user_data = json.load(datafile)
53+
except FileNotFoundError:
54+
self.filedialog_path=USER_DIR
55+
return self.__user_data
56+
57+
def __save_user_data(self):
58+
if not os.path.exists(os.path.dirname(self.__user_data_path)):
59+
plPath(os.path.dirname(self.__user_data_path)).mkdir(parents=True, exist_ok=True)
60+
try:
61+
with (open(self.__user_data_path, 'w')) as datafile:
62+
json.dump(self.__user_data, datafile)
63+
except FileNotFoundError:
64+
print('Something went horribly wrong while trying to save your current user data.')
65+
66+
1667

1768

1869
class PDFInfo:
@@ -146,7 +197,7 @@ def save_as(self):
146197
class SplitTabManager:
147198
'''Manager class for the Split Tab
148199
149-
The instance of this class manages all aspects of the split tab in the program.
200+
An instance of this class manages all aspects of the Split Tab in the calling `PyPDFBuilderApplication` instance
150201
151202
Args:
152203
parent (PyPDFBuilderApplication): Application that created the instance and that contains the Split Tab.
@@ -377,8 +428,7 @@ def __init__(self):
377428

378429
self.builder.connect_callbacks(self)
379430

380-
# Todo get pickled data: last directory visited by user
381-
self.__current_dir = USER_DIR
431+
self.__user_data = UserData()
382432

383433
self.jointab = JoinTabManager(self)
384434
self.splittab = SplitTabManager(self)
@@ -452,15 +502,15 @@ def rotatetab_save_as(self):
452502

453503
def get_file_dialog(self, func, widget_title='Choose File(s) …'):
454504
f = func(
455-
initialdir=self.__current_dir,
505+
initialdir=self.__user_data.filedialog_path,
456506
title=widget_title,
457507
filetypes=(("PDF File", "*.pdf"), ("All Files", "*.*"))
458508
)
459509
if f:
460-
if type(f) == list:
461-
self.__current_dir = os.path.dirname(f[-1])
510+
if type(f) == list or type(f) == tuple:
511+
self.__user_data.filedialog_path = os.path.dirname(f[-1])
462512
elif type(f) == str:
463-
self.__current_dir = os.path.dirname(f)
513+
self.__user_data.filedialog_path = os.path.dirname(f)
464514
return f
465515

466516
def quit(self, event=None):

0 commit comments

Comments
 (0)