Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] envoy.code.check: experimental #397

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions aio.api.aspell/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

pytooling_package("aio.api.aspell")
5 changes: 5 additions & 0 deletions aio.api.aspell/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

aio.api.aspell
==============

Wrapper around aspell
1 change: 1 addition & 0 deletions aio.api.aspell/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.5-dev
16 changes: 16 additions & 0 deletions aio.api.aspell/aio/api/aspell/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

pytooling_library(
"aio.api.aspell",
dependencies=[
"//deps:abstracts",
"//deps:aio.core",
],
sources=[
"abstract/__init__.py",
"abstract/api.py",
"__init__.py",
"api.py",
"exceptions.py",
"utils.py",
],
)
16 changes: 16 additions & 0 deletions aio.api.aspell/aio/api/aspell/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""aio.api.aspell."""

from . import abstract
from . import exceptions
from . import utils
from .api import (
AspellAPI, )
from .abstract import (
AAspellAPI, )


__all__ = (
"abstract",
"AAspellAPI",
"AspellAPI",
"exceptions", )
6 changes: 6 additions & 0 deletions aio.api.aspell/aio/api/aspell/abstract/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

from .api import AAspellAPI


__all__ = (
"AAspellAPI", )
139 changes: 139 additions & 0 deletions aio.api.aspell/aio/api/aspell/abstract/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@

import asyncio
import abc
import shutil
from functools import cached_property
from typing import Any, Type

import abstracts

from aio.core.functional import async_property
from aio.core.tasks import concurrent
from aio.core.interactive import Interactive


class AspellPipe:
handlers = []

def __init__(self, in_q, out_q):
self.in_q = in_q
self.out_q = out_q

@async_property(cache=True)
async def proc(self):
return await asyncio.create_subprocess_exec(
"/home/phlax/.virtualenvs/envoydev/pytooling/echo.py",
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)

async def listen(self):
print ("STARTING LISTENER")
while True:
msg = await self.in_q.get()
print(f"IN Q MESSAGE RECVD: {msg}")
self.in_q.task_done()
print(f"Sending message to process: {msg}")
result = await self.write(msg)
await self.out_q.put(result)

async def start(self):
print ("STARTING ASPELL")
self.task = asyncio.create_task(self.listen())
return (await self.write(b""))[0]

async def stop(self):
print ("STOPPING ASPELL")

async def write(self, message):
print(f"WRITE: {message}")
proc = await self.proc
stdout, stderr = await proc.communicate(message)
other = await proc.stdout.read()
more = await proc.communicate(message)
return stdout, stderr


class MultiPipe:

def __init__(self, pipe_type):
self.pipe_type = pipe_type

@cached_property
def in_q(self):
return asyncio.Queue()

@cached_property
def out_q(self):
return asyncio.Queue()

@async_property(cache=True)
async def pipes(self):
aspell_pipe = self.pipe_type(self.in_q, self.out_q)
print(f"Started aspell pipe: {await aspell_pipe.start()}")
return aspell_pipe

async def write(self, message):
print(f"PUTTING MESSAGE: {message}")
print(f"IN Q: {self.in_q}")
await self.in_q.put(message)
print(f"DONE")
while True:
stdout, stderr = await self.out_q.get()
self.out_q.task_done()
if stdout or stderr:
return stdout, stderr


class AAspellAPI(metaclass=abstracts.Abstraction):
"""Aspell API wrapper.
"""

def __init__(self, *args, **kwargs) -> None:
self.args = args
self.kwargs = kwargs

@cached_property
def aspell_command(self):
command = shutil.which("aspell")
return f"{command} -a"

@async_property(cache=True)
async def session(self):
session = Interactive(self.aspell_command, 1)
await session.start()
return session

async def compile_dictionary(self, path):
# breakpoint()
pass

@async_property(cache=True)
async def pipe(self):
aspell_pipe = MultiPipe(AspellPipe)
await aspell_pipe.pipes
# await aspell_pipe.start()
return aspell_pipe

async def listener(self):
print(f"MESSAGE RCVD: {stdout} {stderr}")

async def spellcheck(self, message):
pipe = await self.pipe
return await pipe.write(message)

async def start(self):
await self.session

async def compile_dictionary(self, dictionary):
words = ["asdfasfdafds", "cabbage"]
session = await self.session
for word in words:
response = await session(f"{word}\n".encode("utf-8"))
if str(response[0]).strip() == "*":
print(f"{word} is a good word")
else:
print(f"{word} is a bad word")

async def stop(self):
pass
13 changes: 13 additions & 0 deletions aio.api.aspell/aio/api/aspell/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from typing import Type

import abstracts

from .abstract import AAspellAPI


@abstracts.implementer(AAspellAPI)
class AspellAPI:

def __init__(self, *args, **kwargs) -> None:
AAspellAPI.__init__(self, *args, **kwargs)
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions aio.api.aspell/aio/api/aspell/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

from datetime import datetime


# these only deal with utc but are good enough for working with the
# github api


def dt_from_js_isoformat(iso: str) -> datetime:
return datetime.fromisoformat(iso.replace("Z", "+00:00"))


def dt_to_js_isoformat(dt: datetime) -> str:
return dt.isoformat().replace("+00:00", "Z")
48 changes: 48 additions & 0 deletions aio.api.aspell/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[metadata]
name = aio.api.aspell
version = file: VERSION
author = Ryan Northey
author_email = [email protected]
maintainer = Ryan Northey
maintainer_email = [email protected]
license = Apache Software License 2.0
url = https://github.com/envoyproxy/pytooling/tree/main/aio.api.aspell
description = Wrapper around aspell
long_description = file: README.rst
classifiers =
Development Status :: 4 - Beta
Framework :: Pytest
Intended Audience :: Developers
Topic :: Software Development :: Testing
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: Implementation :: CPython
Operating System :: OS Independent
License :: OSI Approved :: Apache Software License

[options]
python_requires = >=3.5
py_modules = aio.api.aspell
packages = find_namespace:
install_requires =
abstracts>=0.0.12
aio.core>=0.3.0
gidgethub
packaging

[options.extras_require]
test =
pytest
pytest-asyncio
pytest-coverage
pytest-patches
lint = flake8
types =
mypy
publish = wheel

[options.package_data]
* = py.typed
5 changes: 5 additions & 0 deletions aio.api.aspell/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python

from setuptools import setup # type:ignore

setup()
12 changes: 12 additions & 0 deletions aio.api.aspell/tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

pytooling_tests(
"aio.api.github",
dependencies=[
"//deps:abstracts",
"//deps:aio.core",
"//deps:aiohttp",
"//deps:gidgethub",
"//deps:packaging",
"//deps:pytest-asyncio",
],
)
4 changes: 4 additions & 0 deletions aio.core/aio/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pytooling_library(
"functional/utils.py",
"log/__init__.py",
"log/logging.py",
"interactive/abstract/__init__.py",
"interactive/abstract/interactive.py",
"interactive/exceptions.py",
"interactive/interactive.py",
"output/abstract/__init__.py",
"output/abstract/output.py",
"output/exceptions.py",
Expand Down
2 changes: 2 additions & 0 deletions aio.core/aio/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
directory,
event,
functional,
interactive,
output,
stream,
subprocess,
Expand All @@ -15,6 +16,7 @@
"directory",
"event",
"functional",
"interactive",
"output",
"stream",
"subprocess",
Expand Down
14 changes: 14 additions & 0 deletions aio.core/aio/core/interactive/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


from .abstract import AInteractive, APrompt
from .interactive import Interactive, interactive, Prompt
from . import exceptions


__all__ = (
"AInteractive",
"APrompt",
"exceptions",
"interactive",
"Interactive",
"Prompt")
7 changes: 7 additions & 0 deletions aio.core/aio/core/interactive/abstract/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

from .interactive import AInteractive, APrompt


__all__ = (
"AInteractive",
"APrompt")
Loading