numerous 0.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of numerous might be problematic. Click here for more details.
- numerous/__init__.py +5 -0
- numerous/__main__.py +50 -0
- numerous/_plotly.py +5 -0
- numerous/appdev/__init__.py +1 -0
- numerous/appdev/commands.py +207 -0
- numerous/apps.py +155 -0
- numerous/cli/__init__.py +5 -0
- numerous/cli/__main__.py +6 -0
- numerous/cli/_color.py +48 -0
- numerous/cli/_upgrade.py +115 -0
- numerous/cli/build/darwin_amd64 +0 -0
- numerous/cli/build/darwin_arm64 +0 -0
- numerous/cli/build/linux_amd64 +0 -0
- numerous/cli/build/linux_arm64 +0 -0
- numerous/cli/build/windows_amd64 +0 -0
- numerous/cli/build/windows_arm64 +0 -0
- numerous/cli/run.py +55 -0
- numerous/data_model.py +210 -0
- numerous/experimental/__init__.py +8 -0
- numerous/experimental/marimo.py +172 -0
- numerous/experimental/model.py +372 -0
- numerous/generated/graphql/__init__.py +133 -0
- numerous/generated/graphql/all_elements.py +118 -0
- numerous/generated/graphql/async_base_client.py +372 -0
- numerous/generated/graphql/base_model.py +29 -0
- numerous/generated/graphql/client.py +179 -0
- numerous/generated/graphql/enums.py +20 -0
- numerous/generated/graphql/exceptions.py +85 -0
- numerous/generated/graphql/fragments.py +81 -0
- numerous/generated/graphql/input_types.py +40 -0
- numerous/generated/graphql/update_element.py +26 -0
- numerous/generated/graphql/updates.py +149 -0
- numerous/py.typed +0 -0
- numerous/session.py +520 -0
- numerous/tools/__init__.py +11 -0
- numerous/updates.py +179 -0
- numerous/utils.py +12 -0
- numerous-0.0.0.dist-info/METADATA +128 -0
- numerous-0.0.0.dist-info/RECORD +42 -0
- numerous-0.0.0.dist-info/WHEEL +5 -0
- numerous-0.0.0.dist-info/entry_points.txt +2 -0
- numerous-0.0.0.dist-info/top_level.txt +1 -0
numerous/__init__.py
ADDED
numerous/__main__.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""An entrypoint for creating and running app sessions."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import asyncio
|
|
5
|
+
import logging
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from numerous.appdev.commands import (
|
|
10
|
+
read_app,
|
|
11
|
+
run_app_session,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
log = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
if __name__ == "__main__":
|
|
18
|
+
parser = argparse.ArgumentParser()
|
|
19
|
+
|
|
20
|
+
shared_parser = argparse.ArgumentParser()
|
|
21
|
+
shared_parser.add_argument("module_path", type=Path)
|
|
22
|
+
shared_parser.add_argument("class_name")
|
|
23
|
+
|
|
24
|
+
cmd_parsers = parser.add_subparsers(title="Command", dest="cmd")
|
|
25
|
+
read_parser = cmd_parsers.add_parser(
|
|
26
|
+
"read",
|
|
27
|
+
parents=[shared_parser],
|
|
28
|
+
add_help=False,
|
|
29
|
+
)
|
|
30
|
+
run_parser = cmd_parsers.add_parser("run", parents=[shared_parser], add_help=False)
|
|
31
|
+
run_parser.add_argument("--graphql-url", required=True)
|
|
32
|
+
run_parser.add_argument("--graphql-ws-url", required=True)
|
|
33
|
+
run_parser.add_argument("session_id")
|
|
34
|
+
|
|
35
|
+
ns = parser.parse_args()
|
|
36
|
+
|
|
37
|
+
if ns.cmd == "read":
|
|
38
|
+
read_app(ns.module_path, ns.class_name)
|
|
39
|
+
elif ns.cmd == "run":
|
|
40
|
+
coroutine = run_app_session(
|
|
41
|
+
ns.graphql_url,
|
|
42
|
+
ns.graphql_ws_url,
|
|
43
|
+
ns.session_id,
|
|
44
|
+
ns.module_path,
|
|
45
|
+
ns.class_name,
|
|
46
|
+
)
|
|
47
|
+
asyncio.run(coroutine)
|
|
48
|
+
else:
|
|
49
|
+
print(f"Unsupported command {ns.cmd!r}") # noqa: T201
|
|
50
|
+
sys.exit(1)
|
numerous/_plotly.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Functionality related to the app development CLI."""
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"""App development CLI command implementations."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import logging
|
|
5
|
+
import sys
|
|
6
|
+
import traceback
|
|
7
|
+
from dataclasses import asdict, dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from textwrap import indent
|
|
10
|
+
from typing import Any, Optional, TextIO, Type
|
|
11
|
+
|
|
12
|
+
from numerous.data_model import dump_data_model
|
|
13
|
+
from numerous.generated.graphql import Client
|
|
14
|
+
from numerous.session import Session
|
|
15
|
+
from numerous.utils import AppT
|
|
16
|
+
|
|
17
|
+
log = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class AppNotFoundError(Exception):
|
|
22
|
+
app: str
|
|
23
|
+
existing_apps: list[str]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class AppLoadRaisedError(Exception):
|
|
28
|
+
traceback: str
|
|
29
|
+
typename: str
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class AppSyntaxError(Exception):
|
|
34
|
+
msg: str
|
|
35
|
+
context: str
|
|
36
|
+
lineno: int
|
|
37
|
+
offset: int
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def read_app(app_module: Path, app_class: str, output: TextIO = sys.stdout) -> None:
|
|
41
|
+
try:
|
|
42
|
+
app_cls = _read_app_definition(app_module, app_class)
|
|
43
|
+
except Exception as e: # noqa: BLE001
|
|
44
|
+
print_error(output, e)
|
|
45
|
+
else:
|
|
46
|
+
print_app(output, app_cls)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _transform_lineno(lineno: Optional[int]) -> int:
|
|
50
|
+
if lineno is None:
|
|
51
|
+
return 0
|
|
52
|
+
return lineno - 2
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _transform_offset(offset: Optional[int]) -> int:
|
|
56
|
+
if offset is None:
|
|
57
|
+
return 0
|
|
58
|
+
return offset - 4
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _read_app_definition(app_module: Path, app_class: str) -> Any: # noqa: ANN401
|
|
62
|
+
scope: dict[str, Any] = {}
|
|
63
|
+
module_text = app_module.read_text()
|
|
64
|
+
|
|
65
|
+
# Check for syntax errors with raw code text
|
|
66
|
+
try:
|
|
67
|
+
compile(module_text, str(app_module), "exec")
|
|
68
|
+
except SyntaxError as e:
|
|
69
|
+
text = e.text or ""
|
|
70
|
+
error_pointer = " " * ((e.offset or 0) - 1) + "^"
|
|
71
|
+
|
|
72
|
+
# ensure newline between text and pointer
|
|
73
|
+
if not text.endswith("\n"):
|
|
74
|
+
text += "\n"
|
|
75
|
+
|
|
76
|
+
raise AppSyntaxError(
|
|
77
|
+
msg=e.msg,
|
|
78
|
+
context=text + error_pointer,
|
|
79
|
+
lineno=e.lineno or 0,
|
|
80
|
+
offset=e.offset or 0,
|
|
81
|
+
) from e
|
|
82
|
+
|
|
83
|
+
indented_module_text = indent(module_text, " ")
|
|
84
|
+
exception_handled_module_text = (
|
|
85
|
+
"try:\n"
|
|
86
|
+
f"{indented_module_text}\n"
|
|
87
|
+
"except ModuleNotFoundError:\n"
|
|
88
|
+
" raise\n"
|
|
89
|
+
"except BaseException as e:\n"
|
|
90
|
+
" __numerous_read_error__ = e\n"
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
code = compile(exception_handled_module_text, str(app_module), "exec")
|
|
94
|
+
|
|
95
|
+
exec(code, scope) # noqa: S102
|
|
96
|
+
|
|
97
|
+
unknown_error = scope.get("__numerous_read_error__")
|
|
98
|
+
if isinstance(unknown_error, BaseException):
|
|
99
|
+
tb = traceback.TracebackException.from_exception(unknown_error)
|
|
100
|
+
|
|
101
|
+
# handle inserted exception handler offsetting position
|
|
102
|
+
for frame in tb.stack:
|
|
103
|
+
if frame.filename == str(app_module):
|
|
104
|
+
frame.lineno = _transform_lineno(frame.lineno)
|
|
105
|
+
if sys.version_info >= (3, 11):
|
|
106
|
+
frame.colno = _transform_offset(frame.colno)
|
|
107
|
+
frame.end_lineno = _transform_lineno(frame.end_lineno)
|
|
108
|
+
frame.end_colno = _transform_offset(frame.end_colno)
|
|
109
|
+
|
|
110
|
+
raise AppLoadRaisedError(
|
|
111
|
+
typename=type(unknown_error).__name__,
|
|
112
|
+
traceback="".join(tb.format()),
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
try:
|
|
116
|
+
return scope[app_class]
|
|
117
|
+
except KeyError as err:
|
|
118
|
+
raise AppNotFoundError(
|
|
119
|
+
app_class,
|
|
120
|
+
[
|
|
121
|
+
app.__name__
|
|
122
|
+
for app in scope.values()
|
|
123
|
+
if getattr(app, "__numerous_app__", False)
|
|
124
|
+
],
|
|
125
|
+
) from err
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def print_app(output: TextIO, cls: Type[AppT]) -> None:
|
|
129
|
+
data_model = dump_data_model(cls)
|
|
130
|
+
output.write(json.dumps({"app": asdict(data_model)}))
|
|
131
|
+
output.flush()
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def print_error(output: TextIO, error: Exception) -> None:
|
|
135
|
+
if isinstance(error, AppNotFoundError):
|
|
136
|
+
output.write(
|
|
137
|
+
json.dumps(
|
|
138
|
+
{
|
|
139
|
+
"error": {
|
|
140
|
+
"appnotfound": {
|
|
141
|
+
"app": error.app,
|
|
142
|
+
"found_apps": error.existing_apps,
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
),
|
|
147
|
+
)
|
|
148
|
+
elif isinstance(error, AppSyntaxError):
|
|
149
|
+
output.write(
|
|
150
|
+
json.dumps(
|
|
151
|
+
{
|
|
152
|
+
"error": {
|
|
153
|
+
"appsyntax": {
|
|
154
|
+
"msg": error.msg,
|
|
155
|
+
"context": error.context,
|
|
156
|
+
"pos": {
|
|
157
|
+
"line": error.lineno,
|
|
158
|
+
"offset": error.offset,
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
),
|
|
164
|
+
)
|
|
165
|
+
output.flush()
|
|
166
|
+
elif isinstance(error, ModuleNotFoundError):
|
|
167
|
+
output.write(
|
|
168
|
+
json.dumps(
|
|
169
|
+
{
|
|
170
|
+
"error": {
|
|
171
|
+
"modulenotfound": {
|
|
172
|
+
"module": error.name,
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
),
|
|
177
|
+
)
|
|
178
|
+
output.flush()
|
|
179
|
+
elif isinstance(error, AppLoadRaisedError):
|
|
180
|
+
output.write(
|
|
181
|
+
json.dumps(
|
|
182
|
+
{
|
|
183
|
+
"error": {
|
|
184
|
+
"unknown": {
|
|
185
|
+
"typename": error.typename,
|
|
186
|
+
"traceback": error.traceback,
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
),
|
|
191
|
+
)
|
|
192
|
+
output.flush()
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
async def run_app_session(
|
|
196
|
+
graphql_url: str,
|
|
197
|
+
graphql_ws_url: str,
|
|
198
|
+
session_id: str,
|
|
199
|
+
app_module: Path,
|
|
200
|
+
app_class: str,
|
|
201
|
+
) -> None:
|
|
202
|
+
log.info("running %s:%s in session %s", app_module, app_class, session_id)
|
|
203
|
+
gql = Client(graphql_url, ws_url=graphql_ws_url)
|
|
204
|
+
app_cls = _read_app_definition(app_module, app_class)
|
|
205
|
+
session = await Session.initialize(session_id, gql, app_cls)
|
|
206
|
+
await session.run()
|
|
207
|
+
log.info("app session stopped")
|
numerous/apps.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Define applications with a dataclass-like interface."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from types import MappingProxyType
|
|
5
|
+
from typing import Any, Callable, Optional, Type, Union, overload
|
|
6
|
+
|
|
7
|
+
from typing_extensions import dataclass_transform
|
|
8
|
+
|
|
9
|
+
from numerous.utils import MISSING, AppT
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class HTML:
|
|
13
|
+
def __init__(self, default: str) -> None:
|
|
14
|
+
self.default = default
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def html( # type: ignore[no-untyped-def] # noqa: ANN201, PLR0913
|
|
18
|
+
*,
|
|
19
|
+
default: str = MISSING, # type: ignore[assignment]
|
|
20
|
+
default_factory: Callable[[], str] = MISSING, # type: ignore[assignment] # noqa: ARG001
|
|
21
|
+
init: bool = True, # noqa: ARG001
|
|
22
|
+
repr: bool = True, # noqa: ARG001, A002
|
|
23
|
+
hash: Optional[bool] = None, # noqa: ARG001, A002
|
|
24
|
+
compare: bool = True, # noqa: ARG001
|
|
25
|
+
metadata: Optional[MappingProxyType[str, Any]] = None, # noqa: ARG001
|
|
26
|
+
kw_only: bool = MISSING, # type: ignore[assignment] # noqa: ARG001
|
|
27
|
+
):
|
|
28
|
+
return HTML(default)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
DEFAULT_FLOAT_MIN = 0.0
|
|
32
|
+
DEFAULT_FLOAT_MAX = 100.0
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Slider:
|
|
36
|
+
def __init__( # noqa: PLR0913
|
|
37
|
+
self,
|
|
38
|
+
*,
|
|
39
|
+
default: float = MISSING, # type: ignore[assignment]
|
|
40
|
+
default_factory: Callable[[], float] = MISSING, # type: ignore[assignment] # noqa: ARG002
|
|
41
|
+
init: bool = True, # noqa: ARG002
|
|
42
|
+
repr: bool = True, # noqa: ARG002, A002
|
|
43
|
+
hash: Optional[bool] = None, # noqa: ARG002, A002
|
|
44
|
+
compare: bool = True, # noqa: ARG002
|
|
45
|
+
metadata: Optional[MappingProxyType[str, Any]] = None, # noqa: ARG002
|
|
46
|
+
kw_only: bool = MISSING, # type: ignore[assignment] # noqa: ARG002
|
|
47
|
+
label: Optional[str] = None,
|
|
48
|
+
min_value: float = DEFAULT_FLOAT_MIN,
|
|
49
|
+
max_value: float = DEFAULT_FLOAT_MAX,
|
|
50
|
+
) -> None:
|
|
51
|
+
self.default = default
|
|
52
|
+
self.label = label
|
|
53
|
+
self.min_value = min_value
|
|
54
|
+
self.max_value = max_value
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def slider( # type: ignore[no-untyped-def] # noqa: ANN201, PLR0913
|
|
58
|
+
*,
|
|
59
|
+
default: float = MISSING, # type: ignore[assignment]
|
|
60
|
+
default_factory: Callable[[], float] = MISSING, # type: ignore[assignment] # noqa: ARG001
|
|
61
|
+
init: bool = True, # noqa: ARG001
|
|
62
|
+
repr: bool = True, # noqa: ARG001, A002
|
|
63
|
+
hash: Optional[bool] = None, # noqa: ARG001, A002
|
|
64
|
+
compare: bool = True, # noqa: ARG001
|
|
65
|
+
metadata: Optional[MappingProxyType[str, Any]] = None, # noqa: ARG001
|
|
66
|
+
kw_only: bool = MISSING, # type: ignore[assignment] # noqa: ARG001
|
|
67
|
+
label: Optional[str] = None,
|
|
68
|
+
min_value: float = DEFAULT_FLOAT_MIN,
|
|
69
|
+
max_value: float = DEFAULT_FLOAT_MAX,
|
|
70
|
+
):
|
|
71
|
+
return Slider(
|
|
72
|
+
default=default,
|
|
73
|
+
label=label,
|
|
74
|
+
min_value=min_value,
|
|
75
|
+
max_value=max_value,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class Field:
|
|
80
|
+
def __init__( # noqa: PLR0913
|
|
81
|
+
self,
|
|
82
|
+
*,
|
|
83
|
+
default: Union[str, float] = MISSING, # type: ignore[assignment]
|
|
84
|
+
default_factory: Callable[[], Union[str, float]] = MISSING, # type: ignore[assignment]
|
|
85
|
+
init: bool = True, # noqa: ARG002
|
|
86
|
+
repr: bool = True, # noqa: ARG002, A002
|
|
87
|
+
hash: Optional[bool] = None, # noqa: ARG002, A002
|
|
88
|
+
compare: bool = True, # noqa: ARG002
|
|
89
|
+
metadata: Optional[MappingProxyType[str, Any]] = None, # noqa: ARG002
|
|
90
|
+
kw_only: bool = MISSING, # type: ignore[assignment] # noqa: ARG002
|
|
91
|
+
label: Optional[str] = None,
|
|
92
|
+
) -> None:
|
|
93
|
+
if default is MISSING and default_factory is not MISSING: # type: ignore[comparison-overlap]
|
|
94
|
+
default = default_factory()
|
|
95
|
+
self.default = default
|
|
96
|
+
self.label = label
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def field( # type: ignore[no-untyped-def] # noqa: ANN201, PLR0913
|
|
100
|
+
*,
|
|
101
|
+
default: Union[str, float] = MISSING, # type: ignore[assignment]
|
|
102
|
+
default_factory: Callable[[], float] = MISSING, # type: ignore[assignment]
|
|
103
|
+
init: bool = True, # noqa: ARG001
|
|
104
|
+
repr: bool = True, # noqa: ARG001, A002
|
|
105
|
+
hash: Optional[bool] = None, # noqa: ARG001, A002
|
|
106
|
+
compare: bool = True, # noqa: ARG001
|
|
107
|
+
metadata: Optional[MappingProxyType[str, Any]] = None, # noqa: ARG001
|
|
108
|
+
kw_only: bool = MISSING, # type: ignore[assignment] # noqa: ARG001
|
|
109
|
+
label: Optional[str] = None,
|
|
110
|
+
):
|
|
111
|
+
return Field(default=default, default_factory=default_factory, label=label)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@dataclass_transform()
|
|
115
|
+
def container(cls: Type[AppT]) -> Type[AppT]:
|
|
116
|
+
"""Define a container."""
|
|
117
|
+
cls.__container__ = True # type: ignore[attr-defined]
|
|
118
|
+
return dataclass(cls)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def action(action: Callable[[AppT], Any]) -> Callable[[AppT], Any]:
|
|
122
|
+
"""Define an action."""
|
|
123
|
+
action.__action__ = True # type: ignore[attr-defined]
|
|
124
|
+
return action
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@overload
|
|
128
|
+
def app(cls: Type[AppT]) -> Type[AppT]: ...
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@overload
|
|
132
|
+
def app(title: str = ...) -> Callable[[Type[AppT]], Type[AppT]]: ...
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def app(
|
|
136
|
+
*args: Any,
|
|
137
|
+
**kwargs: Any,
|
|
138
|
+
) -> Union[Type[AppT], Callable[[Type[AppT]], Type[AppT]]]:
|
|
139
|
+
invalid_error_message = "Invalid @app usage"
|
|
140
|
+
if len(args) == 1 and not kwargs:
|
|
141
|
+
return app_decorator()(args[0])
|
|
142
|
+
if len(args) == 0 and "title" in kwargs:
|
|
143
|
+
return app_decorator(**kwargs)
|
|
144
|
+
raise ValueError(invalid_error_message)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def app_decorator(**kwargs: dict[str, Any]) -> Callable[[Type[AppT]], Type[AppT]]:
|
|
148
|
+
@dataclass_transform(field_specifiers=(field, html, slider))
|
|
149
|
+
def decorator(cls: Type[AppT]) -> Type[AppT]:
|
|
150
|
+
cls.__numerous_app__ = True # type: ignore[attr-defined]
|
|
151
|
+
if title := kwargs.get("title"):
|
|
152
|
+
cls.__title__ = title # type: ignore[attr-defined]
|
|
153
|
+
return dataclass(cls)
|
|
154
|
+
|
|
155
|
+
return decorator
|
numerous/cli/__init__.py
ADDED
numerous/cli/__main__.py
ADDED
numerous/cli/_color.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Color(Enum):
|
|
5
|
+
BLACK = 30
|
|
6
|
+
RED = 31
|
|
7
|
+
GREEN = 32
|
|
8
|
+
YELLOW = 33
|
|
9
|
+
BLUE = 34
|
|
10
|
+
MAGENTA = 35
|
|
11
|
+
CYAN = 36
|
|
12
|
+
WHITE = 37
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def colorize(color: Color, message: str) -> str:
|
|
16
|
+
return f"\033[;{color.value}m{message}\033[0m"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def yellow(message: str) -> str:
|
|
20
|
+
return colorize(Color.YELLOW, message)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def green(message: str) -> str:
|
|
24
|
+
return colorize(Color.GREEN, message)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def red(message: str) -> str:
|
|
28
|
+
return colorize(Color.RED, message)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def black(message: str) -> str:
|
|
32
|
+
return colorize(Color.BLACK, message)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def white(message: str) -> str:
|
|
36
|
+
return colorize(Color.WHITE, message)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def blue(message: str) -> str:
|
|
40
|
+
return colorize(Color.BLUE, message)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def magenta(message: str) -> str:
|
|
44
|
+
return colorize(Color.MAGENTA, message)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def cyan(message: str) -> str:
|
|
48
|
+
return colorize(Color.CYAN, message)
|
numerous/cli/_upgrade.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
import json
|
|
3
|
+
import urllib.request
|
|
4
|
+
from dataclasses import asdict, dataclass
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from importlib.metadata import version as metadata_version
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, Optional
|
|
9
|
+
|
|
10
|
+
from packaging import version
|
|
11
|
+
|
|
12
|
+
from ._color import blue, green
|
|
13
|
+
|
|
14
|
+
RESPONSE_STATUS_OK = 200
|
|
15
|
+
SECONDS_IN_5_MIN = 300
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class VersionState:
|
|
20
|
+
latest_version: Optional[str] = None
|
|
21
|
+
last_checked_at: Optional[str] = None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _fetch_data_from_pipy() -> dict[str, Any]:
|
|
25
|
+
pipy_url = "https://pypi.org/pypi/numerous/json"
|
|
26
|
+
with urllib.request.urlopen(pipy_url) as response: # noqa: S310
|
|
27
|
+
if response.status == RESPONSE_STATUS_OK:
|
|
28
|
+
data = response.read()
|
|
29
|
+
encoding = response.info().get_content_charset("utf-8")
|
|
30
|
+
return json.loads(data.decode(encoding)) # type: ignore[no-any-return]
|
|
31
|
+
|
|
32
|
+
message = f"HTTP request failed with status code {response.status}"
|
|
33
|
+
raise Exception( # noqa: TRY002
|
|
34
|
+
message,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def get_version_from_pipy() -> Optional[str]:
|
|
39
|
+
try:
|
|
40
|
+
response = _fetch_data_from_pipy()
|
|
41
|
+
return response["info"]["version"] # type: ignore[no-any-return]
|
|
42
|
+
except: # noqa: E722
|
|
43
|
+
return None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def check_for_updates() -> None:
|
|
47
|
+
try:
|
|
48
|
+
_check_for_updates_internal()
|
|
49
|
+
except Exception: # noqa: BLE001
|
|
50
|
+
contextlib.suppress(Exception)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _check_for_updates_internal() -> None:
|
|
54
|
+
# Find the state file
|
|
55
|
+
state_file = Path(".numerous/state.json")
|
|
56
|
+
home_directory = Path.home()
|
|
57
|
+
file_path = home_directory / state_file
|
|
58
|
+
|
|
59
|
+
# Load the state file or create a default state if it doesn't exist
|
|
60
|
+
state: VersionState
|
|
61
|
+
try:
|
|
62
|
+
with Path.open(file_path) as file:
|
|
63
|
+
data = json.load(file)
|
|
64
|
+
state = VersionState(**data)
|
|
65
|
+
except FileNotFoundError:
|
|
66
|
+
state = VersionState()
|
|
67
|
+
|
|
68
|
+
# Maybe update the state with the latest version
|
|
69
|
+
state = _update_with_latest_version(state)
|
|
70
|
+
|
|
71
|
+
if not state.latest_version:
|
|
72
|
+
# We couldn't get the latest version, so do nothing
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
current_version = metadata_version("numerous")
|
|
76
|
+
if current_version and version.parse(state.latest_version) > version.parse(
|
|
77
|
+
current_version,
|
|
78
|
+
):
|
|
79
|
+
upg_str = f"{current_version} → {state.latest_version}"
|
|
80
|
+
message = f"Heads up - A newer version of Numerous is available {upg_str}"
|
|
81
|
+
print(blue(message)) # noqa: T201
|
|
82
|
+
install_cmd = green("pip install --upgrade numerous")
|
|
83
|
+
guide_cmd = f"Please upgrade by running {install_cmd} for the best experience."
|
|
84
|
+
print(guide_cmd) # noqa: T201
|
|
85
|
+
print() # noqa: T201
|
|
86
|
+
|
|
87
|
+
_maybe_create_directory(file_path)
|
|
88
|
+
with Path.open(file_path, "w") as file:
|
|
89
|
+
json.dump(asdict(state), file)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _maybe_create_directory(file_path: Path) -> None:
|
|
93
|
+
state_directory = Path(file_path).parent
|
|
94
|
+
if not Path.exists(state_directory):
|
|
95
|
+
Path.mkdir(state_directory, parents=True)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _update_with_latest_version(state: VersionState) -> VersionState:
|
|
99
|
+
if state.last_checked_at:
|
|
100
|
+
last_checked_date = datetime.strptime(
|
|
101
|
+
state.last_checked_at,
|
|
102
|
+
"%Y-%m-%d %H:%M:%S",
|
|
103
|
+
).astimezone()
|
|
104
|
+
if (datetime.now().astimezone() - last_checked_date).seconds < SECONDS_IN_5_MIN:
|
|
105
|
+
return state
|
|
106
|
+
|
|
107
|
+
try:
|
|
108
|
+
version = get_version_from_pipy()
|
|
109
|
+
state.latest_version = version
|
|
110
|
+
state.last_checked_at = (
|
|
111
|
+
datetime.now().astimezone().strftime("%Y-%m-%d %H:%M:%S")
|
|
112
|
+
)
|
|
113
|
+
return state # noqa: TRY300
|
|
114
|
+
except Exception: # noqa: BLE001
|
|
115
|
+
return state
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
numerous/cli/run.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Launches the correct binary for the users OS."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import platform
|
|
5
|
+
import subprocess
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from numerous.cli._upgrade import check_for_updates
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def get_executable_os_name() -> str:
|
|
13
|
+
system = platform.system()
|
|
14
|
+
if system == "Linux":
|
|
15
|
+
return "linux"
|
|
16
|
+
if system == "Darwin":
|
|
17
|
+
return "darwin"
|
|
18
|
+
if os.name == "nt":
|
|
19
|
+
return "windows"
|
|
20
|
+
|
|
21
|
+
print( # noqa: T201
|
|
22
|
+
f"Sorry but operating system {system} is not currently supported :(",
|
|
23
|
+
)
|
|
24
|
+
sys.exit(1)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_executable_arch_name() -> str:
|
|
28
|
+
arch = platform.machine().lower()
|
|
29
|
+
if arch in ("x86_64", "amd64"):
|
|
30
|
+
return "amd64"
|
|
31
|
+
if arch == "arm64":
|
|
32
|
+
return "arm64"
|
|
33
|
+
print( # noqa: T201
|
|
34
|
+
f"Sorry but architecutre {arch} is not currently supported :(",
|
|
35
|
+
)
|
|
36
|
+
sys.exit(1)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def get_executable_name() -> str:
|
|
40
|
+
return f"build/{get_executable_os_name()}_{get_executable_arch_name()}"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def main() -> None:
|
|
44
|
+
check_for_updates()
|
|
45
|
+
exe_name = get_executable_name()
|
|
46
|
+
exe_path = Path(__file__).parent / exe_name
|
|
47
|
+
try:
|
|
48
|
+
process = subprocess.Popen(args=[str(exe_path)] + sys.argv[1:])
|
|
49
|
+
process.wait()
|
|
50
|
+
except KeyboardInterrupt:
|
|
51
|
+
process.kill()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if __name__ == "__main__":
|
|
55
|
+
main()
|