Example #1
0
    def get_class_xsd(self, io, cls):
        """ Returns the XSD for a mapped class. """
        attrs = {}
        attrs['xmlns:gml'] = 'http://www.opengis.net/gml'
        attrs['xmlns:xsd'] = 'http://www.w3.org/2001/XMLSchema'
        tb = TreeBuilder()
        with tag(tb, 'xsd:schema', attrs) as tb:
            with tag(tb, 'xsd:complexType', {'name': cls.__name__}) as tb:
                with tag(tb, 'xsd:complexContent') as tb:
                    with tag(tb, 'xsd:extension',
                             {'base': 'gml:AbstractFeatureType'}) as tb:
                        with tag(tb, 'xsd:sequence') as tb:
                            self.add_class_properties_xsd(tb, cls)

        ElementTree(tb.close()).write(io, encoding='utf-8')
        return io
Example #2
0
    def get_class_xsd(self, io, cls):
        """ Returns the XSD for a mapped class. """
        attrs = {}
        attrs['xmlns:gml'] = 'http://www.opengis.net/gml'
        attrs['xmlns:xsd'] = 'http://www.w3.org/2001/XMLSchema'
        tb = TreeBuilder()
        with tag(tb, 'xsd:schema', attrs) as tb:
            with tag(tb, 'xsd:complexType', {'name': cls.__name__}) as tb:
                with tag(tb, 'xsd:complexContent') as tb:
                    with tag(tb, 'xsd:extension',
                             {'base': 'gml:AbstractFeatureType'}) as tb:
                        with tag(tb, 'xsd:sequence') as tb:
                            self.add_class_properties_xsd(tb, cls)

        ElementTree(tb.close()).write(io, encoding='utf-8')
        return io
Example #3
0
class XMLGenerator(object):

    def __init__(self, out, encoding='utf-8'):

        self.out = out
        self.encoding = encoding
        self.builder = TreeBuilder()

    def getOutputStream(self):
        return self.out

    def getEncoding(self):
        return self.encoding

    def write(self, data):
        self.builder.data(data)

    def startDocument(self):
        self.out.write('<?xml version="1.0" encoding="%s"?>' %(self.encoding))

    def endDocument(self):

        root = self.builder.close()
        ElementTree(root).write(self.out, self.encoding)
        self.out.flush()

    def startElement(self, tag, attrs):

        self.builder.start(tag, attrs)

    def endElement(self, tag):

        self.builder.end(tag)
    
    def characters(self, data):

        if data:
            self.builder.data(data)
