示例#1
0
    def process_data(self, data, datamodel, pad):
        # Automatically fill in slugs
        if is_undefined(data['_slug']):
            data['_slug'] = self.get_default_slug(data, pad)
        else:
            data['_slug'] = data['_slug'].strip('/')

        # For attachments figure out the default attachment type if it's
        # not yet provided.
        if is_undefined(data['_attachment_type']) and \
           data['_attachment_for']:
            data['_attachment_type'] = self.get_attachment_type(data['_path'])

        # Automatically fill in templates
        if is_undefined(data['_template']):
            data['_template'] = datamodel.get_default_template_name()
示例#2
0
    def all_children(self, typ):
        from .objtyp import ObjType

        if is_undefined(typ):
            from sqlalchemy.sql import false
            return ObjType.q.filter(false())
        return ObjType.get_mod(typ).q.filter_by(parent=self)
示例#3
0
文件: db.py 项目: jab/lektor
    def process_data(self, data, datamodel, pad):
        # Automatically fill in slugs
        if is_undefined(data['_slug']):
            data['_slug'] = self.get_default_slug(data, pad)
        else:
            data['_slug'] = data['_slug'].strip('/')

        # For attachments figure out the default attachment type if it's
        # not yet provided.
        if is_undefined(data['_attachment_type']) and \
           data['_attachment_for']:
            data['_attachment_type'] = self.get_attachment_type(data['_path'])

        # Automatically fill in templates
        if is_undefined(data['_template']):
            data['_template'] = datamodel.get_default_template_name()
示例#4
0
文件: object.py 项目: smurfix/pybble
	def all_children(self,typ):
		from .objtyp import ObjType

		if is_undefined(typ):
			from sqlalchemy.sql import false
			return ObjType.q.filter(false())
		return ObjType.get_mod(typ).q.filter_by(parent=self)
示例#5
0
    def is_hidden(self):
        """Indicates if a record is hidden.  A record is considered hidden
        if the record itself is hidden or the parent is.
        """
        if not is_undefined(self._data['_hidden']):
            return self._data['_hidden']

        parent = self.parent
        return parent is not None and parent.is_hidden
示例#6
0
def test_expressions():
    expr = env.compile_expression("foo")
    assert expr() is None
    assert expr(foo=42) == 42
    expr2 = env.compile_expression("foo", undefined_to_none=False)
    assert is_undefined(expr2())

    expr = env.compile_expression("42 + foo")
    assert expr(foo=42) == 84
示例#7
0
    def test_expressions(self, env):
        expr = env.compile_expression("foo")
        assert expr() is None
        assert expr(foo=42) == 42
        expr2 = env.compile_expression("foo", undefined_to_none=False)
        assert is_undefined(expr2())

        expr = env.compile_expression("42 + foo")
        assert expr(foo=42) == 84
示例#8
0
文件: utils.py 项目: sunliwen/lektor
 def default(self, o):  # pylint: disable=method-hidden
     if is_undefined(o):
         return None
     if isinstance(o, datetime):
         return http_date(o)
     if isinstance(o, uuid.UUID):
         return str(o)
     if hasattr(o, '__html__'):
         return text_type(o.__html__())
     return json.JSONEncoder.default(self, o)
示例#9
0
 def default(self, o):
     if is_undefined(o):
         return None
     if isinstance(o, datetime):
         return http_date(o)
     if isinstance(o, uuid.UUID):
         return str(o)
     if hasattr(o, '__html__'):
         return unicode(o.__html__())
     return json.JSONEncoder.default(self, o)
示例#10
0
 def default(self, o):  # pylint: disable=method-hidden
     if is_undefined(o):
         return None
     if isinstance(o, datetime):
         return http_date(o)
     if isinstance(o, uuid.UUID):
         return str(o)
     if hasattr(o, '__html__'):
         return text_type(o.__html__())
     return json.JSONEncoder.default(self, o)
示例#11
0
文件: utils.py 项目: rmoorman/lektor
 def default(self, o):
     if is_undefined(o):
         return None
     if isinstance(o, datetime):
         return http_date(o)
     if isinstance(o, uuid.UUID):
         return str(o)
     if hasattr(o, '__html__'):
         return unicode(o.__html__())
     return json.JSONEncoder.default(self, o)
    def on_before_build(self, source, **extra):
        config = self.get_config()

        # if we're building an attachment & hidden isn't explicitly set
        if (getattr(source, 'is_attachment', False) and
            is_undefined(source._data['_hidden'])
            ):

            # see if this extension is hidden by default
            ext = os.path.splitext(source.path)[1][1:]
            source._data['_hidden'] = config.get('hidden.{}'.format(ext), False)
