1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-12 00:10:58 +00:00
trezor-firmware/src/trezor/config.py

51 lines
1.2 KiB
Python
Raw Normal View History

2016-05-31 16:13:23 +00:00
import sys
2016-05-23 14:05:52 +00:00
2016-05-31 16:13:23 +00:00
if sys.platform == 'trezor': # stmhal - use binary module (not working atm)
2016-05-23 14:05:52 +00:00
2016-05-31 16:13:23 +00:00
from TrezorConfig import Config
2016-05-23 14:05:52 +00:00
2016-05-31 16:13:23 +00:00
_config = Config()
2016-05-23 14:05:52 +00:00
2016-05-31 16:13:23 +00:00
def get(app, key, default=None):
v = _config.get(app, key)
return v if v else default
2016-05-23 14:05:52 +00:00
2016-05-31 16:13:23 +00:00
def set(app, key, value):
return _config.set(app, key, value)
2016-05-23 13:44:09 +00:00
2016-05-31 16:13:23 +00:00
else: # emulator (mock implementation using binary file)
2016-05-23 13:44:09 +00:00
2016-05-31 16:13:23 +00:00
import ustruct
2016-05-23 13:44:09 +00:00
2016-05-31 16:13:23 +00:00
_mock = {}
_file = 'trezor.config'
def _load():
try:
with open(_file, 'rb') as f:
while True:
d = f.read(4)
if len(d) != 4:
break
k, l = ustruct.unpack('<HH', d)
v = f.read(l)
_mock[k] = v
except OSError:
pass
def _save():
with open(_file, 'wb') as f:
for k, v in _mock.items():
f.write(ustruct.pack('<HH', k, len(v)))
f.write(v)
_load()
def get(app, key, default=None):
return _mock.get((app << 8) | key, default)
def set(app, key, value):
_mock[(app << 8) | key] = value
_save()
return True