-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from Bubobubobubobubo/rewrite
This is the big rewrite Sardine truly needed to start on the right track. Many many thanks to @thegamecracks for his invaluable contribution to the project. Now for new features :)
- Loading branch information
Showing
102 changed files
with
6,591 additions
and
4,563 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,158 +1,61 @@ | ||
# https://github.com/python/cpython/blob/main/Lib/asyncio/__main__.py | ||
# Taken from the CPython Github Repository. Custom version of the | ||
# asyncio REPL that will autoload Sardine whenever started. | ||
|
||
import concurrent.futures | ||
import threading | ||
import platform | ||
import warnings | ||
import inspect | ||
import asyncio | ||
import psutil | ||
import types | ||
import code | ||
import ast | ||
import os | ||
|
||
from appdirs import user_data_dir | ||
from asyncio import futures | ||
from pathlib import Path | ||
from rich import print as pretty_print | ||
from rich.panel import Panel | ||
|
||
system = platform.system() | ||
|
||
# Setting very high priority for this process (time-critical) | ||
warning_text = "[yellow]/!\\\\[/yellow] [red bold] Run Sardine faster by starting it using\ | ||
\nadministrator priviledges (sudo on Unix..)[/red bold] [yellow]/!\\\\[/yellow]" | ||
if system == "Windows": | ||
try: | ||
p = psutil.Process(os.getpid()) | ||
p.nice(psutil.HIGH_PRIORITY_CLASS) | ||
except psutil.AccessDenied: | ||
pretty_print(Panel.fit(warning_text)) | ||
pass | ||
else: | ||
try: | ||
p = psutil.Process(os.getpid()) | ||
p.nice(-20) | ||
except psutil.AccessDenied: | ||
pretty_print(Panel.fit(warning_text)) | ||
pass | ||
|
||
|
||
# Appdirs boilerplate | ||
APP_NAME, APP_AUTHOR = "Sardine", "Bubobubobubo" | ||
USER_DIR = Path(user_data_dir(APP_NAME, APP_AUTHOR)) | ||
|
||
|
||
class AsyncIOInteractiveConsole(code.InteractiveConsole): | ||
def __init__(self, locals, loop): | ||
super().__init__(locals) | ||
self.compile.compiler.flags |= ast.PyCF_ALLOW_TOP_LEVEL_AWAIT | ||
|
||
self.loop = loop | ||
|
||
def runcode(self, code): | ||
future = concurrent.futures.Future() | ||
|
||
def callback(): | ||
global repl_future | ||
global repl_future_interrupted | ||
|
||
repl_future = None | ||
repl_future_interrupted = False | ||
|
||
func = types.FunctionType(code, self.locals) | ||
try: | ||
coro = func() | ||
except SystemExit: | ||
raise | ||
except KeyboardInterrupt as ex: | ||
repl_future_interrupted = True | ||
future.set_exception(ex) | ||
return | ||
except BaseException as ex: | ||
future.set_exception(ex) | ||
return | ||
|
||
if not inspect.iscoroutine(coro): | ||
future.set_result(coro) | ||
return | ||
|
||
try: | ||
repl_future = self.loop.create_task(coro) | ||
futures._chain_future(repl_future, future) | ||
except BaseException as exc: | ||
future.set_exception(exc) | ||
|
||
loop.call_soon_threadsafe(callback) | ||
|
||
try: | ||
return future.result() | ||
except SystemExit: | ||
raise | ||
except BaseException: | ||
if repl_future_interrupted: | ||
self.write("\nKeyboardInterrupt\n") | ||
else: | ||
self.showtraceback() | ||
|
||
|
||
class REPLThread(threading.Thread): | ||
def run(self): | ||
try: | ||
banner = () | ||
console.push("""import os""") | ||
console.push("""os.environ['SARDINE_INIT_SESSION'] = 'YES'""") | ||
console.push("""from sardine import *""") | ||
console.interact(banner=banner, exitmsg="exiting asyncio REPL...") | ||
finally: | ||
warnings.filterwarnings( | ||
"ignore", | ||
message=r"^coroutine .* was never awaited$", | ||
category=RuntimeWarning, | ||
) | ||
|
||
loop.call_soon_threadsafe(loop.stop) | ||
import click | ||
|
||
from . import console | ||
from .profiler import Profiler | ||
|
||
CONTEXT_SETTINGS = { | ||
"help_option_names": ["-h", "--help"], | ||
} | ||
|
||
|
||
@click.group( | ||
context_settings=CONTEXT_SETTINGS, | ||
help="Starts sardine in an asyncio REPL.", | ||
invoke_without_command=True, | ||
) | ||
@click.version_option( | ||
package_name="sardine", | ||
prog_name=__package__, | ||
message="%(prog)s for %(package)s v%(version)s", | ||
) | ||
@click.pass_context | ||
def main(ctx: click.Context): | ||
if ctx.invoked_subcommand is None: | ||
console.start() | ||
|
||
|
||
@main.command( | ||
short_help="Run sardine with a background profiler (requires the yappi package)", | ||
help=""" | ||
This command starts the deterministic profiler, yappi, and measures statistics | ||
for both sardine and any functions written in the console. Once the REPL | ||
is closed, a pstats file will be written containing the session's stats. | ||
You can inspect the file's contents with Python's built-in pstats module | ||
or a third-party package like snakeviz. | ||
""", | ||
) | ||
@click.option( | ||
"-c", | ||
"--clock", | ||
default="wall", | ||
help="The clock type to use. Wall time includes time spent waiting, " | ||
"while CPU time ignores it.", | ||
show_default=True, | ||
type=click.Choice(("cpu", "wall"), case_sensitive=False), | ||
) | ||
@click.option( | ||
"-o", | ||
"filepath", | ||
default="stats.prof", | ||
help="The path to use when outputting the pstats file", | ||
show_default=True, | ||
type=click.Path(dir_okay=False, writable=True), | ||
) | ||
def profile(clock: str, filepath: str): | ||
profiler = Profiler(clock=clock, filepath=filepath) | ||
with profiler: | ||
console.start() | ||
|
||
|
||
if __name__ == "__main__": | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
|
||
repl_locals = {"asyncio": asyncio} | ||
for key in { | ||
"__name__", | ||
"__package__", | ||
"__loader__", | ||
"__spec__", | ||
"__builtins__", | ||
"__file__", | ||
}: | ||
repl_locals[key] = locals()[key] | ||
|
||
console = AsyncIOInteractiveConsole(repl_locals, loop) | ||
|
||
repl_future = None | ||
repl_future_interrupted = False | ||
|
||
try: | ||
import readline # NoQA | ||
except ImportError: | ||
pass | ||
|
||
repl_thread = REPLThread() | ||
repl_thread.daemon = True | ||
repl_thread.start() | ||
|
||
while True: | ||
try: | ||
loop.run_forever() | ||
except KeyboardInterrupt: | ||
if repl_future and not repl_future.done(): | ||
repl_future.cancel() | ||
repl_future_interrupted = True | ||
continue | ||
else: | ||
break | ||
main() |
Oops, something went wrong.