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

Asyncio & schema V2 #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 1 addition & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
FROM python:3.7.2
FROM python:3.8

WORKDIR /opt
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

# python:3.7.2 only has sqlite 3.26, temporarily compiling 3.27 instead

RUN curl -O "https://www.sqlite.org/2019/sqlite-amalgamation-3270000.zip"
RUN unzip -p sqlite-amalgamation-3270000.zip sqlite-amalgamation-3270000/sqlite3.c | gcc \
-DSQLITE_THREADSAFE=1 -lpthread \
-DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 \
-DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_RTREE \
-ldl -shared -fPIC -o libsqlite3.so.0 -xc -

RUN mv libsqlite3.so.0 /usr/lib/x86_64-linux-gnu/libsqlite3.so.0

COPY markovich markovich

CMD ["python3", "-m", "markovich", "/opt/config.json"]
Expand Down
19 changes: 8 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
version: "3"
services:
markovich:
markovich-irc:
build: .
volumes:
- /home/user/git/markovich/config.json:/opt/config.json
- /home/user/git/markovich/config-irc.json:/opt/config.json
- /home/user/git/markovich/db:/opt/db
ports:
- 6697:6697
- 6667:6667
#networks:
# - znc-network
restart: unless-stopped

# docker network create znc-network
networks:
znc-network:
external: true
markovich-discord:
build: .
volumes:
- /home/user/git/markovich/config-discord.json:/opt/config.json
- /home/user/git/markovich/db:/opt/db
restart: unless-stopped
37 changes: 16 additions & 21 deletions markovich/__main__.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import sys, os
import json
from typing import List, Callable
from asyncio import Future, get_event_loop
import logging

from .markovich_discord import run_markovich_discord
from .markovich_irc import run_markovich_irc
from .markovich_cli import run_markovich_cli

def run_with_config(config):
cleanup_functions = [] #type: List[Callable[[], Future]]
aio_loop = get_event_loop()

try:
if 'irc' in config:
cleanup_fn = run_markovich_irc(config['irc'], eventloop=aio_loop)
#cleanup_functions.append(cleanup_fn)

if 'discord' in config:
cleanup_fn = run_markovich_discord(config['discord'], eventloop=aio_loop)
cleanup_functions.append(cleanup_fn)
if 'irc' in config and 'discord' in config:
logging.error("Cannot run in both Discord and IRC mode at the same time.")
sys.exit(1)

if 'irc' in config:
run_markovich_irc(config['irc'])
elif 'discord' in config:
run_markovich_discord(config['discord'])
else:
logging.error("No known configurations in the specified config file.")
sys.exit(1)

aio_loop.run_forever()
except KeyboardInterrupt:
print("Shutting down")
finally:
for cleanup in cleanup_functions:
aio_loop.run_until_complete(cleanup())
def run_without_config():
run_markovich_cli()

if len(sys.argv) > 1:
config_path = sys.argv[1]
Expand All @@ -35,5 +30,5 @@ def run_with_config(config):

run_with_config(config)
else:
print("Called with no configuration file, launching in test mode")
run_markovich_cli()
logging.warning("Called with no configuration file, launching in test mode")
run_without_config()
26 changes: 13 additions & 13 deletions markovich/backends/backend_manager.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
from .markov_backend_sqlite import MarkovBackendSQLite, sqlite_db_directory
from .markov_backend_sqlite import open_markov_backend_sqlite, sqlite_db_directory
from .markov_backend import MarkovBackend
from typing import Dict
from contextlib import asynccontextmanager
from typing import Dict, AsyncIterator

class MarkovManager:
open_backends: Dict[str, MarkovBackend]

def __init__(self, **kwargs):
self.open_backends = {} # type: Dict[str, MarkovBackend]
self.open_backends = {}

# Recieves a list of initial mappings, the rest will be created dynamically
for k,v in kwargs:
if type(v) is MarkovBackendSQLite:
open_backends[k] = v

def get_markov(self, context_id: str) -> MarkovBackend:
backend = self.open_backends.get(context_id)

if backend is None:
db_path = sqlite_db_directory / context_id
db_path = db_path.with_suffix('.db')
print("Opening database \"{}\" ({})".format(context_id, db_path))
@asynccontextmanager
async def get_markov(self, context_id: str) -> AsyncIterator[MarkovBackend]:
# In the case of SQLite, connections are opened and closed each time, no pooling

backend = MarkovBackendSQLite(db_path)
self.open_backends[context_id] = backend
db_path = sqlite_db_directory / context_id
db_path = db_path.with_suffix('.db')

return backend
async with open_markov_backend_sqlite(db_path) as backend:
yield backend
15 changes: 12 additions & 3 deletions markovich/backends/markov_backend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from typing import Optional, Pattern
from typing import Optional, Pattern, List

class MarkovBackend:
def record_and_generate(self, input_string:str, split_pattern: Pattern, word_limit: int) -> Optional[str]:
raise NotImplementedError
async def record_and_generate(self, input_string:str, split_pattern: Pattern, word_limit: int) -> Optional[str]:
raise NotImplementedError

async def record_words(self, chopped_string:List[str]):
pass

async def generate_sentence(self, starting_word:str, word_limit: int):
pass

# def bulk_learn(self, sentences):
# pass
Loading