def attachment(self, attachid, view = False): """ Download or view an attachment given the id.""" self.log('Getting attachment %s' % attachid) result = Bugz.attachment(self, attachid) if not result: raise RuntimeError('Unable to get attachment') action = {True:'Viewing', False:'Saving'} self.log('%s attachment: "%s"' % (action[view], result['filename'])) safe_filename = os.path.basename(re.sub(r'\.\.', '', result['filename'])) if view: print result['fd'].read() else: if os.path.exists(result['filename']): raise RuntimeError('Filename already exists') open(safe_filename, 'wb').write(result['fd'].read())