Exemplo n.º 1
0
    def test_namespaces_repr(self):
        doc = Namespace("http://example.org/bar")
        bbc = Namespace("http://example.org/bbc")
        dc = Namespace("http://purl.org/dc/elements/1.1/")
        d = parse("""<doc version="2.7182818284590451"
        xmlns="http://example.org/bar"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:bbc="http://example.org/bbc">
            <author>John Polk and John Palfrey</author>
            <dc:creator>John Polk</dc:creator>
            <dc:creator>John Palfrey</dc:creator>
            <bbc:show bbc:station="4">Buffy</bbc:show>
        </doc>""")
        assert repr(d) == '<doc version="2.7182818284590451">...</doc>'
        # I supect py3 does not see equality in type below.
        #assert d.__repr__(1) == '<doc xmlns:bbc="http://example.org/bbc" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://example.org/bar" version="2.7182818284590451"><author>John Polk and John Palfrey</author><dc:creator>John Polk</dc:creator><dc:creator>John Palfrey</dc:creator><bbc:show bbc:station="4">Buffy</bbc:show></doc>'
        #assert d.__repr__(1, 1) == '<doc xmlns:bbc="http://example.org/bbc" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://example.org/bar" version="2.7182818284590451">\n\t<author>John Polk and John Palfrey</author>\n\t<dc:creator>John Polk</dc:creator>\n\t<dc:creator>John Palfrey</dc:creator>\n\t<bbc:show bbc:station="4">Buffy</bbc:show>\n</doc>'
        assert repr(
            parse("<doc xml:lang='en' />")) == '<doc xml:lang="en"></doc>'
        self.assertEqual(d.author, d['author'])
        assert d.author == "John Polk and John Palfrey"
        assert d.author._name == doc.author
        assert str(d[dc.creator]) == "John Polk"
        assert d[dc.creator]._name == dc.creator
        assert str(d[dc.creator:][1]) == "John Palfrey"
        d[dc.creator] = "Me!!!"
        assert str(d[dc.creator]) == "Me!!!"
        assert len(d[dc.creator:]) == 1
        d[dc.creator:] = "You!!!"
        assert len(d[dc.creator:]) == 2
        assert d[bbc.show](bbc.station) == "4"
        d[bbc.show](bbc.station, "5")
        assert d[bbc.show](bbc.station) == "5"

        e = Element('e')
        e.c = '<img src="foo">'
        assert e.__repr__(1) == '<e><c>&lt;img src="foo"></c></e>'
        e.c = '2 > 4'
        assert e.__repr__(1) == '<e><c>2 > 4</c></e>'
        e.c = 'CDATA sections are <em>closed</em> with ]]>.'
        assert e.__repr__(
            1
        ) == '<e><c>CDATA sections are &lt;em>closed&lt;/em> with ]]&gt;.</c></e>'
        e.c = parse(
            '<div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you</div>'
        )
        assert e.__repr__(
            1
        ) == '<e><c><div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you</div></c></e>'

        e = Element('e')
        e('c', 'that "sucks"')
        assert e.__repr__(1) == '<e c="that &quot;sucks&quot;"></e>'
        assert quote("]]>") == "]]&gt;"
        assert quote('< dkdkdsd dkd sksdksdfsd fsdfdsf]]> kfdfkg >'
                     ) == '&lt; dkdkdsd dkd sksdksdfsd fsdfdsf]]&gt; kfdfkg >'
        assert parse('<x a="&lt;"></x>').__repr__(1) == '<x a="&lt;"></x>'
        assert parse('<a xmlns="http://a"><b xmlns="http://b"/></a>').__repr__(
            1) == '<a xmlns="http://a"><b xmlns="http://b"></b></a>'
Exemplo n.º 2
0
    def setUp(cls):
        # Create two unique labs
        lab1 = data_setup.create_labcontroller(fqdn=u'lab_%d' %
                                               int(time.time() * 1000))
        lab2 = data_setup.create_labcontroller(fqdn=u'lab_%d' %
                                               int(time.time() * 1000))

        # Create two distros and only put one in each lab.
        cls.distro_tree1 = data_setup.create_distro_tree()
        cls.distro_tree2 = data_setup.create_distro_tree()
        session.flush()
        cls.distro_tree1.lab_controller_assocs = [
            LabControllerDistroTree(lab_controller=lab2,
                                    url=u'http://notimportant')
        ]
        cls.distro_tree2.lab_controller_assocs = [
            LabControllerDistroTree(lab_controller=lab1,
                                    url=u'http://notimportant')
        ]

        # Create a user
        user = data_setup.create_user()

        # Create two systems but only put them in lab1.
        system1 = data_setup.create_system(owner=user)
        system2 = data_setup.create_system(owner=user)
        system1.lab_controller = lab1
        system2.lab_controller = lab1

        session.flush()

        # Create two jobs, one requiring distro_tree1 and one requiring distro_tree2
        job = '''
            <job>
                <whiteboard>%s</whiteboard>
                <recipeSet>
                    <recipe>
                        <distroRequires>
                            <distro_name op="=" value="%s" />
                        </distroRequires>
                        <hostRequires/>
                        <task name="/distribution/install" role="STANDALONE">
                            <params/>
                        </task>
                    </recipe>
                </recipeSet>
            </job>
                 '''
        xmljob1 = XmlJob(
            xmltramp.parse(
                job %
                (cls.distro_tree1.distro.name, cls.distro_tree1.distro.name)))
        xmljob2 = XmlJob(
            xmltramp.parse(
                job %
                (cls.distro_tree2.distro.name, cls.distro_tree2.distro.name)))

        cls.job1 = Jobs().process_xmljob(xmljob1, user)
        cls.job2 = Jobs().process_xmljob(xmljob2, user)
Exemplo n.º 3
0
    def setUp(cls):
        # Create two unique labs
        lab1 = data_setup.create_labcontroller(fqdn=u'lab_%d' %
                                               int(time.time() * 1000))
        lab2 = data_setup.create_labcontroller(fqdn=u'lab_%d' %
                                               int(time.time() * 1000))

        # Create two distros and only put one in each lab.
        cls.distro_tree1 = data_setup.create_distro_tree()
        cls.distro_tree2 = data_setup.create_distro_tree()
        session.flush()
        cls.distro_tree1.lab_controller_assocs = [LabControllerDistroTree(
                lab_controller=lab2, url=u'http://notimportant')]
        cls.distro_tree2.lab_controller_assocs = [LabControllerDistroTree(
                lab_controller=lab1, url=u'http://notimportant')]

        # Create a user
        user = data_setup.create_user()

        # Create two systems but only put them in lab1.
        system1 = data_setup.create_system(owner=user)
        system2 = data_setup.create_system(owner=user)
        system1.lab_controller = lab1
        system2.lab_controller = lab1

        session.flush()

        # Create two jobs, one requiring distro_tree1 and one requiring distro_tree2
        job = '''
            <job>
                <whiteboard>%s</whiteboard>
                <recipeSet>
                    <recipe>
                        <distroRequires>
                            <distro_name op="=" value="%s" />
                        </distroRequires>
                        <hostRequires/>
                        <task name="/distribution/install" role="STANDALONE">
                            <params/>
                        </task>
                    </recipe>
                </recipeSet>
            </job>
                 ''' 
        xmljob1 = XmlJob(xmltramp.parse(job % (cls.distro_tree1.distro.name,
                cls.distro_tree1.distro.name)))
        xmljob2 = XmlJob(xmltramp.parse(job % (cls.distro_tree2.distro.name,
                cls.distro_tree2.distro.name)))

        cls.job1 = Jobs().process_xmljob(xmljob1, user)
        cls.job2 = Jobs().process_xmljob(xmljob2, user)
