documentation WIP
This commit is contained in:
@ -1,10 +1,29 @@
|
||||
import json
|
||||
|
||||
import os
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
|
||||
from logger import generalLogger
|
||||
import options
|
||||
|
||||
|
||||
def startUp():
|
||||
filePath = 'data.json'
|
||||
|
||||
if os.path.exists(filePath):
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
backupPath = (f'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.')
|
||||
|
||||
|
||||
async def parseParams(params):
|
||||
# Returnes dictionary of params as paramsLines in options
|
||||
# Returnes dictionary of string params as paramsLines in options
|
||||
paramsList = params.split()
|
||||
paramsDict = {}
|
||||
for i in range(len(options.paramsLines)):
|
||||
@ -23,6 +42,7 @@ async def toDictPairParams(pair: str, params):
|
||||
paramsDict[pair][options.paramsLines[i]] = paramsList[i]
|
||||
return paramsDict
|
||||
|
||||
|
||||
async def checkPair(pair: str):
|
||||
# Returnes 1 if pair exists and 0 if not
|
||||
currentData = {}
|
||||
@ -30,13 +50,11 @@ async def checkPair(pair: str):
|
||||
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.')
|
||||
generalLogger.info('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):
|
||||
@ -46,21 +64,20 @@ async def deletePair(pair: str):
|
||||
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.')
|
||||
generalLogger.info('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)
|
||||
generalLogger.info(f'Pair {pair} was deleted successfully!')
|
||||
return 0
|
||||
else:
|
||||
# print(pair, ' not found in data file.')
|
||||
generalLogger.info(f'Pair {pair} was not found in the data file when trying to delete it.')
|
||||
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:
|
||||
@ -71,14 +88,25 @@ async def savePairParams(pair: str, params):
|
||||
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.')
|
||||
generalLogger.info('WARNING: JSON file is empty! Ignore if your installation is fresh.')
|
||||
|
||||
if pair in currentData:
|
||||
print(pair, ' already exists.')
|
||||
generalLogger.info(f"Pair {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!')
|
||||
generalLogger.info(f"Pair {pair} was added!")
|
||||
return 0
|
||||
|
||||
|
||||
async def loadJson():
|
||||
# Returnes the contents of the JSON file as a dictionary
|
||||
data = {}
|
||||
try:
|
||||
with open('data.json', 'r') as f:
|
||||
data = json.load(f)
|
||||
except json.decoder.JSONDecodeError as e:
|
||||
generalLogger.info('WARNING: JSON file is empty! Ignore if your installation is fresh.')
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user