Example #4
0
    def do_rss(self, repo_data, latest):
        """
        Write the RSS feed.
        
        @param repo_data: the dict containing repository data
        @type  repo_data: dict
        @param latest:    the list of tuples returned by get_latest_packages
        @type  latest:    list
        
        @rtype: void
        """
        self.say('Generating rss feed...')
        etb = TreeBuilder()
        out = os.path.join(self.outdir, RSSFILE)
        etb.start('rss', {'version': '2.0'})
        etb.start('channel')
        etb.start('title')
        etb.data(repo_data['title'])
        etb.end('title')
        etb.start('link')
        etb.data('%s/repoview/%s' % (self.opts.url, RSSFILE))
        etb.end('link')
        etb.start('description')
        etb.data('Latest packages for %s' % repo_data['title'])
        etb.end('description')
        etb.start('lastBuildDate')
        etb.data(time.strftime(ISOFORMAT))
        etb.end('lastBuildDate')
        etb.start('generator')
        etb.data('Repoview-%s' % repo_data['my_version'])
        etb.end('generator')

        rss_tpt = os.path.join(self.opts.templatedir, RSSKID)
        rss_kid = Template(file=rss_tpt)
        rss_kid.assume_encoding = "utf-8"
        rss_kid.repo_data = repo_data
        rss_kid.url = self.opts.url

        for row in latest:
            pkg_data = self.get_package_data(row[0])

            rpm = pkg_data['rpms'][0]
            (epoch, version, release, arch, built) = rpm[:5]
            etb.start('item')
            etb.start('guid')
            etb.data('%s/repoview/%s+%s:%s-%s.%s' %
                     (self.opts.url, pkg_data['filename'], epoch, version,
                      release, arch))
            etb.end('guid')
            etb.start('link')
            etb.data('%s/repoview/%s' % (self.opts.url, pkg_data['filename']))
            etb.end('link')
            etb.start('pubDate')
            etb.data(time.strftime(ISOFORMAT, time.gmtime(int(built))))
            etb.end('pubDate')
            etb.start('title')
            etb.data('Update: %s-%s-%s' % (pkg_data['name'], version, release))
            etb.end('title')
            rss_kid.pkg_data = pkg_data
            description = rss_kid.serialize()
            etb.start('description')
            etb.data(description.decode('utf-8'))
            etb.end('description')
            etb.end('item')

        etb.end('channel')
        etb.end('rss')
        rss = etb.close()

        etree = ElementTree(rss)
        out = os.path.join(self.outdir, RSSFILE)
        etree.write(out, 'utf-8')
        self.say('done\n')
    def _get_sync(self, view, iq, args):

        try:
            if 'toRepoId' in args and UUID(args['toRepoId']) != self._repoId:
                raise RepositoryMismatchError, args['toRepoId']

            view.refresh(None, None, False)

            repoId = UUID(args['fromRepoId'])
            name = args['name']
            version = int(args.get('version', '0'))
            uuid = args.get('uuid')
            if uuid is not None:
                uuid = UUID(uuid)

            collection, name, uuid = self.findCollection(view, name, uuid)
            share = self.findShare(view, collection, repoId, iq['from'])

            iq = client.IQ(self.client.xmlstream, "result")
            query = iq.addElement(("jabber:x:chandler", "query"))
            sync = query.addElement('sync')
            sync['name'] = name

            changes = self.computeChanges(view, version, collection, share)
            keys = set()
            compressed = len(changes) > 16

            if compressed:
                builder = TreeBuilder()
                dom = formats.ElementTreeDOM()
                data = dom.openElement(builder, 'data')
            else:
                dom = DomishDOM()
                data = dom.openElement(sync, 'data')

            for key, (_changes, status) in changes.iteritems():
                if key not in keys:
                    if status & CItem.DELETED:
                        dom.openElement(data,
                                        'item',
                                        uuid=key.str64(),
                                        status='deleted')
                    else:
                        attrs = {'uuid': key.str64()}
                        if key in collection:
                            attrs['status'] = 'member'
                        item = dom.openElement(data, 'item', **attrs)
                        share.format.exportProcess(dom, key, item, changes,
                                                   keys)
                    dom.closeElement(data, 'item')
                elif key in collection:
                    dom.openElement(data,
                                    'item',
                                    uuid=key.str64(),
                                    status='member')
                    dom.closeElement(data, 'item')

            dom.closeElement(data, 'data')

            sync['fromRepoId'] = self._repoId.str64()
            sync['toRepoId'] = repoId.str64()
            sync['version'] = str(view.itsVersion)
            sync['uuid'] = collection.itsUUID.str64()

            if compressed:
                sync['compressed'] = 'true'
                out = StringIO()
                ElementTree(builder.close()).write(out, 'utf-8')
                sync.children.append(b64encode(compress(out.getvalue())))
                out.close()

        except:
            view.cancel()
            raise

        share.localVersion = view.itsVersion + 1
        share.established = True
        share.ackPending = True
        view.commit()

        return iq
Example #6
0
    def _get_sync(self, view, shareId, peerId, name, op):

        try:
            view.refresh(None, None, False)

            if shareId is not None:
                share = view[shareId]
                peerId = share.conduit.peerId
                toRepoId = share.repoId
                collection = share.contents
                name = collection.displayName
                uuid = collection.itsUUID
                version = share.localVersion
            else:
                collection, name, uuid = self.findCollection(view, name, None)
                toRepoId = None
                version = 0

            replyTo = view[self.client.account].imap.replyToAddress.emailAddress
            share = self.findShare(view, collection, toRepoId, peerId)

            changes = self.computeChanges(view, version, collection, share)
            if op == 'sync' and not changes:
                share.localVersion = view.itsVersion + 1
                view.commit()
                return None

            message = MIMEMultipart()
            message['From'] = replyTo
            message['Reply-To'] = replyTo
            message['To'] = peerId
            message['Subject'] = 'Chandler sent "%s" collection' %(name)
            message['X-chandler'] = 'p2p'
            textPart = MIMEBase('text', 'plain')
            textPart.set_payload('Chandler sent "%s"' %(name))
            message.attach(textPart)
            attachment = MIMEBase('application', 'octet-stream')

            builder = TreeBuilder()
            dom = ElementTreeDOM()
            data = dom.openElement(builder, 'data')

            keys = set()
            for key, (_changes, status) in changes.iteritems():
                if key not in keys:
                    attrs = { 'uuid': key.str64() }
                    if status & CItem.DELETED:
                        attrs['status'] = 'deleted'
                        item = dom.openElement(data, 'item', **attrs)
                    else:
                        if key in collection:
                            attrs['status'] = 'member'
                        item = dom.openElement(data, 'item', **attrs)
                        share.format.exportProcess(dom, key, item,
                                                   changes, keys)
                    dom.closeElement(data, 'item')
                elif key in collection:
                    item = dom.openElement(data, 'item', uuid=key.str64(),
                                           status='member')
                    dom.closeElement(data, 'item')

            dom.closeElement(builder, 'data')
            out = StringIO()
            ElementTree(builder.close()).write(out, 'utf-8')
            data = compress(out.getvalue())
            out.close()

            message['X-chandler-p2p-name'] = name
            message['X-chandler-p2p-from'] = self._repoId.str64()
            if toRepoId is not None:
                message['X-chandler-p2p-to'] = toRepoId.str64()
            message['X-chandler-p2p-item'] = "%s-%d" %(uuid.str64(),
                                                       view.itsVersion)
            message['X-chandler-p2p-op'] = 'sync'

            attachment.set_payload(data)
            encode_base64(attachment)
            attachment.add_header('Content-Disposition', 'attachment',
                                  name=name)
            message.attach(attachment)
        except:
            view.cancel()
            raise

        share.localVersion = view.itsVersion + 1
        share.established = True
        share.ackPending = True
        view.commit()

        return message
