Ignore:
Timestamp:
Aug 1, 2012 10:33:14 AM (12 years ago)
Author:
gah
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lang/python/Rappture/PyRpLibrary.cc

    r3119 r3127  
    4747} RpLibraryObject;
    4848
    49 /*
    50  * Check to see if this object is a Rappture.library object.
    51  * This does not check to see if the object is correctly
    52  * initialized. To check for correct object type and
    53  * correct initialization, use RpLibraryObject_IsValid.
    54  */
    55 static int
    56 RpLibraryObject_Check(PyObject *objPtr)
    57 {
    58     if (objPtr == NULL) {
    59         return FALSE;
    60     }
    61     if (strcmp(objPtr->ob_type->tp_name, "Rappture.library") != 0) {
    62         return FALSE;
    63     }
    64     return TRUE;
    65 }
    66 
     49
     50static int
     51getArgCount(PyObject *args, PyObject *keywds, int *argc)
     52{
     53    int args_cnt = 0;
     54    int keywds_cnt = 0;
     55
     56    if (argc == NULL) {
     57        // incorrect use of function
     58        // argc cannot be null
     59        PyErr_Format(PyExc_ValueError,"getArgCount(): argc is NULL");
     60        return RP_ERROR;
     61    }
     62    if (args != NULL) {
     63        if (!PyTuple_Check(args)) {
     64            PyErr_Format(PyExc_TypeError,
     65                "getArgCount(): \'args\' should be a PyTuple");
     66            return RP_ERROR;
     67        }
     68        args_cnt = PyTuple_Size(args);
     69    }
     70    if (keywds != NULL) {
     71        if (!PyDict_Check(keywds)) {
     72            PyErr_Format(PyExc_TypeError,
     73                "getArgCount(): \'keywds\' should be a PyDict");
     74            return RP_ERROR;
     75        }
     76        keywds_cnt = PyDict_Size(keywds);
     77    }
     78    *argc = args_cnt + keywds_cnt;
     79    return RP_OK;
     80}
    6781
    6882/*
     
    171185}
    172186
    173 static int
    174 getArgCount (PyObject *args, PyObject *keywds, int *argc)
    175 {
    176     int args_cnt = 0;
    177     int keywds_cnt = 0;
    178 
    179     if (argc == NULL) {
    180         // incorrect use of function
    181         // argc cannot be null
    182         PyErr_Format(PyExc_ValueError,"getArgCount(): argc is NULL");
    183         return RP_ERROR;
    184     }
    185     if (args != NULL) {
    186         if (!PyTuple_Check(args)) {
    187             PyErr_Format(PyExc_TypeError,
    188                 "getArgCount(): \'args\' should be a PyTuple");
    189             return RP_ERROR;
    190         }
    191         args_cnt = PyTuple_Size(args);
    192     }
    193     if (keywds != NULL) {
    194         if (!PyDict_Check(keywds)) {
    195             PyErr_Format(PyExc_TypeError,
    196                 "getArgCount(): \'keywds\' should be a PyDict");
    197             return RP_ERROR;
    198         }
    199         keywds_cnt = PyDict_Size(keywds);
    200     }
    201     *argc = args_cnt + keywds_cnt;
    202     return RP_OK;
     187/*
     188 * Check to see if this object is a Rappture.library object.
     189 * This does not check to see if the object is correctly
     190 * initialized. To check for correct object type and
     191 * correct initialization, use RpLibraryObject_IsValid.
     192 */
     193static int
     194RpLibraryObject_Check(PyObject *objPtr)
     195{
     196    if (objPtr == NULL) {
     197        return FALSE;
     198    }
     199    if (strcmp(objPtr->ob_type->tp_name, "Rappture.library") != 0) {
     200        return FALSE;
     201    }
     202    return TRUE;
    203203}
    204204
     
    249249    PyObject *objPtr;
    250250
     251    objPtr = NULL;
    251252    if (!PyArg_ParseTuple(args, "|O", &objPtr)) {
    252253        PyErr_Format(PyExc_TypeError,
     
    254255        return -1;
    255256    }
     257
    256258    if (objPtr != NULL) {
    257259        if (PyString_Check(objPtr)) {
    258             const char *filename;
     260            char *filename = NULL;
    259261
    260262            filename = PyString_AsString(objPtr);
     
    263265            }
    264266            self->lib = new RpLibrary(std::string(filename));
    265         } else if (RpLibraryObject_IsValid(objPtr)) {
     267        }
     268        else if (RpLibraryObject_IsValid(objPtr)) {
    266269            self->lib = new RpLibrary( *(RpLibraryObject_AsLibrary(objPtr)) );
    267         } else if (RpLibraryObject_Check(objPtr)) {
     270        }
     271        else if (RpLibraryObject_Check(objPtr)) {
    268272            self->lib = new RpLibrary();
    269         } else {
     273        }
     274        else {
    270275            PyErr_Format(PyExc_TypeError,"unrecognized object type");
    271276            return -1;
    272         }
    273     } else {
     277        }
     278    }
     279    else {
    274280        self->lib = new RpLibrary();
    275281    }
    276282
    277     return -1;
     283    return 1;
    278284}
    279285
     
    408414
    409415    if (getArgCount(args,keywds,&argc) != RP_OK) {
    410         return NULL;
    411     }
     416        // trouble ensues
     417        // error message was set in getArgCount()
     418        return NULL;
     419    }
     420
    412421    if (argc > 2) {
     422        // tested with ElementTests.testArguments_TooManyArgs()
    413423        PyErr_Format(PyExc_TypeError,
    414424            "element() takes at most 2 arguments (%i given)", argc);
     
    416426    }
    417427
    418     if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss", kwlist, &path, &as)) {
     428    if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ss",
     429                kwlist, &path, &as)) {
    419430        /* incorrect input values */
    420431        // tested with ElementTests.testArguments_ArgsWrongType2()
     
    447458        }
    448459    }
     460
    449461    return (PyObject *)retVal;
    450462}
     
    626638PutProc(RpLibraryObject *self, PyObject *args, PyObject *keywds)
    627639{
    628     char *path;
    629     PyObject *compressObjPtr, *appendObjPtr, *valueObjPtr, *strObjPtr;
     640    char *path = (char *)"";
     641
     642    PyObject *valueObjPtr, *compressObjPtr, *appendObjPtr, *strObjPtr;
     643    char *id = NULL;
    630644    int appendFlag, compressFlag;
    631     char *id = NULL;
    632645    char *type = (char *)"string";
    633646    int argc = 0;
     
    642655        NULL
    643656    };
    644 
     657    appendFlag = compressFlag = FALSE;
     658    strObjPtr = appendObjPtr = valueObjPtr = compressObjPtr = NULL;
    645659    if (self->lib == NULL) {
    646660        PyErr_Format(PyExc_RuntimeError,
     
    648662        return NULL;
    649663    }
     664
    650665    if (getArgCount(args,keywds,&argc) != RP_OK) {
    651666        return NULL;
     
    653668    if (argc > 6) {
    654669        PyErr_Format(PyExc_TypeError,
    655             "put() takes at most 6 arguments (%i given)", argc);
     670            "put() takes at most 6 arguments (%i given)",argc);
    656671        return NULL;
    657672    }
    658673    if (argc < 2) {
    659674        PyErr_Format(PyExc_TypeError,
    660             "put() takes at least 2 arguments (%i given)", argc);
     675            "put() takes at least 2 arguments (%i given)",argc);
    661676        return NULL;
    662677    }
    663678    if (!PyArg_ParseTupleAndKeywords(args, keywds, "sO|sOsO", kwlist, &path,
    664                 &valueObjPtr, &id, &appendObjPtr, &type, &compressObjPtr)) {
    665         return NULL;
    666     }
     679        &valueObjPtr, &id, &appendObjPtr, &type, &compressObjPtr)) {
     680        return NULL;
     681    }
     682
    667683    if (valueObjPtr == NULL) {
    668684        PyErr_Format(PyExc_ValueError, "put()'s \'value\' arg is required");
    669685        return NULL;
    670686    }
     687
     688    if (PyObjectToBoolean(appendObjPtr, "no"," append", &appendFlag) != RP_OK){
     689        return NULL;
     690    }
     691    if (PyObjectToBoolean(compressObjPtr, "no", "compress", &compressFlag)
     692        != RP_OK) {
     693        return NULL;
     694    }
     695
    671696    strObjPtr = PyObject_Str(valueObjPtr);
    672 
    673     if (PyObjectToBoolean(appendObjPtr, "no", "append", &appendFlag) != RP_OK) {
    674         return NULL;
    675     }
    676     if (PyObjectToBoolean(compressObjPtr, "no", "compress", &compressFlag)
    677         != RP_OK) {
    678         return NULL;
    679     }
    680697    if (RpLibraryObject_IsValid(valueObjPtr)) {
    681698        self->lib->put( std::string(path),
     
    686703
    687704        if (PyString_AsStringAndSize(strObjPtr, &string, &length) == -1) {
    688             // user passed in an improper string
    689             // exception raised within fxn
    690705            return NULL;
    691706        }
    692         if ((type == NULL) || ((*type=='s') && (strcmp("string", type) == 0))) {
     707        if ((type == NULL) || ((*type=='s') && (strcmp("string", type) == 0))){
    693708            if (compressFlag == 0) {
    694709                self->lib->put( std::string(path),
    695710                                std::string(string), "", appendFlag);
    696711            } else {
    697                 self->lib->putData(std::string(path), string, length,
     712                self->lib->putData( std::string(path), string, length,
    698713                        appendFlag);
    699714            }
    700         } else if ((*type == 'f') && (strcmp("file",type) == 0) ) {
     715        } else if ((*type == 'f') && (strcmp("file",type) == 0)) {
    701716            self->lib->putFile( std::string(path),
    702717                                std::string(PyString_AsString(strObjPtr)),
     
    725740XmlProc(RpLibraryObject *self)
    726741{
    727     PyObject *retStr = NULL;
    728 
    729742    if (self->lib == NULL) {
    730743        PyErr_Format(PyExc_RuntimeError,
    731744            "self uninitialized Rappture Library Object");
    732745    }
    733 
    734     retStr = PyString_FromString(self->lib->xml().c_str());
    735 
    736     return (PyObject *)retStr;
     746    return PyString_FromString(self->lib->xml().c_str());
    737747}
    738748
     
    760770        return NULL;
    761771    }
     772
    762773    if (getArgCount(args,keywds,&argc) != RP_OK) {
    763774        // trouble ensues
     
    771782        return NULL;
    772783    }
     784
    773785    if (!PyArg_ParseTupleAndKeywords(args, keywds, "|i", kwlist, &status)) {
    774786        // tested with ResultTests.testArguments_InvalidStatusArg()
     
    776788        return NULL;
    777789    }
    778     self->lib->put("tool.version.rappture.language", "python");
     790
     791    self->lib->put("tool.version.rappture.language","python");
    779792    self->lib->result(status);
    780793
     
    786799    {"copy", (PyCFunction)CopyProc, METH_VARARGS|METH_KEYWORDS, CopyProcDoc},
    787800
    788 /*
     801#ifdef notdef
    789802    {"children", (PyCFunction)ChildrenProc, METH_VARARGS|METH_KEYWORDS,
    790803        ChildrenProcDoc},
    791 */
     804#endif
    792805
    793806    {"element", (PyCFunction)ElementProc, METH_VARARGS|METH_KEYWORDS,
    794807        ElementProcDoc},
    795808
    796     {"get", (PyCFunction)GetProc, METH_VARARGS|METH_KEYWORDS, GetProcDoc},
    797 
    798 /*
     809    {"get", (PyCFunction)GetProc, METH_VARARGS|METH_KEYWORDS,
     810        GetProcDoc},
     811
     812#ifdef notdef
    799813    {"parent", (PyCFunction)ParentProc, METH_VARARGS|METH_KEYWORDS,
    800          ParentProcDoc},
    801 */
     814                ParentProcDoc},
     815#endif
    802816
    803817    {"put", (PyCFunction)PutProc, METH_VARARGS|METH_KEYWORDS, PutProcDoc},
    804818
    805 /*
     819#ifdef notdef
    806820    {"remove", (PyCFunction)RemoveProc, METH_VARARGS|METH_KEYWORDS,
    807821        RemoveProcDoc},
    808 */
     822#endif
    809823
    810824    {"xml", (PyCFunction)XmlProc, METH_NOARGS, XmlProcDoc},
    811825
    812     {"result", (PyCFunction)ResultProc, METH_VARARGS|METH_KEYWORDS, 
     826    {"result", (PyCFunction)ResultProc, METH_VARARGS|METH_KEYWORDS,
    813827        ResultProcDoc},
    814828
    815     {NULL,        NULL}                 /* sentinel */
     829    {NULL,        NULL}        /* sentinel */
    816830};
    817831
     
    825839    0,                                    /*tp_itemsize*/
    826840    /* methods */
    827     (destructor)FreeProc,                 /*tp_dealloc*/
     841    (destructor)FreeProc,               /*tp_dealloc*/
    828842    0,                                    /*tp_print*/
    829843    0,                                    /*tp_getattr*/
     
    856870    0,                                    /*tp_descr_set*/
    857871    0,                                    /*tp_dictoffset*/
    858     (initproc)InitProc,                   /*tp_init*/
     872    (initproc)InitProc,                 /*tp_init*/
    859873    0,                                    /*tp_alloc*/
    860     NewProc,                              /*tp_new*/
     874    NewProc,                            /*tp_new*/
    861875    0,                                    /*tp_new*/
    862876};
     
    890904        if (self != NULL) {
    891905            self->lib = lib;
    892         }
    893         else {
     906        } else {
    894907            // raise error
    895908            PyErr_SetString(PyExc_RuntimeError,
    896                             "trouble creating new RpLibraryObject");
    897         }
    898     }
    899 
     909                "trouble creating new RpLibraryObject");
     910        }
     911    }
    900912    return (PyObject *)self;
    901913}
Note: See TracChangeset for help on using the changeset viewer.