##// END OF EJS Templates
ensure SVG(url=...) is set correctly
Ian Castleden -
Show More
@@ -615,9 +615,12 b' class DisplayObject(object):'
615 filename = data
615 filename = data
616 data = None
616 data = None
617
617
618 self.data = data
619 self.url = url
618 self.url = url
620 self.filename = filename
619 self.filename = filename
620 # because of @data.setter methods in
621 # subclasses ensure url and filename are set
622 # before assigning to self.data
623 self.data = data
621
624
622 if metadata is not None:
625 if metadata is not None:
623 self.metadata = metadata
626 self.metadata = metadata
@@ -652,23 +655,27 b' class DisplayObject(object):'
652 with open(self.filename, self._read_flags) as f:
655 with open(self.filename, self._read_flags) as f:
653 self.data = f.read()
656 self.data = f.read()
654 elif self.url is not None:
657 elif self.url is not None:
655 try:
658 # Deferred import
656 # Deferred import
659 from urllib.request import urlopen
657 from urllib.request import urlopen
660 response = urlopen(self.url)
658 response = urlopen(self.url)
661 data = response.read()
659 self.data = response.read()
662 # extract encoding from header, if there is one:
660 # extract encoding from header, if there is one:
663 encoding = None
661 encoding = None
664 if "content-type" in response.headers:
662 for sub in response.headers['content-type'].split(';'):
665 for sub in response.headers['content-type'].split(';'):
663 sub = sub.strip()
666 sub = sub.strip()
664 if sub.startswith('charset'):
667 if sub.startswith('charset'):
665 encoding = sub.split('=')[-1].strip()
668 encoding = sub.split('=')[-1].strip()
666 break
669 break
667 # decode data, if an encoding was specified
670 # decode data, if an encoding was specified
668 if encoding:
671 # We only touch self.data once since
669 self.data = self.data.decode(encoding, 'replace')
672 # subclasses such as SVG have @data.setter methods
670 except:
673 # that transform self.data into ... well svg.
671 self.data = None
674 if encoding:
675 self.data = data.decode(encoding, 'replace')
676 else:
677 self.data = data
678
672
679
673 class TextDisplayObject(DisplayObject):
680 class TextDisplayObject(DisplayObject):
674 """Validate that display data is text"""
681 """Validate that display data is text"""
@@ -879,7 +886,7 b' class JSON(DisplayObject):'
879 data = str(data)
886 data = str(data)
880
887
881 if isinstance(data, str):
888 if isinstance(data, str):
882 if getattr(self, 'filename', None) is None:
889 if self.filename is None and self.url is None:
883 warnings.warn("JSON expects JSONable dict or list, not JSON strings")
890 warnings.warn("JSON expects JSONable dict or list, not JSON strings")
884 data = json.loads(data)
891 data = json.loads(data)
885 self._data = data
892 self._data = data
General Comments 0
You need to be logged in to leave comments. Login now