<commit_before>from __future__ import absolute_import, division, unicode_literals

import logging

from cStringIO import StringIO
from flask.ext.restful import reqparse
from sqlalchemy.orm import joinedload, subqueryload_all
from werkzeug.datastructures import FileStorage

from changes.api.base import APIView
from changes.api.validators.author import AuthorValidator
from changes.config import db
from changes.constants import Status, ProjectStatus
from changes.db.utils import get_or_create
from changes.events import publish_build_update, publish_job_update
from changes.jobs.create_job import create_job
from changes.jobs.sync_build import sync_build
from changes.models import (
    Project, Build, Job, JobPlan, Repository, Patch, ProjectOption,
    ItemOption, Source, ProjectPlan, Revision
)

def identify_revision(repository, treeish):
    """
    Attempt to transform a a commit-like reference into a valid revision.
    """
    # try to find it from the database first
    if len(treeish) == 40:
        revision = Revision.query.filter(Revision.sha == treeish).first()
        if revision:
            return revision

    vcs = repository.get_vcs()
    if not vcs:
        return

    try:
        commit = list(vcs.log(parent=treeish, limit=1))[0]
    except IndexError:
        return
    except Exception:
        logging.exception('Failed to find commit: %s', treeish)
        return

    revision, _ = commit.save(repository)

    return revision

def create_build(project, label, target, message, author, change=None,
                 patch=None, cause=None, source=None, sha=None):
    assert sha or source

    repository = project.repository

    if source is None:
        source, _ = get_or_create(Source, where={
            'repository': repository,
            'patch': patch,
            'revision_sha': sha,
        })

    build = Build(
        project=project,
        project_id=project.id,
        source=source,
        source_id=source.id if source else None,
        repository=repository,
        status=Status.queued,
        author=author,
        author_id=author.id if author else None,
        label=label,
        target=target,
        revision_sha=sha,
        patch=patch,
        message=message,
        cause=cause,
    )

    db.session.add(build)
    db.session.commit()

    execute_build(build=build)

    return build

def execute_build(build):
    # TODO(dcramer): most of this should be abstracted into sync_build as if it
    # were a "im on step 0, create step 1"
    project = build.project

    jobs = []
    for plan in project.plans:
        job = Job(
            build=build,
            build_id=build.id,
            project=project,
            project_id=project.id,
            source=build.source,
            source_id=build.source_id,
            status=build.status,
            label=plan.label,
        )

        db.session.add(job)

        jobplan = JobPlan(
            project=project,
            job=job,
            build=build,
            plan=plan,
        )

        db.session.add(jobplan)

        jobs.append(job)

    db.session.commit()

    publish_build_update(build)

    for job in jobs:
        publish_job_update(job)
        create_job.delay(
            job_id=job.id.hex,
            task_id=job.id.hex,
            parent_task_id=job.build_id.hex,
        )

    db.session.commit()

    sync_build.delay(
        build_id=job.build_id.hex,
        task_id=job.build_id.hex,
    )

    return build

class BuildIndexAPIView(APIView):
    parser = reqparse.RequestParser()
    parser.add_argument('sha', type=str, required=True)
    parser.add_argument('project', type=lambda x: Project.query.filter(
        Project.slug == x,
        Project.status == ProjectStatus.active,
    ).first())
    parser.add_argument('repository', type=lambda x: Repository.query.filter_by(url=x).first())
    parser.add_argument('author', type=AuthorValidator())
    parser.add_argument('label', type=unicode)
    parser.add_argument('target', type=unicode)
    parser.add_argument('message', type=unicode)
    parser.add_argument('patch', type=FileStorage, dest='patch_file', location='files')
    parser.add_argument('patch[label]', type=unicode, dest='patch_label')

    def get(self):
        queryset = Build.query.options(
            joinedload('project'),
            joinedload('author'),
            joinedload('source').joinedload('revision'),
        ).order_by(Build.date_created.desc(), Build.date_started.desc())

        return self.paginate(queryset)

    def post(self):
        args = self.parser.parse_args()

        assert args.project or args.repository

        if args.patch_file and not args.patch_label:
            return '{"error": "Missing patch label"}', 400

        if args.project:
            projects = [args.project]
            repository = Repository.query.get(args.project.repository_id)
        else:
            repository = args.repository
            projects = list(Project.query.options(
                subqueryload_all(Project.project_plans, ProjectPlan.plan),
            ).filter(
                Project.status == ProjectStatus.active,
                Project.repository_id == repository.id,
            ))

        if not projects:
            return '{"error": "Unable to find project(s)."}', 400

        if args.patch_file:
            # eliminate projects without diff builds
            options = dict(
                db.session.query(
                    ProjectOption.project_id, ProjectOption.value
                ).filter(
                    ProjectOption.project_id.in_([p.id for p in projects]),
                    ProjectOption.name.in_([
                        'build.allow-patches',
                    ])
                )
            )

            projects = [
                p for p in projects
                if options.get(p.id, '1') == '1'
            ]

            if not projects:
                return self.respond([])

        label = args.label
        author = args.author
        message = args.message

        revision = identify_revision(repository, args.sha)
        if revision:
            if not author:
                author = revision.author
            if not label:
                label = revision.subject
            # only default the message if its absolutely not set
            if message is None:
                message = revision.message
            sha = revision.sha
        else:
            sha = args.sha

        if not args.target:
            if args.patch_label:
                target = args.patch_label[:128]
            else:
                target = sha[:12]
        else:
            target = args.target[:128]

        if not label:
            if message:
                label = message.splitlines()[0]
            if not label:
                label = 'A homeless build'
        label = label[:128]

        if args.patch_file:
            fp = StringIO()
            for line in args.patch_file:
                fp.write(line)
            patch_file = fp
        else:
            patch_file = None

        builds = []
        for project in projects:
            plan_list = list(project.plans)
            if not plan_list:
                logging.warning('No plans defined for project %s', project.slug)
                continue

            if plan_list and patch_file:
                options = dict(
                    db.session.query(
                        ItemOption.item_id, ItemOption.value
                    ).filter(
                        ItemOption.item_id.in_([p.id for p in plan_list]),
                        ItemOption.name.in_([
                            'build.allow-patches',
                        ])
                    )
                )
                plan_list = [
                    p for p in plan_list
                    if options.get(p.id, '1') == '1'
                ]

                # no plans remained
                if not plan_list:
                    continue

            if patch_file:
                patch = Patch(
                    repository=repository,
                    project=project,
                    parent_revision_sha=args.sha,
                    label=args.patch_label,
                    diff=patch_file.getvalue(),
                )
                db.session.add(patch)
            else:
                patch = None

            builds.append(create_build(
                project=project,
                sha=sha,
                target=target,
                label=label,
                message=message,
                author=author,
                patch=patch,
            ))

        return self.respond(builds)

    def get_stream_channels(self):
        return ['builds:*']
<commit_msg>Fallback to HEAD/tip when revision isnt found<commit_after>from __future__ import absolute_import, division, unicode_literals

import logging

from cStringIO import StringIO
from flask.ext.restful import reqparse
from sqlalchemy.orm import joinedload, subqueryload_all
from werkzeug.datastructures import FileStorage

from changes.api.base import APIView
from changes.api.validators.author import AuthorValidator
from changes.config import db
from changes.constants import Status, ProjectStatus
from changes.db.utils import get_or_create
from changes.events import publish_build_update, publish_job_update
from changes.jobs.create_job import create_job
from changes.jobs.sync_build import sync_build
from changes.models import (
    Project, Build, Job, JobPlan, Repository, Patch, ProjectOption,
    ItemOption, Source, ProjectPlan, Revision
)

def identify_revision(repository, treeish):
    """
    Attempt to transform a a commit-like reference into a valid revision.
    """
    # try to find it from the database first
    if len(treeish) == 40:
        revision = Revision.query.filter(Revision.sha == treeish).first()
        if revision:
            return revision

    vcs = repository.get_vcs()
    if not vcs:
        return

    try:
        commit = list(vcs.log(parent=treeish, limit=1))[0]
    except IndexError:
        # fall back to HEAD/tip when a matching revision isn't found
        # this case happens frequently with gateways like hg-git
        # TODO(dcramer): it's possible to DOS the endpoint by passing invalid
        # commits so we should really cache the failed lookups
        try:
            commit = list(vcs.log(limit=1))[0]
        except Exception:
            logging.exception('Failed to find commit: %s', treeish)
            return
    except Exception:
        logging.exception('Failed to find commit: %s', treeish)
        return

    revision, _ = commit.save(repository)

    return revision

def create_build(project, label, target, message, author, change=None,
                 patch=None, cause=None, source=None, sha=None):
    assert sha or source

    repository = project.repository

    if source is None:
        source, _ = get_or_create(Source, where={
            'repository': repository,
            'patch': patch,
            'revision_sha': sha,
        })

    build = Build(
        project=project,
        project_id=project.id,
        source=source,
        source_id=source.id if source else None,
        repository=repository,
        status=Status.queued,
        author=author,
        author_id=author.id if author else None,
        label=label,
        target=target,
        revision_sha=sha,
        patch=patch,
        message=message,
        cause=cause,
    )

    db.session.add(build)
    db.session.commit()

    execute_build(build=build)

    return build

def execute_build(build):
    # TODO(dcramer): most of this should be abstracted into sync_build as if it
    # were a "im on step 0, create step 1"
    project = build.project

    jobs = []
    for plan in project.plans:
        job = Job(
            build=build,
            build_id=build.id,
            project=project,
            project_id=project.id,
            source=build.source,
            source_id=build.source_id,
            status=build.status,
            label=plan.label,
        )

        db.session.add(job)

        jobplan = JobPlan(
            project=project,
            job=job,
            build=build,
            plan=plan,
        )

        db.session.add(jobplan)

        jobs.append(job)

    db.session.commit()

    publish_build_update(build)

    for job in jobs:
        publish_job_update(job)
        create_job.delay(
            job_id=job.id.hex,
            task_id=job.id.hex,
            parent_task_id=job.build_id.hex,
        )

    db.session.commit()

    sync_build.delay(
        build_id=job.build_id.hex,
        task_id=job.build_id.hex,
    )

    return build

