Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 吡咯烷酮_Python_Web Services_Encode_Rally_Pyral - Fatal编程技术网

Python 吡咯烷酮

Python 吡咯烷酮,python,web-services,encode,rally,pyral,Python,Web Services,Encode,Rally,Pyral,我有一个相当简单的用例,但我不理解我收到的错误消息 我使用的是请求和pyral模块,pyral()实际上只是Rally的Restful api的包装器。我的目标是从Rally(CA产品)用户故事中获取一个文件(附件),并将其存储到本地文件系统 对于上下文,这里是我的环境设置(通过身份验证来集合和创建对象)。我显然已经删除了身份验证信息 from pyral import Rally, rallyWorkset options = [arg for arg in sys.argv[1:] if

我有一个相当简单的用例,但我不理解我收到的错误消息

我使用的是请求和pyral模块,pyral()实际上只是Rally的Restful api的包装器。我的目标是从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)
在这之后,我只为一个用户故事获得了一个响应对象(请参见针对US845的查询),我这样做只是为了简化问题

r = rally.get('UserStory', fetch = True, projectScopeDown=True, query = 'FormattedID = US845')
然后我使用内置迭代器从RallyRESTResponse对象获取用户故事

us = r.next()
从这里开始,我感觉应该能够轻松地使用getAttachment()方法,该方法接受工件(us)和文件名(附件的名称)。我可以使用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
返回如下错误:

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
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
rally.getAttachments(美国)
文件“C:\Miniconda3\lib\site packages\pyral\restapi.py”,第1721行,在getAttachments中
attachments=[self.getAttachment(工件,附件名称)用于附件名称中的附件名称]
文件“C:\Miniconda3\lib\site packages\pyral\restapi.py”,第1721行,在
attachments=[self.getAttachment(工件,附件名称)用于附件名称中的附件名称]
文件“C:\Miniconda3\lib\site packages\pyral\restapi.py”,第1700行,在getAttachment中
att.Content=base64.decodebytes(att_Content.Content)#可能进一步将txfm转换为Unicode?
文件“C:\Miniconda3\lib\base64.py”,第552行,以字节为单位
_输入类型检查
文件“C:\Miniconda3\lib\base64.py”,第520行,输入类型检查
从错误中引发类型错误(msg)
TypeError:应为类似对象的字节,而不是str
看来我根本上误解了这个方法需要的参数?以前有人能成功地做到这一点吗?值得一提的是,我在使用addAttachment()方法和类似于上述的工作流时没有任何问题。我尝试过用bytes()方法将文件名(字符串)转换为utf-8,但没有帮助

我也在pyral源代码中查看了这个示例,但在尝试执行该示例时,我收到了完全相同的错误


看起来像restapi.py脚本中的问题-base64库中没有decodebytes方法:

att.Content = base64.decodebytes(att_content.Content)
有关所有可用方法的说明,请参见: 因此,解决方法是在restapi.py中用
base64.b64decode
替换decodebytes。至少,它对我有用

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

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyral/restapi.py

由于getAttachments未按预期工作,我已使用以下代码获取所有附件。它将在当前目录中创建一个同名文件

import sys
import string
import base64
from pyral import rallyWorkset, Rally,RallyRESTResponse

rally = Rally(server, user=USER_NAME, password=PASSWORD, workspace=workspace, project=project)
criterion = 'FormattedID = US57844'
response = rally.get('HierarchicalRequirement',  query=criterion, order="FormattedID",pagesize=200, limit=400, projectScopeDown=True)

artifact = response.next()
context, augments = rally.contextHelper.identifyContext()
for att in artifact.Attachments:
    resp = rally._getResourceByOID(context, 'AttachmentContent', att.Content.oid, project=None)
    if resp.status_code not in [200, 201, 202]:
        break
    res = RallyRESTResponse(rally.session, context, "AttachmentContent.x", resp, "full", 1)
    if res.errors or res.resultCount != 1:
        print("breaking the for loop")
    att_content = res.next()
    cont = att_content.Content
    x = base64.b64decode(cont)
    output = open(att.Name, 'wb') 
    output.write(x)

我没想到问题出在模块本身,而不是我:)。感谢您的验证!我在GitHub上提交了一个问题。我也可以使用
base64.b64decode
,在Python 2.7和Python 3.6上使用它(比我必须检查Python版本的解决方案要好)。@sc305495仅供参考,我创建了一个Piral分叉,并更正了该错误()。@Jean FrancoisT。谢谢,帮了大忙!对于提取excel文档(或任何真正的文档)并将其存储在文件系统中,您有什么建议吗?我尝试过使用pickle.dump(.Content,pickle.HIGHEST_PROTOCOL),但这可能是错误的方法?没关系:)。一个简单的output=open('test.xlsx','wb)output.write(attachment_file.Content)实现了这个技巧。再次感谢你的帮助!
import sys
import string
import base64
from pyral import rallyWorkset, Rally,RallyRESTResponse

rally = Rally(server, user=USER_NAME, password=PASSWORD, workspace=workspace, project=project)
criterion = 'FormattedID = US57844'
response = rally.get('HierarchicalRequirement',  query=criterion, order="FormattedID",pagesize=200, limit=400, projectScopeDown=True)

artifact = response.next()
context, augments = rally.contextHelper.identifyContext()
for att in artifact.Attachments:
    resp = rally._getResourceByOID(context, 'AttachmentContent', att.Content.oid, project=None)
    if resp.status_code not in [200, 201, 202]:
        break
    res = RallyRESTResponse(rally.session, context, "AttachmentContent.x", resp, "full", 1)
    if res.errors or res.resultCount != 1:
        print("breaking the for loop")
    att_content = res.next()
    cont = att_content.Content
    x = base64.b64decode(cont)
    output = open(att.Name, 'wb') 
    output.write(x)