示例#13
0
    def on_before_build(self, source, **extra):
        config = self.get_config()

        # if we're building an attachment & hidden isn't explicitly set
        if (getattr(source, 'is_attachment', False)
                and is_undefined(source._data['_hidden'])):

            # see if this extension is hidden by default
            ext = os.path.splitext(source.path)[1][1:]
            source._data['_hidden'] = config.get('hidden.{}'.format(ext),
                                                 False)
def test_pub_date_ignores_nochange(git_repo, pad, pub_date, now):
    ignored_date = pub_date - datetime.timedelta(minutes=5)
    git_repo.commit('site/content/contents.lr',
                    ignored_date,
                    message="Ignore this commit [nochange]")
    git_repo.commit('site/content/contents.lr', pub_date)
    git_repo.commit('site/content/contents.lr',
                    now,
                    message="Also ignore this commit [nochange]")
    assert pad.root['pub_date'] == pub_date
    assert jinja2.is_undefined(pad.root['last_mod'])
示例#15
0
        def index_pages(jinja_ctx, index_name, alt=PRIMARY_ALT):
            pad = jinja_ctx.resolve('site')
            if jinja2.is_undefined(pad):
                return pad

            config = self.read_config()
            try:
                index_root = config.get_index_root(index_name, pad, alt)
            except NoSuchIndex as exc:
                return jinja_ctx.environment.undefined('index_pages: %s' % exc)
            return IndexPages(index_root)
 def value_from_raw(self, raw):
     value = super(GitTimestampType, self).value_from_raw(raw)
     if jinja2.is_undefined(value):
         options = self.options
         value = GitTimestampDescriptor(
             raw,
             ignore_commits=options.get('ignore_commits'),
             strategy=options.get('strategy', 'last'),
             skip_first_commit=bool_from_string(
                 options.get('skip_first_commit', False)),
         )
     return value
示例#17
0
文件: jinja.py 项目: roman901/jablogs
def angular_filter(value):
    """
    A filter to tell Jinja2 that a variable is for the AngularJS template
    engine.
    If the variable is undefined, its name will be used in the AngularJS
    template, otherwise, its content will be used.
    """

    if is_undefined(value):
        return '{{{{{}}}}}'.format(value._undefined_name)
    if type(value) is bool:
        value = repr(value).lower()
    return '{{{{{}}}}}'.format(value)
示例#18
0
文件: db.py 项目: jab/lektor
    def is_hidden(self):
        """If a record is hidden it will not be processed.  This is related
        to the expose flag that can be set on the datamodel.
        """
        if not is_undefined(self._data['_hidden']):
            return self._data['_hidden']

        node = self
        while node is not None:
            if not node.datamodel.expose:
                return True
            node = node.parent

        return False
示例#19
0
    def is_hidden(self):
        """Indicates if a record is hidden.  A record is considered hidden
        if the record itself got hidden or any of the parent is.
        """
        if not is_undefined(self._data['_hidden']):
            return self._data['_hidden']

        node = self.parent
        while node is not None:
            if node.is_hidden:
                return True
            node = node.parent

        return False
示例#20
0
def angular_filter(value):
    """
    A filter to tell Jinja2 that a variable is for the AngularJS template
    engine. 

    If the variable is undefined, its name will be used in the AngularJS
    template, otherwise, its content will be used.
    """

    if is_undefined(value):
        return '{{{{{}}}}}'.format(value._undefined_name)
    if type(value) is bool:
        value = unicode(value).lower()
    return '{{{{{}}}}}'.format(value)
示例#21
0
文件: db.py 项目: jab/lektor
    def is_hidden(self):
        """If a record is hidden it will not be processed.  This is related
        to the expose flag that can be set on the datamodel.
        """
        if not is_undefined(self._data['_hidden']):
            return self._data['_hidden']

        node = self
        while node is not None:
            if not node.datamodel.expose:
                return True
            node = node.parent

        return False
示例#22
0
def provider_login_url(context, provider, **params):
    # return ProviderLoginURLNode(provider_id, params).render(context)

    request = context["request"]
    if isinstance(provider, str):
        provider = providers.registry.by_id(provider, request)
    auth_params = params.get("auth_params", None)
    scope = params.get("scope", None)
    process = params.get("process", None)
    if is_undefined(scope):
        del params["scope"]
    if is_undefined(auth_params):
        del params["auth_params"]
    if "next" not in params:
        next_url = get_request_param(request, "next")
        if next_url:
            params["next"] = next_url
        elif process == "redirect":
            params["next"] = request.get_full_path()
    else:
        if not params["next"]:
            del params["next"]
    # get the login url and append params as url parameters
    return provider.get_login_url(request, **params)
