Пример #1
0
 /*!
  * @brief Format a WSGI application response as an HTTP response.
  * @param status HTTP status line (without the HTTP version)
  * @param headers a list of pairs of strings with HTTP headers
  * @param body the response body
  */
 void operator() ( const py::Bytes& status,
     const py::List& headers, const py::Bytes& body )
 {
       // Send status line.
     myStream
         << "HTTP/1.1 " << status << "\r\n";
     bool contentlength = false;
       // Send headers.
     for ( py::ssize_t i = 0; (i < headers.size()); ++i )
     {
         const py::Tuple header(headers[i]);
         const py::Bytes field(header[0]);
         const py::Bytes value(header[1]);
         if ( field == "Content-Length" ) {
             contentlength = true;
         }
         myStream
             << field << ": " << value << "\r\n";
     }
       // More headers (if desired).
     if ( !contentlength ) {
         myStream
             << "Content-Length" << ": " << body.size() << "\r\n";
     }
       // Send body.
     myStream
         << "\r\n" << body;
 }
Пример #2
0
PyObject* AttachEnginePy::getModeInfo(PyObject* args)
{
    char* modeName;
    if (!PyArg_ParseTuple(args, "s", &modeName))
        return 0;

    try {
        AttachEngine &attacher = *(this->getAttachEnginePtr());
        eMapMode mmode = attacher.getModeByName(modeName);
        Py::List pyListOfCombinations;
        Py::List pyCombination;
        refTypeStringList &listOfCombinations = attacher.modeRefTypes.at(mmode);
        for(const refTypeString &combination: listOfCombinations){
            pyCombination = Py::List(combination.size());
            for(int iref = 0   ;   iref < combination.size()   ;   iref++){
                pyCombination[iref] = Py::String(AttachEngine::getRefTypeName(combination[iref]));
            }
            pyListOfCombinations.append(pyCombination);
        }
        Py::Dict ret;
        ret["ReferenceCombinations"] = pyListOfCombinations;
        ret["ModeIndex"] = Py::Int(mmode);

        try {
            Py::Module module(PyImport_ImportModule("PartGui"),true);
            if (!module.hasAttr("AttachEngineResources")) {
                // in v0.14+, the GUI module can be loaded in console mode (but doesn't have all its document methods)
                throw Py::RuntimeError("Gui is not up");//DeepSOIC: wanted to throw ImportError here, but it's not defined, so I don't know...
            }
            Py::Object submod(module.getAttr("AttachEngineResources"));
            Py::Callable method(submod.getAttr("getModeStrings"));
            Py::Tuple arg(2);
            arg.setItem(0, Py::String(this->getAttachEnginePtr()->getTypeId().getName()));
            arg.setItem(1, Py::Int(mmode));
            Py::List strs = method.apply(arg);
            assert(strs.size() == 2);
            ret["UserFriendlyName"] = strs[0];
            ret["BriefDocu"] = strs[1];
        } catch (Py::Exception& e) {
            if (PyErr_ExceptionMatches(PyExc_ImportError)) {
                // the GUI is not up.
                Base::Console().Warning("AttachEngine: Gui not up, so no gui-related entries in getModeInfo.\n");
                e.clear();
            } else {
                Base::Console().Warning("AttachEngine.getModeInfo: error obtaining GUI strings\n");
                e.clear();
            }
        } catch (Base::Exception &e){
            Base::Console().Warning("AttachEngine.getModeInfo: error obtaining GUI strings:");
            Base::Console().Warning(e.what());
            Base::Console().Warning("\n");
        }

        return Py::new_reference_to(ret);
    } ATTACHERPY_STDCATCH_METH;
}