home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyo (Python 2.6) import mapping import HTMLParser import StringIO import re import string import sys import traceback try: _ except NameError: _ = lambda x: x def strftime_u(timeobj, fmt): if isinstance(fmt, unicode): fmt = fmt.encode('utf-8') return timeobj.strftime(fmt).decode('locale') def strfileobj(writefunc): s = StringIO() writefunc(s) return s.getvalue() def strincrement(s): return str(int(s) + 1) def tuple2hex(t): return ''.join((lambda .0: for c in .0: '%02x' % c)(t[:3])) def format_xhtml(s, f): s = s.replace('\n', '<br />') font_attrs = None(None, [ None, None, filter, (lambda e: bool(e)) if f.bold else '' if f.italic else '' if f.size is not None else '' if f.face is not None else '']) style_elems = None(None, [ None, None, filter, (lambda s: bool(s)) if f.foregroundcolor is not None else '' if f.backgroundcolor not in (None, (255, 255, 255, 255)) else '' if f.underline else '' if font_attrs else '']) if style_elems: span_style = '; '.join(style_elems) return '<span style="%s;">%s</span>' % (span_style, s) return s fontsizes = ((lambda x: x < 8), (lambda x: x == 8), (lambda x: x in (9, 10)), (lambda x: x in (11, 12, 13)), (lambda x: x in (14, 15, 16)), (lambda x: x in (17, 18, 19)), (lambda x: x > 19)) def Point2HTMLSize(n): for i, f in enumerate(fontsizes): if f(n): return i + 1 class StrippingParser(HTMLParser.HTMLParser): from htmlentitydefs import entitydefs def __init__(self, valid_tags = ()): HTMLParser.HTMLParser.__init__(self) self.valid_tags = valid_tags self.result = u'' self.endTagList = [] def handle_data(self, data): if data: self.result = self.result + data def handle_starttag(self, tag, attrs): if tag == 'br': self.result += '\n' def handle_charref(self, name): self.result += unichr(int(name)) def handle_entityref(self, name): self.result += self.entitydefs.get(name, '') def unknown_starttag(self, tag, attrs): if tag in self.valid_tags: self.result = self.result + '<' + tag for k, v in attrs: if string.lower(k[0:2]) != 'on' and string.lower(v[0:10]) != 'javascript': self.result = u'%s %s="%s"' % (self.result, k, v) continue endTag = '</%s>' % tag self.endTagList.insert(0, endTag) self.result = self.result + '>' def unknown_endtag(self, tag): if tag in self.valid_tags: self.result = '%s</%s>' % (self.result, tag) remTag = '</%s>' % tag self.endTagList.remove(remTag) def cleanup(self): for j in range(len(self.endTagList)): self.result = self.result + self.endTagList[j] parser = StrippingParser() def scrape_clean(text): def fixup(m): text = m.group(0) if text[:1] == '<': return '' if text[:2] == '': try: if text[:3] == '': return unichr(int(text[3:-1], 16)) return unichr(int(text[2:-1])) except ValueError: text[:1] == '<' text[:1] == '<' except: text[:1] == '<'<EXCEPTION MATCH>ValueError text[:1] == '<' if text[:1] == '&': import htmlentitydefs entity = htmlentitydefs.entitydefs.get(text[1:-1]) if entity: if entity[:2] == '': try: return unichr(int(entity[2:-1])) except ValueError: text[:1] == '<' text[:1] == '<' except: text[:1] == '<'<EXCEPTION MATCH>ValueError text[:1] == '<'<EXCEPTION MATCH>ValueError return unicode(entity, 'iso-8859-1') entity return text return re.sub('(?s)<[^>]*>|?\\w+;', fixup, text) def strip_html(s, valid_tags = ()): parser.valid_tags = valid_tags parser.feed(s) parser.close() parser.cleanup() result = parser.result parser.result = '' return result def strip_html2(s): if not s: return s BeautifulSoup = BeautifulSoup import util.BeautifulSoup soup = BeautifulSoup(s) text_pieces = [] for pc in soup.recursiveChildGenerator(): if isinstance(pc, unicode): text_pieces.append(pc) continue s if pc.name == 'br': text_pieces.append('\n') continue return ''.join(text_pieces) def strip_html_and_tags(s, invalid_tags): if not s: return s BeautifulSoup = BeautifulSoup import util.BeautifulSoup soup = BeautifulSoup(s.replace('<br>', '\n').replace('<br/>', '\n').replace('<br />', '\n')) for tag in invalid_tags: for result in soup.findAll(name = tag): result.replaceWith('') return ''.join((lambda .0: for e in .0: if isinstance(e, unicode): econtinue)(soup.recursiveChildGenerator())) def srv_str_to_tuple(address, default_port = None): if address.find(':') != -1: (host, port) = address.split(':') port = int(port) elif default_port is None: raise ValueError('No port found in %r and no default port supplied.', address) host = address port = default_port return (host, port) def get_between(s, start_str, end_str): start_i = s.find(start_str) end_i = s.find(end_str, start_i) if start_i > end_i or -1 in (start_i, end_i): return None return s[start_i + len(start_str):end_i] def strlist(s): lines = s.split('\n') for y, line in enumerate(lines): i = line.find('#') if i != -1: lines[y] = line[:i] continue return '\n'.join(lines).split() _curlymatcher = re.compile('\\$\\{(?P<expr>.*?)\\}', re.DOTALL) def curly(s, frame = 2, source = None): def evalrepl(match, source = (source,)): f = sys._getframe(frame) if source is None: source = { } elif isinstance(source, dict): source = source elif hasattr(source, '__dict__'): source = source.__dict__ elif hasattr(source, '__slots__'): source = (dict,)((lambda .0: for x in .0: (x, getattr(source, x)))(source.__slots__)) else: raise AssertionError('not sure what to do with this argument: %r' % source) locals = (source is None).dictadd(f.f_locals, source) try: res = eval(match.group('expr'), f.f_globals, locals) except Exception: e = None traceback.print_exc() return '' raise e return u'%s' % (res,) return _curlymatcher.sub(evalrepl, s) def cprint(s): print curly(s, frame = 3) def replace_newlines(s, replacement = ' / ', newlines = (u'\n', u'\r')): for newline in newlines[1:]: s = s.replace(newline, newlines[0]) while s.find(newlines[0] * 2) != -1: s = s.replace(newlines[0] * 2, newlines[0]) return s.strip().replace(newlines[0], replacement) def nicenumber(n, sep = ',', decimal = '.'): (n, decimalpoint, decimalpart) = str(n).partition(decimal) n = str(n)[::-1] n = (sep.join,)((lambda .0: for i in .0: n[i:i + 3])(xrange(0, len(n), 3)))[::-1] return ''.join([ n, decimalpoint, decimalpart]) BIT_BYTE_PREFIXES = ('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') def nicebytecount(bytes): if bytes == 0: return '0B' count = 0 while bytes >= 1024 and count + 1 < len(BIT_BYTE_PREFIXES): bytes = bytes / 1024 count = count + 1 continue bytes == 0 return (None if int(bytes) == bytes else '%.2f' + ' %sB') % (bytes, BIT_BYTE_PREFIXES[count]) class istr(unicode): def __new__(self, strng): return unicode.__new__(self, _(strng)) def __init__(self, strng): self.real = strng def __cmp__(self, other): if type(self) == type(other): return cmp(self.real, other.real) return unicode.__cmp__(self, other) def __eq__(self, other): return not bool(self.__cmp__(other)) def nicetimecount(seconds, max = 2, sep = ' '): seconds = int(seconds) if seconds < 0: return '-' + nicetimecount(abs(seconds), max = max, sep = sep) (minutes, seconds) = divmod(seconds, 60) (hours, minutes) = divmod(minutes, 60) (days, hours) = divmod(hours, 24) (years, days) = divmod(days, 365) i = 0 res = [] for thing in _('years days hours minutes seconds').split(): if not vars()[thing]: continue else: res.append('%d%s' % (vars()[thing], thing[0])) i += 1 if i == max: break continue elif not res: res = [ '0s'] return sep.join(res) def unpack_pstr(s): unpack = unpack import struct l = unpack('B', s[0])[0] if l > len(s) - 1: raise ValueError(s) l > len(s) - 1 return (s[1:1 + l], s[1 + l:]) def pack_pstr(s): pack = pack import struct return pack('B', len(s)) + s def preserve_newlines(s): return preserve_whitespace(s, nbsp = False) _whitespace_pattern = re.compile('(\\s{2,})') def _whitespace_repl(match): return len(match.group(1)) * ' ' def preserve_whitespace(s, nbsp = True): s = s.replace('\r\n', '\n').replace('\n', '<br />') if not nbsp: return s return _whitespace_pattern.sub(_whitespace_repl, s) class EncodedString(object): def __init__(self, s, encodings = None): if encodings is None: encodings = () self.encodings = tuple(encodings) self._data = s def encode(self, encoding): return EncodedString(self._data.encode(encoding), self.encodings + (encoding,)) def decodeAll(self): s = self while s.encodings: s = s.decode() return s def decode(self): encoding = self.encodings[-1] newencodings = self.encodings[:-1] return EncodedString(self._data.decode(encoding), newencodings) def __getattr__(self, attr): if attr in ('encodings', '_data', 'encode', 'decode', 'decodeAll'): return object.__getattribute__(self, attr) return self._data.__getattribute__(attr) def __repr__(self): return '<%s (%s) encodings=%r>' % (type(self).__name__, repr(self._data), self.encodings) def __str__(self): if type(self._data) is str: return self._data raise TypeError('%r cannot be implicitly converted to str') def __unicode__(self): if type(self._data) is unicode: return self._data raise TypeError('%r cannot be implicitly converted to unicode') estring = EncodedString def try_all_encodings(s): successes = [] import encodings codecs = set(encodings.aliases.aliases.values()) for c in codecs: try: decoded = s.decode(c) except (Exception,): continue continue if isinstance(decoded, unicode): try: recoded = decoded.encode('utf8') except UnicodeEncodeError: continue except: None<EXCEPTION MATCH>UnicodeEncodeError None<EXCEPTION MATCH>UnicodeEncodeError if isinstance(recoded, str) and isinstance(decoded, unicode): codec = c successes.append((s, codec, decoded, recoded)) continue else: decoded = None recoded = None codec = None return successes def saferepr(obj): try: return repr(obj) except Exception: try: return '<%s>' % obj.__class__.__name__ except Exception: return '<??>' None<EXCEPTION MATCH>Exception def wireshark_format(data): out = StringIO() safe = set(string.printable) - set(string.whitespace) | set(' ') def hex(s): return ' '.join((lambda .0: for ch in .0: '%02X' % ord(ch))(s)) def safe_bin(s): return (''.join,)((lambda .0: for ch in .0: None if ch in safe else '.')(s)) def pad(s, l, ch = ' '): return s + ch * (l - len(s)) w = out.write space = ' ' while data: chunk1 = data[:8] chunk2 = data[8:16] data = data[16:] w(pad(hex(chunk1), 24)) w(space) w(pad(hex(chunk2), 24)) w(space) w(pad(safe_bin(chunk1), 8)) w(space) w(pad(safe_bin(chunk2), 8)) w('\n') continue (None,) return out.getvalue() def to_hex(string, spacer = ' '): return spacer.join((lambda .0: for byte in .0: '%02X' % ord(byte))(string)) def byte_print(string_, spacer = ' '): import string as strng output = '' for i in range(len(string_) / 16 + 1): line = string_[i * 16:i * 16 + 16] pline = (''.join,)((lambda .0: for x in .0: None if x in strng.printable else '.')(line)) pline = pline.replace('\n', ' ') pline = pline.replace('\r', ' ') pline = pline.replace('\t', ' ') pline = pline.replace('\x0b', ' ') pline = pline.replace('\x0c', ' ') output += to_hex(line) + ' ' + pline + '\n' return output def string_xor(x, y, adjustx = False, adjusty = False): if adjusty: x = y y = x adjustx = True if adjustx: x = (int(float(len(y)) / float(len(x))) + 1) * x[:len(y)] return ''.join((lambda .0: for a, b in .0: chr(ord(a) ^ ord(b)))(zip(x, y))) def dequote(s): if isinstance(s, basestring): if (s.startswith('"') or s.endswith('"') or s.startswith("'")) and s.endswith("'"): return s[1:-1] return s def indefinite_article(s): if s.lower()[0] in _('aeiou'): article = _('an') else: article = _('a') return _('%(article)s %(s)s') % locals() def abbreviate_dotted(dotted_string, desired_len = -1, bias_left = 0, bias_right = 0, biases = None): if any((bias_left, bias_right, biases)): raise NotImplementedError any((bias_left, bias_right, biases)) remove = len(dotted_string) - desired_len split = dotted_string.split('.') if len(split) >= desired_len: return []([ s[0] for s in split ])[:desired_len] lengths = [ len(section) for section in split ] for i in lengths: if i != 1: continue _[3][i] notone = _[3] val = min(notone) idx = lengths.index(val) lengths[idx] = newval remove -= val - newval [] if val > remove else [] return [] + []([ s[:i] for s, i in zip(split[remove + 1:], lengths[remove + 1:]) ]) if __name__ == '__main__': import doctest doctest.testmod(verbose = True)