Exemplo n.º 4
0
    def test_namespaces_repr(self):
        doc = Namespace("http://example.org/bar")
        bbc = Namespace("http://example.org/bbc")
        dc = Namespace("http://purl.org/dc/elements/1.1/")
        d = parse("""<doc version="2.7182818284590451"
        xmlns="http://example.org/bar"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:bbc="http://example.org/bbc">
            <author>John Polk and John Palfrey</author>
            <dc:creator>John Polk</dc:creator>
            <dc:creator>John Palfrey</dc:creator>
            <bbc:show bbc:station="4">Buffy</bbc:show>
        </doc>""")
        assert repr(d) == '<doc version="2.7182818284590451">...</doc>'
        # I supect py3 does not see equality in type below.
        #assert d.__repr__(1) == '<doc xmlns:bbc="http://example.org/bbc" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://example.org/bar" version="2.7182818284590451"><author>John Polk and John Palfrey</author><dc:creator>John Polk</dc:creator><dc:creator>John Palfrey</dc:creator><bbc:show bbc:station="4">Buffy</bbc:show></doc>'
        #assert d.__repr__(1, 1) == '<doc xmlns:bbc="http://example.org/bbc" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://example.org/bar" version="2.7182818284590451">\n\t<author>John Polk and John Palfrey</author>\n\t<dc:creator>John Polk</dc:creator>\n\t<dc:creator>John Palfrey</dc:creator>\n\t<bbc:show bbc:station="4">Buffy</bbc:show>\n</doc>'
        assert repr(parse("<doc xml:lang='en' />")) == '<doc xml:lang="en"></doc>'
        self.assertEqual(d.author, d['author'])
        assert d.author == "John Polk and John Palfrey"
        assert d.author._name == doc.author
        assert str(d[dc.creator]) == "John Polk"
        assert d[dc.creator]._name == dc.creator
        assert str(d[dc.creator:][1]) == "John Palfrey"
        d[dc.creator] = "Me!!!"
        assert str(d[dc.creator]) == "Me!!!"
        assert len(d[dc.creator:]) == 1
        d[dc.creator:] = "You!!!"
        assert len(d[dc.creator:]) == 2
        assert d[bbc.show](bbc.station) == "4"
        d[bbc.show](bbc.station, "5")
        assert d[bbc.show](bbc.station) == "5"

        e = Element('e')
        e.c = '<img src="foo">'
        assert e.__repr__(1) == '<e><c>&lt;img src="foo"></c></e>'
        e.c = '2 > 4'
        assert e.__repr__(1) == '<e><c>2 > 4</c></e>'
        e.c = 'CDATA sections are <em>closed</em> with ]]>.'
        assert e.__repr__(1) == '<e><c>CDATA sections are &lt;em>closed&lt;/em> with ]]&gt;.</c></e>'
        e.c = parse('<div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you</div>')
        assert e.__repr__(1) == '<e><c><div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you</div></c></e>'

        e = Element('e')
        e('c', 'that "sucks"')
        assert e.__repr__(1) == '<e c="that &quot;sucks&quot;"></e>'
        assert quote("]]>") == "]]&gt;"
        assert quote('< dkdkdsd dkd sksdksdfsd fsdfdsf]]> kfdfkg >') == '&lt; dkdkdsd dkd sksdksdfsd fsdfdsf]]&gt; kfdfkg >'
        assert parse('<x a="&lt;"></x>').__repr__(1) == '<x a="&lt;"></x>'
        assert parse('<a xmlns="http://a"><b xmlns="http://b"/></a>').__repr__(1) == '<a xmlns="http://a"><b xmlns="http://b"></b></a>'
Exemplo n.º 5
0
 def getPhotoListingFromPhotoSet(self, photoSetId, page=1):
     #print "Getting photo listing for photoset", photoSetId, "for page", page, "...",
     d = {
         api.method: "flickr.photosets.getPhotos",
         api.token: str(self.token),
         api.perms: str(self.perms),
         "photoset_id": str(photoSetId),
         "per_page": str(500),
         "page": str(page)
     }
     sig = self.signCall(d)
     d[api.sig] = sig
     d[api.key] = FLICKR[api.key]
     url = self.build_request(api.rest, d, ())
     xml = urllib2.urlopen(url).read()
     res = xmltramp.parse(xml)
     if (self.isGood(res)):
         #print "successful."
         photos = []
         for photo in res.photoset:
             photos.append(photo('title').encode('ascii', 'ignore'))
         if photoSetId in self.listings:
             self.listings[photoSetId].extend(photos)
         else:
             self.listings[photoSetId] = photos
         if int(res.photoset('page')) < int(res.photoset('pages')):
             self.getPhotoListingFromPhotoSet(photoSetId, page + 1)
     else:
         print "Problem while getting photo listing for photoset", photoSetId
         self.reportError(res)
     sys.stdout.flush()
Exemplo n.º 6
0
 def createPhotoSet(self, photoSetIdFile, directoryName, photoId):
     print "Creating photoSet for folder", directoryName, "...",
     d = {
         api.method: "flickr.photosets.create",
         api.token: str(self.token),
         api.perms: str(self.perms),
         "title": str(directoryName),
         "description": str(directoryName),
         "primary_photo_id": str(photoId)
     }
     sig = self.signCall(d)
     d[api.sig] = sig
     d[api.key] = FLICKR[api.key]
     url = self.build_request(api.rest, d, ())
     xml = urllib2.urlopen(url).read()
     res = xmltramp.parse(xml)
     if (self.isGood(res)):
         print "successful."
         photoSetId = res.photoset('id')
         self.listings[photoSetId] = []
         self.setCachedPhotoSetId(photoSetIdFile, photoSetId)
     else:
         print "problem.."
         self.reportError(res)
     sys.stdout.flush()
Exemplo n.º 7
0
 def uploadImage(self, image):
     if (not self.isAlreadyUploaded(image)):
         print "Uploading ", image, "...",
         try:
             photo = ('photo', image, open(image, 'rb').read())
             filename = self.getImageTitle(image)
             d = {
                 api.token: str(self.token),
                 api.perms: str(self.perms),
                 "tags": str(FLICKR["tags"]),
                 "is_public": str(FLICKR["is_public"]),
                 "is_friend": str(FLICKR["is_friend"]),
                 "is_family": str(FLICKR["is_family"]),
                 "title": str(filename)
             }
             sig = self.signCall(d)
             d[api.sig] = sig
             d[api.key] = FLICKR[api.key]
             url = self.build_request(api.upload, d, (photo, ))
             xml = urllib2.urlopen(url).read()
             res = xmltramp.parse(xml)
             if (self.isGood(res)):
                 print "successful."
                 self.addImageToFlickrSet(res.photoid, image)
             else:
                 print "problem.."
                 self.reportError(res)
             sys.stdout.flush()
         except:
             print str(sys.exc_info())
     else:
         #print "Already uploaded image", image
         pass
Exemplo n.º 8
0
def get_youtube_data(video):
    """
    Helper to extract video and thumbnail from youtube
    """
    video.source = 'youtube'
    if 'youtube.com/watch' in video.url:
        parsed = urlparse.urlsplit(video.url)
        query  = urlparse.parse_qs(parsed.query)
        try:
            video.key  = query.get('v')[0]
        except IndexError:
            video.key = None
    else:
        video.key = video.url.rsplit('/', 1)[1]
    video.embed_src = 'http://www.youtube.com/embed/'
    #http://gdata.youtube.com/feeds/api/videos/Agdvt9M3NJA
    api_url = 'http://gdata.youtube.com/feeds/api/videos/%s' % video.key
    video_data = urllib.urlopen(api_url).read()
    xml = xmltramp.parse(video_data)

    video.title = unicode(xml.title)
    video.slug = slugify(video.title)
    video.summary = unicode(xml.content)
    video.thumb_url = xml[xml_media.group][xml_media.thumbnail:][1]('url')
    return video
Exemplo n.º 9
0
    def technorati_search(self, query):
        """
        Given a query string, perform a Technorati search.
        """
        # Construct a Technorati search URL and fetch it.
        url  = self.SEARCH_URL_TMPL % \
               ( self.TECHNORATI_KEY, urllib.quote_plus(query) )
        data = HTTPCache(url).content()

        # HACK: I get occasional encoding issues with Technorati, so
        # here's an ugly hack that seems to make things work anyway.
        try:
            return xmltramp.parse(data).document
        except SAXParseException:
            data = data.decode('ascii', 'ignore')
            return xmltramp.parse(data).document
