cis notified web server of a job completion; upload form interface and validation...
[living-lab-site.git] / cis / api / base.py
index a57d96d..b4e18a8 100644 (file)
@@ -4,11 +4,13 @@
 Base classes for the external programs API.
 """
 
-import api_exceptions
+import os
 import re
-import cis_util
 import random
 
+import cis_exceptions
+import cis_util
+
 class BaseTranscoder:
     """
     Abstraction of the API class for the transcoder program. 
@@ -65,26 +67,28 @@ class BaseTranscoder:
         """
         Transcodes the input file to an audio-video file.
 
-        container: possible values are listed in containers member as keys
-        a_codec: possible values are listed in a_codecs member as keys
-        v_codec: possible values are listed in v_codecs member as keys
-        a_bitrate: (numeric) audio bit rate
-        a_samplingrate: (numeric) audio sampling rate in Hz
-        a_channels: (numeric) number of audio channels
-        v_bitrate: (numeric) video bit rate
-        v_framerate: (numeric) number of frames per second for a video
-        v_resolution: (string) video image size as <width>x<height>
-        v_dar: video display aspect ratio as <den>x<num> or float
+        @param container: possible values are listed in containers member 
+        as keys
+        @param a_codec possible values are listed in a_codecs member as keys
+        @param v_codec possible values are listed in v_codecs member as keys
+        @param a_bitrate (numeric) audio bit rate
+        @param a_samplingrate (numeric) audio sampling rate in Hz
+        @param a_channels (numeric) number of audio channels
+        @param v_bitrate (numeric) video bit rate
+        @param v_framerate (numeric) number of frames per second for a video
+        @param v_resolution (string) video image size as <width>x<height>
+        @param v_dar video display aspect ratio as <den>x<num> or float
+        @return output file name
         """
 
         # Check parameters.
         if a_codec is None and v_codec is None:
             raise ValueError('No audio or video codec specified.')
 
-        if a_codec is not None and type(a_codec) is not str:
+        if a_codec is not None and type(a_codec) not in [str, unicode]:
             raise TypeError('Audio codec must be string.')
 
-        if v_codec is not None and type(v_codec) is not str:
+        if v_codec is not None and type(v_codec) not in [str, unicode]:
             raise TypeError('Video codec must be string.')
 
         if a_samplingrate is not None and type(a_samplingrate) is not int:
@@ -104,7 +108,11 @@ class BaseTranscoder:
                 and re.match('[\d]+:[\d]+', v_dar) is None):
             raise ValueError('Video display aspect ratio must be a float or a string like <den>:<num>.')
 
-        self.output_file = self.dest_path + self.name
+        self.output_file = os.path.join(self.dest_path, self.name)
+        if os.path.exists(self.output_file):
+            raise cis_exceptions.FileAlreadyExistsException( \
+                    'file "%s" already exists' % self.output_file)
+        
         if v_resolution is not None:
             self.output_file += '_'
             self.output_file += v_resolution[(v_resolution.rindex('x')+1):]
@@ -113,7 +121,7 @@ class BaseTranscoder:
         if ext is not None:
             self.output_file += '.' + ext
 
-        self._transcode(self.tr_container(container),
+        return self._transcode(self.tr_container(container),
                 self.tr_a_codec(a_codec), self.tr_v_codec(v_codec),
                 a_bitrate, a_samplingrate, a_channels,
                 v_bitrate, v_framerate, v_resolution, v_dar)
@@ -124,6 +132,8 @@ class BaseTranscoder:
         """
         Called by transcode; must be overridden by a child class which
         effectively transcodes the input file.
+
+        @return output file name
         """
         pass
 
@@ -131,7 +141,8 @@ class BaseTranscoder:
         """ Translates container API name into external program identifier."""
 
         if not self.containers.has_key(name) or self.containers[name] is None:
-            raise api_exceptions.NotImplementedException("Container " + name)
+            raise cis_exceptions.NotImplementedException("Container " + name \
+                    + "not implemented")
 
         return self.containers[name]
 
@@ -152,7 +163,8 @@ class BaseTranscoder:
         """ Translates audio codec API name into external program identifier."""
 
         if not self.a_codecs.has_key(name) or self.a_codecs[name] is None:
-            raise api_exceptions.NotImplementedException("Audio Codec " + name)
+            raise cis_exceptions.NotImplementedException("Audio Codec " + name \
+                    + "not implemented")
 
         return self.a_codecs[name]
 
@@ -160,7 +172,8 @@ class BaseTranscoder:
         """ Translates video codec API name into external program identifier."""
 
         if not self.v_codecs.has_key(name) or self.v_codecs[name] is None:
-            raise api_exceptions.NotImplementedException("Video Codec " + name)
+            raise cis_exceptions.NotImplementedException("Video Codec " + name \
+                    + "not implemented")
 
         return self.v_codecs[name]
 
@@ -221,8 +234,8 @@ class BaseThumbExtractor:
         for index in range (0, count):
             thumb_extracted = True
             try:
-                self.extract_thumb(seek_pos, resolution, index)
-            except api_exceptions.ThumbExtractionException as e:
+                self.extract_thumb(seek_pos, resolution, n_thumbs_extracted)
+            except cis_exceptions.ThumbExtractionException as e:
                 thumb_extracted = False
 
             if thumb_extracted:
@@ -232,18 +245,20 @@ class BaseThumbExtractor:
 
         return n_thumbs_extracted
 
-    def get_video_duration(self):
-        """
-        Returns the number of seconds of a video (int/float).
-        """
-        pass
-
     def get_output_file_name(self, index):
         """ Returns the name required as output file name based on index. """
-        output_file_name = self.dest_path + self.name \
+        output_file_name = os.path.join(self.dest_path, self.name) \
                 + '_t' + ("%02d" % index) + '.jpg'
+                
+        if os.path.exists(output_file_name):
+            raise cis_exceptions.FileAlreadyExistsException( \
+                    'file "%s" already exists' % output_file_name)
+        
         return output_file_name
 
+    def get_video_duration(self):
+        pass
+
 
 class BaseFileTransferer:
     """
@@ -287,3 +302,13 @@ class BaseFileTransferer:
         Class's destructor calls this method.
         """
         pass
+
+
+class BaseAVInfo:
+    @staticmethod
+    def get_video_duration(input_file, formated=False):
+        """
+        Returns the number of seconds of a video (int/float) if formated is
+        False and a string for duration formated as [HH:]:mm:ss otherwise.
+        """
+        pass