Beispiel #1
0
def create_nodes_for_project(project):
    """
    Create some nodes for a project.

    The purpose of this is mainly to able to be able to preview
    the HTML templates used for `Nodes` with real-ish data.

    Generates the same nodes for every project (so you can
    view them regardless of which test user you are logged in
    as).
    """

    nodes = [
        Article(),
        CodeChunk(programmingLanguage="r", text="plot(mtcars)"),
        CodeExpression(programmingLanguage="js", text="x * y"),
        MathBlock(mathLanguage="tex",
                  text="\\int\\limits_a^b x^2  \\mathrm{d} x"),
        MathFragment(mathLanguage="asciimath", text="2 pi r"),
    ]

    for node in nodes:
        Node.objects.create(
            project=project,
            app="gsuita",
            host="https://example.com/some-doc",
            json=object_encode(node),
        )
Beispiel #2
0
def test_code_chunk_exception_capture():
    """
    If an Exception occurs it should be recorded and code outputs up to that point added to outputs.  The rest of the
    code should not be run, although subsequent code blocks should.
    """
    cc1 = CodeChunk(
        "a = 5\na + 2\nprint('Goodbye world!')\nbadref += 1\nprint('After exception!')"
    )
    cc2 = CodeChunk("2 + 2\nprint('CodeChunk2')")

    interpreter = Interpreter()
    for cc in [cc1, cc2]:
        interpreter.execute(cc)

    assert cc1.outputs == [7, "Goodbye world!\n"]
    assert cc1.errors[0].errorType == "NameError"

    assert cc2.outputs == [4, "CodeChunk2\n"]
Beispiel #3
0
def test_code_chunk_exception_capture():
    """
    If an Exception occurs it should be recorded and code outputs up to that point added to outputs.  The rest of the
    code should not be run, although subsequent code blocks should.
    """
    cc1 = CodeChunk(
        'a = 5\na + 2\nprint(\'Goodbye world!\')\nbadref += 1\nprint(\'After exception!\')'
    )
    cc2 = CodeChunk('2 + 2\nprint(\'CodeChunk2\')')

    cce1 = CodeChunkExecution(cc1, CodeChunkParser().parse(cc1))
    cce2 = CodeChunkExecution(cc2, CodeChunkParser().parse(cc2))

    Interpreter().execute([cce1, cce2], {})
    assert cc1.outputs == [7, 'Goodbye world!\n']
    assert cc1.errors[0].kind == 'NameError'

    assert cc2.outputs == [4, 'CodeChunk2\n']
Beispiel #4
0
def test_reads_parsing():
    c = CodeChunk(OPEN_CODE)
    ccp = CodeChunkParser()
    parse_result = ccp.parse(c)

    filenames = [
        'read1', 'read2', 'readwrite', 'kwread', 'kwread2', 'kwread3',
        'kwread4', 'readinfunc'
    ]

    assert sorted(filenames) == sorted(parse_result.reads)
Beispiel #5
0
def test_import_appending():
    """Found imports in a piece of code should be added to the list of imports the code chunk already specifies."""
    c = CodeChunk('import moda\nimport modb\nimport modc', imports=['modc', 'modd'], programmingLanguage='python')

    dc = DocumentCompiler()
    dc.compile(c)

    assert len(c.imports) == 4
    assert 'moda' in c.imports
    assert 'modb' in c.imports
    assert 'modc' in c.imports
    assert 'modd' in c.imports
Beispiel #6
0
def test_import_with_semaphore():
    """If a `CodeChunk`'s imports has an empty string element then no imports should be added to its list."""
    c = CodeChunk('import moda\nimport modb', imports=['modc', 'modd', ''], programmingLanguage='python')

    dc = DocumentCompiler()
    dc.compile(c)

    assert len(c.imports) == 3
    assert 'moda' not in c.imports
    assert 'modb' not in c.imports
    assert 'modc' in c.imports
    assert 'modd' in c.imports
    assert '' in c.imports
Beispiel #7
0
def test_execute_code_chunk(dict_decode):
    """
    Test execution of a CodeChunk (with some mocks).
    """
    interpreter = mock.MagicMock(spec=Interpreter, name="interpreter")
    server = StreamServer(interpreter, BytesIO(), BytesIO())
    cc = CodeChunk("1+1")
    dict_decode.return_value = cc
    server.receive_message(
        json.dumps({
            "id": 13,
            "method": "execute",
            "params": {
                "node": "cc"
            }
        }))
    interpreter.execute.assert_called_with(cc)