Exemplo n.º 10
0
    def test_abort_recipe_bubbles_status_to_job(self):
        xmljob = XmlJob(xmltramp.parse('''
            <job>
                <whiteboard>job </whiteboard>
                <recipeSet>
                    <recipe>
                        <distroRequires>
                            <distro_name op="=" value="BlueShoeLinux5-5" />
                        </distroRequires>
                        <hostRequires/>
                        <task name="/distribution/install" role="STANDALONE">
                            <params/>
                        </task>
                    </recipe>
                </recipeSet>
                <recipeSet>
                    <recipe>
                        <distroRequires>
                            <distro_name op="=" value="BlueShoeLinux5-5" />
                        </distroRequires>
                        <hostRequires/>
                        <task name="/distribution/install" role="STANDALONE">
                            <params/>
                        </task>
                    </recipe>
                </recipeSet>
            </job>
            '''))
        job = self.controller.process_xmljob(xmljob, self.user)
        session.flush()
        for recipeset in job.recipesets:
            for recipe in recipeset.recipes:
                recipe.process()
                recipe.queue()
                recipe.schedule()
                recipe.waiting()

        # Abort the first recipe.
        job.recipesets[0].recipes[0].abort()
        job.update_status()

        # Verify that it and its children are aborted.
        self.assertEquals(job.recipesets[0].recipes[0].status, TaskStatus.aborted)
        for task in job.recipesets[0].recipes[0].tasks:
            self.assertEquals(task.status, TaskStatus.aborted)

        # Verify that the second recipe and its children are still waiting.
        self.assertEquals(job.recipesets[1].recipes[0].status, TaskStatus.waiting)
        for task in job.recipesets[1].recipes[0].tasks:
            self.assertEquals(task.status, TaskStatus.waiting)

        # Verify that the job still shows waiting.
        self.assertEquals(job.status, TaskStatus.waiting)

        # Abort the second recipe now.
        job.recipesets[1].recipes[0].abort()
        job.update_status()

        # Verify that the whole job shows aborted now.
        self.assertEquals(job.status, TaskStatus.aborted)
Exemplo n.º 11
0
    def getResponse( self, url ):
        """
        Send the url and get a response.  Let errors float up
        """

        xml = urllib2.urlopen( url ).read()
        return xmltramp.parse( xml )
Exemplo n.º 12
0
 def getPhotoListingFromPhotoSet( self, photoSetId, page=1):
     #print "Getting photo listing for photoset", photoSetId, "for page", page, "...",
     d = {
         api.method   : "flickr.photosets.getPhotos",
         api.token    : str(self.token),
         api.perms    : str(self.perms),
         "photoset_id": str( photoSetId ),
         "per_page"   : str( 500 ),
         "page"       : str( page )
     }
     sig = self.signCall( d )
     d[ api.sig ] = sig
     d[ api.key ] = FLICKR[ api.key ]
     url = self.build_request(api.rest, d, ())
     xml = urllib2.urlopen( url ).read()
     res = xmltramp.parse(xml)
     if ( self.isGood( res ) ):
         #print "successful."
         photos = []
         for photo in res.photoset:
             photos.append(photo('title').encode('ascii', 'ignore'))
         if photoSetId in self.listings :
             self.listings[photoSetId].extend(photos)
         else :
             self.listings[photoSetId] = photos
         if int(res.photoset('page')) < int(res.photoset('pages')) :
             self.getPhotoListingFromPhotoSet(photoSetId, page+1)
     else :
         print "Problem while getting photo listing for photoset",photoSetId
         self.reportError( res )
     sys.stdout.flush()
Exemplo n.º 13
0
 def createPhotoSet( self, photoSetIdFile, directoryName, photoId):
     print "Creating photoSet for folder", directoryName , "...",
     d = {
         api.method   : "flickr.photosets.create",
         api.token    : str(self.token),
         api.perms    : str(self.perms),
         "title"      : str(directoryName),
         "description": str(directoryName),
         "primary_photo_id"   : str(photoId)
     }
     sig = self.signCall( d )
     d[ api.sig ] = sig
     d[ api.key ] = FLICKR[ api.key ]        
     url = self.build_request(api.rest, d, ())    
     xml = urllib2.urlopen( url ).read()
     res = xmltramp.parse(xml)
     if ( self.isGood( res ) ):
         print "successful."
         photoSetId  = res.photoset('id')
         self.listings[photoSetId] = [];
         self.setCachedPhotoSetId(photoSetIdFile, photoSetId)
     else :
         print "problem.."
         self.reportError( res )
     sys.stdout.flush()
Exemplo n.º 14
0
 def uploadImage( self, image ):
     if ( not self.isAlreadyUploaded(image) ):
         print "Uploading ", image , "...",
         try:
             photo = ('photo', image, open(image,'rb').read())
             filename = self.getImageTitle(image)
             d = {
                 api.token   : str(self.token),
                 api.perms   : str(self.perms),
                 "tags"      : str( FLICKR["tags"] ),
                 "is_public" : str( FLICKR["is_public"] ),
                 "is_friend" : str( FLICKR["is_friend"] ),
                 "is_family" : str( FLICKR["is_family"] ),
                 "title"     : str( filename )
             }
             sig = self.signCall( d )
             d[ api.sig ] = sig
             d[ api.key ] = FLICKR[ api.key ]        
             url = self.build_request(api.upload, d, (photo,))    
             xml = urllib2.urlopen( url ).read()
             res = xmltramp.parse(xml)
             if ( self.isGood( res ) ):
                 print "successful."
                 self.addImageToFlickrSet( res.photoid, image )
             else :
                 print "problem.."
                 self.reportError( res )
             sys.stdout.flush()
         except:
             print str(sys.exc_info())
     else:
         #print "Already uploaded image", image
         pass
Exemplo n.º 15
0
def get_youtube_data(video):
    """
    Helper to extract video and thumbnail from youtube
    """
    video.source = 'youtube'
    if 'youtube.com/watch' in video.url:
        parsed = urlparse.urlsplit(video.url)
        query = urlparse.parse_qs(parsed.query)
        try:
            video.key = query.get('v')[0]
        except IndexError:
            video.key = None
    else:
        video.key = video.url.rsplit('/', 1)[1]
    video.embed_src = 'http://www.youtube.com/embed/'
    #http://gdata.youtube.com/feeds/api/videos/Agdvt9M3NJA
    api_url = 'http://gdata.youtube.com/feeds/api/videos/%s' % video.key
    video_data = urllib.urlopen(api_url).read()
    xml = xmltramp.parse(video_data)

    video.title = unicode(xml.title)
    video.slug = slugify(video.title)
    video.summary = unicode(xml.content)
    video.thumb_url = xml[xml_media.group][xml_media.thumbnail:][1]('url')
    return video
Exemplo n.º 16
0
 def addSetToCollection( self, set_id , collection_id ):
     success = False
     print("Adding set with id " + str(set_id) + " to collection with id " + str(collection_id) + "...")
     try:
         d = {
             api.token       : str(self.token),
             api.perms       : str(self.perms),
             "method"        : "flickr.collections.addSet",
             "collection_id" : str(collection_id),
             "photoset_id"   : str(set_id)
         }
         sig = self.signCall( d )
         d[ api.sig ] = sig
         d[ api.key ] = FLICKR[ api.key ]
         url = self.build_request(api.rest, d, ())
         xml = urllib2.urlopen( url ).read()
         res = xmltramp.parse(xml)
         if ( self.isGood( res ) ):
             print("    Success.")
             success = True
         else :
             print("    Problem:")
             self.reportError( res )
     except KeyboardInterrupt:
         flick.printStats()
         print "\nUploading session interrupted by user..."
         sys.exit()
     except:
         print(str(sys.exc_info()))
     return success
 def fetch_items(self):
     """
     Grab search result items for given index and keywords.
     """
     # Construct the list of arguments for the AWS query
     args = {
         'Service'        : 'AWSECommerceService',
         'Operation'      : 'ListLookup',
         'ResponseGroup'  : 'Medium,ListFull,ItemAttributes',
         'Sort'           : 'LastUpdated',
         'ListType'       : 'WishList',
         'ListId'         : self.wishlist_id,
         'SubscriptionId' : self.aws_id,
     }
     
     # Build the URL for the API call using the base URL and params.
     url = "%s?%s" % (self.AWS_URL, urllib.urlencode(args))
     
     # Perform the query, fetch and parse the results.
     data  = HTTPCache(url).content()
     doc   = xmltramp.parse(data)
     
     # Update the feed link and title from search result metadata
     self.FEED_META['feed.link']  = doc.Lists.List.ListURL
     self.FEED_META['feed.title'] = \
         'Amazon.com wishlist items for "%s"' % \
         doc.Lists.List.CustomerName
     
     # Fetch first page of items.
     return [ x.Item for x in doc.Lists.List if 'ListItem' in x._name ]
