Example #1
0
    def build_artifact(self, artifact):
        ctx = get_ctx()
        feed_source = self.source
        blog = feed_source.parent

        summary = get(blog, feed_source.blog_summary_field) or ''
        subtitle_type = ('html' if hasattr(summary, '__html__') else 'text')
        blog_author = unicode(get(blog, feed_source.blog_author_field) or '')
        generator = ('Lektor Atom Plugin',
                     'https://github.com/ajdavis/lektor-atom',
                     pkg_resources.get_distribution('lektor-atom').version)

        project_id = ctx.env.load_config().base_url
        if not project_id:
            project_id = ctx.env.project.id
        feed = AtomFeed(
            title=feed_source.feed_name,
            subtitle=unicode(summary),
            subtitle_type=subtitle_type,
            author=blog_author,
            feed_url=url_to(feed_source, external=True),
            url=url_to(blog, external=True),
            id=get_id(project_id),
            generator=generator)

        if feed_source.items:
            # "feed_source.items" is a string like "site.query('/blog')".
            expr = Expression(ctx.env, feed_source.items)
            items = expr.evaluate(ctx.pad)
        else:
            items = blog.children

        if feed_source.item_model:
            items = items.filter(F._model == feed_source.item_model)

        order_by = '-' + feed_source.item_date_field
        items = items.order_by(order_by).limit(int(feed_source.limit))

        for item in items:
            item_author_field = feed_source.item_author_field
            item_author = get(item, item_author_field) or blog_author

            feed.add(
                get_item_title(item, feed_source.item_title_field),
                get_item_body(item, feed_source.item_body_field),
                xml_base=url_to(item, external=True),
                url=url_to(item, external=True),
                content_type='html',
                id=get_id(u'%s/%s' % (
                    project_id,
                    item['_path'].encode('utf-8'))),
                author=item_author,
                updated=get_item_updated(item, feed_source.item_date_field))

        with artifact.open('wb') as f:
            f.write(feed.to_string().encode('utf-8'))
Example #2
0
    def build_artifact(self, artifact):
        ctx = get_ctx()
        source = self.source

        appcast = AppCast(
            title=source.cast_name,
            link=url_to(source, external=True),
        )

        try:
            expr = Expression(ctx.env, source.items)
        except AttributeError:
            items = source.parent.children
        else:
            items = expr.evaluate(ctx.pad)

        if source.item_model:
            items = items.filter(F._model == source.item_model)
        items = items.order_by('-build_number')

        for item in items:
            with ctx.changed_base_url(item.url_path):
                description = six.text_type(markupsafe.escape(item['note']))

            try:
                offset = int(source.timezone)
            except ValueError:
                tzinfo = pytz.timezone(source.timezone)
            else:
                tzinfo = pytz.FixedOffset(offset)
            pub_date = item['pub_datetime'].replace(tzinfo=tzinfo)

            try:
                build_number = str(item['build_number'])
                if '.' in build_number:
                    build_number = build_number.rstrip('0').rstrip('.')
                appcast.add(
                    title='Version {}'.format(item['version']),
                    description=description,
                    pub_date=pub_date,
                    url=item['download_url'],
                    build=build_number,
                    version=item['version'],
                    length=item['length'],
                    dsasign=item['dsa_signature'],
                    minsysver=item['min_sysver'],
                )
            except Exception as e:
                msg = '{}: {}'.format(item.id, e)
                click.echo(click.style('E', fg='red') + ' ' + msg)

        with artifact.open('wb') as f:
            f.write(appcast.to_string().encode('utf-8'))
