⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treeselect.py

📁 监控大型网络的软件。能够自动发现拓扑结构
💻 PY
📖 第 1 页 / 共 2 页
字号:
                option.selected = True        return    def addDefaultOptions(self):        " Adds the default options for this select. Called for each init "        for option in self.optionsDefault:            self.options.append(option)        return    def lastKeys(self):        """ Makes hidden inputs for the form. Last_keys tells what keys in         the previous table which were used to get these keys. FIX """        output = ''        for option in self.options:            if option.selected:                output += '<input type="hidden" name="%s_%s" value="%s">\n' \                % (self.elementName, option.value, option.last_key)        return output    def getAttribs(self):        """        Returns the html attributes for this select element        """        attribs = 'size="' + str(self.multipleSize) + '"'        if self.multiple:            attribs += ' multiple="multiple"'         return attribs################################################### class UpdateableSelectclass UpdateableSelect(Select):    """     UpdateableSelect inherits from Select. It can be dynamically updated    based on the table in updateFromTable. The updateOptions() method must    be called when the selections in the previous select has changed.    """    updateFromTable = None    textColumn = None    idColumn = None    foreignColumn = None       def __init__(self,                 prevElement,                 elementName,                 title,                 updateFromTable,                 textColumn,                 idColumn,                 foreignColumn,                 optionsDefault = None,                 multiple = False,                 multipleSize = None,                 initTable = None,                 initTextColumn = None,                 initIdColumn = None,                 onchange = 'this.form.submit()',                 preSelected = [],                 optionFormat = None,                 optgroupFormat = None,                 orderByValue = False):        """         prevElement = the previous select in the TreeSelect        updateFromTable = the table used to dynamically update the select        textColumn = the column containing the text for the Option()        idColumn = the column containing the primary key (value for Option)        foreignColumn = corresponding to the primary key in the last select        """        # link together with the last select        self.prevElement = prevElement        prevElement.nextElement = self        self.updateFromTable = updateFromTable        self.textColumn = textColumn        self.idColumn = idColumn        self.foreignColumn = foreignColumn        # call the constructor of the anecstor (Select)        Select.__init__(self,                        elementName,                        title,                        optionsDefault = optionsDefault,                        multiple = multiple,                        multipleSize = multipleSize,                        initTable = initTable,                        initTextColumn = initTextColumn,                        initIdColumn = initIdColumn,                        onchange = onchange,                        preSelected = preSelected,                        optionFormat = optionFormat,                        optgroupFormat = optgroupFormat,                        orderByValue = orderByValue)        return    def updateOptions(self, selectedList):        """ Takes a list of selected entries (ids) from the previous table        and updates the options in this select. """        table = getattr(nav.db.manage, self.updateFromTable)        for selected in selectedList:            optgroupText = selected            if self.prevElement.initTable:                # this is the first updateable select, so get optgroup                # text from the initTable of the first select element                prevTable = getattr(nav.db.manage, self.prevElement.initTable)                optgroupDescr = str(getattr(prevTable(selected),\                                    self.prevElement.initTextColumn))                optgroupValue = str(selected)                if self.optgroupFormat:                    optgroupText = self.optgroupFormat                    optgroupText = optgroupText.replace('$v',optgroupValue)                    optgroupText = optgroupText.replace('$d',optgroupDescr)                else:                    optgroupText = optgroupDescr + ' (' + optgroupValue + ')'            else:                # this is any other updateable select, so get opgroup                # text from the last updateable select                prevTable = getattr(nav.db.manage, self.prevElement.updateFromTable)                optgroupDescr = str(getattr(prevTable(selected),\                                    self.prevElement.textColumn))                optgroupValue = str(selected)                if self.optgroupFormat:                    optgroupText = self.optgroupFormat                    optgroupText = optgroupText.replace('$v',optgroupValue)                    optgroupText = optgroupText.replace('$d',optgroupDescr)                else:                    optgroupText = optgroupDescr + ' (' + optgroupValue + ')'             self.options.append(Option('',optgroupText,optgroup=True))            where_clause = self.foreignColumn + " = '" + selected + "'"             ob = self.textColumn            if self.orderByValue:                ob = self.idColumn            for row in table.getAllIterator((where_clause),                                            orderBy=ob):                value = getattr(row, self.idColumn)                descr = getattr(row, self.textColumn)                if not type(value) is str:                    value = repr(value)                if not type(descr) is str:                    descr = repr(descr)                if self.optionFormat:                    text = self.optionFormat                    text = text.replace('$v',value)                    text = text.replace('$d',descr)                else:                    text = descr                self.options.append(Option(text,value,selected = False,\                last_key=selected))        return# Class SimpleSelectclass SimpleSelect(Select):    # Don't update this with TreeSelect.update()    updateThis = False    onchange = None    def __init__(self,                 elementName,                 title,                 initTable,                 initTextColumn,                 initIdColumn,                 initIdList,                 multiple = False,                 multipleSize = None,                 optionFormat=None,                 orderByValue=False):        self.options = []        self.elementName = elementName        self.title = title        self.initTable = initTable        self.initTextColumn = initTextColumn        self.initIdColumn = initIdColumn        self.initIdList = initIdList        self.multiple = multiple        self.multipleSize = multipleSize        self.optionFormat = optionFormat        self.orderByValue = orderByValue        self.initOptions()    def initOptions(self):        " Populate the options list from the initTable. "        table = getattr(nav.db.manage, self.initTable)        ob = self.initTextColumn        if self.orderByValue:            ob = self.initIdColumn        if len(self.initIdList):            where = ''            for id in self.initIdList:                if where:                    where += " OR "                where += self.initIdColumn + "='%s' " % (id,)            for row in table.getAllIterator(orderBy=ob,where=where):                descr = getattr(row, self.initTextColumn)                value = getattr(row, self.initIdColumn)                if self.optionFormat:                    text = self.optionFormat                    text = text.replace('$v',str(value))                    text = text.replace('$d',str(descr))                else:                    text = descr                selected = False                                self.options.append(Option(text,value,selected))         return        def lastKeys(self):        return None################################################### class Optionclass Option:    """ 'Struct' describing a single option in any select """    def __init__(self, text, value, last_key = None, selected = False, \    optgroup = False, disabled=False):        self.last_key = last_key        self.disabled = disabled        self.value = value        self.text = text        self.selected = selected        self.optgroup = optgroup        return

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -