More work on the diff kitten

This commit is contained in:
Kovid Goyal
2018-05-05 09:45:35 +05:30
parent ac7596b7c5
commit f5c713b118
2 changed files with 55 additions and 18 deletions

View File

@@ -3,11 +3,11 @@
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
import os
import re
from functools import lru_cache
from hashlib import md5
from mimetypes import guess_type
path_name_map = {}
@@ -86,6 +86,17 @@ def collect_files(collection, left, right):
collection.add_add(right_path_map[name])
sanitize_pat = re.compile('[\x00-\x1f\x7f\x80-\x9f]')
def sanitize_sub(m):
return '<{:x}>'.format(ord(m.group()[0]))
def sanitize(text):
return sanitize_pat.sub(sanitize_sub, text)
@lru_cache(maxsize=1024)
def mime_type_for_path(path):
return guess_type(path)[0] or 'application/octet-stream'
@@ -106,7 +117,7 @@ def data_for_path(path):
@lru_cache(maxsize=1024)
def lines_for_path(path):
data = data_for_path(path)
return data.splitlines()
return tuple(map(sanitize, data.splitlines()))
@lru_cache(maxsize=1024)