87 lines
2.1 KiB
Python
87 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
InvMan Storage Module
|
|
"""
|
|
|
|
# Imports
|
|
from flask import jsonify
|
|
from flask_api import status
|
|
import json
|
|
|
|
|
|
class AlreadyExistsError(Exception):
|
|
pass
|
|
|
|
|
|
def load_raw_json():
|
|
with open("inventory.json", "r") as f:
|
|
print("Opened inventory.json")
|
|
raw_json = json.loads(f.read())
|
|
print("Converted JSON to dictionary")
|
|
return raw_json
|
|
|
|
|
|
def write_raw_json(writedict):
|
|
with open("inventory.json", "w+") as f:
|
|
print("Opened inventory.json for writing")
|
|
f.write(json.dumps(writedict))
|
|
print("Wrote dictionary as JSON to inventory.json")
|
|
|
|
|
|
def get_items(location):
|
|
return load_raw_json()[location]
|
|
|
|
|
|
def init_item(location, item):
|
|
rawitems = load_raw_json()
|
|
if item not in rawitems[location].keys():
|
|
rawitems[location].update({item: {}})
|
|
write_raw_json(rawitems)
|
|
else:
|
|
raise AlreadyExistsError
|
|
|
|
|
|
def init_brand(location, item, brand):
|
|
rawitems = load_raw_json()
|
|
if item not in rawitems[location].keys():
|
|
init_item(location, item)
|
|
rawitems = load_raw_json()
|
|
|
|
if brand not in rawitems[location][item].keys():
|
|
rawitems[location][item].update({brand: {}})
|
|
write_raw_json(rawitems)
|
|
else:
|
|
raise AlreadyExistsError
|
|
|
|
|
|
def set_brand_key(location, item, brand, key, value):
|
|
rawitems = load_raw_json()
|
|
if item not in rawitems[location].keys():
|
|
init_item(location, item)
|
|
rawitems = load_raw_json()
|
|
|
|
if brand not in rawitems[location][item].keys():
|
|
init_brand(location, item, brand)
|
|
rawitems = load_raw_json()
|
|
|
|
rawitems[location][item][brand].update({key: value})
|
|
write_raw_json(rawitems)
|
|
|
|
|
|
def rm_brand_key(location, item, brand, key):
|
|
rawitems = load_raw_json()
|
|
rawitems[location][item][brand].pop(key)
|
|
write_raw_json(rawitems)
|
|
|
|
|
|
def rm_brand(location, item, brand):
|
|
rawitems = load_raw_json()
|
|
rawitems[location][item].pop(brand)
|
|
write_raw_json(rawitems)
|
|
|
|
|
|
def rm_item(location, item):
|
|
rawitems = load_raw_json()
|
|
rawitems[location].pop(item)
|
|
write_raw_json(rawitems)
|