Reload the raw data from file or URL.
(self)
| 363 | return self.data |
| 364 | |
| 365 | def reload(self): |
| 366 | """Reload the raw data from file or URL.""" |
| 367 | if self.filename is not None: |
| 368 | encoding = None if "b" in self._read_flags else "utf-8" |
| 369 | with open(self.filename, self._read_flags, encoding=encoding) as f: |
| 370 | self.data = f.read() |
| 371 | elif self.url is not None: |
| 372 | # Deferred import |
| 373 | from urllib.request import urlopen |
| 374 | response = urlopen(self.url) |
| 375 | data = response.read() |
| 376 | # extract encoding from header, if there is one: |
| 377 | encoding = None |
| 378 | if 'content-type' in response.headers: |
| 379 | for sub in response.headers['content-type'].split(';'): |
| 380 | sub = sub.strip() |
| 381 | if sub.startswith('charset'): |
| 382 | encoding = sub.split('=')[-1].strip() |
| 383 | break |
| 384 | if 'content-encoding' in response.headers: |
| 385 | if 'gzip' in response.headers['content-encoding']: |
| 386 | import gzip |
| 387 | from io import BytesIO |
| 388 | |
| 389 | # assume utf-8 if encoding is not specified |
| 390 | with gzip.open( |
| 391 | BytesIO(data), "rt", encoding=encoding or "utf-8" |
| 392 | ) as fp: |
| 393 | encoding = None |
| 394 | data = fp.read() |
| 395 | |
| 396 | # decode data, if an encoding was specified |
| 397 | # We only touch self.data once since |
| 398 | # subclasses such as SVG have @data.setter methods |
| 399 | # that transform self.data into ... well svg. |
| 400 | if encoding: |
| 401 | self.data = data.decode(encoding, 'replace') |
| 402 | else: |
| 403 | self.data = data |
| 404 | |
| 405 | |
| 406 | class TextDisplayObject(DisplayObject): |