Ejemplo n.º 1
0
    def _renderImpl(self, **kwargs):
        """ All VML tags must override the _renderImpl() method, which handles all of the attribute
            parsing and state modification before the actual rendering of the Mako template. This
            method does not return anything, it is meant to modify the tag state.
        """

        a      = self.attrs
        url    = a.get(TagAttributesEnum.URL, None, kwargs)
        start  = a.get(TagAttributesEnum.START, 1, kwargs)
        aspect = a.getAsEnumerated(TagAttributesEnum.ASPECT_RATIO, AspectRatioEnum, None,
                                   kwargs, allowFailure=True)
        if not aspect:
            aspect = a.getAsFloat(TagAttributesEnum.ASPECT_RATIO, None)

        if url:
            try:
                result = OEmbedRequest.get(url, self._OEMBED)
                if self._processor.debug:
                    self._log.write(u'SlideShare oEmbed result: ' + unicode(result))

                if not aspect:
                    width  = result.get('width', None)
                    height = result.get('height', None)
                    if width and height:
                        try:
                            aspect = float(width)/float(height + 20.0)
                        except ValueError:
                            pass

                a.render['code'] = unicode(result.get('slideshow_id', ''))
            except Exception, err:
                if self._processor.debug:
                   self._log.writeError(u'SlideShare oEmbed error', err)

                a.render['code'] = u''
Ejemplo n.º 2
0
    def _renderImpl(self, **kwargs):
        """ All VML tags must override the _renderImpl() method, which handles all of the attribute
            parsing and state modification before the actual rendering of the Mako template. This
            method does not return anything, it is meant to modify the tag state.
        """

        a     = self.attrs
        url   = a.get(TagAttributesEnum.URL, None, kwargs)
        play  = a.getAsBool(TagAttributesEnum.AUTO_PLAY, False, kwargs)
        color = a.getAsColorMixer(TagAttributesEnum.COLOR, None)
        if not color:
            if a.focalColors.baseColor.lightness > a.backColors.baseColor.lightness:
                color = a.focalColors.baseColor.bareHex
            else:
                color = a.backColors.baseColor.bareHex
        else:
            color = color.bareHex

        aspect = a.getAsEnumerated(TagAttributesEnum.ASPECT_RATIO, AspectRatioEnum, None,
                                   kwargs, allowFailure=True)
        if not aspect:
            aspect = a.getAsFloat(TagAttributesEnum.ASPECT_RATIO, None)

        if url:
            try:
                result = OEmbedRequest.get(url, self._OEMBED)
                if self._processor.debug:
                    self._log.write(u'Vimeo oEmbed result: ' + unicode(result))

                if not aspect:
                    width  = result.get('width', None)
                    height = result.get('height', None)
                    if width and height:
                        try:
                            aspect = float(width)/float(height + 20.0)
                        except ValueError:
                            pass

                res = VimeoTag._URL_RE.search(result.get('html', ''))

                a.render['code'] = res.group('code')
            except Exception, err:
                if self._processor.debug:
                    self._log.writeError(u'Vimeo oEmbed error', err)

                a.render['code'] = u''