class BuildIndexAPIView(APIView):
    parser = reqparse.RequestParser()
    parser.add_argument('sha', type=str, required=True)
    parser.add_argument('project', type=lambda x: Project.query.filter(
        Project.slug == x,
        Project.status == ProjectStatus.active,
    ).first())
    parser.add_argument('repository', type=lambda x: Repository.query.filter_by(url=x).first())
    parser.add_argument('author', type=AuthorValidator())
    parser.add_argument('label', type=unicode)
    parser.add_argument('target', type=unicode)
    parser.add_argument('message', type=unicode)
    parser.add_argument('patch', type=FileStorage, dest='patch_file', location='files')
    parser.add_argument('patch[label]', type=unicode, dest='patch_label')

    def get(self):
        queryset = Build.query.options(
            joinedload('project'),
            joinedload('author'),
            joinedload('source').joinedload('revision'),
        ).order_by(Build.date_created.desc(), Build.date_started.desc())

        return self.paginate(queryset)

    def post(self):
        args = self.parser.parse_args()

        assert args.project or args.repository

        if args.patch_file and not args.patch_label:
            return '{"error": "Missing patch label"}', 400

        if args.project:
            projects = [args.project]
            repository = Repository.query.get(args.project.repository_id)
        else:
            repository = args.repository
            projects = list(Project.query.options(
                subqueryload_all(Project.project_plans, ProjectPlan.plan),
            ).filter(
                Project.status == ProjectStatus.active,
                Project.repository_id == repository.id,
            ))

        if not projects:
            return '{"error": "Unable to find project(s)."}', 400

        if args.patch_file:
            # eliminate projects without diff builds
            options = dict(
                db.session.query(
                    ProjectOption.project_id, ProjectOption.value
                ).filter(
                    ProjectOption.project_id.in_([p.id for p in projects]),
                    ProjectOption.name.in_([
                        'build.allow-patches',
                    ])
                )
            )

            projects = [
                p for p in projects
                if options.get(p.id, '1') == '1'
            ]

            if not projects:
                return self.respond([])

        label = args.label
        author = args.author
        message = args.message

        revision = identify_revision(repository, args.sha)
        if revision:
            if not author:
                author = revision.author
            if not label:
                label = revision.subject
            # only default the message if its absolutely not set
            if message is None:
                message = revision.message
            sha = revision.sha
        else:
            sha = args.sha

        if not args.target:
            if args.patch_label:
                target = args.patch_label[:128]
            else:
                target = sha[:12]
        else:
            target = args.target[:128]

        if not label:
            if message:
                label = message.splitlines()[0]
            if not label:
                label = 'A homeless build'
        label = label[:128]

        if args.patch_file:
            fp = StringIO()
            for line in args.patch_file:
                fp.write(line)
            patch_file = fp
        else:
            patch_file = None

        builds = []
        for project in projects:
            plan_list = list(project.plans)
            if not plan_list:
                logging.warning('No plans defined for project %s', project.slug)
                continue

            if plan_list and patch_file:
                options = dict(
                    db.session.query(
                        ItemOption.item_id, ItemOption.value
                    ).filter(
                        ItemOption.item_id.in_([p.id for p in plan_list]),
                        ItemOption.name.in_([
                            'build.allow-patches',
                        ])
                    )
                )
                plan_list = [
                    p for p in plan_list
                    if options.get(p.id, '1') == '1'
                ]

                # no plans remained
                if not plan_list:
                    continue

            if patch_file:
                patch = Patch(
                    repository=repository,
                    project=project,
                    parent_revision_sha=args.sha,
                    label=args.patch_label,
                    diff=patch_file.getvalue(),
                )
                db.session.add(patch)
            else:
                patch = None

            builds.append(create_build(
                project=project,
                sha=sha,
                target=target,
                label=label,
                message=message,
                author=author,
                patch=patch,
            ))

        return self.respond(builds)

    def get_stream_channels(self):
        return ['builds:*']
<|endoftext|>


<commit_before>import numpy as np
import pytest

import mbuild as mb
from mbuild.tests.base_test import BaseTest
from mbuild.utils.io import get_fn
from mbuild.formats.lammpsdata import write_lammpsdata
from mbuild.utils.io import has_foyer

@pytest.mark.skipif(not has_foyer, reason="Foyer package not installed")
class TestLammpsData(BaseTest):

    def test_save(self, ethane):
        ethane.save(filename='ethane.lammps')

    def test_save_forcefield(self, ethane):
        ethane.save(filename='ethane-opls.lammps', forcefield_name='oplsaa')

    @pytest.mark.skipif(not has_foyer, reason="Foyer package not installed")
    def test_save_charmm(self):
        cmpd = mb.load(get_fn('charmm_dihedral.mol2'))
        for i in cmpd.particles():
            i.name = "_{}".format(i.name)
        structure = cmpd.to_parmed(box=cmpd.boundingbox,
                                    residues=set([p.parent.name for \
                                                 p in cmpd.particles()]))

        from foyer import Forcefield
        ff = Forcefield(forcefield_files=[get_fn('charmm_truncated.xml')])
        structure = ff.apply(structure, assert_dihedral_params=False)

        from mbuild.formats.lammpsdata import write_lammpsdata
        write_lammpsdata(structure, 'charmm_dihedral.lammps')
        out_lammps = open('charmm_dihedral.lammps', 'r').readlines()
        for i, line in enumerate(out_lammps):
            if 'Angle Coeffs' in line:
                assert '# charmm' in line
                assert '#\tk(kcal/mol/rad^2)\t\ttheteq(deg)\tk(kcal/mol/angstrom^2)\treq(angstrom)\n' in out_lammps[i+1]
                assert len(out_lammps[i+2].split('#')[0].split()) == 5
            elif 'Dihedral Coeffs' in line:
                assert '# charmm' in line
                assert '#k, n, phi, weight' in out_lammps[i+1]
                assert len(out_lammps[i+2].split('#')[0].split()) == 5
            else:
                pass

    @pytest.mark.skipif(not has_foyer, reason="Foyer package not installed")
    def test_save_box(self, ethane):
        box = mb.Box(lengths=np.array([2.0, 2.0, 2.0]))
        ethane.save(filename='ethane-box.lammps', forcefield_name='oplsaa', box=box)

    def test_nbfix(self, ethane):
        from foyer import Forcefield

        OPLSAA = Forcefield(name='oplsaa')
        structure = OPLSAA.apply(ethane)
        # Add nbfixes
        types = list(set([a.atom_type for a in structure.atoms]))
        types[0].add_nbfix(types[1].name, 1.2, 2.1)
        types[1].add_nbfix(types[0].name, 1.2, 2.1)
        write_lammpsdata(filename='nbfix.lammps', structure=structure)

        checked_section = False
        with open('nbfix.lammps', 'r') as fi:
            while not checked_section:
                line = fi.readline()
                if 'PairIJ Coeffs' in line:
                    fi.readline()
                    line = fi.readline().partition('#')[0]
                    assert np.allclose(
                        np.asarray(line.split(), dtype=float),
                        [1, 1, 0.066, 3.5])
                    line = fi.readline().partition('#')[0]
                    assert np.allclose(
                        np.asarray(line.split(), dtype=float),
                        [1, 2, 2.1, 1.06907846])
                    line = fi.readline().partition('#')[0]
                    assert np.allclose(
                        np.asarray(line.split(), dtype=float),
                        [2, 2, 0.03, 2.5])
                    line = fi.readline()
                    checked_section = True
                # Break if PairIJ Coeffs is not found
                if 'Atoms' in line:
                    break

    def test_save_triclinic_box(self, ethane):
        box = mb.Box(lengths=np.array([2.0, 2.0, 2.0]), angles=[60, 70, 80])
        ethane.save(filename='triclinic-box.lammps', forcefield_name='oplsaa', box=box)

    @pytest.mark.parametrize(
        'atom_style, n_columns',
        [('full', 7), ('atomic', 5), ('molecular', 6), ('charge', 6)]
    )
    def test_writing_atom_styles(self, ethane, atom_style, n_columns):
        ethane.save(filename='ethane.lammps', atom_style=atom_style)
        with open('ethane.lammps', 'r') as f:
            for line in f:
                if "Atoms" not in line:
                    continue
                    atoms_header = next(f)
                    first_atom_line = next(f)
                    columns = first_atom_line.split("\t")
                    assert len(columns) == n_columns

    def test_resid(self, ethane, methane):
        structure = ethane.to_parmed() + methane.to_parmed()
        n_atoms = len(structure.atoms)
        write_lammpsdata(structure, 'compound.lammps')
        #structure.save(filename='compound.lammps')
        res_list = list()
        with open('compound.lammps', 'r') as f:
            for line in f:
                if 'Atoms' in line:
                    f.readline()
                    for i in range(n_atoms):
                        res_list.append(f.readline().rstrip().split()[1])

        assert set(res_list) == set(['1', '0'])
<commit_msg>Rewrite resid test to prevent messy line iteration<commit_after>import numpy as np
import pytest

import mbuild as mb
from mbuild.tests.base_test import BaseTest
from mbuild.utils.io import get_fn
from mbuild.formats.lammpsdata import write_lammpsdata
from mbuild.utils.io import has_foyer

