[xiph-commits] r15134 - in trunk/subtle: . Subtitles

jmesquita at svn.xiph.org jmesquita at svn.xiph.org
Fri Jul 25 18:16:38 PDT 2008


Author: jmesquita
Date: 2008-07-25 18:16:37 -0700 (Fri, 25 Jul 2008)
New Revision: 15134

Modified:
   trunk/subtle/Subtitles/Discoverer.py
   trunk/subtle/Subtitles/SubRip.py
   trunk/subtle/Subtle.py
Log:
Subtiles package: Move the discover process to each subtitle format file instead of having one function to handle it all
Subtle.py: Give it a more proper error information


Modified: trunk/subtle/Subtitles/Discoverer.py
===================================================================
--- trunk/subtle/Subtitles/Discoverer.py	2008-07-24 19:26:11 UTC (rev 15133)
+++ trunk/subtle/Subtitles/Discoverer.py	2008-07-26 01:16:37 UTC (rev 15134)
@@ -20,7 +20,7 @@
 #       MA 02110-1301, USA.
 
 import os
-from SubRip import SubRip
+import SubRip
 
 def discoverer(file):
     """
@@ -28,9 +28,9 @@
         handle the specific format. If it returns None, format is not yet
         supported.
     """
-    extension = os.path.splitext(file)[1]
-    # TODO: For text based files we should check using
-    ## regex , not extensions
-    if extension == ".srt":
-        return SubRip(file)
+    
+    # Test for SubRip
+    if SubRip.discover(file):
+        return SubRip.SubRip(file)
+        
     return None

Modified: trunk/subtle/Subtitles/SubRip.py
===================================================================
--- trunk/subtle/Subtitles/SubRip.py	2008-07-24 19:26:11 UTC (rev 15133)
+++ trunk/subtle/Subtitles/SubRip.py	2008-07-26 01:16:37 UTC (rev 15134)
@@ -21,10 +21,44 @@
 
 import os
 import string
+import re
 
 from Subtitles import Subtitles
 from Sub import *
 
+def discover(file):
+    """
+        Every subtitle should have a discover function
+        and return true if it should handle the requested
+        file.
+    """
+    # This is not the best option since we rely on Linux-only
+    # Make use of file command to check on the file type
+    try:
+        import magic
+    except:
+        print "We need python-magic, otherwise, this format will not be \
+        supported"
+        return
+
+    m = magic.open(magic.MAGIC_COMPRESS | magic.MAGIC_MIME)
+    status = m.load()
+    
+    if m.file(file).split('/')[0] == "text":
+        # Open file and read it
+        fd = open(file, "r")
+        data = fd.read()
+        fd.close()
+    else:
+        return
+    
+    # Test for SubRip by matching the header
+    regex = re.compile("""^(?P<counter>\d+)\s*
+                        ^(?P<ts_from>\d{2}:\d{2}:\d{2},\d{3})\s*-->\s*(?P<ts_to>\d{2}:\d{2}:\d{2},\d{3})\r?""",re.MULTILINE|re.VERBOSE)
+    if regex.match(data):
+        return True
+    return
+
 class SubRip(Subtitles):
     """
         This class handles the SubRip subtitle format

Modified: trunk/subtle/Subtle.py
===================================================================
--- trunk/subtle/Subtle.py	2008-07-24 19:26:11 UTC (rev 15133)
+++ trunk/subtle/Subtle.py	2008-07-26 01:16:37 UTC (rev 15134)
@@ -550,7 +550,8 @@
                                             gtk.DIALOG_MODAL, \
                                             gtk.MESSAGE_ERROR, \
                                             gtk.BUTTONS_CLOSE, \
-                                            "This file format is not supported.")
+                                            "This file format is not supported \
+                                            or not detected.")
             result = errorDialog.run()
             if (result == gtk.RESPONSE_CLOSE):
                 errorDialog.destroy()



More information about the commits mailing list