def __init__(self, sasl_client_factory, mechanism, trans): """ @param sasl_client_factory: a callable that returns a new sasl.Client object @param mechanism: the SASL mechanism (e.g. "GSSAPI") @param trans: the underlying transport over which to communicate. """ self._trans = trans self.sasl_client_factory = sasl_client_factory self.sasl = None self.mechanism = mechanism self.__wbuf = BufferIO() self.__rbuf = BufferIO() self.opened = False self.encode = None
def generate_file(template, destination, context): base_name = template.split('/')[-1] base_name_components = base_name.split('.') # Remove the last base name component if it's .em if base_name_components[-1] == 'em': base_name_components.pop(-1) package_name, type_name = get_type_components(context['spec'].full_name) context['package'] = package_name context['type'] = type_name # Add the message type name to the source file base_name_components[0] = base_name_components[0] + '__' + type_name filename = '.'.join(base_name_components) output_file_path = '/'.join([destination, filename]) output_buffer = BufferIO() interpreter = em.Interpreter(output=output_buffer, globals=copy.deepcopy(context)) interpreter.file(open(template, 'r')) if not os.path.exists(destination): os.makedirs(destination) with open(output_file_path, 'w') as file: file.write(output_buffer.getvalue())
def test_cell_border(self): # create odt document thin_value = '0.001in solid #ff0000' bold_value = '0.01in solid #00ff00' filename = os.path.join(self.dirname, "test.ods") doc = ooolib.Calc("test_cell") doc.set_cell_property('border', thin_value) doc.set_cell_value(2, 2, "string", "Name") doc.set_cell_property('border', bold_value) doc.set_cell_value(2, 4, "string", "Value") doc.save(filename) # check created document handle = zipfile.ZipFile(filename) xdoc = etree.parse(BufferIO(handle.read('content.xml'))) style_definition = xdoc.xpath( '//style:table-cell-properties[@fo:border="%s"]/../@style:name' % thin_value, namespaces=self.namespaces) cell_style = xdoc.xpath('//text:p[text()="Name"]/../@table:style-name', namespaces=self.namespaces) self.assertEqual(cell_style, style_definition) style_definition = xdoc.xpath( '//style:table-cell-properties[@fo:border="%s"]/../@style:name' % bold_value, namespaces=self.namespaces) cell_style = xdoc.xpath( '//text:p[text()="Value"]/../@table:style-name', namespaces=self.namespaces) self.assertEqual(cell_style, style_definition)
def _handle_body(body): if isinstance(body, six.text_type): body = body.encode("utf-8") if isinstance(body, _io.BufferedReader): return body return BufferIO(body)
def is_valid_image(self, raw_data): buffer = BufferIO(raw_data) try: trial_image = Image.open(buffer) trial_image.verify() except Exception: return False return True
def cstringio_refill(self, prefix, reqlen): # self.__rbuf will already be empty here because fastbinary doesn't # ask for a refill until the previous buffer is empty. Therefore, # we can start reading new frames immediately. while len(prefix) < reqlen: self._read_frame() prefix += self.__rbuf.getvalue() self.__rbuf = BufferIO(prefix) return self.__rbuf
def test_http_client_get_html_successful_connection_to_server(session_factory): expected_page_html = '<html>website</html>' expected_response = Response() expected_response.status_code = 200 expected_response.encoding = 'utf-8' expected_response.raw = BufferIO(expected_page_html.encode('utf-8')) session = session_factory(return_value=expected_response) http_client = HTTPClient(base_url='https://www.example.com', session=session) page_html = http_client.get_html(params={'page': 0}) assert expected_page_html == page_html
def test_cell_padding(self): # create odt document filename = os.path.join(self.dirname, "test.ods") doc = ooolib.Calc("test_cell") doc.set_cell_property('padding-left', '0.1in') doc.set_cell_value(2, 2, "string", "Left") doc.set_cell_property('padding-left', False) doc.set_cell_property('padding-right', '0.2in') doc.set_cell_value(3, 2, "string", "Right") doc.set_cell_property('padding-right', False) doc.set_cell_property('padding', '0.3in') doc.set_cell_value(4, 2, "string", "Full") doc.set_cell_property('padding', False) doc.set_cell_value(5, 2, "string", "No-padding") doc.save(filename) # check created document handle = zipfile.ZipFile(filename) xdoc = etree.parse(BufferIO(handle.read('content.xml'))) style_definition = xdoc.xpath( '//style:table-cell-properties[@fo:padding-left="0.1in"]/../@style:name', namespaces=self.namespaces) cell_style = xdoc.xpath('//text:p[text()="Left"]/../@table:style-name', namespaces=self.namespaces) self.assertEqual(cell_style, style_definition) style_definition = xdoc.xpath( '//style:table-cell-properties[@fo:padding-right="0.2in"]/../@style:name', namespaces=self.namespaces) cell_style = xdoc.xpath( '//text:p[text()="Right"]/../@table:style-name', namespaces=self.namespaces) self.assertEqual(cell_style, style_definition) style_definition = xdoc.xpath( '//style:table-cell-properties[@fo:padding="0.3in"]/../@style:name', namespaces=self.namespaces) cell_style = xdoc.xpath('//text:p[text()="Full"]/../@table:style-name', namespaces=self.namespaces) self.assertEqual(cell_style, style_definition) cell_style = xdoc.xpath( '//text:p[text()="No-padding"]/../@table:style-name', namespaces=self.namespaces) self.assertEqual(cell_style, [])
def _read_frame(self): header = self._trans.read(4) (length, ) = struct.unpack(">I", header) if self.encode: # If the frames are encoded (i.e. you're using a QOP of auth-int or # auth-conf), then make sure to include the header in the bytes you send to # sasl.decode() encoded = header + self._trans.read(length) success, decoded = self.sasl.decode(encoded) if not success: raise TTransportException(type=TTransportException.UNKNOWN, message=self.sasl.getError()) else: # If the frames are not encoded, just pass it through decoded = self._trans.read(length) self.__rbuf = BufferIO(decoded)
def test_cell_hyphenate(self): # create odt document filename = os.path.join(self.dirname, "test.ods") doc = ooolib.Calc("test_cell") doc.set_cell_property('hyphenate', True) doc.set_cell_value(2, 2, "string", "Name") doc.save(filename) # check created document handle = zipfile.ZipFile(filename) xdoc = etree.parse(BufferIO(handle.read('content.xml'))) style_definition = xdoc.xpath( '//style:text-properties[@fo:hyphenate="true"]/../@style:name', namespaces=self.namespaces) cell_style = xdoc.xpath('//text:p[text()="Name"]/../@table:style-name', namespaces=self.namespaces) self.assertEqual(cell_style, style_definition)
def flush(self): buffer = self.__wbuf.getvalue() # The first time we flush data, we send it to sasl.encode() # If the length doesn't change, then we must be using a QOP # of auth and we should no longer call sasl.encode(), otherwise # we encode every time. if self.encode == None: success, encoded = self.sasl.encode(buffer) if not success: raise TTransportException(type=TTransportException.UNKNOWN, message=self.sasl.getError()) if (len(encoded) == len(buffer)): self.encode = False self._flushPlain(buffer) else: self.encode = True self._trans.write(encoded) elif self.encode: self._flushEncoded(buffer) else: self._flushPlain(buffer) self._trans.flush() self.__wbuf = BufferIO()
def get_image(self, source): buffer = BufferIO(source.read()) return Image.open(buffer)
def process_image(f, user_id): buf = BufferIO() if IMAGE_MAX_SIZE: resize_image(f, buf) path = default_storage.save(get_path(user_id, f.name), buf) return path