DeleteResource from Google Docs with Python - python

I am trying to delete a spreadsheet in Google Docs with this function:
def f_DeleteResource(xls_name):
"""Delete a resource"""
client=Auth()
for e1 in client.GetResources().entry:
e2 = client.GetResource(e1)
if xls_name==e2.title.text:
client.DeleteResource(e2.resource_id.text,True)
And I obtain different errors when I change the first parameter of client.DeleteResource(p1,p2):
client.DeleteResource(e2.resource_id.text,True):
Traceback (most recent call last):
File "C:\xmp\D6GDocsDeleteUpload.py", line 164, in <module> main()
File "C:\xmp\D6GDocsDeleteUpload.py", line 157, in main f_DeleteResource(sys.argv[2])
File "C:\xmp\D6GDocsDeleteUpload.py", line 144, in f_DeleteResource client.DeleteResource(e2.resource_id.text,True)
File "C:\Python27\lib\site-packages\gdata\docs\client.py", line 540, in delete_resource uri = entry.GetEditLink().href
AttributeError: 'str' object has no attribute 'GetEditLink'
client.DeleteResource(e2,True):
Traceback (most recent call last):
File "C:\xmp\D6GDocsDeleteUpload.py", line 164, in <module> main()
File "C:\xmp\D6GDocsDeleteUpload.py", line 157, in main f_DeleteResource(sys.argv[2])
File "C:\xmp\D6GDocsDeleteUpload.py", line 144, in f_DeleteResource client.DeleteResource(e2,True)
File "C:\Python27\lib\site-packages\gdata\docs\client.py", line 543, in delete_resource return super(DocsClient, self).delete(uri, **kwargs)
File "C:\Python27\lib\site-packages\gdata\client.py", line 748, in delete **kwargs)
File "C:\Python27\lib\site-packages\gdata\docs\client.py", line 66, in request return super(DocsClient, self).request(method=method, uri=uri, **kwargs)
File "C:\Python27\lib\site-packages\gdata\client.py", line 319, in request RequestError)
gdata.client.RequestError: Server responded with: 403, <errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>matchHeaderRequired</code><location type='header'>If-Match|If-None-Match</location><internalReason>If-Match or If-None-Match header or entry etag attribute required</internalReason></error></errors>
Anyone can help me?

