|
1 | 1 | #!/usr/bin/python |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import appdirs |
| 5 | +import json |
4 | 6 | from pathlib import Path as plPath |
5 | 7 | from operator import itemgetter |
6 | 8 | from settings import * |
|
11 | 13 |
|
12 | 14 | from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter |
13 | 15 |
|
| 16 | +APPNAME = 'pypdfbuilder' |
14 | 17 | CURRENT_DIR = os.path.abspath(os.path.dirname(__file__)) |
15 | 18 | 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 | + |
16 | 67 |
|
17 | 68 |
|
18 | 69 | class PDFInfo: |
@@ -146,7 +197,7 @@ def save_as(self): |
146 | 197 | class SplitTabManager: |
147 | 198 | '''Manager class for the Split Tab |
148 | 199 |
|
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 |
150 | 201 |
|
151 | 202 | Args: |
152 | 203 | parent (PyPDFBuilderApplication): Application that created the instance and that contains the Split Tab. |
@@ -377,8 +428,7 @@ def __init__(self): |
377 | 428 |
|
378 | 429 | self.builder.connect_callbacks(self) |
379 | 430 |
|
380 | | - # Todo get pickled data: last directory visited by user |
381 | | - self.__current_dir = USER_DIR |
| 431 | + self.__user_data = UserData() |
382 | 432 |
|
383 | 433 | self.jointab = JoinTabManager(self) |
384 | 434 | self.splittab = SplitTabManager(self) |
@@ -452,15 +502,15 @@ def rotatetab_save_as(self): |
452 | 502 |
|
453 | 503 | def get_file_dialog(self, func, widget_title='Choose File(s) …'): |
454 | 504 | f = func( |
455 | | - initialdir=self.__current_dir, |
| 505 | + initialdir=self.__user_data.filedialog_path, |
456 | 506 | title=widget_title, |
457 | 507 | filetypes=(("PDF File", "*.pdf"), ("All Files", "*.*")) |
458 | 508 | ) |
459 | 509 | 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]) |
462 | 512 | elif type(f) == str: |
463 | | - self.__current_dir = os.path.dirname(f) |
| 513 | + self.__user_data.filedialog_path = os.path.dirname(f) |
464 | 514 | return f |
465 | 515 |
|
466 | 516 | def quit(self, event=None): |
|
0 commit comments