Skip to content

Commit 9b3ffb0

Browse files
committed
Added config and SECRET_KEY
Now each user will generate their own SECRET_KEY
1 parent 19462ea commit 9b3ffb0

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

qrshare/app.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77
from .auth import Authentication
88
from .models import Route, QRContainer, ZipContent
99
from .tools import NetworkTools
10+
from .config import UserConfig
1011

1112

1213
class App:
1314
def __init__(self, paths: List[Path], code=None, port=5000):
1415
self.app = self.init()
16+
self.user = UserConfig()
17+
18+
# set config
19+
self.app.config.from_mapping(self.user.config.data)
20+
1521
self.auth = Authentication(self.app, code)
1622

1723
self.paths = paths

qrshare/config/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .user import UserConfig

qrshare/config/base.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import json
2+
from pathlib import Path
3+
from typing import Optional, Any
4+
5+
6+
class ConfigBase:
7+
def __init__(self, path, name, load=False):
8+
self.path = path / Path(name)
9+
self.name = name
10+
self.data = {}
11+
12+
if load:
13+
self.load()
14+
15+
def put(self, key, value):
16+
self.data[key] = value
17+
self.save()
18+
19+
def get(self, key) -> Optional[Any]:
20+
try:
21+
return self.data[key]
22+
except KeyError:
23+
return None
24+
25+
def save(self):
26+
with self.path.open('w') as f:
27+
json.dump(self.data, f)
28+
29+
def load(self):
30+
try:
31+
with self.path.open('r') as f:
32+
self.data = json.load(f)
33+
34+
# edit error message
35+
except json.JSONDecodeError as e:
36+
raise json.JSONDecodeError(f'{self.path} - {e.msg}', e.doc, e.pos)
37+
38+
# treat as empty
39+
except FileNotFoundError:
40+
pass

qrshare/config/user.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pathlib import Path
2+
from uuid import uuid4
3+
4+
from appdirs import user_config_dir
5+
from .base import ConfigBase
6+
7+
appname = 'Novelsave'
8+
appauthor = 'mHaisham'
9+
10+
class UserConfig:
11+
def __init__(self):
12+
# get cross os configuration path
13+
self.path = Path(user_config_dir(appname, appauthor))
14+
self.path.mkdir(parents=True, exist_ok=True)
15+
16+
self.config = ConfigBase(self.path, 'config.json')
17+
self.init_secret_key()
18+
19+
def init_secret_key(self):
20+
key = self.config.get("SECRET_KEY")
21+
if key is None:
22+
self.config.put("SECRET_KEY", uuid4().get_hex())

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ click==7.1.2
22
Flask==1.1.2
33
qrcode==6.1
44
waitress==1.4.4
5+
appdirs>=1.4.4

0 commit comments

Comments
 (0)