124 lines
3.4 KiB
Python
124 lines
3.4 KiB
Python
import json
|
|
|
|
import os
|
|
import shutil
|
|
from datetime import datetime
|
|
|
|
from logger import generalLogger
|
|
import options
|
|
|
|
|
|
jsonPath = 'data/data.json'
|
|
|
|
|
|
def startUp():
|
|
filePath = jsonPath
|
|
|
|
if not(os.path.exists('data')):
|
|
os.mkdir('data')
|
|
if not(os.path.exists('data/backup')):
|
|
os.mkdir('data/backup')
|
|
|
|
if os.path.exists(filePath):
|
|
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
|
|
backupPath = (f'data/backup/data_backup_{timestamp}.json')
|
|
shutil.copy(filePath, backupPath)
|
|
generalLogger.info(f'JSON backup was created: {backupPath}')
|
|
|
|
with open(filePath, 'w') as f:
|
|
json.dump({}, f, ensure_ascii = False, indent=4)
|
|
generalLogger.info(f'New {filePath} created with empty JSON.')
|
|
|
|
|
|
def parseParams(params):
|
|
# Returnes dictionary of string params as paramsLines in options
|
|
paramsList = params.split()
|
|
paramsDict = {}
|
|
if len(paramsList) != len(options.paramsLines):
|
|
return -1
|
|
|
|
for i in range(len(options.paramsLines)):
|
|
paramsDict[options.paramsLines[i]] = paramsList[i]
|
|
return paramsDict
|
|
|
|
def toDictPairParams(pair: str, params):
|
|
# Returnes dictionary as pair:internal(params)
|
|
paramsList = params.split()
|
|
|
|
if len(paramsList) != len(options.paramsLines):
|
|
return -1
|
|
|
|
paramsDict = {pair: {}}
|
|
for i in range(len(options.paramsLines)):
|
|
paramsDict[pair][options.paramsLines[i]] = paramsList[i]
|
|
return paramsDict
|
|
|
|
|
|
def checkPair(pair: str):
|
|
# Returnes 1 if pair exists and 0 if not
|
|
currentData = {}
|
|
try:
|
|
with open(jsonPath, 'r') as f:
|
|
currentData = json.load(f)
|
|
except json.decoder.JSONDecodeError as e:
|
|
generalLogger.warning('JSON file is empty!')
|
|
|
|
if pair in currentData:
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
def deletePair(pair: str):
|
|
# Returnes 0 if deleted successfully and -1 if not
|
|
currentData = {}
|
|
try:
|
|
with open(jsonPath, 'r') as f:
|
|
currentData = json.load(f)
|
|
except json.decoder.JSONDecodeError as e:
|
|
generalLogger.warning('JSON file is empty!')
|
|
|
|
if pair in currentData:
|
|
del currentData[pair]
|
|
with open(jsonPath, 'w', encoding = 'utf-8') as f:
|
|
json.dump(currentData, f, ensure_ascii = False, indent = 4)
|
|
generalLogger.info(f'Pair {pair} was deleted successfully!')
|
|
return 0
|
|
else:
|
|
generalLogger.info(f'Pair {pair} was not found in the data file when trying to delete it.')
|
|
return -1
|
|
|
|
def savePairParams(pair: str, params):
|
|
# Saves or updates data in JSON
|
|
newData = toDictPairParams(pair, params)
|
|
|
|
if newData == -1:
|
|
return -1
|
|
|
|
currentData = {}
|
|
try:
|
|
with open(jsonPath, 'r') as f:
|
|
currentData = json.load(f)
|
|
except json.decoder.JSONDecodeError as e:
|
|
generalLogger.warning('JSON file is empty!')
|
|
|
|
if pair in currentData:
|
|
generalLogger.info(f"Pair {pair} already exists.")
|
|
return -2
|
|
else:
|
|
with open(jsonPath, 'w', encoding = 'utf-8') as f:
|
|
currentData.update(newData)
|
|
json.dump(currentData, f, ensure_ascii = False, indent = 4)
|
|
generalLogger.info(f"Pair {pair} was added!")
|
|
return 0
|
|
|
|
|
|
def loadJson():
|
|
# Returnes the contents of the JSON file as a dictionary
|
|
data = {}
|
|
try:
|
|
with open(jsonPath, 'r') as f:
|
|
data = json.load(f)
|
|
except json.decoder.JSONDecodeError as e:
|
|
generalLogger.warning('JSON file is empty!')
|
|
return data
|