예제 #1
0
파일: ui.py 프로젝트: pyq881120/flare-wmi
    def __init__(self, ctx, parent=None):
        super(CimUiForm, self).__init__(parent)
        self._ctx = ctx
        self._tree_model = TreeModel(
                CimRootItem(ctx),
                [
                    ColumnDef("Name", "name"),
                    ColumnDef("Type", "type"),
                ])

        # TODO: maybe subclass the loaded .ui and use that instance directly

        uipath = os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.join("ui", "ui.ui"))
        self._ui = uic.loadUi(uipath)
        emptyLayout(self._ui.browseDetailsLayout)

        tv = self._ui.browseTreeView
        tv.setModel(self._tree_model)
        tv.header().setSectionResizeMode(QHeaderView.Interactive)
        tv.header().resizeSection(0, 250)  # chosen empirically
        tv.activated.connect(self._handle_browse_item_activated)

        self._query_model = QStandardItemModel(self._ui.queryResultsList)
        self._ui.queryResultsList.setModel(self._query_model)
        self._ui.queryInputLineEdit.returnPressed.connect(self._handle_query)
        self._ui.queryInputActionButton.clicked.connect(self._handle_query)
        self._ui.queryResultsList.activated.connect(self._handle_results_item_activated)

        self._save_buffer = None
        self._ui.queryResultsSaveButton.clicked.connect(self._handle_save)

        mainLayout = QGridLayout()
        mainLayout.addWidget(self._ui, 0, 0)

        self.setLayout(mainLayout)
        self.setWindowTitle("cim - ui")
