mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-17 19:00:58 +00:00
add persistance to trezor.config mock
This commit is contained in:
parent
2cf75d85e7
commit
67ea7dbd15
1
src/.gitignore
vendored
Normal file
1
src/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
trezor.config
|
@ -1,25 +1,50 @@
|
||||
# mock in-memory implementation
|
||||
import sys
|
||||
|
||||
_mock = {}
|
||||
if sys.platform == 'trezor': # stmhal - use binary module (not working atm)
|
||||
|
||||
def get(app, key, default=None):
|
||||
return _mock.get((app << 8) | key, default)
|
||||
from TrezorConfig import Config
|
||||
|
||||
def set(app, key, value):
|
||||
_mock[(app << 8) | key] = value
|
||||
return True
|
||||
_config = Config()
|
||||
|
||||
# real implementation commented below
|
||||
def get(app, key, default=None):
|
||||
v = _config.get(app, key)
|
||||
return v if v else default
|
||||
|
||||
'''
|
||||
from TrezorConfig import Config
|
||||
def set(app, key, value):
|
||||
return _config.set(app, key, value)
|
||||
|
||||
_config = Config()
|
||||
else: # emulator (mock implementation using binary file)
|
||||
|
||||
def get(app, key, default=None):
|
||||
v = _config.get(app, key)
|
||||
return v if v else default
|
||||
import ustruct
|
||||
|
||||
def set(app, key, value):
|
||||
return _config.set(app, key, value)
|
||||
'''
|
||||
_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
|
||||
|
Loading…
Reference in New Issue
Block a user