-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned up the script and commented the functions
- Loading branch information
Showing
1 changed file
with
81 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,95 @@ | ||
# Imports | ||
import json | ||
import numpy as np | ||
from datetime import datetime, date | ||
############################## | ||
|
||
|
||
# Constants / Types used in Zendro | ||
ALLOWED_TYPES = ["string", "integer", "boolean"] | ||
ZENDRO_TYPES = { | ||
'string' : 'String', | ||
'integer' : 'Int', | ||
'boolean' : 'Boolean' | ||
'string': 'String', | ||
'integer': 'Int', | ||
'boolean': 'Boolean' | ||
} | ||
############################## | ||
|
||
|
||
# Functions | ||
def log(msg): | ||
"""Writes the message to a log-file.\n | ||
Logs the date and time automatically.\n | ||
Date - Time: msg | ||
""" | ||
|
||
def logMSG(msg): | ||
try: | ||
# Get current date and time and write this with the message to the log file | ||
with open("Log.txt", "a") as file: | ||
currentTime = datetime.now().strftime("%H:%M:%S") | ||
file.write(str(date.today())+" - "+currentTime+":\t"+msg+"\n") | ||
except: | ||
current_time = datetime.now().strftime("%H:%M:%S") | ||
file.write(str(date.today()) + " - " + current_time + ":\t" + msg + "\n") | ||
except OSError as error: | ||
# Prints the occurred error | ||
print(error) | ||
print("Not allowed to create or edit the log") | ||
|
||
def readJSON(path, fname): | ||
|
||
def read_json(file): | ||
"""Reads in a json file and returns its content.\n | ||
Keyword arguments:\n | ||
- file: Path to the file JSON that should be read | ||
""" | ||
|
||
try: | ||
with open(path+fname, "r") as file: | ||
data = json.load(file) | ||
# Open file and load json content | ||
with open(file, "r") as json_file: | ||
data = json.load(json_file) | ||
return data | ||
except: | ||
logMSG("Couldn't open file: "+path+fname) | ||
|
||
def writeJSON(path, fname, propData): | ||
try: | ||
json_object = json.dumps(propData, indent=4) | ||
with open(path+fname, "w") as file: | ||
file.write(json_object) | ||
except: | ||
logMSG("Couldn't open file: "+path+fname) | ||
|
||
def getPropType(types): | ||
boolean_mask = np.nonzero(np.isin(ALLOWED_TYPES, types))[0] | ||
if len(boolean_mask): | ||
return ALLOWED_TYPES[boolean_mask[0]] | ||
return None | ||
|
||
def getZendroTypes(types): | ||
propType = getPropType(types) | ||
if propType != None: | ||
return ZENDRO_TYPES[propType] | ||
return None | ||
|
||
def getPropData(data): | ||
properties = data['properties'] | ||
propData = {} | ||
for prop in properties: | ||
type = getZendroTypes(properties[prop]['type']) | ||
if type==None: continue | ||
propData[prop] = { | ||
'description' : properties[prop]['description'], | ||
'type' : type | ||
} | ||
return propData | ||
except OSError as error: | ||
log("Couldn't open file: " + file + "\tError: " + str(error)) | ||
|
||
|
||
def write_json(path, filename, properties): | ||
"""Writes the passed data to a json file.\n | ||
Keyword arguments:\n | ||
- path: Path where the file should be saved\n | ||
- filename: Filename\n | ||
- property: Data that should be saved in json format | ||
""" | ||
|
||
try: | ||
json_object = json.dumps(properties, indent=4) | ||
with open(path + filename, "w") as file: | ||
file.write(json_object) | ||
except OSError as error: | ||
log("Couldn't open file: " + path + filename + "\tError: " + str(error)) | ||
|
||
|
||
def get_type(types): | ||
"""Checks if the passed type is Zendro compatible.\n | ||
- If compatible: return the type.\n | ||
- Else: return None. | ||
""" | ||
|
||
boolean_mask = np.nonzero(np.isin(ALLOWED_TYPES, types))[0] | ||
if len(boolean_mask): | ||
return ZENDRO_TYPES[ALLOWED_TYPES[boolean_mask[0]]] | ||
return None | ||
|
||
|
||
def get_data(file_data): | ||
"""From the passed data the properties are extracted.\n | ||
Properties with a type that is compatible to Zendro, will be stored in propData.\n | ||
Returns propData, therefore only properties with compatible type. | ||
""" | ||
|
||
properties = file_data['properties'] | ||
properties_data = {} | ||
for current_property in properties: | ||
property_type = get_type(properties[current_property]['type']) | ||
if property_type is None: | ||
continue | ||
properties_data[current_property] = { | ||
'description': properties[current_property]['description'], | ||
'type': property_type | ||
} | ||
return properties_data |