예제 #2
0
파일: ui.py 프로젝트: pyq881120/flare-wmi
class CimUiForm(QWidget, LoggingObject):
    def __init__(self, ctx, parent=None):
        super(CimUiForm, self).__init__(parent)
        self._ctx = ctx
        self._tree_model = TreeModel(
                CimRootItem(ctx),
                [
                    ColumnDef("Name", "name"),
                    ColumnDef("Type", "type"),
                ])

        # TODO: maybe subclass the loaded .ui and use that instance directly

        uipath = os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.join("ui", "ui.ui"))
        self._ui = uic.loadUi(uipath)
        emptyLayout(self._ui.browseDetailsLayout)

        tv = self._ui.browseTreeView
        tv.setModel(self._tree_model)
        tv.header().setSectionResizeMode(QHeaderView.Interactive)
        tv.header().resizeSection(0, 250)  # chosen empirically
        tv.activated.connect(self._handle_browse_item_activated)

        self._query_model = QStandardItemModel(self._ui.queryResultsList)
        self._ui.queryResultsList.setModel(self._query_model)
        self._ui.queryInputLineEdit.returnPressed.connect(self._handle_query)
        self._ui.queryInputActionButton.clicked.connect(self._handle_query)
        self._ui.queryResultsList.activated.connect(self._handle_results_item_activated)

        self._save_buffer = None
        self._ui.queryResultsSaveButton.clicked.connect(self._handle_save)

        mainLayout = QGridLayout()
        mainLayout.addWidget(self._ui, 0, 0)

        self.setLayout(mainLayout)
        self.setWindowTitle("cim - ui")

    def _handle_browse_item_activated(self, itemIndex):
        item = self._tree_model.getIndexData(itemIndex)
        details = self._ui.browseDetails
        details_layout = self._ui.browseDetailsLayout
        emptyLayout(details_layout)

        if isinstance(item, PhysicalDataPageItem):
            v = PhysicalDataPageItemView(item, details)
            details_layout.addWidget(v)

        elif isinstance(item, LogicalDataPageItem):
            v = LogicalDataPageItemView(item, details)
            details_layout.addWidget(v)

        elif isinstance(item, PhysicalIndexPageItem):
            v = PhysicalIndexPageItemView(item, details)
            details_layout.addWidget(v)

        elif isinstance(item, LogicalIndexPageItem):
            v = LogicalIndexPageItemView(item, details)
            details_layout.addWidget(v)

        elif isinstance(item, IndexNodeItem):
            v = IndexNodeItemView(item, details)
            details_layout.addWidget(v)

        elif isinstance(item, IndexKeyItem):
            v = IndexKeyItemView(item, details)
            details_layout.addWidget(v)

        elif isinstance(item, ClassDefinitionItem):
            v = ClassDefinitionItemView(item, details)
            details_layout.addWidget(v)

        elif isinstance(item, ClassInstanceItem):
            v = ClassInstanceItemView(item, details)
            details_layout.addWidget(v)

    def _handle_query(self):
        viewLayout = self._ui.queryResultsViewLayout
        emptyLayout(viewLayout)
        self._query_model.clear()

        query_text = self._ui.queryInputLineEdit.text()
        self.d("query: %s", query_text)
        if query_text.startswith("NS_"):
            self.d("key query")
            for o in self._ctx.object_resolver.get_keys(Key(query_text)):
                self.d("  query result: %s", o)
                item = QStandardItem(o.human_format)
                item.setData(o)
                self._query_model.appendRow(item)
        elif query_text.lower().startswith("root"):
            self.d("logical query (missing host)")
            # TODO: reference to Moniker is broken
            # need reference to a namespace to .parse_object_path(), and .get()
            m = Moniker("//./" + query_text)
            self._handle_moniker_query(m)
        elif query_text.lower().startswith("//"):
            self.d("logical query")
            m = Moniker(query_text)
            self._handle_moniker_query(m)
        elif query_text.lower().startswith("winmgmts:"):
            self.d("logical query (winmgmnts)")
            m = Moniker(query_text)
            self._handle_moniker_query(m)
        else:
            self.w("unknown query schema: %s", query_text)

    def _handle_moniker_query(self, moniker):
        if moniker.instance:
            item = ClassInstanceItem(self._ctx, moniker.namespace, moniker.klass, moniker.instance)
            model_item = QStandardItem(str(moniker))
            model_item.setData(item)
            self._query_model.appendRow(model_item)
        elif moniker.klass:
            model_item = QStandardItem(str(moniker))
            model_item.setData(ClassDefinitionItem(self._ctx, moniker.namespace, moniker.klass))
            self._query_model.appendRow(model_item)
        elif moniker.namespace:
            item = NamespaceItem(self._ctx, moniker.namespace)
            model_item = QStandardItem(str(moniker))
            model_item.setData(item)
            self._query_model.appendRow(model_item)
        else:
            self.w("no meaningful moniker fields: %s", str(moniker))

    def _handle_results_item_activated(self, itemIndex):
        emptyLayout(self._ui.queryResultsViewLayout)
        item = self._query_model.itemFromIndex(itemIndex).data()

        if isinstance(item, Key):
            buf = self._ctx.object_resolver.get_object(item)
            self._save_buffer = buf
            hv = HexViewWidget(buf, self._ui.queryResultsViewFrame)
            self._ui.queryResultsViewLayout.addWidget(hv)

        elif isinstance(item, ClassDefinitionItem):
            v = ClassDefinitionItemView(item, self._ui.queryResultsViewFrame)
            self._save_buffer = item.data
            self._ui.queryResultsViewLayout.addWidget(v)

        elif isinstance(item, ClassInstanceItem):
            v = ClassInstanceItemView(item, self._ui.queryResultsViewFrame)
            self._save_buffer = item.data
            self._ui.queryResultsViewLayout.addWidget(v)

        elif isinstance(item, NamespaceItem):
            l = QLabel(self._ui.queryResultsViewFrame)
            l.setText("Don't know how to display namespaces yet")
            self.w("dont know how to display namespaces")
            self._ui.queryResultsViewLayout.addWidget(l)

    def _handle_save(self):
        if self._save_buffer is None:
            return

        filename, filter = QFileDialog.getSaveFileName(self, "Save binary...", QDir.currentPath(), "Binary files (*.bin)")
        if not filename:
            return

        with open(filename, "wb") as f:
            f.write(self._save_buffer)