qmsk/dmx/web.py
changeset 96 e6dfc98ec50f
parent 88 c923295ee520
equal deleted inserted replaced
95:478b9d0485aa 96:e6dfc98ec50f
     1 import pvl.web
     1 from qmsk.web import urls
     2 from pvl.web import urls, html
     2 from qmsk.web.application import Application
       
     3 from qmsk.web.html import HTMLHandler
       
     4 from qmsk.web.html import html5 as html
     3 
     5 
     4 import collections
     6 import collections
     5 import logging; log = logging.getLogger('qmsk.dmx.web')
     7 import logging; log = logging.getLogger('qmsk.dmx.web')
     6 
     8 
     7 def colorize (x, red, green, blue, alpha=1.0) :
     9 def colorize (x, red, green, blue, alpha=1.0) :
    35     return slider(head, c)
    37     return slider(head, c)
    36 
    38 
    37 def head_color (head, value) :
    39 def head_color (head, value) :
    38     return html.div(class_='dmx-color-background')(colorize(html.div(id='-'.join([head, 'color']), class_='dmx-color')(' '), **value))
    40     return html.div(class_='dmx-color-background')(colorize(html.div(id='-'.join([head, 'color']), class_='dmx-color')(' '), **value))
    39 
    41 
    40 class Handler (pvl.web.Handler) :
    42 class Handler (HTMLHandler) :
    41     # Bootstrap
    43     # Bootstrap
    42     DOCTYPE = 'html'
       
    43     HTML_XMLNS = None
       
    44     HTML_LANG = 'en'
       
    45     CSS = (
    44     CSS = (
    46             '//code.jquery.com/ui/1.10.4/themes/ui-darkness/jquery-ui.css',
    45             '//code.jquery.com/ui/1.10.4/themes/ui-darkness/jquery-ui.css',
    47             '//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css',
    46             '//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css',
    48 
    47 
    49             '/static/dmx.css',
    48             '/static/dmx.css',
    62 
    61 
    63     def process (self) :
    62     def process (self) :
    64         if self.request.method == 'POST' :
    63         if self.request.method == 'POST' :
    65             update = collections.defaultdict(dict)
    64             update = collections.defaultdict(dict)
    66 
    65 
    67             for name, value in self.request.form.iteritems() :
    66             for name, value in self.request.form.items() :
    68                 head_name, attr = name.split('-', 1)
    67                 head_name, attr = name.split('-', 1)
    69                 head = self.app.universe[head_name]
    68                 head = self.app.universe[head_name]
    70 
    69 
    71                 if value:
    70                 if value:
    72                     update[head][attr] = int(value)
    71                     update[head][attr] = int(value)
   123 
   122 
   124                 html.button(type='submit', class_='btn btn-primary')("Go"),
   123                 html.button(type='submit', class_='btn btn-primary')("Go"),
   125             )
   124             )
   126         )
   125         )
   127 
   126 
   128 class DMXWebApplication (pvl.web.Application) :
   127 class DMXWebApplication (Application) :
   129     URLS = urls.Map((
   128     URLS = urls.rules({
   130         urls.rule('/',          Handler),
   129         '/':                    Handler,
   131     ))
   130     })
   132 
   131 
   133     def __init__ (self, universe, **opts) :
   132     def __init__ (self, universe, **opts) :
   134         """
   133         """
   135             universe    - qmsk.dmx.heads.Universe
   134             universe    - qmsk.dmx.heads.Universe
   136         """
   135         """
   142     def update (self, update) :
   141     def update (self, update) :
   143         """
   142         """
   144             Update given {head: {attr: value}}
   143             Update given {head: {attr: value}}
   145         """
   144         """
   146         
   145         
   147         for head, attrs in update.iteritems():
   146         for head, attrs in update.items():
   148             self.universe.update(head, attrs)
   147             self.universe.update(head, attrs)
   149 
   148