@pytest.mark.skipif(not has_foyer, reason="Foyer package not installed")
class TestLammpsData(BaseTest):

    def test_save(self, ethane):
        ethane.save(filename='ethane.lammps')

    def test_save_forcefield(self, ethane):
        ethane.save(filename='ethane-opls.lammps', forcefield_name='oplsaa')

    @pytest.mark.skipif(not has_foyer, reason="Foyer package not installed")
    def test_save_charmm(self):
        cmpd = mb.load(get_fn('charmm_dihedral.mol2'))
        for i in cmpd.particles():
            i.name = "_{}".format(i.name)
        structure = cmpd.to_parmed(box=cmpd.boundingbox,
                                    residues=set([p.parent.name for \
                                                 p in cmpd.particles()]))

        from foyer import Forcefield
        ff = Forcefield(forcefield_files=[get_fn('charmm_truncated.xml')])
        structure = ff.apply(structure, assert_dihedral_params=False)

        from mbuild.formats.lammpsdata import write_lammpsdata
        write_lammpsdata(structure, 'charmm_dihedral.lammps')
        out_lammps = open('charmm_dihedral.lammps', 'r').readlines()
        for i, line in enumerate(out_lammps):
            if 'Angle Coeffs' in line:
                assert '# charmm' in line
                assert '#\tk(kcal/mol/rad^2)\t\ttheteq(deg)\tk(kcal/mol/angstrom^2)\treq(angstrom)\n' in out_lammps[i+1]
                assert len(out_lammps[i+2].split('#')[0].split()) == 5
            elif 'Dihedral Coeffs' in line:
                assert '# charmm' in line
                assert '#k, n, phi, weight' in out_lammps[i+1]
                assert len(out_lammps[i+2].split('#')[0].split()) == 5
            else:
                pass

    @pytest.mark.skipif(not has_foyer, reason="Foyer package not installed")
    def test_save_box(self, ethane):
        box = mb.Box(lengths=np.array([2.0, 2.0, 2.0]))
        ethane.save(filename='ethane-box.lammps', forcefield_name='oplsaa', box=box)

    def test_nbfix(self, ethane):
        from foyer import Forcefield

        OPLSAA = Forcefield(name='oplsaa')
        structure = OPLSAA.apply(ethane)
        # Add nbfixes
        types = list(set([a.atom_type for a in structure.atoms]))
        types[0].add_nbfix(types[1].name, 1.2, 2.1)
        types[1].add_nbfix(types[0].name, 1.2, 2.1)
        write_lammpsdata(filename='nbfix.lammps', structure=structure)

        checked_section = False
        with open('nbfix.lammps', 'r') as fi:
            while not checked_section:
                line = fi.readline()
                if 'PairIJ Coeffs' in line:
                    fi.readline()
                    line = fi.readline().partition('#')[0]
                    assert np.allclose(
                        np.asarray(line.split(), dtype=float),
                        [1, 1, 0.066, 3.5])
                    line = fi.readline().partition('#')[0]
                    assert np.allclose(
                        np.asarray(line.split(), dtype=float),
                        [1, 2, 2.1, 1.06907846])
                    line = fi.readline().partition('#')[0]
                    assert np.allclose(
                        np.asarray(line.split(), dtype=float),
                        [2, 2, 0.03, 2.5])
                    line = fi.readline()
                    checked_section = True
                # Break if PairIJ Coeffs is not found
                if 'Atoms' in line:
                    break

    def test_save_triclinic_box(self, ethane):
        box = mb.Box(lengths=np.array([2.0, 2.0, 2.0]), angles=[60, 70, 80])
        ethane.save(filename='triclinic-box.lammps', forcefield_name='oplsaa', box=box)

    @pytest.mark.parametrize(
        'atom_style, n_columns',
        [('full', 7), ('atomic', 5), ('molecular', 6), ('charge', 6)]
    )
    def test_writing_atom_styles(self, ethane, atom_style, n_columns):
        ethane.save(filename='ethane.lammps', atom_style=atom_style)
        with open('ethane.lammps', 'r') as f:
            for line in f:
                if "Atoms" not in line:
                    continue
                    atoms_header = next(f)
                    first_atom_line = next(f)
                    columns = first_atom_line.split("\t")
                    assert len(columns) == n_columns

    def test_resid(self, ethane, methane):
        structure = ethane.to_parmed() + methane.to_parmed()
        n_atoms = len(structure.atoms)
        write_lammpsdata(structure, 'compound.lammps')
        res_list = list()
        with open('compound.lammps', 'r') as f:
            for i,line in enumerate(f):
                if 'Atoms' in line:
                    break
        atom_lines = open('compound.lammps', 'r').readlines()[i+2:i+n_atoms+2]
        for line in atom_lines:
            res_list.append(line.rstrip().split()[1])

        assert set(res_list) == set(['1', '0'])
<|endoftext|>


<commit_before>"""
replace_cert.py

Simple certificate replacement for load balancers and EBS configurations.

Be careful if two certs have the same name but different paths!!!!

THIS SCRIPT DOES NOT SUPPORT CHANGING PATHS!!!!!!!!!

"""

import boto3, sys, re

def verify_certs_exist(source_cert, dest_cert):
    iam_client = boto3.client('iam')

    #Get server certificates
    certs = iam_client.list_server_certificates()
    source_exists = False
    dest_exists = False

    #Split cert name and take last element (the name of the cert), and see if it matches
    for item in certs['ServerCertificateMetadataList']:
        if item['ServerCertificateName'].split("/")[-1] == source_cert:
            source_exists = True
        if item['ServerCertificateName'].split("/")[-1] == dest_cert:
            dest_exists = True

    #Notify if they exist or error and exit if they dont
    if source_exists:
        print("Source certificate " + source_cert + " exists.")
    else:
        print("ERROR: Unable to found source certificate: " + source_cert)
        sys.exit(1)

    if dest_exists:
        print("Destination certificate " + dest_cert + " exists.")
    else:
        print("ERROR: Unable to found destination certificate: " + dest_cert)
        sys.exit(1)

def main(kwargs, args):

    #TODO: Change to false before production
    dry_run = True
    if "dry-run" in kwargs or "n" in kwargs:
        print("Dry run")
        dry_run = True

    #Assume the cert names are the only two "arguments" (argument is something that doesnt start with '-')
    try:
        source_cert = args[0]
        dest_cert = args[1]
    except IndexError:
        print("You must specify a source cert name (from) and a destination cert name (to)")
        sys.exit(1)

    #Verify certs exist
    verify_certs_exist(source_cert, dest_cert)

    #Get list of load balancers
    elb_client = boto3.client('elb')
    load_balancer_list = elb_client.describe_load_balancers()
    ebs_list = list()

    for lb in load_balancer_list["LoadBalancerDescriptions"]:

        #Describe tags takes a list with each load balancer name you want to look up
        lb_name = list()
        lb_name.append(lb["LoadBalancerName"])

        #Returns a dictionary
        response = elb_client.describe_tags(LoadBalancerNames=lb_name)

        #We need the TagDescriptions value, which is a list with one element (since we only passed in one name), and we need the Tags value
        tags = response["TagDescriptions"][0]["Tags"]

        #Drill down and make sure the listener is HTTPS protocol, make sure source_cert is part of the SSLCertificateId string, and replace that part with dest_cert
        for listener in lb["ListenerDescriptions"]:
            if listener["Listener"]["Protocol"] == "HTTPS":
                if source_cert in listener["Listener"]["SSLCertificateId"]:
                    print("Replacing " + source_cert + " with " + dest_cert + " in " + lb_name[0])
                    replaced_string = re.sub(source_cert, dest_cert, listener["Listener"]["SSLCertificateId"])
                    if not dry_run:
                        print "changes!!!!!"
                    #Try to get the ebs name, if there is one for this load balancer
                    try:
                        print str(tags)
                        ebs_list.append(tags[0]["elasticbeanstalk:environment-name"])
                    except KeyError:
                        pass

#Modified From maestro
def parse_sysargs():
    current_key = None
    kwargs = dict()
    args = list()
    try:
        for arg in sys.argv[1:]:
            if current_key is None:
                if arg.startswith('-'):
                    current_key = arg.lstrip('-')
                else:
                    args.append(arg)
                continue
            if current_key is not None and not arg.startswith("-"):
                args.append(arg)
            else:
                kwargs[current_key] = None
                current_key = arg.lstrip('-')
        if current_key is not None and current_key not in kwargs:
            kwargs[current_key] = None
            current_key = None
    except IndexError:
        args.append(sys.argv[1])

    return kwargs, args

if __name__ == "__main__":
    kwargs, args = parse_sysargs()
    main(kwargs, args)
<commit_msg>Added guard, fixed up logic, fixed name determination<commit_after>"""
replace_cert.py

Simple certificate replacement for load balancers and EBS configurations.

Be careful if two certs have the same name but different paths!!!!

THIS SCRIPT DOES NOT SUPPORT CHANGING PATHS!!!!!!!!!

"""

import boto3, sys, re

def verify_certs_exist(source_cert, dest_cert):
    iam_client = boto3.client('iam')

    #Get server certificates
    certs = iam_client.list_server_certificates()
    source_exists = False
    dest_exists = False

    #Split cert name and take last element (the name of the cert), and see if it matches
    for item in certs['ServerCertificateMetadataList']:
        if item['ServerCertificateName'].split("/")[-1] == source_cert:
            source_exists = True
        if item['ServerCertificateName'].split("/")[-1] == dest_cert:
            dest_exists = True

    #Notify if they exist or error and exit if they dont
    if source_exists:
        print("Source certificate " + source_cert + " exists.")
    else:
        print("ERROR: Unable to found source certificate: " + source_cert)
        sys.exit(1)

    if dest_exists:
        print("Destination certificate " + dest_cert + " exists.")
    else:
        print("ERROR: Unable to found destination certificate: " + dest_cert)
        sys.exit(1)

def main(kwargs, args):

    #TODO: Change to false before production
    dry_run = False
    if "dry-run" in kwargs or "n" in kwargs:
        print("Dry run")
        dry_run = True

    #Assume the cert names are the only two "arguments" (argument is something that doesnt start with '-')
    try:
        source_cert = args[0]
        dest_cert = args[1]
    except IndexError:
        print("You must specify a source cert name (from) and a destination cert name (to)")
        sys.exit(1)

    #Verify certs exist
    verify_certs_exist(source_cert, dest_cert)

    if not dry_run:
        print("!!!WARNING!!!")
        print("This script could make very dangerous changes, and it's recommended to run it with a --dry-run (-n) to see what it will change before it does it.")
        response = raw_input("Please respond 'yes' if you understand the consequences: ")
        if response != "yes":
            print("Aborting changes.")
            sys.exit(1)

    #Get list of load balancers
    elb_client = boto3.client('elb')
    load_balancer_list = elb_client.describe_load_balancers()
    ebs_list = list()

    for lb in load_balancer_list["LoadBalancerDescriptions"]:

        #Describe tags takes a list with each load balancer name you want to look up
        lb_name = list()
        lb_name.append(lb["LoadBalancerName"])

        #Returns a dictionary
        response = elb_client.describe_tags(LoadBalancerNames=lb_name)

        #We need the TagDescriptions value, which is a list with one element (since we only passed in one name), and we need the Tags value
        tags = response["TagDescriptions"][0]["Tags"]
        name = None
        ebs_name = None
        stack_name = None

        for tag in tags:
            if tag["Key"] == "Name":
                name = tag["Value"]
            if tag["Key"] == "elasticbeanstalk:environment-name":
                ebs_name = tag["Value"]
            if tag["Key"] == "aws:cloudformation:stack-name":
                stack_name = tag["Value"]

        if name is None:
            if ebs_name is not None:
                name = ebs_name
            else:
                name = stack_name

        #Drill down and make sure the listener is HTTPS protocol, make sure source_cert is part of the SSLCertificateId string, and replace that part with dest_cert
        for listener in lb["ListenerDescriptions"]:
            if listener["Listener"]["Protocol"] == "HTTPS":
                if source_cert == listener["Listener"]["SSLCertificateId"].split("/")[-1]:
                    replaced_string = re.sub(source_cert, dest_cert, listener["Listener"]["SSLCertificateId"])
                    print("Replacing " + listener["Listener"]["SSLCertificateId"] + " with " + replaced_string + " in loadbalancer: " + str(name))
                    if not dry_run:
                        pass
                    #Try to get the ebs name, if there is one for this load balancer
                    if ebs_name is not None:
                        ebs_list.append(ebs_name)

    for bs in ebs_list:
        print "Replacing " + source_cert + " with " + dest_cert + " in beanstalk: " + bs

