Пример #1
0
DECLARE_EXPORT PyObject* Calendar::addPythonBucket(PyObject* self, PyObject* args, PyObject* kwdict) // TODO Remove this method. Calendarbuckets now have a good Python API
{
  try
  {
    // Pick up the calendar
    Calendar* cal = static_cast<Calendar*>(self);
    if (!cal) throw LogicException("Can't set value of a NULL calendar");

    // Parse the arguments
    int id = INT_MIN;
    if (!PyArg_ParseTuple(args, "|i:addBucket", &id))
      return NULL;

    // See if the bucket exists, or create it
    CalendarBucket * b = NULL;
    if (id != INT_MIN)
      b = cal->findBucket(id);
    if (!b)
    {
      b = new CalendarBucket();
      b->setCalendar(cal);
      if (id != INT_MIN)
        b->setId(id);
    }

    // Return a reference
    Py_INCREF(b);
    return b;
  }
  catch(...)
  {
    PythonType::evalException();
    return NULL;
  }
  return Py_BuildValue("");
}
Пример #2
0
DECLARE_EXPORT Object* CalendarBucket::createBucket(
  const MetaClass* cat, const DataValueDict& atts
  )
{
  // Pick up the calendar and id fields
  const DataValue* cal_val = atts.get(Tags::calendar);
  Calendar *cal = cal_val ? static_cast<Calendar*>(cal_val->getObject()) : NULL;
  const DataValue* id_val = atts.get(Tags::id);
  int id = id_val ? id_val->getInt() : INT_MIN;

  // Check for existence of a bucket with the same identifier
  CalendarBucket* result = NULL;
  if (cal)
    result = cal->findBucket(id);

  // Pick up the action attribute and update the bucket accordingly
  switch (MetaClass::decodeAction(atts))
  {
    case ADD:
      // Only additions are allowed
      if (result)
      {
        ostringstream o;
        o << "Bucket " << id << " already exists in calendar '" << cal << "'";
        throw DataException(o.str());
      }
      result = new CalendarBucket();
      result->setId(id);
      if (cal) result->setCalendar(cal);
      return result;
    case CHANGE:
      // Only changes are allowed
      if (!result)
      {
        ostringstream o;
        o << "Bucket " << id << " doesn't exist in calendar '"
          << (cal ? cal->getName() : "NULL") << "'";
        throw DataException(o.str());
      }
      return result;
    case REMOVE:
      // Delete the entity
      if (!result)
      {
        ostringstream o;
        o << "Bucket " << id << " doesn't exist in calendar '"
          << (cal ? cal->getName() : "NULL") << "'";
        throw DataException(o.str());
      }
      else
      {
        // Delete it
        cal->removeBucket(result);
        return NULL;
      }
    case ADD_CHANGE:
      if (!result)
      {
        // Adding a new bucket
        result = new CalendarBucket();
        result->setId(id);
        if (cal) result->setCalendar(cal);
      }
      return result;
  }

  // This part of the code isn't expected not be reached
  throw LogicException("Unreachable code reached");
}