Ejemplo n.º 3
0
    def _renderImpl(self, **kwargs):
        """ All VML tags must override the _renderImpl() method, which handles all of the attribute
            parsing and state modification before the actual rendering of the Mako template. This
            method does not return anything, it is meant to modify the tag state.
        """

        # As mentioned, the a property is my convention for self.attrs, which is an instance of
        # vmi.web.vml.render.AttributeData.
        a = self.attrs

        # The parsed VML attributes for a tag are accessed through the AttributeData class using the
        # various get*() methods. In this case I use a.get(), which returns the raw string value for
        # given attributes. As you can see the first argument is a list of possible attribute names.
        # To make VizmeML as flexible as possible, we support in many case multiple names for an
        # attribute. A good example of this is the TagAttributeEnum.URL case, which allows url
        # properties to be specified as url:... or href:... or src:..., which are the three possible
        # values used in html. Multiple attribute names also allow for long and short versions of
        # names and the user can decide which to use. Here the START and TIME attribute names are
        # combined, which means the start time can be specified as start:..., begin:..., or time:...
        # The second argument in this method is the default value to return if the property was not
        # specified. Finally, kwargs is passed in as an override. This is optional, but I've started
        # doing it by default because it makes refactoring easier. The kwargs acts as an override,
        # meaning if start, begin, or time is specified in kwargs, that value will be used instead
        # of accessing the attribute directly. This is quite useful when you want to modify the
        # value of an attribute from a subclass but let the parent class still handle the result.
        start = a.get(TagAttributesEnum.START + TagAttributesEnum.TIME, None, kwargs)

        # Getting the url in the same, raw fashion as the start property.
        url  = a.get(TagAttributesEnum.URL, None, kwargs)
        code = a.get(TagAttributesEnum.CODE, None, kwargs)

        # Here play is a boolean value so getAsBool is used instead of get, which converts the value
        # from a string into a boolean where 'true', 'yes', 'on', and '1' all equate to True and
        # anything else is False.
        play = a.getAsBool(TagAttributesEnum.AUTO_PLAY, False, kwargs)

        size = a.getAsTShirtSize(
            TagAttributesEnum.SIZE,
            None,
            kwargs,
            allowFailure=True,
            values=[320, 480, 640, 800, 1024, 1280, 0])

        if size is None:
            size = a.getAsInt(TagAttributesEnum.SIZE, 0, kwargs)

        # Get as enumerated returns an enumerated result based on the enumeration class specified as
        # the second argument. The third argument then becomes the default value to return if the
        # value is not specified OR the value specified doesn't match any of the values in the
        # enumeration class. If you look at the AspectRatioEnum class you will see entries like:
        #       WIDESCREEN  = [1.7778, ['wide', 'widescreen', 'sixteen-nine']]
        # where the first element of the list is the enumerated value and the second element is a
        # list of possible values the user can specify that equate to the given enumeration entry.
        # In this case, if the user specifies:
        #   [#youtube aspect:widescreen ...]
        #   [#youtube aspect:wide ...]
        # the getAsEnumerated method would return 1.7778. However, if the user specified:
        #   [#youtube aspect:foo ...]
        # None would be returned because foo isn't specified in any of the enumeration values within
        # the class and the default value is set to None.
        #
        # Another key point here is the use of the allowFailure argument. When True it means an
        # invalid value will fail silently instead of logging as an error. This error logging will
        # be presented to the user during page previews once we get it implemented. The reason for
        # allowing silent failure in this case (which is the most common use of it) is that the
        # aspect ratio can be specified either as an enumerated value, or directly as a float. So
        # if this enumerated access fails, it means the value is likely a float. We don't want to
        # log the error, instead we want to try to access the attribute as a float instead as is
        # done below.
        aspect = a.getAsEnumerated(
            TagAttributesEnum.ASPECT_RATIO, AspectRatioEnum, None, kwargs, allowFailure=True)

        # Here if the aspect ratio is None the aspect ratio should be accessed as a float value.
        # Now this case doesn't allow failure since it is the last attempt to access the attribute.
        # If the attribute get fails this time, the failure should be logged because the attribute
        # was specified incorrectly. NOTE: failures only occur if the property was specified but
        # could not be typecast/converted into the format specified by the get method. No failure
        # occurs if the property just wasn't specified.
        if not aspect:
            aspect = a.getAsFloat(TagAttributesEnum.ASPECT_RATIO, None)

        # Finally we've got all of the attributes we need for the tag. I try to put these all first
        # because it makes it easier to maintain and reference. Now we have to modify the states
        # according to the given properties.

        if url:
            try:
                result = OEmbedRequest.get(url, self._OEMBED)
                if self._processor.debug:
                    self._log.write(u'YouTube oEmbed result: ' + unicode(result))

                if not aspect:
                    width  = result.get('width', None)
                    height = result.get('height', None)
                    if width and height:
                        try:
                            aspect = float(width)/float(height + 20.0)
                        except ValueError:
                            pass

                res = YouTubeTag._URL_RE.search(result.get('html', ''))

                a.render['code'] = res.group('code')
            except Exception, err:
                if self._processor.debug:
                    self._log.writeError(u'YouTube oEmbed error', err)

                a.render['code'] = u''
Ejemplo n.º 4
0
    def _renderImpl(self, **kwargs):
        """ All VML tags must override the _renderImpl() method, which handles all of the attribute
            parsing and state modification before the actual rendering of the Mako template. This
            method does not return anything, it is meant to modify the tag state.
        """

        a      = self.attrs
        url    = a.get(TagAttributesEnum.URL, None, kwargs)
        aspect = a.getAsEnumerated(
            TagAttributesEnum.ASPECT_RATIO,
            AspectRatioEnum,
            None,
            kwargs,
            allowFailure=True
        )

        if not aspect:
            aspect = a.getAsFloat(TagAttributesEnum.ASPECT_RATIO, None)

        if url:
            try:
                result = OEmbedRequest.get(url, self._OEMBED)
                if self._processor.debug:
                    self._log.write(u'Hulu oEmbed result: ' + unicode(result))

                if not aspect:
                    width  = result.get('width', None)
                    height = result.get('height', None)
                    if width and height:
                        try:
                            aspect = float(width)/float(height + 20.0)
                        except ValueError:
                            pass

                res = HuluTag._URL_RE.search(result.get('embed_url', ''))

                code = res.group('code')

                start, startKeyData = a.get(TagAttributesEnum.START, None, kwargs, returnKey=True)
                if start:
                    try:
                        start = int(start)
                    except Exception, err:
                        start = 0

                        MarkupAttributeError(
                            tag=self,
                            errorDef=self._INVALID_ERROR_FRAME_DEF,
                            attribute=startKeyData[0],
                            attributeData=startKeyData[1],
                            attributeGroup=TagAttributesEnum.START,
                            rawValue=start).log()

                stop, stopKeyData = a.get(TagAttributesEnum.STOP, None, kwargs, returnKey=True)
                if stop:
                    try:
                        stop = int(stop)
                    except Exception, err:
                        stop = 0

                        MarkupAttributeError(
                            tag=self,
                            errorDef=self._INVALID_ERROR_FRAME_DEF,
                            attribute=stopKeyData[0],
                            attributeData=stopKeyData[1],
                            attributeGroup=TagAttributesEnum.STOP,
                            rawValue=stop).log()


                thumb, thumbKeyData = a.get(TagAttributesEnum.THUMB, None, kwargs, returnKey=True)
                if thumb:
                    try:
                        thumb = int(thumb)
                    except Exception, err:
                        thumn = 0

                        MarkupAttributeError(
                            tag=self,
                            errorDef=self._INVALID_ERROR_FRAME_DEF,
                            attribute=thumbKeyData[0],
                            attributeData=thumbKeyData[1],
                            attributeGroup=TagAttributesEnum.THUMB,
                            rawValue=thumb).log()