Reload the raw data from file or URL.
(self)
| 650 | return self.data |
| 651 | |
| 652 | def reload(self): |
| 653 | """Reload the raw data from file or URL.""" |
| 654 | if self.filename is not None: |
| 655 | with open(self.filename, self._read_flags) as f: |
| 656 | self.data = f.read() |
| 657 | elif self.url is not None: |
| 658 | # Deferred import |
| 659 | from urllib.request import urlopen |
| 660 | response = urlopen(self.url) |
| 661 | data = response.read() |
| 662 | # extract encoding from header, if there is one: |
| 663 | encoding = None |
| 664 | if 'content-type' in response.headers: |
| 665 | for sub in response.headers['content-type'].split(';'): |
| 666 | sub = sub.strip() |
| 667 | if sub.startswith('charset'): |
| 668 | encoding = sub.split('=')[-1].strip() |
| 669 | break |
| 670 | if 'content-encoding' in response.headers: |
| 671 | # TODO: do deflate? |
| 672 | if 'gzip' in response.headers['content-encoding']: |
| 673 | import gzip |
| 674 | from io import BytesIO |
| 675 | with gzip.open(BytesIO(data), 'rt', encoding=encoding) as fp: |
| 676 | encoding = None |
| 677 | data = fp.read() |
| 678 | |
| 679 | # decode data, if an encoding was specified |
| 680 | # We only touch self.data once since |
| 681 | # subclasses such as SVG have @data.setter methods |
| 682 | # that transform self.data into ... well svg. |
| 683 | if encoding: |
| 684 | self.data = data.decode(encoding, 'replace') |
| 685 | else: |
| 686 | self.data = data |
| 687 | |
| 688 | |
| 689 | class TextDisplayObject(DisplayObject): |