#Modified From maestro
def parse_sysargs():
    current_key = None
    kwargs = dict()
    args = list()
    try:
        for arg in sys.argv[1:]:
            if current_key is None:
                if arg.startswith('-'):
                    current_key = arg.lstrip('-')
                else:
                    args.append(arg)
                continue
            if current_key is not None and not arg.startswith("-"):
                args.append(arg)
            else:
                kwargs[current_key] = None
                current_key = arg.lstrip('-')
        if current_key is not None and current_key not in kwargs:
            kwargs[current_key] = None
            current_key = None
    except IndexError:
        args.append(sys.argv[1])

    return kwargs, args

if __name__ == "__main__":
    kwargs, args = parse_sysargs()
    main(kwargs, args)
<|endoftext|>


<commit_before>import sys, pygame, math, random
from Level import *
from Player import *
from NPC import *
from Menu import *
pygame.init()

clock = pygame.time.Clock()

width = 960
height = 672
size = width, height

bgColor = r,b,g = 0,0,0

screen = pygame.display.set_mode(size)

mode = "game"

while True:
    while mode == "game":
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    mode = "continue"
                if event.key == pygame.K_2:
                    mode = "start"
                if event.key == pygame.K_3:
                    mode = "how to play"
                if event.key == pygame.K_q:
                    mode = "quit"

        screen.fill(bgColor)
        pygame.display.flip()
        clock.tick(60)

    while mode == "continue":

    while mode == "start":

    while mode == "how to play":

    while mode == "quit":
<commit_msg>mode updates to start and how to play<commit_after>import sys, pygame, math, random
from Level import *
from Player import *
from NPC import *
from Menu import *
pygame.init()

clock = pygame.time.Clock()

width = 960
height = 672
size = width, height

bgColor = r,b,g = 0,0,0

screen = pygame.display.set_mode(size)

mode = "game"

while True:
    while mode == "game":
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    mode = "start"
                if event.key == pygame.K_2:
                    mode = "how to play"
                if event.key == pygame.K_q:
                    mode = "quit"

        screen.fill(bgColor)
        pygame.display.flip()
        clock.tick(60)

    while mode == "start":
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    player.go("up")
                elif event.key == pygame.K_s:
                    player.go("down")
                elif event.key == pygame.K_a:
                    player.go("left")
                elif event.key == pygame.K_d:
                    player.go("right")

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_w:
                    player.go("stop up")
                elif event.key == pygame.K_s:
                    player.go("stop down")
                elif event.key == pygame.K_a:
                    player.go("stop left")
                elif event.key == pygame.K_d:
                    player.go("stop right")

    while mode == "how to play":
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

    while mode == "quit":
<|endoftext|>


<commit_before>from django.contrib.auth.models import User
from django.core.urlresolvers import reverse_lazy
from django.db import models

from common import fields
from data_import.models import BaseDataFile, DataRetrievalTask

from ..models import BaseStudyUserData

class UserData(BaseStudyUserData):
    """
    Represents the user data for one PGP participant.
    """

    user = fields.AutoOneToOneField(User, related_name='pgp')

    text_name = 'PGP Harvard'
    href_connect = 'https://my.pgp-hms.org/open_humans/participate'
    href_add_data = 'https://my.pgp-hms.org/open_humans/participate'
    href_learn = 'http://www.personalgenomes.org/harvard/'
    retrieval_url = reverse_lazy('studies:pgp:request-data-retrieval')
    msg_add_data = ("We don't have your PGP Harvard identifier (huID). "
                    'You can add this through the PGP Harvard website.')

    def get_retrieval_params(self):
        # TODO: We assume a single huID.
        # If true, change HuID.user_data to OneToOne?
        # If false, change data processing?
        return {
            'huID': HuId.objects.filter(user_data=self)[0].value,
        }

    @property
    def has_key_data(self):
        """
        Return false if key data needed for data retrieval is not present.
        """
        connected = self.is_connected
        if connected:
            try:
                HuId.objects.get(user_data=self)
                return True
            except HuId.DoesNotExist:
                return False
        return False

class HuId(models.Model):
    """
    A PGP huID (human ID).
    """
    user_data = models.ForeignKey(UserData, related_name='huids')

    value = models.CharField(primary_key=True, max_length=64)

class DataFile(BaseDataFile):
    """
    Storage for an PGP data file.
    """
    user_data = models.ForeignKey(UserData)
    task = models.ForeignKey(DataRetrievalTask, related_name='datafile_pgp')

    def __unicode__(self):
        return '%s:%s:%s' % (self.user_data.user, 'pgp', self.file)
<commit_msg>🌹 fix typo<commit_after>from django.contrib.auth.models import User
from django.core.urlresolvers import reverse_lazy
from django.db import models

from common import fields
from data_import.models import BaseDataFile, DataRetrievalTask

from ..models import BaseStudyUserData

class UserData(BaseStudyUserData):
    """
    Represents the user data for one PGP participant.
    """

    user = fields.AutoOneToOneField(User, related_name='pgp')

    text_name = 'PGP Harvard'
    href_connect = 'https://my.pgp-hms.org/open_humans/participate'
    href_add_data = 'https://my.pgp-hms.org/open_humans/participate'
    href_learn = 'http://www.personalgenomes.org/harvard/'
    retrieval_url = reverse_lazy('studies:pgp:request-data-retrieval')
    msg_add_data = ("We don't have your PGP Harvard identifier (huID). "
                    'You can add this through the PGP Harvard website.')

    def get_retrieval_params(self):
        # TODO: We assume a single huID.
        # If true, change HuID.user_data to OneToOne?
        # If false, change data processing?
        return {
            'huID': HuId.objects.filter(user_data=self)[0].value,
        }

    @property
    def has_key_data(self):
        """
        Return false if key data needed for data retrieval is not present.
        """
        connected = self.is_connected
        if connected:
            try:
                HuId.objects.get(user_data=self)
                return True
            except HuId.DoesNotExist:
                return False
        return False

class HuId(models.Model):
    """
    A PGP huID (human ID).
    """
    user_data = models.ForeignKey(UserData, related_name='huids')

    value = models.CharField(primary_key=True, max_length=64)

class DataFile(BaseDataFile):
    """
    Storage for a PGP data file.
    """
    user_data = models.ForeignKey(UserData)
    task = models.ForeignKey(DataRetrievalTask, related_name='datafile_pgp')

    def __unicode__(self):
        return '%s:%s:%s' % (self.user_data.user, 'pgp', self.file)
<|endoftext|>


<commit_before>#!/usr/bin/python

import os
from collections import namedtuple

import yaml
from click import command, confirm, echo, option, prompt, secho
from pocket import Pocket

from rules import DEFAULT_RULES, compile_rules

PocketItem = namedtuple('PocketItem', ['id', 'url', 'tags', 'title'])

def save_config(path, cfg_dict):
    with open(path, 'w') as fp:
        fp.write(yaml.dump(cfg_dict))
    return True

def load_config(path):
    try:
        with open(path, 'r') as fp:
            return yaml.load(fp.read())
    except IOError:
        return {}

def get_consumer_key():
    return prompt('Please enter your Pocket consumer key')

def get_access_token(consumer_key):
    request_token = Pocket.get_request_token(
        consumer_key=consumer_key,
        redirect_uri='localhost',
    )
    auth_url = Pocket.get_auth_url(
        code=request_token,
        redirect_uri='localhost',
    )

    echo('Please, open this URL in your browser: {}'.format(auth_url))
    if confirm('Did you went to that link?'):
        echo('Getting credentials...')
        credentials = Pocket.get_credentials(
            consumer_key=consumer_key,
            code=request_token,
        )
        return credentials['access_token']

def retrieve_items(pocket, count=10, sort=None, full=True):
    call_args = dict(sort=sort or 'newest')
    if full:
        call_args['detailType'] = 'complete'
    if count:
        call_args['count'] = count

    returned_items = pocket.get(**call_args)[0]['list']
    for item_id, resp_item in returned_items.iteritems():
        yield PocketItem(
            item_id,
            resp_item['resolved_url'],
            resp_item.get('tags', {}).keys(),
            resp_item['resolved_title']
        )

def print_item(item, suggested_tags):
    secho(u'Title:\t', fg='cyan', nl=False)
    echo(item.title)
    secho('URL:\t', fg='cyan', nl=False)
    echo(item.url)
    if item.tags:
        secho('Existing tags:\t', fg='cyan', nl=False)
        echo(', '.join(item.tags))
    secho('Added tags:\t', fg='cyan', nl=False)
    echo(', '.join(suggested_tags))
    echo()

