示例#1
0
文件: xso.py 项目: migarbo1/SpyAgent
class EventItem(xso.XSO):
    TAG = (namespaces.xep0060_event, "item")

    id_ = xso.Attr(
        "id",
        default=None,
    )

    node = xso.Attr(
        "node",
        default=None,
    )

    publisher = xso.Attr(
        "publisher",
        default=None,
    )

    registered_payload = xso.Child([], strict=True)

    unregistered_payload = xso.Collector()

    def __init__(self, payload, *, id_=None):
        super().__init__()
        self.registered_payload = payload
        self.id_ = id_
示例#2
0
文件: xso.py 项目: migarbo1/SpyAgent
class Query(xso.XSO):
    """
    The XSO for queries to private XML storage.

    .. automethod:: as_payload_class
    """
    TAG = (namespaces.xep0049, "query")

    registered_payload = xso.Child([], strict=True)
    unregistered_payload = xso.Collector()

    def __init__(self, payload):
        self.registered_payload = payload

    @classmethod
    def as_payload_class(mycls, xso_class):
        """
        Register the given class `xso_class` as possible payload
        for private XML storage.

        Return `xso_class`, to allow this to be used as a decorator.
        """
        mycls.register_child(Query.registered_payload, xso_class)

        return xso_class
示例#3
0
文件: xso.py 项目: migarbo1/SpyAgent
class Item(xso.XSO):
    TAG = (namespaces.xep0060, "item")

    id_ = xso.Attr("id", default=None)

    registered_payload = xso.Child([], strict=True)

    unregistered_payload = xso.Collector()

    def __init__(self, id_=None):
        super().__init__()
        self.id_ = id_
示例#4
0
文件: xso.py 项目: migarbo1/SpyAgent
class VCard(xso.XSO):
    """
    The container for vCard data as per :xep:`vcard-temp <54>`.

    .. attribute:: elements

       The raw elements of the vCard (as etree).

    The following methods are defined to access and modify certain
    entries of the vCard in a highlevel manner:

    .. automethod:: get_photo_data

    .. automethod:: set_photo_data

    .. automethod:: clear_photo_data
    """

    TAG = (namespaces.xep0054, "vCard")

    elements = xso.Collector()

    def get_photo_mime_type(self):
        """
        Get the mime type of the photo stored in the vCard.

        :returns: the MIME type of the photo as :class:`str` or :data:`None`.
        """
        mime_type = self.elements.xpath("/ns0:vCard/ns0:PHOTO/ns0:TYPE/text()",
                                        namespaces={"ns0": namespaces.xep0054})
        if mime_type:
            return mime_type[0]
        return None

    def get_photo_data(self):
        """
        Get the photo stored in the vCard.

        :returns: the photo as :class:`bytes` or :data:`None`.
        """
        photo = self.elements.xpath("/ns0:vCard/ns0:PHOTO/ns0:BINVAL/text()",
                                    namespaces={"ns0": namespaces.xep0054})
        if photo:
            return base64.b64decode(photo[0])
        return None

    def set_photo_data(self, mime_type, data):
        """
        Set the photo stored in the vCard.

        :param mime_type: the MIME type of the image data
        :param data: the image data as :class:`bytes`
        """
        res = self.elements.xpath("/ns0:vCard/ns0:PHOTO",
                                  namespaces={"ns0": namespaces.xep0054})
        if res:
            photo = res[0]
            photo.clear()
        else:
            photo = etree.SubElement(self.elements,
                                     etree.QName(namespaces.xep0054, "PHOTO"))

        binval = etree.SubElement(photo,
                                  etree.QName(namespaces.xep0054, "BINVAL"))
        binval.text = base64.b64encode(data)
        type_ = etree.SubElement(photo, etree.QName(namespaces.xep0054,
                                                    "TYPE"))
        type_.text = mime_type

    def clear_photo_data(self):
        """
        Remove the photo stored in the vCard.
        """
        res = self.elements.xpath("/ns0:vCard/ns0:PHOTO",
                                  namespaces={"ns0": namespaces.xep0054})
        for to_remove in res:
            self.elements.remove(to_remove)
示例#5
0
class Pointer(xso.XSO):
    """
    A pointer metadata node. The contents are implementation defined.

    The following attributes may be present (they default to
    :data:`None`):

    .. attribute:: id_

       The SHA1 of the avatar image data.

    .. attribute:: mime_type

       The MIME type of the avatar image.

    .. attribute:: nbytes

       The size of the image data in bytes.

    .. attribute:: width

       The width of the image in pixels.

    .. attribute:: height

       The height of the image in pixels.
    """
    TAG = (namespaces.xep0084_metadata, "pointer")

    # according to the XEP those MAY occur if their values are known
    id_ = xso.Attr(tag="id", type_=xso.String(), default=None)
    mime_type = xso.Attr(tag="type", type_=xso.String(), default=None)
    nbytes = xso.Attr(tag="bytes", type_=xso.Integer(), default=None)
    width = xso.Attr(tag="width", type_=xso.Integer(), default=None)
    height = xso.Attr(tag="height", type_=xso.Integer(), default=None)

    registered_payload = xso.Child([])
    unregistered_payload = xso.Collector()

    @classmethod
    def as_payload_class(mycls, cls):
        """
        Register the given class `cls` as possible payload for a
        :class:`Pointer`.

        Return the class, to allow this to be used as decorator.
        """

        mycls.register_child(
            Pointer.registered_payload,
            cls
        )

        return cls

    def __init__(self, payload, id_, mime_type, nbytes, width=None,
                 height=None, url=None):
        self.registered_payload = payload

        self.id_ = id_
        self.mime_type = mime_type
        self.nbytes = nbytes
        self.width = width
        self.height = height