Skip to content

Commit 7682dd1

Browse files
committed
Added saving theme to settings
Also updated method how to change theme.
1 parent 8da3b3e commit 7682dd1

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

python_password/PyPassword.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from python_password.utils.crypto import *
2121
from python_password.utils.database import *
2222
from python_password.utils.files import *
23+
from python_password.utils.settings import *
2324

2425

2526
class SimpleDialog:
@@ -94,7 +95,7 @@ def build(self):
9495
def on_start(self):
9596
self.update_passwords_list()
9697
self.masters_ok()
97-
# self.switch_theme()
98+
self.switch_theme(get_theme())
9899

99100
# ================================
100101
# Information
@@ -502,19 +503,28 @@ def update_passwords_list(self):
502503
# ================================
503504

504505
def switch_theme(self, force=None):
505-
if force is not None:
506-
self.theme_cls.theme_style = force
506+
"""
507+
Changes theme to opposite or forced.
508+
:param force: Which theme has to be applied.
509+
:return: Nothing.
510+
"""
511+
if force == 'Light':
512+
current = 'Dark'
513+
elif force == 'Dark':
514+
current = 'Light'
515+
else:
516+
current = self.theme_cls.theme_style
507517

508-
elif self.theme_cls.theme_style == 'Light':
518+
if current == 'Light':
509519
self.theme_cls.theme_style = 'Dark'
510520
self.text_color_hex = 'ffffff'
511521
self.text_color_rgba = (1, 1, 1, 1)
512-
513-
elif self.theme_cls.theme_style == 'Dark':
522+
set_theme('Dark')
523+
elif current == 'Dark':
514524
self.theme_cls.theme_style = 'Light'
515525
self.text_color_hex = '111111'
516526
self.text_color_rgba = (.06, .06, .06, 1)
517-
527+
set_theme('Light')
518528
else:
519529
raise NameError('No theme found')
520530

python_password/utils/files.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Files:
66
"""Names for program's files."""
77
alpha_key = 'Alpha.key'
88
beta_key = 'Beta.key'
9+
settings = 'Settings.json'
910
sqlite = 'Passwords.db'
1011

1112

python_password/utils/settings.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
from json import dump, load
3+
4+
from python_password.utils.files import appdata, Files
5+
6+
7+
default_settings = {
8+
'theme': 'Light'
9+
}
10+
11+
12+
def create_settings():
13+
try:
14+
open(appdata(Files.settings), 'x')
15+
except FileExistsError:
16+
pass
17+
with open(appdata(Files.settings), 'w') as f:
18+
dump(default_settings, f)
19+
20+
21+
reset_settings = create_settings
22+
23+
24+
def set_setting(name: str, value):
25+
try:
26+
open(appdata(Files.settings))
27+
except FileNotFoundError:
28+
create_settings()
29+
finally:
30+
with open(appdata(Files.settings)) as f:
31+
settings = load(f)
32+
33+
settings[name] = value
34+
35+
with open(appdata(Files.settings), 'w') as f:
36+
dump(settings, f)
37+
38+
39+
def get_setting(name: str):
40+
try:
41+
open(appdata(Files.settings))
42+
except FileNotFoundError:
43+
create_settings()
44+
finally:
45+
with open(appdata(Files.settings)) as f:
46+
settings = load(f)
47+
try:
48+
result = settings[name]
49+
except IndexError:
50+
return IndexError('Missing setting')
51+
else:
52+
return result
53+
54+
55+
def set_theme(theme: str):
56+
set_setting('theme', theme)
57+
58+
59+
def get_theme():
60+
return get_setting('theme')

0 commit comments

Comments
 (0)