From 79f8199b560f6e77233191d250347aa8512da454 Mon Sep 17 00:00:00 2001 From: Frank Sauerburger <frank@sauerburger.com> Date: Thu, 3 Aug 2017 22:46:58 +0200 Subject: [PATCH] Implement document class Implement the document class which functions as an entry point. This constitutes the last puzzle piece of the doxec package. --- doxec/__init__.py | 25 +++++++++++-- doxec/tests/__init__.py | 1 + doxec/tests/document.py | 81 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 doxec/tests/document.py diff --git a/doxec/__init__.py b/doxec/__init__.py index 7ef09e5..60b09d4 100644 --- a/doxec/__init__.py +++ b/doxec/__init__.py @@ -271,16 +271,33 @@ class Document: operations within the object are executed. """ - def __init__(self, document_path): + def __init__(self, document_path, syntax=Markdown): """ Creates a new document object and parses the given document. """ self.operations = [] - pass + with open(document_path) as f: + self.parse(f.read(), syntax) - def parse(self, string): + def parse(self, string, syntax): """ Parses the content string and appends all operations defined in the string to the internal storage. """ - pass + lines = re.split("\r?\n", string) + while len(lines) > 0: + op_tuple = syntax.parse(lines) + if op_tuple is None: + continue + op_obj = Operation.factory(*op_tuple) + if op_obj is None: + continue + self.operations.append(op_obj) + + + def run(self): + """ + Runs all operations stored attached to this object. + """ + for op in self.operations: + op.execute() diff --git a/doxec/tests/__init__.py b/doxec/tests/__init__.py index 4d4992f..a640a6b 100644 --- a/doxec/tests/__init__.py +++ b/doxec/tests/__init__.py @@ -5,3 +5,4 @@ from .operation import OpWriteTestCase from .operation import OpAppendTestCase from .operation import OpConsoleTestCase from .operation import OpConsoleOutputTestCase +from .document import DocumentTestCase diff --git a/doxec/tests/document.py b/doxec/tests/document.py new file mode 100644 index 0000000..e0ac317 --- /dev/null +++ b/doxec/tests/document.py @@ -0,0 +1,81 @@ + +import os +import unittest +from tempfile import NamedTemporaryFile + +from doxec import Document, OpWrite, OpConsoleOutput + + +toy_doc = """ +# Installation + +In order to run the code examples in this repository you need `python3` and +the +python packages `numpy`, `scipy` and `matplotlib`. On ubuntu 16.04 you can +setup +you system by running the following two commands as super-user (prefix +`sudo`). + +```bash +apt-get update +apt-get install python3 python3-numpy python3-scipy python3-matplotlib +``` + +# First Steps +As a first example we can compute the square root of 2. Create a +file named `example.py` with the following content. + +<!-- write example.py --> +```python +import math + +print("Example 1:") +print("Square root of 2 = %g" % math.sqrt(2)) +``` + +The file can be run with the `python3` interpreter. + +<!-- console_output --> +```bash +$ python3 example.py +Example 1: +Square root of 2 = 1.41421 +``` +""" + +class DocumentTestCase(unittest.TestCase): + """ + This class tests the methods provided by the document class. + """ + + def test_parse(self): + """ + Test whether the parse() correctly parses all operations in the toy + file. + """ + tmp = NamedTemporaryFile(delete=False) + tmp.write(toy_doc.encode('utf8')) + tmp.close() + + doc = Document(tmp.name) + + self.assertEqual(len(doc.operations), 2) + self.assertIsInstance(doc.operations[0], OpWrite) + self.assertIsInstance(doc.operations[1], OpConsoleOutput) + + os.remove(tmp.name) + + def test_run(self): + """ + Check that run() does no raise an exception on the toy file. + """ + tmp = NamedTemporaryFile(delete=False) + tmp.write(toy_doc.encode('utf8')) + tmp.close() + + doc = Document(tmp.name) + + doc.run() + + os.remove(tmp.name) + -- GitLab