Example #7
0
 def do_rss(self, repo_data, latest):
     """
     Write the RSS feed.
     
     @param repo_data: the dict containing repository data
     @type  repo_data: dict
     @param latest:    the list of tuples returned by get_latest_packages
     @type  latest:    list
     
     @rtype: void
     """
     self.say('Generating rss feed...')
     etb = TreeBuilder()
     out = os.path.join(self.outdir, RSSFILE)
     etb.start('rss', {'version': '2.0'})
     etb.start('channel')
     etb.start('title')
     etb.data(repo_data['title'])
     etb.end('title')
     etb.start('link')
     etb.data('%s/repoview/%s' % (self.opts.url, RSSFILE))
     etb.end('link')
     etb.start('description')
     etb.data('Latest packages for %s' % repo_data['title'])
     etb.end('description')
     etb.start('lastBuildDate')
     etb.data(time.strftime(ISOFORMAT))
     etb.end('lastBuildDate')
     etb.start('generator')
     etb.data('Repoview-%s' % repo_data['my_version'])
     etb.end('generator')
     
     rss_tpt = os.path.join(self.opts.templatedir, RSSKID)
     rss_kid = Template(file=rss_tpt)
     rss_kid.assume_encoding = "utf-8"
     rss_kid.repo_data = repo_data
     rss_kid.url = self.opts.url
     
     for row in latest:
         pkg_data = self.get_package_data(row[0])
         
         rpm = pkg_data['rpms'][0]
         (epoch, version, release, arch, built) = rpm[:5]
         etb.start('item')
         etb.start('guid')
         etb.data('%s/repoview/%s+%s:%s-%s.%s' % (self.opts.url, 
                                                  pkg_data['filename'], 
                                                  epoch, version, release, 
                                                  arch))
         etb.end('guid')
         etb.start('link')
         etb.data('%s/repoview/%s' % (self.opts.url, pkg_data['filename']))
         etb.end('link')
         etb.start('pubDate')
         etb.data(time.strftime(ISOFORMAT, time.gmtime(int(built))))
         etb.end('pubDate')
         etb.start('title')
         etb.data('Update: %s-%s-%s' % (pkg_data['name'], version, release))
         etb.end('title')
         rss_kid.pkg_data = pkg_data
         description = rss_kid.serialize()
         etb.start('description')
         etb.data(description.decode('utf-8'))
         etb.end('description')
         etb.end('item')
     
     etb.end('channel')
     etb.end('rss')
     rss = etb.close()
     
     etree = ElementTree(rss)
     out = os.path.join(self.outdir, RSSFILE)
     etree.write(out, 'utf-8')
     self.say('done\n')
