add core Bureau, InhumanResources, HumorDept and kbtest bureaus.
parent
28652863d8
commit
7ac0fc8f3f
@ -0,0 +1,94 @@
|
||||
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()
|
@ -0,0 +1,29 @@
|
||||
import subprocess
|
||||
|
||||
from bureau import Bureau, add_command
|
||||
|
||||
|
||||
class Humor(Bureau):
|
||||
"""
|
||||
This bureau entertains the modern worker and provides colorful
|
||||
bons mots for managers who need to warm up an audience.
|
||||
"""
|
||||
|
||||
name = "Department of Humor"
|
||||
prefix = "HA"
|
||||
version = 0
|
||||
|
||||
def __init__(self):
|
||||
Bureau.__init__(self)
|
||||
|
||||
@add_command("joke", "Fortune Cookie")
|
||||
def print_fortune(self):
|
||||
"""
|
||||
Prints a clever quip.
|
||||
"""
|
||||
print(str.decode(subprocess.check_output("fortune")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ha = Humor()
|
||||
ha.run()
|
@ -0,0 +1,9 @@
|
||||
import zmq
|
||||
|
||||
ctx = zmq.Context()
|
||||
send = ctx.socket(zmq.PUB)
|
||||
send.connect("tcp://localhost:10100")
|
||||
|
||||
while True:
|
||||
msg = input("> ")
|
||||
send.send_string(msg)
|
Loading…
Reference in New Issue