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.
95 lines
2.8 KiB
Python
95 lines
2.8 KiB
Python
10 years ago
|
import json
|
||
|
|
||
|
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",
|
||
|
bureauname: "Inhuman Resources",
|
||
|
desc: "Keep track of public resources provided by bureaus"
|
||
|
}
|
||
|
"""
|
||
|
d = json.loads(data)
|
||
|
try:
|
||
|
name = d["name"]
|
||
|
prefix = d["prefix"]
|
||
|
desc = d["desc"]
|
||
|
except KeyError as e:
|
||
|
print("cannot add invalid bureau:", str(e))
|
||
|
return
|
||
|
print("added menu")
|
||
|
self.menu[prefix] = {"name": name,
|
||
|
"desc": desc,
|
||
|
"commands": {},
|
||
|
"apis": {}}
|
||
|
|
||
|
@add_api("addcommand", "Register Command")
|
||
|
def add_cmd(self, data):
|
||
|
d = json.loads(data)
|
||
|
try:
|
||
|
prefix = d["prefix"]
|
||
|
cmd = d["cmd"]
|
||
|
cmdname = d["cmdname"]
|
||
|
desc = d["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]["commands"][cmd] = {"name": cmdname,
|
||
|
"desc": desc}
|
||
|
|
||
|
@add_api("addapi", "Register API Method")
|
||
|
def add_api_method(self, data):
|
||
|
d = json.loads(data)
|
||
|
try:
|
||
|
prefix = d["prefix"]
|
||
|
cmd = d["api"]
|
||
|
cmdname = d["apiname"]
|
||
|
desc = d["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.
|
||
|
"""
|
||
|
print(self.menu)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
hr = InhumanResources()
|
||
|
hr.run()
|