[Python-checkins] r88814 - tracker/instances/python-dev/detectors/textplain.py

martin.v.loewis python-checkins at python.org
Thu Apr 14 21:27:59 CEST 2011


Author: martin.v.loewis
Date: Thu Apr 14 21:27:58 2011
New Revision: 88814

Log:
Add detector to change application/octet-stream to
text/plain sometimes.


Added:
   tracker/instances/python-dev/detectors/textplain.py
      - copied, changed from r88813, /tracker/instances/python-dev/detectors/no_texthtml.py

Copied: tracker/instances/python-dev/detectors/textplain.py (from r88813, /tracker/instances/python-dev/detectors/no_texthtml.py)
==============================================================================
--- /tracker/instances/python-dev/detectors/no_texthtml.py	(original)
+++ tracker/instances/python-dev/detectors/textplain.py	Thu Apr 14 21:27:58 2011
@@ -1,9 +1,21 @@
-
-def audit_html_files(db, cl, nodeid, newvalues):
-    if newvalues.has_key('type') and newvalues['type'] == 'text/html':
+# On file creation, if the file is application/octet-stream,
+# yet the file content looks like text, change the type to
+# text/plain.
+def audit_application_octetstream(db, cl, nodeid, newvalues):
+    if newvalues.has_key('type') and newvalues['type'] == 'application/octet-stream':
+        # check whether this might be a text file
+        try:
+            text = newvalues['content'].decode('utf-8')
+        except UnicodeError:
+            return
+        # check that there aren't funny control characters in there
+        for c in text:
+            if ord(c) >= 32:
+                continue
+            if c not in u' \f\t\r\n':
+                return
         newvalues['type'] = 'text/plain'
     
 
 def init(db):
-    db.file.audit('set', audit_html_files)
-    db.file.audit('create', audit_html_files)
+    db.file.audit('create', audit_application_octetstream)


More information about the Python-checkins mailing list