Skip to content

Commit d25bf59

Browse files
author
Luiko Czub
committed
_getAttachmentArgs() handle now file path #40
1 parent 282f7ab commit d25bf59

File tree

4 files changed

+58
-26
lines changed

4 files changed

+58
-26
lines changed

example/TestLinkExample.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@
164164
# else:
165165
# print "Error creating the project '%s': %s " % (NEWPROJECT,isOk)
166166
# sys.exit(-1)
167-
projInfo = 'Example created with Python API class %s (PY %s) in TL %s' % \
168-
( myApiVersion, python_version(), myTLVersion )
167+
projInfo = 'Example created with Python %s API class %s in TL %s' % \
168+
( python_version(), myApiVersion, myTLVersion )
169169
newProject = myTestLink.createTestProject(NEWPROJECT, NEWPREFIX,
170170
notes=projInfo, active=1, public=1,
171171
options={'requirementsEnabled' : 0, 'testPriorityEnabled' : 1,
@@ -630,9 +630,8 @@
630630
newAttachment = myTestLink.uploadTestProjectAttachment(a_file, newProjectID,
631631
title='PNG Example', description='PNG Attachment Example for a TestProject')
632632
print("uploadTestProjectAttachment", newAttachment)
633-
# add png file as Attachnent to test suite A
634-
a_file=open(NEWATTACHMENT_PNG, mode='rb')
635-
newAttachment = myTestLink.uploadTestSuiteAttachment(a_file, newTestSuiteID_A,
633+
# add png file as Attachnent to test suite A - uploadXxzAttachmemt also file path
634+
newAttachment = myTestLink.uploadTestSuiteAttachment(NEWATTACHMENT_PNG, newTestSuiteID_A,
636635
title='PNG Example', description='PNG Attachment Example for a TestSuite')
637636
print("uploadTestSuiteAttachment", newAttachment)
638637
# add png file as Attachment to test case B

example/TestLinkExampleGenericApi.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@
134134
print("")
135135

136136
# Creates the project
137-
projInfo = 'Example created with Python API class %s (PY %s) in TL %s' % \
138-
( myApiVersion, python_version(), myTLVersion )
137+
projInfo = 'Example created with Python %s API class %s in TL %s' % \
138+
( python_version(), myApiVersion, myTLVersion )
139139
newProject = myTestLink.createTestProject(NEWPROJECT, NEWPREFIX,
140140
notes=projInfo, active=1, public=1,
141141
options={'requirementsEnabled' : 1, 'testPriorityEnabled' : 1,
@@ -555,9 +555,8 @@
555555
newAttachment = myTestLink.uploadTestProjectAttachment(a_file, newProjectID,
556556
title='PNG Example', description='PNG Attachment Example for a TestProject')
557557
print("uploadTestProjectAttachment", newAttachment)
558-
# add png file as Attachnent to test suite A
559-
a_file=open(NEWATTACHMENT_PNG, mode='rb')
560-
newAttachment = myTestLink.uploadTestSuiteAttachment(a_file, newTestSuiteID_A,
558+
# add png file as Attachnent to test suite A - uploadXyzAttachmemt also file path
559+
newAttachment = myTestLink.uploadTestSuiteAttachment(NEWATTACHMENT_PNG, newTestSuiteID_A,
561560
title='PNG Example', description='PNG Attachment Example for a TestSuite')
562561
print("uploadTestSuiteAttachment", newAttachment)
563562
# add png file as Attachment to test case B

src/testlink/testlinkapigeneric.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@
1717
#
1818
# ------------------------------------------------------------------------
1919

20-
import sys
20+
import sys, os.path
2121
IS_PY3 = sys.version_info[0] < 3
2222
if IS_PY3:
2323
import xmlrpclib
24+
# in py 3 encodestring is deprecated and an alias for encodebytes
25+
# see issue #39 and compare py2 py3 doc
26+
# https://docs.python.org/2/library/base64.html#base64.encodestring
27+
# https://docs.python.org/3/library/base64.html#base64.encodebytes
28+
from base64 import encodestring as encodebytes
2429
else:
2530
import xmlrpc.client as xmlrpclib
31+
from base64 import encodebytes
32+
2633
from platform import python_version
34+
from mimetypes import guess_type
2735

2836
from . import testlinkerrors
2937
from .testlinkhelper import TestLinkHelper, VERSION
@@ -1235,16 +1243,42 @@ def _convertPostionalArgs(self, methodName, valueList):
12351243
def _getAttachmentArgs(self, attachmentfile):
12361244
""" returns dictionary with key/value pairs needed, to transfer
12371245
ATTACHMENTFILE via the api to into TL
1238-
ATTACHMENTFILE: python file descriptor pointing to the file """
1239-
import mimetypes
1240-
import base64
1241-
import os.path
1242-
return {'filename':os.path.basename(attachmentfile.name),
1243-
'filetype':mimetypes.guess_type(attachmentfile.name)[0],
1244-
'content':base64.encodestring(attachmentfile.read())
1246+
1247+
ATTACHMENTFILE could be:
1248+
a) a python file descriptor pointing to the file (class file)
1249+
b) a valid file path"""
1250+
1251+
a_file_obj = None
1252+
is_file_obj = isinstance(attachmentfile, file)
1253+
if not is_file_obj:
1254+
# handling a file path
1255+
a_file_path = attachmentfile
1256+
a_file_obj = self._openAttachmentForRead(a_file_path)
1257+
else:
1258+
# handling a file object
1259+
a_file_path = attachmentfile.name
1260+
a_file_obj = attachmentfile
1261+
1262+
return {'filename':os.path.basename(a_file_path),
1263+
'filetype':guess_type(a_file_path)[0],
1264+
'content':encodebytes(a_file_obj.read())
12451265
}
1246-
1266+
1267+
def _openAttachmentForRead(self, a_file_path):
1268+
""" opens the A_FILE_PATH for reading and returns the file descriptor.
1269+
Read mode will be set depending from py version and mimetype
1270+
PY2: text file = 'r', others = 'rb' PY3: general 'rb' """
1271+
a_read_mode = 'rb'
1272+
is_text_file = 'text/' in guess_type(a_file_path)
1273+
if not IS_PY3 and is_text_file:
1274+
# under py2 text file shpuld be open as 'r' and not 'rb'
1275+
# for details compare py2 and py docs
1276+
# https://docs.python.org/2/library/base64.html#base64.encodestring
1277+
# https://docs.python.org/3/library/base64.html#base64.encodebytes
1278+
a_read_mode = 'r'
1279+
return open(a_file_path, a_read_mode)
12471280

1281+
12481282
def _checkResponse(self, response, methodNameAPI, argsOptional):
12491283
""" Checks if RESPONSE is empty or includes Error Messages
12501284
Will raise TLRepsonseError in this case """

test/utest-offline/testlinkapigeneric_offline_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -689,13 +689,13 @@ def test__getAttachmentArgs_textfile(self):
689689
self.assertIn('text/', args['filetype'])
690690
self.assertIsNotNone(args['content'])
691691

692-
# def test__getAttachmentArgs_filepath(self):
693-
# "enhancement #40 handle file patch instead file object"
694-
# args = self.api._getAttachmentArgs(ATTACHMENT_EXAMPLE_TEXT)
695-
# self.assertEqual('testlinkapigeneric_offline_test.py', args['filename'])
696-
# # filetype is also OS depended, either 'text/plain' or 'text/x-python'
697-
# self.assertIn('text/', args['filetype'])
698-
# self.assertIsNotNone(args['content'])
692+
def test__getAttachmentArgs_filepath(self):
693+
"enhancement #40 handle file patch instead file object"
694+
args = self.api._getAttachmentArgs(ATTACHMENT_EXAMPLE_TEXT)
695+
self.assertEqual('testlinkapigeneric_offline_test.py', args['filename'])
696+
# filetype is also OS depended, either 'text/plain' or 'text/x-python'
697+
self.assertIn('text/', args['filetype'])
698+
self.assertIsNotNone(args['content'])
699699

700700
def test___str__pyversion(self):
701701
self.api.loadScenario(SCENARIO_TL199)

0 commit comments

Comments
 (0)