1
0
Fork 0
This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
mining-companion/companion/state.py
Julien Riou 14aa8a6d59
Add "state" tests
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-02-12 09:43:38 +01:00

34 lines
1 KiB
Python

import json
import os
class State:
def __init__(self, filename):
self.filename = filename
self.create()
def create(self):
if not os.path.isfile(self.filename):
with open(self.filename, 'w') as fd:
json.dump({}, fd)
def read(self):
with open(self.filename, 'r') as fd:
return json.load(fd)
def write(self, pool_name, block_number=None, miner_balance=None, miner_payment=None):
content = self.read()
if pool_name not in content:
content[pool_name] = {}
if block_number is not None:
content[pool_name]['block'] = block_number
if miner_balance is not None:
content[pool_name]['balance'] = miner_balance
if miner_payment:
content[pool_name]['payment'] = miner_payment
with open(self.filename, 'w') as fd:
json.dump(content, fd, indent=2, separators=(',', ': '))
def get(self, key):
content = self.read()
return content.get(key, {})