It seems to be a bug in Google API Python library. I checked gdata-2.0.16 and noticed that DeleteResource() function uses only URL of the resource (gdata/docs/client.py lines 540-543), but later checks for hasattr(entry_or_uri, 'etag') (gdata/client.py lines 737-741) and of course string value (uri) doesn't have etag attribute.
You may work around it using force keyword argument:
import gdata.docs.data
import gdata.docs.client
client = gdata.docs.client.DocsClient()
client.ClientLogin('xxxxxx#gmail.com', 'xxxxxx', 'XxX')
for doc in client.GetAllResources():
if doc.title.text == 'qpqpqpqpqpqp':
client.DeleteResource(doc, force=True)
break
If you want you may report an error to library maintainers (if it isn't already reported).

This issue has been fixed in this revision:
http://code.google.com/p/gdata-python-client/source/detail?r=f98fff494fb89fca12deede00c3567dd589e5f97
If you sync you client to the repository, you should be able to delete a resource without having to specify 'force=True'.

Related

Can we convert google-cloud-dialogflow api returned types to json?

I having trouble converting dialogflow types such as ListIntentsResponse, EntityType to json. I have researched a lot into this. Converting every entry one by one is a headache thats why I want a workaround.
I have tried using google.protobuf.json_format methods. But it doesnt works. says UNknown field : DESCRIPTOR
from google.protobuf.json_format import *
client = dialogflow.IntentsClient()
request = dialogflow.ListIntentsRequest(
parent=f'projects/{DIALOGFLOW_PROJECT_ID}/agent'
)
response = client.list_intents(request)
# print(response)
print(MessageToJson(response ,descriptor_pool=None))```
**Error==>>>**
Traceback (most recent call last):
File "c:\Users\1150-Bilal\Desktop\chatbot\intents.py", line 12, in <module>
intentlist()
File "c:\Users\1150-Bilal\Desktop\chatbot\intents.py", line 10, in intentlist
print(MessageToJson(response ,descriptor_pool=None))
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\google\protobuf\json_format.py", line 130, in MessageToJson
return printer.ToJsonString(message, indent, sort_keys, ensure_ascii)
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\google\protobuf\json_format.py", line 197, in ToJsonString
js = self._MessageToJsonObject(message)
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\google\protobuf\json_format.py", line 203, in _MessageToJsonObject
message_descriptor = message.DESCRIPTOR
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\google\cloud\dialogflow_v2\services\intents\pagers.py", line 74, in __getattr__
return getattr(self._response, name)
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\proto\message.py", line 747, in __getattr__
raise AttributeError(
AttributeError: Unknown field for ListIntentsResponse: DESCRIPTOR

Official Softlayer script to retrieve locations ids does not work

I used to use the following script for retrieving the different locations ids, to create an VSI order:
https://softlayer.github.io/python/list_packages/
Specifically:
def getAllLocations(self):
mask = "mask[id,locations[id,name]]"
result = self.client['SoftLayer_Location_Group_Pricing'].getAllObjects(mask=mask);
pp(result)
Unfortunately meanwhile it throws the following exception:
Traceback (most recent call last):
File "new.py", line 59, in <module>
main.getAllLocations()
File "new.py", line 52, in getAllLocations
result = self.client['SoftLayer_Location_Group_Pricing'].getAllObjects(mask=mask);
File "/usr/local/lib/python2.7/site-packages/SoftLayer/API.py", line 392, in call_handler
return self(name, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/SoftLayer/API.py", line 360, in call
return self.client.call(self.name, name, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/SoftLayer/API.py", line 263, in call
return self.transport(request)
File "/usr/local/lib/python2.7/site-packages/SoftLayer/transports.py", line 195, in __call__
raise _ex(ex.faultCode, ex.faultString)
SoftLayer.exceptions.SoftLayerAPIError: SoftLayerAPIError(SOAP-ENV:Server): Internal Error
Is there something that needs to be changed within the function?
Nope your code is fine the problem is that this is an issue with the method which is not working I am gonna report the issue or if you want it you can open an softlayer's ticket and report the issue yourself.
An issue with the getAllObjects method was fixed yesterday for this service. Please try the request again.

Type not found: '(schema, http://www.w3.org/2001/XMLSchema, )

I am using suds-client for soap wsdl services. When i try to setup the suds client for soap service, I get the Type not found error. I search everywhere. There are many unanswered questions with the same error. I am adding the links as Question1, Question2, Question3
Here is my code
from suds.client import Client
wsdlfile = 'http://track.tcs.com.pk/trackingaccount/track.asmx?WSDL'
track_client = Client(TCS_TRACK_URI)
On the last line, I got this error
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/adil/Code/mezino/RoyalTag/royal_tag_services/sms_service/tcs_api.py", line 24, in <module>
track_client = Client(TCS_TRACK_URI)
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/suds/client.py", line 112, in __init__
self.wsdl = reader.open(url)
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/suds/reader.py", line 152, in open
d = self.fn(url, self.options)
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/suds/wsdl.py", line 159, in __init__
self.build_schema()
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/suds/wsdl.py", line 220, in build_schema
self.schema = container.load(self.options)
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/suds/xsd/schema.py", line 95, in load
child.dereference()
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/suds/xsd/schema.py", line 323, in dereference
midx, deps = x.dependencies()
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/suds/xsd/sxbasic.py", line 422, in dependencies
raise TypeNotFound(self.ref)
TypeNotFound: Type not found: '(schema, http://www.w3.org/2001/XMLSchema, )'
Please help me find a solution ?
I have been searching for the answer and finally I found a solution that solved my problem.
We Just need to add the missing schema to the imports of suds. Below is the code
from suds.xsd.doctor import Import, ImportDoctor
imp=Import('http://www.w3.org/2001/XMLSchema',location='http://www.w3.org/2001/XMLSchema.xsd')
imp.filter.add('http://tempuri.org/')
track_client = Client(TCS_TRACK_URI, doctor=ImportDoctor(imp))

Web2py built-in authentication, after filling in the username, email, etc it generates the following ticket

I am new to web2py, when I came around to use the built-in authentication, after filling in the username, email, etc it generates the following ticket.
I am a clueless to what to do! Any help would be really welcomed!
I am running a mac osx 10.8. Postgres 9.3.
The ticket
Traceback (most recent call last):
File "/Users/JoaoBtte/Documents/python/Hello/gluon/restricted.py", line 217, in restricted
exec ccode in environment
File "/Users/JoaoBtte/Documents/python/Hello/applications/Cifra/controllers/default.py", line 90, in <module>
File "/Users/JoaoBtte/Documents/python/Hello/gluon/globals.py", line 378, in <lambda>
self._caller = lambda f: f()
File "/Users/JoaoBtte/Documents/python/Hello/applications/Cifra/controllers/default.py", line 88, in user
return dict(form=auth())
File "/Users/JoaoBtte/Documents/python/Hello/gluon/tools.py", line 1297, in __call__
return getattr(self, args[0])()
File "/Users/JoaoBtte/Documents/python/Hello/gluon/tools.py", line 2554, in register
self.login_user(user)
File "/Users/JoaoBtte/Documents/python/Hello/gluon/tools.py", line 1976, in login_user
user = Row(user)
File "/Users/JoaoBtte/Documents/python/Hello/gluon/dal.py", line 6986, in <lambda>
__init__ = lambda self,*args,**kwargs: self.__dict__.update(*args,**kwargs)
TypeError: 'NoneType' object is not iterable
Do you have the following line in one of your model files (usually in db.py) ?
from gluon.tools import Auth
auth = Auth(db)
Could you show us your default.py controller?

Python 2.7.1 Blogger API problem

I recently started writing a simple client using the Blogger API to do some basic posting I implemented the client in Python and used the example code verbatim from the Blogger Developer's Guide to login, get the blog id, and make a new post. I ran the script and everything went fine until I got to this line:
return blogger_service.Post(entry, '/feeds/%s/posts/default' % blog_id)
I got the error message:
Traceback (most recent call last):
File "cs1121post.py", line 38, in <module>
cs1121post()
File "cs1121post.py", line 33, in cs1121post
return blogger_service.Post(entry, '/feeds/%s/posts/default' % blog_id)
File "/usr/local/lib/python2.7/dist-packages/gdata/service.py", line 1236, in Post
media_source=media_source, converter=converter)
File "/usr/local/lib/python2.7/dist-packages/gdata/service.py", line 1322, in PostOrPut
headers=extra_headers, url_params=url_params)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 93, in optional_warn_function
return f(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/atom/service.py", line 176, in request
content_length = CalculateDataLength(data)
File "/usr/local/lib/python2.7/dist-packages/atom/service.py", line 736, in CalculateDataLength
return len(str(data))
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 377, in __str__
return self.ToString()
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 374, in ToString
return ElementTree.tostring(self._ToElementTree(), encoding=string_encoding)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 369, in _ToElementTree
self._AddMembersToElementTree(new_tree)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 331, in _AddMembersToElementTree
member._BecomeChildElement(tree)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 357, in _BecomeChildElement
self._AddMembersToElementTree(new_child)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 342, in _AddMembersToElementTree
ExtensionContainer._AddMembersToElementTree(self, tree)
File "/usr/local/lib/python2.7/dist-packages/atom/__init__.py", line 224, in _AddMembersToElementTree
tree.text = self.text.decode(MEMBER_STRING_ENCODING)
AttributeError: 'list' object has no attribute 'decode'
By which I'm taking it that ElementTree is at fault here. I installed ElementTree via
sudo python setup.py install
in case it matters. Is there some known incompatibility between ElementTree and Python v2.7.1? Has this happened to anybody else and how did you get it working? If you need any additional information, please reply to the thread. All the source code that is relevant is basically just the example code from the Developers Guide mentioned above. I haven't modified that at all (not even the variable names). Any input is greatly appreciated.
The stacktrace is actually pretty clear about this: You're calling decode() on a list instead of a tree element. Try getting the first element from the list and calling decode() on that:
firsttext = self.text[0].decode(MEMBER_STRING_ENCODING)

Categories

Resources