Exemplo n.º 18
0
def character( guild, character, force = False ):
    if not guild and not character.guild:
        # erase characters without a guild
        logging.info("deleting guildless character %s"%character.name)
        character.delete()
        return

    try:
        raw_xml = fetch_raw( character.armory_url() )
    except FetchError:
        logging.info("fetcherror")
        return # normally this is an armory failure. I'm not going to put clever handling in here.
    
    char_xml = xmltramp.parse( raw_xml )
    try:
        char = char_xml['characterInfo']['character']
    except KeyError:
        logging.info("keyerror")
        return # normally armoury error

    character.achPoints = long(char('points'))
    
    added_count = add_achievements( character, raw_xml )
    if added_count >= 5:
        logging.info("Seen at least 5 new achievements - running backfill")
        added_count = backfill( character, char_xml )
    
    logging.info("added %d achievements"%added_count)

    # otherwise we're done.
    character.last_fetch = datetime.utcnow()
    character.put()

    guild.update_achievements_cache_for( character )
    guild.put()
def main():
    """
    Search for wishlists using command line arguments.
    """
    # Leaving out the program name, grab all space-separated arguments.
    name = " ".join(sys.argv[1:])

    # Construct the list of arguments for the AWS query
    args = {
        'Service': 'AWSECommerceService',
        'Operation': 'ListSearch',
        'ListType': 'WishList',
        'SubscriptionId': AWS_ID,
        'Name': name
    }

    # Build the URL for the API call using the base URL and params.
    url = "%s?%s" % (AWS_URL, urllib.urlencode(args))

    # Perform the query, fetch and parse the results.
    data = HTTPCache(url).content()
    doc = xmltramp.parse(data)

    # Print out the list IDs found.
    lists = [x for x in doc.Lists if 'List' in x._name]
    for list in lists:
        print '%15s: %s' % (list.ListId, list.CustomerName)
Exemplo n.º 20
0
 def uploadImage( self, image ):
     if ( not self.uploaded.has_key( image ) ):
         print "Uploading ", image , "...",
         try:
             photo = ('photo', image, open(image,'rb').read())
             d = {
                 api.token   : str(self.token),
                 api.perms   : str(self.perms),
                 "tags"      : str( FLICKR["tags"] ),
                 "is_public" : str( FLICKR["is_public"] ),
                 "is_friend" : str( FLICKR["is_friend"] ),
                 "is_family" : str( FLICKR["is_family"] )
             }
             sig = self.signCall( d )
             d[ api.sig ] = sig
             d[ api.key ] = FLICKR[ api.key ]        
             url = self.build_request(api.upload, d, (photo,))    
             xml = urllib2.urlopen( url ).read()
             res = xmltramp.parse(xml)
             if ( self.isGood( res ) ):
                 print "successful."
                 self.logUpload( res.photoid, image )
                 message = "Uploaded photo " + image
                 #self.growlNotify( "Photo uploaded", message)
                 return True
             else :
                 print "problem.."
                 self.reportError( res )
                 return False
         except:
             print str(sys.exc_info())
Exemplo n.º 21
0
	def post(self, conn=None, alwaysReturnList=False):
		headers = { "User-Agent": "BeatBox/" + __version__,
					"SOAPAction": "\"\"",
					"Content-Type": "text/xml; charset=utf-8" }
		if gzipResponse:
			headers['accept-encoding'] = 'gzip'
		if gzipRequest:
			headers['content-encoding'] = 'gzip'					
		close = False
		(scheme, host, path, params, query, frag) = urlparse(self.serverUrl)
		if conn == None:
			conn = makeConnection(scheme, host)
			close = True
		conn.request("POST", path, self.makeEnvelope(), headers)
		response = conn.getresponse()
		rawResponse = response.read()
		if response.getheader('content-encoding','') == 'gzip':
			rawResponse = gzip.GzipFile(fileobj=StringIO(rawResponse)).read()
		if close:
			conn.close()
		tramp = xmltramp.parse(rawResponse)
		try:
			faultString = str(tramp[_tSoapNS.Body][_tSoapNS.Fault].faultstring)
			faultCode   = str(tramp[_tSoapNS.Body][_tSoapNS.Fault].faultcode).split(':')[-1]
			raise SoapFaultError(faultCode, faultString)
		except KeyError:
			pass
		# first child of body is XXXXResponse
		result = tramp[_tSoapNS.Body][0]
		# it contains either a single child, or for a batch call multiple children
		if alwaysReturnList or len(result) > 1:
			return result[:]
		else:
			return result[0]
Exemplo n.º 22
0
 def addSetToCollection(self, set_id, collection_id):
     success = False
     print("Adding set with id " + str(set_id) + " to collection with id " +
           str(collection_id) + "...")
     try:
         d = {
             api.token: str(self.token),
             api.perms: str(self.perms),
             "method": "flickr.collections.addSet",
             "collection_id": str(collection_id),
             "photoset_id": str(set_id)
         }
         sig = self.signCall(d)
         d[api.sig] = sig
         d[api.key] = FLICKR[api.key]
         url = self.build_request(api.rest, d, ())
         xml = urllib2.urlopen(url).read()
         res = xmltramp.parse(xml)
         if (self.isGood(res)):
             print("    Success.")
             success = True
         else:
             print("    Problem:")
             self.reportError(res)
     except KeyboardInterrupt:
         flick.printStats()
         print "\nUploading session interrupted by user..."
         sys.exit()
     except:
         print(str(sys.exc_info()))
     return success
Exemplo n.º 23
0
    def uploadImage(self, image):
        """ uploadImage
        """

        if not self.uploaded.has_key(image):
            print("Uploading ", image, "...")
            try:
                photo = ("photo", image, open(image, "rb").read())
                d = {
                    api.token: str(self.token),
                    api.perms: str(self.perms),
                    "title": str(FLICKR["title"]),
                    "description": str(FLICKR["description"]),
                    "tags": str(FLICKR["tags"]),
                    "is_public": str(FLICKR["is_public"]),
                    "is_friend": str(FLICKR["is_friend"]),
                    "is_family": str(FLICKR["is_family"]),
                }
                sig = self.signCall(d)
                d[api.sig] = sig
                d[api.key] = FLICKR[api.key]
                url = self.build_request(api.upload, d, (photo,))
                xml = urllib2.urlopen(url).read()
                res = xmltramp.parse(xml)
                if self.isGood(res):
                    print("successful.")
                    self.logUpload(res.photoid, image)
                else:
                    print("problem..")
                    self.reportError(res)
            except:
                print(str(sys.exc_info()))
