/usr/share/sigil/python3lib/gencheck.py is in sigil-data 0.9.9+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
from __future__ import unicode_literals, division, absolute_import, print_function
import re
xml_structure={}
min_required_attribs = []
ALL_SPACES = re.compile(r'^\s*$', re.U)
def empty(last_text):
if last_text is None:
return True
return (ALL_SPACES.match(last_text) is not None)
def get_xml_specs(mtype, pkgver):
global xml_structure
global min_required_attribs
if mtype == 'application/oebps-package+xml':
import opf2data
xml_structure = opf2data.xml_structure
min_required_attribs = opf2data.min_required_attribs
if pkgver == '3.0':
import opf3data
xml_structure = opf3data.xml_structure
min_required_attribs = opf3data.min_required_attribs
elif mtype == 'application/x-dtbncx+xml':
import ncxdata
xml_structure = ncxdata.xml_structure
min_required_attribs = ncxdata.min_required_attribs
elif mtype == 'application/smil+xml':
import smildata
xml_structure = smildata.xml_structure
min_required_attribs = smildata.min_required_attribs
elif mtype == 'application/oebps-page-map+xml':
import pagedata
xml_structure = pagedata.xml_structure
min_required_attribs = pagedata.min_required_attribs
class GenCheck(object):
def __init__(self, mtype, pkgver):
get_xml_specs(mtype, pkgver)
self.mtype, self.pkgver = mtype, pkgver
self.cnt_structure = {}
for key in xml_structure:
self.cnt_structure[key] = 0
def check(self, sc, text, tp, tname, ttype, tattr):
if tname in xml_structure:
[allowed_parents, isVoid, mincnt, maxcnt, predecessors] = xml_structure[tname]
if ttype in ('begin', 'single'):
self.cnt_structure[tname] = self.cnt_structure[tname] + 1
cnt = self.cnt_structure[tname]
if maxcnt != -1 and cnt > maxcnt:
error_msg = 'tag occurred too many times: %s' % tname
sc.errors.append((sc.tag_start[0], sc.tag_start[1], error_msg))
sc.has_error = True
return
if sc.tagpath and sc.tagpath[-1] not in allowed_parents:
error_msg = 'incorrect parent for tag: %s' % tname
sc.errors.append((sc.tag_start[0], sc.tag_start[1], error_msg))
sc.has_error = True
return
if predecessors is not None:
for tag in predecessors:
if tag is not None:
if self.cnt_structure[tag] == 0:
error_msg = '%s must precede %s' % (tag, tname)
sc.errors.append((sc.tag_start[0], sc.tag_start[1], error_msg))
sc.has_error = True
return
if self.pkgver is not None and self.pkgver == '2.0':
# illegal EPUB3 manifest properties in an EPUB2
if tname == 'item' and 'properties' in [x.lower() for x in tattr.keys()]:
error_msg = 'invalid EPUB3 manifest properties for manifest entry: %s' % (tname)
sc.errors.append((sc.tag_start[0], sc.tag_start[1], error_msg))
sc.has_error = True
if self.pkgver is not None and self.pkgver == '3.0':
# invalid manifest properties in an EPUB3
pass
elif ttype == 'end':
# Void tag check (closing tags are OK if last_text is None or all spaces)
if isVoid and ttype == 'end' and not empty(sc.last_text):
error_msg = 'Void tag: ' + tname + ' has illegal contents'
sc.errors.append((sc.tag_start[0], sc.tag_start[1], error_msg))
sc.has_error = True
return
else:
error_msg = 'illegal tag found: %s' % tname
sc.errors.append((sc.tag_start[0], sc.tag_start[1], error_msg))
sc.has_error = True
return
def mincounts(self):
error_msg = None
for key in self.cnt_structure:
cnt = self.cnt_structure[key]
[allowed_parents, isVoid, mincnt, maxcnt, predecessors] = xml_structure[key]
if cnt < mincnt:
error_msg = 'required tag missing: %s' % key
break
return error_msg
|