Skip to content

Commit f95183f

Browse files
committed
Support python versions 2 and 3
1 parent 2be9b58 commit f95183f

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

getBitstreams.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import argparse
77
import os
88
import re
9+
from six.moves import input
910

1011

1112
def main():
@@ -67,7 +68,7 @@ def main():
6768

6869
args = parser.parse_args()
6970

70-
secretsVersion = raw_input('To edit production server, enter the name of the secrets file: ')
71+
secretsVersion = input('To edit production server, enter the name of the secrets file: ')
7172
if secretsVersion != '':
7273
try:
7374
secrets = __import__(secretsVersion)
@@ -101,29 +102,29 @@ def main():
101102
if args.handle:
102103
handle = args.handle
103104
else:
104-
handle = raw_input('Enter handle: ')
105+
handle = input('Enter handle: ')
105106

106107
if args.verbose:
107108
print('verbosity turned on')
108109

109110
if args.handle:
110-
print('retreiving object with handle {}').format(args.handle)
111+
print('retreiving object with handle {}'.format(args.handle))
111112

112113
if args.formats:
113-
print('filtering results to the following bitstream formats: {}').format(args.formats)
114+
print('filtering results to the following bitstream formats: {}'.format(args.formats))
114115
else:
115116
print('returning bitstreams of any format')
116117

117118
if args.bundles:
118-
print('filtering results to the following bundles: {}').format(args.bundles)
119+
print('filtering results to the following bundles: {}'.format(args.bundles))
119120
else:
120121
print('returning bitstreams from any bundle')
121122

122123
if args.download:
123124
print('downloading bitstreams')
124125

125126
if args.rtimeout:
126-
print('response_timeout set to {}').format(args.rtimeout)
127+
print('response_timeout set to {}'.format(args.rtimeout))
127128

128129
# end: argument parsing
129130

@@ -134,21 +135,21 @@ def main():
134135
header = {'content-type': 'application/json', 'accept': 'application/json'}
135136
session = requests.post(args.baseURL+'/rest/login', headers=header, verify=args.verify, params=data, timeout=args.rtimeout).cookies['JSESSIONID']
136137
cookies = {'JSESSIONID': session}
137-
print 'authenticated'
138+
print('authenticated')
138139

139140
# NOTE: expanding items (of collections) and bitstreams (of items) to get the count
140141
endpoint = args.baseURL+'/rest/handle/'+handle+'?expand=items,bitstreams'
141142
dsObject = requests.get(endpoint, headers=header, cookies=cookies, verify=args.verify, timeout=args.rtimeout)
142143
dsObject.raise_for_status() # ensure we notice bad responses
143144
dsObject = dsObject.json()
144-
if args.verbose: print dsObject
145+
if args.verbose: print(dsObject)
145146
dsObjectID = dsObject['uuid']
146147
# TODO: extend
147148
if dsObject['type'] == 'collection':
148-
if args.verbose: print dsObject['type']
149+
if args.verbose: print(dsObject['type'])
149150

150151
itemCount = len(dsObject['items'])
151-
print('{} items').format(itemCount)
152+
print('{} items'.format(itemCount))
152153
for collItem in dsObject['items']:
153154
endpoint = args.baseURL + collItem['link'] + '?expand=bitstreams'
154155
item = requests.get(endpoint, headers=header, cookies=cookies, verify=args.verify, timeout=args.rtimeout)
@@ -160,14 +161,14 @@ def main():
160161
processItem(dsObject, args)
161162

162163
else:
163-
print('object is of an invalid type for this script ({}). please enter the handle of an item or a collection.').format(dsObject['type'])
164+
print('object is of an invalid type for this script ({}). please enter the handle of an item or a collection.'.format(dsObject['type']))
164165

165166
logout = requests.post(args.baseURL+'/rest/logout', headers=header, cookies=cookies, verify=args.verify, timeout=args.rtimeout)
166167

167168
elapsedTime = time.time() - startTime
168169
m, s = divmod(elapsedTime, 60)
169170
h, m = divmod(m, 60)
170-
print('Total script run time: {:01.0f}:{:02.0f}:{:02.0f}').format(h, m, s)
171+
print('Total script run time: {:01.0f}:{:02.0f}:{:02.0f}'.format(h, m, s))
171172

172173

173174
def processItem(dsObject, args):
@@ -179,7 +180,7 @@ def processItem(dsObject, args):
179180
if not os.path.exists(itemPath):
180181
os.makedirs(itemPath)
181182

182-
f = csv.writer(open(itemPath + handleID + '_bitstreams.csv', 'wb'))
183+
f = csv.writer(open(itemPath + handleID + '_bitstreams.csv', 'w'))
183184
f.writerow(['sequenceId']+['name']+['format']+['bundleName'])
184185

185186
itemID = dsObject['uuid']
@@ -193,7 +194,7 @@ def processItem(dsObject, args):
193194
# don't retreive more bitstreams than we have left
194195
if limit > bitstreamCount:
195196
limit = bitstreamCount
196-
print('bitstreamCount: {0} offset: {1} limit: {2}').format(bitstreamCount, offset, limit)
197+
print('bitstreamCount: {0} offset: {1} limit: {2}'.format(bitstreamCount, offset, limit))
197198
bitstreams = requests.get(args.baseURL+'/rest/items/' + str(itemID) + '/bitstreams?limit=' + str(limit) + '&offset='+str(offset), headers=header, cookies=cookies, verify=args.verify, timeout=args.rtimeout)
198199
bitstreams.raise_for_status() # ensure we notice bad responses
199200
bitstreams = bitstreams.json()

0 commit comments

Comments
 (0)