<commit_before>"""Flask SQLAlchemy ORM models for Social Auth"""
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.schema import UniqueConstraint

from social.utils import setting_name, module_member
from social.storage.sqlalchemy_orm import SQLAlchemyUserMixin, \
                                          SQLAlchemyAssociationMixin, \
                                          SQLAlchemyNonceMixin, \
                                          BaseSQLAlchemyStorage
from social.apps.flask_app.fields import JSONType

class FlaskStorage(BaseSQLAlchemyStorage):
    user = None
    nonce = None
    association = None

def init_social(app, Base):
    UID_LENGTH = app.config.get(setting_name('UID_LENGTH'), 255)
    User = module_member(app.config[setting_name('USER_MODEL')])

    class UserSocialAuth(Base, SQLAlchemyUserMixin):
        """Social Auth association model"""
        __tablename__ = 'social_auth_usersocialauth'
        __table_args__ = (UniqueConstraint('provider', 'uid'),)
        id = Column(Integer, primary_key=True)
        provider = Column(String(32))
        uid = Column(String(UID_LENGTH))
        extra_data = Column(JSONType)
        user_id = Column(Integer, ForeignKey(User.id),
                         nullable=False, index=True)
        user = relationship(User, backref='social_auth')

        @classmethod
        def username_max_length(cls):
            return User.__table__.columns.get('username').type.length

        @classmethod
        def user_model(cls):
            return User

        @classmethod
        def _session(cls):
            return cls.query.session

        @classmethod
        def _query(cls):
            return cls.query

    class Nonce(Base, SQLAlchemyNonceMixin):
        """One use numbers"""
        __tablename__ = 'social_auth_nonce'
        __table_args__ = (UniqueConstraint('server_url', 'timestamp', 'salt'),)
        id = Column(Integer, primary_key=True)
        server_url = Column(String(255))
        timestamp = Column(Integer)
        salt = Column(String(40))

        @classmethod
        def _session(cls):
            return cls.query.session

        @classmethod
        def _query(cls):
            return cls.query

    class Association(Base, SQLAlchemyAssociationMixin):
        """OpenId account association"""
        __tablename__ = 'social_auth_association'
        __table_args__ = (UniqueConstraint('server_url', 'handle'),)
        id = Column(Integer, primary_key=True)
        server_url = Column(String(255))
        handle = Column(String(255))
        secret = Column(String(255))  # base64 encoded
        issued = Column(Integer)
        lifetime = Column(Integer)
        assoc_type = Column(String(64))

        @classmethod
        def _session(cls):
            return cls.query.session

        @classmethod
        def _query(cls):
            return cls.query

    # Set the references in the storage class
    FlaskStorage.user = UserSocialAuth
    FlaskStorage.nonce = Nonce
    FlaskStorage.association = Association
<commit_msg>Enforce db session passing on flask init<commit_after>"""Flask SQLAlchemy ORM models for Social Auth"""
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.schema import UniqueConstraint

from social.utils import setting_name, module_member
from social.storage.sqlalchemy_orm import SQLAlchemyUserMixin, \
                                          SQLAlchemyAssociationMixin, \
                                          SQLAlchemyNonceMixin, \
                                          BaseSQLAlchemyStorage
from social.apps.flask_app.fields import JSONType

class FlaskStorage(BaseSQLAlchemyStorage):
    user = None
    nonce = None
    association = None

def init_social(app, Base, session):
    UID_LENGTH = app.config.get(setting_name('UID_LENGTH'), 255)
    User = module_member(app.config[setting_name('USER_MODEL')])
    app_session = session

    class _AppSession(object):
        @classmethod
        def _session(cls):
            return app_session

    class UserSocialAuth(Base, SQLAlchemyUserMixin, _AppSession):
        """Social Auth association model"""
        __tablename__ = 'social_auth_usersocialauth'
        __table_args__ = (UniqueConstraint('provider', 'uid'),)
        id = Column(Integer, primary_key=True)
        provider = Column(String(32))
        uid = Column(String(UID_LENGTH))
        extra_data = Column(JSONType)
        user_id = Column(Integer, ForeignKey(User.id),
                         nullable=False, index=True)
        user = relationship(User, backref='social_auth')

        @classmethod
        def username_max_length(cls):
            return User.__table__.columns.get('username').type.length

        @classmethod
        def user_model(cls):
            return User

    class Nonce(Base, SQLAlchemyNonceMixin, _AppSession):
        """One use numbers"""
        __tablename__ = 'social_auth_nonce'
        __table_args__ = (UniqueConstraint('server_url', 'timestamp', 'salt'),)
        id = Column(Integer, primary_key=True)
        server_url = Column(String(255))
        timestamp = Column(Integer)
        salt = Column(String(40))

    class Association(Base, SQLAlchemyAssociationMixin, _AppSession):
        """OpenId account association"""
        __tablename__ = 'social_auth_association'
        __table_args__ = (UniqueConstraint('server_url', 'handle'),)
        id = Column(Integer, primary_key=True)
        server_url = Column(String(255))
        handle = Column(String(255))
        secret = Column(String(255))  # base64 encoded
        issued = Column(Integer)
        lifetime = Column(Integer)
        assoc_type = Column(String(64))

    # Set the references in the storage class
    FlaskStorage.user = UserSocialAuth
    FlaskStorage.nonce = Nonce
    FlaskStorage.association = Association
<|endoftext|>


<commit_before>import sys

from homely.general import (WHERE_TOP, blockinfile, mkdir, run, section,
                            writefile)
from homely.install import InstallFromSource, installpkg
from homely.system import haveexecutable
from homely.ui import warn, yesno

from HOMELY import (HERE, HOME, lineinfile, memoize, mypipinstall,
                    powerline_path, wantfull, wantpowerline)

@memoize
def _wanttmux():
    return yesno('install_tmux', 'Install tmux?', wantfull())

@section
def tmux_config():
    if not _wanttmux():
        return

    tmux_plugins = yesno('install_tmux_plugins',
                         'Install TPM and use tmux plugins?',
                         wantfull())

    if tmux_plugins:
        mkdir('~/.tmux')
        mkdir('~/.tmux/plugins')
        tpm = InstallFromSource('https://github.com/tmux-plugins/tpm',
                                '~/.tmux/plugins/tpm')
        tpm.select_branch('master')
        run(tpm)

    # what to put in tmux config?
    wildcards = {"DOTFILES": HERE, "HOME": HOME}
    lines = []
    # add our dotfiles python folder to PYTHONPATH before restarting the powerline daemon
    #lines.append("set-environment PYTHONPATH '%(DOTFILES)s/python'")
    if wantpowerline():
        wildcards["POWERLINE"] = powerline_path()
        lines.extend([
            # always replace the daemon on startup, so that re-sourcing the
            # tmux conf always loads updated python modules
            'run-shell "powerline-daemon --replace -q"',
            'source "%(POWERLINE)s/bindings/tmux/powerline.conf"',
        ])
    if tmux_plugins:
        lines.append('source "%(DOTFILES)s/tmux/plugins.conf"')
        # FIXME: the only way to determine if TPM installed correctly is to
        # press `[PREFIX]`, `I` to do a plugin install
    lines.append('source "%(DOTFILES)s/tmux/tmux.conf"')
    lines = [l % wildcards for l in lines]
    blockinfile('~/.tmux.conf',
                lines,
                '# start of automated tmux config',
                '# end of automated tmux config',
                where=WHERE_TOP)

# install or compile tmux
@section
def tmux_install():
    if not _wanttmux():
        return

    if yesno('own_tmux', 'Compile tmux from source?', None):
        # FIXME: compiling tmux from source like this requires
        # - libevent (libevent-dev on Ubuntu)
        # - curses (ncurses-dev on Ubuntu)
        # how do we make sure that that library has been installed?
        tmux = InstallFromSource('https://github.com/tmux/tmux.git',
                                 '~/src/tmux.git')
        tmux.select_tag('2.7')
        tmux.compile_cmd([
            ['make', 'distclean'],
            ['sh', 'autogen.sh'],
            ['./configure'],
            ['make'],
        ])
        tmux.symlink('tmux', '~/bin/tmux')
        tmux.symlink('tmux.1', '~/man/man1/tmux.1')
        run(tmux)
    else:
        try:
            # install tmux using brew or apt-get ...
            installpkg('tmux')
        except Exception:
            print("-" * 50)
            print("Compiling `tmux` failed - do you need to install automake or gcc?")  # noqa
            print("-" * 50)
            raise

class TmuxCustomMode(object):
    _all = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
            ',<.>/?:[{]}-_=+!@%^&*()`')
    # TODO: what about #'"~;
    _special = ['Enter', 'Left', 'Right', 'Up', 'Down', 'Space']

    def __init__(self, table, prompt):
        super(TmuxCustomMode, self).__init__()
        self._table = table
        self._prompt = prompt
        self._rootkeys = []
        self._prefixkeys = []
        self._bindings = []

    def rootkeybind(self, key):
        self._rootkeys.append(key)

    def prefixkeybind(self, key):
        self._prefixkeys.append(key)

    def bindkey(self, key, command):
        assert key != 'Escape'
        self._bindings.append((key, command))

    def getlines(self):
        safeprompt = self._prompt.replace('\\', '\\\\').replace('"', '\\"')
        seen = set()

        for key in self._rootkeys:
            seen.add(key)
            line = 'bind-key -n {key} switch-client -T {table} \; display-message "{safeprompt}"'
            yield line.format(table=self._table, key=key, safeprompt=safeprompt)

        for key in self._prefixkeys:
            line = 'bind-key {key} switch-client -T {table} \; display-message "{safeprompt}"'
            yield line.format(table=self._table, key=key, safeprompt=safeprompt)

        for key, command in self._bindings:
            seen.add(key)
            line = 'bind-key -T {table} {key} {command} \; switch-client -T {table} \; display-message "{safeprompt}"'
            yield line.format(table=self._table, key=key, command=command, safeprompt=safeprompt)
        # set up bindings for any keys that haven't been seen
        for key in self._all:
            if key not in seen:
                line = 'bind-key -T {table} {key} switch-client -T {table} \; display-message "INVALID KEY {key}! {safeprompt}"'
                yield line.format(table=self._table, key=key, safeprompt=safeprompt)
        for key in self._special:
            if key not in seen:
                line = 'bind-key -T {table} {key} switch-client -T {table} \; display-message "INVALID KEY {key}! {safeprompt}"'
                yield line.format(table=self._table, key=key, safeprompt=safeprompt)

        yield 'bind-key -T {table} Escape display-message "Exiting ..."'.format(table=self._table)

