PyObject* Application::sGetMainWindow(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) { if (!PyArg_ParseTuple(args, "")) return NULL; PythonWrapper wrap; wrap.loadCoreModule(); wrap.loadGuiModule(); try { return Py::new_reference_to(wrap.fromQWidget(Gui::getMainWindow(), "QMainWindow")); } catch (const Py::Exception&) { return 0; } }
Py::Object ViewProviderPy::getIcon(void) const { #if 0 QByteArray ba; QDataStream str(&ba, QIODevice::WriteOnly); QIcon icon = getViewProviderPtr()->getIcon(); str << icon; return Py::String(ba.constData(), ba.size()); #else PythonWrapper wrap; wrap.loadGuiModule(); QIcon icon = getViewProviderPtr()->getIcon(); return wrap.fromQIcon(new QIcon(icon)); #endif }
// build dom tree from python lxml dom tree. DomNode* build_dom_tree(DomNode* parent, PyObject* node, map<DomNode*, PyObject*>& relation) { const char* tag = python.get_attr_string(node, "tag"); const char* inner_text = python.get_attr_string(node, "text"); const char* inner_tail = python.get_attr_string(node, "tail"); string text; if (inner_text != NULL) { text.append(inner_text); } if (inner_tail != NULL) { if (parent != NULL) { parent->append_text(inner_tail); } } DomNode* dom_node = new DomNode(tag, text); relation[dom_node] = node; //PyObject* parent = python.invoke_object(node, "getparent"); FILE* fp = fopen("object.txt", "w+"); PyObject_Print(node, fp, Py_PRINT_RAW); fclose(fp); PyObject* children = python.invoke_object(node, "getchildren"); if (children != NULL) { int count = PyList_GET_SIZE(children); for (int i = 0; i < count; ++i) { PyObject* child = PyList_GET_ITEM(children, i); if (PyObject_IsInstance(child, tag_type) != 1) { continue; } DomNode* child_node = build_dom_tree(dom_node, child, relation); dom_node->append_child(child_node); } } dom_node->add_attribute("class", get_dom_attr(node, "class")); dom_node->add_attribute("id", get_dom_attr(node, "id")); return dom_node; }
void node_dropped(DomNode* node) { map<DomNode*, PyObject*>::iterator iter = dom_relation.find(node); if (iter != dom_relation.end()) { PyObject* py_node = iter->second; python.invoke_object(py_node, "drop_tree"); } }
PyObject * wrapEntity(LocatedEntity * le) { PyObject * wrapper; PythonWrapper * pw = dynamic_cast<PythonWrapper *>(le->script()); if (pw == 0) { Entity * entity = dynamic_cast<Entity *>(le); if (entity != 0) { Character * ch_entity = dynamic_cast<Character *>(entity); if (ch_entity != 0) { PyEntity * pc = newPyCharacter(); if (pc == NULL) { return NULL; } pc->m_entity.c = ch_entity; wrapper = (PyObject *)pc; } else { PyEntity * pe = newPyEntity(); if (pe == NULL) { return NULL; } pe->m_entity.e = entity; wrapper = (PyObject *)pe; } } else { PyEntity * pe = newPyLocatedEntity(); if (pe == NULL) { return NULL; } pe->m_entity.l = le; wrapper = (PyObject *)pe; } if (le->script() == &noScript) { pw = new PythonWrapper(wrapper); le->setScript(pw); } else { log(WARNING, "Entity has script of unknown type"); } } else { wrapper = pw->wrapper(); assert(wrapper != NULL); Py_INCREF(wrapper); } return wrapper; }
PyObject* Application::sGetMainWindow(PyObject * /*self*/, PyObject *args) { if (!PyArg_ParseTuple(args, "")) return NULL; PythonWrapper wrap; if (!wrap.loadCoreModule() || !wrap.loadGuiModule() || !wrap.loadWidgetsModule()) { PyErr_SetString(PyExc_RuntimeError, "Failed to load Python wrapper for Qt"); return 0; } try { return Py::new_reference_to(wrap.fromQWidget(Gui::getMainWindow(), "QMainWindow")); } catch (const Py::Exception&) { return 0; } }
PyObject * wrapTask(Task * task) { PyObject * wrapper; Task * ts = dynamic_cast<Task *>(task); PythonWrapper * pw; if (ts == 0 || ((pw = dynamic_cast<PythonWrapper *>(ts->script())) == 0)) { PyTask * pt = newPyTask(); if (pt != NULL) { pt->m_task = task; } wrapper = (PyObject *)pt; // This wrapper cannot be stashed back int the task yet so // we don't have to do this next time. } else { wrapper = pw->wrapper(); assert(wrapper != NULL); Py_INCREF(wrapper); } return wrapper; }
void test_file(const string& html) { PyObject* py_dom = NULL; if (!python.invoke_string(module, "fromstring", html, py_dom)) { EXPECT_TRUE(false); } // build dom tree from python lxml dom tree. DomNode* dom = build_dom_tree(NULL, py_dom, dom_relation); // get html text of dom tree by call python lxml tostring method. const char* body_str = python.invoke(module, "tostring", dom_relation[dom]); // write. write_file("news.ori.html", body_str); // init visitor IDomNodeVisitor* visitor = new DomNodeVisitor(); // init transcoder Transcoder transcoder("transcoder.ini", visitor); /* // extract dom DomNode* body = extractor.extract(dom); EXPECT_TRUE(body != NULL); if (body != NULL) { cout << "body result " << body->get_tag() << " " << body->get_attribute("class") << " " << body->get_attribute("id") << endl; FILE* fp = fopen("object.txt", "w+"); PyObject_Print(py_dom, fp, Py_PRINT_RAW); fclose(fp); const char* body_str = python.invoke(module, "tostring", dom_relation[body]); write_file("news.out.html", body_str); } stringstream text; print_dom(dom, text); dom_relation.clear(); delete dom; */ }
int main(int argc, char * argv[]) { // Declare the supported options. std::string OptionsConfigFile; po::options_description desc("Allowed options"); desc.add_options() ("help", "Prints this help message") ("config", po::value< std::string >(&OptionsConfigFile)->default_value(NoTouchScreenDefaultConfigFile), "Override the default config file") ; // Parsing po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if( vm.count("help") ) { std::cout << desc << std::endl; return 0; } try { //ActionManager::Instance().FillRegisterWithConfigFile(OptionsConfigFile); // Config with a python script PythonWrapper wrapper; wrapper.test(); NoTouchScreen instance; instance.MainLoop(); } catch(NoTouchScreenException& e) { e.print(); } return 0; }
TEST(Transcoder, main) { // init python wrapper EXPECT_TRUE(python.init()); EXPECT_TRUE(python.import_module(module)); DomNode::register_node_dropped(node_dropped); tag_type = PyDict_GetItemString(python._modules["lxml.html"], "HtmlElement"); stringstream text; read_file("sina.html", text); test_file(text.str()); /* // init dom visitor DomNodeVisitor* visitor = new DomNodeVisitor(); // get dom node DomNodeKey* node = new DomNodeKey(); // init config Config cfg; cfg.Init("transcoder.ini"); // get feature repository FeatureRepository* fr = FeatureRepository::get_instance(); fr->init(); map<int, FEATURE_EXTRACTOR_PTR> features = fr->get_features(); vector<char*> names = fr->get_feature_names(); for (int i=0; i < names.size(); i++) { cout << "name: "; cout << names[i]; cout << ", id: "; cout << i; cout << ", result: "; cout << (features.find(i)->second)(cfg, visitor, node) << endl; } */ }
void ViewProviderPythonFeatureImp::setupContextMenu(QMenu* menu) { // Run the attach method of the proxy object. Base::PyGILStateLocker lock; try { App::Property* proxy = object->getPropertyByName("Proxy"); if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { Py::Object vp = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); if (vp.hasAttr(std::string("setupContextMenu"))) { if (vp.hasAttr("__object__")) { PythonWrapper wrap; wrap.loadGuiModule(); wrap.loadWidgetsModule(); Py::Callable method(vp.getAttr(std::string("setupContextMenu"))); Py::Tuple args(1); args.setItem(0, wrap.fromQWidget(menu, "QMenu")); method.apply(args); } else { PythonWrapper wrap; wrap.loadGuiModule(); wrap.loadWidgetsModule(); Py::Callable method(vp.getAttr(std::string("setupContextMenu"))); Py::Tuple args(2); args.setItem(0, Py::Object(object->getPyObject(), true)); args.setItem(1, wrap.fromQWidget(menu, "QMenu")); method.apply(args); } } } } catch (Py::Exception&) { Base::PyException e; // extract the Python error text e.ReportException(); } }