Skip to content

Commit

Permalink
Meson: add sage cli
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Nov 20, 2024
1 parent 39ebbe4 commit 99c45e8
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ R = [
file = "README.md"
content-type = "text/markdown"

[project.scripts]
sage = "sage.cli:main"

[tool.conda-lock]
platforms = [
'osx-64', 'linux-64', 'linux-aarch64', 'osx-arm64'
Expand Down
40 changes: 40 additions & 0 deletions src/sage/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

import argparse
import logging
import sys

from sage.cli.interactive_shell_cmd import InteractiveShellCmd
from sage.cli.options import CliOptions
from sage.cli.version_cmd import VersionCmd


def main() -> int:
input_args = sys.argv[1:]
parser = argparse.ArgumentParser(
prog="sage",
description="If no command is given, starts the interactive interpreter where you can enter statements and expressions, immediately execute them and see their results.",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=False,
help="print additional information",
)

VersionCmd.extend_parser(parser)

if not input_args:
InteractiveShellCmd(CliOptions()).run()

args = parser.parse_args(input_args)
options = CliOptions(**vars(args))

logging.basicConfig(level=logging.DEBUG if options.verbose else logging.INFO)

return InteractiveShellCmd(options).run()


if __name__ == "__main__":
sys.exit(main())
25 changes: 25 additions & 0 deletions src/sage/cli/interactive_shell_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from sage.cli.options import CliOptions


class InteractiveShellCmd:
def __init__(self, options: CliOptions):
r"""
Initialize the command.
"""
self.options = options

def run(self) -> int:
r"""
Start the interactive shell.
"""
# Display startup banner. Do this before anything else to give the user
# early feedback that Sage is starting.
from sage.misc.banner import banner

banner()

from sage.repl.interpreter import SageTerminalApp

app = SageTerminalApp.instance()
app.initialize([])
return app.start() # type: ignore
8 changes: 8 additions & 0 deletions src/sage/cli/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

py.install_sources(
'__init__.py',
'interactive_shell_cmd.py',
'options.py',
'version_cmd.py',
subdir: 'sage/cli',
)
13 changes: 13 additions & 0 deletions src/sage/cli/options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import dataclass


@dataclass
class CliOptions:
"""
A TypedDict for command-line interface options.
Attributes:
verbose (bool): Indicates whether verbose output is enabled.
"""

verbose: bool = False
25 changes: 25 additions & 0 deletions src/sage/cli/version_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import argparse

from sage.version import version


class VersionCmd:
@staticmethod
def extend_parser(parser: argparse.ArgumentParser):
r"""
Extend the parser with the version command.
INPUT:
- ``parsers`` -- the parsers to extend.
OUTPUT:
- the extended parser.
"""
parser.add_argument(
"--version",
action="version",
version=version,
help="print the version number and exit",
)
1 change: 1 addition & 0 deletions src/sage/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ subdir('symbolic')
subdir('tests')
subdir('dynamics')
subdir('sat')
subdir('cli')
3 changes: 3 additions & 0 deletions src/sage/misc/banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def version():
sage: version()

Check failure on line 27 in src/sage/misc/banner.py

View workflow job for this annotation

GitHub Actions / test-new

Failed example:

Failed example:: Got: doctest:warning File "<doctest sage.misc.banner.version[0]>", line 1, in <module> version() File "/sage/src/sage/misc/banner.py", line 32, in version deprecation(1, "Use sage.version instead.") File "/sage/src/sage/misc/superseded.py", line 99, in deprecation warning(issue_number, message, DeprecationWarning, stacklevel) File "/sage/src/sage/misc/superseded.py", line 180, in warning warn(message, warning_class, stacklevel) File "/usr/lib/python3.10/warnings.py", line 109, in _showwarnmsg sw(msg.message, msg.category, msg.filename, msg.lineno, : DeprecationWarning: Use sage.version instead. See https://github.com/sagemath/sage/issues/1 for details. 'SageMath version 10.5.rc0, Release Date: 2024-11-16'
'SageMath version ..., Release Date: ...'
"""
from sage.misc.superseded import deprecation

deprecation(1, "Use sage.version instead.")
return SAGE_VERSION_BANNER


Expand Down

0 comments on commit 99c45e8

Please sign in to comment.