Record the original state of the media entry in the processor
[mediagoblin.git] / mediagoblin / processing / __init__.py
index 95622b9d7359ee3a611c0198bb8b873b979838a2..47f0b84e92abcc4e8d1d40383b5e8d62d0719259 100644 (file)
@@ -18,9 +18,10 @@ from collections import OrderedDict
 import logging
 import os
 
-from mediagoblin.db.util import atomic_update
 from mediagoblin import mg_globals as mgg
-
+from mediagoblin.db.util import atomic_update
+from mediagoblin.db.models import MediaEntry
+from mediagoblin.tools.pluginapi import hook_handle
 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
 
 _log = logging.getLogger(__name__)
@@ -112,12 +113,21 @@ class MediaProcessor(object):
     # action this MediaProcessor provides
     description = None
 
-    def __init__(self, manager):
+    def __init__(self, manager, media_entry):
         self.manager = manager
+        self.media_entry = media_entry
+        self.entry_orig_state = media_entry.state
 
         # Should be initialized at time of processing, at least
         self.workbench = None
 
+    def __enter__(self):
+        self.workbench = mgg.workbench_manager.create()
+
+    def __exit__(self, *args):
+        self.workbench.destroy()
+        self.workbench = None
+
     # @with_workbench
     def process(self, **kwargs):
         """
@@ -126,7 +136,7 @@ class MediaProcessor(object):
         raise NotImplementedError
 
     @classmethod
-    def media_is_eligibile(self, media_entry):
+    def media_is_eligible(cls, media_entry):
         raise NotImplementedError
 
     ###############################
@@ -134,11 +144,11 @@ class MediaProcessor(object):
     ###############################
 
     @classmethod
-    def generate_parser(self):
+    def generate_parser(cls):
         raise NotImplementedError
 
     @classmethod
-    def parser_to_request(self, parser):
+    def args_to_request(cls, args):
         raise NotImplementedError
 
     ##########################################
@@ -146,6 +156,11 @@ class MediaProcessor(object):
     ##########################################
 
 
+class ProcessingKeyError(Exception): pass
+class ProcessorDoesNotExist(ProcessingKeyError): pass
+class ProcessorNotEligible(ProcessingKeyError): pass
+
+
 class ProcessingManager(object):
     """Manages all the processing actions available for a media type
 
@@ -183,13 +198,76 @@ class ProcessingManager(object):
         # Got to figure out what actually goes here before I can write this properly
         pass
 
-    def process(self, entry, directive, request):
+    def get_processor(self, key, entry=None):
+        """
+        Get the processor with this key.
+
+        If entry supplied, make sure this entry is actually compatible;
+        otherwise raise error.
+        """
+        try:
+            processor = self.processors[key]
+        except KeyError:
+            raise ProcessorDoesNotExist(
+                "'%s' processor does not exist for this media type" % key)
+
+        if entry and not processor.media_is_eligible(entry):
+            raise ProcessorNotEligible(
+                "This entry is not eligible for processor with name '%s'" % key)
+
+        return processor
+
+    def process_from_args(self, entry, reprocess_command, request):
         """
         Process a media entry.
         """
         pass
 
 
+def request_from_args(args, which_args):
+    """
+    Generate a request from the values of some argparse parsed args
+    """
+    request = {}
+    for arg in which_args:
+        request[arg] = getattr(args, arg)
+
+    return request
+
+
+class MediaEntryNotFound(Exception): pass
+
+
+def get_processing_manager_for_type(media_type):
+    """
+    Get the appropriate media manager for this type
+    """
+    manager_class = hook_handle(('reprocess_manager', media_type))
+    manager = manager_class()
+
+    return manager
+
+
+def get_entry_and_processing_manager(media_id):
+    """
+    Get a MediaEntry, its media type, and its manager all in one go.
+
+    Returns a tuple of: `(entry, media_type, media_manager)`
+    """
+    entry = MediaEntry.query.filter_by(id=media_id).first()
+    if entry is None:
+        raise MediaEntryNotFound("Can't find media with id '%s'" % media_id)
+
+    manager = get_processing_manager_for_type(entry.media_type)
+
+    return entry, manager
+
+
+################################################
+# TODO: This ProcessingState is OUTDATED,
+#   and needs to be refactored into other tools!
+################################################
+
 class ProcessingState(object):
     """
     The first and only argument to the "processor" of a media type