def save(self, *args, **kwargs): if self.image: #open image pil_image_obj = Image.open(self.image) img_size = pil_image_obj.size if float(img_size[0]) > 500: new_image = resizeimage.resize_width(pil_image_obj, 500) new_image_io = BytesIO() new_image.save(new_image_io, format='JPEG') temp_name = self.image.name self.image.delete(save=False) self.image.save(temp_name, content=ContentFile(new_image_io.getvalue()), save=False) if self.image_slurp: print("image slurp") imgRequest = request.urlopen(self.image_slurp) if imgRequest.status == 200: file_name = self.image_slurp.split('/')[-1] img_temp = NamedTemporaryFile() img_temp.write(imgRequest.read()) img_temp.flush() img_file = File(img_temp) pil_image_obj = Image.open(img_temp) img_size = pil_image_obj.size if float(img_size[0]) > 500: new_image = resizeimage.resize_width(pil_image_obj, 500) else: new_image = pil_image_obj new_image_io = BytesIO() new_image.save(new_image_io, format='JPEG') temp_name = file_name self.image_slurp = None self.image.delete(save=False) self.image.save(temp_name, content=ContentFile(new_image_io.getvalue()), save=False) super(Article, self).save(*args, **kwargs)
def test_encoding_integrity(self): with open_utf8('test_Sources') as f: utf8 = list(deb822.Deb822.iter_paragraphs(f)) with open('test_Sources.iso8859-1', 'rb') as f: latin1 = list(deb822.Deb822.iter_paragraphs( f, encoding='iso8859-1')) # dump() with no fd returns a unicode object - both should be identical self.assertEqual(len(utf8), len(latin1)) for i in range(len(utf8)): self.assertEqual(utf8[i].dump(), latin1[i].dump()) # XXX: The way multiline fields parsing works, we can't guarantee # that trailing whitespace is reproduced. with open('test_Sources', 'rb') as f: utf8_contents = b"\n".join([line.rstrip() for line in f] + [b'']) with open('test_Sources.iso8859-1', 'rb') as f: latin1_contents = b"\n".join([line.rstrip() for line in f] + [b'']) utf8_to_latin1 = BytesIO() for d in utf8: d.dump(fd=utf8_to_latin1, encoding='iso8859-1') utf8_to_latin1.write(b"\n") latin1_to_utf8 = BytesIO() for d in latin1: d.dump(fd=latin1_to_utf8, encoding='utf-8') latin1_to_utf8.write(b"\n") self.assertEqual(utf8_contents, latin1_to_utf8.getvalue()) self.assertEqual(latin1_contents, utf8_to_latin1.getvalue())
class PreambleTestCase(unittest.TestCase): class doc_info: doc_id = 'D-deadbeef' rev = '397932e0c77f45fcb7c3732930e7e9b2:1' def setUp(self): self.cleartext = BytesIO(snowden1) self.blob = _crypto.BlobEncryptor( self.doc_info, self.cleartext, secret='A' * 96) def test_preamble_starts_with_magic_signature(self): preamble = self.blob._encode_preamble() assert preamble.startswith(_crypto.MAGIC) def test_preamble_has_cipher_metadata(self): preamble = self.blob._encode_preamble() unpacked = _preamble.PACMAN.unpack(preamble) encryption_scheme, encryption_method = unpacked[1:3] assert encryption_scheme in _crypto.ENC_SCHEME assert encryption_method in _crypto.ENC_METHOD assert unpacked[4] == self.blob.iv def test_preamble_has_document_sync_metadata(self): preamble = self.blob._encode_preamble() unpacked = _preamble.PACMAN.unpack(preamble) doc_id, doc_rev = unpacked[5:7] assert doc_id == self.doc_info.doc_id assert doc_rev == self.doc_info.rev def test_preamble_has_document_size(self): preamble = self.blob._encode_preamble() unpacked = _preamble.PACMAN.unpack(preamble) size = unpacked[7] assert size == _crypto._ceiling(len(snowden1)) @defer.inlineCallbacks def test_preamble_can_come_without_size(self): # XXX: This test case is here only to test backwards compatibility! preamble = self.blob._encode_preamble() # repack preamble using legacy format, without doc size unpacked = _preamble.PACMAN.unpack(preamble) preamble_without_size = _preamble.LEGACY_PACMAN.pack(*unpacked[0:7]) # encrypt it manually for custom tag ciphertext, tag = _aes_encrypt(self.blob.sym_key, self.blob.iv, self.cleartext.getvalue(), aead=preamble_without_size) ciphertext = ciphertext + tag # encode it ciphertext = base64.urlsafe_b64encode(ciphertext) preamble_without_size = base64.urlsafe_b64encode(preamble_without_size) # decrypt it ciphertext = preamble_without_size + ' ' + ciphertext cleartext = yield _crypto.BlobDecryptor( self.doc_info, BytesIO(ciphertext), secret='A' * 96).decrypt() assert cleartext.getvalue() == self.cleartext.getvalue() warnings = self.flushWarnings() assert len(warnings) == 1 assert 'legacy preamble without size' in warnings[0]['message']
def __init__(self, strings=()): self.strings = OrderedDict((s, 0) for s in strings) self.records = [] offset = 0 buf = BytesIO() for key in tuple(self.strings.iterkeys()): utf8 = utf8_text(key[:self.MAX_STRING_LENGTH]) l = len(utf8) sz_bytes = encint(l) raw = sz_bytes + utf8 if 0xfbf8 - buf.tell() < 6 + len(raw): # Records in PDB files cannot be larger than 0x10000, so we # stop well before that. pad = 0xfbf8 - buf.tell() buf.write(b'\0' * pad) self.records.append(buf.getvalue()) buf.seek(0), buf.truncate(0) offset = len(self.records) * 0x10000 buf.write(raw) self.strings[key] = offset offset += len(raw) val = buf.getvalue() if val: self.records.append(align_block(val))
def process(self, response:Response, responseCnt:ResponseContent, **keyargs): ''' @see: HandlerProcessorProceed.process Process the error into a response content. ''' assert isinstance(response, Response), 'Invalid response %s' % response assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt if Response.code in response and not response.code.isSuccess and Response.renderFactory in response: errors = [Value('code', str(response.code.code))] if Response.errorMessage in response: errors.append(Value('message', response.errorMessage)) elif Response.text in response: errors.append(Value('message', response.text)) if Response.errorDetails in response: errors.append(Object('details', response.errorDetails)) output = BytesIO() render = response.renderFactory(output) renderObject(Object('error', *errors), render) content = output.getvalue() responseCnt.length = len(content) responseCnt.source = (output.getvalue(),)
def test_compression(): arr = np.zeros(100).reshape((5,20)) arr[2,10] = 1 stream = BytesIO() savemat(stream, {'arr':arr}) raw_len = len(stream.getvalue()) vals = loadmat(stream) yield assert_array_equal, vals['arr'], arr stream = BytesIO() savemat(stream, {'arr':arr}, do_compression=True) compressed_len = len(stream.getvalue()) vals = loadmat(stream) yield assert_array_equal, vals['arr'], arr yield assert_, raw_len > compressed_len # Concatenate, test later arr2 = arr.copy() arr2[0,0] = 1 stream = BytesIO() savemat(stream, {'arr':arr, 'arr2':arr2}, do_compression=False) vals = loadmat(stream) yield assert_array_equal, vals['arr2'], arr2 stream = BytesIO() savemat(stream, {'arr':arr, 'arr2':arr2}, do_compression=True) vals = loadmat(stream) yield assert_array_equal, vals['arr2'], arr2
def tar_stream(repo, tree, mtime, format=''): """ Returns a generator that lazily assembles a .tar.gz archive, yielding it in pieces (bytestrings). To obtain the complete .tar.gz binary file, simply concatenate these chunks. 'repo' and 'tree' are the dulwich Repo and Tree objects the archive shall be created from. 'mtime' is a UNIX timestamp that is assigned as the modification time of all files in the resulting .tar.gz archive. """ buf = BytesIO() with closing(tarfile.open(None, "w:%s" % format, buf)) as tar: for entry_abspath, entry in walk_tree(repo, tree): try: blob = repo[entry.sha] except KeyError: # Entry probably refers to a submodule, which we don't yet support. continue data = ListBytesIO(blob.chunked) info = tarfile.TarInfo() info.name = entry_abspath info.size = blob.raw_length() info.mode = entry.mode info.mtime = mtime tar.addfile(info, data) yield buf.getvalue() buf.truncate(0) buf.seek(0) yield buf.getvalue()
def test_read_write_sio(): eg_sio1 = BytesIO() with make_simple(eg_sio1, 'w') as f1: str_val = eg_sio1.getvalue() eg_sio2 = BytesIO(str_val) with netcdf_file(eg_sio2) as f2: check_simple(f2) # Test that error is raised if attempting mmap for sio eg_sio3 = BytesIO(str_val) assert_raises(ValueError, netcdf_file, eg_sio3, 'r', True) # Test 64-bit offset write / read eg_sio_64 = BytesIO() with make_simple(eg_sio_64, 'w', version=2) as f_64: str_val = eg_sio_64.getvalue() eg_sio_64 = BytesIO(str_val) with netcdf_file(eg_sio_64) as f_64: check_simple(f_64) assert_equal(f_64.version_byte, 2) # also when version 2 explicitly specified eg_sio_64 = BytesIO(str_val) with netcdf_file(eg_sio_64, version=2) as f_64: check_simple(f_64) assert_equal(f_64.version_byte, 2)
def writeOut( outputKey, image, metadata, heartbeat, outputToAccumulo, outputToLocalDirectory, outputDirectoryName, outputToStdOut ): if outputToAccumulo: heartbeat.write("%s write to Accumulo with key %s\n" % (time.strftime("%H:%M:%S"), outputKey)) buff = BytesIO() image.save(buff, "PNG", options="optimize") try: AccumuloInterface.write(outputKey, json.dumps(metadata), buff.getvalue()) except jpype.JavaException as exception: raise RuntimeError(exception.stacktrace()) if outputToLocalDirectory: heartbeat.write( "%s write to local filesystem with path %s/%s.png\n" % (time.strftime("%H:%M:%S"), outputDirectoryName, outputKey) ) image.save("%s/%s.png" % (outputDirectoryName, outputKey), "PNG", options="optimize") json.dump(metadata, open("%s/%s.metadata" % (outputDirectoryName, outputKey), "w")) if outputToStdOut: heartbeat.write("%s write to standard output with key %s\n" % (time.strftime("%H:%M:%S"), outputKey)) buff = BytesIO() image.save(buff, "PNG", options="optimize") sys.stdout.write("%s\t%s\n" % (outputKey, base64.b64encode(buff.getvalue())))
def check_pickling(self, family): # The "family" objects are singletons; they can be pickled and # unpickled, and the same instances will always be returned on # unpickling, whether from the same unpickler or different # unpicklers. import pickle from io import BytesIO s = pickle.dumps((family, family)) (f1, f2) = pickle.loads(s) self.assertIs(f1, family) self.assertIs(f2, family) # Using a single memo across multiple pickles: sio = BytesIO() p = pickle.Pickler(sio) p.dump(family) p.dump([family]) u = pickle.Unpickler(BytesIO(sio.getvalue())) f1 = u.load() f2, = u.load() self.assertTrue(f1 is family) self.assertTrue(f2 is family) # Using separate memos for each pickle: sio = BytesIO() p = pickle.Pickler(sio) p.dump(family) p.clear_memo() p.dump([family]) u = pickle.Unpickler(BytesIO(sio.getvalue())) f1 = u.load() f2, = u.load() self.assertTrue(f1 is family) self.assertTrue(f2 is family)
class Msgunfmt(Command): CHARSET_RE = re.compile(rb'^"Content-Type: [^;]+; charset=([^\\]+)\\n"$') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._header = BytesIO() self._encoding = None @tool_required('msgunfmt') def cmdline(self): return ['msgunfmt', self.path] def filter(self, line): if not self._encoding: self._header.write(line) if line == b'\n': logger.debug("unable to determine PO encoding, let's hope it's utf-8") self._encoding = 'utf-8' return self._header.getvalue() found = Msgunfmt.CHARSET_RE.match(line) if found: self._encoding = found.group(1).decode('us-ascii').lower() return self._header.getvalue().decode(self._encoding).encode('utf-8') return b'' if self._encoding != 'utf-8': return line.decode(self._encoding).encode('utf-8') else: return line
def test_projectmodels(self): """ Test working projectmodels command """ stdout = BytesIO() stderr = BytesIO() call_command('projectmodels', stdout=stdout, stderr=stderr) stdout = stdout.getvalue() self.assertIn('Session -', stdout) self.assertNotIn('error: ', stdout) models = ['Session', 'LogEntry', 'Permission', 'Group', 'User', 'ContentType', 'Bio', 'HttpRequest', 'History', 'MigrationHistory'] records = [Session, LogEntry, Permission, Group, User, ContentType, Bio, HttpRequest, History, MigrationHistory] records = [num.objects.count() for num in records] for out, name, count in zip(stdout.split('\n'), models, records): out = out.strip().split() self.assertEqual(out[0], name) self.assertEqual(out[2], str(count)) stderr = stderr.getvalue() self.assertIn('error: Session -', stderr) for err, name, count in zip(stderr.split('\n'), models, records): err = err.strip().split() self.assertEqual(err[0], 'error:') self.assertEqual(err[1], name) self.assertEqual(err[3], str(count))
class StdoutAssertionsMixin(object): """ Mix this in to be able to assert on stdout during the test """ def setUpStdoutAssertions(self): # # sys.stdout is implemented differently # in Python 2 and Python 3, so we need to # override it differently. # In Python 2, sys.stdout is a byte stream. # In Python 3, sys.stdout is a text stream. if PY3: self.stdout = StringIO() else: self.stdout = BytesIO() self.patch(sys, "stdout", self.stdout) def assertWasQuiet(self): self.assertEqual(self.stdout.getvalue(), "") def assertInStdout(self, exp): self.assertIn(exp, self.stdout.getvalue()) def assertStdoutEqual(self, exp, msg=None): self.assertEqual(exp, self.stdout.getvalue(), msg) def getStdout(self): return self.stdout.getvalue().strip()
def test_failed_validation(self): """Check that failed validation writes sensible XML to stdout.""" # Override abstract methods class NewScript(Script): def get_scheme(self): return None def validate_input(self, definition): raise ValueError("Big fat validation error!") def stream_events(self, inputs, ew): # unused return script = NewScript() out = BytesIO() err = BytesIO() ew = EventWriter(out, err) args = [TEST_SCRIPT_PATH, "--validate-arguments"] return_value = script.run_script(args, ew, data_open("data/validation.xml")) expected = ET.parse(data_open("data/validation_error.xml")).getroot() found = ET.fromstring(out.getvalue().decode('utf-8')) self.assertEqual(b"", err.getvalue()) self.assertTrue(xml_compare(expected, found)) self.assertNotEqual(0, return_value)
def get_data_url(self, max_width = None): data_url = b'data:{};base64,{}' if self.is_image(): image_file = ImageBufferIO(self.file_data) output = ImageBufferIO() try: image = Image.open(image_file) s = image.size if max_width and s[0] > max_width: ratio = max_width / s[0] width = s[0] * ratio height = s[1] * ratio self._generate_thumbnail(image, output, (width, height)) file_data = output.getvalue() else: file_data = image_file.getvalue() except IOError: file_data = self.file_data logger.exception('Error when trying to resize image for data: url') else: file_data = self.file_data data = bytes(url_quote(file_data.encode('base64'))) return data_url.format(self.content_type, data)
def test_error_on_script_with_null_scheme(self): """A script that returns a null scheme should generate no output on stdout and an error on stderr saying that it the scheme was null.""" # Override abstract methods class NewScript(Script): def get_scheme(self): return None def stream_events(self, inputs, ew): # not used return script = NewScript() out = BytesIO() err = BytesIO() ew = EventWriter(out, err) in_stream = StringIO() args = [TEST_SCRIPT_PATH, "--scheme"] return_value = script.run_script(args, ew, in_stream) self.assertEqual(b"", out.getvalue()) self.assertEqual(b"FATAL Modular input script returned a null scheme.\n", err.getvalue()) self.assertNotEqual(0, return_value)
def test_read_write_sio(): eg_sio1 = BytesIO() f1 = make_simple(eg_sio1, 'w') str_val = eg_sio1.getvalue() f1.close() eg_sio2 = BytesIO(str_val) f2 = netcdf_file(eg_sio2) for testargs in gen_for_simple(f2): yield testargs f2.close() # Test that error is raised if attempting mmap for sio eg_sio3 = BytesIO(str_val) yield assert_raises, ValueError, netcdf_file, eg_sio3, 'r', True # Test 64-bit offset write / read eg_sio_64 = BytesIO() f_64 = make_simple(eg_sio_64, 'w', version=2) str_val = eg_sio_64.getvalue() f_64.close() eg_sio_64 = BytesIO(str_val) f_64 = netcdf_file(eg_sio_64) for testargs in gen_for_simple(f_64): yield testargs yield assert_equal, f_64.version_byte, 2 # also when version 2 explicitly specified eg_sio_64 = BytesIO(str_val) f_64 = netcdf_file(eg_sio_64, version=2) for testargs in gen_for_simple(f_64): yield testargs yield assert_equal, f_64.version_byte, 2
def Generate(self, File=None): Buffer = BytesIO() if len(self.PostfixNotation) == 0: return False for Item in self.PostfixNotation: if Item in self.Opcode[self.Phase]: Buffer.write(pack("B", self.Opcode[self.Phase][Item])) elif Item in self.SupportedOpcode: EdkLogger.error("GenDepex", FORMAT_INVALID, "Opcode [%s] is not expected in %s phase" % (Item, self.Phase), ExtraData=self.ExpressionString) else: Buffer.write(self.GetGuidValue(Item)) FilePath = "" FileChangeFlag = True if File is None: sys.stdout.write(Buffer.getvalue()) FilePath = "STDOUT" else: FileChangeFlag = SaveFileOnChange(File, Buffer.getvalue(), True) Buffer.close() return FileChangeFlag
class PktLineParser(object): """Packet line parser that hands completed packets off to a callback. """ def __init__(self, handle_pkt): self.handle_pkt = handle_pkt self._readahead = BytesIO() def parse(self, data): """Parse a fragment of data and call back for any completed packets. """ self._readahead.write(data) buf = self._readahead.getvalue() if len(buf) < 4: return while len(buf) >= 4: size = int(buf[:4], 16) if size == 0: self.handle_pkt(None) buf = buf[4:] elif size <= len(buf): self.handle_pkt(buf[4:size]) buf = buf[size:] else: break self._readahead = BytesIO() self._readahead.write(buf) def get_tail(self): """Read back any unused data.""" return self._readahead.getvalue()
def test_send_pack_new_ref_only(self): self.rin.write( b'0063310ca9477129b8586fa2afc779c1f57cf64bba6c ' b'refs/heads/master\x00report-status delete-refs ofs-delta\n' b'0000000eunpack ok\n' b'0019ok refs/heads/blah12\n' b'0000') self.rin.seek(0) def determine_wants(refs): return { b'refs/heads/blah12': b'310ca9477129b8586fa2afc779c1f57cf64bba6c', b'refs/heads/master': b'310ca9477129b8586fa2afc779c1f57cf64bba6c' } def generate_pack_data(have, want, ofs_delta=False): return 0, [] f = BytesIO() write_pack_objects(f, {}) self.client.send_pack('/', determine_wants, generate_pack_data) self.assertIn( self.rout.getvalue(), [b'007f0000000000000000000000000000000000000000 ' b'310ca9477129b8586fa2afc779c1f57cf64bba6c ' b'refs/heads/blah12\x00report-status ofs-delta0000' + f.getvalue(), b'007f0000000000000000000000000000000000000000 ' b'310ca9477129b8586fa2afc779c1f57cf64bba6c ' b'refs/heads/blah12\x00ofs-delta report-status0000' + f.getvalue()])
def test_odata_directory(self): """Test that conversions to output files work properly""" print("starting") IMP.set_log_level(IMP.MEMORY) IMP._test_ofile("ofile_test") self.assertRaises(IOError, IMP._test_ofile, "nodir/hi") f = open("hi", "w") IMP._test_ofile(f) # In Python 3 binary files are handled differently (as raw bytes, # not Unicode) f = open("hi", "wb") IMP._test_ofile(f) s = BytesIO() IMP._test_ofile(s) self.assertTrue(s.getvalue().startswith(b"hi\n")) if sys.version_info[0] >= 3: s = StringIO() IMP._test_ofile(s) self.assertTrue(s.getvalue().startswith("hi\n")) self.assertRaises(TypeError, IMP._test_ofile, 1) class NoMethods(object): pass self.assertRaises(TypeError, IMP._test_ofile, NoMethods) del f print("unlinking") # os.unlink('ofile_test') # os.unlink('hi') print("done")
def _shell_command_full(self): """ Generates a shell command which execution is equal to self.execute() including handling of errors in the middle of the process pipe. """ command = BytesIO() pipe_ok_statuses = [] self._shell_command(command, pipe_ok_statuses) if len(pipe_ok_statuses) == 1: return command.getvalue() command.write(b"; statuses=(${PIPESTATUS[@]});") for process_id, ok_statuses in enumerate(pipe_ok_statuses): if process_id == len(pipe_ok_statuses) - 1: command.write(psys.b(" exit ${{statuses[{0}]}};".format(process_id))) else: command.write(psys.b(" case ${{statuses[{0}]}} in".format(process_id))) if ok_statuses: command.write(psys.b(" {0});;".format("|".join(str(status) for status in ok_statuses)))) command.write(b" *) exit 128;; esac;") return b"bash -c '" + command.getvalue().replace(b"'", b"""'"'"'""") + b"'"
def run(size, chan=None, df=0, success=True, reader=None, cfg=None): if cfg is None: cfg = config.fastest() tx_data = os.urandom(size) tx_audio = BytesIO() main.send(config=cfg, src=BytesIO(tx_data), dst=tx_audio, gain=0.5) data = tx_audio.getvalue() data = common.loads(data) if chan is not None: data = chan(data) if df: sampler = sampling.Sampler(data, sampling.Interpolator()) sampler.freq += df data = sampler.take(len(data)) data = common.dumps(data) rx_audio = BytesIO(data) rx_data = BytesIO() dump = BytesIO() if reader: rx_audio = reader(rx_audio) try: result = main.recv(config=cfg, src=rx_audio, dst=rx_data, dump_audio=dump, pylab=None) finally: rx_audio.close() rx_data = rx_data.getvalue() assert data.startswith(dump.getvalue()) assert result == success if success: assert rx_data == tx_data
def test_image_saving(self): """Test image saving.""" tmp_file = BytesIO() compare_file = BytesIO() self.assertEqual(tmp_file.getvalue(), compare_file.getvalue()) self.image_save.save(tmp_file) self.assertNotEqual(str(tmp_file.getvalue()), compare_file.getvalue())
def save(self, filename_or_buf=None, compressed=True): ''' Save the TAG_Compound element to a file. Since this element is the root tag, it can be named. Pass a filename to save the data to a file. Pass a file-like object (with a read() method) to write the data to that object. Pass nothing to return the data as a string. ''' if self.name is None: self.name = '' buf = BytesIO() self.write_tag(buf) self.write_name(buf) self.write_value(buf) data = buf.getvalue() if compressed: gzio = BytesIO() gz = gzip.GzipFile(fileobj=gzio, mode='wb') gz.write(data) gz.close() data = gzio.getvalue() if filename_or_buf is None: return data if isinstance(filename_or_buf, str): f = open(filename_or_buf, 'wb') f.write(data) else: filename_or_buf.write(data)
def _test_IntBase_write_base(format_): # test one-byte values stream = BytesIO() format = format_(1) context = {} format.write(stream, 0x25, context) assert stream.getvalue() == "\x25" # test two-byte values stream = BytesIO() format = format_(2) context = {} format.write(stream, 0x1516, context) assert stream.getvalue() == "\x16\x15" # test three-byte values (our decoder) stream = BytesIO() format = format_(3) context = {} format.write(stream, 0x171615, context) assert stream.getvalue() == "\x15\x16\x17" # test endianness stream = BytesIO() format = format_(3, ">") context = {} format.write(stream, 0x151617, context) assert stream.getvalue() == "\x15\x16\x17" # test 'L' format to guarantee non-special alignment stream = BytesIO() format = format_(4) context = {} format.write(stream, 0x18171615, context) assert stream.getvalue() == "\x15\x16\x17\x18"
def _gzip_compress(self, f): gz_out = BytesIO() gz = gzip.GzipFile(mode='wb', fileobj=gz_out) gz.write(f.read()) gz.flush() gz_out.getvalue() return gz_out
def retrieve(palabra): buffer = BytesIO() #change to use `requests' c = pycurl.Curl() address = 'http://lema.rae.es/drae/srv/search?' palabra_enc = urllib.parse.urlencode({'key':palabra}) c.setopt(c.URL, address+palabra_enc) c.setopt(c.WRITEDATA, buffer) c.perform() body = buffer.getvalue() l = body.decode().split("<script>")[1].split("</script>")[1].replace('<script type="text/javascript">', '').replace('document.forms[0].elements[1].value=', 'return ') ctx = execjs.compile(l) chall = ctx.call("challenge") pdata = "&".join(body.decode().split("<body")[1].replace("/>", "\n").split("\n")[1:-1]).replace('<input type="hidden" name="', '').replace('" value="', '=').replace('"','').replace('TS014dfc77_cr=', 'TS014dfc77_cr=' + urllib.parse.quote_plus(chall)) buffer = BytesIO() c = pycurl.Curl() c.setopt(c.URL, address+palabra_enc) c.setopt(c.WRITEDATA, buffer) c.setopt(pycurl.HTTPHEADER, ["Referer: http://lema.rae.es/drae/srv/search?key=hola", "Cache-Control: max-age=0", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Origin: http://lema.rae.es", "User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/39.0.2171.65 Chrome/39.0.2171.65 Safari/537.36" ]) c.setopt(c.POSTFIELDS, pdata) c.perform() body = buffer.getvalue().decode() return body
def test_basic(self): fake_stdin = BytesIO(b'HI') fake_stdout = BytesIO() fake_stderr = BytesIO() with save_sys_std(): sys.stdin = fake_stdin self.assertEqual(sys.stdin.read(), b'HI') sys.stdout = fake_stdout sys.stdout.write(b'Hello!\n') sys.stderr = fake_stderr sys.stderr.write(b'!!!') self.assertEqual(sys.stdin, self.stdin) self.assertEqual(sys.stdout, self.stdout) self.assertEqual(sys.stderr, self.stderr) self.assertFalse(self.stdin.read.called) self.assertFalse(self.stdout.write.called) self.assertFalse(self.stderr.write.called) self.assertEqual(fake_stdout.getvalue(), b'Hello!\n') self.assertEqual(fake_stderr.getvalue(), b'!!!')
def test_send_pack_new_ref_only(self): self.rin.write( '0063310ca9477129b8586fa2afc779c1f57cf64bba6c ' 'refs/heads/master\x00report-status delete-refs ofs-delta\n' '0000000eunpack ok\n' '0019ok refs/heads/blah12\n' '0000') self.rin.seek(0) def determine_wants(refs): return { 'refs/heads/blah12': '310ca9477129b8586fa2afc779c1f57cf64bba6c', 'refs/heads/master': '310ca9477129b8586fa2afc779c1f57cf64bba6c' } def generate_pack_contents(have, want): return {} f = BytesIO() write_pack_objects(f, {}) self.client.send_pack('/', determine_wants, generate_pack_contents) self.assertIn( self.rout.getvalue(), ['007f0000000000000000000000000000000000000000 ' '310ca9477129b8586fa2afc779c1f57cf64bba6c ' 'refs/heads/blah12\x00report-status ofs-delta0000%s' % f.getvalue(), '007f0000000000000000000000000000000000000000 ' '310ca9477129b8586fa2afc779c1f57cf64bba6c ' 'refs/heads/blah12\x00ofs-delta report-status0000%s' % f.getvalue()])
def image2string(im, format='BMP', **kwargs): fp = BytesIO() im.save(fp, format=format, **kwargs) return fp.getvalue()
def getPhoneNum(self): stream = BytesIO() self.getEWSUrl("/hp/device/settings_fax_setup_wizard.xml", stream) fax_setup = utils.XMLToDictParser().parseXML(stream.getvalue()) return fax_setup['faxsetupwizard-faxvoicenumber-faxnumber']
<filter id='MyFilter' filterUnits='objectBoundingBox' x='0' y='0' width='1' height='1'> <feGaussianBlur in='SourceAlpha' stdDeviation='4%' result='blur'/> <feOffset in='blur' dx='4%' dy='4%' result='offsetBlur'/> <feSpecularLighting in='blur' surfaceScale='5' specularConstant='.75' specularExponent='20' lighting-color='#bbbbbb' result='specOut'> <fePointLight x='-5000%' y='-10000%' z='20000%'/> </feSpecularLighting> <feComposite in='specOut' in2='SourceAlpha' operator='in' result='specOut'/> <feComposite in='SourceGraphic' in2='specOut' operator='arithmetic' k1='0' k2='1' k3='1' k4='0'/> </filter> </defs> """ tree, xmlid = ET.XMLID(f.getvalue()) # insert the filter definition in the svg dom tree. tree.insert(0, ET.XML(filter_def)) for i, pie_name in enumerate(labels): pie = xmlid[pie_name] pie.set("filter", 'url(#MyFilter)') shadow = xmlid[pie_name + "_shadow"] shadow.set("filter", 'url(#dropshadow)') fn = "svg_filter_pie.svg" print("Saving '%s'" % fn) ET.ElementTree(tree).write(fn)
# 결과로 반환된 값을 저장할 buffer 지정 # ByteIO를 사용할 것을 권장 c.setopt(c.WRITEDATA, buffer) # SSL 인증서 확인 무시 c.setopt(c.SSL_VERIFYPEER, False) c.setopt(pycurl.FOLLOWLOCATION, 1) # 웹에 접근한 후, 세션을 종료하는 코드 c.perform() c.close() page = 1 while 1: body = buffer.getvalue().decode('utf-8') soup = BeautifulSoup(body, 'html.parser') obj = soup.find("nav", {"class": "pagination"}) a_list = obj.find_all("a") for a in a_list: page += 1 break print("총 " + str(page) + " 개의 페이지가 확인 됬습니다.") i = 0 while 1: c = pycurl.Curl() c.setopt(pycurl.FOLLOWLOCATION, 1) if page == i: break else:
def send(obj): buffer = BytesIO() CustomizablePickler(buffer, self._reducers).dump(obj) self._writer.send_bytes(buffer.getvalue())
class CallbackFileWrapper(object): """ Small wrapper around a fp object which will tee everything read into a buffer, and when that file is closed it will execute a callback with the contents of that buffer. All attributes are proxied to the underlying file object. This class uses members with a double underscore (__) leading prefix so as not to accidentally shadow an attribute. """ def __init__(self, fp, callback): self.__buf = BytesIO() self.__fp = fp self.__callback = callback def __getattr__(self, name): # The vaguaries of garbage collection means that self.__fp is # not always set. By using __getattribute__ and the private # name[0] allows looking up the attribute value and raising an # AttributeError when it doesn't exist. This stop thigns from # infinitely recursing calls to getattr in the case where # self.__fp hasn't been set. # # [0] https://docs.python.org/2/reference/expressions.html#atom-identifiers fp = self.__getattribute__("_CallbackFileWrapper__fp") return getattr(fp, name) def __is_fp_closed(self): try: return self.__fp.fp is None except AttributeError: pass try: return self.__fp.closed except AttributeError: pass # We just don't cache it then. # TODO: Add some logging here... return False def _close(self): if self.__callback: self.__callback(self.__buf.getvalue()) # We assign this to None here, because otherwise we can get into # really tricky problems where the CPython interpreter dead locks # because the callback is holding a reference to something which # has a __del__ method. Setting this to None breaks the cycle # and allows the garbage collector to do it's thing normally. self.__callback = None def read(self, amt=None): data = self.__fp.read(amt) self.__buf.write(data) if self.__is_fp_closed(): self._close() return data def _safe_read(self, amt): data = self.__fp._safe_read(amt) if amt == 2 and data == b"\r\n": # urllib executes this read to toss the CRLF at the end # of the chunk. return data self.__buf.write(data) if self.__is_fp_closed(): self._close() return data
def run(self): #results = {} # {'file' : error_code,...} STATE_DONE = 0 STATE_ABORTED = 10 STATE_SUCCESS = 20 STATE_BUSY = 25 STATE_READ_SENDER_INFO = 30 STATE_PRERENDER = 40 STATE_COUNT_PAGES = 50 STATE_NEXT_RECIPIENT = 60 STATE_COVER_PAGE = 70 STATE_SINGLE_FILE = 80 STATE_MERGE_FILES = 90 STATE_SINGLE_FILE = 100 STATE_SEND_FAX = 110 STATE_CLEANUP = 120 STATE_ERROR = 130 next_recipient = self.next_recipient_gen() state = STATE_READ_SENDER_INFO self.rendered_file_list = [] while state != STATE_DONE: # --------------------------------- Fax state machine if self.check_for_cancel(): state = STATE_ABORTED log.debug("STATE=(%d, 0, 0)" % state) if state == STATE_ABORTED: # --------------------------------- Aborted (10, 0, 0) log.error("Aborted by user.") self.write_queue((STATUS_IDLE, 0, '')) state = STATE_CLEANUP elif state == STATE_SUCCESS: # --------------------------------- Success (20, 0, 0) log.debug("Success.") self.write_queue((STATUS_COMPLETED, 0, '')) state = STATE_CLEANUP elif state == STATE_ERROR: # --------------------------------- Error (130, 0, 0) log.error("Error, aborting.") self.write_queue((STATUS_ERROR, 0, '')) state = STATE_CLEANUP elif state == STATE_BUSY: # --------------------------------- Busy (25, 0, 0) log.error("Device busy, aborting.") self.write_queue((STATUS_BUSY, 0, '')) state = STATE_CLEANUP elif state == STATE_READ_SENDER_INFO: # --------------------------------- Get sender info (30, 0, 0) log.debug("%s State: Get sender info" % ("*"*20)) state = STATE_PRERENDER try: try: self.dev.open() except Error as e: log.error("Unable to open device (%s)." % e.msg) state = STATE_ERROR else: try: self.sender_name = self.dev.station_name log.debug("Sender name=%s" % self.sender_name) self.sender_fax = self.dev.phone_num log.debug("Sender fax=%s" % self.sender_fax) except Error: log.error("HTTP GET failed!") state = STATE_ERROR finally: self.dev.close() elif state == STATE_PRERENDER: # --------------------------------- Pre-render non-G4 files (40, 0, 0) log.debug("%s State: Pre-render non-G4 files" % ("*"*20)) state = self.pre_render(STATE_COUNT_PAGES) elif state == STATE_COUNT_PAGES: # --------------------------------- Get total page count (50, 0, 0) log.debug("%s State: Get total page count" % ("*"*20)) state = self.count_pages(STATE_NEXT_RECIPIENT) elif state == STATE_NEXT_RECIPIENT: # --------------------------------- Loop for multiple recipients (60, 0, 0) log.debug("%s State: Next recipient" % ("*"*20)) state = STATE_COVER_PAGE try: recipient = next(next_recipient) log.debug("Processing for recipient %s" % recipient['name']) self.write_queue((STATUS_SENDING_TO_RECIPIENT, 0, recipient['name'])) except StopIteration: state = STATE_SUCCESS log.debug("Last recipient.") continue recipient_file_list = self.rendered_file_list[:] elif state == STATE_COVER_PAGE: # --------------------------------- Create cover page (70, 0, 0) log.debug("%s State: Render cover page" % ("*"*20)) state = self.cover_page(recipient) elif state == STATE_SINGLE_FILE: # --------------------------------- Special case for single file (no merge) (80, 0, 0) log.debug("%s State: Handle single file" % ("*"*20)) state = self.single_file(STATE_SEND_FAX) elif state == STATE_MERGE_FILES: # --------------------------------- Merge multiple G4 files (90, 0, 0) log.debug("%s State: Merge multiple files" % ("*"*20)) state = self.merge_files(STATE_SEND_FAX) elif state == STATE_SEND_FAX: # --------------------------------- Send fax state machine (110, 0, 0) log.debug("%s State: Send fax" % ("*"*20)) state = STATE_NEXT_RECIPIENT FAX_SEND_STATE_DONE = 0 FAX_SEND_STATE_ABORT = 10 FAX_SEND_STATE_ERROR = 20 FAX_SEND_STATE_BUSY = 25 FAX_SEND_STATE_SUCCESS = 30 FAX_SEND_STATE_DEVICE_OPEN = 40 FAX_SEND_STATE_BEGINJOB = 50 FAX_SEND_STATE_DOWNLOADPAGES = 60 FAX_SEND_STATE_ENDJOB = 70 FAX_SEND_STATE_CANCELJOB = 80 FAX_SEND_STATE_CLOSE_SESSION = 170 monitor_state = False fax_send_state = FAX_SEND_STATE_DEVICE_OPEN while fax_send_state != FAX_SEND_STATE_DONE: if self.check_for_cancel(): log.error("Fax send aborted.") fax_send_state = FAX_SEND_STATE_ABORT if monitor_state: fax_state = self.getFaxDownloadState() if not fax_state in (pml.UPDN_STATE_XFERACTIVE, pml.UPDN_STATE_XFERDONE): log.error("D/L error state=%d" % fax_state) fax_send_state = FAX_SEND_STATE_ERROR state = STATE_ERROR log.debug("STATE=(%d, %d, 0)" % (STATE_SEND_FAX, fax_send_state)) if fax_send_state == FAX_SEND_STATE_ABORT: # -------------- Abort (110, 10, 0) monitor_state = False fax_send_state = FAX_SEND_STATE_CANCELJOB state = STATE_ABORTED elif fax_send_state == FAX_SEND_STATE_ERROR: # -------------- Error (110, 20, 0) log.error("Fax send error.") monitor_state = False fax_send_state = FAX_SEND_STATE_CLOSE_SESSION state = STATE_ERROR elif fax_send_state == FAX_SEND_STATE_BUSY: # -------------- Busy (110, 25, 0) log.error("Fax device busy.") monitor_state = False fax_send_state = FAX_SEND_STATE_CLOSE_SESSION state = STATE_BUSY elif fax_send_state == FAX_SEND_STATE_SUCCESS: # -------------- Success (110, 30, 0) log.debug("Fax send success.") monitor_state = False fax_send_state = FAX_SEND_STATE_CLOSE_SESSION state = STATE_NEXT_RECIPIENT elif fax_send_state == FAX_SEND_STATE_DEVICE_OPEN: # -------------- Device open (110, 40, 0) log.debug("%s State: Open device" % ("*"*20)) fax_send_state = FAX_SEND_STATE_BEGINJOB try: self.dev.open() except Error as e: log.error("Unable to open device (%s)." % e.msg) fax_send_state = FAX_SEND_STATE_ERROR else: if self.dev.device_state == DEVICE_STATE_NOT_FOUND: fax_send_state = FAX_SEND_STATE_ERROR elif fax_send_state == FAX_SEND_STATE_BEGINJOB: # -------------- BeginJob (110, 50, 0) log.debug("%s State: BeginJob" % ("*"*20)) try: ff = open(self.f, 'rb') except IOError: log.error("Unable to read fax file.") fax_send_state = FAX_SEND_STATE_ERROR continue try: header = ff.read(FILE_HEADER_SIZE) except IOError: log.error("Unable to read fax file.") fax_send_state = FAX_SEND_STATE_ERROR continue magic, version, total_pages, hort_dpi, vert_dpi, page_size, \ resolution, encoding, reserved1, reserved2 = self.decode_fax_header(header) if magic != b'hplip_g3': log.error("Invalid file header. Bad magic.") fax_send_state = FAX_SEND_STATE_ERROR else: log.debug("Magic=%s Ver=%d Pages=%d hDPI=%d vDPI=%d Size=%d Res=%d Enc=%d" % (magic, version, total_pages, hort_dpi, vert_dpi, page_size, resolution, encoding)) job_id = self.job_id delay = 0 faxnum = recipient['fax'] speeddial = 0 if resolution == RESOLUTION_STD: res = "STANDARD" elif resolution == RESOLUTION_FINE: res = "FINE" elif resolution == RESOLUTION_300DPI: res = "SUPERFINE" soap = utils.cat( """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><Fax:BeginJob xmlns:Fax="urn:Fax"><ticket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Fax:Ticket"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$job_id</jobId><resolution xsi:type="Fax:Resolution">$res</resolution><delay xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$delay</delay><phoneNumber xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string">$faxnum</phoneNumber><speedDial xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:positiveInteger">$speeddial</speedDial></ticket></Fax:BeginJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""") data = self.format_http(soap.encode('utf-8')) log.log_data(data) if log.is_debug(): open('beginjob.log', 'wb').write(data) self.dev.openSoapFax() self.dev.writeSoapFax(data) ret = BytesIO() while self.dev.readSoapFax(8192, ret, timeout=5): pass ret = ret.getvalue() if log.is_debug(): open('beginjob_ret.log', 'wb').write(ret) log.log_data(ret) self.dev.closeSoapFax() if self.get_error_code(ret.decode('utf-8')) == HTTP_OK: fax_send_state = FAX_SEND_STATE_DOWNLOADPAGES else: fax_send_state = FAX_SEND_STATE_ERROR elif fax_send_state == FAX_SEND_STATE_DOWNLOADPAGES: # -------------- DownloadPages (110, 60, 0) log.debug("%s State: DownloadPages" % ("*"*20)) page = BytesIO() for p in range(total_pages): if self.check_for_cancel(): fax_send_state = FAX_SEND_STATE_ABORT if fax_send_state == FAX_SEND_STATE_ABORT: break try: header = ff.read(PAGE_HEADER_SIZE) except IOError: log.error("Unable to read fax file.") fax_send_state = FAX_SEND_STATE_ERROR continue page_num, ppr, rpp, bytes_to_read, thumbnail_bytes, reserved2 = \ self.decode_page_header(header) log.debug("Page=%d PPR=%d RPP=%d BPP=%d Thumb=%d" % (page_num, ppr, rpp, bytes_to_read, thumbnail_bytes)) if ppr != PIXELS_PER_LINE: log.error("Pixels per line (width) must be %d!" % PIXELS_PER_LINE) page.write(ff.read(bytes_to_read)) thumbnail = ff.read(thumbnail_bytes) # thrown away for now (should be 0 read) page.seek(0) try: data = page.read(bytes_to_read) except IOError: log.error("Unable to read fax file.") fax_send_state = FAX_SEND_STATE_ERROR break if data == b'': log.error("No data!") fax_send_state = FAX_SEND_STATE_ERROR break height = rpp job_id = self.job_id soap = utils.cat( """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string" SOAP-ENV:mustUnderstand="1">$job_id</jobId></SOAP-ENV:Header><SOAP-ENV:Body><Fax:DownloadPage xmlns:Fax="urn:Fax"><height xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:positiveInteger">$height</height></Fax:DownloadPage></SOAP-ENV:Body></SOAP-ENV:Envelope>""") m = dime.Message() m.add_record(dime.Record(b"cid:id0", b"http://schemas.xmlsoap.org/soap/envelope/", dime.TYPE_T_URI, to_bytes_utf8(soap))) m.add_record(dime.Record(b"", b"image/g4fax", dime.TYPE_T_MIME, data)) output = BytesIO() m.generate(output) data = self.format_http(output.getvalue(), content_type="application/dime") log.log_data(data) if log.is_debug(): open('downloadpages%d.log' % p, 'wb').write(data) try: self.dev.writeSoapFax(data) except Error: fax_send_state = FAX_SEND_STATE_ERROR ret = BytesIO() try: while self.dev.readSoapFax(8192, ret, timeout=5): pass except Error: fax_send_state = FAX_SEND_STATE_ERROR ret = ret.getvalue() if log.is_debug(): open('downloadpages%d_ret.log' % p, 'wb').write(ret) log.log_data(ret) self.dev.closeSoapFax() if self.get_error_code(ret.decode('utf-8')) != HTTP_OK: fax_send_state = FAX_SEND_STATE_ERROR break page.truncate(0) page.seek(0) else: fax_send_state = FAX_SEND_STATE_ENDJOB elif fax_send_state == FAX_SEND_STATE_ENDJOB: # -------------- EndJob (110, 70, 0) log.debug("%s State: EndJob" % ("*"*20)) job_id = self.job_id soap = utils.cat( """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string" SOAP-ENV:mustUnderstand="1">$job_id</jobId></SOAP-ENV:Header><SOAP-ENV:Body><Fax:EndJob xmlns:Fax="urn:Fax"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">$job_id</jobId></Fax:EndJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""") data = self.format_http(soap.encode('utf-8')) log.log_data(data) if log.is_debug(): open('endjob.log', 'wb').write(data) self.dev.writeSoapFax(data) ret = BytesIO() while self.dev.readSoapFax(8192, ret, timeout=5): pass ret = ret.getvalue() if log.is_debug(): open('endjob_ret.log', 'wb').write(ret) log.log_data(ret) self.dev.closeSoapFax() if self.get_error_code(ret.decode('utf-8')) == HTTP_OK: fax_send_state = FAX_SEND_STATE_SUCCESS else: fax_send_state = FAX_SEND_STATE_ERROR elif fax_send_state == FAX_SEND_STATE_CANCELJOB: # -------------- CancelJob (110, 80, 0) log.debug("%s State: CancelJob" % ("*"*20)) job_id = self.job_id soap = utils.cat( """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string" SOAP-ENV:mustUnderstand="1">$job_id</jobId></SOAP-ENV:Header><SOAP-ENV:Body><Fax:CancelJob xmlns:Fax="urn:Fax"><jobId xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">$job_id</jobId></Fax:CancelJob></SOAP-ENV:Body></SOAP-ENV:Envelope>""") data = self.format_http(soap.encode('utf-8')) log.log_data(data) if log.is_debug(): open('canceljob.log', 'wb').write(data) self.dev.writeSoapFax(data) ret = BytesIO() while self.dev.readSoapFax(8192, ret, timeout=5): pass ret = ret.getvalue() if log.is_debug(): open('canceljob_ret.log', 'wb').write(ret) log.log_data(ret) self.dev.closeSoapFax() if self.get_error_code(ret.decode('utf-8')) == HTTP_OK: fax_send_state = FAX_SEND_STATE_CLOSE_SESSION else: fax_send_state = FAX_SEND_STATE_ERROR elif fax_send_state == FAX_SEND_STATE_CLOSE_SESSION: # -------------- Close session (110, 170, 0) log.debug("%s State: Close session" % ("*"*20)) log.debug("Closing session...") try: mm.close() except NameError: pass try: ff.close() except NameError: pass time.sleep(1) self.dev.closeSoapFax() self.dev.close() fax_send_state = FAX_SEND_STATE_DONE # Exit inner state machine elif state == STATE_CLEANUP: # --------------------------------- Cleanup (120, 0, 0) log.debug("%s State: Cleanup" % ("*"*20)) if self.remove_temp_file: log.debug("Removing merged file: %s" % self.f) try: os.remove(self.f) log.debug("Removed") except OSError: log.debug("Not found") state = STATE_DONE # Exit outer state machine
def getStationName(self): stream = BytesIO() self.getEWSUrl("/hp/device/settings_fax_setup_wizard.xml", stream) fax_setup = utils.XMLToDictParser().parseXML(stream.getvalue()) return fax_setup['faxsetupwizard-userinformation-faxcompanyname']
ax.set_title('Important dates in the 2008-2009 financial crisis') fig = plt.figure() ax = fig.add_subplot(1,1,1) rect = plt.Rectangle((0.2, 0.75), 0.4, 0.15, color='k', alpha=0.3) circ = plt.Circle((0.7, 0.2), 0.15, color='b', alpha=0.3) pgon = plt.Polygon([[0.15, 0.15], [0.35, 0.4], [0.2, 0.6]], color='g', alpha=0.5) ax.add_patch(rect) ax.add_patch(circ) ax.add_patch(pgon) fig.savefig('figpath.svg') # plt.savefig fig.savefig('figpath.png', dpi=400, bbox_inches='tight') from io import BytesIO buffer = BytesIO() plt.savefig(buffer) plot_data = buffer.getvalue() plt.rc('figure', figsize=(10, 10)) # first argument is the component to customize, # after that can follow a sequence of keyword rguments indicating the new parameters font_options = {'family': 'monospace', 'weight': 'bold', 'size': 10} plt.rc('font', **font_options) data.head() data.plot() s = pd.Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10)) s.plot() df = pd.DataFrame(np.random.randn(10, 4).cumsum(0), columns=['A', 'B', 'C', 'D'],
def get_payload(self, i=None, decode=False): """Return a reference to the payload. The payload will either be a list object or a string. If you mutate the list object, you modify the message's payload in place. Optional i returns that index into the payload. Optional decode is a flag indicating whether the payload should be decoded or not, according to the Content-Transfer-Encoding header (default is False). When True and the message is not a multipart, the payload will be decoded if this header's value is `quoted-printable' or `base64'. If some other encoding is used, or the header is missing, or if the payload has bogus data (i.e. bogus base64 or uuencoded data), the payload is returned as-is. If the message is a multipart and the decode flag is True, then None is returned. """ # Here is the logic table for this code, based on the email5.0.0 code: # i decode is_multipart result # ------ ------ ------------ ------------------------------ # None True True None # i True True None # None False True _payload (a list) # i False True _payload element i (a Message) # i False False error (not a list) # i True False error (not a list) # None False False _payload # None True False _payload decoded (bytes) # Note that Barry planned to factor out the 'decode' case, but that # isn't so easy now that we handle the 8 bit data, which needs to be # converted in both the decode and non-decode path. if self.is_multipart(): if decode: return None if i is None: return self._payload else: return self._payload[i] # For backward compatibility, Use isinstance and this error message # instead of the more logical is_multipart test. if i is not None and not isinstance(self._payload, list): raise TypeError('Expected list, got %s' % type(self._payload)) payload = self._payload # cte might be a Header, so for now stringify it. cte = str(self.get('content-transfer-encoding', '')).lower() # payload may be bytes here. if isinstance(payload, str): if _has_surrogates(payload): bpayload = payload.encode('ascii', 'surrogateescape') if not decode: try: payload = bpayload.decode( self.get_param('charset', 'ascii'), 'replace') except LookupError: payload = bpayload.decode('ascii', 'replace') elif decode: try: bpayload = payload.encode('ascii') except UnicodeError: # This won't happen for RFC compliant messages (messages # containing only ASCII codepoints in the unicode input). # If it does happen, turn the string into bytes in a way # guaranteed not to fail. bpayload = payload.encode('raw-unicode-escape') if not decode: return payload if cte == 'quoted-printable': return utils._qdecode(bpayload) elif cte == 'base64': try: return base64.b64decode(bpayload) except binascii.Error: # Incorrect padding return bpayload elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'): in_file = BytesIO(bpayload) out_file = BytesIO() try: uu.decode(in_file, out_file, quiet=True) return out_file.getvalue() except uu.Error: # Some decoding problem return bpayload if isinstance(payload, str): return bpayload return payload
def view_page(request, pk): """ Контроллер страницы просмотра и изменения размеров рисунка """ title = 'Просмотр. изменение размеров рисунка' if request.method == 'POST': # file request.Files view_form = ImageViewForm(request.POST) if view_form.is_valid(): # Если размер изменен, рассчитываем новый размер, делаем его ресайз (от текущего current), декодируем в base64, # добавляем заголовок картинки (PNG) new_size_x = int(request.POST['size_x']) new_size_y = int(request.POST['size_y']) current_size_x = int(request.POST['current_size_x']) current_size_y = int(request.POST['current_size_y']) image = Images.objects.get(id=int(pk)) _image = Image.open(image.document) if new_size_x != current_size_x: # new_size_x changed new_size_y = int( (current_size_y * new_size_x) / current_size_x) else: if new_size_y != current_size_y: # new_size_y changed new_size_x = int( (current_size_x * new_size_y) / current_size_y) new_size = (new_size_x, new_size_y) _image = _image.resize(new_size) data = { 'size_x': f'{_image.size[0]}', 'size_y': f'{_image.size[1]}' } view_form = ImageViewForm(initial=data) buffered = BytesIO() _image.save(buffered, format="PNG") img_byte = base64.b64encode(buffered.getvalue()) image_decoded = "data:image/png;base64," + img_byte.decode() content = {'title': title, 'view_form': view_form, 'pk': int(pk), 'image': image_decoded, \ 'image_x': new_size_x, 'image_y': new_size_y} return render(request, 'mainapp/view_page.html', content) else: # GET # загружаем картинку и выводим в форму ее размеры image = Images.objects.get(id=int(pk)) _image = Image.open(image.document) data = {'size_x': f'{_image.size[0]}', 'size_y': f'{_image.size[1]}'} view_form = ImageViewForm(initial=data) # распаковываем картинку в массив, декодируем в base64 добавляем заголовок картинки (PNG) buffered = BytesIO() _image.save(buffered, format="PNG") img_byte = base64.b64encode(buffered.getvalue()) image_decoded = "data:image/png;base64," + img_byte.decode() content = {'title': title, 'view_form': view_form, 'pk': int(pk), 'image': image_decoded, \ 'image_x': _image.size[0], 'image_y': _image.size[1]} return render(request, 'mainapp/view_page.html', content)
def GenFmpCapsule(self): # # Generate capsule header # typedef struct { # EFI_GUID CapsuleGuid; # UINT32 HeaderSize; # UINT32 Flags; # UINT32 CapsuleImageSize; # } EFI_CAPSULE_HEADER; # Header = BytesIO() # # Use FMP capsule GUID: 6DCBD5ED-E82D-4C44-BDA1-7194199AD92A # Header.write( PackRegistryFormatGuid('6DCBD5ED-E82D-4C44-BDA1-7194199AD92A')) HdrSize = 0 if 'CAPSULE_HEADER_SIZE' in self.TokensDict: Header.write( pack('=I', int(self.TokensDict['CAPSULE_HEADER_SIZE'], 16))) HdrSize = int(self.TokensDict['CAPSULE_HEADER_SIZE'], 16) else: Header.write(pack('=I', 0x20)) HdrSize = 0x20 Flags = 0 if 'CAPSULE_FLAGS' in self.TokensDict: for flag in self.TokensDict['CAPSULE_FLAGS'].split(','): flag = flag.strip() if flag == 'PopulateSystemTable': Flags |= 0x00010000 | 0x00020000 elif flag == 'PersistAcrossReset': Flags |= 0x00010000 elif flag == 'InitiateReset': Flags |= 0x00040000 Header.write(pack('=I', Flags)) # # typedef struct { # UINT32 Version; # UINT16 EmbeddedDriverCount; # UINT16 PayloadItemCount; # // UINT64 ItemOffsetList[]; # } EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER; # FwMgrHdr = BytesIO() if 'CAPSULE_HEADER_INIT_VERSION' in self.TokensDict: FwMgrHdr.write( pack('=I', int(self.TokensDict['CAPSULE_HEADER_INIT_VERSION'], 16))) else: FwMgrHdr.write(pack('=I', 0x00000001)) FwMgrHdr.write( pack('=HH', len(self.CapsuleDataList), len(self.FmpPayloadList))) FwMgrHdrSize = 4 + 2 + 2 + 8 * (len(self.CapsuleDataList) + len(self.FmpPayloadList)) # # typedef struct _WIN_CERTIFICATE { # UINT32 dwLength; # UINT16 wRevision; # UINT16 wCertificateType; # //UINT8 bCertificate[ANYSIZE_ARRAY]; # } WIN_CERTIFICATE; # # typedef struct _WIN_CERTIFICATE_UEFI_GUID { # WIN_CERTIFICATE Hdr; # EFI_GUID CertType; # //UINT8 CertData[ANYSIZE_ARRAY]; # } WIN_CERTIFICATE_UEFI_GUID; # # typedef struct { # UINT64 MonotonicCount; # WIN_CERTIFICATE_UEFI_GUID AuthInfo; # } EFI_FIRMWARE_IMAGE_AUTHENTICATION; # # typedef struct _EFI_CERT_BLOCK_RSA_2048_SHA256 { # EFI_GUID HashType; # UINT8 PublicKey[256]; # UINT8 Signature[256]; # } EFI_CERT_BLOCK_RSA_2048_SHA256; # PreSize = FwMgrHdrSize Content = BytesIO() for driver in self.CapsuleDataList: FileName = driver.GenCapsuleSubItem() FwMgrHdr.write(pack('=Q', PreSize)) PreSize += os.path.getsize(FileName) File = open(FileName, 'rb') Content.write(File.read()) File.close() for fmp in self.FmpPayloadList: if fmp.Existed: FwMgrHdr.write(pack('=Q', PreSize)) PreSize += len(fmp.Buffer) Content.write(fmp.Buffer) continue if fmp.ImageFile: for Obj in fmp.ImageFile: fmp.ImageFile = Obj.GenCapsuleSubItem() if fmp.VendorCodeFile: for Obj in fmp.VendorCodeFile: fmp.VendorCodeFile = Obj.GenCapsuleSubItem() if fmp.Certificate_Guid: ExternalTool, ExternalOption = FindExtendTool( [], GenFdsGlobalVariable.ArchList, fmp.Certificate_Guid) CmdOption = '' CapInputFile = fmp.ImageFile if not os.path.isabs(fmp.ImageFile): CapInputFile = os.path.join( GenFdsGlobalVariable.WorkSpaceDir, fmp.ImageFile) CapOutputTmp = os.path.join(GenFdsGlobalVariable.FvDir, self.UiCapsuleName) + '.tmp' if ExternalTool is None: EdkLogger.error( "GenFds", GENFDS_ERROR, "No tool found with GUID %s" % fmp.Certificate_Guid) else: CmdOption += ExternalTool if ExternalOption: CmdOption = CmdOption + ' ' + ExternalOption CmdOption += ' -e ' + ' --monotonic-count ' + str( fmp.MonotonicCount ) + ' -o ' + CapOutputTmp + ' ' + CapInputFile CmdList = CmdOption.split() GenFdsGlobalVariable.CallExternalTool( CmdList, "Failed to generate FMP auth capsule") if uuid.UUID(fmp.Certificate_Guid) == EFI_CERT_TYPE_PKCS7_GUID: dwLength = 4 + 2 + 2 + 16 + os.path.getsize( CapOutputTmp) - os.path.getsize(CapInputFile) else: dwLength = 4 + 2 + 2 + 16 + 16 + 256 + 256 fmp.ImageFile = CapOutputTmp AuthData = [ fmp.MonotonicCount, dwLength, WIN_CERT_REVISION, WIN_CERT_TYPE_EFI_GUID, fmp.Certificate_Guid ] fmp.Buffer = fmp.GenCapsuleSubItem(AuthData) else: fmp.Buffer = fmp.GenCapsuleSubItem() FwMgrHdr.write(pack('=Q', PreSize)) PreSize += len(fmp.Buffer) Content.write(fmp.Buffer) BodySize = len(FwMgrHdr.getvalue()) + len(Content.getvalue()) Header.write(pack('=I', HdrSize + BodySize)) # # The real capsule header structure is 28 bytes # Header.write(b'\x00' * (HdrSize - 28)) Header.write(FwMgrHdr.getvalue()) Header.write(Content.getvalue()) # # Generate FMP capsule file # CapOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiCapsuleName) + '.Cap' SaveFileOnChange(CapOutputFile, Header.getvalue(), True) return CapOutputFile
def _find_module_utils(module_name, b_module_data, module_path, module_args, task_vars, module_compression): """ Given the source of the module, convert it to a Jinja2 template to insert module code and return whether it's a new or old style module. """ module_substyle = module_style = 'old' # module_style is something important to calling code (ActionBase). It # determines how arguments are formatted (json vs k=v) and whether # a separate arguments file needs to be sent over the wire. # module_substyle is extra information that's useful internally. It tells # us what we have to look to substitute in the module files and whether # we're using module replacer or ansiballz to format the module itself. if _is_binary(b_module_data): module_substyle = module_style = 'binary' elif REPLACER in b_module_data: # Do REPLACER before from ansible.module_utils because we need make sure # we substitute "from ansible.module_utils basic" for REPLACER module_style = 'new' module_substyle = 'python' b_module_data = b_module_data.replace( REPLACER, b'from ansible.module_utils.basic import *') elif b'from ansible.module_utils.' in b_module_data: module_style = 'new' module_substyle = 'python' elif REPLACER_WINDOWS in b_module_data or b'#Requires -Module' in b_module_data: module_style = 'new' module_substyle = 'powershell' elif REPLACER_JSONARGS in b_module_data: module_style = 'new' module_substyle = 'jsonargs' elif b'WANT_JSON' in b_module_data: module_substyle = module_style = 'non_native_want_json' shebang = None # Neither old-style, non_native_want_json nor binary modules should be modified # except for the shebang line (Done by modify_module) if module_style in ('old', 'non_native_want_json', 'binary'): return b_module_data, module_style, shebang output = BytesIO() py_module_names = set() if module_substyle == 'python': params = dict(ANSIBLE_MODULE_ARGS=module_args, ) python_repred_params = repr(json.dumps(params)) try: compression_method = getattr(zipfile, module_compression) except AttributeError: display.warning( u'Bad module compression string specified: %s. Using ZIP_STORED (no compression)' % module_compression) compression_method = zipfile.ZIP_STORED lookup_path = os.path.join(C.DEFAULT_LOCAL_TMP, 'ansiballz_cache') cached_module_filename = os.path.join( lookup_path, "%s-%s" % (module_name, module_compression)) zipdata = None # Optimization -- don't lock if the module has already been cached if os.path.exists(cached_module_filename): display.debug('ANSIBALLZ: using cached module: %s' % cached_module_filename) zipdata = open(cached_module_filename, 'rb').read() else: if module_name in action_write_locks.action_write_locks: display.debug('ANSIBALLZ: Using lock for %s' % module_name) lock = action_write_locks.action_write_locks[module_name] else: # If the action plugin directly invokes the module (instead of # going through a strategy) then we don't have a cross-process # Lock specifically for this module. Use the "unexpected # module" lock instead display.debug('ANSIBALLZ: Using generic lock for %s' % module_name) lock = action_write_locks.action_write_locks[None] display.debug('ANSIBALLZ: Acquiring lock') with lock: display.debug('ANSIBALLZ: Lock acquired: %s' % id(lock)) # Check that no other process has created this while we were # waiting for the lock if not os.path.exists(cached_module_filename): display.debug('ANSIBALLZ: Creating module') # Create the module zip data zipoutput = BytesIO() zf = zipfile.ZipFile(zipoutput, mode='w', compression=compression_method) # Note: If we need to import from release.py first, # remember to catch all exceptions: https://github.com/ansible/ansible/issues/16523 zf.writestr( 'ansible/__init__.py', b'from pkgutil import extend_path\n__path__=extend_path(__path__,__name__)\n__version__="' + to_bytes(__version__) + b'"\n__author__="' + to_bytes(__author__) + b'"\n') zf.writestr( 'ansible/module_utils/__init__.py', b'from pkgutil import extend_path\n__path__=extend_path(__path__,__name__)\n' ) zf.writestr('ansible_module_%s.py' % module_name, b_module_data) py_module_cache = {('__init__', ): (b'', '[builtin]')} recursive_finder(module_name, b_module_data, py_module_names, py_module_cache, zf) zf.close() zipdata = base64.b64encode(zipoutput.getvalue()) # Write the assembled module to a temp file (write to temp # so that no one looking for the file reads a partially # written file) if not os.path.exists(lookup_path): # Note -- if we have a global function to setup, that would # be a better place to run this os.makedirs(lookup_path) display.debug('ANSIBALLZ: Writing module') with open(cached_module_filename + '-part', 'wb') as f: f.write(zipdata) # Rename the file into its final position in the cache so # future users of this module can read it off the # filesystem instead of constructing from scratch. display.debug('ANSIBALLZ: Renaming module') os.rename(cached_module_filename + '-part', cached_module_filename) display.debug('ANSIBALLZ: Done creating module') if zipdata is None: display.debug('ANSIBALLZ: Reading module after lock') # Another process wrote the file while we were waiting for # the write lock. Go ahead and read the data from disk # instead of re-creating it. try: zipdata = open(cached_module_filename, 'rb').read() except IOError: raise AnsibleError( 'A different worker process failed to create module file. ' 'Look at traceback for that process for debugging information.' ) zipdata = to_text(zipdata, errors='surrogate_or_strict') shebang, interpreter = _get_shebang(u'/usr/bin/python', task_vars) if shebang is None: shebang = u'#!/usr/bin/python' # Enclose the parts of the interpreter in quotes because we're # substituting it into the template as a Python string interpreter_parts = interpreter.split(u' ') interpreter = u"'{0}'".format(u"', '".join(interpreter_parts)) now = datetime.datetime.utcnow() output.write( to_bytes(ACTIVE_ANSIBALLZ_TEMPLATE % dict( zipdata=zipdata, ansible_module=module_name, params=python_repred_params, shebang=shebang, interpreter=interpreter, coding=ENCODING_STRING, year=now.year, month=now.month, day=now.day, hour=now.hour, minute=now.minute, second=now.second, ))) b_module_data = output.getvalue() elif module_substyle == 'powershell': # Powershell/winrm don't actually make use of shebang so we can # safely set this here. If we let the fallback code handle this # it can fail in the presence of the UTF8 BOM commonly added by # Windows text editors shebang = u'#!powershell' # powershell wrapper build is currently handled in build_windows_module_payload, called in action # _configure_module after this function returns. elif module_substyle == 'jsonargs': module_args_json = to_bytes(json.dumps(module_args)) # these strings could be included in a third-party module but # officially they were included in the 'basic' snippet for new-style # python modules (which has been replaced with something else in # ansiballz) If we remove them from jsonargs-style module replacer # then we can remove them everywhere. python_repred_args = to_bytes(repr(module_args_json)) b_module_data = b_module_data.replace(REPLACER_VERSION, to_bytes(repr(__version__))) b_module_data = b_module_data.replace(REPLACER_COMPLEX, python_repred_args) b_module_data = b_module_data.replace( REPLACER_SELINUX, to_bytes(','.join(C.DEFAULT_SELINUX_SPECIAL_FS))) # The main event -- substitute the JSON args string into the module b_module_data = b_module_data.replace(REPLACER_JSONARGS, module_args_json) facility = b'syslog.' + to_bytes(task_vars.get( 'ansible_syslog_facility', C.DEFAULT_SYSLOG_FACILITY), errors='surrogate_or_strict') b_module_data = b_module_data.replace(b'syslog.LOG_USER', facility) return (b_module_data, module_style, shebang)
def write_response(self, socket_file, data): buf = BytesIO() self._write(buf, data) buf.seek(0) socket_file.write(buf.getvalue()) socket_file.flush()
class wsgi_fake_socket: """ Handle HTTP traffic and stuff into a WSGI application object instead. Note that this class assumes: 1. 'makefile' is called (by the response class) only after all of the data has been sent to the socket by the request class; 2. non-persistent (i.e. non-HTTP/1.1) connections. """ def __init__(self, app, host, port, script_name): self.app = app # WSGI app object self.host = host self.port = port self.script_name = script_name # SCRIPT_NAME (app mount point) self.inp = BytesIO() # stuff written into this "socket" self.write_results = [] # results from the 'write_fn' self.results = None # results from running the app self.output = BytesIO() # all output from the app, incl headers def makefile(self, *args, **kwargs): """ 'makefile' is called by the HTTPResponse class once all of the data has been written. So, in this interceptor class, we need to: 1. build a start_response function that grabs all the headers returned by the WSGI app; 2. create a wsgi.input file object 'inp', containing all of the traffic; 3. build an environment dict out of the traffic in inp; 4. run the WSGI app & grab the result object; 5. concatenate & return the result(s) read from the result object. """ # dynamically construct the start_response function for no good reason. def start_response(status, headers, exc_info=None): # construct the HTTP request. status = "HTTP/1.0 " + status + "\n" self.output.write(status.encode('utf-8')) for k, v in headers: header_line = '%s: %s\n' % ( k, v, ) self.output.write(header_line.encode('utf-8')) self.output.write(b'\n') def write_fn(s): if isinstance(s, str): s = s.encode('utf-8') self.write_results.append(s) return write_fn # construct the wsgi.input file from everything that's been # written to this "socket". inp = BytesIO(self.inp.getvalue()) # build the environ dictionary. environ = make_environ(inp, self.host, self.port, self.script_name) # run the application. app_result = self.app(environ, start_response) self.result = iter(app_result) ### # read all of the results. the trick here is to get the *first* # bit of data from the app via the generator, *then* grab & return # the data passed back from the 'write' function, and then return # the generator data. this is because the 'write' fn doesn't # necessarily get called until the first result is requested from # the app function. # # see twill tests, 'test_wrapper_intercept' for a test that breaks # if this is done incorrectly. try: generator_data = None try: generator_data = next(self.result).encode('utf-8') finally: for data in self.write_results: self.output.write(data) if generator_data: self.output.write(generator_data) while 1: data = next(self.result) self.output.write(data) except StopIteration: pass if hasattr(app_result, 'close'): app_result.close() if debuglevel >= 2: print(("***", self.output.getvalue(), "***")) # return the concatenated results. return BytesIO(self.output.getvalue()) def sendall(self, str): """ Save all the traffic to self.inp. """ if debuglevel >= 2: print((">>>", str, ">>>")) self.inp.write(str) def close(self): "Do nothing, for now." pass
def __init__(self, data=None, *args, **kwargs): self.sample_width = kwargs.pop("sample_width", None) self.frame_rate = kwargs.pop("frame_rate", None) self.channels = kwargs.pop("channels", None) audio_params = (self.sample_width, self.frame_rate, self.channels) if isinstance(data, array.array): try: data = data.tobytes() except: data = data.tostring() # prevent partial specification of arguments if any(audio_params) and None in audio_params: raise MissingAudioParameter("Either all audio parameters or no parameter must be specified") # all arguments are given elif self.sample_width is not None: if len(data) % (self.sample_width * self.channels) != 0: raise ValueError("data length must be a multiple of '(sample_width * channels)'") self.frame_width = self.channels * self.sample_width self._data = data # keep support for 'metadata' until audio params are used everywhere elif kwargs.get('metadata', False): # internal use only self._data = data for attr, val in kwargs.pop('metadata').items(): setattr(self, attr, val) else: # normal construction try: data = data if isinstance(data, (basestring, bytes)) else data.read() except(OSError): d = b'' reader = data.read(2 ** 31 - 1) while reader: d += reader reader = data.read(2 ** 31 - 1) data = d wav_data = read_wav_audio(data) if not wav_data: raise CouldntDecodeError("Couldn't read wav audio from data") self.channels = wav_data.channels self.sample_width = wav_data.bits_per_sample // 8 self.frame_rate = wav_data.sample_rate self.frame_width = self.channels * self.sample_width self._data = wav_data.raw_data # Convert 24-bit audio to 32-bit audio. # (stdlib audioop and array modules do not support 24-bit data) if self.sample_width == 3: byte_buffer = BytesIO() # Workaround for python 2 vs python 3. _data in 2.x are length-1 strings, # And in 3.x are ints. pack_fmt = 'BBB' if isinstance(self._data[0], int) else 'ccc' # This conversion maintains the 24 bit values. The values are # not scaled up to the 32 bit range. Other conversions could be # implemented. i = iter(self._data) padding = {False: b'\x00', True: b'\xFF'} for b0, b1, b2 in izip(i, i, i): byte_buffer.write(padding[b2 > b'\x7f'[0]]) old_bytes = struct.pack(pack_fmt, b0, b1, b2) byte_buffer.write(old_bytes) self._data = byte_buffer.getvalue() self.sample_width = 4 self.frame_width = self.channels * self.sample_width super(AudioSegment, self).__init__(*args, **kwargs)
from io import BytesIO # 操作二进制数据 f = BytesIO() f.write('中文'.encode('utf-8')) print(f.getvalue())
class Packer(object): """ MessagePack Packer usage: packer = Packer() astream.write(packer.pack(a)) astream.write(packer.pack(b)) Packer's constructor has some keyword arguments: :param callable default: Convert users type to builtin type that Packer supports. See also simplejson's document. :param bool use_single_float: Use single precision float type for float. (default: False) :param bool autoreset: Reset buffer after each pack and return its content as `bytes`. (default: True). If set this to false, use `bytes()` to get content and `.reset()` to clear buffer. :param bool use_bin_type: Use bin type introduced in msgpack spec 2.0 for bytes. It also enables str8 type for unicode. :param bool strict_types: If set to true, types will be checked to be exact. Derived classes from serializeable types will not be serialized and will be treated as unsupported type and forwarded to default. Additionally tuples will not be serialized as lists. This is useful when trying to implement accurate serialization for python types. :param str encoding: (deprecated) Convert unicode to bytes with this encoding. (default: 'utf-8') :param str unicode_errors: Error handler for encoding unicode. (default: 'strict') """ def __init__(self, default=None, encoding=None, unicode_errors=None, use_single_float=False, autoreset=True, use_bin_type=False, strict_types=False): if encoding is None: encoding = 'utf_8' else: warnings.warn( "encoding is deprecated, Use raw=False instead.", PendingDeprecationWarning) if unicode_errors is None: unicode_errors = 'strict' self._strict_types = strict_types self._use_float = use_single_float self._autoreset = autoreset self._use_bin_type = use_bin_type self._encoding = encoding self._unicode_errors = unicode_errors self._buffer = StringIO() if default is not None: if not callable(default): raise TypeError("default must be callable") self._default = default def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT, check=isinstance, check_type_strict=_check_type_strict): default_used = False if self._strict_types: check = check_type_strict list_types = list else: list_types = (list, tuple) while True: if nest_limit < 0: raise PackValueError("recursion limit exceeded") if obj is None: return self._buffer.write(b"\xc0") if check(obj, bool): if obj: return self._buffer.write(b"\xc3") return self._buffer.write(b"\xc2") if check(obj, int_types): if 0 <= obj < 0x80: return self._buffer.write(struct.pack("B", obj)) if -0x20 <= obj < 0: return self._buffer.write(struct.pack("b", obj)) if 0x80 <= obj <= 0xff: return self._buffer.write(struct.pack("BB", 0xcc, obj)) if -0x80 <= obj < 0: return self._buffer.write(struct.pack(">Bb", 0xd0, obj)) if 0xff < obj <= 0xffff: return self._buffer.write(struct.pack(">BH", 0xcd, obj)) if -0x8000 <= obj < -0x80: return self._buffer.write(struct.pack(">Bh", 0xd1, obj)) if 0xffff < obj <= 0xffffffff: return self._buffer.write(struct.pack(">BI", 0xce, obj)) if -0x80000000 <= obj < -0x8000: return self._buffer.write(struct.pack(">Bi", 0xd2, obj)) if 0xffffffff < obj <= 0xffffffffffffffff: return self._buffer.write(struct.pack(">BQ", 0xcf, obj)) if -0x8000000000000000 <= obj < -0x80000000: return self._buffer.write(struct.pack(">Bq", 0xd3, obj)) if not default_used and self._default is not None: obj = self._default(obj) default_used = True continue raise PackOverflowError("Integer value out of range") if check(obj, (bytes, bytearray)): n = len(obj) if n >= 2**32: raise PackValueError("%s is too large" % type(obj).__name__) self._pack_bin_header(n) return self._buffer.write(obj) if check(obj, Unicode): if self._encoding is None: raise TypeError( "Can't encode unicode string: " "no encoding is specified") obj = obj.encode(self._encoding, self._unicode_errors) n = len(obj) if n >= 2**32: raise PackValueError("String is too large") self._pack_raw_header(n) return self._buffer.write(obj) if check(obj, memoryview): n = len(obj) * obj.itemsize if n >= 2**32: raise PackValueError("Memoryview is too large") self._pack_bin_header(n) return self._buffer.write(obj) if check(obj, float): if self._use_float: return self._buffer.write(struct.pack(">Bf", 0xca, obj)) return self._buffer.write(struct.pack(">Bd", 0xcb, obj)) if check(obj, ExtType): code = obj.code data = obj.data assert isinstance(code, int) assert isinstance(data, bytes) L = len(data) if L == 1: self._buffer.write(b'\xd4') elif L == 2: self._buffer.write(b'\xd5') elif L == 4: self._buffer.write(b'\xd6') elif L == 8: self._buffer.write(b'\xd7') elif L == 16: self._buffer.write(b'\xd8') elif L <= 0xff: self._buffer.write(struct.pack(">BB", 0xc7, L)) elif L <= 0xffff: self._buffer.write(struct.pack(">BH", 0xc8, L)) else: self._buffer.write(struct.pack(">BI", 0xc9, L)) self._buffer.write(struct.pack("b", code)) self._buffer.write(data) return if check(obj, list_types): n = len(obj) self._pack_array_header(n) for i in xrange(n): self._pack(obj[i], nest_limit - 1) return if check(obj, dict): return self._pack_map_pairs(len(obj), dict_iteritems(obj), nest_limit - 1) if not default_used and self._default is not None: obj = self._default(obj) default_used = 1 continue raise TypeError("Cannot serialize %r" % (obj, )) def pack(self, obj): try: self._pack(obj) except: self._buffer = StringIO() # force reset raise ret = self._buffer.getvalue() if self._autoreset: self._buffer = StringIO() elif USING_STRINGBUILDER: self._buffer = StringIO(ret) return ret def pack_map_pairs(self, pairs): self._pack_map_pairs(len(pairs), pairs) ret = self._buffer.getvalue() if self._autoreset: self._buffer = StringIO() elif USING_STRINGBUILDER: self._buffer = StringIO(ret) return ret def pack_array_header(self, n): if n >= 2**32: raise PackValueError self._pack_array_header(n) ret = self._buffer.getvalue() if self._autoreset: self._buffer = StringIO() elif USING_STRINGBUILDER: self._buffer = StringIO(ret) return ret def pack_map_header(self, n): if n >= 2**32: raise PackValueError self._pack_map_header(n) ret = self._buffer.getvalue() if self._autoreset: self._buffer = StringIO() elif USING_STRINGBUILDER: self._buffer = StringIO(ret) return ret def pack_ext_type(self, typecode, data): if not isinstance(typecode, int): raise TypeError("typecode must have int type.") if not 0 <= typecode <= 127: raise ValueError("typecode should be 0-127") if not isinstance(data, bytes): raise TypeError("data must have bytes type") L = len(data) if L > 0xffffffff: raise PackValueError("Too large data") if L == 1: self._buffer.write(b'\xd4') elif L == 2: self._buffer.write(b'\xd5') elif L == 4: self._buffer.write(b'\xd6') elif L == 8: self._buffer.write(b'\xd7') elif L == 16: self._buffer.write(b'\xd8') elif L <= 0xff: self._buffer.write(b'\xc7' + struct.pack('B', L)) elif L <= 0xffff: self._buffer.write(b'\xc8' + struct.pack('>H', L)) else: self._buffer.write(b'\xc9' + struct.pack('>I', L)) self._buffer.write(struct.pack('B', typecode)) self._buffer.write(data) def _pack_array_header(self, n): if n <= 0x0f: return self._buffer.write(struct.pack('B', 0x90 + n)) if n <= 0xffff: return self._buffer.write(struct.pack(">BH", 0xdc, n)) if n <= 0xffffffff: return self._buffer.write(struct.pack(">BI", 0xdd, n)) raise PackValueError("Array is too large") def _pack_map_header(self, n): if n <= 0x0f: return self._buffer.write(struct.pack('B', 0x80 + n)) if n <= 0xffff: return self._buffer.write(struct.pack(">BH", 0xde, n)) if n <= 0xffffffff: return self._buffer.write(struct.pack(">BI", 0xdf, n)) raise PackValueError("Dict is too large") def _pack_map_pairs(self, n, pairs, nest_limit=DEFAULT_RECURSE_LIMIT): self._pack_map_header(n) for (k, v) in pairs: self._pack(k, nest_limit - 1) self._pack(v, nest_limit - 1) def _pack_raw_header(self, n): if n <= 0x1f: self._buffer.write(struct.pack('B', 0xa0 + n)) elif self._use_bin_type and n <= 0xff: self._buffer.write(struct.pack('>BB', 0xd9, n)) elif n <= 0xffff: self._buffer.write(struct.pack(">BH", 0xda, n)) elif n <= 0xffffffff: self._buffer.write(struct.pack(">BI", 0xdb, n)) else: raise PackValueError('Raw is too large') def _pack_bin_header(self, n): if not self._use_bin_type: return self._pack_raw_header(n) elif n <= 0xff: return self._buffer.write(struct.pack('>BB', 0xc4, n)) elif n <= 0xffff: return self._buffer.write(struct.pack(">BH", 0xc5, n)) elif n <= 0xffffffff: return self._buffer.write(struct.pack(">BI", 0xc6, n)) else: raise PackValueError('Bin is too large') def bytes(self): return self._buffer.getvalue() def reset(self): self._buffer = StringIO()
def _pycurl_post(self, url, json=None, data=None, username="", password="", headers={}, timeout=30): """This function will POST to the url endpoint using pycurl. returning an AdyenResult object on 200 HTTP responce. Either json or data has to be provided. If username and password are provided, basic auth will be used. Args: url (str): url to send the POST json (dict, optional): Dict of the JSON to POST data (dict, optional): Dict, presumed flat structure of key/value of request to place username (str, optionl): Username for basic auth. Must be included as part of password. password (str, optional): Password for basic auth. Must be included as part of username. headers (dict, optional): Key/Value pairs of headers to include timeout (int, optional): Default 30. Timeout for the request. Returns: str: Raw response received str: Raw request placed int: HTTP status code, eg 200,404,401 dict: Key/Value pairs of the headers received. """ response_headers={} def handle_header(header_line): header_line = header_line.decode('iso-8859-1') if ':' in header_line: name, value = header_line.split(':', 1) name = name.strip() value = value.strip() response_headers[name] = value curl = pycurl.Curl() curl.setopt(curl.URL, url) if sys.version_info[0] >= 3: stringbuffer = BytesIO() else: stringbuffer = StringIO() #stringbuffer = StringIO() curl.setopt(curl.WRITEDATA, stringbuffer) # Add User-Agent header to request so that the request can be identified as coming # from the Adyen Python library. headers['User-Agent'] = self.user_agent # Convert the header dict to formatted array as pycurl needs. if sys.version_info[0] >= 3: header_list = ["%s:%s" % (k, v) for k, v in headers.items()] else: header_list = ["%s:%s" % (k, v) for k, v in headers.iteritems()] #Ensure proper content-type when adding headers if json: header_list.append("Content-Type:application/json") curl.setopt(pycurl.HTTPHEADER, header_list) # Return regular dict instead of JSON encoded dict for request: raw_store = json # Set the request body. raw_request = json_lib.dumps(json) if json else urlencode(data) curl.setopt(curl.POSTFIELDS, raw_request) if username and password: curl.setopt(curl.USERPWD, '%s:%s' % (username, password)) curl.setopt(curl.TIMEOUT, timeout) curl.perform() # Grab the response content result = stringbuffer.getvalue() status_code = curl.getinfo(curl.RESPONSE_CODE) curl.close() # Return regular dict instead of JSON encoded dict for request: raw_request = raw_store return result, raw_request, status_code, response_headers
last_image_url = user_info['entry_data']['ProfilePage'][0]['user'][ "media"]['nodes'][number]['thumbnail_src'] last_image_likes = user_info['entry_data']['ProfilePage'][0]['user'][ "media"]['nodes'][number]['likes']['count'] last_image_caption = user_info['entry_data']['ProfilePage'][0]['user'][ "media"]['nodes'][number]['caption'] last_image_comments = user_info['entry_data']['ProfilePage'][0][ 'user']["media"]['nodes'][number]['comments']['count'] response = requests.get(last_image_url) img = Image.open(BytesIO(response.content)) img.thumbnail((50, 50), Image.ANTIALIAS) buffered = BytesIO() img.save(buffered, format="JPEG") last_image_b64 = base64.b64encode(buffered.getvalue()) last_medias.append({ 'last_image_url': last_image_url, 'last_image_likes': last_image_likes, 'last_image_comments': last_image_comments, 'last_image_caption': last_image_caption, 'last_image_b64': last_image_b64 }) except: pass bio = user_info['entry_data']['ProfilePage'][0]['user']['biography'] uimage_url = user_info['entry_data']['ProfilePage'][0]['user'][ 'profile_pic_url']
def do_GET(self): self.send_response(200, "OK") self.end_headers() response = BytesIO() response.write(b"Hello world!") self.wfile.write(response.getvalue())
def test_process_response_force_recalculate_encoding(self): headers = { 'Content-Type': 'text/html', 'Content-Encoding': 'gzip', } f = BytesIO() plainbody = (b'<html><head><title>Some page</title>' b'<meta http-equiv="Content-Type" content="text/html; charset=gb2312">') zf = GzipFile(fileobj=f, mode='wb') zf.write(plainbody) zf.close() response = HtmlResponse("http;//www.example.com/page.html", headers=headers, body=f.getvalue()) request = Request("http://www.example.com/") newresponse = self.mw.process_response(request, response, self.spider) assert isinstance(newresponse, HtmlResponse) self.assertEqual(newresponse.body, plainbody) self.assertEqual(newresponse.encoding, resolve_encoding('gb2312'))
def dump_uvarint(value): writer = BytesIO() protobuf.dump_uvarint(writer, value) return writer.getvalue()
'https://www.baidu.com/', 'http://t.cn/aKln8T', 'https://blog.csdn.net/cityzenoldwang/article/details/78621337' ] for i in url: cobj.setopt(pycurl.URL, i) cobj.setopt(pycurl.CAINFO, certifi.where()) cobj.setopt(pycurl.CONNECTTIMEOUT, 10) cobj.setopt(pycurl.WRITEFUNCTION, str.write) # cobj.setopt(pycurl.VERBOSE, 1) # cobj.setopt(pycurl.POSTFIELDS,postfields) # cobj.setopt(pycurl.NOPROGRESS,0) # cobj.setopt(pycurl.HTTPHEADER, list_head) cobj.perform() print(str.getvalue()) #获取内容 print( cobj.getinfo(pycurl.HTTP_CODE), '\n', cobj.getinfo(pycurl.RESPONSE_CODE), '\n', cobj.getinfo(pycurl.EFFECTIVE_URL), '\n', # cobj.getinfo(pycurl.URL), '\n', # cobj.getinfo(pycurl.HTTPHEADER, list_head),'\n', # len(str.getvalue()) ) str.close() cobj.close()
def bg_report(request): try: f_i = request.POST['inicio'] f_f = request.POST['fin'] filtroF = Transaccion.objects.filter(fecha__range=(f_i, f_f)) except: messages.error(request, 'fechas con formato incorrecto') return redirect('report_index') if request.POST: filtroI = Transaccion.objects.filter(fecha__lt=f_i) # <--- filtra el valor previo ctas = reset_subcuentas_now() if filtroI: for T in filtroI: for ct in ctas: if T.tipo_transaccion == 0: if ct == T.cargo: ct.debe += float(T.monto) else: if ct == T.abono: ct.haber += float(T.monto) else: if T.tipo_transaccion == 1: if ct == T.cargo: ct.debe += float(T.monto_contado) else: if ct == T.cargo_alt: ct.debe += float(T.monto_credito) else: if ct == T.abono: ct.haber += float(T.monto) else: if ct == T.cargo: ct.debe += float(T.monto) else: if ct == T.abono: ct.haber += float(T.monto_contado) else: if ct == T.abono_alt: ct.haber += float(T.monto_credito) ct.saldo = float(abs(ct.debe - ct.haber)) for T in filtroF: for ct in ctas: if T.tipo_transaccion == 0: if ct == T.cargo: ct.debe += float(T.monto) else: if ct == T.abono: ct.haber += float(T.monto) else: if T.tipo_transaccion == 1: if ct == T.cargo: ct.debe += float(T.monto_contado) else: if ct == T.cargo_alt: ct.debe += float(T.monto_credito) else: if ct == T.abono: ct.haber += float(T.monto) else: if ct == T.cargo: ct.debe += float(T.monto) else: if ct == T.abono: ct.haber += float(T.monto_contado) else: if ct == T.abono_alt: ct.haber += float(T.monto_credito) ct.saldo = float(abs(ct.debe - ct.haber)) response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename=balance_general_{}_{}.pdf'.format(f_i, f_f) buffer = BytesIO() cv = canvas.Canvas(buffer, pagesize=A4) # encabezados cv.setFont('Helvetica-Bold', 12) cv.drawString(220, 750, 'EMPRESA RECITEC') cv.drawString(150, 735, 'balance general del {} al {}'.format(f_i, f_f)) cv.drawString(30,715,'cuenta') cv.drawString(360, 715, 'activos') cv.drawString(460, 715, 'participaciones') a = 30 b = 700 c = 360 d = 460 conteo = 0 t_debe = 0 t_haber = 0.00 cv.line(a, b, 550, b, ) b -= 15 cv.setFillColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setStrokeColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setFont('Helvetica-Bold', 42) cv.line(130, 443, 450, 443, ) cv.line(130, 442, 450, 442, ) cv.drawString(130, 400, 'CONFIDENCIAL') cv.line(130, 390, 450, 390, ) cv.line(130, 389, 450, 389, ) cv.setFillColorRGB(0, 0, 0) for ct in ctas: if conteo == 44: cv.showPage() conteo = 0 cv.setFillColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setStrokeColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setFont('Helvetica-Bold', 42) cv.line(130, 443, 450, 443, ) cv.line(130, 442, 450, 442, ) cv.drawString(130, 400, 'CONFIDENCIAL') cv.line(130, 390, 450, 390, ) cv.line(130, 389, 450, 389, ) cv.setFillColorRGB(0, 0, 0) b = 715 cv.setFont('Helvetica', 12) if ct.saldo > 0: if ct.padre.rubro.tipo.codigo == '1': cv.drawString(a, b, ct.nombre) cv.drawString(c, b, ct.saldo.__str__()) b -= 25 t_debe += ct.saldo else: if ct.padre.rubro.tipo.codigo == '2' or ct.padre.rubro.tipo.codigo == '3': cv.drawString(a, b, ct.nombre) cv.drawString(d, b, ct.saldo.__str__()) b -= 25 t_haber += ct.saldo conteo += 1 cv.setFillColorRGB(0, 0, 0) cv.setStrokeColorRGB(0, 0, 0) cv.line(a, b, 550, b, ) b -= 15 cv.drawString(a, b, 'totales') cv.drawString(c, b, t_debe.__str__()) cv.drawString(d, b, t_haber.__str__()) cv.setFont('Helvetica', 12) fecha = time.strftime("%d/%m/%y") hora = time.strftime("%H:%M:%S") cv.drawString(30, 30, '{} {} {} {} {} {}'.format('balance general generado por ', request.user.username, 'el', fecha, ' a las ', hora)) cv.save() pdf = buffer.getvalue() buffer.close() response.write(pdf) return response else: if filtroF: for T in filtroF: for ct in ctas: if T.tipo_transaccion == 0: if ct == T.cargo: ct.debe += float(T.monto) else: if ct == T.abono: ct.haber += float(T.monto) else: if T.tipo_transaccion == 1: if ct == T.cargo: ct.debe += float(T.monto_contado) else: if ct == T.cargo_alt: ct.debe += float(T.monto_credito) else: if ct == T.abono: ct.haber += float(T.monto) else: if ct == T.cargo: ct.debe += float(T.monto) else: if ct == T.abono: ct.haber += float(T.monto_contado) else: if ct == T.abono_alt: ct.haber += float(T.monto_credito) ct.saldo = float(abs(ct.debe - ct.haber)) response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename=balance_general_{}_{}.pdf'.format(f_i,f_f) buffer = BytesIO() cv = canvas.Canvas(buffer, pagesize=A4) #encabezados cv.setFont('Helvetica-Bold', 12) cv.drawString(220, 750, 'EMPRESA RECITEC') cv.drawString(150, 735, 'balance general del {} al {}'.format(f_i, f_f)) cv.drawString(30, 715, 'cuenta') cv.drawString(360, 715, 'activos') cv.drawString(460, 715, 'participaciones') a = 30 b = 700 c = 360 d = 460 conteo = 0 t_debe = 0 t_haber = 0.00 cv.line(a,b,550, b, ) b -=15 cv.setFillColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setStrokeColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setFont('Helvetica-Bold', 42) cv.line(130, 443, 450, 443, ) cv.line(130, 442, 450, 442, ) cv.drawString(130, 400, 'CONFIDENCIAL') cv.line(130, 390, 450, 390, ) cv.line(130, 389, 450, 389, ) cv.setFillColorRGB(0, 0, 0) for ct in ctas: if conteo == 44: cv.showPage() conteo = 0 cv.setFillColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setStrokeColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setFont('Helvetica-Bold', 42) cv.line(130, 443, 450, 443, ) cv.line(130, 442, 450, 442, ) cv.drawString(130, 400, 'CONFIDENCIAL') cv.line(130, 390, 450, 390, ) cv.line(130, 389, 450, 389, ) cv.setFillColorRGB(0, 0, 0) b = 715 cv.setFont('Helvetica',12) if ct.saldo > 0: if ct.padre.rubro.tipo.codigo == '1': cv.drawString(a, b, ct.nombre) cv.drawString(c, b, ct.saldo.__str__()) b -= 25 t_debe += ct.saldo else: if ct.padre.rubro.tipo.codigo == '2' or ct.padre.rubro.tipo.codigo == '3': cv.drawString(a, b, ct.nombre) cv.drawString(d, b, ct.saldo.__str__()) b -= 25 t_haber += ct.saldo conteo +=1 utilidades = SubCuenta.objects.get(codigo='320101') t_haber += utilidades.haber cv.drawString(a, b, utilidades.nombre) cv.drawString(d, b, utilidades.saldo.__str__()) b -= 25 cv.setFillColorRGB(0, 0, 0) cv.setStrokeColorRGB(0, 0, 0) cv.line(a, b, 550, b, ) b -= 15 cv.drawString(a,b,'totales') cv.drawString(c,b,t_debe.__str__()) cv.drawString(d,b,t_haber.__str__()) cv.setFont('Helvetica', 12) fecha = time.strftime("%d/%m/%y") hora = time.strftime("%H:%M:%S") cv.drawString(30, 30, '{} {} {} {} {} {}'.format('balance general generado por ', request.user.username, 'el', fecha,' a las ', hora)) cv.save() pdf = buffer.getvalue() buffer.close() response.write(pdf) return response else: messages.error(request, 'lo sentimos pero no existen transaciones para el periodo ingresado') return redirect('report_index')
def dump_message(msg): writer = BytesIO() protobuf.dump_message(writer, msg) return writer.getvalue()
def _prepare_thumbnail(im): im.thumbnail((100, 100), Image.ANTIALIAS) buffer = BytesIO() im = im.convert('RGB') im.save(buffer, format="JPEG") return base64.b64encode(buffer.getvalue()).decode('utf-8')
def tipos_repot(request): tipos = Tipo.objects.all() response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename=clasificacion_general.pdf' buffer = BytesIO() cv = canvas.Canvas(buffer, pagesize=A4) # generacion de encabezados del pdf # dragString(px izq a derecha, px de abajo hacia arriba, string que queremos dibujar) cv.setLineWidth(.3) cv.setFont('Helvetica-Bold', 22) cv.drawString(30, 750, 'SIC115 Reporte') cv.setFont('Helvetica', 12) cv.drawString(30, 735, 'Clasificacion General') cv.setFont('Helvetica-Bold', 12) # generando contenido del pdf a = 30 b = 645 c = 390 d = 460 cv.setFont('Helvetica-Bold', 14) cv.drawString(a, b, 'nombre') cv.setFont('Helvetica-Bold', 14) cv.drawString(c, b, 'saldo') cv.setFont('Helvetica-Bold', 14) cv.drawString(d, b, 'tipo de saldo') b -= 25 c += 10 d += 10 # marca de agua cv.setFillColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setStrokeColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setFont('Helvetica-Bold', 42) cv.line(130, 443, 450, 443, ) cv.line(130, 442, 450, 442, ) cv.drawString(130, 400, 'CONFIDENCIAL') cv.line(130, 390, 450, 390, ) cv.line(130, 389, 450, 389, ) cv.setFillColorRGB(0, 0, 0) for tipo in tipos: cv.setFont('Helvetica', 12) cv.drawString(a, b, tipo.nombre.__str__()) cv.setFont('Helvetica', 12) cv.drawString(c, b, tipo.saldo.__str__()) if tipo.debe > tipo.haber: cv.setFont('Helvetica', 12) cv.drawString(d, b, 'saldo deudor') else: if tipo.debe < tipo.haber: cv.setFont('Helvetica', 12) cv.drawString(d, b, 'saldo acreedor') else: cv.setFont('Helvetica', 12) cv.drawString(d, b, 'saldo cero') b -= 25 cv.setFont('Helvetica', 12) fecha = time.strftime("%d/%m/%y") hora = time.strftime("%H:%M:%S") cv.drawString(30, 30, '{} {} {} {} {} {}'.format('reporte generado por ', request.user.username, 'el', fecha, ' a las ', hora)) cv.save() pdf = buffer.getvalue() buffer.close() response.write(pdf) return response
def doit(num_ins, num_outs, M, keys, fee=10000, outvals=None, segwit_in=False, outstyles=['p2pkh'], change_outputs=[], incl_xpubs=False, hack_change_out=False): psbt = BasicPSBT() txn = Tx(2, [], []) if incl_xpubs: # add global header with XPUB's # - assumes BIP45 for xfp, m, sk in keys: kk = pack('<II', xfp, 45 | 0x80000000) psbt.xpubs.append((sk.serialize(as_private=False), kk)) psbt.inputs = [BasicPSBTInput(idx=i) for i in range(num_ins)] psbt.outputs = [BasicPSBTOutput(idx=i) for i in range(num_outs)] for i in range(num_ins): # make a fake txn to supply each of the inputs # - each input is 1BTC # addr where the fake money will be stored. addr, scriptPubKey, script, details = make_ms_address(M, keys, idx=i) # lots of supporting details needed for p2sh inputs if segwit_in: psbt.inputs[i].witness_script = script else: psbt.inputs[i].redeem_script = script for pubkey, xfp_path in details: psbt.inputs[i].bip32_paths[pubkey] = b''.join( pack('<I', j) for j in xfp_path) # UTXO that provides the funding for to-be-signed txn supply = Tx(2, [TxIn(pack('4Q', 0xdead, 0xbeef, 0, 0), 73)], []) supply.txs_out.append(TxOut(1E8, scriptPubKey)) with BytesIO() as fd: if not segwit_in: supply.stream(fd) psbt.inputs[i].utxo = fd.getvalue() else: supply.txs_out[-1].stream(fd) psbt.inputs[i].witness_utxo = fd.getvalue() spendable = TxIn(supply.hash(), 0) txn.txs_in.append(spendable) for i in range(num_outs): # random P2PKH if not outstyles: style = ADDR_STYLES[i % len(ADDR_STYLES)] else: style = outstyles[i % len(outstyles)] if i in change_outputs: make_redeem_args = dict() if hack_change_out: make_redeem_args = hack_change_out(i) addr, scriptPubKey, scr, details = \ make_ms_address(M, keys, idx=i, addr_fmt=unmap_addr_fmt[style], **make_redeem_args) for pubkey, xfp_path in details: psbt.outputs[i].bip32_paths[pubkey] = b''.join( pack('<I', j) for j in xfp_path) if 'w' in style: psbt.outputs[i].witness_script = scr if style.endswith('p2sh'): psbt.outputs[i].redeem_script = b'\0\x20' + sha256( scr).digest() elif style.endswith('sh'): psbt.outputs[i].redeem_script = scr else: scr = fake_dest_addr(style) assert scr if not outvals: h = TxOut(round(((1E8 * num_ins) - fee) / num_outs, 4), scriptPubKey) else: h = TxOut(outvals[i], scriptPubKey) txn.txs_out.append(h) with BytesIO() as b: txn.stream(b) psbt.txn = b.getvalue() rv = BytesIO() psbt.serialize(rv) assert rv.tell() <= MAX_TXN_LEN, 'too fat' return rv.getvalue()
def diario_report(request): if request.POST: try: f_i = request.POST['inicio'] f_f = request.POST['fin'] query = Transaccion.objects.filter(fecha__range=(f_i,f_f)) except: messages.error(request,'fechas con formato incorrecto') return redirect('report_index') if query: response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename=libro_diario.pdf' buffer = BytesIO() cv = canvas.Canvas(buffer, pagesize=A4) cv.setFont('Helvetica-Bold', 12) cv.drawString(220, 750, 'EMPRESA RECITEC') cv.drawString(150, 735, 'registro de transacciones del {} al {}'.format(f_i, f_f)) a=30 b=700 c=260 d=460 conteo =0 for T in query: if conteo == 44: cv.showPage() conteo = 0 cv.setFont('Helvetica-Bold', 12) cv.drawString(30, 710, 'fecha') cv.drawString(120, 710, 'descripcion') cv.drawString(450, 710, 'monto') cv.setFillColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setStrokeColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setFont('Helvetica-Bold', 42) cv.line(130, 443, 450, 443, ) cv.line(130, 442, 450, 442, ) cv.drawString(130, 400, 'CONFIDENCIAL') cv.line(130, 390, 450, 390, ) cv.line(130, 389, 450, 389, ) cv.setFillColorRGB(0, 0, 0) b = 735 cv.setFont('Helvetica', 12) cv.drawString(a,b,T.fecha.__str__()) cv.drawString(c,b,T.descripcion) cv.drawString(d,b,T.monto.__str__()) b -=25 conteo +=1 fecha = time.strftime("%d/%m/%y") hora = time.strftime("%H:%M:%S") string = '{} {} {} {} {} {}'.format('reporte generado por ', request.user.username, 'el', fecha,' a las ',hora) cv.drawString(30, 30, string) cv.setFillColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setStrokeColorRGB(0.92578125, 0.92578125, 0.92578125) cv.setFont('Helvetica-Bold', 42) cv.line(130, 443, 450, 443, ) cv.line(130, 442, 450, 442, ) cv.drawString(130, 400, 'CONFIDENCIAL') cv.line(130, 390, 450, 390, ) cv.line(130, 389, 450, 389, ) cv.save() pdf = buffer.getvalue() buffer.close() response.write(pdf) return response else: messages.error(request,'no se encontraron transacciones para el periodo del {} al {}'.format(f_i,f_f)) return redirect('report_index')