def publish_tweet(self, vals, afterCreation): try: image = self.image if afterCreation else vals['image'] description = self.description if afterCreation else vals['description'] file = BytesIO(base64.b64decode(image)) file.mode = 'rb+' file.name = 'tmp.png' file.seek(0) twitterApi = twitter.Api( consumer_key=self.env['ir.config_parameter'].sudo().get_param( 'twitter_consumer_key'), consumer_secret=self.env['ir.config_parameter'].sudo().get_param( 'twitter_consumer_secret'), access_token_key=self.env['ir.config_parameter'].sudo().get_param( 'twitter_access_token_key'), access_token_secret=self.env['ir.config_parameter'].sudo( ).get_param('twitter_access_token_secret')) twitterResponse = twitterApi.PostUpdate(description, file) vals['twitter_id'] = twitterResponse.id vals['twitter_post_url'] = twitterResponse.media[0].expanded_url vals['active'] = True except twitter.TwitterError as e: raise exceptions.ValidationError(e)
def get_data_in_batches( self, bucket_name, prefix=None, data_after=None, data_until=None, batch_size=10000, ): rows = [] for blob in self.generate_blob_list(bucket_name, prefix, data_after, data_until): # download file content as bytes, read via avro blob_meta = { "blob_name": blob.name, "blob_modified_at": blob.updated, } bytes_data = blob.download_as_string() bytes_object = BytesIO(bytes_data) bytes_object.mode = "rb+" # need to "fake" the mode attribute because # avro checks the mode of the file given for some reason, fails otherwise reader = DataFileReader(bytes_object, DatumReader()) for row in reader: # add blob-level metadata row.update(blob_meta) rows.append(row) if len(rows) >= batch_size: yield rows rows = [] if rows: # return any data that was left after the last iteration yield rows
def test_read_StringIO(self): if is_bad_file(self.filepathCas): raise SkipTest f = open(self.filepathCas, 'rb') file = BytesIO(f.read()) file.mode = 'rb' f.close() self._read_tests(file)
def test_read_StringIO(self): if is_bad_file(self.filepathSim): raise SkipTest f = open(self.filepathSim, 'rb') file = BytesIO(f.read()) file.mode = 'rb' f.close() self._read_tests(file, self.version_2_45)
def open_on_swift(name, mode="rb"): headers, content = swiftclient.get_object( PRE_AUTH_URL, PRE_AUTH_TOKEN, CONTAINER_NAME, name, http_conn=connect_swift() ) buf = BytesIO(content) buf.name = basename(name) buf.mode = mode return File(buf)
def test_read_StringIO(self): if is_bad_file(self.filepathSim): raise SkipTest f = open(self.filepathSim, 'rb') buf = BytesIO(f.read()) buf.mode = 'rb' f.close() self._read_tests(buf)
def _open(self, name, mode='rb'): original_name = name name = self.name_prefix + name headers, content = self.swift_conn.get_object(self.container_name, name) buf = BytesIO(content) buf.name = os.path.basename(original_name) buf.mode = mode return File(buf)
def _open(self, name, mode='rb'): if self.name_prefix: name = self.name_prefix + name headers, content = swiftclient.get_object(self.storage_url, self.token, self.container_name, name, http_conn=self.http_conn) buf = BytesIO(content) buf.name = os.path.basename(name) buf.mode = mode return File(buf)
def intercept_stdin(content, is_bytes=False): if not is_bytes: content = content.encode() i = BytesIO() object.__setattr__(i, 'name', '<stdin>') i.write(content) if not (is_bytes or content.endswith(b'\n')): i.write(b'\n') i.seek(0) i = TextIOWrapper(cast(BufferedIOBase, i)) i.mode = 'r' with patch('sys.stdin', i): yield
def _open(self, name, mode='rb'): """ Retrieves the specified file from storage. """ try: fobj = FileUpload.objects.get(name=name) fobj.save() except FileUpload.DoesNotExist: return None inMemFile = BytesIO(fobj.data) inMemFile.name = fobj.name inMemFile.mode = mode retFile = File(inMemFile) return retFile
def uploadVideo(envName, movieUrl, text): text = text ioimg = requests.get(movieUrl) upimg = BytesIO(ioimg.content) upimg.mode = 'rb' upimg.name = 'mobie.mp4' api2 = auth_api2(envName) mediaId = api2.UploadMediaChunked(media=upimg, media_category="tweet_video") print(f'mediaId is {mediaId}') time.sleep(10) # https://github.com/bear/python-twitter/issues/654 if mediaId != "" and text != "": postTweet = api2.PostUpdate(status=text, media=mediaId) pprint(postTweet)
def sideEffect(self, filename, *args, **kwargs): if self.count <= 1: self.test.assertEqual('filename.fasta.bgz', filename) self.count += 1 writerIO = BytesIO() writer = bgzf.BgzfWriter(fileobj=writerIO) writer.write(b'>id0\nAC\n') writer.flush() fileobj = BytesIO(writerIO.getvalue()) fileobj.mode = 'rb' return bgzf.BgzfReader(fileobj=fileobj) else: self.test.fail( 'Open called too many times. Filename: %r, Args: %r, ' 'Keyword args: %r.' % (filename, args, kwargs))
def test_read_StringIO(self): # sim if is_bad_file(self.filepathSim): raise SkipTest f = open(self.filepathSim, 'rb') buf = BytesIO(f.read()) buf.mode = 'rb' f.close() file = File.File() file.readFromFileObject(buf) self.assertEqual(0, file._numberSimulations) # cas f = open(self.filepathCas, 'rb') buf = BytesIO(f.read()) buf.mode = 'rb' f.close() file = File.File() file.readFromFileObject(buf) self.assertEqual(1, file._numberSimulations) self.assertEqual(1, len(file._resultSimulationDataList))
def test_xopen_invalid(self): # invalid mode with self.assertRaises(ValueError): xopen('foo', 'z') with self.assertRaises(ValueError): xopen('foo', 'rz') with self.assertRaises(ValueError): xopen('foo', 'rU', newline='\n') with self.assertRaises(ValueError): xopen(STDOUT, 'w', compression=True) with self.assertRaises(ValueError): xopen('foo.bar', 'w', compression=True) with self.assertRaises(ValueError): xopen('foo', file_type=FileType.STDIO) with self.assertRaises(ValueError): xopen(STDOUT, file_type=FileType.LOCAL) with self.assertRaises(ValueError): xopen('foo', file_type=FileType.URL) with self.assertRaises(IOError): xopen('http://foo.com', file_type=FileType.LOCAL) with self.assertRaises(ValueError): xopen('xyz', file_type=FileType.FILELIKE) path = self.root.make_file(contents='foo') with open(path, 'r') as fh: with self.assertRaises(ValueError): xopen(fh, 'w') f = xopen(fh, context_wrapper=True) self.assertEquals('r', f.mode) f = xopen(path, context_wrapper=True) f.close() with self.assertRaises(IOError): with f: pass with self.assertRaises(ValueError): with open(path, 'rt') as fh: xopen(fh, 'rt', compression=True) # can't guess compression without a name with self.assertRaises(ValueError): b = BytesIO() b.mode = 'wb' xopen(b, 'wt') # can't read from stderr with self.assertRaises(ValueError): xopen(STDERR, 'rt')
def _open(self, name, mode="rb"): """Open file with filename `name` from the database.""" name = self.get_instance_name(name) try: # Load file from database. f = models.File.objects.get_from_name(name) content = f.content size = f.size if _settings.DB_FILES_AUTO_EXPORT_DB_TO_FS and not utils.is_fresh( f.name, f.content_hash): # Automatically write the file to the filesystem # if it's missing and exists in the database. # This happens if we're using multiple web servers connected # to a common database behind a load balancer. # One user might upload a file from one web server, and then # another might access if from another server. utils.write_file(name, f.content) except models.File.DoesNotExist: # If not yet in the database, check the local file system # and load it into the database if present. fqfn = self.path(name) if os.path.isfile(fqfn): # print('Loading file into database.') self._save(name, open(fqfn, mode)) fh = super(DatabaseStorage, self)._open(name, mode) content = fh.read() size = fh.size else: # Otherwise we don't know where the file is so we return an # empty file size = 0 content = b"" # Normalize the content to a new file object. fh = BytesIO(content) fh.name = name fh.mode = mode fh.size = size o = files.File(fh) return o
def scaleImage(image, width=None, height=None, mode="contain", quality=88, result=None, direction=None): """Scale the given image data to another size and return the result as a string or optionally write in to the file-like `result` object. The `image` parameter can either be the raw image data (ie a `str` instance) or an open file. The `quality` parameter can be used to set the quality of the resulting image scales. The return value is a tuple with the new image, the image format and a size-tuple. Optionally a file-like object can be given as the `result` parameter, in which the generated image scale will be stored. The `width`, `height`, `mode` parameters will be passed to :meth:`scalePILImage`, which performs the actual scaling. The generated image is a JPEG image, unless the original is a PNG or GIF image. This is needed to make sure alpha channel information is not lost, which JPEG does not support. """ if isinstance(image, (bytes, str)): image = BytesIO(image) image = PIL.Image.open(image) # When we create a new image during scaling we loose the format # information, so remember it here. format_ = image.format if format_ not in ("PNG", "GIF"): # Always generate JPEG, except if format is PNG or GIF. format_ = "JPEG" elif format_ == "GIF": # GIF scaled looks better if we have 8-bit alpha and no palette format_ = "PNG" icc_profile = image.info.get("icc_profile") image = scalePILImage(image, width, height, mode, direction=direction) # convert to simpler mode if possible colors = image.getcolors(maxcolors=256) if image.mode not in ("P", "L") and colors: if format_ == "JPEG": # check if it's all grey if all(rgb[0] == rgb[1] == rgb[2] for c, rgb in colors): image = image.convert("L") elif format_ == "PNG": image = image.convert("P") if image.mode == "RGBA" and format_ == "JPEG": extrema = dict(zip(image.getbands(), image.getextrema())) if extrema.get("A") == (255, 255): # no alpha used, just change the mode, which causes the alpha band # to be dropped on save image.mode = "RGB" else: # switch to PNG, which supports alpha format_ = "PNG" new_result = False if result is None: result = BytesIO() new_result = True image.save(result, format_, quality=quality, optimize=True, progressive=True, icc_profile=icc_profile) if new_result: result = result.getvalue() else: result.seek(0) return result, format_, image.size