websocket WIP 3

This commit is contained in:
2025-04-29 19:32:43 +03:00
parent c612d5b8d4
commit f70126af46
4 changed files with 112 additions and 88 deletions

84
src/jsonProcessing.py Normal file
View File

@ -0,0 +1,84 @@
import json
import options
async def parseParams(params):
# Returnes dictionary of params as paramsLines in options
paramsList = params.split()
paramsDict = {}
for i in range(len(options.paramsLines)):
paramsDict[options.paramsLines[i]] = paramsList[i]
return paramsDict
async 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
async def checkPair(pair: str):
# Returnes 1 if pair exists and 0 if not
currentData = {}
try:
with open('data.json', 'r') as f:
currentData = json.load(f)
except json.decoder.JSONDecodeError as e:
print('WARNING: JSON file is empty! Ignore if your installation is fresh.')
if pair in currentData:
# print(pair, ' exists in data file.')
return 1
else:
# print(pair, ' not found in data file.')
return 0
async def deletePair(pair: str):
# Returnes 0 if deleted successfully and -1 if not
currentData = {}
try:
with open('data.json', 'r') as f:
currentData = json.load(f)
except json.decoder.JSONDecodeError as e:
print('WARNING: JSON file is empty! Ignore if your installation is fresh.')
if pair in currentData:
# print(pair, ' exists in data file.')
del currentData[pair]
with open('data.json', 'w', encoding = 'utf-8') as f:
json.dump(currentData, f, ensure_ascii = False, indent = 4)
return 0
else:
# print(pair, ' not found in data file.')
return -1
async def savePairParams(pair: str, params):
# Saves or updates data in JSON
# Fix no file or empty file
newData = await toDictPairParams(pair, params)
if newData == -1:
return -1
currentData = {}
try:
with open('data.json', 'r') as f:
currentData = json.load(f)
except json.decoder.JSONDecodeError as e:
print('WARNING: JSON file is empty! Ignore if your installation is fresh.')
if pair in currentData:
print(pair, ' already exists.')
return -2
else:
with open('data.json', 'w', encoding = 'utf-8') as f:
currentData.update(newData)
json.dump(currentData, f, ensure_ascii = False, indent = 4)
print(pair, ' was added!')
return 0