Example #3
0
File: multi.py Project: jab/lektor
class ChoiceSource(object):

    def __init__(self, env, options):
        source = options.get('source')
        if source is not None:
            self.source = Expression(env, source)
            self.choices = None
            item_key = options.get('item_key') or '{{ this._id }}'
            item_label = options.get('item_label') or '{{ this._id }}'
        else:
            self.source = None
            self.choices = parse_choices(options.get('choices'))
            item_key = options.get('item_key') or '{{ this.0 }}'
            item_label = options.get('item_label') or '{{ this.1 }}'
        self.item_key = FormatExpression(env, item_key)
        self.item_label = FormatExpression(env, item_label)

    @property
    def has_choices(self):
        return self.source is not None or self.choices is not None

    def iter_choices(self, pad):
        if self.choices is not None:
            iterable = self.choices
        else:
            iterable = self.source.evaluate(pad)

        for item in iterable or ():
            key = self.item_key.evaluate(pad, this=item)
            label = self.item_label.evaluate(pad, this=item)
            yield key, label
Example #4
0
class ChoiceSource(object):

    def __init__(self, env, options):
        source = options.get('source')
        if source is not None:
            self.source = Expression(env, source)
            self.choices = None
            item_key = options.get('item_key') or '{{ this._id }}'
            item_label = options.get('item_label')
        else:
            self.source = None
            self.choices = _parse_choices(options)
            item_key = options.get('item_key') or '{{ this.0 }}'
            item_label = options.get('item_label')
        self.item_key = FormatExpression(env, item_key)
        if item_label is not None:
            item_label = FormatExpression(env, item_label)
        self.item_label = item_label

    @property
    def has_choices(self):
        return self.source is not None or self.choices is not None

    def iter_choices(self, pad, record=None, alt=PRIMARY_ALT):
        values = {}
        if record is not None:
            values['record'] = record
        if self.choices is not None:
            iterable = self.choices
        else:
            try:
                iterable = self.source.evaluate(pad, alt=alt, values=values)
            except Exception:
                traceback.print_exc()
                iterable = ()

        for item in iterable or ():
            key = self.item_key.evaluate(pad, this=item, alt=alt,
                                         values=values)

            # If there is a label expression, use it.  Since in that case
            # we only have one language to fill in, we fill it in for the
            # default language
            if self.item_label is not None:
                label = {
                    'en': self.item_label.evaluate(pad, this=item, alt=alt,
                                                   values=values)
                }

            # Otherwise we create a proper internationalized key out of
            # our target label
            else:
                if isinstance(item, (tuple, list)) and len(item) == 2:
                    label = item[1]
                elif hasattr(item, 'get_record_label_i18n'):
                    label = item.get_record_label_i18n()
                else:
                    label = {'en': item['_id']}

            yield key, label
Example #5
0
File: multi.py Project: jab/lektor
 def __init__(self, env, options):
     source = options.get('source')
     if source is not None:
         self.source = Expression(env, source)
         self.choices = None
         item_key = options.get('item_key') or '{{ this._id }}'
         item_label = options.get('item_label') or '{{ this._id }}'
     else:
         self.source = None
         self.choices = parse_choices(options.get('choices'))
         item_key = options.get('item_key') or '{{ this.0 }}'
         item_label = options.get('item_label') or '{{ this.1 }}'
     self.item_key = FormatExpression(env, item_key)
     self.item_label = FormatExpression(env, item_label)
Example #6
0
 def eval_expr(expr, **kwargs):
     expr = Expression(env, expr)
     return expr.evaluate(**kwargs)
Example #7
0
 def items(self):
     items_exp = Expression(self.pad.env, self.plugin.get_items_expression())
     return items_exp.evaluate(self.pad, this=self, values={'tag': self.tag})
Example #8
0
 def get_all_tags(self, parent):
     exp = Expression(self.env, self.get_tags_expression())
     tags = exp.evaluate(parent.pad, values={'parent': parent})
     return sorted(set(tags))
Example #9
0
 def eval_expr(expr, **kwargs):
     expr = Expression(env, expr)
     return expr.evaluate(**kwargs)
