How to use the piecash.kvp.SlotFrame function in piecash

To help you get started, we’ve selected a few piecash examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sdementen / piecash / piecash / kvp.py View on Github external
def __setitem__(self, key, value):
        keys = key.split("/", 1)
        key = keys[0]
        for sl in self.slots:
            if sl.name == key:
                break
        else:
            # new key
            if len(keys) > 1:
                if isinstance(self, SlotFrame):
                    sf = SlotFrame(name=self._name + "/" + key,
                                   obj_guid=self.guid_val)
                else:
                    sf = SlotFrame(name=key,
                                   obj_guid=self.guid)
                sf[keys[1]] = value
                self.slots.append(sf)
            else:
                self.slots.append(slot(parent=self, name=key, value=value))

            return

        if len(keys) > 1:
            sl[keys[1]] = value
            return
        # assign if type is correct
github sdementen / piecash / piecash / kvp.py View on Github external
def __setitem__(self, key, value):
        keys = key.split("/", 1)
        key = keys[0]
        for sl in self.slots:
            if sl.name == key:
                break
        else:
            # new key
            if len(keys) > 1:
                if isinstance(self, SlotFrame):
                    sf = SlotFrame(name=self._name + "/" + key,
                                   obj_guid=self.guid_val)
                else:
                    sf = SlotFrame(name=key,
                                   obj_guid=self.guid)
                sf[keys[1]] = value
                self.slots.append(sf)
            else:
                self.slots.append(slot(parent=self, name=key, value=value))

            return

        if len(keys) > 1:
            sl[keys[1]] = value
            return
        # assign if type is correct
        if isinstance(value, sl._python_type):
github sdementen / piecash / piecash / kvp.py View on Github external
@event.listens_for(SlotFrame.slots, 'remove')
def remove_slot(target, value, initiator):
    s = object_session(value)
    if value in s.new:
        s.expunge(value)
    else:
        s.delete(value)
github sdementen / piecash / piecash / kvp.py View on Github external
    @property
    def value(self):
        # convert to dict
        return {sl.name: sl.value for sl in self.slots}

    @value.setter
    def value(self, value):
        self.slots = [slot(parent=self, name=k, value=v) for k, v in value.items()]

    def __init__(self, **kwargs):
        self.guid_val = uuid.uuid4().hex
        super(SlotFrame, self).__init__(**kwargs)


class SlotList(SlotFrame):
    __mapper_args__ = {
        'polymorphic_identity': KVP_Type.KVP_TYPE_GLIST
    }
    _python_type = (list,)

    @property
    def value(self):
        # convert to dict
        return [sl.value for sl in self.slots]

    @value.setter
    def value(self, value):
        self.slots = [slot(parent=self, name=str(i), value=v) for i, v in enumerate(value)]

    def __init__(self, **kwargs):
        self.guid_val = uuid.uuid4().hex
github sdementen / piecash / piecash / kvp.py View on Github external
name = parent._name + "/" + name
        guid_parent = parent.guid_val
    else:
        guid_parent = parent.guid

    # handle datetime before others (as otherwise can be mixed with date)
    if isinstance(value, datetime.datetime):
        return SlotTime(name=name, value=value, obj_guid=guid_parent)

    for cls in get_all_subclasses(Slot):
        if isinstance(value, cls._python_type) and cls != SlotFrame and cls != SlotList:
            return cls(name=name, value=value, obj_guid=guid_parent)

    if isinstance(value, dict):
        # transform a dict to Frame/Slots
        sf = SlotFrame(name=name, obj_guid=guid_parent)
        for k, v in value.items():
            sl = slot(parent=sf, name=k, value=v)
            sl.parent = sf
        return sf

    if isinstance(value, list):
        # transform a list to List/Slots
        sf = SlotList(name=name)
        for i, v in enumerate(value):
            sl = slot(parent=sf, name=str(i), value=v)
            sl.parent = sf
        return sf

    raise ValueError("Cannot handle type of '{}'".format(value))
github sdementen / piecash / piecash / kvp.py View on Github external
def slot(parent, name, value):
    if isinstance(parent, SlotFrame):
        name = parent._name + "/" + name
        guid_parent = parent.guid_val
    else:
        guid_parent = parent.guid

    # handle datetime before others (as otherwise can be mixed with date)
    if isinstance(value, datetime.datetime):
        return SlotTime(name=name, value=value, obj_guid=guid_parent)

    for cls in get_all_subclasses(Slot):
        if isinstance(value, cls._python_type) and cls != SlotFrame and cls != SlotList:
            return cls(name=name, value=value, obj_guid=guid_parent)

    if isinstance(value, dict):
        # transform a dict to Frame/Slots
        sf = SlotFrame(name=name, obj_guid=guid_parent)
github sdementen / piecash / piecash / kvp.py View on Github external
def __init__(self, **kwargs):
        self.guid_val = uuid.uuid4().hex
        super(SlotFrame, self).__init__(**kwargs)


@event.listens_for(SlotFrame.slots, 'remove')
def remove_slot(target, value, initiator):
    s = object_session(value)
    if value in s.new:
        s.expunge(value)
    else:
        s.delete(value)


class SlotGUID(SlotFrame):
    __mapper_args__ = {
        'polymorphic_identity': KVP_Type.KVP_TYPE_GUID
    }
    _python_type = (DeclarativeBase,)

    # add
    _mapping_name_class = {
        'from-sched-xaction': 'piecash.core.transaction.ScheduledTransaction',
        'account': 'piecash.core.account.Account',
        'invoice-guid': 'piecash.business.invoice.Invoice',
        'peer_guid': 'piecash.core.transaction.Split',
        'gains-split': 'piecash.core.transaction.Split',
        'gains-source': 'piecash.core.transaction.Split',
        'default-currency': 'piecash.core.commodity.Commodity',
    }
github sdementen / piecash / piecash / kvp.py View on Github external
def __init__(self, **kwargs):
        self.guid_val = uuid.uuid4().hex
        super(SlotFrame, self).__init__(**kwargs)