Exemplo n.º 24
0
    def uploadImage( self, image ):
        if ( not (LOG_UPLOADED and self.uploaded.has_key( image ) ) ):
            print "Uploading ", image , "...",
            try:
                photo = ('photo', image, open(image,'rb').read())
                d = {
                    api.token   : str(self.token),
                    api.perms   : str(self.perms),
                    "tags"      : str( FLICKR["tags"] ),
                    "is_public" : str( FLICKR["is_public"] ),
                    "is_friend" : str( FLICKR["is_friend"] ),
                    "is_family" : str( FLICKR["is_family"] )
                }
                sig = self.signCall( d )
                d[ api.sig ] = sig
                d[ api.key ] = FLICKR[ api.key ]        
                url = self.build_request(api.upload, d, (photo,))    
                xml = urllib2.urlopen( url ).read()
                res = xmltramp.parse(xml)
                if ( self.isGood( res ) ):
                    print "successful."
                    if ( LOG_UPLOADED ):
                        self.logUpload( res.photoid, image )
                    return res.photoid
                else :
                    print "problem.."
                    self.reportError( res )
	    except KeyboardInterrupt:
		print "stopping:";
		if (LOG_UPLOADED): self.uploaded.close();
		sys.exit(-1);		    
            except:
        	print "ERROR"
                print str(sys.exc_info())
def main():
    """
    Search for wishlists using command line arguments.
    """
    # Leaving out the program name, grab all space-separated arguments.
    name   = " ".join(sys.argv[1:])
    
    # Construct the list of arguments for the AWS query
    args = {
        'Service'        : 'AWSECommerceService',
        'Operation'      : 'ListSearch',
        'ListType'       : 'WishList',
        'SubscriptionId' : AWS_ID,
        'Name'           : name
    }
    
    # Build the URL for the API call using the base URL and params.
    url = "%s?%s" % (AWS_URL, urllib.urlencode(args))
    
    # Perform the query, fetch and parse the results.
    data  = HTTPCache(url).content()
    doc   = xmltramp.parse(data)
    
    # Print out the list IDs found.
    lists = [ x for x in doc.Lists if 'List' in x._name ]
    for list in lists:
        print '%15s: %s' % ( list.ListId, list.CustomerName )
Exemplo n.º 26
0
    def getResponse(self, url):
        """
        Send the url and get a response.  Let errors float up
        """

        xml = urllib2.urlopen(url).read()
        return xmltramp.parse(xml)
Exemplo n.º 27
0
    def uploadImage( self, image ):
        """ uploadImage
        """
	if self.history.uploaded(image):
	  print "Already uploaded ", image, ", skipping."
	else:
	    success = False

	    while not success:
	      print "Uploading ", image , "..."
	      try:
		  photo = ('photo', image, open(image,'rb').read())
		  d = {
		      api.token   : str(self.token),
		      api.perms   : str(self.perms),
		      "tags"      : str( FLICKR["tags"] ),
		      "is_public" : str( FLICKR["is_public"] ),
		      "is_friend" : str( FLICKR["is_friend"] ),
		      "is_family" : str( FLICKR["is_family"] )
		  }
		  sig = self.signCall( d )
		  d[ api.sig ] = sig
		  d[ api.key ] = FLICKR[ api.key ]
		  url = self.build_request(api.upload, d, (photo,))
		  xml = urllib2.urlopen( url ).read()
		  res = xmltramp.parse(xml)
		  if self.isGood(res):
		      print "successful."
		      self.logUpload(res.photoid, image)
		      success = True
		  else:
		      print "problem."
		      self.reportError( res )
	      except:
		  logging.exception('Failed upload')
Exemplo n.º 28
0
    def uploadImage(self, image):
        if (not self.uploaded.has_key(image)):
            print "Uploading ", image, "...",
            try:
                photo = ('photo', image, open(image, 'rb').read())
                d = {
                    api.token: str(self.token),
                    api.perms: str(self.perms),
                    "tags": str(FLICKR["tags"]),
                    "is_public": str(FLICKR["is_public"]),
                    "is_friend": str(FLICKR["is_friend"]),
                    "is_family": str(FLICKR["is_family"])
                }
                sig = self.signCall(d)
                d[api.sig] = sig
                d[api.key] = FLICKR[api.key]
                url = self.build_request(api.upload, d, (photo, ))
                xml = urllib2.urlopen(url).read()
                res = xmltramp.parse(xml)

                if (self.isGood(res)):
                    print "successful."
                    self.logUpload(res.photoid, image)
                    self.tweetAboutUpload(res.photoid, image)
                else:
                    print "problem.."
                    self.reportError(res)
            except:
                print str(sys.exc_info())
Exemplo n.º 29
0
 def test_uploading_job_with_invalid_hostRequires_raises_exception(self):
     session.begin()
     try:
         xmljob = XmlJob(xmltramp.parse('''
             <job>
                 <whiteboard>job with invalid hostRequires</whiteboard>
                 <recipeSet>
                     <recipe>
                         <distroRequires>
                             <distro_name op="=" value="BlueShoeLinux5-5" />
                         </distroRequires>
                         <hostRequires>
                             <memory op=">=" value="500MB" />
                         </hostRequires>
                         <task name="/distribution/install" role="STANDALONE">
                             <params/>
                         </task>
                     </recipe>
                 </recipeSet>
             </job>
             '''))
         self.assertRaises(BX, lambda: self.controller.process_xmljob(xmljob, self.user))
     finally:
         session.rollback()
         session.close()
Exemplo n.º 30
0
 def test_uploading_job_with_invalid_hostRequires_raises_exception(self):
     session.begin()
     try:
         xmljob = XmlJob(
             xmltramp.parse('''
             <job>
                 <whiteboard>job with invalid hostRequires</whiteboard>
                 <recipeSet>
                     <recipe>
                         <distroRequires>
                             <distro_name op="=" value="BlueShoeLinux5-5" />
                         </distroRequires>
                         <hostRequires>
                             <memory op=">=" value="500MB" />
                         </hostRequires>
                         <task name="/distribution/install" role="STANDALONE">
                             <params/>
                         </task>
                     </recipe>
                 </recipeSet>
             </job>
             '''))
         self.assertRaises(
             BX, lambda: self.controller.process_xmljob(xmljob, self.user))
     finally:
         session.rollback()
Exemplo n.º 31
0
 def uploadImage( self, image ):
     print "Uploading ", image , "..."
     try:
         photo = ('photo', image, open(image,'rb').read())
         d = {
             api.token   : str(self.token),
             api.perms   : str(self.perms),
             "tags"      : str( FLICKR["tags"] ),
             "is_public" : str( FLICKR["is_public"] ),
             "is_friend" : str( FLICKR["is_friend"] ),
             "is_family" : str( FLICKR["is_family"] )
         }
         sig = self.signCall( d )
         d[ api.sig ] = sig
         d[ api.key ] = FLICKR[ api.key ]        
         url = self.build_request(api.upload, d, (photo,))    
         xml = urllib2.urlopen( url ).read()
         res = xmltramp.parse(xml)
         if ( self.isGood( res ) ):
             print "successful."
             self.logUpload( image )
             self.addPhotoToSet(image, res.photoid)
         else :
             print "problem.."
             self.reportError( res )
     except:
         print str(sys.exc_info())
Exemplo n.º 32
0
 def test_uploading_job_without_recipeset_raises_exception(self):
     xmljob = XmlJob(xmltramp.parse('''
         <job>
             <whiteboard>job with norecipesets</whiteboard>
         </job>
         '''))
     with session.begin():
         self.assertRaises(BX, lambda: self.controller.process_xmljob(xmljob, self.user))
Exemplo n.º 33
0
def serviceGetParameters():
    printDebugMessage('serviceGetParameters', 'Begin', 1)
    requestUrl = baseUrl + '/parameters'
    printDebugMessage('serviceGetParameters', 'requestUrl: ' + requestUrl, 2)
    xmlDoc = restRequest(requestUrl)
    doc = xmltramp.parse(xmlDoc)
    printDebugMessage('serviceGetParameters', 'End', 1)
    return doc['id':]
