皮拉尔·格塔奇曼

2024-09-30 03:25:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个相当简单的用例,但我不明白我收到的错误消息。在

我使用的是请求和pyral模块,pyral(http://pyral.readthedocs.io/en/latest/interface.html#)实际上只是一个Rally的restfulapi的包装器。我的目标是从Rally(CA产品)用户故事中获取一个文件(附件),并将其存储到本地文件系统中。在

对于上下文,这里是我的环境设置(通过身份验证来聚集并创建一个对象)。我显然删除了身份验证信息。在

from pyral import Rally, rallyWorkset

options = [arg for arg in sys.argv[1:] if arg.startswith('--')]
args = [arg for arg in sys.argv[1:] if arg not in options]
server, user, password, apikey, workspace, project = rallyWorkset(options)
rally = Rally(server='rally1.rallydev.com', 
user='**********', password='***********', 
          apikey="**************",
          workspace='**************', project='**************',
          server_ping=False)

之后,我只为一个用户情景获得了一个response对象(请参阅US845的查询),我这样做只是为了简化问题。在

^{pr2}$

然后我使用内置迭代器从rallyresponse对象获取用户故事。在

us = r.next()

从这里看来,我应该能够轻松地使用接受工件(us)和文件名(附件名称)的getAttachment()方法。我可以使用getAttachmentNames(us)返回附件名称列表。当我尝试像

attachment_names = rally.getAttachmentNames(us) #get attachments for this UserStory
attachment_file = rally.getAttachment(us, attachment_names[0]) #Try to get the first attachment 

返回如下错误

Traceback (most recent call last):

File "<ipython-input-81-a4a342a59c5a>", line 1, in <module>
attachment_file = rally.getAttachment(us, attachment_names[0])

File "C:\Miniconda3\lib\site-packages\pyral\restapi.py", line 1700, in getAttachment
att.Content = base64.decodebytes(att_content.Content)  # maybe further txfm to Unicode ?

File "C:\Miniconda3\lib\base64.py", line 552, in decodebytes
_input_type_check(s)

File "C:\Miniconda3\lib\base64.py", line 520, in _input_type_check
raise TypeError(msg) from err

TypeError: expected bytes-like object, not str

如果我尝试使用

test_obj = rally.getAttachments(us)

返回如下错误:

Traceback (most recent call last):

File "<ipython-input-82-06a8cd525177>", line 1, in <module>
rally.getAttachments(us)

File "C:\Miniconda3\lib\site-packages\pyral\restapi.py", line 1721, in getAttachments
attachments = [self.getAttachment(artifact, attachment_name) for attachment_name in attachment_names]

File "C:\Miniconda3\lib\site-packages\pyral\restapi.py", line 1721, in <listcomp>
attachments = [self.getAttachment(artifact, attachment_name) for attachment_name in attachment_names]

File "C:\Miniconda3\lib\site-packages\pyral\restapi.py", line 1700, in getAttachment
att.Content = base64.decodebytes(att_content.Content)  # maybe further txfm to Unicode ?

File "C:\Miniconda3\lib\base64.py", line 552, in decodebytes
_input_type_check(s)

File "C:\Miniconda3\lib\base64.py", line 520, in _input_type_check
raise TypeError(msg) from err

TypeError: expected bytes-like object, not str

我好像根本上误解了这个方法需要的参数?以前有人能成功地做到这一点吗?值得一提的是,将addAttachment()方法与上面类似的工作流一起使用没有问题。我尝试过用bytes()方法将文件名(string)转换为utf-8,但是没有用。在

我也看过pyral源代码中的这个例子,但是当我试图执行它时,我收到了完全相同的错误。在

https://github.com/klehman-rally/pyral/blob/master/examples/get_attachments.py


Tags: inpyforinputattachmentliblinearg
1条回答
网友
1楼 · 发布于 2024-09-30 03:25:52

看来问题出在重新打印.py脚本-base64库中没有decodebytes方法:

att.Content = base64.decodebytes(att_content.Content)

所有可用的方法描述如下: RFC 3548: Base16, Base32, Base64 Data Encodings 所以,解决方法是在中用base64.b64decode替换decodebytes重新打印.py. 至少,这对我有用。在

例如,在Mac OS X上的位置:

^{pr2}$

相关问题 更多 >

    热门问题