@command()
@option('--count', default=25, help='Number of items to process.')
@option('--dry-run', is_flag=True)
@option('-a', '--process-all', is_flag=True)
def processor(count, process_all, dry_run):
    cfg_path = os.path.join(
        os.path.expanduser('~'),
        '.repocket.yml',
    )
    cfg = load_config(cfg_path)
    creds = cfg.get('credentials', {})
    consumer_key = creds.get('consumer_key')
    access_token = creds.get('access_token')

    if not consumer_key or not access_token:
        consumer_key = get_consumer_key()
        access_token = get_access_token(consumer_key)
        cfg['credentials'] = {
            'consumer_key': str(consumer_key),
            'access_token': str(access_token),
        }

    secho('Your consumer key: ', fg='cyan', nl=False)
    secho(consumer_key)
    secho('Your access token: ', fg='cyan', nl=False)
    secho(access_token)
    echo()

    api_connector = Pocket(consumer_key, access_token)
    cfg.setdefault('rules', DEFAULT_RULES)
    rules = compile_rules(cfg['rules'])
    save_config(cfg_path, cfg)

    secho('Processing items...', fg='cyan')
    modified_items = []
    items = retrieve_items(api_connector, count=process_all and 0 or count)
    for item in items:
        suggested_for_item = set()
        for rule in rules:
            tags = rule.suggest_tags(item)
            suggested_for_item.update(tags or [])
            new_tags = suggested_for_item - set(item.tags)
            if new_tags:
                modified_items.append((item, new_tags))

    if modified_items:
        echo()
        for saved_item, suggested_tags in modified_items:
            print_item(saved_item, suggested_tags)
            api_connector.tags_add(item.id, ','.join(list(new_tags)))

        if not dry_run:
            api_connector.commit()
            secho('Changes are sent to server.', fg='green')
        else:
            secho('"Dry run", no changes are sent to server.', fg='yellow')

if __name__ == '__main__':
    processor()
<commit_msg>Silly bug fix.<commit_after>#!/usr/bin/python

import os
from collections import namedtuple

import yaml
from click import command, confirm, echo, option, prompt, secho
from pocket import Pocket

from rules import DEFAULT_RULES, compile_rules

PocketItem = namedtuple('PocketItem', ['id', 'url', 'tags', 'title'])

def save_config(path, cfg_dict):
    with open(path, 'w') as fp:
        fp.write(yaml.dump(cfg_dict))
    return True

def load_config(path):
    try:
        with open(path, 'r') as fp:
            return yaml.load(fp.read())
    except IOError:
        return {}

def get_consumer_key():
    return prompt('Please enter your Pocket consumer key')

def get_access_token(consumer_key):
    request_token = Pocket.get_request_token(
        consumer_key=consumer_key,
        redirect_uri='localhost',
    )
    auth_url = Pocket.get_auth_url(
        code=request_token,
        redirect_uri='localhost',
    )

    echo('Please, open this URL in your browser: {}'.format(auth_url))
    if confirm('Did you went to that link?'):
        echo('Getting credentials...')
        credentials = Pocket.get_credentials(
            consumer_key=consumer_key,
            code=request_token,
        )
        return credentials['access_token']

def retrieve_items(pocket, count=10, sort=None, full=True):
    call_args = dict(sort=sort or 'newest')
    if full:
        call_args['detailType'] = 'complete'
    if count:
        call_args['count'] = count

    returned_items = pocket.get(**call_args)[0]['list']
    for item_id, resp_item in returned_items.iteritems():
        yield PocketItem(
            item_id,
            resp_item['resolved_url'],
            resp_item.get('tags', {}).keys(),
            resp_item['resolved_title']
        )

def print_item(item, suggested_tags):
    secho(u'Title:\t', fg='cyan', nl=False)
    echo(item.title)
    secho('URL:\t', fg='cyan', nl=False)
    echo(item.url)
    if item.tags:
        secho('Existing tags:\t', fg='cyan', nl=False)
        echo(', '.join(item.tags))
    secho('Added tags:\t', fg='cyan', nl=False)
    echo(', '.join(suggested_tags))
    echo()

@command()
@option('--count', default=25, help='Number of items to process.')
@option('--dry-run', is_flag=True)
@option('-a', '--process-all', is_flag=True)
def processor(count, process_all, dry_run):
    cfg_path = os.path.join(
        os.path.expanduser('~'),
        '.repocket.yml',
    )
    cfg = load_config(cfg_path)
    creds = cfg.get('credentials', {})
    consumer_key = creds.get('consumer_key')
    access_token = creds.get('access_token')

    if not consumer_key or not access_token:
        consumer_key = get_consumer_key()
        access_token = get_access_token(consumer_key)
        cfg['credentials'] = {
            'consumer_key': str(consumer_key),
            'access_token': str(access_token),
        }

    secho('Your consumer key: ', fg='cyan', nl=False)
    secho(consumer_key)
    secho('Your access token: ', fg='cyan', nl=False)
    secho(access_token)
    echo()

    api_connector = Pocket(consumer_key, access_token)
    cfg.setdefault('rules', DEFAULT_RULES)
    rules = compile_rules(cfg['rules'])
    save_config(cfg_path, cfg)

    secho('Processing items...', fg='cyan')
    modified_items = []
    items = retrieve_items(api_connector, count=process_all and 0 or count)
    for item in items:
        suggested_for_item = set()
        for rule in rules:
            tags = rule.suggest_tags(item)
            suggested_for_item.update(tags or [])
            new_tags = suggested_for_item - set(item.tags)
            if new_tags:
                modified_items.append((item, new_tags))

    if modified_items:
        echo()
        for saved_item, suggested_tags in modified_items:
            print_item(saved_item, suggested_tags)
            api_connector.tags_add(saved_item.id, ','.join(list(suggested_tags)))

        if not dry_run:
            api_connector.commit()
            secho('Changes are sent to server.', fg='green')
        else:
            secho('"Dry run", no changes are sent to server.', fg='yellow')
    else:
        secho('No changes have been made.', fg='green')

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


<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import os

from osv import fields, osv
import tools
from tools.translate import _

class hr_employee_category(osv.osv):
    _name = "hr.employee.category"
    _description = "Employee Category"
    _columns = {
        'name': fields.char("Category", size=64, required=True),
        'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
        'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories')
    }

    def _check_recursion(self, cr, uid, ids, context=None):
        level = 100
        while len(ids):
            cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
            ids = filter(None, map(lambda x:x[0], cr.fetchall()))
            if not level:
                return False
            level -= 1
        return True

    _constraints = [
        (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
    ]

hr_employee_category()

class hr_employee_marital_status(osv.osv):
    _name = "hr.employee.marital.status"
    _description = "Employee Marital Status"
    _columns = {
        'name': fields.char('Marital Status', size=32, required=True),
        'description': fields.text('Status Description'),
    }

hr_employee_marital_status()

class hr_job(osv.osv):

    def _no_of_employee(self, cr, uid, ids, name, args, context=None):
        res = {}
        for emp in self.browse(cr, uid, ids):
            res[emp.id] = len(emp.employee_ids or [])
        return res

    _name = "hr.job"
    _description = "Job Description"
    _columns = {
        'name': fields.char('Job Name', size=128, required=True, select=True),
        'expected_employees': fields.integer('Expected Employees', help='Required number of Employees'),
        'no_of_employee': fields.function(_no_of_employee, method=True, string='No of Employees', type='integer', help='Number of Employees selected'),
        'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees'),
        'description': fields.text('Job Description'),
        'requirements': fields.text('Requirements'),
        'department_id': fields.many2one('hr.department', 'Department'),
        'company_id': fields.many2one('res.company', 'Company'),
        'state': fields.selection([('open', 'Open'),('old', 'Old'),('recruit', 'In Recruitement')], 'State', readonly=True, required=True),
    }
    _defaults = {
        'expected_employees': 1,
        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
        'state': 'open'
    }

    def job_old(self, cr, uid, ids, *args):
        self.write(cr, uid, ids, {'state': 'old'})
        return True

    def job_recruitement(self, cr, uid, ids, *args):
        self.write(cr, uid, ids, {'state': 'recruit'})
        return True

    def job_open(self, cr, uid, ids, *args):
        self.write(cr, uid, ids, {'state': 'open'})
        return True

hr_job()