Example #8
0
    def _get_sync(self, view, shareId, peerId, name, op):

        try:
            view.refresh(None, None, False)

            if shareId is not None:
                share = view[shareId]
                peerId = share.conduit.peerId
                toRepoId = share.repoId
                collection = share.contents
                name = collection.displayName
                uuid = collection.itsUUID
                version = share.localVersion
            else:
                collection, name, uuid = self.findCollection(view, name, None)
                toRepoId = None
                version = 0

            replyTo = view[
                self.client.account].imap.replyToAddress.emailAddress
            share = self.findShare(view, collection, toRepoId, peerId)

            changes = self.computeChanges(view, version, collection, share)
            if op == 'sync' and not changes:
                share.localVersion = view.itsVersion + 1
                view.commit()
                return None

            message = MIMEMultipart()
            message['From'] = replyTo
            message['Reply-To'] = replyTo
            message['To'] = peerId
            message['Subject'] = 'Chandler sent "%s" collection' % (name)
            message['X-chandler'] = 'p2p'
            textPart = MIMEBase('text', 'plain')
            textPart.set_payload('Chandler sent "%s"' % (name))
            message.attach(textPart)
            attachment = MIMEBase('application', 'octet-stream')

            builder = TreeBuilder()
            dom = ElementTreeDOM()
            data = dom.openElement(builder, 'data')

            keys = set()
            for key, (_changes, status) in changes.iteritems():
                if key not in keys:
                    attrs = {'uuid': key.str64()}
                    if status & CItem.DELETED:
                        attrs['status'] = 'deleted'
                        item = dom.openElement(data, 'item', **attrs)
                    else:
                        if key in collection:
                            attrs['status'] = 'member'
                        item = dom.openElement(data, 'item', **attrs)
                        share.format.exportProcess(dom, key, item, changes,
                                                   keys)
                    dom.closeElement(data, 'item')
                elif key in collection:
                    item = dom.openElement(data,
                                           'item',
                                           uuid=key.str64(),
                                           status='member')
                    dom.closeElement(data, 'item')

            dom.closeElement(builder, 'data')
            out = StringIO()
            ElementTree(builder.close()).write(out, 'utf-8')
            data = compress(out.getvalue())
            out.close()

            message['X-chandler-p2p-name'] = name
            message['X-chandler-p2p-from'] = self._repoId.str64()
            if toRepoId is not None:
                message['X-chandler-p2p-to'] = toRepoId.str64()
            message['X-chandler-p2p-item'] = "%s-%d" % (uuid.str64(),
                                                        view.itsVersion)
            message['X-chandler-p2p-op'] = 'sync'

            attachment.set_payload(data)
            encode_base64(attachment)
            attachment.add_header('Content-Disposition',
                                  'attachment',
                                  name=name)
            message.attach(attachment)
        except:
            view.cancel()
            raise

        share.localVersion = view.itsVersion + 1
        share.established = True
        share.ackPending = True
        view.commit()

        return message
Example #9
0
    def _get_sync(self, view, iq, args):

        try:
            if 'toRepoId' in args and UUID(args['toRepoId']) != self._repoId:
                raise RepositoryMismatchError, args['toRepoId']

            view.refresh(None, None, False)

            repoId = UUID(args['fromRepoId'])
            name = args['name']
            version = int(args.get('version', '0'))
            uuid = args.get('uuid')
            if uuid is not None:
                uuid = UUID(uuid)

            collection, name, uuid = self.findCollection(view, name, uuid)
            share = self.findShare(view, collection, repoId, iq['from'])

            iq = client.IQ(self.client.xmlstream, "result")
            query = iq.addElement(("jabber:x:chandler", "query"))
            sync = query.addElement('sync')
            sync['name'] = name

            changes = self.computeChanges(view, version, collection, share)
            keys = set()
            compressed = len(changes) > 16

            if compressed:
                builder = TreeBuilder()
                dom = formats.ElementTreeDOM()
                data = dom.openElement(builder, 'data')
            else:
                dom = DomishDOM()
                data = dom.openElement(sync, 'data')

            for key, (_changes, status) in changes.iteritems():
                if key not in keys:
                    if status & CItem.DELETED:
                        dom.openElement(data, 'item', uuid=key.str64(),
                                        status='deleted')
                    else:
                        attrs = { 'uuid': key.str64() }
                        if key in collection:
                            attrs['status'] = 'member'
                        item = dom.openElement(data, 'item', **attrs)
                        share.format.exportProcess(dom, key, item,
                                                   changes, keys)
                    dom.closeElement(data, 'item')
                elif key in collection:
                    dom.openElement(data, 'item', uuid=key.str64(),
                                    status='member')
                    dom.closeElement(data, 'item')

            dom.closeElement(data, 'data')

            sync['fromRepoId'] = self._repoId.str64()
            sync['toRepoId'] = repoId.str64()
            sync['version'] = str(view.itsVersion)
            sync['uuid'] = collection.itsUUID.str64()

            if compressed:
                sync['compressed'] = 'true'
                out = StringIO()
                ElementTree(builder.close()).write(out, 'utf-8')
                sync.children.append(b64encode(compress(out.getvalue())))
                out.close()

        except:
            view.cancel()
            raise

        share.localVersion = view.itsVersion + 1
        share.established = True
        share.ackPending = True
        view.commit()

        return iq