Exemplo n.º 34
0
 def getInfo( self ):
     # get user NSID
     try:
         d = {
             api.token          : str(self.token),
             api.perms          : str(self.perms),
             "method"           : "flickr.urls.getUserProfile",
         }
         sig = self.signCall( d )
         d[ api.sig ] = sig
         d[ api.key ] = FLICKR[ api.key ]
         url = self.build_request(api.rest, d, ())
         xml = urllib2.urlopen( url ).read()
         res = xmltramp.parse(xml)
         if ( self.isGood( res ) ):
             self.nsid = str(res[0]('nsid'));
         else :
             self.reportError( res )
             raise
     except:
         print(str(sys.exc_info()))
         sys.exit()
     
     # get username and real name
     try:
         d = {
             api.token          : str(self.token),
             api.perms          : str(self.perms),
             "method"           : "flickr.people.getInfo",
             "user_id"          : self.nsid
         }
         sig = self.signCall( d )
         d[ api.sig ] = sig
         d[ api.key ] = FLICKR[ api.key ]
         url = self.build_request(api.rest, d, ())
         xml = urllib2.urlopen( url ).read()
         res = xmltramp.parse(xml)
         if ( self.isGood( res ) ):
             self.username = res[0][0][0]
             self.realname = res[0][1][0]
         else :
             self.reportError( res )
             raise
     except:
         print(str(sys.exc_info()))
         sys.exit()
Exemplo n.º 35
0
    def getInfo(self):
        # get user NSID
        try:
            d = {
                api.token: str(self.token),
                api.perms: str(self.perms),
                "method": "flickr.urls.getUserProfile",
            }
            sig = self.signCall(d)
            d[api.sig] = sig
            d[api.key] = FLICKR[api.key]
            url = self.build_request(api.rest, d, ())
            xml = urllib2.urlopen(url).read()
            res = xmltramp.parse(xml)
            if (self.isGood(res)):
                self.nsid = str(res[0]('nsid'))
            else:
                self.reportError(res)
                raise
        except:
            print(str(sys.exc_info()))
            sys.exit()

        # get username and real name
        try:
            d = {
                api.token: str(self.token),
                api.perms: str(self.perms),
                "method": "flickr.people.getInfo",
                "user_id": self.nsid
            }
            sig = self.signCall(d)
            d[api.sig] = sig
            d[api.key] = FLICKR[api.key]
            url = self.build_request(api.rest, d, ())
            xml = urllib2.urlopen(url).read()
            res = xmltramp.parse(xml)
            if (self.isGood(res)):
                self.username = res[0][0][0]
                self.realname = res[0][1][0]
            else:
                self.reportError(res)
                raise
        except:
            print(str(sys.exc_info()))
            sys.exit()
Exemplo n.º 36
0
def serviceGetParameters():
    printDebugMessage('serviceGetParameters', 'Begin', 1)
    requestUrl = baseUrl + '/parameters'
    printDebugMessage('serviceGetParameters', 'requestUrl: ' + requestUrl, 2)
    xmlDoc = restRequest(requestUrl)
    doc = xmltramp.parse(xmlDoc)
    printDebugMessage('serviceGetParameters', 'End', 1)
    return doc['id':]
Exemplo n.º 37
0
 def post(self, conn=None, alwaysReturnList=False):
     logger.debug(self.__class__)
     headers = { "User-Agent": "BeatBox/" + __version__,
                 "SOAPAction": "\"\"",
                 "Content-Type": "text/xml; charset=utf-8" }
     if gzipResponse:
         headers['accept-encoding'] = 'gzip'
     if gzipRequest:
         headers['content-encoding'] = 'gzip'
     close = False
     (scheme, host, path, params, query, frag) = urlparse(self.serverUrl)
     max_attempts = 3
     response = None
     attempt = 1
     while not response and attempt <= max_attempts:
         print 'attempting connection ----------'
         try:
             if conn == None:
                 conn = makeConnection(scheme, host)
                 close = True
             print 'connection made ----------'
             print 'sent payload ---------'
             print self.makeEnvelope()
             conn.request("POST", path, self.makeEnvelope(), headers)
             response = conn.getresponse()
             rawResponse = response.read()
         except (httplib.HTTPException, socket.error):
             if conn != None:
                 conn.close()
                 conn = None
                 response = None
             attempt += 1
     if not response:
         raise RuntimeError, 'No response from Salesforce'
     
     if response.getheader('content-encoding','') == 'gzip':
         rawResponse = gzip.GzipFile(fileobj=StringIO(rawResponse)).read()
         print '--------- response '
         print rawResponse
     if close:
         conn.close()
     tramp = xmltramp.parse(rawResponse)
     try:
         faultString = str(tramp[_tSoapNS.Body][_tSoapNS.Fault].faultstring)
         faultCode   = str(tramp[_tSoapNS.Body][_tSoapNS.Fault].faultcode).split(':')[-1]
         if faultCode == 'INVALID_SESSION_ID':
             raise SessionTimeoutError(faultCode, faultString)
         else:
             raise SoapFaultError(faultCode, faultString)
     except KeyError:
         pass
     # first child of body is XXXXResponse
     result = tramp[_tSoapNS.Body][0]
     # it contains either a single child, or for a batch call multiple children
     if alwaysReturnList or len(result) > 1:
         return result[:]
     else:
         return result[0]
def main():
    tmpl  = 'http://api.technorati.com/search?key=%s&limit=5&query=%s'
    key   = open("technorati-key.txt", "r").read().strip()
    query = (len(sys.argv) > 1) and sys.argv[1] or 'test query'
    url   = tmpl % (key, urllib.quote_plus(query))
    data  = HTTPCache(url).content()
    
    # HACK: I get occasional encoding issues with Technorati, so
    # here's an ugly hack that seems to make things work anyway.
    try:
        doc = xmltramp.parse(data)
    except SAXParseException:
        data = data.decode('utf8', 'ignore').encode('utf8')
        doc = xmltramp.parse(data)

    items = [ x for x in doc.document if x._name == 'item' ]
    for i in items:
        print '"%(title)s"\n\t%(permalink)s' % i
Exemplo n.º 39
0
    def uploadImage( self, image ):
        folderTag = image[len(IMAGE_DIR):]
        #print folderTag
        #return
        if ( not self.uploaded.has_key( folderTag ) ):

            try:
                logging.debug( "Getting EXIF for %s" % image)
                f = open(image, 'rb')
                exiftags = exif.process_file(f)
                f.close()
                #print exiftags[XPKEYWORDS]

                
                #print folderTag
                #make one tag equal to original file path with spaces replaced by # and start it with # (for easier recognition) since space is used as TAG separator by flickr
                # this is needed for later syncing flickr with folders 
                realTags  = folderTag.replace('\\',' ')   # look for / or \ or _ or .  and replace them with SPACE to make real Tags 
                realTags =  realTags.replace('/',' ')   # these will be the real tags ripped from folders
                realTags =  realTags.replace('_',' ')
                realTags =  realTags.replace('.',' ')  
                picTags = '#' + folderTag.replace(' ','#') + ' ' + realTags

                if exiftags == {}:
                   logging.debug( 'NO_EXIF_HEADER for %s' % image)
                else:
                   if XPKEYWORDS in exiftags:  #look for additional tags in EXIF to tag picture with
                            if len(exiftags[XPKEYWORDS].printable) > 4:
                                picTags += exif.make_string( eval(exiftags[XPKEYWORDS].printable)).replace(';',' ')
                
                #print picTags
                logging.debug( "Uploading image %s" % image)
                photo = ('photo', image, open(image,'rb').read())


                d = {
                    api.token   : str(self.token),
                    api.perms   : str(self.perms),
                    "tags"      : str(picTags),
                    "is_public" : str( FLICKR["is_public"] ),
                    "is_friend" : str( FLICKR["is_friend"] ),
                    "is_family" : str( FLICKR["is_family"] )
                }
                sig = self.signCall( d )
                d[ api.sig ] = sig
                d[ api.key ] = FLICKR[ api.key ]
                url = self.build_request(api.upload, d, (photo,))
                xml = urllib2.urlopen( url ).read()
                res = xmltramp.parse(xml)
                if ( self.isGood( res ) ):
                    logging.debug( "successful.")
                    self.logUpload( res.photoid, folderTag )
                else :
                    print "problem.."
                    self.reportError( res )
            except:
                logging.error(sys.exc_info())
