import picoweb
from config import model
from util import handler, ok

def get(req, res):
  req.parse_qs()
  yield from ok(res, model.load(req.form['name'][0]))

def set(req, res):
  yield from req.read_form_data()
  cfg = model.load()
  for key, value in req.form.items():
    cfg[key] = value
  model.save(cfg)
  yield from get(req, res)

def reset(req, res):
  yield from ok(res)
  model.reset()

def factory(req, res):
  model.factory()
  from wlan.ap import controller
  yield from controller.get(req, res)

def crud(req, res):
  ret = {
    'GET': get,
    'PUT': set
  }
  yield from ret[req.method](req, res)

class Config:
  def __init__(self, filename):
    self.filename = filename

  def load(self):
    f = open(self.filename)
    data = ujson.load(f)
    f.close()
    return data

  def save(self, data):
    f = open(self.filename, 'w')
    ujson.dump(data, f)
    f.close()
    return self

@@ -1,12 +1,25 @@
+import ujson%0A
 import picow

add missing module ujson