Skip to content

Commit

Permalink
Added error messages for empty artwork files. Tweaked the error messa…
Browse files Browse the repository at this point in the history
…ge for missing artwork text. Addresses issue #370

 - Legacy-Id: 2563
  • Loading branch information
levkowetz committed Sep 15, 2018
1 parent 6362e0d commit a331fbf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions cli/xml2rfc/writers/preptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from codecs import open
from collections import namedtuple
from contextlib import closing

try:
import debug
Expand Down Expand Up @@ -1335,6 +1336,7 @@ def element_artwork(self, e, p):
src = e.get('src','').strip()
if src:
original_src = src
data = None
scheme, netloc, path, query, fragment = self.check_src_scheme(e, src)

# 2. If an <artwork> element has a "src" attribute with a "file:"
Expand Down Expand Up @@ -1366,7 +1368,9 @@ def element_artwork(self, e, p):

if src: # Test again, after check_src_file_path()
awtype = e.get('type')
if awtype == 'svg':
if awtype is None:
self.warn(e, "No 'type' attribute value provided for <artwork>, cannot process source %s" % src)
elif awtype == 'svg':

# * If the "src" URI scheme is "data:", fill the content of the
# <artwork> element with that data and remove the "src"
Expand Down Expand Up @@ -1430,7 +1434,7 @@ def element_artwork(self, e, p):
elif awtype == 'binary-art':
# keep svg in src attribute
if scheme in ['http', 'https']:
with urlopen(src) as f:
with closing(urlopen(src)) as f:
data = f.read()
if six.PY2:
mediatype = f.info().gettype()
Expand Down Expand Up @@ -1458,12 +1462,14 @@ def element_artwork(self, e, p):
# "originalSrc" attribute with the value of the URI and remove
# the "src" attribute.
else:
with urlopen(src) as f:
with closing(urlopen(src)) as f:
data = f.read()
debug.type('data')
e.text = data
del e.attrib['src']

if src and not data:
self.warn(e, "No image data found in source %s" % src)

# 5.5.2. <sourcecode> Processing

def element_sourcecode(self, e, p):
Expand Down Expand Up @@ -1514,7 +1520,7 @@ def element_sourcecode(self, e, p):
# the value of the URI and remove the "src" attribute.

if src: # Test again, after check_src_file_path()
with urlopen(src) as f:
with closing(urlopen(src)) as f:
data = f.read()
e.text = data
del e.attrib['src']
Expand Down
2 changes: 1 addition & 1 deletion cli/xml2rfc/writers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def render_annotation(self, e, width, **kwargs):
# See Section 5 for a description of how to deal with issues of using
# "&" and "<" characters in artwork.
def render_artwork(self, e, width, **kwargs):
text = (e.text and e.text.expandtabs()) or "(Artwork only available as %s: <%s>)" % (e.get('type'), e.get('originalSrc'))
text = (e.text and e.text.expandtabs()) or "(Artwork only available as %s: <%s>)" % (e.get('type', '(unknown type)'), e.get('originalSrc'))
text += e.tail or ''
text = text.strip('\n')
text = '\n'.join( [ l.rstrip() for l in text.splitlines() ] )
Expand Down

0 comments on commit a331fbf

Please sign in to comment.