Exemplo n.º 40
0
def getDatabaseInfoList():
    printDebugMessage('getDatabaseInfoList', 'Begin', 11)
    requestUrl = baseUrl + '/dbfetch.databases?style=xml'
    xmlDoc = restRequest(requestUrl)
    printDebugMessage('getDatabaseInfoList', 'xmlDoc: ' + xmlDoc, 21)
    doc = xmltramp.parse(xmlDoc)
    databaseInfoList = doc['databaseInfo':]
    printDebugMessage('getDatabaseInfoList', 'End', 11)
    return databaseInfoList
Exemplo n.º 41
0
def main():
    tmpl = 'http://api.technorati.com/search?key=%s&limit=5&query=%s'
    key = open("technorati-key.txt", "r").read().strip()
    query = (len(sys.argv) > 1) and sys.argv[1] or 'test query'
    url = tmpl % (key, urllib.quote_plus(query))
    data = HTTPCache(url).content()

    # HACK: I get occasional encoding issues with Technorati, so
    # here's an ugly hack that seems to make things work anyway.
    try:
        doc = xmltramp.parse(data)
    except SAXParseException:
        data = data.decode('utf8', 'ignore').encode('utf8')
        doc = xmltramp.parse(data)

    items = [x for x in doc.document if x._name == 'item']
    for i in items:
        print '"%(title)s"\n\t%(permalink)s' % i
Exemplo n.º 42
0
 def test_ack_jobxml(self):
     _response_type = u'ack'
     with session.begin():
         rs = self.job.recipesets[0]
         rs.nacked = RecipeSetResponse(type=_response_type)
     jobxml = XmlJob(xmltramp.parse(self.job.to_xml(clone=False).toprettyxml()))
     for xmlrecipeSet in jobxml.iter_recipeSets():
         response = xmlrecipeSet.get_xml_attr('response',unicode,None)
         self.assertEqual(response,_response_type)
Exemplo n.º 43
0
def serviceGetParameterDetails(paramName):
    printDebugMessage('serviceGetParameterDetails', 'Begin', 1)
    printDebugMessage('serviceGetParameterDetails', 'paramName: ' + paramName, 2)
    requestUrl = baseUrl + '/parameterdetails/' + paramName
    printDebugMessage('serviceGetParameterDetails', 'requestUrl: ' + requestUrl, 2)
    xmlDoc = restRequest(requestUrl)
    doc = xmltramp.parse(xmlDoc)
    printDebugMessage('serviceGetParameterDetails', 'End', 1)
    return doc
Exemplo n.º 44
0
def serviceGetResultTypes(jobId):
    printDebugMessage('serviceGetResultTypes', 'Begin', 1)
    printDebugMessage('serviceGetResultTypes', 'jobId: ' + jobId, 2)
    requestUrl = baseUrl + '/resulttypes/' + jobId
    printDebugMessage('serviceGetResultTypes', 'requestUrl: ' + requestUrl, 2)
    xmlDoc = restRequest(requestUrl)
    doc = xmltramp.parse(xmlDoc)
    printDebugMessage('serviceGetResultTypes', 'End', 1)
    return doc['type':]
Exemplo n.º 45
0
    def post(self, conn=None, alwaysReturnList=False):
        headers = {
            "User-Agent": "Pyforce/{0}".format(__version__),
            "SOAPAction": '""',
            "Content-Type": "text/xml; charset=utf-8"
        }
        if gzipResponse:
            headers['accept-encoding'] = 'gzip'
        if gzipRequest:
            headers['content-encoding'] = 'gzip'
        close = False
        (scheme, host, path, params, query, frag) = urlparse(self.serverUrl)
        max_attempts = 3
        response = None
        attempt = 1
        while not response and attempt <= max_attempts:
            try:
                if conn is None:
                    conn = makeConnection(scheme, host)
                    close = True
                conn.request("POST", path, self.makeEnvelope(), headers)
                response = conn.getresponse()
                rawResponse = response.read()
            except (httplib.HTTPException, socket.error):
                if conn is not None:
                    conn.close()
                    conn = None
                    response = None
                attempt += 1
        if not response:
            raise RuntimeError('No response from Salesforce')

        if response.getheader('content-encoding', '') == 'gzip':
            rawResponse = gzip.GzipFile(fileobj=StringIO(rawResponse)).read()
        if close:
            conn.close()
        tramp = xmltramp.parse(rawResponse)
        try:
            faultString = str(tramp[_tSoapNS.Body][_tSoapNS.Fault].faultstring)
            faultCode = str(
                tramp[_tSoapNS.Body][_tSoapNS.Fault].faultcode).split(':')[-1]
            if faultCode == 'INVALID_SESSION_ID':
                raise SessionTimeoutError(faultCode, faultString)
            else:
                raise SoapFaultError(faultCode, faultString)
        except KeyError:
            pass
        # first child of body is XXXXResponse
        result = tramp[_tSoapNS.Body][0]
        # it contains either a single child, or for a batch call multiple
        # children
        if alwaysReturnList or len(result) > 1:
            return result[:]
        elif len(result) == 1:
            return result[0]
        else:
            return result
Exemplo n.º 46
0
def serviceGetResultTypes(jobId):
    printDebugMessage('serviceGetResultTypes', 'Begin', 1)
    printDebugMessage('serviceGetResultTypes', 'jobId: ' + jobId, 2)
    requestUrl = baseUrl + '/resulttypes/' + jobId
    printDebugMessage('serviceGetResultTypes', 'requestUrl: ' + requestUrl, 2)
    xmlDoc = restRequest(requestUrl)
    doc = xmltramp.parse(xmlDoc)
    printDebugMessage('serviceGetResultTypes', 'End', 1)
    return doc['type':]
Exemplo n.º 47
0
def serviceGetParameterDetails(paramName):
    printDebugMessage('serviceGetParameterDetails', 'Begin', 1)
    printDebugMessage('serviceGetParameterDetails', 'paramName: ' + paramName, 2)
    requestUrl = baseUrl + '/parameterdetails/' + paramName
    printDebugMessage('serviceGetParameterDetails', 'requestUrl: ' + requestUrl, 2)
    xmlDoc = restRequest(requestUrl)
    doc = xmltramp.parse(xmlDoc)
    printDebugMessage('serviceGetParameterDetails', 'End', 1)
    return doc
Exemplo n.º 48
0
 def test_job_xml_can_be_roundtripped(self):
     # Ideally the logic for parsing job XML into a Job instance would live in model code,
     # so that this test doesn't have to go through the web layer...
     complete_job_xml = pkg_resources.resource_string('bkr.inttest', 'complete-job.xml')
     xmljob = XmlJob(xmltramp.parse(complete_job_xml))
     with session.begin():
         job = testutil.call(self.controller.process_xmljob, xmljob, self.user)
     roundtripped_xml = job.to_xml(clone=True).toprettyxml(indent='    ')
     self.assertMultiLineEqual(roundtripped_xml, complete_job_xml)
Exemplo n.º 49
0
    def uploadImage(self, image):
        """ uploadImage
        """

        success = False
        photoid = ""
        if (not self.uploaded.has_key(image)):
            if (not args.silent): print("Uploading " + image + "...")
            try:
                photo = ('photo', image, open(image, 'rb').read())
                #if args.title: # Replace
                #    FLICKR["title"] = args.title
                #if args.description: # Replace
                #    FLICKR["description"] = args.description
                #if args.tags: # Append
                #    FLICKR["tags"] += " " + args.tags + " "
                d = {
                    api.token: str(self.token),
                    api.perms: str(self.perms),
                    "title": str(FLICKR["title"]),
                    "description": str(FLICKR["description"]),
                    "tags": str(FLICKR["tags"]),
                    "is_public": str(FLICKR["is_public"]),
                    "is_friend": str(FLICKR["is_friend"]),
                    "is_family": str(FLICKR["is_family"])
                }
                sig = self.signCall(d)
                d[api.sig] = sig
                d[api.key] = FLICKR[api.key]
                url = self.build_request(api.upload, d, (photo, ))
                xml = urllib2.urlopen(url).read()
                res = xmltramp.parse(xml)
                if (self.isGood(res)):
                    if (not args.silent): print("Success.")
                    photoid = res.photoid
                    self.logUpload(res.photoid, image)
                    if (args.log):
                        self.log.write(str(photoid) + "," + str(image) + "\n")
                        self.log.flush()
                    success = True
                else:
                    print("Problem:")
                    self.reportError(res)
                    if (args.log):
                        self.log.write("error," + str(image) + "\n")
                        self.log.flush()
            except urllib2.HTTPError as e:
                print e.code
                print e.read()
            except:
                print(str(sys.exc_info()))
                if (args.log):
                    self.log.write("error," + str(image) + "," +
                                   str(sys.exc_info()) + "\n")
                    self.log.flush()
        return success, photoid
