commit 087aa39918a147ad9df7de35e6484ccb3efdc6c9 Author: timotheyca Date: Sun Dec 19 20:09:47 2021 +0300 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0b96173 --- /dev/null +++ b/.gitignore @@ -0,0 +1,225 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + + + + + + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + + + + +# Other +/dev.py +/*.db diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..69f4ba6 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,51 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7a9ef39 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e15dbcb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/v6d2ctx.iml b/.idea/v6d2ctx.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/.idea/v6d2ctx.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..81ead4e --- /dev/null +++ b/setup.py @@ -0,0 +1,12 @@ +from setuptools import setup + +setup( + name='v6d2ctx', + version='', + packages=['v6d2ctx'], + url='', + license='', + author='PARRRATE T&V', + author_email='', + description='' +) diff --git a/v6d2ctx/__init__.py b/v6d2ctx/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/v6d2ctx/context.py b/v6d2ctx/context.py new file mode 100644 index 0000000..ed501fe --- /dev/null +++ b/v6d2ctx/context.py @@ -0,0 +1,102 @@ +import asyncio +import time +from io import StringIO +from typing import Union, Optional, Callable, Awaitable + +# noinspection PyPackageRequirements +import discord + +usertype = Union[discord.abc.User, discord.user.BaseUser, discord.Member, discord.User] + + +class Context: + def __init__(self, message: discord.Message): + self.message: discord.Message = message + self.channel: discord.abc.Messageable = message.channel + self.dm_or_text: Union[discord.DMChannel, discord.TextChannel] = message.channel + self.author: usertype = message.author + self.content: str = message.content + self.member: Optional[discord.Member] = message.author if isinstance(message.author, discord.Member) else None + self.guild: Optional[discord.Guild] = None if self.member is None else self.member.guild + + async def reply(self, content=None, **kwargs) -> discord.Message: + return await self.message.reply(content, mention_author=False, **kwargs) + + async def long(self, s: str): + resio = StringIO(s) + res = '' + for line in resio: + if len(res) + len(line) < 2000: + res += line + else: + await self.reply(res) + res = line + if res: + await self.reply(res) + + +ESCAPED = '`_*\'"\\' + + +def escape(s: str): + res = StringIO() + for c in s: + if c in ESCAPED: + c = '\\' + c + res.write(c) + return res.getvalue() + + +buckets: dict[str, dict[str, Callable[[Context, list[str]], Awaitable[None]]]] = {} + + +def at(bucket: str, name: str): + def wrap(f: Callable[[Context, list[str]], Awaitable[None]]): + buckets.setdefault(bucket, {})[name] = f + + return f + + return wrap + + +class Explicit(Exception): + def __init__(self, msg: str): + self.msg = msg + + +class Implicit(Exception): + pass + + +def of(bucket: str, name: str) -> Callable[[Context, list[str]], Awaitable[None]]: + try: + return buckets[bucket][name] + except KeyError: + raise Implicit + + +benchmarks: dict[str, dict[str, float]] = {} +_t = time.perf_counter() + + +class Benchmark: + def __init__(self, benchmark: str): + self.benchmark = benchmark + + def __enter__(self): + self.t = time.perf_counter() + + def __exit__(self, exc_type, exc_val, exc_tb): + d = (time.perf_counter() - self.t) + benchmarks.setdefault(self.benchmark, {'integral': 0.0, 'max': 0.0}) + benchmarks[self.benchmark]['integral'] += d + benchmarks[self.benchmark]['max'] = max(benchmarks[self.benchmark]['max'], d) + + +async def monitor(): + while True: + await asyncio.sleep(10) + dt = time.perf_counter() - _t + print('Benchmarks:') + for benchmark, metrics in benchmarks.items(): + print(benchmark, '=', metrics['integral'] / max(dt, .00001), ':', metrics['max'])