You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.0 KiB
Python

import json
import code128
from bureau import Bureau, add_command, add_api
class InhumanResources(Bureau):
"""
This is a core functional bureau of the Screenless office it keeps track
of all published methods provided by all other bureaus and can print
the menu for the whole system or an individual item
"""
name = "Inhuman Resources"
prefix = "IR"
version = 0
def __init__(self):
Bureau.__init__(self)
self.menu = {}
# update_commands(self)
@add_api("addbureau", "Register Bureau")
def add_bureau(self, data):
"""
Register Bureau with Inhuman Resources
data = { prefix: "IR",
name: "Inhuman Resources",
desc: "Keep track of public resources provided by bureaus"
}
"""
try:
name = data["name"]
prefix = data["prefix"]
desc = data["desc"]
except KeyError as e:
print("cannot add invalid bureau:", str(e))
return str(e)
print("added menu")
self.menu[prefix] = {"name": name,
"desc": desc,
"commands": {},
"apis": {}}
@add_api("addcommand", "Register Command")
def add_cmd(self, data):
try:
prefix = data["prefix"]
cmd = data["cmd"]
cmdname = data["cmdname"]
desc = data["desc"]
barcode = code128.svg(prefix + cmd + ".")
except KeyError as e:
print("cannot add invalid command:", str(e))
return
if prefix not in self.menu:
# TODO: this should throw some kind of error message/log
print("error: cannot add command ", cmd, "to non-existent prefix",
prefix)
else:
self.menu[prefix]["commands"][cmd] = {"name": cmdname,
"desc": desc,
"barcode": barcode}
@add_api("addapi", "Register API Method")
def add_api_method(self, data):
try:
prefix = data["prefix"]
cmd = data["api"]
cmdname = data["apiname"]
desc = data["desc"]
except KeyError as e:
print("cannot add invalid command:", str(e))
return
if prefix not in self.menu:
# TODO: this should throw some kind of error message/log
print("error: cannot add command ", cmd, "to non-existent prefix",
prefix)
else:
self.menu[prefix]["apis"][cmd] = {"name": cmdname,
"desc": desc}
@add_command("menu", "Print Menu")
def print_menu(self):
"""
Prints the menu of commands for all operational bureaus.
"""
self.print_full("menu.html", menu=self.menu)
print(self.menu)
def main():
hr = InhumanResources()
hr.run()
if __name__ == "__main__":
hr = InhumanResources()
hr.run()