示例#23
0
文件: app.py 项目: BadSeby/scout
    def client_filter(value):
      """Pass through variable in Jinja2 to client side template.

      The filter tells Jinja2 that a variable is for a client side
      template engine.

      If the variable is undefined, its name will be used in the
      client side template, otherwise, its content will be used.
      """

      if is_undefined(value):
          return '{{{{{}}}}}'.format(value._undefined_name)
      if type(value) is bool:
          value = repr(value).lower()
      return '{{{{{}}}}}'.format(value)
示例#24
0
文件: app.py 项目: BadSeby/scout
        def client_filter(value):
            """Pass through variable in Jinja2 to client side template.

      The filter tells Jinja2 that a variable is for a client side
      template engine.

      If the variable is undefined, its name will be used in the
      client side template, otherwise, its content will be used.
      """

            if is_undefined(value):
                return "{{{{{}}}}}".format(value._undefined_name)
            if type(value) is bool:
                value = repr(value).lower()
            return "{{{{{}}}}}".format(value)
    def get_type_name(self, this):
        polymorphic_type_expr = self.options.get('polymorphic_type')
        if polymorphic_type_expr:
            expr = Expression(this.pad.env, polymorphic_type_expr)
            type_name = expr.evaluate(this.pad, this, alt=this.alt)
            if jinja2.is_undefined(type_name):
                raise TypeResolutionFailed(type_name._undefined_message)
            return type_name

        field_name = self.raw_value.name
        selector_field = field_name + "_type"
        try:
            return this[selector_field]
        except KeyError:
            raise TypeResolutionFailed("field %r does not exist" %
                                       selector_field)
示例#26
0
文件: jinja2.py 项目: cslms/cs-server
def breadcrumbs(ctx, page=None):
    """
    Return a list of breadcrumb links for the given page or request.
    """

    if is_undefined(page):
        page = ctx.get('page', None)
    if hasattr(page, 'breadcrumbs'):
        return page.breadcrumbs()
    elif page is None:
        return breadcrumbs_from_request(ctx['request'])
    elif isinstance(page, str):
        raise ValueError('cannot extract breadcrumbs from string')
    elif isinstance(page, collections.Sequence):
        return list(page)
    else:
        return breadcrumbs_from_page(page)
示例#27
0
def breadcrumbs(ctx, page=None):
    """
    Return a list of breadcrumb links for the given page or request.
    """

    if is_undefined(page):
        page = ctx.get('page', None)
    if hasattr(page, 'breadcrumbs'):
        return page.breadcrumbs()
    elif page is None:
        return breadcrumbs_from_request(ctx['request'])
    elif isinstance(page, str):
        raise ValueError('cannot extract breadcrumbs from string')
    elif isinstance(page, collections.Sequence):
        return list(page)
    else:
        return breadcrumbs_from_page(page)
示例#28
0
文件: jinja.py 项目: smurfix/pybble
				def can_do(env, obj=None, objtyp=None):
					if objtyp is None:
						if isinstance(obj,(int,long)):
							objtyp=obj
							obj=None
					if obj is None:
						obj = env.get('obj',None)
					if isinstance(obj,basestring):
						obj = Object.by_oid(obj)
					u = getattr(request,"user",None)
					if current_app.config.DEBUG_ACCESS:
						access_logger.debug("can_do_{}: {} {} {} {}".format(b, u,obj,objtyp,a))
					if not u or is_undefined(obj):
						return False
					if a > PERM_NONE:
						return u.can_do(obj, objtyp=objtyp) >= a
					elif a == PERM_ADD:
						return u.can_do(obj, objtyp=obj, new_objtyp=objtyp, want=a) == a
					else:
						return u.can_do(obj, objtyp=objtyp, want=a) == a
示例#29
0
def docker_link(value, format='{addr}:{port}'):
    """ Given a Docker Link environment variable value, format it into something else.

    This first parses a Docker Link value like this:

        DB_PORT=tcp://172.17.0.5:5432

    Into a dict:

    ```python
    {
      'proto': 'tcp',
      'addr': '172.17.0.5',
      'port': '5432'
    }
    ```

    And then uses `format` to format it, where the default format is '{addr}:{port}'.

    More info here: [Docker Links](https://docs.docker.com/userguide/dockerlinks/)

    :param value: Docker link (from an environment variable)
    :param format: The format to apply. Supported placeholders: `{proto}`, `{addr}`, `{port}`
    :return: Formatted string
    """
    # pass undefined values on down the pipeline
    if is_undefined(value):
        return value

    # Parse the value
    m = re.match(r'(?P<proto>.+)://' r'(?P<addr>.+):' r'(?P<port>.+)$', value)
    if not m:
        raise ValueError(
            'The provided value does not seems to be a Docker link: {0}'.
            format(value))
    d = m.groupdict()

    # Format
    return format.format(**d)
