addon-sdk/source/python-lib/cuddlefish/options_xul.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 # This Source Code Form is subject to the terms of the Mozilla Public
     2 # License, v. 2.0. If a copy of the MPL was not distributed with this
     3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
     5 from xml.dom.minidom import Document
     7 VALID_PREF_TYPES = ['bool', 'boolint', 'integer', 'string', 'color', 'file',
     8                     'directory', 'control', 'menulist', 'radio']
    10 class Error(Exception):
    11     pass
    13 class BadPrefTypeError(Error):
    14     pass
    16 class MissingPrefAttr(Error):
    17     pass
    19 def validate_prefs(options):
    20     for pref in options:
    21         # Make sure there is a 'title'
    22         if ("title" not in pref):
    23             raise MissingPrefAttr("The '%s' pref requires a 'title'" % (pref["name"]))
    25         # Make sure that the pref type is a valid inline pref type
    26         if (pref["type"] not in VALID_PREF_TYPES):
    27             raise BadPrefTypeError('%s is not a valid inline pref type' % (pref["type"]))
    29         # Make sure the 'control' type has a 'label'
    30         if (pref["type"] == "control"):
    31             if ("label" not in pref):
    32                 raise MissingPrefAttr("The 'control' inline pref type requires a 'label'")
    34         # Make sure the 'menulist' type has a 'menulist'
    35         if (pref["type"] == "menulist" or pref["type"] == "radio"):
    36             if ("options" not in pref):
    37                 raise MissingPrefAttr("The 'menulist' and the 'radio' inline pref types requires a 'options'")
    39             # Make sure each option has a 'value' and a 'label'
    40             for item in pref["options"]:
    41                 if ("value" not in item):
    42                     raise MissingPrefAttr("'options' requires a 'value'")
    43                 if ("label" not in item):
    44                     raise MissingPrefAttr("'options' requires a 'label'")
    46         # TODO: Check that pref["type"] matches default value type
    48 def parse_options(options, jetpack_id, preferencesBranch):
    49     doc = Document()
    50     root = doc.createElement("vbox")
    51     root.setAttribute("xmlns", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
    52     doc.appendChild(root)
    54     for pref in options:
    55         if ("hidden" in pref and pref["hidden"] == True):
    56             continue;
    58         setting = doc.createElement("setting")
    59         setting.setAttribute("pref-name", pref["name"])
    60         setting.setAttribute("data-jetpack-id", jetpack_id)
    61         setting.setAttribute("pref", "extensions." + preferencesBranch + "." + pref["name"])
    62         setting.setAttribute("type", pref["type"])
    63         setting.setAttribute("title", pref["title"])
    65         if ("description" in pref):
    66             setting.appendChild(doc.createTextNode(pref["description"]))
    68         if (pref["type"] == "control"):
    69             button = doc.createElement("button")
    70             button.setAttribute("pref-name", pref["name"])
    71             button.setAttribute("data-jetpack-id", jetpack_id)
    72             button.setAttribute("label", pref["label"])
    73             button.setAttribute("oncommand", "Services.obs.notifyObservers(null, '" +
    74                                               jetpack_id + "-cmdPressed', '" +
    75                                               pref["name"] + "');");
    76             setting.appendChild(button)
    77         elif (pref["type"] == "boolint"):
    78             setting.setAttribute("on", pref["on"])
    79             setting.setAttribute("off", pref["off"])
    80         elif (pref["type"] == "menulist"):
    81             menulist = doc.createElement("menulist")
    82             menupopup = doc.createElement("menupopup")
    83             for item in pref["options"]:
    84                 menuitem = doc.createElement("menuitem")
    85                 menuitem.setAttribute("value", item["value"])
    86                 menuitem.setAttribute("label", item["label"])
    87                 menupopup.appendChild(menuitem)
    88             menulist.appendChild(menupopup)
    89             setting.appendChild(menulist)
    90         elif (pref["type"] == "radio"):
    91             radiogroup = doc.createElement("radiogroup")
    92             for item in pref["options"]:
    93                 radio = doc.createElement("radio")
    94                 radio.setAttribute("value", item["value"])
    95                 radio.setAttribute("label", item["label"])
    96                 radiogroup.appendChild(radio)
    97             setting.appendChild(radiogroup)
    99         root.appendChild(setting)
   101     return doc.toprettyxml(indent="  ")

mercurial