class hr_employee(osv.osv):
    _name = "hr.employee"
    _description = "Employee"
    _inherits = {'resource.resource': "resource_id"}
    _columns = {
        'country_id': fields.many2one('res.country', 'Nationality'),
        'birthday': fields.date("Date of Birth"),
        'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
        'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
        'otherid': fields.char('Other ID', size=32),
        'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
        'marital': fields.many2one('hr.employee.marital.status', 'Marital Status'),
        'bank_account': fields.char('Bank Account', size=64),
        'partner_id': fields.related('company_id', 'partner_id', type='many2one', relation='res.partner', readonly=True),
        'department_id':fields.many2one('hr.department', 'Department'),
        'address_id': fields.many2one('res.partner.address', 'Working Address'),
        'address_home_id': fields.many2one('res.partner.address', 'Home Address'),
        'work_phone': fields.related('address_id', 'phone', type='char', string='Work Phone', readonly=True),
        'work_email': fields.related('address_id', 'email', type='char', size=240, string='Work E-mail'),
        'work_location': fields.char('Office Location', size=32),
        'notes': fields.text('Notes'),
        'parent_id': fields.related('department_id', 'manager_id', relation='hr.employee', string='Manager', type='many2one', store=True, select=True,help='It is linked to Department',readonly=True),
        'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel','category_id','emp_id','Category'),
        'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
        'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
        'coach_id': fields.many2one('hr.employee', 'Coach'),
        'job_id': fields.many2one('hr.job', 'Job'),
        'photo': fields.binary('Photo')
    }
    def onchange_company(self, cr, uid, ids, company, context=None):
        return {'value': {'address_id': company}}

    def onchange_department(self, cr, uid, ids, department_id, context=None):
        return {'value': {'parent_id':department_id}}

    def onchange_user(self, cr, uid, ids, user_id, context=None):
        user_obj = self.pool.get('res.users')
        t = user_obj.browse(cr,uid,user_id)
        return {'value': {'work_email':t.user_email}}

    def _get_photo(self, cr, uid, context=None):
        return open(os.path.join(
            tools.config['addons_path'], 'hr/image', 'photo.png'),
                    'rb') .read().encode('base64')

    _defaults = {
        'active': 1,
        'photo': _get_photo,
        'address_id': lambda self,cr,uid,c: self.pool.get('res.partner.address').browse(cr, uid, uid, c).partner_id.id
    }

    def _check_recursion(self, cr, uid, ids, context=None):
        level = 100
        while len(ids):
            cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
            ids = filter(None, map(lambda x:x[0], cr.fetchall()))
            if not level:
                return False
            level -= 1
        return True

    def _check_department_id(self, cr, uid, ids, context=None):
        for emp in self.browse(cr, uid, ids, context=context):
            if emp.department_id.manager_id and emp.id == emp.department_id.manager_id.id:
                return False
        return True

    _constraints = [
        (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id']),
        (_check_department_id, 'Error ! You cannot select a department for which the employee is the manager.', ['department_id']),
    ]

hr_employee()

class hr_department(osv.osv):
    _description = "Department"
    _inherit = 'hr.department'
    _columns = {
        'manager_id': fields.many2one('hr.employee', 'Manager'),
        'member_ids': fields.one2many('hr.employee', 'department_id', 'Members'),
    }

hr_department()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<commit_msg>[IMP] hr : improvemetns in on_change_company and on_change_department<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import os

from osv import fields, osv
import tools
from tools.translate import _

class hr_employee_category(osv.osv):
    _name = "hr.employee.category"
    _description = "Employee Category"
    _columns = {
        'name': fields.char("Category", size=64, required=True),
        'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
        'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories')
    }

    def _check_recursion(self, cr, uid, ids, context=None):
        level = 100
        while len(ids):
            cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
            ids = filter(None, map(lambda x:x[0], cr.fetchall()))
            if not level:
                return False
            level -= 1
        return True

    _constraints = [
        (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
    ]

hr_employee_category()

class hr_employee_marital_status(osv.osv):
    _name = "hr.employee.marital.status"
    _description = "Employee Marital Status"
    _columns = {
        'name': fields.char('Marital Status', size=32, required=True),
        'description': fields.text('Status Description'),
    }

hr_employee_marital_status()

class hr_job(osv.osv):

    def _no_of_employee(self, cr, uid, ids, name, args, context=None):
        res = {}
        for emp in self.browse(cr, uid, ids):
            res[emp.id] = len(emp.employee_ids or [])
        return res

    _name = "hr.job"
    _description = "Job Description"
    _columns = {
        'name': fields.char('Job Name', size=128, required=True, select=True),
        'expected_employees': fields.integer('Expected Employees', help='Required number of Employees'),
        'no_of_employee': fields.function(_no_of_employee, method=True, string='No of Employees', type='integer', help='Number of Employees selected'),
        'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees'),
        'description': fields.text('Job Description'),
        'requirements': fields.text('Requirements'),
        'department_id': fields.many2one('hr.department', 'Department'),
        'company_id': fields.many2one('res.company', 'Company'),
        'state': fields.selection([('open', 'Open'),('old', 'Old'),('recruit', 'In Recruitement')], 'State', readonly=True, required=True),
    }
    _defaults = {
        'expected_employees': 1,
        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
        'state': 'open'
    }

    def job_old(self, cr, uid, ids, *args):
        self.write(cr, uid, ids, {'state': 'old'})
        return True

    def job_recruitement(self, cr, uid, ids, *args):
        self.write(cr, uid, ids, {'state': 'recruit'})
        return True

    def job_open(self, cr, uid, ids, *args):
        self.write(cr, uid, ids, {'state': 'open'})
        return True

hr_job()