@section
def tmux_keys():
    if not _wanttmux():
        return

    import re

    lines = []

    # needs to be installed for the current version of python
    mypipinstall('pyyaml', ['pip%d' % sys.version_info.major])
    import yaml

    with open(HERE + '/keybindings/keys.yaml') as f:
        document = yaml.load(f)
    regex = re.compile(r"^(C-)?(O-)?(M-)?(S-)?([ -~]|CR|BS|SPACE|ESC)$")
    if 'tmux' in document:
        static = {}
        dynamic = {}
        if haveexecutable("reattach-to-user-namespace"):
            static['tmux_copy_cmd'] = 'copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"'
        elif haveexecutable("pbcopy"):
            static['tmux_copy_cmd'] = 'copy-pipe-and-cancel "pbcopy"'
        else:
            static['tmux_copy_cmd'] = 'copy-selection'

        sections = [
            ('direct', 'bind-key -n {key} {binding}'),
            ('prefixed', 'bind-key {key} {binding}'),
            ('copy-mode-vi', 'bind-key -T copy-mode-vi {key} send-keys -X {binding}'),
        ]

        for sectionname, template in sections:
            for keycombo, binding in document["tmux"].get(sectionname, {}).items():
                if hasattr(binding, 'keys'):  # is it a dict?
                    if 'static' in binding:
                        binding = static[binding['static']]
                    elif 'dynamic' in binding:
                        binding = dynamic[binding['dynamic']]()
                    else:
                        raise Exception("Invalid binding %r" % binding)
                m = regex.match(keycombo)
                if not m:
                    warn("Invalid keycombo for tmux: direct: %r" % keycombo)
                    continue
                ctrl, opt, meta, shift, key = m.groups()
                if key == "CR":
                    key = "Enter"
                elif key == "ESC":
                    key = "Escape"
                elif key == "SPACE":
                    key = "Space"
                else:
                    assert not key.islower(), "Lowercase keycombo %r is not allowed" % keycombo
                    key = key.lower()
                modifiers = ''
                if ctrl:
                    modifiers += 'C-'
                assert not opt, "O- prefix not allowed for tmux keybinding %r" % keycombo
                if meta:
                    modifiers += 'M-'
                if shift:
                    assert key.islower(), "Invalid keycombo %r" % keycombo
                    key = key.upper()
                lines.append(template.format(key=modifiers + key, binding=binding))

    # we also want to make our special PANE mode
    pm = TmuxCustomMode(
        'panemode',
        "PANEMODE: Move between panes using h, j, k, l. Resize panes using H, J, K, L")
    pm.prefixkeybind('p')
    pm.prefixkeybind('C-p')
    pm.bindkey('h', 'select-pane -L')
    pm.bindkey('j', 'select-pane -D')
    pm.bindkey('k', 'select-pane -U')
    pm.bindkey('l', 'select-pane -R')
    pm.bindkey('H', 'resize-pane -L 2')
    pm.bindkey('J', 'resize-pane -D 2')
    pm.bindkey('K', 'resize-pane -U 2')
    pm.bindkey('L', 'resize-pane -R 2')
    lines.extend(pm.getlines())

    with writefile('~/.tmux/keybindings.conf') as f:
        for line in lines:
            f.write(line)
            f.write("\n")

    lineinfile('~/.tmux.conf', 'source ~/.tmux/keybindings.conf', where=WHERE_TOP)
<commit_msg>We can now install the dependencies we need reliably<commit_after>import sys

from homely.general import (WHERE_TOP, blockinfile, mkdir, run, section,
                            writefile)
from homely.install import InstallFromSource, installpkg
from homely.system import haveexecutable
from homely.ui import warn, yesno

from HOMELY import (HERE, HOME, lineinfile, memoize, mypipinstall,
                    need_installpkg, powerline_path, wantfull, wantpowerline)

@memoize
def _wanttmux():
    return yesno('install_tmux', 'Install tmux?', wantfull())

@section
def tmux_config():
    if not _wanttmux():
        return

    tmux_plugins = yesno('install_tmux_plugins',
                         'Install TPM and use tmux plugins?',
                         wantfull())

    if tmux_plugins:
        mkdir('~/.tmux')
        mkdir('~/.tmux/plugins')
        tpm = InstallFromSource('https://github.com/tmux-plugins/tpm',
                                '~/.tmux/plugins/tpm')
        tpm.select_branch('master')
        run(tpm)

    # what to put in tmux config?
    wildcards = {"DOTFILES": HERE, "HOME": HOME}
    lines = []
    # add our dotfiles python folder to PYTHONPATH before restarting the powerline daemon
    #lines.append("set-environment PYTHONPATH '%(DOTFILES)s/python'")
    if wantpowerline():
        wildcards["POWERLINE"] = powerline_path()
        lines.extend([
            # always replace the daemon on startup, so that re-sourcing the
            # tmux conf always loads updated python modules
            'run-shell "powerline-daemon --replace -q"',
            'source "%(POWERLINE)s/bindings/tmux/powerline.conf"',
        ])
    if tmux_plugins:
        lines.append('source "%(DOTFILES)s/tmux/plugins.conf"')
        # FIXME: the only way to determine if TPM installed correctly is to
        # press `[PREFIX]`, `I` to do a plugin install
    lines.append('source "%(DOTFILES)s/tmux/tmux.conf"')
    lines = [l % wildcards for l in lines]
    blockinfile('~/.tmux.conf',
                lines,
                '# start of automated tmux config',
                '# end of automated tmux config',
                where=WHERE_TOP)

# install or compile tmux
@section
def tmux_install():
    if not _wanttmux():
        return

    if yesno('own_tmux', 'Compile tmux from source?', None):
        need_installpkg(apt=['libevent-dev', 'ncurses-dev'])
        tmux = InstallFromSource('https://github.com/tmux/tmux.git',
                                 '~/src/tmux.git')
        tmux.select_tag('2.7')
        tmux.compile_cmd([
            ['make', 'distclean'],
            ['sh', 'autogen.sh'],
            ['./configure'],
            ['make'],
        ])
        tmux.symlink('tmux', '~/bin/tmux')
        tmux.symlink('tmux.1', '~/man/man1/tmux.1')
        run(tmux)
    else:
        try:
            # install tmux using brew or apt-get ...
            installpkg('tmux')
        except Exception:
            print("-" * 50)
            print("Compiling `tmux` failed - do you need to install automake or gcc?")  # noqa
            print("-" * 50)
            raise

class TmuxCustomMode(object):
    _all = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
            ',<.>/?:[{]}-_=+!@%^&*()`')
    # TODO: what about #'"~;
    _special = ['Enter', 'Left', 'Right', 'Up', 'Down', 'Space']

    def __init__(self, table, prompt):
        super(TmuxCustomMode, self).__init__()
        self._table = table
        self._prompt = prompt
        self._rootkeys = []
        self._prefixkeys = []
        self._bindings = []

    def rootkeybind(self, key):
        self._rootkeys.append(key)

    def prefixkeybind(self, key):
        self._prefixkeys.append(key)

    def bindkey(self, key, command):
        assert key != 'Escape'
        self._bindings.append((key, command))

    def getlines(self):
        safeprompt = self._prompt.replace('\\', '\\\\').replace('"', '\\"')
        seen = set()

        for key in self._rootkeys:
            seen.add(key)
            line = 'bind-key -n {key} switch-client -T {table} \; display-message "{safeprompt}"'
            yield line.format(table=self._table, key=key, safeprompt=safeprompt)

        for key in self._prefixkeys:
            line = 'bind-key {key} switch-client -T {table} \; display-message "{safeprompt}"'
            yield line.format(table=self._table, key=key, safeprompt=safeprompt)

        for key, command in self._bindings:
            seen.add(key)
            line = 'bind-key -T {table} {key} {command} \; switch-client -T {table} \; display-message "{safeprompt}"'
            yield line.format(table=self._table, key=key, command=command, safeprompt=safeprompt)
        # set up bindings for any keys that haven't been seen
        for key in self._all:
            if key not in seen:
                line = 'bind-key -T {table} {key} switch-client -T {table} \; display-message "INVALID KEY {key}! {safeprompt}"'
                yield line.format(table=self._table, key=key, safeprompt=safeprompt)
        for key in self._special:
            if key not in seen:
                line = 'bind-key -T {table} {key} switch-client -T {table} \; display-message "INVALID KEY {key}! {safeprompt}"'
                yield line.format(table=self._table, key=key, safeprompt=safeprompt)

        yield 'bind-key -T {table} Escape display-message "Exiting ..."'.format(table=self._table)

@section
def tmux_keys():
    if not _wanttmux():
        return

    import re

    lines = []

    # needs to be installed for the current version of python
    mypipinstall('pyyaml', ['pip%d' % sys.version_info.major])
    import yaml

    with open(HERE + '/keybindings/keys.yaml') as f:
        document = yaml.load(f)
    regex = re.compile(r"^(C-)?(O-)?(M-)?(S-)?([ -~]|CR|BS|SPACE|ESC)$")
    if 'tmux' in document:
        static = {}
        dynamic = {}
        if haveexecutable("reattach-to-user-namespace"):
            static['tmux_copy_cmd'] = 'copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"'
        elif haveexecutable("pbcopy"):
            static['tmux_copy_cmd'] = 'copy-pipe-and-cancel "pbcopy"'
        else:
            static['tmux_copy_cmd'] = 'copy-selection'

        sections = [
            ('direct', 'bind-key -n {key} {binding}'),
            ('prefixed', 'bind-key {key} {binding}'),
            ('copy-mode-vi', 'bind-key -T copy-mode-vi {key} send-keys -X {binding}'),
        ]

        for sectionname, template in sections:
            for keycombo, binding in document["tmux"].get(sectionname, {}).items():
                if hasattr(binding, 'keys'):  # is it a dict?
                    if 'static' in binding:
                        binding = static[binding['static']]
                    elif 'dynamic' in binding:
                        binding = dynamic[binding['dynamic']]()
                    else:
                        raise Exception("Invalid binding %r" % binding)
                m = regex.match(keycombo)
                if not m:
                    warn("Invalid keycombo for tmux: direct: %r" % keycombo)
                    continue
                ctrl, opt, meta, shift, key = m.groups()
                if key == "CR":
                    key = "Enter"
                elif key == "ESC":
                    key = "Escape"
                elif key == "SPACE":
                    key = "Space"
                else:
                    assert not key.islower(), "Lowercase keycombo %r is not allowed" % keycombo
                    key = key.lower()
                modifiers = ''
                if ctrl:
                    modifiers += 'C-'
                assert not opt, "O- prefix not allowed for tmux keybinding %r" % keycombo
                if meta:
                    modifiers += 'M-'
                if shift:
                    assert key.islower(), "Invalid keycombo %r" % keycombo
                    key = key.upper()
                lines.append(template.format(key=modifiers + key, binding=binding))

    # we also want to make our special PANE mode
    pm = TmuxCustomMode(
        'panemode',
        "PANEMODE: Move between panes using h, j, k, l. Resize panes using H, J, K, L")
    pm.prefixkeybind('p')
    pm.prefixkeybind('C-p')
    pm.bindkey('h', 'select-pane -L')
    pm.bindkey('j', 'select-pane -D')
    pm.bindkey('k', 'select-pane -U')
    pm.bindkey('l', 'select-pane -R')
    pm.bindkey('H', 'resize-pane -L 2')
    pm.bindkey('J', 'resize-pane -D 2')
    pm.bindkey('K', 'resize-pane -U 2')
    pm.bindkey('L', 'resize-pane -R 2')
    lines.extend(pm.getlines())

    with writefile('~/.tmux/keybindings.conf') as f:
        for line in lines:
            f.write(line)
            f.write("\n")

    lineinfile('~/.tmux.conf', 'source ~/.tmux/keybindings.conf', where=WHERE_TOP)
