ran isort

This commit is contained in:
ari committed 2020-10-30 14:11:43 -04:00
1 parent b40d2bf6e5
commit 343a0ae4de
28 files changed
+60 -659

No files matched your search

+5
View File
@@ -10,9 +10,14 @@ clean:
rm -rf *.egg-info
lint:
isort pdfmajor
black pdfmajor
bandit -r pdfmajor
mypy pdfmajor
isort tests
black tests
bandit -r tests
mypy tests
test:
./venv/bin/python -m unittest discover tests
+8 -7
View File
@@ -1,18 +1,19 @@
from typing import Iterator
from pdfmajor.lexer.dict_and_hex import parse_double_angled_bracket
from pdfmajor.lexer.string import parse_string
from pdfmajor.lexer.keyword import parse_keyword
from pdfmajor.lexer.number import parse_number
from pdfmajor.lexer.name import parse_name
from pdfmajor.lexer.comment import parse_comment
from pdfmajor.lexer.dict_and_hex import parse_double_angled_bracket
from pdfmajor.lexer.keyword import parse_keyword
from pdfmajor.lexer.name import parse_name
from pdfmajor.lexer.number import parse_number
from pdfmajor.lexer.regex import NONSPC
from pdfmajor.lexer.string import parse_string
from pdfmajor.lexer.token import (
TArrayValue,
TDictValue,
Token,
TokenDictionary,
TokenKeyword,
TokenArray,
TokenDictionary,
TokenKeyword
)
from pdfmajor.streambuffer import BufferStream
+1 -3
View File
@@ -1,8 +1,6 @@
from pdfmajor.lexer.exceptions import LexerEOF
from pdfmajor.lexer.regex import EOL
from pdfmajor.lexer.token import (
TokenComment,
)
from pdfmajor.lexer.token import TokenComment
from pdfmajor.streambuffer import BufferStream
+3 -2
View File
@@ -1,8 +1,9 @@
from pdfmajor.streambuffer import BufferStream
from typing import Union
from pdfmajor.lexer.exceptions import LexerEOF
from pdfmajor.lexer.regex import END_HEX_STRING, HEX_PAIR, SPC
from pdfmajor.lexer.token import TDictValue, TokenDictionary, TokenHexString
from typing import Union
from pdfmajor.streambuffer import BufferStream
def parse_double_angled_bracket(
+5 -9
View File
@@ -1,14 +1,10 @@
from pdfmajor.streambuffer import BufferStream
from pdfmajor.lexer.exceptions import LexerEOF
from pdfmajor.lexer.token import (
Token,
TokenBoolean,
TokenKeyword,
TokenNull,
)
from pdfmajor.lexer.regex import END_KEYWORD
from typing import Optional, Union
from pdfmajor.lexer.exceptions import LexerEOF
from pdfmajor.lexer.regex import END_KEYWORD
from pdfmajor.lexer.token import Token, TokenBoolean, TokenKeyword, TokenNull
from pdfmajor.streambuffer import BufferStream
def parse_keyword(buffer: BufferStream, initialpos: Optional[int] = None) -> Token:
"""Parses input stream into a keyword or bool
+5 -11
View File
@@ -1,17 +1,11 @@
from dataclasses import dataclass
from pdfmajor.lexer.exceptions import (
InvalidHexToken,
LexerEOF,
LexerError,
)
from pdfmajor.streambuffer import BufferStream
from pdfmajor.lexer.token import TokenName
from pdfmajor.lexer.regex import HEX, END_LITERAL
from typing import Optional
from pdfmajor.lexer.exceptions import InvalidHexToken, LexerEOF, LexerError
from pdfmajor.lexer.regex import END_LITERAL, HEX
from pdfmajor.lexer.token import TokenName
from pdfmajor.streambuffer import BufferStream
@dataclass
class LiteralParseState:
+4 -9
View File
@@ -1,15 +1,10 @@
from decimal import Decimal
from typing import Optional
from pdfmajor.streambuffer import BufferStream
from pdfmajor.lexer.exceptions import InvalidToken, LexerEOF, LexerError
from pdfmajor.lexer.token import (
TokenReal,
TokenNumber,
TokenInteger,
)
from pdfmajor.lexer.regex import (
END_NUMBER,
)
from pdfmajor.lexer.regex import END_NUMBER
from pdfmajor.lexer.token import TokenInteger, TokenNumber, TokenReal
from pdfmajor.streambuffer import BufferStream
def parse_number(
+2 -1
View File
@@ -1,8 +1,9 @@
from dataclasses import dataclass
from typing import Optional
from pdfmajor.lexer.exceptions import LexerEOF, LexerError
from pdfmajor.lexer.regex import END_STRING, ESC_STRING, OCT_STRING
from pdfmajor.lexer.token import TokenString
from pdfmajor.lexer.exceptions import LexerEOF, LexerError
from pdfmajor.streambuffer import BufferStream
+1 -1
View File
@@ -1,6 +1,6 @@
from decimal import Decimal
from enum import Enum
from typing import Any, Generic, NamedTuple, TypeVar, Union
from decimal import Decimal
T = TypeVar("T")
+2 -1
View File
@@ -1,6 +1,7 @@
from typing import List
from pdfmajor.exceptions import BrokenFile, PDFMajorException
from pdfmajor.lexer.token import TokenKeyword
from pdfmajor.exceptions import PDFMajorException, BrokenFile
class ParserError(PDFMajorException):
+4 -7
View File
@@ -1,16 +1,13 @@
import zlib
from typing import Any, Dict, List, Optional
from base64 import a85decode
from binascii import a2b_hex
from ..exceptions import InvalidDecoderOrNotImplemented
from typing import Any, Dict, List, Optional
from .types import FilterType
from ..exceptions import InvalidDecoderOrNotImplemented
from .ccit import ccittfaxdecode
from .lzw import lzwdecode
from .rld import rldecode
from .ccit import ccittfaxdecode
from ..exceptions import InvalidDecoderOrNotImplemented
from .types import FilterType
_filter_type_name_mapping: Dict[str, FilterType] = {
"FlateDecode": FilterType.FlateDecode,
-1
View File
@@ -10,7 +10,6 @@
import array
from typing import Optional
from ..exceptions import DecodeFailed
+2
View File
@@ -1,7 +1,9 @@
from io import BytesIO
from typing import List, Optional
from ..exceptions import DecodeFailed
## LZWDecoder
##
class LZWDecoder(object):
+8
View File
@@ -1,4 +1,5 @@
from typing import Any, Dict, List, Optional
from ..exceptions import ParserError
from .base import PDFContextualObject, PDFObject
from .primitives import PDFName
@@ -13,6 +14,13 @@ class PDFArray(List[PDFObject], PDFContextualObject):
def to_python(self) -> List[Any]:
return [x.to_python() for x in self]
@classmethod
def from_list(cls, data: List[PDFObject]):
out = cls()
for item in data:
out.pass_item(item)
return out
class PDFDictionary(Dict[str, PDFObject], PDFContextualObject):
"""A class representing a PDF Dictionary as specified in PDF spec 1.7 section 7.3.7"""
+1
View File
@@ -1,4 +1,5 @@
from typing import Optional, Tuple
from .base import PDFObject
+1
View File
@@ -1,6 +1,7 @@
from typing import Optional
from pdfmajor.parser.stream.PDFStream import PDFStream
from .base import PDFContextualObject, PDFObject
+4 -4
View File
@@ -3,17 +3,17 @@ from typing import Dict, Generic, Optional, Type, TypeVar
from pdfmajor.lexer.token import (
Token,
TokenPrimitive,
TokenBoolean,
TokenHexString,
TokenInteger,
TokenName,
TokenNull,
TokenPrimitive,
TokenReal,
TokenString,
)
from .base import PDFObject
from .base import PDFObject
T = TypeVar("T", str, bytes, int, bool, Decimal)
@@ -80,7 +80,7 @@ _token_to_obj_map: Dict[Type[Token], Type[PDFPrimitiveObject]] = {
}
def get_obj_from_token_primitive(token: Token) -> Optional[PDFObject]:
def get_obj_from_token_primitive(token: Token) -> Optional[PDFPrimitiveObject]:
"""converts tokens to their primitive object counterparts
Args:
@@ -91,7 +91,7 @@ def get_obj_from_token_primitive(token: Token) -> Optional[PDFObject]:
"""
obj_const = _token_to_obj_map.get(type(token), None)
if obj_const:
return obj_const.from_token(token) # type: ignore
return obj_const.from_token(token)
elif isinstance(token, TokenNull):
return PDFNull()
else:
+2 -1
View File
@@ -1,6 +1,7 @@
from pdfmajor.parser.objects.base import PDFObject
from typing import Any, List, Optional, TypeVar, Union
from pdfmajor.parser.objects.base import PDFObject
from ..objects.collections import PDFDictionary
from ..objects.primitives import PDFNull
+2
View File
@@ -0,0 +1,2 @@
[tool.isort]
multi_line_output = 3
-24
View File
@@ -1,24 +0,0 @@
from pathlib import Path
from unittest import TestCase, main
from pdfmajor.interpreter import PDFInterpreter
from pdfmajor.execptions import UnicodeNotDefined
CURRENT_FOLDER = Path("./")
class BadUnicode(TestCase):
def test_ensure_failure(self):
pdf = PDFInterpreter(CURRENT_FOLDER / "tests" / "samples" / "pdf" / "bad-unicode.pdf", ignore_bad_chars=False)
pages = list(pdf)
with self.assertRaises(UnicodeNotDefined):
list(pages[2])
def test_ensure_no_failure(self):
pdf = PDFInterpreter(CURRENT_FOLDER / "tests" / "samples" / "pdf" / "bad-unicode.pdf", ignore_bad_chars=True)
pages = list(pdf)
list(pages[2])
if __name__ == '__main__':
# Run Tests
main()
-38
View File
@@ -1,38 +0,0 @@
import os
from unittest import TestCase, main
# from pdfmajor.extractor import extract_items_from_pdf
from pdfmajor.interpreter import LTCharBlock, PDFInterpreter, logging
from pdfmajor.interpreter import PageInterpreter
from pdfmajor.interpreter.commands import LTContainer
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
INPUT_FOLDER = os.path.join(
CUR_PATH, "tests/samples/pdf"
)
FILES = os.listdir(INPUT_FOLDER)
NUM_LOOPS = 10
def count_elms(file_path: str):
count = 0
for page in PDFInterpreter(file_path, ignore_bad_chars=True):
for item in page:
if isinstance(item, LTContainer):
count += len([*item])
else:
count += 1
return count
class CachingTester(TestCase):
def test_cache(self):
for file_name in FILES:
self.subTest(name = file_name)
file_path = os.path.join(INPUT_FOLDER, file_name)
last_count = count_elms(file_path)
for _ in range(NUM_LOOPS):
new_count = count_elms(file_path)
self.assertEqual( new_count , last_count )
last_count = new_count
if __name__ == '__main__':
# Run Tests
main()
-115
View File
@@ -1,115 +0,0 @@
import os
from typing import List
from unittest import TestCase, main
# from pdfmajor.extractor import extract_items_from_pdf
from pdfmajor.interpreter import LTCharBlock, PDFInterpreter, logging
from pdfmajor.interpreter import PageInterpreter
from pdfmajor.interpreter.commands import LTCurve, PDFColor, LTXObject
from pdfmajor.interpreter.commands import PREDEFINED_COLORSPACE
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
INPUT_FOLDER = os.path.join(
CUR_PATH, "tests/samples/pdf"
)
FILES = os.listdir(INPUT_FOLDER)
NUM_LOOPS = 10
class ColorTest(TestCase):
def run_test(self, file_path: str, curve_defs: List[List[PDFColor]]):
def run_once(item):
if isinstance(item, LTCurve):
t_stroke, t_fill = curve_defs.pop(0)
self.assertEqual( item.stroke.values, t_stroke.values )
self.assertEqual( item.stroke.color_space, t_stroke.color_space )
self.assertEqual( item.fill.values, t_fill.values )
self.assertEqual( item.fill.color_space, t_fill.color_space )
elif isinstance(item, LTXObject):
for c in item:
run_once(c)
for page in PDFInterpreter(file_path):
for item in page:
run_once(item)
def test_lorem_v3(self):
self.run_test(
os.path.join(INPUT_FOLDER, "lorem-v3.pdf"),
[
[
PDFColor(None),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255),
],
[
PDFColor(None),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 114, 159, 207),
],
[
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 0),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 114, 159, 207),
],
[
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 0),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 114, 159, 207),
],
[
PDFColor(None),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255),
],
[
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 128),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 0, 0),
]
] +
([[
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 128),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255),
]]*24)+
([[
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 128),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 0),
]]*11)+
([[
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 128, 128, 128),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 0),
]]*5)+
[[
PDFColor(None),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255),
]]
)
def test_lorem_v4(self):
self.run_test(
os.path.join(INPUT_FOLDER, "lorem-v4.pdf"),
[
[
PDFColor(None),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255),
],
[
PDFColor(None),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 64, 64, 64),
]
]
)
def test_lorem_v5(self):
self.run_test(
os.path.join(INPUT_FOLDER, "lorem-v5.pdf"),
[
[
PDFColor(None),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255),
],
[
PDFColor(None),
PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 128, 128, 128),
]
]
)
if __name__ == '__main__':
# Run Tests
main()
-61
View File
@@ -1,61 +0,0 @@
#!/usr/bin/env python
import re
import os
import logging
from unittest import TestCase, main
from typing import List
from pdfmajor.converters import convert_file
from xml.etree import ElementTree
from json import load as json_load
from tqdm import tqdm
def rel_path(*arg: str) -> str:
return os.path.normpath(os.path.join(os.path.dirname(__file__), *arg))
EXTS = ['text', 'json','xml','html']
INPUT_FOLDER = rel_path("./samples/pdf")
OUTPUT_FOLDER = rel_path(f"./samples/output/")
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
files = os.listdir(INPUT_FOLDER)
TOTAL = len(files)*len(EXTS)
class ConversionTest(TestCase):
def test_conversion(self):
# with tqdm(total=TOTAL) as waiter:
for idx, file_name in enumerate(files):
self.subTest(i=idx);
file_name_no_ext = re.sub(r'\.pdf$',"", file_name)
input_file_path = os.path.join(INPUT_FOLDER, file_name)
for ext in EXTS:
if not os.path.exists(os.path.join(OUTPUT_FOLDER, ext)):
os.makedirs(os.path.join(OUTPUT_FOLDER, ext))
output_file_path = os.path.join(OUTPUT_FOLDER, ext, file_name_no_ext + "." + ext)
if len(file_name) > len(file_name_no_ext):
# waiter.set_description(f"Conversion test -- {file_name}")
convert_file(
input_file=input_file_path,
output_file=output_file_path,
out_type=ext,
ignore_bad_chars=True,
debug_level=logging.WARN,
)
if ext == 'xml':
# waiter.set_description(f" > testing format {file_name}")
ElementTree.parse(output_file_path)
elif ext == 'json':
# waiter.set_description(f" > testing format {file_name}")
with open(output_file_path, 'r') as outf:
json_load(outf)
# waiter.update()
# waiter.set_description("Done")
if __name__ == '__main__':
# Run Tests
main()
-82
View File
@@ -1,82 +0,0 @@
from contextlib import contextmanager
from unittest import TestCase, main
from typing import List, Tuple, Callable
from os import remove
from tempfile import mktemp
from pdfmajor.converters.writers.html import HTMLMaker
from tqdm import tqdm
# Make temp file
FILE_NAME = mktemp()
TestFunction = Callable[[HTMLMaker], str];
TESTS: List[Tuple[TestFunction, str]] = [];
class HTMLTester(TestCase):
@contextmanager
def get_html(self):
with HTMLMaker(FILE_NAME) as html:
yield html
def compare(self, expected: str):
with open(FILE_NAME, 'r') as f:
self.assertEqual(f.read() , expected)
def test_html_basic(self):
with self.get_html() as html:
with html.elm("html", nolineend=True):
html.write("test")
self.compare("""<html >
test
</html>""")
def test_html_with_attributes(self):
with self.get_html() as html:
with html.elm("html", nolineend=True):
with html.elm("head"):
html.singleton("meta", {
'http-equiv': "Content-Type",
'content': "text/html"
})
self.compare("""<html >
<head >
<meta http-equiv="Content-Type" content="text/html" />
</head>
</html>""")
def test_html_with_children(self):
with self.get_html() as html:
with html.elm("html", nolineend=True):
with html.elm("head"):
html.singleton("meta", {
'http-equiv': "Content-Type",
'content': "text/html"
})
with html.elm("body"):
with html.elm("h1", css={"color": "red"}):
html.write("Hello World")
self.compare("""<html >
<head >
<meta http-equiv="Content-Type" content="text/html" />
</head>
<body >
<h1 style="color: red">
Hello World
</h1>
</body>
</html>""")
def test_html_with_css(self):
with self.get_html() as html:
with html.elm('style'):
html.write('''.page { position: relative; overflow: hidden;border: 1px black solid;}''')
self.compare("""<style >
.page { position: relative; overflow: hidden;border: 1px black solid;}
</style>
""")
if __name__ == '__main__':
# Run Tests
main()
# Clean up
remove(FILE_NAME)
-41
View File
@@ -1,41 +0,0 @@
import os
from unittest import TestCase, main
from pdfmajor.interpreter import LTImage, logging
from pdfmajor.interpreter import PageInterpreter, PDFInterpreter
from pdfmajor.interpreter.commands import LTItem
from pdfmajor.interpreter.commands import LTCharBlock, LTChar
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
FILES = [
"jpg",
"looseless"
]
OUTPUT_FOLDER = os.path.join(
CUR_PATH,
'samples/output/images'
)
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
class ImageTester(TestCase):
def test_image_extract(self):
for file_name in FILES:
self.subTest(file_name=file_name)
for page in PDFInterpreter(os.path.join(
CUR_PATH,
f"tests/samples/pdf/{file_name}.pdf"
), debug_level=logging.ERROR):
self.assertTrue( isinstance(page, PageInterpreter) )
for item_idx, item in enumerate(page):
self.assertTrue( isinstance(item, LTItem) )
if isinstance(item, LTImage):
item.save_image(
OUTPUT_FOLDER,
file_name=f'{file_name}_p{page.page_num}i{item_idx}'
)
if __name__ == '__main__':
# Run Tests
main()
-29
View File
@@ -1,29 +0,0 @@
import os
from unittest import TestCase, main
from pdfmajor.interpreter import LTCharBlock, PDFInterpreter, logging
from pdfmajor.interpreter import PageInterpreter
from pdfmajor.interpreter.commands import LTItem
from pdfmajor.interpreter.commands import LTCharBlock, LTChar
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
INPUT_FOLDER = os.path.join(
CUR_PATH, "tests/samples/pdf"
)
FILES = os.listdir(INPUT_FOLDER)
class TestInterpreter(TestCase):
def test_pdfintp(self):
for file_name in FILES:
self.subTest(file_name=file_name)
file_path = os.path.join(INPUT_FOLDER, file_name)
for page in PDFInterpreter(file_path, debug_level=logging.ERROR, ignore_bad_chars=True):
self.assertTrue( isinstance(page, PageInterpreter) )
for item in page:
self.assertTrue( isinstance(item, LTItem) )
if isinstance(item, LTCharBlock):
for char in item:
self.assertTrue( isinstance(char, LTChar) )
if __name__ == '__main__':
# Run Tests
main()
-111
View File
@@ -1,111 +0,0 @@
from os import remove
from tempfile import mktemp
from pdfmajor.converters.writers.json import JSONMaker
# Make temp file
FILE_NAME = mktemp()
with JSONMaker(FILE_NAME) as obj:
obj.number("a", 2)
obj.string("hello", "world")
assert(open(FILE_NAME, 'r').read() == """{
"a": 2,
"hello": "world"
}""")
with JSONMaker(FILE_NAME) as obj:
with obj.object("test") as child:
child.number("A", 2)
assert(open(FILE_NAME, 'r').read() == """{
"test": {
"A": 2
}
}""")
with JSONMaker(FILE_NAME) as obj:
with obj.object("hello") as child:
with child.object("nested") as nested:
with nested.object("so") as so:
so.string("really", 'nested'),
so.number("man", 2)
nested.number("he", 3)
child.string('fo', 'real')
obj.string('fo', 'real2')
assert(open(FILE_NAME, 'r').read() == """{
"hello": {
"nested": {
"so": {
"really": "nested",
"man": 2
},
"he": 3
},
"fo": "real"
},
"fo": "real2"
}""")
with JSONMaker(FILE_NAME) as obj:
with obj.array("hello") as child:
child.number(1)
with child.array() as nchild:
nchild.string('wow')
child.number(3)
assert(open(FILE_NAME, 'r').read() == """{
"hello": [
1,
[
"wow"
],
3
]
}""")
with JSONMaker(FILE_NAME) as obj:
with obj.array("pages") as pages:
with pages.object() as page:
page.number("id", 1)
page.number("height", 20)
page.number("width", 20)
with page.array("children") as children:
with children.object() as item:
item.string("type", "char-block")
item.string("text", "test")
item.string("color_type", "rgb")
with item.array("color_vals") as col_vals:
col_vals.number(0)
col_vals.number(0)
col_vals.number(0)
item.number("x0", 2)
item.number("x1", 4.2)
item.number("y0", 231.12)
item.number("y1", 331.12)
assert(open(FILE_NAME, 'r').read() == """{
"pages": [
{
"id": 1,
"height": 20,
"width": 20,
"children": [
{
"type": "char-block",
"text": "test",
"color_type": "rgb",
"color_vals": [
0,
0,
0
],
"x0": 2,
"x1": 4.2,
"y0": 231.12,
"y1": 331.12
}
]
}
]
}""")
# Clean up
remove(FILE_NAME)
-101
View File
@@ -1,101 +0,0 @@
from os import remove
from tempfile import mktemp
from pdfmajor.converters.writers.yaml import YAMLMaker
# Make temp file
FILE_NAME = mktemp()
with YAMLMaker(FILE_NAME) as yml:
yml.write("name", "bob")
yml.write("age", "20")
with yml.object("wife") as wife:
wife.write("name", "kory")
wife.write("age", "21")
with yml.array("children") as children:
with children.object() as child:
child.write("name", "Mike")
with child.array("toys") as toys:
toys.write("lion")
toys.write("bear")
with yml.object("computer") as computer:
computer.write("type", "awesome")
assert(open(FILE_NAME, 'r').read() == """name: bob
age: 20
wife:
name: kory
age: 21
children:
- name: Mike
toys:
- lion
- bear
computer:
type: awesome""")
with YAMLMaker(FILE_NAME) as yml:
yml.write("name", "bob")
yml.write("age", "20")
yml.place_object("wife", {
"name": "kory",
"age": 21
})
with yml.array("children") as children:
with children.object() as child:
child.write("name", "Mike")
child.place_array("toys", [
"lion", "bear"
])
yml.place_object("computer", {"type": "awesome"})
assert(open(FILE_NAME, 'r').read() == """name: bob
age: 20
wife:
name: kory
age: 21
children:
- name: Mike
toys:
- lion
- bear
computer:
type: awesome""")
with YAMLMaker(FILE_NAME) as yml:
with yml.array('pages') as pages:
with pages.object() as page:
page.write("id", 0)
page.write("width", 100)
page.write("height", 200)
with page.array("children") as children:
with children.object() as curve:
with curve.object("stroke") as fill:
fill.write("type", "DeviceRGB")
fill.write("value", ['1','2','3'])
curve.write("x0", 0)
curve.write("x1", 1)
with curve.object("fill") as fill:
fill.write("type", "DeviceRGB")
fill.write("value", ['1','2','3'])
curve.write("y0", 0)
curve.write("y1", 1)
assert(open(FILE_NAME, 'r').read() == """pages:
- id: 0
width: 100
height: 200
children:
- stroke:
type: DeviceRGB
value: ['1', '2', '3']
x0: 0
x1: 1
fill:
type: DeviceRGB
value: ['1', '2', '3']
y0: 0
y1: 1""")
# Clean up
remove(FILE_NAME)