Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
src/sage/repl/rich_output/: Move example files here from src/sage/ext…
Browse files Browse the repository at this point in the history
…_data, use importlib.resources
  • Loading branch information
mkoeppe committed Dec 6, 2021
1 parent af47b5f commit b2cb173
Show file tree
Hide file tree
Showing 22 changed files with 24 additions and 51 deletions.
31 changes: 7 additions & 24 deletions src/sage/repl/rich_output/output_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import os
import base64
import importlib.resources

from sage.cpython.string import bytes_to_str
from sage.repl.rich_output.output_basic import OutputBase
Expand Down Expand Up @@ -74,10 +75,7 @@ def example(cls):
sage: OutputImagePng.example().png.get().startswith(b'\x89PNG')
True
"""
from sage.env import SAGE_EXTCODE
filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.png')
with open(filename, 'rb') as f:
return cls(f.read())
return cls(importlib.resources.read_binary(__package__, 'example.png'))


class OutputImageGif(OutputBase):
Expand Down Expand Up @@ -125,10 +123,7 @@ def example(cls):
sage: OutputImageGif.example().gif.get().startswith(b'GIF89a')
True
"""
from sage.env import SAGE_EXTCODE
filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.gif')
with open(filename, 'rb') as f:
return cls(f.read())
return cls(importlib.resources.read_binary(__package__, 'example.gif'))

def html_fragment(self):
"""
Expand Down Expand Up @@ -195,10 +190,7 @@ def example(cls):
sage: OutputImageJpg.example().jpg.get().startswith(b'\xff\xd8\xff\xe0\x00\x10JFIF')
True
"""
from sage.env import SAGE_EXTCODE
filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.jpg')
with open(filename, 'rb') as f:
return cls(f.read())
return cls(importlib.resources.read_binary(__package__, 'example.jpg'))


class OutputImageSvg(OutputBase):
Expand Down Expand Up @@ -246,10 +238,7 @@ def example(cls):
sage: b'</svg>' in OutputImageSvg.example().svg.get()
True
"""
from sage.env import SAGE_EXTCODE
filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.svg')
with open(filename, 'rb') as f:
return cls(f.read())
return cls(importlib.resources.read_binary(__package__, 'example.svg'))


class OutputImagePdf(OutputBase):
Expand Down Expand Up @@ -297,10 +286,7 @@ def example(cls):
sage: OutputImagePdf.example().pdf.get().startswith(b'%PDF-1.4')
True
"""
from sage.env import SAGE_EXTCODE
filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.pdf')
with open(filename, 'rb') as f:
return cls(f.read())
return cls(importlib.resources.read_binary(__package__, 'example.pdf'))


class OutputImageDvi(OutputBase):
Expand Down Expand Up @@ -348,7 +334,4 @@ def example(cls):
sage: b'TeX output' in OutputImageDvi.example().dvi.get()
True
"""
from sage.env import SAGE_EXTCODE
filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.dvi')
with open(filename, 'rb') as f:
return cls(f.read())
return cls(importlib.resources.read_binary(__package__, 'example.dvi'))
30 changes: 10 additions & 20 deletions src/sage/repl/rich_output/output_graphics3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


import os
import importlib.resources

from sage.cpython.string import bytes_to_str, FS_ENCODING
from sage.repl.rich_output.output_basic import OutputBase
Expand Down Expand Up @@ -110,15 +111,8 @@ def example(cls):
sage: rich_output.preview_png.get().startswith(b'\x89PNG')
True
"""
from sage.env import SAGE_EXTCODE
example_png_filename = os.path.join(
SAGE_EXTCODE, 'doctest', 'rich_output', 'example.png')
with open(example_png_filename, 'rb') as f:
example_png = f.read()
scene_zip_filename = os.path.join(
SAGE_EXTCODE, 'doctest', 'rich_output', 'example_jmol.spt.zip')
with open(scene_zip_filename, 'rb') as f:
scene_zip = f.read()
example_png = importlib.resources.read_binary(__package__, 'example.png')
scene_zip = importlib.resources.read_binary(__package__, 'example_jmol.spt.zip')
return cls(scene_zip, example_png)


Expand Down Expand Up @@ -163,10 +157,8 @@ def example(cls):
sage: rich_output.canvas3d.get_str()
'[{"vertices":[{"x":1,"y":1,"z":1},...{"x":1,"y":-1,"z":-1}],"faces":[[0,1,2,3]],"color":"008000"}]'
"""
from sage.env import SAGE_EXTCODE
filename = os.path.join(
SAGE_EXTCODE, 'doctest', 'rich_output', 'example.canvas3d')
return cls(OutputBuffer.from_file(filename))
with importlib.resources.path(__package__, 'example.canvas3d') as filename:
return cls(OutputBuffer.from_file(filename))


class OutputSceneThreejs(OutputBase):
Expand Down Expand Up @@ -358,10 +350,8 @@ def example(cls):
sage: rich_output.mtl.get_str()
'newmtl texture177\nKa 0.2 0.2 0.5\nKd 0.4 0.4 1.0\nKs 0.0 0.0 0.0\nillum 1\nNs 1\nd 1\n'
"""
from sage.env import SAGE_EXTCODE
with_path = lambda x: os.path.join(
SAGE_EXTCODE, 'doctest', 'rich_output', 'example_wavefront', x)
return cls(
OutputBuffer.from_file(with_path('scene.obj')),
OutputBuffer.from_file(with_path('scene.mtl')),
)
with importlib.resources.path(__package__, 'example_wavefront_scene.obj') as filename:
scene_obj = OutputBuffer.from_file(filename)
with importlib.resources.path(__package__, 'example_wavefront_scene.mtl') as filename:
scene_mtl = OutputBuffer.from_file(filename)
return cls(scene_obj, scene_mtl)
9 changes: 4 additions & 5 deletions src/sage/repl/rich_output/output_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


import os
import importlib.resources

from sage.repl.rich_output.output_basic import OutputBase
from sage.repl.rich_output.buffer import OutputBuffer
Expand Down Expand Up @@ -70,11 +71,9 @@ def example(cls):
sage: OutputVideoOgg.example().mimetype
'video/ogg'
"""
from sage.env import SAGE_EXTCODE
filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output',
'example' + cls.ext)
return cls(OutputBuffer.from_file(filename),
{'controls': True, 'loop': False})
with importlib.resources.path(__package__, 'example' + cls.ext) as filename:
return cls(OutputBuffer.from_file(filename),
{'controls': True, 'loop': False})

def html_fragment(self, url, link_attrs=''):
r"""
Expand Down
5 changes: 3 additions & 2 deletions src/setup.cfg.m4
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ sage.interfaces =
sage.doctest =
tests/*

sage.repl.rich_output =
example*

sage =
ext_data/*
ext_data/kenzo/*
Expand All @@ -140,8 +143,6 @@ sage =
ext_data/images/*
ext_data/doctest/*
ext_data/doctest/invalid/*
ext_data/doctest/rich_output/*
ext_data/doctest/rich_output/example_wavefront/*
ext_data/gap/*
ext_data/gap/joyner/*
ext_data/mwrank/*
Expand Down

0 comments on commit b2cb173

Please sign in to comment.