- ensured that all tests are working
- added a main test script
This commit is contained in:
1 parent
bab104885f
commit
844408c6e3
8 files changed
+71
-50
No files matched your search
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
# Get working dir (this will make sure that the script will work the same anywhere you run it)
|
||||
WK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
cd $WK_DIR
|
||||
cd ..
|
||||
|
||||
for filename in ./tests/test*.py; do
|
||||
python $filename
|
||||
done
|
||||
@@ -166,7 +166,7 @@ def create_svg_path(item: LTCurve):
|
||||
|
||||
if item.height > 0:
|
||||
dy = 1.0/item.height
|
||||
dx = (item.width/item.height)/(item.x1-item.x0)
|
||||
dx = (item.width/item.height)/max([(item.x1-item.x0),0.01])
|
||||
svg_attr.update({
|
||||
"viewBox": "0 0 %s %s" % ( item.width/item.height, 1 )
|
||||
})
|
||||
|
||||
@@ -50,7 +50,8 @@ class JSONMakerObject:
|
||||
self.write_prop(key, val)
|
||||
|
||||
def string(self, key: str, val: str):
|
||||
self.write_prop(key, f'"{val}"')
|
||||
val_sant = str(val).replace('"', '\\"')
|
||||
self.write_prop(key, f'"{val_sant}"')
|
||||
|
||||
@contextmanager
|
||||
def object(self, key: str):
|
||||
@@ -109,7 +110,8 @@ class JSONMakerArray:
|
||||
self.write_prop(val)
|
||||
|
||||
def string(self, val: str):
|
||||
self.write_prop(f'"{val}"')
|
||||
val_sant = str(val).replace('"', '\\"')
|
||||
self.write_prop(f'"{val_sant}"')
|
||||
|
||||
@contextmanager
|
||||
def object(self):
|
||||
|
||||
+24
-26
@@ -22,30 +22,28 @@ if not os.path.exists(OUTPUT_FOLDER):
|
||||
files = os.listdir(INPUT_FOLDER)
|
||||
|
||||
TOTAL = len(files)*len(EXTS)
|
||||
waiter = tqdm(total=TOTAL)
|
||||
for idx, file_name in enumerate(files):
|
||||
file_name_no_ext = re.sub(r'\.pdf$',"", file_name)
|
||||
input_file_path = os.path.join(INPUT_FOLDER, file_name)
|
||||
with tqdm(total=TOTAL) as waiter:
|
||||
for idx, file_name in enumerate(files):
|
||||
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"Processing {file_name}")
|
||||
convert_file(
|
||||
input_file=input_file_path,
|
||||
output_file=output_file_path,
|
||||
out_type=ext,
|
||||
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}")
|
||||
json_load(open(output_file_path, 'r'))
|
||||
waiter.refresh()
|
||||
waiter.update()
|
||||
waiter.set_description("Done")
|
||||
waiter.close()
|
||||
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,
|
||||
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}")
|
||||
json_load(open(output_file_path, 'r'))
|
||||
waiter.update()
|
||||
waiter.set_description("Done")
|
||||
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
|
||||
from pdfmajor.interpreter import LTImage, logging
|
||||
from pdfmajor.interpreter import PageInterpreter, PDFInterpreter
|
||||
@@ -18,16 +19,16 @@ OUTPUT_FOLDER = os.path.join(
|
||||
if not os.path.exists(OUTPUT_FOLDER):
|
||||
os.makedirs(OUTPUT_FOLDER)
|
||||
|
||||
for file_name in FILES:
|
||||
for file_name in tqdm(FILES, desc='Image Writer Test'):
|
||||
for page in PDFInterpreter(os.path.join(
|
||||
CUR_PATH,
|
||||
f"samples/pdf/{file_name}.pdf"
|
||||
f"tests/samples/pdf/{file_name}.pdf"
|
||||
), debug_level=logging.ERROR):
|
||||
assert isinstance(page, PageInterpreter)
|
||||
for item_idx, item in enumerate(page):
|
||||
assert isinstance(item, LTItem)
|
||||
if isinstance(item, LTImage):
|
||||
print(item.save_image(
|
||||
item.save_image(
|
||||
OUTPUT_FOLDER,
|
||||
file_name=f'{file_name}_p{page.page_num}i{item_idx}'
|
||||
))
|
||||
)
|
||||
+13
-12
@@ -1,21 +1,22 @@
|
||||
import os
|
||||
|
||||
# from pdfmajor.extractor import extract_items_from_pdf
|
||||
from tqdm import tqdm
|
||||
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__))
|
||||
FILE_NAME = os.path.join(
|
||||
CUR_PATH,
|
||||
"samples/pdf/tables.pdf"
|
||||
INPUT_FOLDER = os.path.join(
|
||||
CUR_PATH, "tests/samples/pdf"
|
||||
)
|
||||
FILES = os.listdir(INPUT_FOLDER)
|
||||
|
||||
for page in PDFInterpreter(FILE_NAME, debug_level=logging.INFO):
|
||||
assert isinstance(page, PageInterpreter)
|
||||
for item in page:
|
||||
assert isinstance(item, LTItem)
|
||||
if isinstance(item, LTCharBlock):
|
||||
for char in item:
|
||||
assert(char, LTChar)
|
||||
for file_name in tqdm(FILES, desc="Interpreter Test"):
|
||||
file_path = os.path.join(INPUT_FOLDER, file_name)
|
||||
for page in PDFInterpreter(file_path, debug_level=logging.ERROR):
|
||||
assert isinstance(page, PageInterpreter)
|
||||
for item in page:
|
||||
assert isinstance(item, LTItem)
|
||||
if isinstance(item, LTCharBlock):
|
||||
for char in item:
|
||||
assert isinstance(char, LTChar)
|
||||
@@ -80,7 +80,6 @@ with JSONMaker(FILE_NAME) as obj:
|
||||
item.number("x1", 4.2)
|
||||
item.number("y0", 231.12)
|
||||
item.number("y1", 331.12)
|
||||
print(open(FILE_NAME, 'r').read())
|
||||
assert(open(FILE_NAME, 'r').read() == """{
|
||||
"pages": [
|
||||
{
|
||||
|
||||
@@ -20,7 +20,6 @@ with YAMLMaker(FILE_NAME) as yml:
|
||||
toys.write("bear")
|
||||
with yml.object("computer") as computer:
|
||||
computer.write("type", "awesome")
|
||||
print(open(FILE_NAME, 'r').read())
|
||||
assert(open(FILE_NAME, 'r').read() == """name: bob
|
||||
age: 20
|
||||
wife:
|
||||
@@ -48,7 +47,6 @@ with YAMLMaker(FILE_NAME) as yml:
|
||||
"lion", "bear"
|
||||
])
|
||||
yml.place_object("computer", {"type": "awesome"})
|
||||
print(open(FILE_NAME, 'r').read())
|
||||
assert(open(FILE_NAME, 'r').read() == """name: bob
|
||||
age: 20
|
||||
wife:
|
||||
@@ -81,8 +79,21 @@ with YAMLMaker(FILE_NAME) as yml:
|
||||
curve.write("y0", 0)
|
||||
curve.write("y1", 1)
|
||||
|
||||
print(open(FILE_NAME, 'r').read())
|
||||
assert(open(FILE_NAME, 'r').read() == """""")
|
||||
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""")
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user