Skip to content

Commit 53047ae

Browse files
committed
Finished persistent user data feature
1 parent e1f666c commit 53047ae

1 file changed

Lines changed: 50 additions & 10 deletions

File tree

pypdfbuilder.py

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,68 @@ class UserData:
2424
'''Class for storing current user's application data'''
2525

2626
def __init__(self):
27-
self.__user_data = {}
2827
self.__user_data_path = os.path.join(DATA_DIR, 'data.json')
28+
self.__data_defaults = {
29+
'filedialog_path': USER_DIR,
30+
'number_of_processed_files': 0,
31+
}
32+
self.__user_data = self.__get_user_data()
2933

3034
@property
3135
def filedialog_path(self):
3236
'''The last directory the user visited while opening or saving a file
3337
using a Tk File Dialog.
3438
35-
The getter will first try to return the value stored in the state of the
39+
The getter will first try to return the value stored in the
3640
instance, then try to read it out of the user data file, and if all else fails,
3741
set it to the user's home directory and return that value.
3842
3943
The setter will set the according class instance property and save that property to
4044
a user data file. If no such file exists yet, one will be created.
4145
'''
42-
return self.__user_data.get('filedialog_path') or self.__get_user_data()['filedialog_path']
46+
return self.__user_data.get('filedialog_path', self.__get_user_data()['filedialog_path'])
4347

4448
@filedialog_path.setter
4549
def filedialog_path(self, val):
4650
self.__user_data['filedialog_path'] = val
4751
self.__save_user_data()
4852

53+
@property
54+
def number_of_processed_files(self):
55+
'''Simple counter of PDF produced with PyPDF Builder
56+
57+
The getter will first try to return the value stored in the state of the
58+
instance, then try to read it out of the user data file, and if all else fails,
59+
set it to 0 and return that value.
60+
61+
The setter will set the according class instance property and save that property to
62+
a user data file. If no such file exists yet, one will be created.
63+
'''
64+
return self.__user_data.get('number_of_processed_files', self.__get_user_data()['number_of_processed_files'])
65+
66+
@number_of_processed_files.setter
67+
def number_of_processed_files(self, val):
68+
self.__user_data['number_of_processed_files'] = val
69+
self.__save_user_data()
70+
4971
def __get_user_data(self):
72+
'''Method to retrieve current user's data
73+
74+
Return:
75+
dict: Dictionary of user data with keys:
76+
* `filedialog_path`: last accessed file path
77+
* `number_of_processed_files`: number of processed files
78+
'''
5079
try:
5180
with (open(self.__user_data_path, 'r')) as datafile:
52-
self.__user_data = json.load(datafile)
81+
user_data = json.load(datafile)
82+
# make sure all values are returned. If a key is non-existant, fill it with default value
83+
for key, val in self.__data_defaults.items():
84+
if key not in user_data:
85+
user_data[key] = val
5386
except FileNotFoundError:
54-
self.filedialog_path=USER_DIR
55-
return self.__user_data
87+
user_data = self.__data_defaults
88+
return user_data
5689

5790
def __save_user_data(self):
5891
if not os.path.exists(os.path.dirname(self.__user_data_path)):
@@ -63,6 +96,9 @@ def __save_user_data(self):
6396
except FileNotFoundError:
6497
print('Something went horribly wrong while trying to save your current user data.')
6598

99+
def save_success(self):
100+
self.number_of_processed_files += 1
101+
66102

67103

68104

@@ -192,6 +228,7 @@ def save_as(self):
192228
out_pdf.addPage(bottom_page)
193229
with open(save_filepath, "wb") as out_pdf_stream:
194230
out_pdf.write(out_pdf_stream)
231+
self.parent.user_data.save_success()
195232

196233

197234
class SplitTabManager:
@@ -240,6 +277,7 @@ def save_as(self):
240277
out_pdf.addPage(in_pdf.getPage(p))
241278
with open(output_path, "wb") as out_pdf_stream:
242279
out_pdf.write(out_pdf_stream)
280+
self.parent.user_data.save_success()
243281

244282

245283
class RotateTabManager:
@@ -293,6 +331,7 @@ def save_as(self):
293331
out_pdf.addPage(in_pdf.getPage(p))
294332
with open(save_filepath, "wb") as out_pdf_stream:
295333
out_pdf.write(out_pdf_stream)
334+
self.parent.user_data.save_success()
296335

297336

298337
class JoinTabManager:
@@ -378,6 +417,7 @@ def save_as(self):
378417
merger.append(fileobj=open(f[PDF_FILEPATH], 'rb'), pages=page_range)
379418
with open(save_filepath, 'wb') as out_pdf:
380419
merger.write(out_pdf)
420+
self.parent.user_data.save_success()
381421

382422
def move_up(self):
383423
selected_files = self.__selected_files
@@ -428,7 +468,7 @@ def __init__(self):
428468

429469
self.builder.connect_callbacks(self)
430470

431-
self.__user_data = UserData()
471+
self.user_data = UserData()
432472

433473
self.jointab = JoinTabManager(self)
434474
self.splittab = SplitTabManager(self)
@@ -502,15 +542,15 @@ def rotatetab_save_as(self):
502542

503543
def get_file_dialog(self, func, widget_title='Choose File(s) …'):
504544
f = func(
505-
initialdir=self.__user_data.filedialog_path,
545+
initialdir=self.user_data.filedialog_path,
506546
title=widget_title,
507547
filetypes=(("PDF File", "*.pdf"), ("All Files", "*.*"))
508548
)
509549
if f:
510550
if type(f) == list or type(f) == tuple:
511-
self.__user_data.filedialog_path = os.path.dirname(f[-1])
551+
self.user_data.filedialog_path = os.path.dirname(f[-1])
512552
elif type(f) == str:
513-
self.__user_data.filedialog_path = os.path.dirname(f)
553+
self.user_data.filedialog_path = os.path.dirname(f)
514554
return f
515555

516556
def quit(self, event=None):

0 commit comments

Comments
 (0)