diff options
author | Cody Hiar <cody@hiar.ca> | 2021-02-03 14:29:24 -0700 |
---|---|---|
committer | Cody Hiar <cody@hiar.ca> | 2021-02-03 14:29:24 -0700 |
commit | 2b007a5a500b635ddcfe4b9a512777f3d21fa6b6 (patch) | |
tree | 7a9ccb2d41222fc571c6d08b169d4946e26c130c |
Initial commit of project
-rw-r--r-- | .gitignore | 13 | ||||
-rw-r--r-- | Makefile | 19 | ||||
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | alembic.ini | 85 | ||||
-rw-r--r-- | alembic/README | 1 | ||||
-rw-r--r-- | alembic/env.py | 78 | ||||
-rw-r--r-- | alembic/script.py.mako | 24 | ||||
-rw-r--r-- | alembic/versions/a773ce65a262_create_the_initial_db.py | 28 | ||||
-rw-r--r-- | docker-compose.yml | 19 | ||||
-rw-r--r-- | docker/Dockerfile | 47 | ||||
-rw-r--r-- | main.py | 41 | ||||
-rw-r--r-- | mappings.py | 40 | ||||
-rw-r--r-- | requirements.txt | 6 |
13 files changed, 408 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d83040b --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +.*.swp +*.pyc +*.pyo +.DS_Store +tags +.ropeproject +*.actual +.vimcache +.idea +.mypy_cache +.envrc +*.sqlite +results.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..aa51099 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +.PHONY: build + +help: + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +build: ## Build the Docker image + docker-compose -p slacky build + +up: build ## Bring the container up + docker-compose -p slacky up -d + +down: ## Stop the container + docker-compose -p slacky stop + +enter: ## Enter the running container + docker-compose -p slacky exec backend /bin/bash + +clean: down ## Remove stoped containers + docker-compose -p slacky rm -f diff --git a/README.md b/README.md new file mode 100644 index 0000000..e70f362 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ + +# Generating Migrations + +``` +alembic revision --autogenerate --message "Create the example Database" +``` + diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 0000000..7c18982 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,85 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = alembic + +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# timezone to use when rendering the date +# within the migration file as well as the filename. +# string value is passed to dateutil.tz.gettz() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; this defaults +# to alembic/versions. When using multiple version +# directories, initial revisions must be specified with --version-path +# version_locations = %(here)s/bar %(here)s/bat alembic/versions + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = sqlite:///db.sqlite + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks=black +# black.type=console_scripts +# black.entrypoint=black +# black.options=-l 79 + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/alembic/README b/alembic/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration.
\ No newline at end of file diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 0000000..bda27cf --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,78 @@ +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool +from mappings import Base + +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/alembic/script.py.mako b/alembic/script.py.mako new file mode 100644 index 0000000..2c01563 --- /dev/null +++ b/alembic/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/alembic/versions/a773ce65a262_create_the_initial_db.py b/alembic/versions/a773ce65a262_create_the_initial_db.py new file mode 100644 index 0000000..fad15d3 --- /dev/null +++ b/alembic/versions/a773ce65a262_create_the_initial_db.py @@ -0,0 +1,28 @@ +"""Create the initial db + +Revision ID: a773ce65a262 +Revises: +Create Date: 2021-02-03 21:24:54.897514 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a773ce65a262' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..29ff964 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3' +services: + backend: + build: + context: ./ + dockerfile: ./docker/Dockerfile + network_mode: "bridge" + container_name: slacky + image: thornycrackers/slacky + ports: + - "8000" + volumes: + - .:/usr/src/app + command: /bin/bash + environment: + - "PYTHONPATH=/usr/src/app" + - "SLACK_TOKEN=${SLACK_TOKEN}" + tty: true + stdin_open: true diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..dedd538 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,47 @@ +#################### +# Dependancy Builder +#################### +FROM python:3.7-stretch AS builder + +# Set working directory +WORKDIR /usr/src/app + +# Turns off writing .pyc files; superfluous on an ephemeral container. +ENV PYTHONDONTWRITEBYTECODE=1 +# Seems to speed things up +ENV PYTHONUNBUFFERED=1 + +# Use the executables from the virtualenv instead of globals +ENV PATH="/opt/venv/bin:$PATH" + +# Setup virtualenv to build dependancies into +RUN python -m venv /opt/venv + +# Install python dependancies +COPY requirements.txt ./requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +##################### +# Condensed App Image +##################### + +# Smaller official Debian-based Python image +FROM python:3.7-slim-stretch AS app + +# Set working directory +WORKDIR /usr/src/app + +# Extra python env +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 +ENV PIP_DISABLE_PIP_VERSION_CHECK=1 +ENV PATH="/opt/venv/bin:$PATH" + +# Setup the virtualenv +RUN python -m venv /opt/venv + +# Copy in tools and python environment +COPY --from=builder /opt/venv /opt/venv + +# Copy in the rest of the app +COPY ./ /usr/src/app @@ -0,0 +1,41 @@ +"""Slack stuff.""" +import json +import os + +from slack_sdk import WebClient + +RESULTS_FILE = "results.json" + + +def get_slack_client(): + """Initialize the slack client.""" + token = os.environ["SLACK_TOKEN"] + return WebClient(token=token) + + +def write_results(data): + """Write the results to a file.""" + with open(RESULTS_FILE, "w") as f: + json.dump(data, f) + + +def load_results(): + """Load the recent search results.""" + with open(RESULTS_FILE, "r") as f: + return json.load(f) + + +def fetch_results(): + """Query slack for the latest results.""" + c = get_slack_client() + r = c.search_messages(query="@cody", sort="timestamp") + + # Write the results if + if r.data["ok"]: + write_results(r.data) + print("Search complete.") + else: + print("Failed to search.") + + +def write_to_database diff --git a/mappings.py b/mappings.py new file mode 100644 index 0000000..e80756f --- /dev/null +++ b/mappings.py @@ -0,0 +1,40 @@ +"""Database for project.""" +from sqlalchemy import Column, Integer, String, create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +DB_FILE = "sqlite:///db.sqlite" +engine = create_engine(DB_FILE, echo=True) +Base = declarative_base() + + +class Message(Base): + """Message from slack.""" + + __tablename__ = "message" + + id = Column(Integer, primary_key=True) + iid = Column(String) + type = Column(String) + username = Column(String) + text = Column(String) + + # ---------------------------------------------------------------------- + def __init__(self, iid, type, username, text): + """Create record.""" + self.iid = iid + self.type = type + self.username = username + self.text = text + + +def create_tables(): + """Create tables.""" + Base.metadata.create_all(engine) + + +def get_session(): + """Start a session.""" + engine = create_engine(DB_FILE, echo=True) + Session = sessionmaker(bind=engine) + return Session() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..048269e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +ipython==7.19.0 +parso==0.7.1 +jedi==0.17.2 +slack-sdk==3.2.0 +SQLAlchemy==1.3.23 +alembic==1.5.4 |