class hr_employee(osv.osv):
    _name = "hr.employee"
    _description = "Employee"
    _inherits = {'resource.resource': "resource_id"}
    _columns = {
        'country_id': fields.many2one('res.country', 'Nationality'),
        'birthday': fields.date("Date of Birth"),
        'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
        'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
        'otherid': fields.char('Other ID', size=32),
        'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
        'marital': fields.many2one('hr.employee.marital.status', 'Marital Status'),
        'bank_account': fields.char('Bank Account', size=64),
        'partner_id': fields.related('company_id', 'partner_id', type='many2one', relation='res.partner', readonly=True),
        'department_id':fields.many2one('hr.department', 'Department'),
        'address_id': fields.many2one('res.partner.address', 'Working Address'),
        'address_home_id': fields.many2one('res.partner.address', 'Home Address'),
        'work_phone': fields.related('address_id', 'phone', type='char', string='Work Phone', readonly=True),
        'work_email': fields.related('address_id', 'email', type='char', size=240, string='Work E-mail'),
        'work_location': fields.char('Office Location', size=32),
        'notes': fields.text('Notes'),
        'parent_id': fields.related('department_id', 'manager_id', relation='hr.employee', string='Manager', type='many2one', store=True, select=True, readonly=True, help="It is linked with manager of Department"),
        'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel','category_id','emp_id','Category'),
        'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
        'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
        'coach_id': fields.many2one('hr.employee', 'Coach'),
        'job_id': fields.many2one('hr.job', 'Job'),
        'photo': fields.binary('Photo')
    }

    def onchange_company(self, cr, uid, ids, company, context=None):
        company_id = self.pool.get('res.company').browse(cr,uid,company)
        for address in company_id.partner_id.address:
            return {'value': {'address_id': address.id}}
        return {'value':{}}

    def onchange_department(self, cr, uid, ids, department_id, context=None):
        manager = self.pool.get('hr.department').browse(cr, uid, department_id).manager_id.id
        return {'value': {'parent_id':manager or False}}

    def onchange_user(self, cr, uid, ids, user_id, context=None):
        mail = self.pool.get('res.users').browse(cr,uid,user_id)
        return {'value': {'work_email':mail}}

    def _get_photo(self, cr, uid, context=None):
        return open(os.path.join(
            tools.config['addons_path'], 'hr/image', 'photo.png'),
                    'rb') .read().encode('base64')

    _defaults = {
        'active': 1,
        'photo': _get_photo,
        'address_id': lambda self,cr,uid,c: self.pool.get('res.partner.address').browse(cr, uid, uid, c).partner_id.id
    }

    def _check_recursion(self, cr, uid, ids, context=None):
        level = 100
        while len(ids):
            cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
            ids = filter(None, map(lambda x:x[0], cr.fetchall()))
            if not level:
                return False
            level -= 1
        return True

    def _check_department_id(self, cr, uid, ids, context=None):
        for emp in self.browse(cr, uid, ids, context=context):
            if emp.department_id.manager_id and emp.id == emp.department_id.manager_id.id:
                return False
        return True

    _constraints = [
        (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id']),
        (_check_department_id, 'Error ! You cannot select a department for which the employee is the manager.', ['department_id']),
    ]

hr_employee()

class hr_department(osv.osv):
    _description = "Department"
    _inherit = 'hr.department'
    _columns = {
        'manager_id': fields.many2one('hr.employee', 'Manager'),
        'member_ids': fields.one2many('hr.employee', 'department_id', 'Members'),
    }

hr_department()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<|endoftext|>


<commit_before><commit_msg>Fix example code formatting.<commit_after><|endoftext|>


<commit_before>import discord
from discord.ext import commands
from sys import argv
import requests

class Urban:
    """
    Urban Dictionnary requests
    """
    def __init__(self, bot):
        self.bot = bot
        print('Addon "{}" loaded'.format(self.__class__.__name__))

    @commands.command()
    async def urban(self,term=None):
        """Lookup a term on Urban Dictionnary. If no term is specified, returns a random word / definition"""
        def slicer(str,sep1:str,sep2:str=None,select=True): #Slices a defined part of a string
            if select is True:
                selectFirstDef = str.split("}", 1)[0]
                delBefore = selectFirstDef.split(sep1, 1)[1]
            else:
                delBefore = str.split(sep1, 1)[1]
            delAfter = delBefore.split(sep2, 1)[0]
            if sep2 is None:
                return delBefore
            else:
                return delAfter

        if term is None:
            r = requests.get("http://api.urbandictionary.com/v0/random")
        else:
            r = requests.get("http://api.urbandictionary.com/v0/define?term={}".format(term))

        source = r.text
        if source == "{\"tags\":[],\"result_type\":\"no_results\",\"list\":[],\"sounds\":[]}" : #aka error 404
            embed = discord.Embed(title="¯\_(ツ)_/¯", colour=discord.Color.orange())
            embed.url = "http://www.urbandictionary.com/define.php?term={}".format(term)
            embed.description = "\nThere aren't any definitions for *{0}* yet.\n\n[Can you define it?](http://www.urbandictionary.com/add.php?word={0})\n".format(term)
            embed.set_footer(text="Error 404", icon_url="http://i.imgur.com/w6TtWHK.png")
            await self.bot.say(embed=embed)
        else:
            word = slicer(source,"\"word\":\"","\",\"defid\":")
            definition = slicer(source,"\"definition\":\"","\",\"permalink\":\"").replace("\\r\\n", "\n")
            examples = slicer(source,"\"example\":\"","\",\"thumbs_down\":").replace("\\r\\n", "\n")
            author = slicer(source,"\"author\":\"","\",\"word\":\"")
            permalink = slicer(source,"\"permalink\":\"","\",\"thumbs_up\":")
            thumbsup = slicer(source,"\"thumbs_up\":",",\"author\":\"")
            thumbsdown = slicer(source,"\"thumbs_down\":")
#            def date(permalink, author): #there's no date field on the api site, so let's do it ourselves
 #               d = requests.get(permalink)
  #              page = d.text.replace("'", "\"")
   #             date = slicer(page,">{}".format(author),"

",False)
    #            return date
            embed = discord.Embed(title="Definition of {}\n\n".format(word), colour=discord.Color.orange())
            embed.set_thumbnail(url="http://i.imgur.com/B1gZbQz.png")
            embed.url = permalink
            embed.description = definition.replace("\\n", "\n") + "\n"
            if examples != "":
                embed.add_field(name="__Example(s) :__", value=examples.replace("\\n", "\n"), inline=False)
                textExamples = examples
            else:
                textExamples = "None"
            embed.add_field(name="Upvotes", value="👍 **{}**".format(thumbsup), inline=True)
            embed.add_field(name="Downvotes", value="👎 **{}**\n\n".format(thumbsdown), inline=True)
            embed.set_footer(text="Defined by {0}".format(author))
            try:
                await self.bot.say(embed=embed)
            except:
                await self.bot.say("**__Definition of {0}__**__ ({1})__\n\n\n".format(word, permalink) + definition.replace("\\n", "\n") + "\n\n" + "__Example(s) :__\n\n" + textExamples.replace("\\n", "\n") + "\n\n" + thumbsup + "👍\n\n" + thumbsdown + "👎\n\n\n\n" + "*Defined by* " + author)

#Load the extension
def setup(bot):
    bot.add_cog(Urban(bot))
<commit_msg>No longer needs quotes for multiple words in urban.py<commit_after>import discord
from discord.ext import commands
from sys import argv
import requests

class Urban:
    """
    Urban Dictionnary requests
    """
    def __init__(self, bot):
        self.bot = bot
        print('Addon "{}" loaded'.format(self.__class__.__name__))

    @commands.command()
    async def urban(self, *, term=None):
        """Lookup a term on Urban Dictionnary. If no term is specified, returns a random word / definition"""
        def slicer(str,sep1:str,sep2:str=None,select=True): #Slices a defined part of a string
            if select is True:
                selectFirstDef = str.split("}", 1)[0]
                delBefore = selectFirstDef.split(sep1, 1)[1]
            else:
                delBefore = str.split(sep1, 1)[1]
            delAfter = delBefore.split(sep2, 1)[0]
            if sep2 is None:
                return delBefore
            else:
                return delAfter

        if term is None:
            r = requests.get("http://api.urbandictionary.com/v0/random")
        else:
            r = requests.get("http://api.urbandictionary.com/v0/define?term={}".format(term))

        source = r.text
        if source == "{\"tags\":[],\"result_type\":\"no_results\",\"list\":[],\"sounds\":[]}" : #aka error 404
            embed = discord.Embed(title="¯\_(ツ)_/¯", colour=discord.Color.orange())
            embed.url = "http://www.urbandictionary.com/define.php?term={}".format(term)
            embed.description = "\nThere aren't any definitions for *{0}* yet.\n\n[Can you define it?](http://www.urbandictionary.com/add.php?word={0})\n".format(term)
            embed.set_footer(text="Error 404", icon_url="http://i.imgur.com/w6TtWHK.png")
            await self.bot.say(embed=embed)
        else:
            word = slicer(source,"\"word\":\"","\",\"defid\":")
            definition = slicer(source,"\"definition\":\"","\",\"permalink\":\"").replace("\\r\\n", "\n")
            examples = slicer(source,"\"example\":\"","\",\"thumbs_down\":").replace("\\r\\n", "\n")
            author = slicer(source,"\"author\":\"","\",\"word\":\"")
            permalink = slicer(source,"\"permalink\":\"","\",\"thumbs_up\":")
            thumbsup = slicer(source,"\"thumbs_up\":",",\"author\":\"")
            thumbsdown = slicer(source,"\"thumbs_down\":")
#            def date(permalink, author): #there's no date field on the api site, so let's do it ourselves
 #               d = requests.get(permalink)
  #              page = d.text.replace("'", "\"")
   #             date = slicer(page,">{}".format(author),"

",False)
    #            return date
            embed = discord.Embed(title="Definition of {}\n\n".format(word), colour=discord.Color.orange())
            embed.set_thumbnail(url="http://i.imgur.com/B1gZbQz.png")
            embed.url = permalink
            embed.description = definition.replace("\\n", "\n") + "\n"
            if examples != "":
                embed.add_field(name="__Example(s) :__", value=examples.replace("\\n", "\n"), inline=False)
                textExamples = examples
            else:
                textExamples = "None"
            embed.add_field(name="Upvotes", value="👍 **{}**".format(thumbsup), inline=True)
            embed.add_field(name="Downvotes", value="👎 **{}**\n\n".format(thumbsdown), inline=True)
            embed.set_footer(text="Defined by {0}".format(author))
            try:
                await self.bot.say(embed=embed)
            except:
                await self.bot.say("**__Definition of {0}__**__ ({1})__\n\n\n".format(word, permalink) + definition.replace("\\n", "\n") + "\n\n" + "__Example(s) :__\n\n" + textExamples.replace("\\n", "\n") + "\n\n" + thumbsup + "👍\n\n" + thumbsdown + "👎\n\n\n\n" + "*Defined by* " + author)

#Load the extension
def setup(bot):
    bot.add_cog(Urban(bot))
<|endoftext|>


<commit_before>"""
Manage deployments from a GitHub repository.

Aegea deploy is a deployment manager daemon. It uses an SNS-SQS bus
to notify instances about updates to a GitHub repo. When
an update is detected, the deploy daemon pulls a new copy of the repo,
builds it, swaps it with the existing copy, and restarts your service.
"""

from __future__ import absolute_import, division, print_function, unicode_literals

import os, sys, argparse, getpass
from datetime import datetime

from botocore.exceptions import ClientError

from . import register_parser, logger
from .util import Timestamp, paginate
from .util.printing import format_table, page_output, get_field, get_cell, tabulate, BOLD
from .util.aws import ARN, resources, clients, IAMPolicyBuilder

def deploy(args):
    deploy_parser.print_help()

deploy_parser = register_parser(deploy, help='Manage deployments of GitHub repositories', description=__doc__,
                                formatter_class=argparse.RawTextHelpFormatter)

def configure(args):
    import github3
    try:
        gh = github3.login(token=os.environ["GH_AUTH"])
    except Exception:
        msg = "GitHub login failed. Please get a token at https://github.com/settings/tokens and set the GH_AUTH environment variable to its value." # noqa
        return SystemExit(msg)
    if args.repo.endswith(".git"):
        args.repo = args.repo[:len(".git")]
    gh_owner_name, gh_repo_name = args.repo.split("/")[-2:]
    repo = gh.repository(gh_owner_name, gh_repo_name)

    iam_user_name = __name__ + "-github-event-relay"
    try:
        user = resources.iam.User(iam_user_name)
        user.load()
    except ClientError:
        user = resources.iam.create_user(UserName=iam_user_name)

    topic = resources.sns.create_topic(Name="github-{}-{}-events".format(gh_owner_name, gh_repo_name))
    policy = IAMPolicyBuilder(action="sns:Publish", resource=topic.arn)
    user.create_policy(PolicyName="sqs_send_message", PolicyDocument=str(policy))

    for key in user.access_keys.all():
        key.delete()
    key = user.create_access_key_pair()
    repo.create_hook("amazonsns", dict(sns_topic=topic.arn,
                                       sns_region=ARN(topic.arn).region,
                                       aws_key=key.id,
                                       aws_secret=key.secret))
    logger.info("Created SNS topic %s and GitHub hook for repo %s", topic, repo)
    return dict(topic_arn=topic.arn)

parser = register_parser(configure, parent=deploy_parser)
parser.add_argument('repo')

def status(args):
    table = []
    queues = list(resources.sqs.queues.filter(QueueNamePrefix="github"))
    for topic in resources.sns.topics.all():
        if ARN(topic.arn).resource.startswith("github"):
            for queue in queues:
                if ARN(queue.attributes["QueueArn"]).resource.startswith(ARN(topic.arn).resource):
                    table.append(dict(Topic=topic, Queue=queue))
    args.columns = ["Topic", "Queue"]
    page_output(tabulate(table, args))

parser = register_parser(status, parent=deploy_parser)
<commit_msg>Remove unused imports<commit_after>"""
Manage deployments from a GitHub repository.

Aegea deploy is a deployment manager daemon. It uses an SNS-SQS bus
to notify instances about updates to a GitHub repo. When
an update is detected, the deploy daemon pulls a new copy of the repo,
builds it, swaps it with the existing copy, and restarts your service.
"""

from __future__ import absolute_import, division, print_function, unicode_literals

import os, sys, argparse, getpass
from datetime import datetime

from botocore.exceptions import ClientError

from . import register_parser, logger
from .util.printing import format_table, page_output, get_field, get_cell, tabulate, BOLD
from .util.aws import ARN, resources, clients, IAMPolicyBuilder

def deploy(args):
    deploy_parser.print_help()

deploy_parser = register_parser(deploy, help='Manage deployments of GitHub repositories', description=__doc__,
                                formatter_class=argparse.RawTextHelpFormatter)

def configure(args):
    import github3
    try:
        gh = github3.login(token=os.environ["GH_AUTH"])
    except Exception:
        msg = "GitHub login failed. Please get a token at https://github.com/settings/tokens and set the GH_AUTH environment variable to its value." # noqa
        return SystemExit(msg)
    if args.repo.endswith(".git"):
        args.repo = args.repo[:len(".git")]
    gh_owner_name, gh_repo_name = args.repo.split("/")[-2:]
    repo = gh.repository(gh_owner_name, gh_repo_name)

    iam_user_name = __name__ + "-github-event-relay"
    try:
        user = resources.iam.User(iam_user_name)
        user.load()
    except ClientError:
        user = resources.iam.create_user(UserName=iam_user_name)

    topic = resources.sns.create_topic(Name="github-{}-{}-events".format(gh_owner_name, gh_repo_name))
    policy = IAMPolicyBuilder(action="sns:Publish", resource=topic.arn)
    user.create_policy(PolicyName="sqs_send_message", PolicyDocument=str(policy))

    for key in user.access_keys.all():
        key.delete()
    key = user.create_access_key_pair()
    repo.create_hook("amazonsns", dict(sns_topic=topic.arn,
                                       sns_region=ARN(topic.arn).region,
                                       aws_key=key.id,
                                       aws_secret=key.secret))
    logger.info("Created SNS topic %s and GitHub hook for repo %s", topic, repo)
    return dict(topic_arn=topic.arn)

parser = register_parser(configure, parent=deploy_parser)
parser.add_argument('repo')

def status(args):
    table = []
    queues = list(resources.sqs.queues.filter(QueueNamePrefix="github"))
    for topic in resources.sns.topics.all():
        if ARN(topic.arn).resource.startswith("github"):
            for queue in queues:
                if ARN(queue.attributes["QueueArn"]).resource.startswith(ARN(topic.arn).resource):
                    table.append(dict(Topic=topic, Queue=queue))
    args.columns = ["Topic", "Queue"]
    page_output(tabulate(table, args))

parser = register_parser(status, parent=deploy_parser)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

__author__ = 'nickl-'

import sys
import textwrap

from .__version__ import __version__

from .cache import CacheProviderFactory
from .adapters import AVAILABLE_ADAPTERS

def coroutine(func):
    def start(*args,**kwargs):
        cr = func(*args,**kwargs)
        cr.next()
        return cr
    return start

class ProgressTicker():

    ref = None
    result = {}
    steps = len(AVAILABLE_ADAPTERS)
    taken = 0

    def routine(self, routine):
        self.ref = routine

    def done(self):
        return int(float(self.taken) / float(self.steps) * 100)

    def send(self, args):
        if isinstance(args, int):
            self.taken += args
        elif isinstance(args[0], str) and args[0] == 'step':
            self.steps += args[1]
        else:
            self.taken += args[0]
            try:
                self.result.update(args[1])
            except ValueError:
                self.result = args[1]
        self.ref.send(self.result)

class CommandProcessor():

    cache = CacheProviderFactory().whichProvider()

    def __init__(self, data):
        if data.invalidate:
            self.cache.invalidate(True)

    def cmd(self):
        return self.__class__.__name__.replace('Command', '').lower()

    def do(self, packages, adapters):
        self.ticker.steps = len(packages) * len(AVAILABLE_ADAPTERS)
        for package in packages:
            if ':' in package:
                mngr = package.partition(':')
                pkg = mngr[2]
                mngr = mngr[0]
            else:
                mngr = None
                pkg = package
            adapters.send((pkg, mngr))
    @coroutine
    def each(self, target):
        while True:
            args = (yield)
            package = args[0]
            manager = args[1]
            for adapter in AVAILABLE_ADAPTERS:
                adapter_name = adapter[0]
                if not manager or manager == adapter_name.lower():
                    self.out.send(
                        'Doing an aero {} of package: {} using {} '.format(
                            self.cmd(), package, adapter_name
                        )
                    )
                res = self.seen(self.cmd(), adapter_name, package)
                self.ticker.send(('step', 4))
                if self.data.invalidate or res is False:
                    if adapter_name not in self.data.disabled:
                        if not manager or manager == adapter_name.lower():
                            self.ticker.send(1)
                            target.send((adapter, package))
                            self.ticker.send(1)
                        else:
                            self.ticker.send(3)
                        self.ticker.send(1)
                    else:
                        self.ticker.send(4)
                    self.ticker.send(1)
                else:
                    if not manager or manager == adapter_name.lower():
                        self.out.send('Found ({}) options [CACHED]\n'.format(len(res)))
                    self.ticker.send((5, res))

    def call(self, adapter, args):
        try:
            return getattr(adapter, self.cmd())(args)
        except AttributeError:  # as e: # Trust me you're going to want to use that e when duck duck misses
            # print e
            return 'Aborted: {} has no implementation for command: {}'.format(
                adapter.__class__.__name__, self.cmd()
            )

class SearchCommand(CommandProcessor):

    def do(self, pkg, data, mngr=None):
        res = self.cache.seen(pkg)
        if data.invalidate or res is False:
            res = {}
            for adapter in AVAILABLE_ADAPTERS:
                if dptr not in data.disabled:
                    if not mngr or mngr == dptr.lower():
                        sys.stdout.write('Doing an aero {} of package: {} using {} '.format(
                            self.cmd(), pkg, type(adapter).__name__)
                        )
                        aero = self.call(adapter, pkg)
                        print 'Found ({}) options'.format(len(aero))
                        if aero:
                            res.update(aero)

        if res:
            res = sorted(self.cache.seen(pkg, res).items())
            use_pager = len(res) > 30  # TODO find real screen height
            pager.write("\n{:>48}   {:<52}\n".format("PACKAGE NAME", "DESCRIPTION"))
            pager.write("{:>48}   {:<52}\n".format("_" * 40, "_" * 50))
            for k, v in res:
                for line in v.splitlines():
                    if k:
                        k += ' :'
                    if len(line) > 50:
                        for wrap in textwrap.wrap(line, 50):
                            pager.write("{:>50} {:<50}\n".format(k, wrap))
                            k = ''
                    else:
                        pager.write("{:>50} {:<50}\n".format(k, line))
                    k = ''
            pager.write('\n')
                from cStringIO import StringIO
                pager = StringIO()
                from pygments import highlight
                from pygments.lexers import CppLexer
                from pygments.formatters import Terminal256Formatter
                out = pager.getvalue()
                out = highlight(out, CppLexer(), Terminal256Formatter())
                out = out.encode('utf')
                if len(out.splitlines()) > 30:
                    from subprocess import Popen, PIPE
                    Popen(self.data.pager, shell=True, stdin=PIPE).communicate(input=out)
                else:
                    print out

class InstallCommand(CommandProcessor):

    pass

class InfoCommand(CommandProcessor):

    def do(self, pkg, data, mngr=None):
        res = self.cache.seen(pkg)
        if data.invalidate or res is False:
            res = []
            for adapter in AVAILABLE_ADAPTERS:
                if dptr not in data.disabled:
                    if not mngr or mngr == dptr.lower():
                        print 'Doing an aero {} of package: {} using {} \n'.format(
                            self.cmd(), pkg, type(adapter).__name__
                        )
                        res = self.cache.seen(pkg, self.call(adapter, pkg))

        if 'Aborted:' in res:
            print res
            return
        k = ''
        print "\n{:>48}   {:<52}".format('', 'INFORMATION: ' + pkg)
        print "{:>48}   {:<52}".format("_" * 40, "_" * 50)
        for line in res:
            if isinstance(line, tuple) or isinstance(line, list):
                if len(line) >= 2:
                    k = line[0] + ' :'
                    line = line[1]
                else:
                    line = line[0]
            if line:
                for l in line.splitlines():
                    if len(l) > 50:
                        for wrap in textwrap.wrap(l, 50):
                            print "{:>50} {:50}".format(k, wrap)
                            k = ''
                    else:
                        print "{:>50} {:50}".format(k, l)
                        k = ''
<commit_msg>More extendable<commit_after># -*- coding: utf-8 -*-

__author__ = 'nickl-'

import sys
import textwrap

from .__version__ import __version__

from .cache import CacheProviderFactory
from .adapters import AVAILABLE_ADAPTERS

def coroutine(func):
    def start(*args,**kwargs):
        cr = func(*args,**kwargs)
        cr.next()
        return cr
    return start

class ProgressTicker():

    ref = None
    result = {}
    steps = len(AVAILABLE_ADAPTERS)
    taken = 0

    def routine(self, routine):
        self.ref = routine

    def done(self):
        return int(float(self.taken) / float(self.steps) * 100)

    def send(self, args):
        if isinstance(args, int):
            self.taken += args
        elif isinstance(args[0], str) and args[0] == 'step':
            self.steps += args[1]
        else:
            self.taken += args[0]
            try:
                self.result.update(args[1])
            except ValueError:
                self.result = args[1]
        self.ref.send(self.result)

class CommandProcessor():

    cache = CacheProviderFactory().whichProvider()

    def __init__(self, data):
        if data.invalidate:
            self.cache.invalidate(True)

    def cmd(self):
        return self.__class__.__name__.replace('Command', '').lower()

    def do(self, packages, adapters):
        self.ticker.steps = len(packages) * len(AVAILABLE_ADAPTERS)
        for package in packages:
            if ':' in package:
                mngr = package.partition(':')
                pkg = mngr[2]
                mngr = mngr[0]
            else:
                mngr = None
                pkg = package
            adapters.send((pkg, mngr))
    @coroutine
    def each(self, target):
        while True:
            args = (yield)
            package = args[0]
            manager = args[1]
            for adapter in AVAILABLE_ADAPTERS:
                adapter_name = adapter[0]
                if not manager or manager == adapter_name.lower():
                    self.out.send(
                        'Doing an aero {} of package: {} using {} '.format(
                            self.cmd(), package, adapter_name
                        )
                    )
                res = self.seen(self.cmd(), adapter_name, package)
                self.ticker.send(('step', 4))
                if self.data.invalidate or res is False:
                    if adapter_name not in self.data.disabled:
                        if not manager or manager == adapter_name.lower():
                            self.ticker.send(1)
                            target.send((adapter, package))
                            self.ticker.send(1)
                        else:
                            self.ticker.send(3)
                        self.ticker.send(1)
                    else:
                        self.ticker.send(4)
                    self.ticker.send(1)
                else:
                    if not manager or manager == adapter_name.lower():
                        self.out.send('Found ({}) options [CACHED]\n'.format(len(res)))
                    self.ticker.send((5, res))

    def call(self, adapter, args):
        try:
            return getattr(adapter, self.cmd())(args)
        except AttributeError:  # as e: # Trust me you're going to want to use that e when duck duck misses
            # print e
            return 'Aborted: {} has no implementation for command: {}'.format(
                adapter.__class__.__name__, self.cmd()
            )

class SearchCommand(CommandProcessor):

        if res:
            res = sorted(self.cache.seen(pkg, res).items())
            use_pager = len(res) > 30  # TODO find real screen height
            pager.write("\n{:>48}   {:<52}\n".format("PACKAGE NAME", "DESCRIPTION"))
            pager.write("{:>48}   {:<52}\n".format("_" * 40, "_" * 50))
            for k, v in res:
                for line in v.splitlines():
                    if k:
                        k += ' :'
                    if len(line) > 50:
                        for wrap in textwrap.wrap(line, 50):
                            pager.write("{:>50} {:<50}\n".format(k, wrap))
                            k = ''
                    else:
                        pager.write("{:>50} {:<50}\n".format(k, line))
                    k = ''
            pager.write('\n')
                from cStringIO import StringIO
                pager = StringIO()
                from pygments import highlight
                from pygments.lexers import CppLexer
                from pygments.formatters import Terminal256Formatter
                out = pager.getvalue()
                out = highlight(out, CppLexer(), Terminal256Formatter())
                out = out.encode('utf')
                if len(out.splitlines()) > 30:
                    from subprocess import Popen, PIPE
                    Popen(self.data.pager, shell=True, stdin=PIPE).communicate(input=out)
                else:
                    print out

class InstallCommand(CommandProcessor):

    pass

class InfoCommand(CommandProcessor):

        if 'Aborted:' in res:
            print res
            return
        k = ''
        print "\n{:>48}   {:<52}".format('', 'INFORMATION: ' + pkg)
        print "{:>48}   {:<52}".format("_" * 40, "_" * 50)
        for line in res:
            if isinstance(line, tuple) or isinstance(line, list):
                if len(line) >= 2:
                    k = line[0] + ' :'
                    line = line[1]
                else:
                    line = line[0]
            if line:
                for l in line.splitlines():
                    if len(l) > 50:
                        for wrap in textwrap.wrap(l, 50):
                            print "{:>50} {:50}".format(k, wrap)
                            k = ''
                    else:
                        print "{:>50} {:50}".format(k, l)
                        k = ''
<|endoftext|>