<|endoftext|>


<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from configman import RequiredConfig
import markus

metrics = markus.get_metrics('processor.rule')

class Rule(RequiredConfig):
    """Base class for transform rules

    Provides structure for calling rules during the processor pipeline and also
    has some useful utilities for rules.

    """

    def __init__(self, config=None, quit_check_callback=None):
        self.config = config
        self.quit_check_callback = quit_check_callback

    def predicate(self, raw_crash, raw_dumps, processed_crash, processor_meta_data):
        """Determines whether to run the action for this crash

        :arg raw_crash: the raw crash data
        :arg raw_dumps: any minidumps associated with this crash
        :arg processed_crash: the processed crash
        :arg processor_meta_data: any notes or bookkeeping we need to keep about
            processing as we process

        :returns: True if the action should run, False otherwise

        """
        return True

    def action(self, raw_crash, raw_dumps, processed_crash, processor_meta_data):
        """Executes the rule transforming the crash data

        :arg raw_crash: the raw crash data
        :arg raw_dumps: any minidumps associated with this crash
        :arg processed_crash: the processed crash
        :arg processor_meta_data: any notes or bookkeeping we need to keep about
            processing as we process

        """
        return

    def act(self, raw_crash, raw_dumps, processed_crash, processor_meta_data):
        """Runs predicate and action for a rule

        :arg raw_crash: the raw crash data
        :arg raw_dumps: any minidumps associated with this crash
        :arg processed_crash: the processed crash
        :arg processor_meta_data: any notes or bookkeeping we need to keep about
            processing as we process

        """
        rule_name = self.__class__.__name__
        with metrics.timer('act.timing', tags=['rule:%s' % rule_name]):
            if self.predicate(raw_crash, raw_dumps, processed_crash, processor_meta_data):
                self.action(raw_crash, raw_dumps, processed_crash, processor_meta_data)

    def close(self):
        # FIXME(willkg): see if any of the rules use .close()
        self.config.logger.debug('null close on rule %s', self.__class__)
<commit_msg>Remove FIXME<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from configman import RequiredConfig
import markus

metrics = markus.get_metrics('processor.rule')

class Rule(RequiredConfig):
    """Base class for transform rules

    Provides structure for calling rules during the processor pipeline and also
    has some useful utilities for rules.

    """

    def __init__(self, config=None, quit_check_callback=None):
        self.config = config
        self.quit_check_callback = quit_check_callback

    def predicate(self, raw_crash, raw_dumps, processed_crash, processor_meta_data):
        """Determines whether to run the action for this crash

        :arg raw_crash: the raw crash data
        :arg raw_dumps: any minidumps associated with this crash
        :arg processed_crash: the processed crash
        :arg processor_meta_data: any notes or bookkeeping we need to keep about
            processing as we process

        :returns: True if the action should run, False otherwise

        """
        return True

    def action(self, raw_crash, raw_dumps, processed_crash, processor_meta_data):
        """Executes the rule transforming the crash data

        :arg raw_crash: the raw crash data
        :arg raw_dumps: any minidumps associated with this crash
        :arg processed_crash: the processed crash
        :arg processor_meta_data: any notes or bookkeeping we need to keep about
            processing as we process

        """
        return

    def act(self, raw_crash, raw_dumps, processed_crash, processor_meta_data):
        """Runs predicate and action for a rule

        :arg raw_crash: the raw crash data
        :arg raw_dumps: any minidumps associated with this crash
        :arg processed_crash: the processed crash
        :arg processor_meta_data: any notes or bookkeeping we need to keep about
            processing as we process

        """
        rule_name = self.__class__.__name__
        with metrics.timer('act.timing', tags=['rule:%s' % rule_name]):
            if self.predicate(raw_crash, raw_dumps, processed_crash, processor_meta_data):
                self.action(raw_crash, raw_dumps, processed_crash, processor_meta_data)

    def close(self):
        pass
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

import datetime
import dateparser
import feedparser
import requests
import json
import re

from six.moves.html_parser import HTMLParser

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
CLIENT_CREDENTIALS_FILE = 'client_credentials.json'
APPLICATION_NAME = 'Minsk Geek Eventer'
CALENDAR = 'Minsk Geek Events'

FACEBOOK= [
  u"imaguruby",
  u"eventspace.by",
  u"loftbalki",
  u"john.galt.space.minsk",
  u"cech.by",
  u"HTPBelarus",
  u"cultcenterkorpus",
  u"komn302",
  u"Talaka.by",
  u"hs.minsk",
  u"falanster.by",
  u"MinskPythonMeetup",
  u"minskruby",
  u"MinskJS",
  u"BelarusJavaUserGroup",
  u"UXBelarus",
  u"DataTalks",
  u"391132934426041", #devopsby
  u"Scala-Enthusiasts-Belarus-137171759692484",
  u"modxby",
  u"AzureBelarus",
  u"opendataby",
  u"comaqa.by",
  u"gdgminsk",
  u"webnotbombs",
  u"dotnet.minsk",
  u"funcby",
  u"TheRollingScopes",
  u"BelarusKUG",
  u"javaprofessionalsby",
  u"MinskCSS",
  u"minsk.user.group",
  u"inproductshoes",
  u"kurilka.minsk",
  u"delaisvoedelo",
  u"ReveraLawFirm",
  u"VilgertsBelarus",
  u"34travel",
  u"probusiness.io",
  u"cyberfund",
  u"blockchainmeetups",
  u"iot.belarus.community",
  u"itportal.by",
  u"iloveFSP",
]

STOPWORDS = [
  u"Обучение английскому в онлайн-школе Skyeng",
  u"KinoKORPUS",
]

HEADERS = {"Accept-Language": "en"}

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    store = Storage(CLIENT_CREDENTIALS_FILE)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + CLIENT_CREDENTIALS_FILE)
    return credentials

