예제 #1
0
    def test_let_syntax_with_scope(self):
        class m:
            from zenmai.actions import partial  # NOQA
            prefix = staticmethod(lambda d, prefix=":": prefix + d)
            add = staticmethod(lambda n, v=1: n + v)

        source = textwrap.dedent("""
        $let:
          withPlus:
            {$partial: $prefix, prefix: +}
        person:
          name: {$withPlus: foo}
          age:
            $let:
              withPlus: {$partial: $add, v: 10}
            body:
              $withPlus: 10
        friends:
          - name: {$withPlus: bar}
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        person:
          name: +foo
          age: 20
        friends:
        - name: +bar
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #2
0
    def test_unless_syntax(self):
        class m:
            pass

        source = textwrap.dedent("""
        ok0:
          $unless: false
          body: ok
        ng0:
          $unless: true
          body: ng
        ok1:
          $unless: false
          body:
            message: ok
        ok2:
          $unless: false
          message: ok
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        ok0: ok
        ok1:
          message: ok
        ok2:
          message: ok
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #3
0
    def test_jinja2(self):
        class m:
            from zenmai.actions import jinja2_template  # NOQA

        source = textwrap.dedent("""
        $let:
          item-template:
            $jinja2_template: |
              items:
                {% for i in nums %}
                - {{prefix|default("no")}}.{{i}}
                {% endfor %}
        body:
          listing:
            $item-template:
              nums: [1,2,3]
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        listing:
          items:
          - no.1
          - no.2
          - no.3
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #4
0
    def test_import_with_physical_path(self):
        class m:
            from zenmai.actions import import_  # NOQA

        source = textwrap.dedent("""
        main:
          {$import: ./_inc.py, as: f}

        n: {$f.inc: 10}
        """)
        d = self._callFUT(source, m, filename=__file__)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        n: 11
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #5
0
    def test_quote_syntax2(self):
        class m:
            pass

        source = textwrap.dedent("""
        body:
          $$load:  foo.yaml
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        body:
          $load: foo.yaml
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #6
0
    def test_concat(self):
        class m:
            from zenmai.actions import concat  # NOQA

        source = textwrap.dedent("""
        person:
          $concat:
            - name: foo
            - age: 10
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        person:
          name: foo
          age: 10
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #7
0
    def test_let_syntax(self):
        class m:
            from zenmai.actions import partial  # NOQA
            prefix = staticmethod(lambda d, prefix=":": prefix + d)

        source = textwrap.dedent("""
        $let:
          withPlus:
            {$partial: $prefix, prefix: +}
        body:
          definitions:
            name: {$withPlus: foo}
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        definitions:
          name: +foo
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #8
0
    def test_return_value_with_action(self):
        class m:
            @staticmethod
            def inc(n):
                return n + 1

            @staticmethod
            def inc2(n):
                return {"$inc": n + 1}

        source = textwrap.dedent("""
        n:
          $inc2: 10
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        n: 12
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #9
0
    def test_format2(self):
        class m:
            from zenmai.actions import format  # NOQA
            from zenmai.actions import get  # NOQA

        source = textwrap.dedent("""
        $let:
          items:
            app: "{prefix}-app"
            batch: "{prefix}-batch"
        body:
          $format: {$get: items}
          prefix: dev
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        app: dev-app
        batch: dev-batch
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #10
0
    def test_get(self):
        class m:
            from zenmai.actions import get  # NOQA

        source = textwrap.dedent("""
        $let:
          person:
            name: foo
        body:
          - {$get: person}
          - {$get: "person#/name"}
          - {$get: "person#/age", default: 0}
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        - name: foo
        - foo
        - 0
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #11
0
    def test_import(self):
        class m:
            from zenmai.actions import import_  # NOQA

        source = textwrap.dedent("""
        main:
          $import: zenmai.actions.suffix
          as: s

        definitions:
          $s.suffix:
            name: foo
          suffix: +
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        definitions:
          name+: foo
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #12
0
    def test_load__with_kwargs(self):
        from tempfile import TemporaryDirectory
        from pathlib import Path

        with TemporaryDirectory() as d:
            d = Path(d)

            a = textwrap.dedent("""
            b:
              $let:
                mydata: {value: 10}
              body:
                $load:  ./b.yaml
                data: {$get: mydata}
            """)
            loading.dumpfile(loading.loads(a), str(d.joinpath("./a.yaml")))

            b = textwrap.dedent("""
            # need: data
            name: b
            data: {$get: data}
            """)
            loading.dumpfile(loading.loads(b), str(d.joinpath("./b.yaml")))

            class m:
                from zenmai.actions import get  # NOQA
                from zenmai.actions import load  # NOQA

            d = self._callFUT(a, m, filename=str(d.joinpath("./a.yaml")))
            actual = loading.dumps(d)
            expected = textwrap.dedent("""
            b:
              name: b
              data:
                value: 10
            """)
            self.assertDiff(actual.strip(), expected.strip())
예제 #13
0
    def test_counter(self):
        class m:
            from zenmai.actions import counter  # NOQA

        source = textwrap.dedent("""
        $let:
          c0: {$counter: 3}
          c1: {$counter: 0}
        body:
          - {$c0: "item{:04}"}
          - {$c1}
          - {$c0: "item{:04}"}
          - {$c1}
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        - item0003
        - 0
        - item0004
        - 1
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #14
0
    def test_format(self):
        class m:
            from zenmai.actions import format  # NOQA

        source = textwrap.dedent("""
        - $format: "{prefix}{number:04}"
          prefix: foo
          number: 0
        - $format: "{prefix}{number:04}"
          prefix: foo
          number: 1
        - $format: "{prefix}{number:04}"
          prefix: bar
          number: 0
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        - foo0000
        - foo0001
        - bar0000
        """)
        self.assertDiff(actual.strip(), expected.strip())
예제 #15
0
def dumps(d):
    return loading.dumps(d)
예제 #16
0
    def test_load(self):
        from tempfile import TemporaryDirectory
        from pathlib import Path

        with TemporaryDirectory() as d:
            d = Path(d)

            main = textwrap.dedent("""
            definitions:
              one:
                $load: "./one/one.yaml"
              two:
                $load: "./two/two.yaml"
            """)
            loading.dumpfile(loading.loads(main),
                             str(d.joinpath("./main.yaml")))

            one = textwrap.dedent("""
            type: object
            properties:
              value:
                $load: "./value.yaml"
            """)
            loading.dumpfile(loading.loads(one),
                             str(d.joinpath("./one/one.yaml")))

            value = textwrap.dedent("""
            description: value
            type: integer
            """)
            loading.dumpfile(loading.loads(value),
                             str(d.joinpath("./one/value.yaml")))

            two = textwrap.dedent("""
            type: object
            properties:
              value:
                $load: "../one/value.yaml"
            """)
            loading.dumpfile(loading.loads(two),
                             str(d.joinpath("./two/two.yaml")))

            class m:
                from zenmai.actions import load  # NOQA

            d = self._callFUT(main, m, filename=str(d.joinpath("./main.yaml")))
            actual = loading.dumps(d)
            expected = textwrap.dedent("""
            definitions:
              one:
                type: object
                properties:
                  value:
                    description: value
                    type: integer
              two:
                type: object
                properties:
                  value:
                    description: value
                    type: integer
            """)
            self.assertDiff(actual.strip(), expected.strip())
예제 #17
0
    def test_jinja2_raw_format(self):
        from tempfile import TemporaryDirectory
        from pathlib import Path

        class m:
            from zenmai.actions import jinja2_templatefile, jinja2_template  # NOQA
            from zenmai.actions import load  # NOQA

        with TemporaryDirectory() as d:
            d = Path(d)

            main = textwrap.dedent("""
            $let:
              readme-template:
                $jinja2_templatefile: ./readme.jinja2
                format: raw
            body:
              ./one.md:
                $readme-template:
                  name: one
              ./two.md:
                $readme-template:
                  name: two
              ./three.md:
                $readme-template:
                  name: three
            """)
            loading.dumpfile(loading.loads(main),
                             str(d.joinpath("./main.yaml")))

            template = textwrap.dedent("""\
            # {{name}}
            this is {{name}}.
            """)
            with open(str(d.joinpath("./readme.jinja2")), "w") as wf:
                wf.write(template)

            d = self._callFUT(main, m, filename=str(d.joinpath("./main.yaml")))
            actual = loading.dumps(d)
            expected = textwrap.dedent("""
            ./one.md: |-
              # one
              this is one.
            ./two.md: |-
              # two
              this is two.
            ./three.md: |-
              # three
              this is three.
            """)
            self.assertDiff(actual.strip(), expected.strip())

        source = textwrap.dedent("""
        $let:
          item-template:
            $jinja2_template: |
              items:
                {% for i in nums %}
                - {{prefix|default("no")}}.{{i}}
                {% endfor %}
        body:
          listing:
            $item-template:
              nums: [1,2,3]
        """)

        d = self._callFUT(source, m)
        actual = loading.dumps(d)
        expected = textwrap.dedent("""
        listing:
          items:
          - no.1
          - no.2
          - no.3
        """)
        self.assertDiff(actual.strip(), expected.strip())
loading.setup()

yaml = """
definitions:
  person:
    type: object
    properties:
      name:
        type: string
      age:
        type: integer
"""
d = loading.loads(yaml, format="yaml")

with indent(2, "load data\n"):
    print(loading.dumps(d, format="yaml"))

with indent(2, "access by json pointer\n"):
    from dictknife.jsonknife import access_by_json_pointer

    q = "/definitions/person/properties"
    v = access_by_json_pointer(d, q)
    print("access : {}".format(q))
    print(loading.dumps(v, format="yaml"))

    # this is also ok(but this is json reference).
    q = "#/definitions/person/properties"
    v = access_by_json_pointer(d, q)
    print("access : {}".format(q))
    print(loading.dumps(v, format="yaml"))
예제 #19
0
import jedi
from dictknife.loading import dumps

defs = jedi.Script("""import marshmallow""", 1, 7, ".").goto_definitions()
print(dumps({d.name: d.module_path for d in defs}, format="json"))
# (ffap-python:find-program ffap-python:python-program-name)
예제 #20
0
def dumps(d):
    return loading.dumps(d)