Skip to content

Commit

Permalink
Merge pull request #24 from alix-tz/limb
Browse files Browse the repository at this point in the history
Limb scenario
  • Loading branch information
alix-tz authored Mar 25, 2021
2 parents 0df4c05 + 1135be8 commit a38c620
Show file tree
Hide file tree
Showing 6 changed files with 481 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![MIT License](https://img.shields.io/static/v1?style=plastic&label=license&message=MIT&color=brightgreen)](LICENSE) [![Version](https://img.shields.io/static/v1?style=plastic&label=version&message=0.3.2&color=blue)]()
[![MIT License](https://img.shields.io/static/v1?style=plastic&label=license&message=MIT&color=brightgreen)](LICENSE) [![Version](https://img.shields.io/static/v1?style=plastic&label=version&message=0.4.2&color=blue)]()

# ASPYRE GT

Expand Down Expand Up @@ -37,9 +37,9 @@ Process essential information to run Aspyre
[opt] :param vpadding: value to add to VPOS attr. in String nodes (int)
```

> supported values for `scenario`: "tkb", "pdfalto"
> supported values for `scenario`: "tkb", "pdfalto", "limb"
> `vpadding` is only used in PDFALTO scenario
> `vpadding` is only used in PDFALTO and LIMB scenarios
##### Transkribus to eScriptorium scenario with `aspyre.TkbToEs()`
:warning: really not the best way to [transfer data between these two softwares](https://lectaurep.hypotheses.org/documentation/de-transkribus-a-escriptorium).
Expand Down
91 changes: 86 additions & 5 deletions aspyre/aspyrelib/aspyre.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from tqdm import tqdm

from .utils import utils
from .manage import (manage_tkbtoes, manage_pdfaltotoes, zip)
from .manage import (manage_tkbtoes, manage_pdfaltotoes, manage_limbtoes, zip)

SUPPORTED_SCENARIOS = ["tkb", "pdfalto"] # + ["limb", "finereader"]
SUPPORTED_SCENARIOS = ["tkb", "pdfalto", "limb"] # + ["finereader"]
ARCHIVE_EXTENSIONS = ["zip"]


Expand Down Expand Up @@ -94,7 +94,7 @@ def __init__(self, scenario=None, source=None, destination=None, talkative=False
if self.proceed():
# parsing vpadding
# only valid with PDFALTO scenario
if self.scenario != 'pdfalto':
if not self.scenario in ['pdfalto', 'limb']:
self.vpadding = 0
else:
self.vpadding = vpadding
Expand All @@ -105,10 +105,10 @@ def __init__(self, scenario=None, source=None, destination=None, talkative=False
self.padding = True

if self.talkative:
if self.padding and self.scenario == 'pdfalto':
if self.padding and self.scenario in ['pdfalto', 'limb']:
utils.report(f'Will add padding to y-axis coords in string nodes: {self.vpadding}\n---',
"H")
elif self.scenario == 'pdflato' and not self.padding:
elif self.scenario in ['pdflato', 'limb'] and not self.padding:
utils.report(f"No modification made to y-axis coords in string nodes\n---", "H")


Expand Down Expand Up @@ -303,3 +303,84 @@ def __init__(self, args):
self.args = None
utils.report("Failed to run PdfaltoToEs: args must be an AspyreArgs object!\n===[!]===", "E")


class LimbToEs():
def __init__(self, args):
"""Handle a Limb to eScriptorium transformation scenario
:param args: essential information to run transformation scenario
:type args: AspyreArgs object
"""
if isinstance(args, type(AspyreArgs(test_type=True))):
self.args = args
self.args.add_log("Starting LIMB transformation scenario.")

# 1. handling zip
self.unzipped_source = None
if self.args.source.split(".")[-1] in ARCHIVE_EXTENSIONS:
if self.args.talkative:
utils.report("Source is an archive, running unzipping scenario.\n---", "H")
self.unzipped_source = zip.unzip_scenario(self.args.source, self.args.scenario)
if self.unzipped_source is False:
self.args.execution_status = "Failed"
self.add_log("Something went wrong while unpacking the source.")
utils.report("Failing at unpacking the archive, Apsyre can't proceed.\n---", "E")
else:
self.args.add_log("Successfully unzipped source.\n---")
else:
if self.args.talkative:
utils.report("Source is not an archive.\n---", "H")

if self.args.proceed():
# 2. collecting data
package = utils.list_directory(self.unzipped_source)
self.alto_files, self.image_files = manage_limbtoes.locate_alto_and_image_files(package)

if self.alto_files is False:
self.args.add_log("Couldn't find any XML file or any image file.")
utils.report("Aspyre can't run without either of these.\n---", "E")
self.args.execution_status = "Failed"
else:
self.args.add_log("Successfully collected data.")

if self.args.proceed():
# 3. transforming files
if self.args.talkative:
iterator = tqdm(self.alto_files, desc="Processing ALTO XML files", unit=' file')
else:
iterator = self.alto_files
for file in iterator:
processed = 0
try:
manage_limbtoes.handle_a_file(file, self)
except Exception as e:
if self.args.talkative:
utils.report(f"Error while processing {file} :", "E")
print(e)
self.args.add_log(f"Failed to process {file}.")
else:
processed += 1
if processed == 0:
self.args.execution_status = "Failed"
elif processed < len(self.alto_files):
self.args.add_log(f"Successfully transformed {processed} out of {len(self.alto_files)}")
else:
self.args.add_log(f"Successfully processed sources files!")

if self.args.proceed():
# 4. serve a zip file
try:
zip.zip_dir(self.args.destination, self.unzipped_source)
except Exception as e:
if self.args.talkative:
print(e)
self.args.execution_status = "Failed"
self.args.add_log('Failed to zip output.')
else:
utils.report("Task completed ✓", "S")
self.args.execution_status = 'Finished'
self.args.add_log('Aspyre ran Limb scenario successufully!')

else:
self.args = None
utils.report("Failed to run LimbToEs: args must be an AspyreArgs object!\n===[!]===", "E")
Loading

0 comments on commit a38c620

Please sign in to comment.