def main():
    """"""
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)
    now = datetime.datetime.utcnow().date().isoformat() + 'T00:00:00.000000Z' # 'Z' indicates UTC time
    calendarId = 'primary'
    h = HTMLParser()

    # Fetch all calendars and get id of CALENDAR
    page_token = None
    while True:
        calendar_list = service.calendarList().list(pageToken=page_token).execute()
        for calendar_list_entry in calendar_list['items']:
            if CALENDAR == calendar_list_entry["summary"]:
                calendarId = calendar_list_entry["id"]
        page_token = calendar_list.get('nextPageToken')
        if not page_token:
            break

    print('Getting the already existed events')
    events = []
    page_token = None
    while True:
        eventsResult = service.events().list(
            calendarId=calendarId, timeMin=now, maxResults=2500, singleEvents=True,
            orderBy='startTime', pageToken=page_token).execute()
        events.extend(eventsResult.get('items', []))
        page_token = eventsResult.get('nextPageToken')
        if not page_token:
            break
    events_summary = [e['summary'] for e in events]

    today = datetime.datetime.today()
    today = today.replace(hour=0, minute=0, second=0, microsecond=0)

    # instantiate a chrome options object so you can set the size and headless preference
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--window-size=800x5000")
    chrome_options.add_argument('--no-sandbox') # fix for running from root
    chrome_options.add_argument("-lang=en")

    # download the chrome driver from https://sites.google.com/a/chromium.org/chromedriver/downloads
    chrome_driver = "/usr/lib/chromium-browser/chromedriver"

    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)

    # Facebook
    # Facebook closed their graph api so now we would parse it manually
    if FACEBOOK:
        facebook_events = []
        for page in FACEBOOK:
            link = 'https://m.facebook.com/{0}/events'.format(page)
            print('Getting events from {}'.format(link))
            #page = requests.get(link, headers=HEADERS)
            driver.get(link)
            # get 14 next events
            ids = re.findall('href="/events/([0-9]*)\?', driver.page_source)

            for id in ids:
                try:
                    event = {'id': id}
                    link = 'https://m.facebook.com/events/{0}'.format(id)
                    #page = requests.get(link, headers=HEADERS)
                    #event['name'] = h.unescape(re.findall('<title>(.*)</title>', page.text)[0])
                    #event['date'] = h.unescape(re.findall('
]*>([^<]*)
', page.text)[0])
                    #event['place'] = h.unescape(re.findall('
]*>([^<]*)
', page.text)[2])
                    #event['address'] = h.unescape(re.findall('
]*>([^<]*)
', page.text)[3])
                    #event['description'] = ''

                    driver.get(link)
                    event['name'] = h.unescape(re.findall('<title>(.*)</title>', driver.page_source)[0])
                    event['date'] = h.unescape(re.findall('
]*>([^<]*)
', driver.page_source)[1])
                    event['place'] = h.unescape(re.findall('
]*>([^<]*)
', driver.page_source)[4])
                    event['address'] = h.unescape(re.findall('
]*>([^<]*)
', driver.page_source)[5])
                    event['description'] = ''

                    if "UTC" in event['date']:
                        event['timeZone'] = event['date'].split()[-1]
                        event['date'] = event['date'][:-len(event['timeZone'])]
                        if event['timeZone'] != 'UTC':
                            event['timeZone'] += ':00'
                    else:
                        event['timeZone'] = 'UTC+03:00'

                    if u'\xb7' in event['date']:
                        start, end = event['date'].split(u'\xb7')[1].split('-')
                        event['start_time'] = dateparser.parse(start).isoformat()
                        event['end_time'] = dateparser.parse(end).isoformat()
                    elif ' - ' in event['date']:
                        start, end = event['date'].split('-')
                        if ' at ' not in end:
                            end = start.split()[:-2] + end.split()
                            end = ' '.join(end)
                        event['start_time'] = dateparser.parse(start).isoformat()
                        event['end_time'] = dateparser.parse(end).isoformat()
                    else:
                        event['start_time'] = dateparser.parse(event['date']).isoformat()
                        event['end_time'] = event['start_time']

                    # stopwords
                    bullshit_bingo = False
                    for word in STOPWORDS:
                        if word in event['name']:
                            bullshit_bingo = "Bingo!"
                            break
                    if bullshit_bingo:
                        continue
                    elif event['name'] not in events_summary and dateparser.parse(event['start_time']) > dateparser.parse('yesterday'):
                        calendar_event_data = {
                            'summary': event['name'],
                            'location': ", ".join((event['place'], event['address'])),
                            'description': link + "\n\n" + event['description'],
                            'start': {
                                'dateTime': event['start_time'],
                                'timeZone': event['timeZone'],
                            },
                            'end': {
                                'dateTime': event['end_time'],
                                'timeZone': event['timeZone'],
                            }
                        }
                        calendar_event = service.events().insert(calendarId=calendarId, body=calendar_event_data).execute()
                        print('Event created: {}'.format(calendar_event.get('htmlLink')))

                        events_summary.append(event['name'])
                        facebook_events.append(event)
                except Exception as er:
                    print("Can't add '{}' event".format(link))
                    print(event)
                    print(er.__doc__)
                    # print(er.message)

    print('Parsing events.dev.by rss')
    rss = feedparser.parse('https://events.dev.by/rss')
    for e in rss['entries']:
        # Enable it back but only while facebook disabled api
        # https://developers.facebook.com/blog/post/2018/04/04/facebook-api-platform-product-changes
        if facebook_events:
            break

        # stopwords
        bullshit_bingo = False
        for word in STOPWORDS:
            if word in e['title']:
                bullshit_bingo = "Bingo!"
                break
        if bullshit_bingo:
            continue
        elif e['title'] not in events_summary:
            try:
                page = requests.get(e['link'])
                desc = page.text[page.text.find("
")+18:page.text.find("
", page.text.find("
"))]
                desc = desc.replace('«', '"')
                desc = desc.replace('»', '"')
                desc = desc.replace(' ', ' ')
                desc = desc.replace('–', ' - ')
                desc = re.sub(r'\s*
', '- ', desc)
                desc = re.sub(r'\s*
', '\n', desc)
                desc = re.sub(r'\n\n+', '\n', desc)
                desc = re.sub(r'&.+;', '', desc)
                desc = re.sub(r'<(?!\/?a(?=>|\s.*>))\/?.*?>', '', desc)
                location = page.text[page.text.find("&location=")+10:page.text.find("&", page.text.find("&location=")+10)]
                dates = page.text[page.text.find("dates=")+6:page.text.find("&", page.text.find("dates="))]

                event = {
                    'summary': e['title'],
                    'location': location,
                    'description': e['link'] + "\n" + desc,
                    'start': {
                        'dateTime': datetime.datetime.strptime(dates.split('/')[0], "%Y%m%dT%H%M%S").isoformat(),
                        'timeZone': 'GMT',
                    },
                    'end': {
                        'dateTime': datetime.datetime.strptime(dates.split('/')[1], "%Y%m%dT%H%M%S").isoformat(),
                        'timeZone': 'GMT',
                    }
                }
                event = service.events().insert(calendarId=calendarId, body=event).execute()
                print('Event created: {}'.format(event.get('htmlLink')))

                events_summary.append(e['title'])
            except Exception as er:
                raise
                print("Can't add '{}' event".format(e['title']))
                print(event)
                print(er.__doc__)
                print(er.message)

    # TODO:
    # http://www.park.by/cat-38/
    # https://citydog.by/afisha/
    # https://everyng.com/place/Belarus/Minsk

if __name__ == '__main__':
    main()
<commit_msg>add teardown for chromedriver<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

import datetime
import dateparser
import feedparser
import requests
import json
import re

from six.moves.html_parser import HTMLParser

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
CLIENT_CREDENTIALS_FILE = 'client_credentials.json'
APPLICATION_NAME = 'Minsk Geek Eventer'
CALENDAR = 'Minsk Geek Events'

FACEBOOK= [
  u"imaguruby",
  u"eventspace.by",
  u"loftbalki",
  u"john.galt.space.minsk",
  u"cech.by",
  u"HTPBelarus",
  u"cultcenterkorpus",
  u"komn302",
  u"Talaka.by",
  u"hs.minsk",
  u"falanster.by",
  u"MinskPythonMeetup",
  u"minskruby",
  u"MinskJS",
  u"BelarusJavaUserGroup",
  u"UXBelarus",
  u"DataTalks",
  u"391132934426041", #devopsby
  u"Scala-Enthusiasts-Belarus-137171759692484",
  u"modxby",
  u"AzureBelarus",
  u"opendataby",
  u"comaqa.by",
  u"gdgminsk",
  u"webnotbombs",
  u"dotnet.minsk",
  u"funcby",
  u"TheRollingScopes",
  u"BelarusKUG",
  u"javaprofessionalsby",
  u"MinskCSS",
  u"minsk.user.group",
  u"inproductshoes",
  u"kurilka.minsk",
  u"delaisvoedelo",
  u"ReveraLawFirm",
  u"VilgertsBelarus",
  u"34travel",
  u"probusiness.io",
  u"cyberfund",
  u"blockchainmeetups",
  u"iot.belarus.community",
  u"itportal.by",
  u"iloveFSP",
]

STOPWORDS = [
  u"Обучение английскому в онлайн-школе Skyeng",
  u"KinoKORPUS",
]

HEADERS = {"Accept-Language": "en"}

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    store = Storage(CLIENT_CREDENTIALS_FILE)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + CLIENT_CREDENTIALS_FILE)
    return credentials

def main():
    """"""
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)
    now = datetime.datetime.utcnow().date().isoformat() + 'T00:00:00.000000Z' # 'Z' indicates UTC time
    calendarId = 'primary'
    h = HTMLParser()

    # Fetch all calendars and get id of CALENDAR
    page_token = None
    while True:
        calendar_list = service.calendarList().list(pageToken=page_token).execute()
        for calendar_list_entry in calendar_list['items']:
            if CALENDAR == calendar_list_entry["summary"]:
                calendarId = calendar_list_entry["id"]
        page_token = calendar_list.get('nextPageToken')
        if not page_token:
            break

    print('Getting the already existed events')
    events = []
    page_token = None
    while True:
        eventsResult = service.events().list(
            calendarId=calendarId, timeMin=now, maxResults=2500, singleEvents=True,
            orderBy='startTime', pageToken=page_token).execute()
        events.extend(eventsResult.get('items', []))
        page_token = eventsResult.get('nextPageToken')
        if not page_token:
            break
    events_summary = [e['summary'] for e in events]

    today = datetime.datetime.today()
    today = today.replace(hour=0, minute=0, second=0, microsecond=0)

    # instantiate a chrome options object so you can set the size and headless preference
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--window-size=800x5000")
    chrome_options.add_argument('--no-sandbox') # fix for running from root
    chrome_options.add_argument("-lang=en")

    # download the chrome driver from https://sites.google.com/a/chromium.org/chromedriver/downloads
    chrome_driver = "/usr/lib/chromium-browser/chromedriver"

    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)

    # Facebook
    # Facebook closed their graph api so now we would parse it manually
    if FACEBOOK:
        facebook_events = []
        for page in FACEBOOK:
            link = 'https://m.facebook.com/{0}/events'.format(page)
            print('Getting events from {}'.format(link))
            #page = requests.get(link, headers=HEADERS)
            driver.get(link)
            # get 14 next events
            ids = re.findall('href="/events/([0-9]*)\?', driver.page_source)

            for id in ids:
                try:
                    event = {'id': id}
                    link = 'https://m.facebook.com/events/{0}'.format(id)
                    #page = requests.get(link, headers=HEADERS)
                    #event['name'] = h.unescape(re.findall('<title>(.*)</title>', page.text)[0])
                    #event['date'] = h.unescape(re.findall('
]*>([^<]*)
', page.text)[0])
                    #event['place'] = h.unescape(re.findall('
]*>([^<]*)
', page.text)[2])
                    #event['address'] = h.unescape(re.findall('
]*>([^<]*)
', page.text)[3])
                    #event['description'] = ''

                    driver.get(link)
                    event['name'] = h.unescape(re.findall('<title>(.*)</title>', driver.page_source)[0])
                    event['date'] = h.unescape(re.findall('
]*>([^<]*)
', driver.page_source)[1])
                    event['place'] = h.unescape(re.findall('
]*>([^<]*)
', driver.page_source)[4])
                    event['address'] = h.unescape(re.findall('
]*>([^<]*)
', driver.page_source)[5])
                    event['description'] = ''

                    if "UTC" in event['date']:
                        event['timeZone'] = event['date'].split()[-1]
                        event['date'] = event['date'][:-len(event['timeZone'])]
                        if event['timeZone'] != 'UTC':
                            event['timeZone'] += ':00'
                    else:
                        event['timeZone'] = 'UTC+03:00'

                    if u'\xb7' in event['date']:
                        start, end = event['date'].split(u'\xb7')[1].split('-')
                        event['start_time'] = dateparser.parse(start).isoformat()
                        event['end_time'] = dateparser.parse(end).isoformat()
                    elif ' - ' in event['date']:
                        start, end = event['date'].split('-')
                        if ' at ' not in end:
                            end = start.split()[:-2] + end.split()
                            end = ' '.join(end)
                        event['start_time'] = dateparser.parse(start).isoformat()
                        event['end_time'] = dateparser.parse(end).isoformat()
                    else:
                        event['start_time'] = dateparser.parse(event['date']).isoformat()
                        event['end_time'] = event['start_time']

                    # stopwords
                    bullshit_bingo = False
                    for word in STOPWORDS:
                        if word in event['name']:
                            bullshit_bingo = "Bingo!"
                            break
                    if bullshit_bingo:
                        continue
                    elif event['name'] not in events_summary and dateparser.parse(event['start_time']) > dateparser.parse('yesterday'):
                        calendar_event_data = {
                            'summary': event['name'],
                            'location': ", ".join((event['place'], event['address'])),
                            'description': link + "\n\n" + event['description'],
                            'start': {
                                'dateTime': event['start_time'],
                                'timeZone': event['timeZone'],
                            },
                            'end': {
                                'dateTime': event['end_time'],
                                'timeZone': event['timeZone'],
                            }
                        }
                        calendar_event = service.events().insert(calendarId=calendarId, body=calendar_event_data).execute()
                        print('Event created: {}'.format(calendar_event.get('htmlLink')))

                        events_summary.append(event['name'])
                        facebook_events.append(event)
                except Exception as er:
                    print("Can't add '{}' event".format(link))
                    print(event)
                    print(er.__doc__)
                    # print(er.message)

    # Close chrome driver
    driver.quit()

    print('Parsing events.dev.by rss')
    rss = feedparser.parse('https://events.dev.by/rss')
    for e in rss['entries']:
        # Enable it back but only while facebook disabled api
        # https://developers.facebook.com/blog/post/2018/04/04/facebook-api-platform-product-changes
        if facebook_events:
            break

        # stopwords
        bullshit_bingo = False
        for word in STOPWORDS:
            if word in e['title']:
                bullshit_bingo = "Bingo!"
                break
        if bullshit_bingo:
            continue
        elif e['title'] not in events_summary:
            try:
                page = requests.get(e['link'])
                desc = page.text[page.text.find("
")+18:page.text.find("
", page.text.find("
"))]
                desc = desc.replace('«', '"')
                desc = desc.replace('»', '"')
                desc = desc.replace(' ', ' ')
                desc = desc.replace('–', ' - ')
                desc = re.sub(r'\s*
', '- ', desc)
                desc = re.sub(r'\s*
', '\n', desc)
                desc = re.sub(r'\n\n+', '\n', desc)
                desc = re.sub(r'&.+;', '', desc)
                desc = re.sub(r'<(?!\/?a(?=>|\s.*>))\/?.*?>', '', desc)
                location = page.text[page.text.find("&location=")+10:page.text.find("&", page.text.find("&location=")+10)]
                dates = page.text[page.text.find("dates=")+6:page.text.find("&", page.text.find("dates="))]

                event = {
                    'summary': e['title'],
                    'location': location,
                    'description': e['link'] + "\n" + desc,
                    'start': {
                        'dateTime': datetime.datetime.strptime(dates.split('/')[0], "%Y%m%dT%H%M%S").isoformat(),
                        'timeZone': 'GMT',
                    },
                    'end': {
                        'dateTime': datetime.datetime.strptime(dates.split('/')[1], "%Y%m%dT%H%M%S").isoformat(),
                        'timeZone': 'GMT',
                    }
                }
                event = service.events().insert(calendarId=calendarId, body=event).execute()
                print('Event created: {}'.format(event.get('htmlLink')))

                events_summary.append(e['title'])
            except Exception as er:
                raise
                print("Can't add '{}' event".format(e['title']))
                print(event)
                print(er.__doc__)
                print(er.message)

    # TODO:
    # http://www.park.by/cat-38/
    # https://citydog.by/afisha/
    # https://everyng.com/place/Belarus/Minsk

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
from celery.task.schedules import crontab
from celery.decorators import periodic_task

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo.settings')
app = Celery('todo')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
from todoapp.models import Todolist, Registration
from datetime import datetime
from django.utils.timezone import get_current_timezone

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

# @app.task(bind=True)
# def debug_task(self):
#     print('Request: {0!r}'.format(self.request))

@app.task(bind=True)
def reverse(self, string):
	return string[::-1]

@periodic_task(run_every=(crontab(minute='*/15')), name="some_task", ignore_result=True)
def some_task():

    print "sheesh"
<commit_msg>added task queue<commit_after>from __future__ import absolute_import
import os
import datetime
from celery import Celery
from django.conf import settings
from celery.task.schedules import crontab
from celery.decorators import periodic_task
from django.core.mail import send_mail

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo.settings')
app = Celery('todo')

# Using a string here means the worker will not have to
# pickle the object when using Windows.

#from django.utils.timezone import get_current_timezone

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

# @app.task(bind=True)
# def debug_task(self):
#     print('Request: {0!r}'.format(self.request))

@app.task(bind=True)
def reverse(self, string):
    return string[::-1]

@periodic_task(run_every=(crontab(minute='*/1')), name="some_task", ignore_result=True)
def some_task():
    from todoapp.models import Todolist, Registration
    from todo import settings
    obj_list= Todolist.objects.all()
    for x in obj_list:
        if x.due_date == None:
            pass
        else:
            date_obj = x.due_date
            print date_obj
            #date_obj = str(date_obj).split('+')[0]
            #obj_date=datetime.datetime.strptime(date_obj, '%Y-%m-%d %H:%M:%S')
            mm= (date_obj - datetime.datetime.now())
            print mm
            print mm.total_seconds()/60
            noti_date = mm.total_seconds()/60
            #print x.person_id
            print x.task
            obj = Registration.objects.get(id = x.person_id)
            ss = str(obj.email_id)

            if noti_date > 0 and noti_date <20:
                send_mail('Hii %s' % str(obj.user_name), "Just a reminder that your {%s} task is due soon." %x.task, 'snehatezu@gmail.com', [ss], fail_silently=False)
                #print obj.email_id
<|endoftext|>


<commit_before>from dateutil.relativedelta import relativedelta

from custom.icds_reports.const import AGG_MIGRATION_TABLE
from custom.icds_reports.utils.aggregation_helpers import month_formatter
from custom.icds_reports.utils.aggregation_helpers.distributed.base import (
    StateBasedAggregationDistributedHelper,
)

class MigrationFormsAggregationDistributedHelper(StateBasedAggregationDistributedHelper):
    helper_key = 'migration-forms'
    ucr_date_source_id = 'static-migration_form'
    aggregate_parent_table = AGG_MIGRATION_TABLE

    def data_from_ucr_query(self):
        month = self.month.replace(day=1)
        current_month_start = month_formatter(self.month)
        next_month_start = month_formatter(self.month + relativedelta(month=1))

        query_params = {
            "month": month_formatter(month),
            "state_id": self.state_id,
            "current_month_start": current_month_start,
            "next_month_start": next_month_start,
        }
        return """
        SELECT DISTINCT ON (person_case_id)
            %(state_id)s AS state_id,
            supervisor_id,
            %(month)s::DATE AS month,
            person_case_id AS person_case_id,
            LAST_VALUES(is_migrated) OVER w AS is_migrated,
          FROM "{ucr_tablename}"
          WHERE state_id = %(state_id)s AND
                timeend >= %(current_month_start)s AND timeend < %(next_month_start)s AND
                person_case_id IS NOT NULL
          WINDOW w AS (
            PARTITION BY supervisor_id, person_case_id
            ORDER BY timeend RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
        )
        """.format(
            ucr_tablename=self.ucr_tablename,
            tablename=self.aggregate_parent_table,
        ), query_params

    def aggregation_query(self):
        month = self.month.replace(day=1)

        ucr_query, ucr_query_params = self.data_from_ucr_query()

        query_params = {
            "month": month_formatter(month),
            "state_id": self.state_id,
            "previous_month": month_formatter(month - relativedelta(months=1)),
        }
        query_params.update(ucr_query_params)

        return """
        INSERT INTO "{tablename}" (
          state_id, supervisor_id, month, person_case_id, migration_status
        ) (
          SELECT
            %(state_id)s AS state_id,
            COALESCE(ucr.supervisor_id, prev_month.supervisor_id) AS supervisor_id,
            %(month)s::DATE AS month,
            COALESCE(ucr.person_case_id, prev_month.person_case_id) AS person_case_id,
            COALESCE(ucr.is_migrated, prev_month.is_migrated) as migration_status
          FROM ({ucr_table_query}) ucr
          FULL OUTER JOIN (
             SELECT * FROM "{tablename}" WHERE month = %(previous_month)s AND state_id = %(state_id)s
             ) prev_month
            ON ucr.person_case_id = prev_month.person_case_id AND ucr.supervisor_id = prev_month.supervisor_id
            WHERE coalesce(ucr.month, %(month)s) = %(month)s
                AND coalesce(prev_month.month, %(previous_month)s) = %(previous_month)s
                AND coalesce(prev_month.state_id, %(state_id)s) = %(state_id)s
        )
        """.format(
            ucr_tablename=self.ucr_tablename,
            tablename=self.aggregate_parent_table,
            ucr_table_query=ucr_query
        ), query_params
<commit_msg>[fix]: minor column name fix<commit_after>from dateutil.relativedelta import relativedelta

from custom.icds_reports.const import AGG_MIGRATION_TABLE
from custom.icds_reports.utils.aggregation_helpers import month_formatter
from custom.icds_reports.utils.aggregation_helpers.distributed.base import (
    StateBasedAggregationDistributedHelper,
)

class MigrationFormsAggregationDistributedHelper(StateBasedAggregationDistributedHelper):
    helper_key = 'migration-forms'
    ucr_date_source_id = 'static-migration_form'
    aggregate_parent_table = AGG_MIGRATION_TABLE

    def data_from_ucr_query(self):
        month = self.month.replace(day=1)
        current_month_start = month_formatter(self.month)
        next_month_start = month_formatter(self.month + relativedelta(month=1))

        query_params = {
            "month": month_formatter(month),
            "state_id": self.state_id,
            "current_month_start": current_month_start,
            "next_month_start": next_month_start,
        }
        return """
        SELECT DISTINCT ON (person_case_id)
            %(state_id)s AS state_id,
            supervisor_id,
            %(month)s::DATE AS month,
            person_case_id AS person_case_id,
            LAST_VALUES(is_migrated) OVER w AS is_migrated,
          FROM "{ucr_tablename}"
          WHERE state_id = %(state_id)s AND
                timeend >= %(current_month_start)s AND timeend < %(next_month_start)s AND
                person_case_id IS NOT NULL
          WINDOW w AS (
            PARTITION BY supervisor_id, person_case_id
            ORDER BY timeend RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
        )
        """.format(
            ucr_tablename=self.ucr_tablename,
            tablename=self.aggregate_parent_table,
        ), query_params

    def aggregation_query(self):
        month = self.month.replace(day=1)

        ucr_query, ucr_query_params = self.data_from_ucr_query()

        query_params = {
            "month": month_formatter(month),
            "state_id": self.state_id,
            "previous_month": month_formatter(month - relativedelta(months=1)),
        }
        query_params.update(ucr_query_params)

        return """
        INSERT INTO "{tablename}" (
          state_id, supervisor_id, month, person_case_id, is_migrated
        ) (
          SELECT
            %(state_id)s AS state_id,
            COALESCE(ucr.supervisor_id, prev_month.supervisor_id) AS supervisor_id,
            %(month)s::DATE AS month,
            COALESCE(ucr.person_case_id, prev_month.person_case_id) AS person_case_id,
            COALESCE(ucr.is_migrated, prev_month.is_migrated) as is_migrated
          FROM ({ucr_table_query}) ucr
          FULL OUTER JOIN (
             SELECT * FROM "{tablename}" WHERE month = %(previous_month)s AND state_id = %(state_id)s
             ) prev_month
            ON ucr.person_case_id = prev_month.person_case_id AND ucr.supervisor_id = prev_month.supervisor_id
            WHERE coalesce(ucr.month, %(month)s) = %(month)s
                AND coalesce(prev_month.month, %(previous_month)s) = %(previous_month)s
                AND coalesce(prev_month.state_id, %(state_id)s) = %(state_id)s
        )
        """.format(
            ucr_tablename=self.ucr_tablename,
            tablename=self.aggregate_parent_table,
            ucr_table_query=ucr_query
        ), query_params
<|endoftext|>


<commit_before>from __future__ import absolute_import
import os

from .helper import SolveBioTestCase

class VCFParserTest(SolveBioTestCase):

    def test_vcfparser(self):
        from solvebio.contrib.vcf_parser.vcf_parser import ExpandingVCFParser

        expected_fields = (
            'genomic_coordinates',
            'variant',
            'allele',
            'row_id',
            'reference_allele',
            'alternate_alleles',
            'info',
            'qual',
            'filter',
        )

        path = os.path.join(os.path.dirname(__file__),
                            "data/sample.vcf.gz")

        infile = open(path, 'rb')
        parser = ExpandingVCFParser(infile, genome_build='GRCh37')
        # Test that the keys exist and that certain fields are always set
        # Also test that allele expansion is working
        for row in parser:
            self.assertEqual(set(expected_fields), set(row.keys()))
            self.assertTrue(row['genomic_coordinates'])
            self.assertTrue(row['variant'])
            # Ensure 1 allele per row
            self.assertEqual(len(row['allele']), 1)
<commit_msg>fix vcf parser test<commit_after>from __future__ import absolute_import
import os

from .helper import SolveBioTestCase

class VCFParserTest(SolveBioTestCase):

    def test_vcfparser(self):
        from solvebio.contrib.vcf_parser.vcf_parser import ExpandingVCFParser

        expected_fields = (
            'genomic_coordinates',
            'variant',
            'allele',
            'row_id',
            'reference_allele',
            'alternate_alleles',
            'info',
            'qual',
            'filter',
        )

        path = os.path.join(os.path.dirname(__file__),
                            "data/sample.vcf.gz")
        parser = ExpandingVCFParser(path, genome_build='GRCh37')
        # Test that the keys exist and that certain fields are always set
        # Also test that allele expansion is working
        for row in parser:
            self.assertEqual(set(expected_fields), set(row.keys()))
            self.assertTrue(row['genomic_coordinates'])
            self.assertTrue(row['variant'])
            # Ensure 1 allele per row
            self.assertEqual(len(row['allele']), 1)
<|endoftext|>


<commit_before>from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.rst import RstDocument

doc = '''
* An Example
* Layout3D for Kivy Widgets
* More to come
* If you want to help develop this project mail me: kpiorno@uci.cu
'''

class AtomPlatformApp(App):
    def build(self):
        self.m_doc = doc
        from kivy.lang import Builder
        root = Builder.load_string('''
#:kivy 1.0
#: import Layout3D kivy3dgui.layout3d
#: import Animation kivy.animation.Animation
Layout3D:
    id: par
    size_hint: (1.0, 1.0)
    post_processing: True
    Node:
        id: Node1
        name: 'Node 0'
        rotate: (90, 0.3, 1, 0)
        scale: (0.4, 0.4, 0.4)
        translate: (20, -10.0, -30)
        effect: True
        meshes: ("./data/obj/sphere.obj", )
        FloatLayout:
            canvas:
                Color:
                    rgb: 1, 1, 1,0.1
                Rectangle:
                    size: self.size
                    pos: self.pos
                    source: "./data/imgs/background.jpg"
            size_hint: (1.0, 1.0)
            Button:
                #size_hint: (0.2, 0.2)
                pos_hint: {"x":0, "y":0 }
                size_hint: (None, None)
                text: "Hello"

            Button:
                size_hint: (0.3, 0.3)
                text: "Rotate"

            Button:
                size_hint: (0.2, 0.2)
                pos_hint: {"x":0.0, "y":0.6}
                text: "GO!!!"
                font_size: 50

                on_release: Animation(rotate=(-60, 0, 1.0, 0.0), scale=(1, 1, 1), duration=4.3).start(SecondWindow)
                on_release: Animation(rotate=(7, 0, 1.0, 0.0), translate=(-240, -80, 380), duration=10.3).start(FirstWindow)

            TextInput:
                id: TextSphere
                text: "Please write or write please?"
                size_hint: (0.8, 0.4)
                on_focus: if args[1]: Animation(translate= (10, -10.0, -20), rotate=(95, 0, 1.0, -0.3), duration=0.3).start(Node1)
                on_focus: if not args[1]: Animation(translate= (20, -10.0, -40), rotate= (90, 0.3, 1, 0), duration=0.3).start(Node1)
                font_size: 40

    Node:
        id: SecondWindow
        name: 'Node 1'
        scale: (0.01, 0.01, 0.01)
        translate: (95, 0, -80)
        effect: True
        meshes: ("./data/obj/2dbox.obj",)
        FloatLayout:
            id: float
            canvas:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    size: self.size
                    pos: self.pos
                    source: "./data/imgs/background.jpg"
            size_hint: (1.0, 1.0)
            RstDocument:
                size_hint: (1.0, 1.0)
                pos_hint: {"x": 0.00, "y": 0.00}
                text: app.m_doc
    Node:
        id: FirstWindow
        name: 'Node 1'
        rotate: (10, 0, 1, 0)
        scale: (1, 0.7, 1)
        translate: (0, 0, -40)
        effect: True
        meshes: ("./data/obj/2dbox.obj",)
        FloatLayout:
            canvas:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    size: self.size
                    pos: self.pos
                    source: "./data/imgs/background.jpg"
            size_hint: (1.0, 1.0)
            RstDocument:
                size_hint: (0.3, 0.98)
                pos_hint: {"x": 0.7, "y": 0.01}
                text: app.m_doc

            GridLayout:
                cols: 2
                pos_hint: {"x": 0.4, "y": 0.1}

                size_hint: (0.28, 0.5)
                GridLayout:
                    cols: 2
                    Label:
                        text: "Effect"
                        font_size: 40

                    CheckBox:
                        text: 'Disable'
                        active: True
                        on_active: par.post_processing = self.active
                Spinner:
                    values: ["A","B"]
                Button:
                    size_hint: (0.3, 1.0)
                    text: "Dummy Button!!!"

                TextInput:
                Label:
                    size_hint: (0.3, 1.0)
                    text: "Kivy 3D, Enjoy it!!!"

            Image:
                size_hint: (0.26, 0.8)
                pos_hint: {"x": 0.1, "y": 0.1}
                source: "./data/imgs/faust_github.jpg"

            Button:
                size_hint: (0.1, 0.05)
                pos_hint: {"x": 0.5, "y": 0.8}
                text: "Rotate"
                on_release: (Animation(rotate=(30, 0, 1, 0), duration=4.0, t="out_bounce") + Animation(rotate=(10, 0, 1, 0), duration=4.0, t="out_bounce")).start(FirstWindow)
                on_release: Animation.cancel_all(FirstWindow)
            Slider:
                size_hint: (0.3, 0.1)
                pos_hint: {"x": 0.4, "y": 0.6}
                text: "Spinx"

            Label:
                size_hint: (0.3, 0.3)
                pos_hint: {"x": 0.4, "y": 0.62}
                text: TextSphere.text
    Node:
        id: SecondWindowA
        name: 'Node 1'
        rotate: (7, 0, 1, 0)
        scale: (20, 20, 20)
        translate: (-240, -80, 380)
        effect: True
        FloatLayout:
            canvas:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    size: self.size
                    pos: self.pos
                    source: "./data/imgs/imgback.jpg"
            size_hint: (1.0, 1.0)
            RstDocument:
                size_hint: (0.3, 0.98)
                pos_hint: {"x": 0.01, "y": 0.01}
                text: app.m_doc

        ''')
        self.root = root
        return root
        #return self.app
        #return self.app

if __name__ == '__main__':
    AtomPlatformApp().run()
<commit_msg>Basic Example<commit_after>from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.rst import RstDocument

doc = '''
* An Example
* Layout3D for Kivy Widgets
* More to come
* If you want to help develop this project mail me: kpiorno@uci.cu
'''

class AtomPlatformApp(App):
    def build(self):
        self.m_doc = doc
        from kivy.lang import Builder
        root = Builder.load_string('''
#:kivy 1.0
#: import Layout3D kivy3dgui.layout3d
#: import Animation kivy.animation.Animation
Layout3D:
    id: par
    size_hint: (1.0, 1.0)
    post_processing: True
    Node:
        id: Node1
        name: 'Node 0'
        rotate: (90, 0.3, 1, 0)
        scale: (0.4, 0.4, 0.4)
        translate: (20, -10.0, -30)
        effect: True
        meshes: ("./data/obj/sphere.obj", )
        FloatLayout:
            canvas:
                Color:
                    rgb: 1, 1, 1,0.1
                Rectangle:
                    size: self.size
                    pos: self.pos
                    source: "./data/imgs/background.jpg"
            size_hint: (1.0, 1.0)
            Button:
                #size_hint: (0.2, 0.2)
                pos_hint: {"x":0, "y":0 }
                size_hint: (None, None)
                text: "Hello"

            Button:
                size_hint: (0.3, 0.3)
                text: "Rotate"

            Button:
                size_hint: (0.2, 0.2)
                pos_hint: {"x":0.0, "y":0.6}
                text: "GO!!!"
                font_size: 50

                on_release: Animation(rotate=(-60, 0, 1.0, 0.0), scale=(1, 1, 1), duration=4.3).start(SecondWindow)
                on_release: Animation(rotate=(7, 0, 1.0, 0.0), translate=(-240, -80, 380), duration=10.3).start(FirstWindow)

            TextInput:
                id: TextSphere
                text: "Please write or write please?"
                size_hint: (0.8, 0.4)
                on_focus: if args[1]: Animation(translate= (10, -10.0, -20), rotate=(95, 0, 1.0, -0.3), duration=0.3).start(Node1)
                on_focus: if not args[1]: Animation(translate= (20, -10.0, -40), rotate= (90, 0.3, 1, 0), duration=0.3).start(Node1)
                font_size: 40

    Node:
        id: SecondWindow
        name: 'Node 1'
        scale: (0.01, 0.01, 0.01)
        translate: (95, 0, -80)
        effect: True
        meshes: ("./data/obj/2dbox.obj",)
        FloatLayout:
            id: float
            canvas:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    size: self.size
                    pos: self.pos
                    source: "./data/imgs/background.jpg"
            size_hint: (1.0, 1.0)
            RstDocument:
                size_hint: (1.0, 1.0)
                pos_hint: {"x": 0.00, "y": 0.00}
                text: app.m_doc
    Node:
        id: FirstWindow
        name: 'Node 1'
        rotate: (10, 0, 1, 0)
        scale: (1, 0.7, 1)
        translate: (0, 0, -40)
        effect: True
        meshes: ("./data/obj/2dbox.obj",)
        FloatLayout:
            canvas:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    size: self.size
                    pos: self.pos
                    source: "./data/imgs/background.jpg"
            size_hint: (1.0, 1.0)
            RstDocument:
                size_hint: (0.3, 0.98)
                pos_hint: {"x": 0.7, "y": 0.01}
                text: app.m_doc

            GridLayout:
                cols: 2
                pos_hint: {"x": 0.4, "y": 0.1}

                size_hint: (0.28, 0.5)
                GridLayout:
                    cols: 2
                    Label:
                        text: "Effect"
                        font_size: 40

                    CheckBox:
                        text: 'Disable'
                        active: True
                        on_active: par.post_processing = self.active
                Spinner:
                    values: ["A","B"]
                Button:
                    size_hint: (0.3, 1.0)
                    text: "Dummy Button!!!"

                TextInput:
                Label:
                    size_hint: (0.3, 1.0)
                    text: "Kivy 3D, Enjoy it!!!"

            Image:
                size_hint: (0.26, 0.8)
                pos_hint: {"x": 0.1, "y": 0.1}
                source: "./data/imgs/faust_github.jpg"

            Button:
                size_hint: (0.1, 0.05)
                pos_hint: {"x": 0.5, "y": 0.8}
                text: "Rotate"
                on_release: (Animation(rotate=(30, 0, 1, 0), duration=4.0, t="out_bounce") + Animation(rotate=(10, 0, 1, 0), duration=4.0, t="out_bounce")).start(FirstWindow)
                on_release: Animation.cancel_all(FirstWindow)
            Slider:
                size_hint: (0.3, 0.1)
                pos_hint: {"x": 0.4, "y": 0.6}
                text: "Spinx"

            Label:
                size_hint: (0.3, 0.3)
                pos_hint: {"x": 0.4, "y": 0.62}
                text: TextSphere.text
    Node:
        id: SecondWindowA
        name: 'Node 1'
        rotate: (7, 0, 1, 0)
        scale: (20, 20, 20)
        translate: (-240, -80, 380)
        effect: True
        FloatLayout:
            canvas:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    size: self.size
                    pos: self.pos
                    source: "./data/imgs/imgback.jpg"
            size_hint: (1.0, 1.0)
            RstDocument:
                size_hint: (0.3, 0.98)
                pos_hint: {"x": 0.01, "y": 0.01}
                text: app.m_doc

        ''')
        self.root = root
        return root

if __name__ == '__main__':
    AtomPlatformApp().run()
<|endoftext|>


<commit_before># /usr/bin/python3
import io
import argparse

parser = argparse.ArgumentParser(description='Process some integers.', prefix_chars='-+', epilog="here's some epilog text", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('keyword', metavar='Q', type=str, nargs=1,
        help='action keyword')

parser.add_argument('integers', metavar='N', type=int, nargs='+',
        help='an integer for the accumulator')

parser.add_argument('--sum', '-s', dest='accumulate', action='store_const',
        const=sum, default=max, help='sum the integers (default: find the max)')

parser.add_argument('--foo', nargs='?', help='foo help')
parser.add_argument('--file', nargs='?', help='foo help', type=io.IOBase)
parser.add_argument('--bar', nargs='*', default=[1, 2, 3], help='BAR!')
#parser.add_argument('+f', action='store_const', const='43')
parser.add_argument('--true', action='store_true', help='Store a true')
parser.add_argument('--false', action='store_false', help='Store a false')
parser.add_argument('--append', action='append', help='Append a value')

parser.add_argument('--nargs2', nargs=2, help='nargs2')

parser.add_argument('--mode', choices=['rock', 'paper', 'scissors'], default='scissors')

parser.add_argument('--version', action='version', version='2.0')
args = parser.parse_args()
<commit_msg>Fix #16<commit_after># /usr/bin/python3
import argparse

parser = argparse.ArgumentParser(description='Process some integers.', prefix_chars='-+', epilog="here's some epilog text", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('keyword', metavar='Q', type=str, nargs=1,
        help='action keyword')

parser.add_argument('integers', metavar='N', type=int, nargs='+',
        help='an integer for the accumulator')

parser.add_argument('--sum', '-s', dest='accumulate', action='store_const',
        const=sum, default=max, help='sum the integers (default: find the max)')

parser.add_argument('--foo', nargs='?', help='foo help')
parser.add_argument('--bar', nargs='*', default=[1, 2, 3], help='BAR!')
parser.add_argument('--true', action='store_true', help='Store a true')
parser.add_argument('--false', action='store_false', help='Store a false')
parser.add_argument('--append', action='append', help='Append a value')

parser.add_argument('--nargs2', nargs=2, help='nargs2')

parser.add_argument('--mode', choices=['rock', 'paper', 'scissors'], default='scissors')

parser.add_argument('--version', action='version', version='2.0')
args = parser.parse_args()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# See LICENSE file for full copyright and licensing details.

from random import randint
from thingface import Client

def connection_handler(connected):
    print('connection status %d' % (connected,))

def error_handler(error):
    print('error: %s' % (error,))

def command_handler(sender_type, sender_id, command, args):
    print(
        'command handler, sender_type:%s, sender_id:%s, command:%s, args:%s' %
        (sender_type, sender_id, command, args)
    )

def generate_temp():
    return randint(180, 390) / 10.0

if __name__ == '__main__':
    client = Client()
    client.on_error = error_handler
    client.on_connection_state(connection_handler)
    client.tls_set('ca.crt')  # use on ssl only
    client.connect(
        'mydevice01',
        'xxxxxxxxxxxxSSSSSSSSHHHHHHAAA7',
        'dev-app.thingface.io'
    )
    client.on_command(command_handler)
    temp = generate_temp()
    client.send_sensor_value('temp101', temp)
    client.disconnect()
    print('Temp: %f' % (temp,))
<commit_msg>[FIX] connect strings<commit_after># -*- coding: utf-8 -*-
# See LICENSE file for full copyright and licensing details.

from random import randint
from thingface import Client

def connection_handler(connected):
    print('connection status %d' % (connected,))

def error_handler(error):
    print('error: %s' % (error,))

def command_handler(sender_type, sender_id, command, args):
    print(
        'command handler, sender_type:%s, sender_id:%s, command:%s, args:%s' %
        (sender_type, sender_id, command, args)
    )

def generate_temp():
    return randint(180, 390) / 10.0

if __name__ == '__main__':
    client = Client()
    client.on_error = error_handler
    client.on_connection_state(connection_handler)
    client.tls_set('ca.crt')  # use on ssl only
    client.connect(
        'device ID',
        'device secret key',
        'host'
    )
    client.on_command(command_handler)
    temp = generate_temp()
    client.send_sensor_value('temp101', temp)
    client.disconnect()
    print('Temp: %f' % (temp,))
<|endoftext|>


<commit_before>#!/usr/bin/env python3
import sys, argparse, subprocess, json, base64, binascii, re, copy, logging, requests

LOGGER = logging.getLogger("acme_account_rollover")
LOGGER.addHandler(logging.StreamHandler())

def account_rollover(old_accountkeypath, new_accountkeypath, acme_directory, log=LOGGER):
    def _b64(b):
        """"Encodes string as base64 as specified in ACME RFC """
        return base64.urlsafe_b64encode(b).decode("utf8").rstrip("=")

    def _openssl(command, options, communicate=None):
        """Run openssl command line and raise IOError on non-zero return."""
        openssl = subprocess.Popen(["openssl", command] + options,
                                   stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = openssl.communicate(communicate)
        if openssl.returncode != 0:
            raise IOError("OpenSSL Error: {0}".format(err))
        return out

    def _jws_header(accountkeypath):
        """Creates a JWS header according to a specific account key path."""
        accountkey = _openssl("rsa", ["-in", accountkeypath, "-noout", "-text"])
        pub_hex, pub_exp = re.search(
            r"modulus:\r?\n\s+00:([a-f0-9\:\s]+?)\r?\npublicExponent: ([0-9]+)",
            accountkey.decode("utf8"), re.MULTILINE | re.DOTALL).groups()
        pub_exp = "{0:x}".format(int(pub_exp))
        pub_exp = "0{0}".format(pub_exp) if len(pub_exp) % 2 else pub_exp
        jws_header = {
            "alg": "RS256",
            "jwk": {
                "e": _b64(binascii.unhexlify(pub_exp.encode("utf-8"))),
                "kty": "RSA",
                "n": _b64(binascii.unhexlify(re.sub(r"(\s|:)", "", pub_hex).encode("utf-8"))),
            },
            "kid": None
        }
        return jws_header

    def _sign_request(url, keypath, payload, is_inner = False):
        """Signs request with a specific right account key."""
        nonlocal jws_nonce
        if payload == "": # on POST-as-GET, final payload has to be just empty string
            payload64 = ""
        else:
            payload64 = _b64(json.dumps(payload).encode("utf8"))
        if keypath == new_accountkeypath:
            protected = copy.deepcopy(new_jws_header)
        elif keypath == old_accountkeypath:
            protected = copy.deepcopy(old_jws_header)

        if is_inner or url == acme_config["newAccount"]:
            del protected["kid"]
        else:
            del protected["jwk"]

        if not is_inner:
            protected["nonce"] = jws_nonce or requests.get(acme_config["newNonce"]).headers['Replay-Nonce']
        protected["url"] = url
        protected64 = _b64(json.dumps(protected).encode("utf8"))
        signature = _openssl("dgst", ["-sha256", "-sign", keypath],
                             "{0}.{1}".format(protected64, payload64).encode("utf8"))
        signedjws = {
            "protected": protected64, "payload": payload64,"signature": _b64(signature)
        }
        return signedjws

    def _send_signed_request(url, keypath, payload):
        """Sends signed requests to ACME server."""
        nonlocal jws_nonce
        jose = _sign_request(url, keypath, payload)
        try:
            response = requests.post(url, json=jose, headers=joseheaders)
        except requests.exceptions.RequestException as error:
            response = error.response
        finally:
            jws_nonce = response.headers['Replay-Nonce']
            try:
                return response, response.json()
            except ValueError as error:
                return response, json.dumps({})

    # main code
    adtheaders =  {'User-Agent': 'acme-dns-tiny/2.0'}
    joseheaders=copy.deepcopy(adtheaders)
    joseheaders['Content-Type']='application/jose+json'

    log.info("Fetch informations from the ACME directory.")
    directory = requests.get(acme_directory, headers=adtheaders)
    acme_config = directory.json()

    log.info("Parsing current account key...")
    old_jws_header = _jws_header(old_accountkeypath)

    log.info("Parsing new account key...")
    new_jws_header = _jws_header(new_accountkeypath)
    del new_jws_header["kid"]

    jws_nonce = None

    log.info("Ask CA provider account url.")
    http_response, result = _send_signed_request(acme_config["newAccount"], old_accountkeypath, {
        "onlyReturnExisting": True })
    if http_response.status_code == 200:
        old_jws_header["kid"] = http_response.headers["Location"]
        new_jws_header["kid"] = http_response.headers["Location"]
    else:
        raise ValueError("Error looking or account URL: {0} {1}".format(http_response.status_code, result))

    log.info("Rolls over account key...")
    # The signature by the new key covers the account URL and the old key,
    # signifying a request by the new key holder to take over the account from
    # the old key holder.
    inner_payload = _sign_request(acme_config["keyChange"], new_accountkeypath, {
        "account": old_jws_header["kid"],
        "oldKey": old_jws_header["jwk"] },
        is_inner = True)
    # The signature by the old key covers this request and its signature, and
    # indicates the old key holder's assent to the roll-over request.
    http_response, result  = _send_signed_request(acme_config["keyChange"], old_accountkeypath, inner_payload)

    if http_response.status_code != 200:
        raise ValueError("Error rolling over account key: {0} {1}".format(http_response.status_code, result))
    log.info("Account keys rolled over !")

def main(argv):
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="Tiny ACME client to roll over an ACME account key with another one.",
        epilog="""This script *rolls over* ACME account keys.

It will need to have access to the ACME private account keys, so PLEASE READ THROUGH IT!
It's around 150 lines, so it won't take long.

Example: roll over account key from account.key to newaccount.key:
  python3 acme_account_rollover.py --current account.key --new newaccount.key --acme-directory https://acme-staging-v02.api.letsencrypt.org/directory""")
    parser.add_argument("--current", required = True, help="path to the current private account key")
    parser.add_argument("--new", required = True, help="path to the newer private account key to register")
    parser.add_argument("--acme-directory", required = True, help="ACME directory URL of the ACME server where to remove the key")
    parser.add_argument("--quiet", action="store_const", const=logging.ERROR, help="suppress output except for errors")
    args = parser.parse_args(argv)

    LOGGER.setLevel(args.quiet or logging.INFO)
    account_rollover(args.current, args.new, args.acme_directory, log=LOGGER)

if __name__ == "__main__":  # pragma: no cover
    main(sys.argv[1:])
<commit_msg>account_rollover: adapt script to best practices provided by linter<commit_after>#!/usr/bin/env python3
#pylint: disable=too-many-statements
"""Tiny script to rollover two keys for an ACME account"""
import sys
import argparse
import subprocess
import json
import base64
import binascii
import re
import copy
import logging
import requests

LOGGER = logging.getLogger("acme_account_rollover")
LOGGER.addHandler(logging.StreamHandler())

def _b64(text):
    """"Encodes text as base64 as specified in ACME RFC """
    return base64.urlsafe_b64encode(text).decode("utf8").rstrip("=")

def _openssl(command, options, communicate=None):
    """Run openssl command line and raise IOError on non-zero return."""
    openssl = subprocess.Popen(["openssl", command] + options, stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = openssl.communicate(communicate)
    if openssl.returncode != 0:
        raise IOError("OpenSSL Error: {0}".format(err))
    return out

def account_rollover(old_accountkeypath, new_accountkeypath, acme_directory, log=LOGGER):
    """Rollover the old and new account key for an ACME account."""
    def _get_private_acme_signature(accountkeypath):
        """Read the account key to get the signature to authenticate with the ACME server."""
        accountkey = _openssl("rsa", ["-in", accountkeypath, "-noout", "-text"])
        pub_hex, pub_exp = re.search(
            r"modulus:\r?\n\s+00:([a-f0-9\:\s]+?)\r?\npublicExponent: ([0-9]+)",
            accountkey.decode("utf8"), re.MULTILINE | re.DOTALL).groups()
        pub_exp = "{0:x}".format(int(pub_exp))
        pub_exp = "0{0}".format(pub_exp) if len(pub_exp) % 2 else pub_exp
        return {
            "alg": "RS256",
            "jwk": {
                "e": _b64(binascii.unhexlify(pub_exp.encode("utf-8"))),
                "kty": "RSA",
                "n": _b64(binascii.unhexlify(re.sub(r"(\s|:)", "", pub_hex).encode("utf-8"))),
            },
            "kid": None
        }

    def _sign_request(url, keypath, payload, is_inner=False):
        """Signs request with a specific right account key."""
        nonlocal nonce
        if payload == "": # on POST-as-GET, final payload has to be just empty string
            payload64 = ""
        else:
            payload64 = _b64(json.dumps(payload).encode("utf8"))
        if keypath == new_accountkeypath:
            protected = copy.deepcopy(private_acme_new_signature)
        elif keypath == old_accountkeypath:
            protected = copy.deepcopy(private_acme_old_signature)

        if is_inner or url == acme_config["newAccount"]:
            del protected["kid"]
        else:
            del protected["jwk"]

        if not is_inner:
            protected["nonce"] = (nonce
                                  or requests.get(acme_config["newNonce"])
                                  .headers['Replay-Nonce'])
        protected["url"] = url
        protected64 = _b64(json.dumps(protected).encode("utf8"))
        signature = _openssl("dgst", ["-sha256", "-sign", keypath],
                             "{0}.{1}".format(protected64, payload64).encode("utf8"))
        return {
            "protected": protected64, "payload": payload64, "signature": _b64(signature)
        }

    def _send_signed_request(url, keypath, payload):
        """Sends signed requests to ACME server."""
        nonlocal nonce
        jose = _sign_request(url, keypath, payload)
        joseheaders = {
            'User-Agent': adtheaders.get('User-Agent'),
            'Content-Type': 'application/jose+json'
        }
        try:
            response = requests.post(url, json=jose, headers=joseheaders)
        except requests.exceptions.RequestException as error:
            response = error.response
        finally:
            nonce = response.headers['Replay-Nonce']
        if not response.text:
            return response, json.dumps({})
        return response, response.json()

    # main code
    adtheaders = {'User-Agent': 'acme-dns-tiny/2.0'}
    nonce = None

    log.info("Fetch informations from the ACME directory.")
    acme_config = requests.get(acme_directory, headers=adtheaders).json()

    log.info("Get private signature from old account key.")
    private_acme_old_signature = _get_private_acme_signature(old_accountkeypath)

    log.info("Get private signature from new account key.")
    private_acme_new_signature = _get_private_acme_signature(new_accountkeypath)

    log.info("Ask to the ACME server the account identifier to complete the private signature.")
    http_response, result = _send_signed_request(acme_config["newAccount"], old_accountkeypath, {
        "onlyReturnExisting": True})
    if http_response.status_code == 200:
        private_acme_old_signature["kid"] = http_response.headers["Location"]
        private_acme_new_signature["kid"] = http_response.headers["Location"]
    else:
        raise ValueError("Error looking or account URL: {0} {1}"
                         .format(http_response.status_code, result))

    log.info("Rolling over account keys.")
    # The signature by the new key covers the account URL and the old key,
    # signifying a request by the new key holder to take over the account from
    # the old key holder.
    inner_payload = _sign_request(acme_config["keyChange"], new_accountkeypath, {
        "account": private_acme_old_signature["kid"],
        "oldKey": private_acme_old_signature["jwk"]}, is_inner=True)
    # The signature by the old key covers this request and its signature, and
    # indicates the old key holder's assent to the roll-over request.
    http_response, result = _send_signed_request(acme_config["keyChange"], old_accountkeypath,
                                                 inner_payload)

    if http_response.status_code != 200:
        raise ValueError("Error rolling over account key: {0} {1}"
                         .format(http_response.status_code, result))
    log.info("Keys rolled over.")

def main(argv):
    """Parse arguments and roll over the ACME account keys"""
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="Tiny ACME client to roll over an ACME account key with another one.",
        epilog="""This script *rolls over* ACME account keys.

It will need to have access to the ACME private account keys, so PLEASE READ THROUGH IT!
It's around 150 lines, so it won't take long.

Example: roll over account key from account.key to newaccount.key:
  python3 acme_account_rollover.py --current account.key --new newaccount.key --acme-directory \
https://acme-staging-v02.api.letsencrypt.org/directory""")
    parser.add_argument("--current", required=True,
                        help="path to the current private account key")
    parser.add_argument("--new", required=True,
                        help="path to the newer private account key to register")
    parser.add_argument("--acme-directory", required=True,
                        help="ACME directory URL of the ACME server where to remove the key")
    parser.add_argument("--quiet", action="store_const", const=logging.ERROR,
                        help="suppress output except for errors")
    args = parser.parse_args(argv)

    LOGGER.setLevel(args.quiet or logging.INFO)
    account_rollover(args.current, args.new, args.acme_directory, log=LOGGER)

if __name__ == "__main__":  # pragma: no cover
    main(sys.argv[1:])
<|endoftext|>


<commit_before>#!/usr/bin/env python

from harness import Game

game = Game(title="pysdl2 HARNESS demo", width=240, height=240, zoom=3)
title = game.load_resource("title.png")
font = game.load_bitmap_font("font.png", width=6, height=10)

scenes = []

class MenuScene(object):

    def __init__(self):
        self.counter = 0

    def draw(self, renderer):
        renderer.draw(title)

        if 2 < self.counter < 10:
            renderer.draw_text(font, 120, 120, "Press 's' to start!", align="center")

    def update(self, dt):

        self.counter += dt * 10
        if self.counter > 10:
            self.counter -= 10

        if game.keys[game.KEY_ESCAPE]:
            game.quit()
        elif game.keys[game.KEY_S]:
            # press "s" to play
            scenes.append(PlayScene())
            return

class PlayScene(object):

    def draw(self, renderer):
        pass

    def update(self, dt):
        if game.keys[game.KEY_ESCAPE]:
            # avoid leaving the game just after
            # leaving this scene!
            game.keys[game.KEY_ESCAPE] = False

            # go to previous scene
            scenes.pop()
            return

@game.draw
def draw(renderer):
    # draw last scene
    scenes[-1].draw(renderer)

@game.update
def update(dt):
    # uddate last scene
    scenes[-1].update(dt)

scenes.append(MenuScene())

game.loop()

<commit_msg>start the cycle showing the text<commit_after>#!/usr/bin/env python

from harness import Game

game = Game(title="pysdl2 HARNESS demo", width=240, height=240, zoom=3)
title = game.load_resource("title.png")
font = game.load_bitmap_font("font.png", width=6, height=10)

scenes = []

class MenuScene(object):

    def __init__(self):
        self.counter = 2

    def draw(self, renderer):
        renderer.draw(title)

        if 2 < self.counter < 10:
            renderer.draw_text(font, 120, 120, "Press 's' to start!", align="center")

    def update(self, dt):

        self.counter += dt * 10
        if self.counter > 10:
            self.counter -= 10

        if game.keys[game.KEY_ESCAPE]:
            game.quit()
        elif game.keys[game.KEY_S]:
            # press "s" to play
            scenes.append(PlayScene())
            return

class PlayScene(object):

    def draw(self, renderer):
        pass

    def update(self, dt):
        if game.keys[game.KEY_ESCAPE]:
            # avoid leaving the game just after
            # leaving this scene!
            game.keys[game.KEY_ESCAPE] = False

            # go to previous scene
            scenes.pop()
            return

@game.draw
def draw(renderer):
    # draw last scene
    scenes[-1].draw(renderer)

@game.update
def update(dt):
    # uddate last scene
    scenes[-1].update(dt)

scenes.append(MenuScene())

game.loop()

<|endoftext|>