Skip to content

Instantly share code, notes, and snippets.

View mmerickel's full-sized avatar

Michael Merickel mmerickel

View GitHub Profile
from .tm_context import tm_context
with tm_context(request.tm):
...
@mmerickel
mmerickel / readme.md
Created September 1, 2019 14:12 — forked from frostbtn/readme.md
rocket.chat web-hook to post messages silently

This is a Rocket.Chat incoming web hook. Hook gets an array of "messages" and silently creates chat messages directly in the Rocket's database without disturbing users with notifications or alerts - messages just appear in the channels. Messages appear silently even if the user has the channel openned: no refresh or re-enter is required (this is not this script's feature, it's how Rocket works).

This script can post messages to channels and groups by name (if message destination set to #name), or by api roomId (no prefixes in destination). And it can post DM to a user (if destination is set to @username). Please note, in this case DM between message author and destination user must already be created.

This hook expects request.content: ISilentMessage[];

ISilentMessage {
  // Message body.
  text: string;
@mmerickel
mmerickel / commands.py
Last active August 23, 2019 20:22
subparse base cli
from subparse import command
@command('.shell')
def shell(parser):
""" Launch a python interpreter."""
@command('.run')
def run(parser):
""" Run a script.
import plaster
import sys
def main():
if '--reload' in sys.argv:
import hupper
reloader = hupper.start_reloader(__name__ + '.main')
reloader.watch_files(['site.ini'])
loader = plaster.get_loader('site.ini', protocols=['wsgi'])
from alembic import context
from pyramid.paster import setup_logging
from sqlalchemy import create_engine, pool
# 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.
from pyramid.decorator import reify
from .principals import Principals as P
class AccessToken:
def __init__(self, type, user=None, claims=None):
self.type = type
self.user = user
self.claims = claims or []
@mmerickel
mmerickel / .env
Last active August 30, 2018 01:16
DEBUG=yes
AUTH_SECRET=seekrit
AUTH_EXPIRES=3600
HTTPS_ONLY=no
WEB_CONCURRENCY=4
TRUSTED_PROXY=127.0.0.1
BIND_HOST=127.0.0.1
from nacl.bindings.crypto_secretstream import (
crypto_secretstream_xchacha20poly1305_ABYTES,
crypto_secretstream_xchacha20poly1305_HEADERBYTES,
crypto_secretstream_xchacha20poly1305_KEYBYTES,
crypto_secretstream_xchacha20poly1305_TAG_MESSAGE,
crypto_secretstream_xchacha20poly1305_TAG_FINAL,
crypto_secretstream_xchacha20poly1305_init_pull,
crypto_secretstream_xchacha20poly1305_init_push,
crypto_secretstream_xchacha20poly1305_pull,
crypto_secretstream_xchacha20poly1305_push,
@mmerickel
mmerickel / tm.py
Last active July 13, 2018 15:51
a context for helping with explicit transaction managers
from contextlib import contextmanager, suppress
from transaction.interfaces import NoTransaction
class DoomedAbort(Exception):
pass
@contextmanager
def tm_context(tm):
tm.begin()
try:
@mmerickel
mmerickel / basic_auth_example.py
Last active April 27, 2021 18:27
Basic Auth in Pyramid with simple ACLs
from pyramid.authentication import BasicAuthAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.config import Configurator
from pyramid.httpexceptions import HTTPForbidden
from pyramid.httpexceptions import HTTPUnauthorized
from pyramid.security import ALL_PERMISSIONS
from pyramid.security import Allow
from pyramid.security import Authenticated
from pyramid.security import forget
from pyramid.view import forbidden_view_config