Some prelim work on builder api.
[librarian.git] / src / librarian / epubcheck.py
1 import json
2 import re
3 import subprocess
4 import zipfile
5
6
7 def epubcheck(filename):
8     p = subprocess.run(
9         [
10             'epubcheck', '-q',
11             '-j', '-',
12             filename
13         ],
14         capture_output=True
15     )
16     output = json.loads(p.stdout)
17     epub = zipfile.ZipFile(filename)
18     messages = output.get('messages', [])
19     for message in messages:
20         for loc in message.get('locations', []):
21             if loc['path'].startswith('EPUB/part'):
22                 with epub.open(loc['path']) as zfile:
23                     text = zfile.read().decode('utf-8')
24                 line = text.split('\n')[loc['line'] - 1][:loc['column'] - 1:]
25                 debug = re.findall(r' data-debug="(\d+):(\d+)', line)
26                 if debug:
27                     debug = debug[-1]
28                     loc['wl_chunk'] = int(debug[0])
29                     loc['wl_line'] = int(debug[1])
30     return messages
31
32             
33