Exemplo n.º 50
0
 def test_ack_jobxml(self):
     _response_type = u'ack'
     with session.begin():
         rs = self.job.recipesets[0]
         rs.nacked = RecipeSetResponse(type=_response_type)
     jobxml = XmlJob(
         xmltramp.parse(self.job.to_xml(clone=False).toprettyxml()))
     for xmlrecipeSet in jobxml.iter_recipeSets():
         response = xmlrecipeSet.get_xml_attr('response', unicode, None)
         self.assertEqual(response, _response_type)
Exemplo n.º 51
0
	def parse(cls, xml, **kwargs):
		"""
		Default parser. Just parse the string, if not already done,
		and use it to build a class instance.
		"""
		import xmltramp
		if not isinstance(xml, xmltramp.Element):
			xml = xmltramp.parse(xml)
		kwargs['xml'] = xml
		return general_parser(cls, xml._name, 'xmltag', **kwargs)
Exemplo n.º 52
0
    def parse(cls, xml, **kwargs):
        """
		Default parser. Just parse the string, if not already done,
		and use it to build a class instance.
		"""
        import xmltramp
        if not isinstance(xml, xmltramp.Element):
            xml = xmltramp.parse(xml)
        kwargs['xml'] = xml
        return general_parser(cls, xml._name, 'xmltag', **kwargs)
Exemplo n.º 53
0
 def test_uploading_job_without_recipeset_raises_exception(self):
     xmljob = XmlJob(
         xmltramp.parse('''
         <job>
             <whiteboard>job with norecipesets</whiteboard>
         </job>
         '''))
     with session.begin():
         self.assertRaises(
             BX, lambda: self.controller.process_xmljob(xmljob, self.user))
Exemplo n.º 54
0
	def __init__(self, xml, engine=None):
		from xmltramp import Element, parse
		if not isinstance(xml, Element):
			xml = parse(xml)
		self.xml = xml
		self.engine = engine
		self.type = ResultType.parse(str(xml.Esito))
		self.timestamp = ParseDateTime(str(xml.Timestamp))
		self.check_mac()
		for el in xml.Dati:
			self.add_data(el)
Exemplo n.º 55
0
    def send(self):
        """
		Send the request over HTTP to the designated server, and return
		a parsed XML tree of the answer.
		"""
        self._validate_params()
        from urllib import urlopen
        qs = self._qs()
        call = urlopen(self.engine.env_api + '?' + qs)
        from xmltramp import parse
        return parse(call.read())
Exemplo n.º 56
0
 def getUploadedPhotos(self):
     self.uploaded_images_file = os.path.join(self.image_dir,
                                              UPLOADED_IMAGES_FILENAME)
     self.uploaded_images = shelve.open(self.uploaded_images_file)
     if len(self.uploaded_images) > 0:
         print(
             "\n---------- Loading list of already uploaded photos from file "
             + self.uploaded_images_file + " ----------\n")
     else:
         print(
             "\n---------- Getting list of already uploaded photos from Flickr account ----------\n"
         )
         page = 1
         while True:
             try:
                 d = {
                     api.token: str(self.token),
                     api.perms: str(self.perms),
                     "method": "flickr.people.getPhotos",
                     "user_id": "me",
                     "extras": "description",
                     "per_page": str(FLICKR["max_photos_per_page"]),
                     "page": str(page)
                 }
                 page = page + 1
                 sig = self.signCall(d)
                 d[api.sig] = sig
                 d[api.key] = FLICKR[api.key]
                 url = self.build_request(api.rest, d, ())
                 xml = urllib2.urlopen(url).read()
                 res = xmltramp.parse(xml)
                 if (self.isGood(res)):
                     if len(res[0]) == 0:
                         break
                     for image in res[0]:
                         image_name = image('title')
                         image_path = image[0][0]
                         image_id = image('id')
                         print '    Existing image ' + image_path + ' (id = ' + str(
                             image_id) + ')'
                         self.uploaded_images[str2key(
                             image_path)] = image_id
                 else:
                     print("    Problem:")
                     self.reportError(res)
                     raise
             except:
                 print(str(sys.exc_info()))
                 # cleanup
                 self.uploaded_images.close()
                 os.remove(self.uploaded_images_file + ".db")
                 sys.exit()
     print '\nLoaded ' + str(len(
         self.uploaded_images)) + ' uploaded images!\n\n'
Exemplo n.º 57
0
 def test_job_xml_can_be_roundtripped(self):
     # Ideally the logic for parsing job XML into a Job instance would live in model code,
     # so that this test doesn't have to go through the web layer...
     complete_job_xml = pkg_resources.resource_string(
         'bkr.inttest', 'complete-job.xml')
     xmljob = XmlJob(xmltramp.parse(complete_job_xml))
     with session.begin():
         job = testutil.call(self.controller.process_xmljob, xmljob,
                             self.user)
     roundtripped_xml = job.to_xml(clone=True).toprettyxml(indent='    ')
     self.assertMultiLineEqual(roundtripped_xml, complete_job_xml)
Exemplo n.º 58
0
    def getCreatedSets(self):
        self.created_sets_file = os.path.join(self.image_dir,
                                              CREATED_SETS_FILENAME)
        self.created_sets = shelve.open(self.created_sets_file)
        if len(self.created_sets) > 0:
            print(
                "\n---------- Loading list of already created sets from file "
                + self.created_sets_file + " ----------\n")
        else:
            print(
                "\n---------- Getting list of already created sets from Flick account ----------\n"
            )
            page = 1
            while True:
                try:
                    d = {
                        api.token: str(self.token),
                        api.perms: str(self.perms),
                        "method": "flickr.photosets.getList",
                        "per_page": str(FLICKR["max_sets_per_page"]),
                        "page": str(page)
                    }
                    page = page + 1
                    sig = self.signCall(d)
                    d[api.sig] = sig
                    d[api.key] = FLICKR[api.key]
                    url = self.build_request(api.rest, d, ())
                    xml = urllib2.urlopen(url).read()
                    res = xmltramp.parse(xml)
                    if (self.isGood(res)):
                        if len(res[0]) == 0:
                            break
                        for nextset in res[0]:
                            set_name = nextset[0][0]
                            set_path = nextset[1][0]
                            set_id = nextset('id')
                            print '    Existing set ' + set_path + ' (id = ' + str(
                                set_id) + ')'
                            self.created_sets[str2key(set_path)] = set_id
                        success = True
                    else:
                        print("    Problem:")
                        self.reportError(res)
                        raise
                except:
                    print(str(sys.exc_info()))
                    # cleanup
                    self.created_sets.close()
                    os.remove(self.created_sets_file + ".db")
                    sys.exit()

        print '\nLoaded ' + str(len(self.created_sets)) + ' sets!\n\n'
Exemplo n.º 59
0
 def test_ack_jobxml_clone(self):
     """
     Unline test_ack_jobxml, we do _not_ want to see our response in here
     """
     _response_type = u'ack'
     with session.begin():
         rs = self.job.recipesets[0]
         rs.nacked = RecipeSetResponse(type=_response_type)
     jobxml = XmlJob(
         xmltramp.parse(self.job.to_xml(clone=True).toprettyxml()))
     for xmlrecipeSet in jobxml.iter_recipeSets():
         response = xmlrecipeSet.get_xml_attr('response', unicode, None)
         self.assertEqual(response, None)