Beispiel #1
0
def test_f():
    from flamingo.core.data_model import Content, Q, F

    q = Q(a=F('b'))

    assert q.check(Content(a=1, b=1))
    assert not q.check(Content(a=1, b=2))
Beispiel #2
0
def test_not():
    from flamingo.core.data_model import Content, Q

    q = ~Q(a=1)

    assert q.check(Content(a=2))
    assert not q.check(Content(a=1))
Beispiel #3
0
def test_Content_get():
    from flamingo.core.data_model import Content

    c = Content(a=1)

    assert c.get('a') == 1
    assert not c.get('b')
    assert c.get('b', 2) == 2
Beispiel #4
0
def test_or():
    from flamingo.core.data_model import Content, Q

    q = Q(a=10) | Q(b=10)

    assert q.check(Content(a=10, b=20))
    assert q.check(Content(a=20, b=10))

    assert not q.check(Content(b=20))
    assert not q.check(Content(a=20))
def test_meta_data_parsing():
    from io import StringIO

    from flamingo.core.parser import ContentParser
    from flamingo.core.data_model import Content

    raw_content = StringIO("""
    a: content of a
    b: content of b
    c:
        a, b, c


    content
    """)

    parser = ContentParser()
    content = Content()

    parser.parse(raw_content, content)

    assert sorted(list(content.data.keys())) == ['a', 'b', 'c', 'content_body']

    assert content['a'] == 'content of a'
    assert content['b'] == 'content of b'
    assert content['c'].strip() == 'a, b, c'

    assert content['content_body'] == 'content'

    # test with whitespaces
    raw_content = StringIO("""
    a: content of a
    b: content of b
    c:
        a, b, c
        

    content
    """)  # NOQA

    parser = ContentParser()
    content = Content()

    parser.parse(raw_content, content)

    assert sorted(list(content.data.keys())) == ['a', 'b', 'c', 'content_body']

    assert content['a'] == 'content of a'
    assert content['b'] == 'content of b'
    assert content['c'].strip() == 'a, b, c'

    assert content['content_body'] == 'content'
Beispiel #6
0
def test_iadd():
    from flamingo.core.data_model import ContentSet, Content

    cs = ContentSet()
    c1 = Content(a=1)
    c2 = Content(a=2)

    cs.add(c1)

    assert c1 in cs
    assert c2 not in cs

    cs += c2

    assert c1 in cs
    assert c2 in cs
Beispiel #7
0
def test_and():
    from flamingo.core.data_model import Content, Q

    c = Content(a=10, b=20)

    assert (Q(a=10) & Q(b=20)).check(c)
    assert not (Q(a=10) & Q(b=21)).check(c)
Beispiel #8
0
def test_q_api():
    from flamingo.core.data_model import Content, Q

    # to few arguments
    with pytest.raises(TypeError) as excinfo:
        Q()

    assert str(excinfo.value) == 'to few arguments'

    # to many arguments
    with pytest.raises(TypeError) as excinfo:
        Q(Q(a=1), b=2)

    assert str(excinfo.value) == 'to many arguments'

    c = Content(a=10, b=20)

    assert Q(a=10).check(c)
    assert Q({'a': 10}).check(c)
    assert not Q(a=20).check(c)
    assert Q(Q(Q(Q(Q(a=10))))).check(Content(a=10))
Beispiel #9
0
def test_add_media(flamingo_dummy_context, flamingo_env):
    from flamingo.core.plugins.media import add_media
    from flamingo.core.data_model import Content

    # relative path
    content = Content(path='foo/bar/index.rst')
    media_content = add_media(flamingo_dummy_context, content, 'image.jpg')

    assert media_content['source'] == 'content/foo/bar/image.jpg'
    assert media_content['destination'] == 'output/media/foo/bar/image.jpg'
    assert media_content['link'] == '/media/foo/bar/image.jpg'

    # absolute path
    content = Content(path='foo/bar/index.rst')
    media_content = add_media(flamingo_dummy_context, content, '/image.jpg')

    assert media_content['source'] == 'content/image.jpg'
    assert media_content['destination'] == 'output/media/image.jpg'
    assert media_content['link'] == '/media/image.jpg'

    # real build tests
    flamingo_env.settings.PLUGINS.append('flamingo.core.plugins.Media')

    flamingo_env.write('/content/foo.jpg', '1')

    flamingo_env.write(
        '/content/index.rst', """


    Hello World
    ===========

    .. img:: foo.jpg

    """)

    flamingo_env.build()

    assert flamingo_env.read('/output/media/foo.jpg') == '1'
