def view_file(self, f, metadata): compressed = ['x-bzip2', 'x-bzip', 'x-gzip', 'gzip', 'x-tar', 'x-xz'] mime_type, mime_subtype = f.mimetype.split('/') if sys.stdout.isatty() and not (mime_type == 'text' or mime_subtype in compressed): self.log(' ! Warning: The attachment "{}" has type "{}"'.format(f.filename, f.mimetype)) if not confirm('Are you sure you want to view it?'): return self.log('Viewing file: {}'.format(f.filename)) if mime_subtype == 'x-tar': tar_file = tarfile.open(fileobj=StringIO.StringIO(f.read())) if metadata: # show listing of tarfile elements tar_file.list() else: for tarinfo_file in tar_file.getmembers(): try: temp = tar_file.extractfile(tarinfo_file) except KeyError: # symlink points to a nonexistent file return if temp is not None: prefix = '=== {} '.format(temp.name) print(prefix + '=' * (self.columns - len(prefix))) sys.stdout.write(TarAttachment(tarfile=tar_file, cfile=tarinfo_file).data()) else: sys.stdout.write(f.read())
def remove_auth_token(self): if confirm(prompt='Remove auth token?', default=True): os.remove(self.authfile) else: # currently only keep one backup and which is overwritten if it already exists self.log('Moving old auth token to "{}"'.format(self.authfile + '.old')) os.rename(self.authfile, self.authfile + '.old') self.service.auth_token = None
def save_file(self, f): if os.path.exists(f.filename): print(' ! Warning: The file "{}" already exists'.format(f.filename)) if not confirm('Do you want to overwrite it?'): return self.log('Saving file: {}'.format(f.filename)) try: with open(f.filename, 'w+') as save_file: os.chmod(f.filename, stat.S_IREAD | stat.S_IWRITE) save_file.write(f.read(raw=True)) except IOError as e: raise RuntimeError('Cannot create file "{}": {}'.format(f.filename, e.strerror))
def modify(self, ask, dry_run, ids, **kw): kw = self._modify_params(**kw) request = self.service.modify(ids, **kw) self.log(self._truncate('Modifying {}(s): {}'.format(self.service.item, ', '.join(map(str, ids))))) self.log(request.options, prefix='') if ask: if not confirm(prompt='Modify {}(s)?'.format(self.service.item), default=True): self.log('Modification aborted') return if dry_run: return data = request.send() self.print_changes(data, params=kw)
def save_alias(args, value): if args.connection is not None: section = args.connection else: section = 'default' exists = False try: alias_cmd = args.config[section]['alias'][args.alias] print(' ! Alias "{}" already exists. '.format(args.alias), end='') if confirm('Overwrite it?'): exists = True else: return except KeyError: pass set_config_option(args.config, section, option=option, value=value, exists=exists)
def create(self, ask, batch, dry_run, **kw): options_log, params = self._create_params(batch, **kw) for line in options_log: self.log(line, prefix='') if ask or not batch: if not confirm(prompt='Submit {}?'.format(self.service.item), default=True): self.log('Submission aborted') return if dry_run: return try: data = self.service.create(**params) except ValueError as e: raise RuntimeError(e) if sys.stdout.isatty(): self.log('Submitted {} {}'.format(self.service.item, data)) else: sys.stdout.write(str(data))