Skip to content

Commit

Permalink
Add initial package commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Sawyers committed Dec 25, 2018
0 parents commit 259c69a
Show file tree
Hide file tree
Showing 23 changed files with 765 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Created by .ignore support plugin (hsz.mobi)
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM jfloff/alpine-python
LABEL Description="Docker container for standing up a pyramid web app to bridge Smartthings events to a rethinkDB" Maintainer="Andrew Sawyers <[email protected]>"
SHELL ["/bin/bash", "-c"]

# We copy this file first to leverage docker cache
COPY src /app/

WORKDIR /app

RUN pip install -U pip && \
pip install -r requirements.txt && \
python setup.py install

ENTRYPOINT [ "pserve" ]

CMD [ "/app/production.ini" ]
24 changes: 24 additions & 0 deletions etc/pyre.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package: pyramidbridge
environment: dev
handlers:
date:
arrow_format: "YYYY/MM/DD"
default_format: "YYYY/MM/DD"
query_format: "YYYY-MM-DD"
datetime:
arrow_format: "YYYY/MM/DD HH:mm:ss"
default_format: "%%Y/%%m/%%d %%H:%%M:%%S"
logging:
name: pyramidbridge
level: DEBUG
rethinkdb:
host: rethinkdb
port: 28015
smartthings:
db: smartthings
events:
table: events
subscribe:
table: subscriptions
user: pyre
password: f1r3st0rm
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
testpaths = pyramidbridge
python_files = *.py
9 changes: 9 additions & 0 deletions src/CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CHANGES

0.2.0
----
- add subscribe route for supporting sending messages back to smartthings

0.1.0
----
- initial pyramid app for briding smartthigns events to the rethinkDB
2 changes: 2 additions & 0 deletions src/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include *.txt *.ini *.cfg *.rst *.yml
recursive-include pyramidbridge *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 *.yml
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A Smartthings event messaging bridge to RethinkDB
60 changes: 60 additions & 0 deletions src/development.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
###
# app configuration
# https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/environment.html
###

[app:main]
use = egg:pyramidbridge

pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes =
pyramid_chameleon
pyramid_debugtoolbar

# By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'.
# debugtoolbar.hosts = 127.0.0.1 ::1

###
# wsgi server configuration
###

[server:main]
use = egg:waitress#main
listen = 0.0.0.0:6543

###
# logging configuration
# https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/logging.html
###

[loggers]
keys = root, pyramidbridge

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console

[logger_pyramidbridge]
level = DEBUG
handlers =
qualname = pyramidbridge

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
53 changes: 53 additions & 0 deletions src/production.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
###
# app configuration
# https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/environment.html
###

[app:main]
use = egg:pyramidbridge

pyramid.reload_templates = false
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en

###
# wsgi server configuration
###

[server:main]
use = egg:waitress#main
listen = *:6543

###
# logging configuration
# https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/logging.html
###

[loggers]
keys = root, pyramidbridge

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console

[logger_pyramidbridge]
level = INFO
handlers = console
qualname = pyramidbridge

[handler_console]
class = StreamHandler
args = (sys.stdout,)
level = DEBUG
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
14 changes: 14 additions & 0 deletions src/pyramidpridge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pyramid.config import Configurator


def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.include('pyramid_chameleon')

config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('smartthings_add_event', '/push')
config.add_route('smartthings_subscribe', '/smartthings/subscribe')
config.scan()
return config.make_wsgi_app()
113 changes: 113 additions & 0 deletions src/pyramidpridge/connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""rethinkDB connection"""
import arrow
import rethinkdb as r


class Connection(object):
"""RethinkDB Connection handler"""
__connection__ = None
__db__ = None
__host__ = None
__port__ = None
__user__ = None
__password__ = None
__session__ = None
__table__ = None

def __init__(self,
host,
port,
user,
password,
db,
**kw):
self.__db__ = db
self.__host__ = host
self.__password__ = password
self.__port__ = port
self.__user__ = user
self.__table__ = kw.get('table')
self._setup_connection(**kw)

def _setup_connection(self, **kw):
if self.__connection__ is None:
self.__connection__ = r.connect(host=self.host,
port=self.port,
user=self.user,
password=self.password,
db=self.db,
**kw)

@property
def db(self):
return self.__db__

@property
def host(self):
return self.__host__

@property
def password(self):
return self.__password__

@property
def port(self):
return self.__port__

@property
def session(self):
"""get the connection to the database"""
if self.__connection__ == None:
self._setup_connection()

return self.__connection__

@property
def table(self):
return self.__table__

@property
def user(self):
return self.__user__

def close(self):
if self.__session__:
self.session.close()

def remove(self):
"""close and remove the connection"""
self.close()
self.__connection__ = None

def commit(self):
"""call run on the db"""
return

def open(self):
"""
close the connection if it is open
reopen a new connection to the DB"""
self.session.reconnect()

@property
def connect(self):
"""make the connection; create a connection if it does not
exist.
"""
self._setup_connection()

@property
def created(self):
return r.now()

modified = created

@property
def model(self):
"""create a model of the record to store in the database
"""
return self.__model__

@property
def r(self):
return r
24 changes: 24 additions & 0 deletions src/pyramidpridge/etc/pyre.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package: pyramidbridge
environment: dev
handlers:
date:
arrow_format: "YYYY/MM/DD"
default_format: "YYYY/MM/DD"
query_format: "YYYY-MM-DD"
datetime:
arrow_format: "YYYY/MM/DD HH:mm:ss"
default_format: "%%Y/%%m/%%d %%H:%%M:%%S"
logging:
name: pyramidbridge
level: INFO
rethinkdb:
host: rethinkdb
port: 28015
smartthings:
db: smartthings
events:
table: events
subscribe:
table: subscriptions
user: smartthings
password: sm@rt!3s
Binary file added src/pyramidpridge/static/pyramid-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/pyramidpridge/static/pyramid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 259c69a

Please sign in to comment.