示例#30
0
def docker_link(value, format='{addr}:{port}'):
    """ Given a Docker Link environment variable value, format it into something else.

    This first parses a Docker Link value like this:

        DB_PORT=tcp://172.17.0.5:5432

    Into a dict:

    ```python
    {
      'proto': 'tcp',
      'addr': '172.17.0.5',
      'port': '5432'
    }
    ```

    And then uses `format` to format it, where the default format is '{addr}:{port}'.

    More info here: [Docker Links](https://docs.docker.com/userguide/dockerlinks/)

    :param value: Docker link (from an environment variable)
    :param format: The format to apply. Supported placeholders: `{proto}`, `{addr}`, `{port}`
    :return: Formatted string
    """
    # pass undefined values on down the pipeline
    if(is_undefined(value)):
        return value

    # Parse the value
    m = re.match(r'(?P<proto>.+)://' r'(?P<addr>.+):' r'(?P<port>.+)$', value)
    if not m:
        raise ValueError('The provided value does not seems to be a Docker link: {0}'.format(value))
    d = m.groupdict()

    # Format
    return format.format(**d)
示例#31
0
文件: jinja.py 项目: smurfix/pybble
 def can_do(env, obj=None, objtyp=None):
     if objtyp is None:
         if isinstance(obj, (int, long)):
             objtyp = obj
             obj = None
     if obj is None:
         obj = env.get('obj', None)
     if isinstance(obj, basestring):
         obj = Object.by_oid(obj)
     u = getattr(request, "user", None)
     if current_app.config.DEBUG_ACCESS:
         access_logger.debug("can_do_{}: {} {} {} {}".format(
             b, u, obj, objtyp, a))
     if not u or is_undefined(obj):
         return False
     if a > PERM_NONE:
         return u.can_do(obj, objtyp=objtyp) >= a
     elif a == PERM_ADD:
         return u.can_do(obj,
                         objtyp=obj,
                         new_objtyp=objtyp,
                         want=a) == a
     else:
         return u.can_do(obj, objtyp=objtyp, want=a) == a
示例#32
0
 def test_returns_undefined(self, jinja_env):
     result = limit_dependencies(jinja_env, 'not a query')
     assert jinja2.is_undefined(result)
示例#33
0
def sif(variable):
    """"string if": `variable` is defined and truthy, else ''"""
    if not is_undefined(variable) and variable:
        return variable
    else:
        return ""
示例#34
0
 def test_get_returns_undefined(self, desc, record):
     assert jinja2.is_undefined(desc.__get__(record))
示例#35
0
 def __eval__(self, record):
     val = self.__expr.__eval__(record)
     return (not is_undefined(val) and
             val not in (None, 0, False, '')) == self.__true
def test_last_mod_ignores_initial_commit(git_repo, pad, pub_date):
    git_repo.commit('site/content/contents.lr', pub_date)
    assert pad.root['pub_date'] == pub_date
    assert jinja2.is_undefined(pad.root['last_mod'])
def test_pub_date_for_untracked_file(git_repo, pad, pub_date):
    git_repo.touch('site/content/contents.lr', pub_date)
    assert pad.root['pub_date'] == pub_date
    assert jinja2.is_undefined(pad.root['last_mod'])
示例#38
0
 def test_index_pages_returns_undefined(self, index_pages, jinja_ctx):
     rv = index_pages(jinja_ctx, 'missing-index')
     assert jinja2.is_undefined(rv)
 def test_get_returns_undefined(self, desc, page, model_data):
     value = desc.__get__(page)
     assert jinja2.is_undefined(value)
示例#40
0
def sif(variable, val):
    """"string if": `val` if `variable` is defined and truthy, else ''"""
    if not jinja2.is_undefined(variable) and variable:
        return val
    else:
        return ""
示例#41
0
 def test_no_pad(self, type_, raw_value):
     bad_value = type_.value_from_raw(raw_value)
     assert jinja2.is_undefined(bad_value)
     assert "a pad is required" in bad_value._undefined_message
示例#42
0
 def test_jinja_syntax_error(self, type_, raw_value):
     bad_value = type_.value_from_raw(raw_value)
     assert jinja2.is_undefined(bad_value)
     assert "syntax error" in bad_value._undefined_message
示例#43
0
 def __contains__(self, name):
     return name in self._data and not is_undefined(self._data[name])
示例#44
0
 def __contains__(self, name):
     return name in self._data and not is_undefined(self._data[name])
示例#45
0
 def test_index_pages_missing_site(self, index_pages, jinja_ctx):
     rv = index_pages(jinja_ctx, 'year-index')
     assert jinja2.is_undefined(rv)
示例#46
0
def env_json(value):
    # pass undefined values on down the pipeline
    if is_undefined(value):
        return value
    result = json.loads(value)
    return result
示例#47
0
def sif(variable, val):
    """"string if": `val` if `variable` is defined and truthy, else ''"""
    if not jinja2.is_undefined(variable) and variable:
        return val
    else:
        return ""