# # # patch "fdo/sharedmimeinfo.py" # from [8aca6c73abedbeb59751ba139242d594fc23dcd5] # to [6db1bfd2628f4ecd84b7c64ab79d6843d2dc910c] # ============================================================ --- fdo/sharedmimeinfo.py 8aca6c73abedbeb59751ba139242d594fc23dcd5 +++ fdo/sharedmimeinfo.py 6db1bfd2628f4ecd84b7c64ab79d6843d2dc910c @@ -91,6 +91,8 @@ class MagicLookup(object): if fd.readline() != 'MIME-Magic\0\n': raise Exception("Not a Mime Magic file: %s" % fname) header_re = re.compile(r'^\[([0-9]+):([^\]]+)\]$') + value_re = re.compile(r'^([0-9]*)>([0-9]+)=') + options_re = re.compile(r'^(\~[0-9]+)?(\+[0-9]+)?$') while True: # read the first line (priority and mimetype) header = fd.readline() @@ -98,18 +100,62 @@ class MagicLookup(object): break m = header_re.match(header) if not m: + print "no match:", repr(header) continue priority, mime_type = m.groups() - print priority, mime_type - # now read the data, pattern, etc - - + # the next line will have indent, start_offset and the + # start of the value + buf = fd.readline() + m = value_re.match(buf) + if not m: + continue + indent, start_offset = m.groups() + try: + indent = int(indent) + except: + indent = 0 + buf = buf[m.end():] + # the next two bytes are the length, in big-endian format + length = (ord(buf[0]) << 8) + ord(buf[1]) + buf = buf[2:] + # read the remainder of the value + to_read = length - len(buf) + if to_read > 0: + buf += fd.read(to_read) + value, buf = buf[:length], buf[length:] + # is the next thing a mask? + if len(buf) == 0: + buf += fd.read(1) + if buf[0] == '&': + buf = buf[1:] + to_read = length - len(buf) + if to_read > 0: + buf += fd.read(to_read) + mask, buf = buf[:length], buf[length:] + else: + mask = '\xff' * length + # anything remaining will end in a newline; so readline() + # does what we want. see whether or not we need to call it.. + if len(buf) == 0 or buf[-1] != '\n': + buf += fd.readline() + word_size, range_length = 1, 0 + m = options_re.match(buf) + if m: + for group in m.groups(): + if not group: + continue + if group[0] == '~': + word_size = int(group[1:]) + elif group[0] == '+': + range_length = int(group[1:]) + print indent, start_offset, repr(value), repr(mask), word_size, range_length def lookup(self, data): return None if __name__ == '__main__': a = GlobLookup() b = MagicLookup() + sys.exit(0) for line in sys.stdin: line = line.strip() print line, a.lookup(line), b.lookup(line)