def read_metadata(pathname): """ Read artist and title information from the file. Partially duplicated from the cplay code, but the get_tag method in cplay was un-reusable. """ if re.compile("^http://").match(pathname) or not os.path.exists(pathname): return None try: if re.compile(".*\.ogg$", re.I).match(pathname): import ogg.vorbis vf = ogg.vorbis.VorbisFile(pathname) vc = vf.comment() tags = vc.as_dict() elif re.compile(".*\.mp3$", re.I).match(pathname): import ID3 vc = ID3.ID3(pathname, as_tuple=1) tags = vc.as_dict() else: return None artist = tags.get("ARTIST", [""])[0] title = tags.get("TITLE", [""])[0] import codecs if artist and title: return {'artist': codecs.latin_1_encode(artist)[0], 'track': codecs.latin_1_encode(title)[0]} else: return None except: return None
def _b(message): """convert string to correct format for buffer object""" import sys if hex(sys.hexversion) >= '0x30000f0': import codecs return codecs.latin_1_encode(message)[0] return message
def create_preauth(byval, key, by='name', expires=0, timestamp=None): """ Generates a zimbra preauth value :param byval: The value of the targeted user (according to the by-parameter). For example: The account name, if "by" is "name". :param key: The domain preauth key (you can retrieve that using zmprov gd) :param by: What type is the byval-parameter? Valid parameters are "name" (default), "id" and "foreignPrincipal" :param expires: Milliseconds when the auth token expires. Defaults to 0 for default account expiration :param timestamp: Current timestamp (is calculated by default) :returns: The preauth value to be used in an AuthRequest :rtype: str """ if timestamp is None: timestamp = int(datetime.now().strftime("%s")) * 1000 pak = hmac.new( codecs.latin_1_encode(key)[0], ('%s|%s|%s|%s' % ( byval, by, expires, timestamp )).encode("utf-8"), hashlib.sha1 ).hexdigest() return pak
def _as_bytes(s): """Turn byte string or unicode string into a bytes string.""" if isinstance(s, bytes): return s #Assume it is a unicode string #Note ISO-8859-1 aka Latin-1 preserves first 256 chars return codecs.latin_1_encode(s)[0]
def b(s): # BSON and socket operations deal in binary data. In # python 3 that means instances of `bytes`. In python # 2.7 you can create an alias for `bytes` using # the b prefix (e.g. b'foo'). # See http://python3porting.com/problems.html#nicer-solutions return codecs.latin_1_encode(s)[0]
def _have_bug17666(): """Debug function to check if Python's gzip is broken (PRIVATE). Checks for http://bugs.python.org/issue17666 expected in Python 2.7.4, 3.2.4 and 3.3.1 only. """ if os.name == 'java': #Jython not affected return False import gzip #Would like to use byte literal here: bgzf_eof = "\x1f\x8b\x08\x04\x00\x00\x00\x00\x00\xff\x06\x00BC" + \ "\x02\x00\x1b\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00" if sys.version_info[0] >= 3: import codecs bgzf_eof = codecs.latin_1_encode(bgzf_eof)[0] h = gzip.GzipFile(fileobj=BytesIO(bgzf_eof)) try: data = h.read() h.close() assert not data, "Should be zero length, not %i" % len(data) return False except TypeError as err: #TypeError: integer argument expected, got 'tuple' h.close() return True
def _have_bug17666(): """Debug function to check if Python's gzip is broken (PRIVATE). Checks for http://bugs.python.org/issue17666 expected in Python 2.7.4, 3.2.4 and 3.3.1 only. """ if os.name == 'java': # Jython not affected return False import gzip # Would like to use byte literal here: bgzf_eof = "\x1f\x8b\x08\x04\x00\x00\x00\x00\x00\xff\x06\x00BC" + \ "\x02\x00\x1b\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00" if sys.version_info[0] >= 3: import codecs bgzf_eof = codecs.latin_1_encode(bgzf_eof)[0] handle = gzip.GzipFile(fileobj=BytesIO(bgzf_eof)) try: data = handle.read() handle.close() assert not data, "Should be zero length, not %i" % len(data) return False except TypeError as err: # TypeError: integer argument expected, got 'tuple' handle.close() return True
def write_file(): info = request.get_json() path = info['path'] file = open(CONFIGURE_PATH + path, 'wb') file.write(codecs.latin_1_encode(info['cont'])[0]) file.close() return jsonify({'resp': 201})
def b(s): # BSON and socket operations deal in binary data. In # python 3 that means instances of `bytes`. In python # 2.6 and 2.7 you can create an alias for `bytes` using # the b prefix (e.g. b'foo'). # See http://python3porting.com/problems.html#nicer-solutions return codecs.latin_1_encode(s)[0]
def b(string): """ Create a byte string field - Python 3.x :param string: input string :return: a byte array containing the string """ return codecs.latin_1_encode(string)[0]
def test_latin_1_encode(self): #sanity new_str, size = codecs.latin_1_encode("abc") self.assertEqual(new_str, 'abc') self.assertEqual(size, 3) # so many ways to express latin 1... for x in ['iso-8859-1', 'iso8859-1', '8859', 'cp819', 'latin', 'latin1', 'L1']: self.assertEqual('abc'.encode(x), 'abc')
def my_handler(environ, start_response): path_info = environ.get("PATH_INFO", None) query_string = environ.get("QUERY_STRING", None) response_body = "You asked for {0} with query {1}".format(path_info, query_string) response_headers = [("Content-Type", "text/plain"), ("Content-Length", str(len(response_body)))] start_response("200 OK", response_headers) response = latin_1_encode(response_body)[0] return [response]
def b(s): # BSON and socket operations deal in binary data. In # python 3 that means instances of `bytes`. In python # 2.6 and 2.7 you can create an alias for `bytes` using # the b prefix (e.g. b'foo'). Python 2.4 and 2.5 don't # provide this marker so we provide this compat function. # In python 3.x b('foo') results in b'foo'. # See http://python3porting.com/problems.html#nicer-solutions return codecs.latin_1_encode(s)[0]
def send_colors(self, colors): data = [chr(r) + chr(g) + chr(b) for r, g, b in colors] + [chr(255)] data = "".join(data) data = _codecs.latin_1_encode(data)[0] if self.device.serial is not None: self.device.serial.write(data) self.device.serial.flush() self.device.serial.flushInput()
def _as_bytes(s): """Turn byte string or unicode string into a bytes string. The Python 2 version returns a (byte) string. """ if isinstance(s, bytes): return s #Assume it is a unicode string #Note ISO-8859-1 aka Latin-1 preserves first 256 chars return codecs.latin_1_encode(s)[0]
def format_json(tracks, ping_num): tracks_msg = [] for t in tracks: tracks_msg.append(t.get_buffer()) msg = {"ping_num": ping_num, "tracks":tracks_msg, "num_tracks":len(tracks)} msg = json.dumps(msg) msg = codecs.latin_1_encode(msg)[0] frmt = "=%ds" % len(msg) msg = struct.pack(frmt, msg) return msg
def files_by_pattern(self, pattern): """Retrieve list of files matching a given pattern. """ try: r = requests.get(self.jcmt_info_url, params={'file': pattern}) r.raise_for_status() return latin_1_encode(r.text)[0].strip().split('\n') except HTTPError as e: raise JSAProcError('Error fetching CADC file list: ' + str(e))
def compute_seguid(self): """Generate protein sequence hash :return: hashed protein sequence :rtype: str """ self.sequence = codecs.latin_1_encode(self.sequence.upper())[0] m = hashlib.sha1() m.update(self.sequence) return base64.b64encode(m.digest()).rstrip("=")
def format_json(tracks, ping_num): tracks_msg = [] for t in tracks: tracks_msg.append(t.get_buffer()) msg = { "ping_num": ping_num, "tracks": tracks_msg, "num_tracks": len(tracks) } msg = json.dumps(msg) msg = codecs.latin_1_encode(msg)[0] frmt = "=%ds" % len(msg) msg = struct.pack(frmt, msg) return msg
def _to_bytes(msg: Union[str, bytes]) -> bytes: """ Provides the ASCII bytes for the given string. This is purely to provide python 3 compatability, normalizing the unicode/ASCII change in the version bump. For an explanation of this see... http://python3porting.com/problems.html#nicer-solutions :param msg: string to be converted :returns: ASCII bytes for string """ if isinstance(msg, str): return codecs.latin_1_encode(msg, 'replace')[0] # type: ignore else: return msg
def systemd_notify(msg): """ Allow app to be setup as `Type=notify` in systemd Sending signals when it is ready to start serving. """ if "NOTIFY_SOCKET" not in os.environ: log.debug("No systemd NOTIFY_SOCKET set") return msg = codecs.latin_1_encode(msg)[0] addr = os.environ["NOTIFY_SOCKET"] if addr[0] == "@": addr = "\0" + addr[1:] sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) try: sock.connect(addr) sock.sendall(msg) except: log.exception("Could not notify systemd")
def getFingerprint(publicKey): """Get a digest of the ASN.1 DER-encoded **publicKey**. :type publicKey: str :param publicKey: A public key (as within the return parameters of :func:`generateOnionKey` or :func:`generateSigningKey`.) :rtype: str :returns: A spacey fingerprint. """ keyDigest = hashlib.sha1(publicKey) keyDigestBinary = keyDigest.digest() keyDigestHex = keyDigest.hexdigest() keyDigestHexUpper = keyDigestHex.upper() keyDigestBytes = codecs.latin_1_encode(keyDigestHexUpper, 'replace')[0] fingerprint = convertToSpaceyFingerprint(keyDigestBytes) return (fingerprint, keyDigestBinary)
def test_codecs_builtins(self): s = "abc" encoded = codecs.utf_8_encode(s) self.assertEqual(s, codecs.utf_8_decode(encoded[0])[0]) encoded = codecs.utf_7_encode(s) self.assertEqual(s, codecs.utf_7_decode(encoded[0])[0]) encoded = codecs.utf_16_encode(s) self.assertEqual(s, codecs.utf_16_decode(encoded[0])[0]) encoded = codecs.utf_16_le_encode(s) self.assertEqual(s, codecs.utf_16_le_decode(encoded[0])[0]) encoded = codecs.utf_16_be_encode(s) self.assertEqual(s, codecs.utf_16_be_decode(encoded[0])[0]) encoded = codecs.utf_32_encode(s) self.assertEqual(s, codecs.utf_32_decode(encoded[0])[0]) encoded = codecs.utf_32_le_encode(s) self.assertEqual(s, codecs.utf_32_le_decode(encoded[0])[0]) encoded = codecs.utf_32_be_encode(s) self.assertEqual(s, codecs.utf_32_be_decode(encoded[0])[0]) encoded = codecs.utf_32_be_encode(s) self.assertEqual(s, codecs.utf_32_be_decode(encoded[0])[0]) encoded = codecs.raw_unicode_escape_encode(s) self.assertEqual(s, codecs.raw_unicode_escape_decode(encoded[0])[0]) encoded = codecs.unicode_escape_encode(s) self.assertEqual(s, codecs.unicode_escape_decode(encoded[0])[0]) encoded = codecs.latin_1_encode(s) self.assertEqual(s, codecs.latin_1_decode(encoded[0])[0]) encoded = codecs.ascii_encode(s) self.assertEqual(s, codecs.ascii_decode(encoded[0])[0])
def create_preauth(byval, key, by='name', expires=0, timestamp=None): """ Generates a zimbra preauth value :param byval: The value of the targeted user (according to the by-parameter). For example: The account name, if "by" is "name". :param key: The domain preauth key (you can retrieve that using zmprov gd) :param by: What type is the byval-parameter? Valid parameters are "name" (default), "id" and "foreignPrincipal" :param expires: Milliseconds when the auth token expires. Defaults to 0 for default account expiration :param timestamp: Current timestamp (is calculated by default) :returns: The preauth value to be used in an AuthRequest :rtype: str """ if timestamp is None: timestamp = int(datetime.now().strftime("%s")) * 1000 pak = hmac.new( codecs.latin_1_encode(key)[0], ('%s|%s|%s|%s' % (byval, by, expires, timestamp)).encode("utf-8"), hashlib.sha1).hexdigest() return pak
def b(x): if type(x) == bytes: return x return codecs.latin_1_encode(x)[0]
def ords(self, string): """Convert an alphabetic string into a byte array of ordinals.""" string = str(string) s = string.translate(self._ord_table) a = array("B", codecs.latin_1_encode(s)[0]) return a
def _to_bytes_impl(msg): if msg is not None and isinstance(msg, str_type): return codecs.latin_1_encode(msg, 'replace')[0] else: return msg
def b(s): return codecs.latin_1_encode(s)[0]
def b(x): return codecs.latin_1_encode(x)[0] import transaction
#!/usr/bin/env python from base64 import b64encode, b64decode from codecs import (utf_8_encode, utf_8_decode, latin_1_encode, latin_1_decode) import random, time buf_len = 10000 iterations = 10000 print("Generating random input buffer") r = random.Random() buf = "".join([chr(r.randint(0, 255)) for i in range(buf_len)]) tests = {'UTF8 encode': lambda: utf_8_encode(unicode(buf, 'latin-1'))[0], 'B64 encode': lambda: b64encode(buf)} utf8_buf = tests['UTF8 encode']() b64_buf = tests['B64 encode']() tests.update({'UTF8 decode': lambda: latin_1_encode(utf_8_decode(utf8_buf)[0])[0], 'B64 decode': lambda: b64decode(b64_buf)}) print("Running tests") for test in 'UTF8 encode', 'B64 encode', 'UTF8 decode', 'B64 decode': start = time.time() for i in range(iterations): res_buf = tests[test]() print("%s took %s seconds (result size %s)" % ( test, (time.time() - start), len(res_buf)))
def _to_bytes_impl(msg): if isinstance(msg, str): return codecs.latin_1_encode(msg, 'replace')[0] else: return msg
def b(string): return codecs.latin_1_encode(string)[0]
try: consumed = 0 start = time.time() for i in range(count): mine.append(SHA256(message[:i]).hexdigest()) consumed += i finally: elapsed = time.time() - start print('Mine: %d hashes (%d bytes) in %0.2f secs (%0.2f H/s %d B/s)' % (i + 1, consumed, elapsed, float(i + 1) / elapsed, float(consumed) / elapsed)) try: consumed = 0 start = time.time() for i in range(count): # The encoding before is to make sure we're comparing the same # types (bytes to bytes). This is a Python 3 issue. stdlib.append( codecs.latin_1_encode(hashlib.sha256( message[:i]).hexdigest())[0]) consumed += i finally: elapsed = time.time() - start print('stdlib: %d hashes (%d bytes) in %0.2f secs (%0.2f H/s %d B/s)' % (i + 1, consumed, elapsed, float(i + 1) / elapsed, float(consumed) / elapsed)) for a, b, i in zip(mine, stdlib, range(count)): assert a == b, '%r (mine) != %r (stdlib) calculating SHA256(%r) of length %d' % ( a, b, message[:i], i)
cpp = ctypes.cdll.LoadLibrary('libc++.1.dylib') ffs_ = lambda _: cpp.ffs(_) except OSError: pass #idp_ = lambda _:"%s/%s" % (os.environ["IDPATH"],_) #uidp_ = lambda _:_.replace(os.environ["IDPATH"],"$IDPATH") idp_ = lambda _: "%s/%s" % (KEYDIR, _) uidp_ = lambda _: _.replace(KEYDIR, "$KEYDIR") gcp_ = lambda _: "%s/%s" % (os.environ["GEOCACHE"], _) import sys, codecs if sys.version_info.major > 2: u_ = lambda _: _ # py3 strings are unicode already b_ = lambda _: codecs.latin_1_encode(_)[ 0] # from py3 unicode string to bytes d_ = lambda _: codecs.latin_1_decode(_)[ 0] # from bytes to py3 unicode string else: u_ = lambda _: unicode(_, "utf-8") # py2 strings are bytes b_ = lambda _: _ d_ = lambda _: _ pass def findfile(base, name, relative=True): paths = [] for root, dirs, files in os.walk(base): if name in files: path = os.path.join(root, name) paths.append(path[len(base) + 1:] if relative else path)
def _to_bytes_impl(msg): if msg is not None and isinstance(msg, unicode): return codecs.latin_1_encode(msg, "replace")[0] else: return msg
def encode(self, input, final = False): return codecs.latin_1_encode(input, self.errors)[0]
def as_char(x): """Return character representation for a unicode symbol""" return codecs.latin_1_encode(x)[0]
try: from ordereddict import OrderedDict as odict except ImportError: odict = dict # objects used by dill for type declaration registered = d = odict() # objects dill fails to pickle failures = x = odict() # all other type objects succeeds = a = odict() # types module (part of CH 8) a["BooleanType"] = bool(1) a["BuiltinFunctionType"] = len a["BuiltinMethodType"] = a["BuiltinFunctionType"] a["BytesType"] = _bytes = codecs.latin_1_encode("\x00")[0] # bytes(1) a["ClassType"] = _class a["ComplexType"] = complex(1) a["DictType"] = _dict = {} a["DictionaryType"] = a["DictType"] a["FloatType"] = float(1) a["FunctionType"] = _function a["InstanceType"] = _instance = _class() a["IntType"] = _int = int(1) a["ListType"] = _list = [] a["NoneType"] = None a["ObjectType"] = object() a["StringType"] = _str = str(1) a["TupleType"] = _tuple = () a["TypeType"] = type if PY3:
try: from ordereddict import OrderedDict as odict except ImportError: odict = dict # objects used by dill for type declaration registered = d = odict() # objects dill fails to pickle failures = x = odict() # all other type objects succeeds = a = odict() # types module (part of CH 8) a['BooleanType'] = bool(1) a['BuiltinFunctionType'] = len a['BuiltinMethodType'] = a['BuiltinFunctionType'] a['BytesType'] = _bytes = codecs.latin_1_encode('\x00')[0] # bytes(1) a['ClassType'] = _class a['ComplexType'] = complex(1) a['DictType'] = _dict = {} a['DictionaryType'] = a['DictType'] a['FloatType'] = float(1) a['FunctionType'] = _function a['InstanceType'] = _instance = _class() a['IntType'] = _int = int(1) a['ListType'] = _list = [] a['NoneType'] = None a['ObjectType'] = object() a['StringType'] = _str = str(1) a['TupleType'] = _tuple = () a['TypeType'] = type if PY3:
def b(x): # deal with b'foo' versus 'foo' import codecs return codecs.latin_1_encode(x)[0]
def b(x): return codecs.latin_1_encode(x)[0]
_meth_func = "__func__" _meth_self = "__self__" _func_closure = "__closure__" _func_code = "__code__" _func_defaults = "__defaults__" _func_globals = "__globals__" _iterkeys = "keys" _itervalues = "values" _iteritems = "items" _iterlists = "lists" xrange = range Byte = lambda data: isinstance(data, bytes) and data or codecs.latin_1_encode(data)[0] ByteIndex = lambda data, idx: data[idx] IterBytes = lambda data: iter(data) else: _meth_func = "im_func" _meth_self = "im_self" _func_closure = "func_closure" _func_code = "func_code" _func_defaults = "func_defaults" _func_globals = "func_globals" _iterkeys = "iterkeys" _itervalues = "itervalues" _iteritems = "iteritems" _iterlists = "iterlists"
# Python 2 and 3 workarounds import sys if sys.version_info < (3,): import BaseHTTPServer b = lambda x: x else: import codecs from http import server as BaseHTTPServer b = lambda x: codecs.latin_1_encode(x)[0] # Regular imports import json import threading import unittest from simplegcm import Sender, Message, GCMException class MockGCMHandler(BaseHTTPServer.BaseHTTPRequestHandler): """Mock HTTP handler for testing.""" TEST_CASES_DATA = { '/200/': { 'status': 200, 'headers': {'Content-Type': 'application/json'}, 'response': { 'multicast_id': 1, 'canonical_ids': 0, 'success': 1, 'failure': 0, 'results': [
# or just a specific set of fields which is much faster #oOutlook.loadContacts() oOutlook.loadContacts(fields) if DEBUG: print "loading took %f seconds" % (time.time() - startTime) print "Number of contacts: %d" % len(oOutlook.records) print "Contact: %s" % oOutlook.records[0]['FullName'] print "Body:\n%s" % oOutlook.records[0]['Body'] ## The following attempt is from: ## http://win32com.goermezer.de/content/view/97/192/ import codecs, win32com.client # This example dumps the items in the default address book # needed for converting Unicode->Ansi (in local system codepage) DecodeUnicodeString = lambda x: codecs.latin_1_encode(x)[0] def DumpDefaultAddressBook_mod (handler=None): if handler is None: writer = getattr(logging, "debug") else: writer = handler.write # Create instance of Outlook o = win32com.client.Dispatch("Outlook.Application") mapi = o.GetNamespace("MAPI") folder = mapi.GetDefaultFolder(win32com.client.constants.olFolderContacts) print "The default address book contains",folder.Items.Count,"items" # see Outlook object model for more available properties on ContactItem objects # attributes = [ 'FullName', 'Email1DisplayName', # 'Email1AddressType'] attributes = ['Email1Address', 'EntryID',
def convert(s): return latin_1_encode(s)[0]
def b(x): if isinstance(x, bytes) is False: return codecs.latin_1_encode(x)[0] return x