Example #10
0
class ChoiceSource(object):
    def __init__(self, env, options):
        source = options.get("source")
        if source is not None:
            self.source = Expression(env, source)
            self.choices = None
            item_key = options.get("item_key") or "{{ this._id }}"
            item_label = options.get("item_label")
        else:
            self.source = None
            self.choices = _parse_choices(options)
            item_key = options.get("item_key") or "{{ this.0 }}"
            item_label = options.get("item_label")
        self.item_key = FormatExpression(env, item_key)
        if item_label is not None:
            item_label = FormatExpression(env, item_label)
        self.item_label = item_label

    @property
    def has_choices(self):
        return self.source is not None or self.choices is not None

    def iter_choices(self, pad, record=None, alt=PRIMARY_ALT):
        values = {}
        if record is not None:
            values["record"] = record
        if self.choices is not None:
            iterable = self.choices
        else:
            try:
                iterable = self.source.evaluate(pad, alt=alt, values=values)
            except Exception:
                traceback.print_exc()
                iterable = ()

        for item in iterable or ():
            key = self.item_key.evaluate(pad,
                                         this=item,
                                         alt=alt,
                                         values=values)

            # If there is a label expression, use it.  Since in that case
            # we only have one language to fill in, we fill it in for the
            # default language
            if self.item_label is not None:
                label = {
                    "en":
                    self.item_label.evaluate(pad,
                                             this=item,
                                             alt=alt,
                                             values=values)
                }

            # Otherwise we create a proper internationalized key out of
            # our target label
            else:
                if isinstance(item, (tuple, list)) and len(item) == 2:
                    label = item[1]
                elif hasattr(item, "get_record_label_i18n"):
                    label = item.get_record_label_i18n()
                else:
                    label = {"en": item["_id"]}

            yield key, label
Example #11
0
def query(lektor_env, lektor_pad, query_expr):
    record = lektor_pad.root
    expr = Expression(lektor_env, query_expr)
    return expr.evaluate(lektor_pad, this=record, alt=record.alt)
Example #12
0
    def build_artifact(self, artifact):
        ctx = get_ctx()
        feed_source = self.source
        blog = feed_source.parent

        summary = get(blog, feed_source.blog_summary_field) or ''
        if hasattr(summary, '__html__'):
            subtitle_type = 'html'
            summary = text_type(summary.__html__())
        else:
            subtitle_type = 'text'
        blog_author = text_type(get(blog, feed_source.blog_author_field) or '')
        generator = ('Lektor Atom Plugin',
                     'https://github.com/ajdavis/lektor-atom',
                     pkg_resources.get_distribution('lektor-atom').version)

        feed = AtomFeed(title=feed_source.feed_name,
                        subtitle=summary,
                        subtitle_type=subtitle_type,
                        author=blog_author,
                        feed_url=url_to(feed_source, external=True),
                        url=url_to(blog, external=True),
                        id=get_id(ctx.env.project.id),
                        generator=generator)

        if feed_source.items:
            # "feed_source.items" is a string like "site.query('/blog')".
            expr = Expression(ctx.env, feed_source.items)
            items = expr.evaluate(ctx.pad)
        else:
            items = blog.children

        if feed_source.item_model:
            items = items.filter(F._model == feed_source.item_model)

        order_by = '-' + feed_source.item_date_field
        items = items.order_by(order_by).limit(int(feed_source.limit))

        for item in items:
            try:
                item_author_field = feed_source.item_author_field
                item_author = get(item, item_author_field) or blog_author

                feed.add(
                    get_item_title(item, feed_source.item_title_field),
                    get_item_body(item, feed_source.item_body_field),
                    xml_base=url_to(item, external=True),
                    url=url_to(item, external=True),
                    content_type='html',
                    id=get_id(
                        u'%s/%s' %
                        (ctx.env.project.id, item['_path'].encode('utf-8'))),
                    author=item_author,
                    updated=get_item_updated(item,
                                             feed_source.item_date_field))
            except Exception as exc:
                msg = '%s: %s' % (item.id, exc)
                click.echo(click.style('E', fg='red') + ' ' + msg)

        with artifact.open('wb') as f:
            f.write(feed.to_string().encode('utf-8'))