示例#1
0
 def pre_pipeline(self):
     self.context = self.transmogrifier.context
     self.seen_count = self.changed_count = 0
     self.redirector = queryUtility(IRedirectionStorage)
     if self.redirector is None:
         logger.error(u'No IRedirectionStorage found, '
                      u'skipping all redirections.')
         self.transmogrify = lambda i: i
 def pre_pipeline(self):
     self.context = self.transmogrifier.context
     self.seen_count = self.changed_count = 0
     self.redirector = queryUtility(IRedirectionStorage)
     if self.redirector is None:
         logger.error(u'No IRedirectionStorage found, '
                   u'skipping all redirections.')
         self.transmogrify = lambda i: i
def get_remote_image(url, item, img_title="", pathkey="_path",
                     jsonmigrator = False):

    # FIXME: not making an extra call to get the real pathkey
    logger.info("""Fetching image %s for article %s """ % (url,
            item.get(pathkey, "")))
    # Strip plone view from URL:
    url, it_worked = _strip_view(url)
    # it won't work if the image url does not have a proper image
    if not it_worked:
        jsonmigrator = False
    if jsonmigrator:
        url += "/get_item"
    try:
        http = urllib.urlopen(url)
        image_data = http.read()
        if http.code > 399:
            # we can't  get the image here
            raise IOError
        real_url = http.url

    except Exception as error:
        logger.error("Could not retrieve image at %s: %s - skipping" %
                     (url, error))
        return None, None, []

    if jsonmigrator and real_url.endswith("/get_item"):
            real_url = real_url[:len("/get_item")]
    image_filename, post_parts = _get_filename(real_url)

    if jsonmigrator:
        try:
            image = json.loads(image_data)
        except ValueError:
            logger.warn("Could not retrieve image json contents at %s " % url)
            raise None, None, []

    else: # build object item for the pipeline
        image = {}
        image["_type"] = "Image"
        image["image"] = image_data

        image["creation_date"] = item.get("creation_date", None)
        image["modification_date"] = item.get("modification_date", None)
        image["_transitions"] = item.get("_transitions", "published")

        if not img_title:
            img_title = image_filename.split(".")[0].encode("utf-8")
        image["title"] = img_title

    image["_filename"] = image_filename
    return real_url, image, post_parts
    def set_options(self):
        """Scans the class "OPTIONS" attribute for blueprint options

         Set the options as a list of 1, 2,  3- or 4-tuple
         with each option: name, default_value, and
         type - one of "string"(default), "literal" and
         documentation string
         TODO: generate blueprint docs from option docs.

         Each "_" on the option name will accept either "_" or "-"
         literals in the transmogrifier.cfg  file
         """

        options = self.__class__.OPTIONS

        # Normalize options withdefault values:
        options = [(opt[0], None, "string", "") if len(opt) == 1 else
                   (opt[0], opt[1], "string", "") if len(opt) == 2 else
                   (opt[0], opt[1], opt[2], "") if len(opt) == 3 else
                   (opt[0], opt[1], opt[2], opt[3])
                                                 for opt in options]
        set_options = {}
        for name, default, type_, doc in options:
            value = self.options.get(name,
                self.options.get(name.replace("_", "-"), default))
            if type_ == "literal" and isinstance(value, basestring):
                value = ast.literal_eval(value)
            set_options[name] = value
        logger.info("Transmogrifier section %s configured with options:\n %s"
            % (self.name, pformat(set_options)))

        for opt_name, value in sorted(set_options.items()):
            if hasattr(self, opt_name):
                logger.error("Attention: Blueprint object in "
                    "section %s already has an attribute named %s - "
                    "overriding with option value %s - but this is "
                    "probably broken" % (self.name, opt_name, value))
            setattr(self, opt_name, value)
    def set_options(self):
        """Scans the class "OPTIONS" attribute for blueprint options

         Set the options as a list of 1, 2,  3- or 4-tuple
         with each option: name, default_value, and
         type - one of "string"(default), "literal" and
         documentation string
         TODO: generate blueprint docs from option docs.

         Each "_" on the option name will accept either "_" or "-"
         literals in the transmogrifier.cfg  file
         """

        options = self.__class__.OPTIONS

        # Normalize options withdefault values:
        options = [(opt[0], None, "string", "") if len(opt) == 1 else
                   (opt[0], opt[1], "string", "") if len(opt) == 2 else
                   (opt[0], opt[1], opt[2], "") if len(opt) == 3 else
                   (opt[0], opt[1], opt[2], opt[3]) for opt in options]
        set_options = {}
        for name, default, type_, doc in options:
            value = self.options.get(
                name, self.options.get(name.replace("_", "-"), default))
            if type_ == "literal" and isinstance(value, basestring):
                value = ast.literal_eval(value)
            set_options[name] = value
        logger.info("Transmogrifier section %s configured with options:\n %s" %
                    (self.name, pformat(set_options)))

        for opt_name, value in sorted(set_options.items()):
            if hasattr(self, opt_name):
                logger.error("Attention: Blueprint object in "
                             "section %s already has an attribute named %s - "
                             "overriding with option value %s - but this is "
                             "probably broken" % (self.name, opt_name, value))
            setattr(self, opt_name, value)
    def transmogrify(self, item):
        if not "__remote_url_fetch" in item:
            raise NothingToDoHere
        remote_url = item["__remote_url_fetch"]

        remote_url += (("/" if remote_url[-1] != "/" else  "" ) +
                        self.json_posfix)
        if self.remote_url_prefix and ":" not in remote_url[:7]:
            remote_url =  (self.remote_url_prefix.rstrip("/") + "/"
                           + remote_url.lstrip("/"))
        try:
            logger.info("Fetching remote item at %s " % remote_url)
            new_item = json.loads(urllib2.urlopen(remote_url).read())
        except Exception as error:
            logger.error("Could not retrieve and decode remote item "
                          "at %s, skipping" % remote_url)
            raise ThouShallNotPass
        if self.pop_path_prefix and "_path" in new_item:
            pathcomps = new_item["_path"].lstrip("/").split("/")
            pathcomps = pathcomps[self.pop_path_prefix:]
            new_item["_path"] = "/" + "/".join(pathcomps)
        item.update(new_item)
        item.pop("__remote_url_fetch", "")
        return item
示例#7
0
# Author: João S. O. Bueno

from Acquisition import aq_inner
from zope.component import getUtility
from sc.transmogrifier import logger


import transaction

from sc.transmogrifier.utils import blueprint
from sc.transmogrifier.utils import BluePrintBoiler

try:
    from zope.intid.interfaces import IIntIds
except ImportError:
    logger.error("five.intid is not installed  -t he set_intid blueprint won't work at all")


@blueprint("sc.transmogrifier.utils.set_intid")
class SetIntId(BluePrintBoiler):
    """
    In order to be a target to a dexterity related item field, an object has to have
    an "IntId". It  is created by objects created in the GUI, but not for
    objects created with the constructor, updater and reindex blueprints.

    This should be used next to them (after constructor, of course)

    Objects that already have an intid are not affectd by this call -
    the inner "intids.register" call just returns the existing
    int_id
    """