Beispiel #10
0
def add_media(context, content, name):
    # gen source
    if name.startswith('/'):
        source = os.path.join(context.settings.CONTENT_ROOT, name[1:])

    else:
        source = os.path.join(
            os.path.dirname(
                os.path.join(context.settings.CONTENT_ROOT, content['path'])),
            name,
        )

    source = os.path.normpath(source)

    # gen destination
    if name.startswith('/'):
        destination = os.path.join(
            context.settings.MEDIA_ROOT,
            name[1:],
        )

    else:
        destination = os.path.join(
            context.settings.MEDIA_ROOT,
            os.path.dirname(content['path']),
            os.path.basename(name),
        )

    # gen link
    link = os.path.join(
        '/media',
        os.path.relpath(destination, context.settings.MEDIA_ROOT),
    )

    # content['media']
    if not content['media']:
        content['media'] = ContentSet()

    media_content = Content(source=source, destination=destination, link=link)

    content['media'].add(media_content)

    return media_content
def test_rst_parsing(flamingo_dummy_context):
    from flamingo.plugins.rst.base import RSTParser
    from flamingo.core.data_model import Content

    raw_content = """
title: foo


bar
===

foobar"""

    parser = RSTParser(flamingo_dummy_context)
    content = Content()

    parser.parse(raw_content, content)

    assert content['title'] == 'foo'
    assert content['content_title'] == 'bar'
    assert content['content_body'] == '<p>foobar</p>\n'
def test_rst_parsing():
    from io import StringIO

    from flamingo.core.plugins.rst.base import RSTParser
    from flamingo.core.data_model import Content

    raw_content = StringIO("""
title: foo


bar
===

foobar""")

    parser = RSTParser()
    content = Content()

    parser.parse(raw_content, content)

    assert content['title'] == 'foo'
    assert content['content_title'] == 'bar'
    assert content['content_body'] == '<p>foobar</p>\n'
Beispiel #13
0
    def __init__(self, settings):
        self.settings = settings

        # setup logging
        self.logger = logging.getLogger('flamingo')
        self.logger.debug('setting up context')

        # setup plugins
        self.plugins = []
        plugins = (self.settings.CORE_PLUGINS + self.settings.DEFAULT_PLUGINS +
                   self.settings.PLUGINS)

        for plugin in plugins:
            self.logger.debug("setting up plugin '%s' ", plugin)

            try:
                plugin_class = acquire(plugin)
                self.plugins.append(plugin_class())

            except Exception:
                self.logger.error('plugin setup failed', exc_info=True)

        # setup parser
        self.parser = FileParser()
        self.run_plugin_hook('parser_setup')

        # parse contents
        self.contents = ContentSet()
        self.content = None

        self._media = []  # FIXME: this should be part of Content()

        for path in self.get_source_paths():
            self.logger.debug("reading %s ", path)

            try:
                self.content = Content(
                    path=os.path.relpath(path, settings.CONTENT_ROOT))

                self.parser.parse(path, self.content)

                self.run_plugin_hook('content_parsed', self.content)

                self.contents.add(self.content)

            except ParsingError as e:
                self.logger.error('%s: %s', path, e)

            except Exception:
                self.logger.error('exception occoured while reading %s',
                                  path,
                                  exc_info=True)

        del self.content
        self.run_plugin_hook('contents_parsed')

        # setup templating engine
        templating_engine_class = acquire(settings.TEMPLATING_ENGINE)

        self.templating_engine = templating_engine_class(
            settings.THEME_PATHS + settings.CORE_THEME_PATHS)

        self.run_plugin_hook('templating_engine_setup', self.templating_engine)
        self.run_plugin_hook('context_setup')
def test_basic_meta_data_parsing(flamingo_dummy_context):
    from flamingo.core.parser import ContentParser
    from flamingo.core.data_model import Content

    raw_content = """
    a: content of a
    b: content of b
    c:
        a, b, c


    content
    """

    parser = ContentParser(flamingo_dummy_context)
    content = Content()

    parser.parse(raw_content, content)

    assert sorted(list(content.data.keys())) == ['a', 'b', 'c', 'content_body',
                                                 'content_offset']

    assert content['a'] == 'content of a'
    assert content['b'] == 'content of b'
    assert content['c'].strip() == 'a, b, c'

    assert content['content_body'] == 'content'

    # test with whitespaces
    raw_content = """
    a: content of a
    b: content of b
    c:
        a, b, c
        

    content
    """  # NOQA

    parser = ContentParser(flamingo_dummy_context)
    content = Content()

    parser.parse(raw_content, content)

    assert sorted(list(content.data.keys())) == ['a', 'b', 'c', 'content_body',
                                                 'content_offset']

    assert content['a'] == 'content of a'
    assert content['b'] == 'content of b'
    assert content['c'].strip() == 'a, b, c'

    assert content['content_body'] == 'content'

    # test multiple blocks
    raw_content = """
    a: content of a
    b: content of b

    c: content of c


    content
    """

    parser = ContentParser(flamingo_dummy_context)
    content = Content()

    parser.parse(raw_content, content)

    assert sorted(list(content.data.keys())) == ['a', 'b', 'c', 'content_body',
                                                 'content_offset']

    assert content['a'] == 'content of a'
    assert content['b'] == 'content of b'
    assert content['c'] == 'content of c'
    assert content['content_body'] == 'content'