# this is a web interface for simple edits to configuration or editing data # for particular bureaus import configparser import inspect import os from bottle import Bottle, request, static_file import mako class IhrApp(Bottle): def __init__(self): super(IhrApp, self).__init__() self.name = "IHR App" self.route("/", callback=self.index) self.route("/config/", callback=self.bureau_config) self.route("/config/", callback=self.bureau_config, method="POST") self.route('/static/', callback=self.static) self.bureaus = [] mpath = inspect.getfile(self.__class__) self.tdir = os.path.dirname(mpath) def index(self): # return a list of active bureaus with links # TODO: allow (de)activation of non-essential bureaus templfile = os.path.join(self.tdir, "web_index.html") out = """

The Screenless Office

Config Manager

    """ for bureau in self.bureaus: out += '
  • {0}
  • \n'.format(bureau) out += '
  • Printers
  • \n' out += '
  • Management
  • \n' out += "
" return out def bureau_config(self, bureau): # parse the config and make it into a form # TODO: add a little html snippet with cfg file docs templfile = os.path.join(self.tdir, "web_config.html") template = mako.template.Template(filename=templfile) basedir = os.path.expanduser("~/.screenless") msg = "" if bureau == "mgmt": cfgfile = "mgmt.ini" mode = "ini" elif bureau == "printers": cfgfile = "printers.cfg" mode = "ini" else: cfgfile = bureau + ".yml" mode = "yaml" cfgfile = os.path.join(basedir, cfgfile) if request.method == "POST": with open(cfgfile, "w") as cfg: cfg.write(request.forms.get("cfgdata")) # TODO: tell the bureau to restart / reload msg = "Modified config file saved!" #parse and show the config as a form with open(cfgfile) as cfg: cfgdata = cfg.read() return template.render_unicode(msg=msg, bureau=bureau, cfgfile=cfgfile, mode=mode, cfgdata=cfgdata) def register_bureau(self, bureau): self.bureaus.append(bureau) def register_crud(self, data): # adds callbacks for crud operations on a database pass def static(self, path): root_path = os.path.join(self.tdir, "web_static") return static_file(path, root=root_path)