Beispiel #8
0
    def set_code_imports(code: CodeChunk, imports: typing.List[str]) -> None:
        """
        Set a list of imports (strings) onto the `code` CodeChunk.

        `imports` will be combined with existing imports on the CodeChunk with duplicates removed, unless the existing
        imports has an empty string semaphore which indicates no new imports should be added.
        """
        if code.imports is None:
            code.imports = imports
            return

        if '' in code.imports:
            return

        for imp in imports:
            if imp not in code.imports:
                code.imports.append(imp)
Beispiel #9
0
def test_reads_parsing():
    c = CodeChunk(OPEN_CODE)
    ccp = CodeChunkParser()
    parse_result = ccp.parse(c)

    filenames = [
        "read1",
        "read2",
        "readwrite",
        "kwread",
        "kwread2",
        "kwread3",
        "kwread4",
        "readinfunc",
    ]

    assert sorted(filenames) == sorted(parse_result.reads)
Beispiel #10
0
def test_import_appending():
    """
    Found imports in a piece of code should be added to the list of imports the code chunk already specifies.
    """
    c = CodeChunk(
        "import moda\nimport modb\nimport modc",
        imports=["modc", "modd"],
        programmingLanguage="python",
    )

    dc = DocumentCompiler()
    dc.compile(c)

    assert len(c.imports) == 4
    assert "moda" in c.imports
    assert "modb" in c.imports
    assert "modc" in c.imports
    assert "modd" in c.imports
Beispiel #11
0
def test_import_with_semaphore():
    """
    If a `CodeChunk`'s imports has an empty string element then no imports should be added to its list.
    """
    c = CodeChunk(
        "import moda\nimport modb",
        imports=["modc", "modd", ""],
        programmingLanguage="python",
    )

    dc = DocumentCompiler()
    dc.compile(c)

    assert len(c.imports) == 3
    assert "moda" not in c.imports
    assert "modb" not in c.imports
    assert "modc" in c.imports
    assert "modd" in c.imports
    assert "" in c.imports
Beispiel #12
0
    def compile_code_chunk(
        chunk: CodeChunk, ) -> typing.Tuple[CodeChunkParseResult, CodeChunk]:
        """
        Compile a `CodeChunk`.

        Returns a `CodeChunkParseResult` which is primarily needed for the AST, and the `CodeChunk` itself, which has
        its code metadata properties set.
        """
        parser = CodeChunkParser()
        cc_result = parser.parse(chunk)
        chunk.imports = cc_result.combined_code_imports(chunk.imports)
        chunk.declares = cc_result.declares
        chunk.assigns = cc_result.assigns
        chunk.alters = cc_result.alters
        chunk.uses = cc_result.uses
        chunk.reads = cc_result.reads

        if cc_result.error:
            set_code_error(chunk, cc_result.error)
        return cc_result, chunk
Beispiel #13
0
def execute_code_chunk(text: str) -> CodeChunk:
    cc = CodeChunk(text)
    cce = CodeChunkExecution(cc, CodeChunkParser().parse(cc))
    Interpreter().execute([cce], {})
    return cc
Beispiel #14
0
def parse_code(code: str) -> CodeChunkParseResult:
    return CodeChunkParser().parse(CodeChunk(code))
Beispiel #15
0
Generates the same nodes for every project (so you can
view them regardless of which test user you are logged in
as), with keys that are is easy to remember (the name of
the type).

Browse `/api/nodes/joe-private-project-codechunk.html` etc to preview the
templates with these data.
"""

from stencila.schema.types import CodeChunk, CodeExpression, MathBlock, MathFragment
from stencila.schema.json import object_encode

from projects.models import Project, Node

nodes = [
    CodeChunk(programmingLanguage="r", text="plot(mtcars)"),
    CodeExpression(programmingLanguage="js", text="x * y"),
    MathBlock(mathLanguage="tex", text="\\int\\limits_a^b x^2  \\mathrm{d} x"),
    MathFragment(mathLanguage="asciimath", text="2 pi r"),
]


def run(*args):
    for project in Project.objects.all():
        for node in nodes:
            Node.objects.get_or_create(
                project=project,
                key="{}-{}".format(project.name,
                                   node.__class__.__name__.lower()),
                app="gsuita",
                host="https://example.com/some-doc",
Beispiel #16
0
def execute_code_chunk(text: str) -> CodeChunk:
    cc = CodeChunk(text)
    Interpreter().execute(cc)
    return cc