<commit_before>#!/usr/bin/python

import subprocess
import os
from subprocess import PIPE

from optparse import OptionParser

parser = OptionParser()

parser.add_option("-c",dest="cid")
parser.add_option("--tmpDir",dest="tmpDirectory")
parser.add_option("--dataDir", dest="dataDirectory")

(options, args) = parser.parse_args()

cid = options.cid
tmpDir = options.tmpDirectory

ctFileName = os.path.join(options.dataDirectory,cid + ".nhdr")
plFileName = os.path.join(tmpDir,cid + "_partialLungLabelMap.nhdr")
csvFileName = os.path.join(tmpDir,cid + "_emphysemaMeasures.csv")
qualityFileName = os.path.join(tmpDir,cid + "_labelMapCoronalProjectionImage.png")
quality2FileName= os.path.join(tmpDir,cid + "_labelMapAxialProjectionImage.png")

lpiPath="/PHShome/rs117/projects/code/Slicer3_lmi-build/lib/Slicer3/Plugins/"
cipPath="/PHShome/rs117/projects/code/cip-build/"
itkToolsPath="/data/acil/Code/ITKTools-build/"

tmpCommand = "LungMaskExtraction %(vol)s %(output)s"
tmpCommand = tmpCommand % {'vol':ctFileName, 'output':plFileName}
tmpCommand = os.path.join(lpiPath,tmpCommand);
#subprocess.call( tmpCommand, shell=True)

# Smooth data to normalized recon kernels
smoothImage = os.path.join(tmpDir,cid + "_smooth.nhdr")
tmpCommand = "unu resample -k dgauss:0.75,3 -s x1 x1 = -i %(vol)s -o %(out)s"
tmpCommand = tmpCommand % {'vol':ctFileName, 'out':smoothImage}
#subprocess.call (tmpCommand, shell=True)

#Compute distance map for rine/core computations
distanceImage = os.path.join(tmpDir,cid + "_distanceMap.nhdr")
lungImage = os.path.join(tmpDir,cid + "_lung.nhdr")
tmpCommand ="unu 3op in_cl 2 %(lm-in)s 7 -o %(lung)s"
tmpCommand = tmpCommand % {'lm-in':plFileName,'lung':lungImage}
subprocess.call( tmpCommand, shell=True)
#tmpCommand = "ComputeDistanceMap -l %(lung)s -d %(distance-map)s -s 2"
tmpCommand = "pxdistancetransform -in %(lung)s -out %(distance-map)s -m Maurer"
tmpCommand = tmpCommand % {'lung':lungImage,'distance-map':distanceImage}
tmpCommand = os.path.join(itkToolsPath,"bin",tmpCommand)
print tmpCommand
#subprocess.call( tmpCommand, shell=True )

tmpCommand = "GenerateEmphysemaMeasures %(vol)s %(output)s --outputCSV %(res)s -d %(dm)s"
tmpCommand = tmpCommand % {'vol':smoothImage, 'output':plFileName, 'res':csvFileName, 'dm':distanceImage}
tmpCommand = os.path.join(lpiPath,tmpCommand);

subprocess.call( tmpCommand, shell=True)

tmpCommand = "unu 2op lt %(output)s 8 | unu 2op x %(output)s - | unu project -a 1 -m variance \
             | unu resample -s x1 x3 -k tent | unu flip -a 1 | unu quantize -b 8 -o %(quality)s"
tmpCommand = tmpCommand % {'output':plFileName, 'quality':qualityFileName}
subprocess.call( tmpCommand, shell=True)

tmpCommand = "unu 2op lt %(output)s 8 | unu 2op x %(output)s - | \
             unu project -a 2 -m variance | unu resample -s x1 x1 -k tent | \
             unu quantize -b 8 -o %(quality2)s"
tmpCommand = tmpCommand % {'output':plFileName, 'quality2':quality2FileName}
subprocess.call ( tmpCommand, shell=True)

<commit_msg>ENH: Removing comments on subprocess calls<commit_after>#!/usr/bin/python

import subprocess
import os
from subprocess import PIPE

from optparse import OptionParser

parser = OptionParser()

parser.add_option("-c",dest="cid")
parser.add_option("--tmpDir",dest="tmpDirectory")
parser.add_option("--dataDir", dest="dataDirectory")

(options, args) = parser.parse_args()

cid = options.cid
tmpDir = options.tmpDirectory

ctFileName = os.path.join(options.dataDirectory,cid + ".nhdr")
plFileName = os.path.join(tmpDir,cid + "_partialLungLabelMap.nhdr")
csvFileName = os.path.join(tmpDir,cid + "_emphysemaMeasures.csv")
qualityFileName = os.path.join(tmpDir,cid + "_labelMapCoronalProjectionImage.png")
quality2FileName= os.path.join(tmpDir,cid + "_labelMapAxialProjectionImage.png")

lpiPath="/PHShome/rs117/projects/code/Slicer3_lmi-build/lib/Slicer3/Plugins/"
cipPath="/PHShome/rs117/projects/code/cip-build/"
itkToolsPath="/data/acil/Code/ITKTools-build/"

tmpCommand = "LungMaskExtraction %(vol)s %(output)s"
tmpCommand = tmpCommand % {'vol':ctFileName, 'output':plFileName}
tmpCommand = os.path.join(lpiPath,tmpCommand);
subprocess.call( tmpCommand, shell=True)

# Smooth data to normalized recon kernels
smoothImage = os.path.join(tmpDir,cid + "_smooth.nhdr")
tmpCommand = "unu resample -k dgauss:0.75,3 -s x1 x1 = -i %(vol)s -o %(out)s"
tmpCommand = tmpCommand % {'vol':ctFileName, 'out':smoothImage}
subprocess.call (tmpCommand, shell=True)

#Compute distance map for rine/core computations
distanceImage = os.path.join(tmpDir,cid + "_distanceMap.nhdr")
lungImage = os.path.join(tmpDir,cid + "_lung.nhdr")
tmpCommand ="unu 3op in_cl 2 %(lm-in)s 7 -o %(lung)s"
tmpCommand = tmpCommand % {'lm-in':plFileName,'lung':lungImage}
subprocess.call( tmpCommand, shell=True)
#tmpCommand = "ComputeDistanceMap -l %(lung)s -d %(distance-map)s -s 2"
tmpCommand = "pxdistancetransform -in %(lung)s -out %(distance-map)s -m Maurer"
tmpCommand = tmpCommand % {'lung':lungImage,'distance-map':distanceImage}
tmpCommand = os.path.join(itkToolsPath,"bin",tmpCommand)
print tmpCommand
subprocess.call( tmpCommand, shell=True )

tmpCommand = "GenerateEmphysemaMeasures %(vol)s %(output)s --outputCSV %(res)s -d %(dm)s"
tmpCommand = tmpCommand % {'vol':smoothImage, 'output':plFileName, 'res':csvFileName, 'dm':distanceImage}
tmpCommand = os.path.join(lpiPath,tmpCommand);

subprocess.call( tmpCommand, shell=True)

tmpCommand = "unu 2op lt %(output)s 8 | unu 2op x %(output)s - | unu project -a 1 -m variance \
             | unu resample -s x1 x3 -k tent | unu flip -a 1 | unu quantize -b 8 -o %(quality)s"
tmpCommand = tmpCommand % {'output':plFileName, 'quality':qualityFileName}
subprocess.call( tmpCommand, shell=True)

tmpCommand = "unu 2op lt %(output)s 8 | unu 2op x %(output)s - | \
             unu project -a 2 -m variance | unu resample -s x1 x1 -k tent | \
             unu quantize -b 8 -o %(quality2)s"
tmpCommand = tmpCommand % {'output':plFileName, 'quality2':quality2FileName}
subprocess.call ( tmpCommand, shell=True)

<|endoftext|>


<commit_before>import os

class Config(object):
    DEBUG = False
    TESTING = False

    DB_HOST = os.environ.get('DB_HOST')
    DB_PORT = os.environ.get('DB_PORT')
    DB_USER = os.environ.get('DB_USER')
    DB_PASS = os.environ.get('DB_PASS')
    DB_NAME = os.environ.get('DB_NAME')

    SQLALCHEMY_DATABASE_URI = "mysql+mysqldb://{}:{}@{}:{}/{}".\
            format(DB_USER, DB_PASS, DB_HOST, DB_PORT, DB_NAME)

    @staticmethod
    def init_app(app):
        pass

class ProductionConfig(Config):
    pass

class DevelopmentConfig(Config):
    DEBUG = True

    DB_HOST = os.environ.get('DB_HOST') or '127.0.0.1'
    DB_PORT = os.environ.get('DB_PORT') or 3306
    DB_USER = os.environ.get('DB_USER') or 'zipkin'
    DB_PASS = os.environ.get('DB_PASS') or 'kinect'
    DB_NAME = os.environ.get('DB_NAME') or 'zipkin'

    SQLALCHEMY_DATABASE_URI = "mysql+mysqldb://{}:{}@{}:{}/{}".\
            format(DB_USER, DB_PASS, DB_HOST, DB_PORT, DB_NAME)

class TestingConfig(Config):
    TESTING = True

config = {
        'development': DevelopmentConfig,
        'testing': TestingConfig,
        'production': ProductionConfig,

        'default': DevelopmentConfig
}
<commit_msg>remove default envoirnment variables from config.py<commit_after>import os

class Config(object):
    DEBUG = False
    TESTING = False

    DB_HOST = os.environ.get('DB_HOST')
    DB_PORT = os.environ.get('DB_PORT')
    DB_USER = os.environ.get('DB_USER')
    DB_PASS = os.environ.get('DB_PASS')
    DB_NAME = os.environ.get('DB_NAME')

    SQLALCHEMY_DATABASE_URI = "mysql+mysqldb://{}:{}@{}:{}/{}".\
            format(DB_USER, DB_PASS, DB_HOST, DB_PORT, DB_NAME)

    @staticmethod
    def init_app(app):
        pass

class ProductionConfig(Config):
    pass

class DevelopmentConfig(Config):
    DEBUG = True

    DB_HOST = os.environ.get('DB_HOST')
    DB_PORT = os.environ.get('DB_PORT')
    DB_USER = os.environ.get('DB_USER')
    DB_PASS = os.environ.get('DB_PASS')
    DB_NAME = os.environ.get('DB_NAME')

    SQLALCHEMY_DATABASE_URI = "mysql+mysqldb://{}:{}@{}:{}/{}".\
            format(DB_USER, DB_PASS, DB_HOST, DB_PORT, DB_NAME)

class TestingConfig(Config):
    TESTING = True

config = {
        'development': DevelopmentConfig,
        'testing': TestingConfig,
        'production': ProductionConfig,

        'default': DevelopmentConfig
}
<|endoftext|>


<commit_before># Copyright 2016 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import shutil
import tempfile

from mistral.workflow import utils as mistral_workflow_utils
from oslo_concurrency.processutils import ProcessExecutionError

from tripleo_common.actions import base
from tripleo_common import constants
from tripleo_common.utils import validations as utils

class GetPubkeyAction(base.TripleOAction):
    def __init__(self):
        super(GetPubkeyAction, self).__init__()

    def run(self):
        mc = self._get_workflow_client()
        try:
            env = mc.environments.get('ssh_keys')
            public_key = env.variables['public_key']
        except Exception:
            tmp_dir = tempfile.mkdtemp()
            private_key_path = os.path.join(tmp_dir, 'id_rsa')
            public_key_path = private_key_path + '.pub'
            utils.create_ssh_keypair(private_key_path)

            with open(private_key_path, 'r') as f:
                private_key = f.read().strip()
            with open(public_key_path, 'r') as f:
                public_key = f.read().strip()

            shutil.rmtree(tmp_dir)

            workflow_env = {
                'name': 'ssh_keys',
                'description': 'SSH keys for TripleO validations',
                'variables': {
                    'public_key': public_key,
                    'private_key': private_key,
                }
            }
            mc.environments.create(**workflow_env)

        return public_key

class ListValidationsAction(base.TripleOAction):
    """Return a set of TripleO validations"""
    def __init__(self, groups=None):
        super(ListValidationsAction, self).__init__()
        self.groups = groups

    def run(self):
        return utils.load_validations(groups=self.groups)

class ListGroupsAction(base.TripleOAction):
    """Return a set of TripleO validation groups"""
    def __init__(self):
        super(ListGroupsAction, self).__init__()

    def run(self):
        validations = utils.load_validations()
        return {
            group for validation in validations
            for group in validation['groups']
        }

class RunValidationAction(base.TripleOAction):
    """Run the given validation"""
    def __init__(self, validation, plan=constants.DEFAULT_CONTAINER_NAME):
        super(RunValidationAction, self).__init__()
        self.validation = validation
        self.plan = plan

    def run(self):
        mc = self._get_workflow_client()
        try:
            env = mc.environments.get('ssh_keys')
            private_key = env.variables['private_key']
            identity_file = utils.write_identity_file(private_key)

            stdout, stderr = utils.run_validation(self.validation,
                                                  identity_file,
                                                  self.plan)
            return_value = {'stdout': stdout, 'stderr': stderr}
            mistral_result = (return_value, None)
        except ProcessExecutionError as e:
            return_value = {'stdout': e.stdout, 'stderr': e.stderr}
            # Indicates to Mistral there was a failure
            mistral_result = (None, return_value)
        finally:
            utils.cleanup_identity_file(identity_file)
        return mistral_workflow_utils.Result(*mistral_result)
<commit_msg>Fix error when identity file is missing<commit_after># Copyright 2016 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import shutil
import tempfile

from mistral.workflow import utils as mistral_workflow_utils
from oslo_concurrency.processutils import ProcessExecutionError

from tripleo_common.actions import base
from tripleo_common import constants
from tripleo_common.utils import validations as utils

class GetPubkeyAction(base.TripleOAction):
    def __init__(self):
        super(GetPubkeyAction, self).__init__()

    def run(self):
        mc = self._get_workflow_client()
        try:
            env = mc.environments.get('ssh_keys')
            public_key = env.variables['public_key']
        except Exception:
            tmp_dir = tempfile.mkdtemp()
            private_key_path = os.path.join(tmp_dir, 'id_rsa')
            public_key_path = private_key_path + '.pub'
            utils.create_ssh_keypair(private_key_path)

            with open(private_key_path, 'r') as f:
                private_key = f.read().strip()
            with open(public_key_path, 'r') as f:
                public_key = f.read().strip()

            shutil.rmtree(tmp_dir)

            workflow_env = {
                'name': 'ssh_keys',
                'description': 'SSH keys for TripleO validations',
                'variables': {
                    'public_key': public_key,
                    'private_key': private_key,
                }
            }
            mc.environments.create(**workflow_env)

        return public_key

class ListValidationsAction(base.TripleOAction):
    """Return a set of TripleO validations"""
    def __init__(self, groups=None):
        super(ListValidationsAction, self).__init__()
        self.groups = groups

    def run(self):
        return utils.load_validations(groups=self.groups)

class ListGroupsAction(base.TripleOAction):
    """Return a set of TripleO validation groups"""
    def __init__(self):
        super(ListGroupsAction, self).__init__()

    def run(self):
        validations = utils.load_validations()
        return {
            group for validation in validations
            for group in validation['groups']
        }

class RunValidationAction(base.TripleOAction):
    """Run the given validation"""
    def __init__(self, validation, plan=constants.DEFAULT_CONTAINER_NAME):
        super(RunValidationAction, self).__init__()
        self.validation = validation
        self.plan = plan

    def run(self):
        mc = self._get_workflow_client()
        identity_file = None
        try:
            env = mc.environments.get('ssh_keys')
            private_key = env.variables['private_key']
            identity_file = utils.write_identity_file(private_key)

            stdout, stderr = utils.run_validation(self.validation,
                                                  identity_file,
                                                  self.plan)
            return_value = {'stdout': stdout, 'stderr': stderr}
            mistral_result = (return_value, None)
        except ProcessExecutionError as e:
            return_value = {'stdout': e.stdout, 'stderr': e.stderr}
            # Indicates to Mistral there was a failure
            mistral_result = (None, return_value)
        finally:
            if identity_file:
                utils.cleanup_identity_file(identity_file)
        return mistral_workflow_utils.Result(*mistral_result)
<|endoftext|>


<commit_before>import asyncio
import re
import datetime
from datetime import datetime as dt

import peewee

from modules.base.command import Command
from modules.base.database import BaseModel

class Mute(Command):
    muted_role = 'Muted'
    rx_timediff_all = re.compile('([0-9]+[smhdSMDH]?)+')
    rx_timediff = re.compile('([0-9]+[smhdSMDH]?)')

    cant_manage_msg = 'No se puede agregar el rol de muteado a un usuario porque no se dispone del permiso necesario'

    def __init__(self, bot):
        super().__init__(bot)
        self.name = 'mute'
        self.help = 'Mutea usuarios'
        self.owner_only = True
        self.db_models = [MutedUser]
        self.run_task = True
        self.allow_pm = False

    async def handle(self, message, cmd):
        if len(cmd.args) < 1 or len(message.mentions) != 1:
            await cmd.answer('Formato: !mute <@mención> [duración]|[razón]')
            return

        member = message.mentions[0]
        server = message.server

        # Verificar si se pueden manejar roles
        if not self.can_manage_roles(server):
            self.log.warning(Mute.cant_manage_msg + ' (server: %s)', str(server))
            cmd.answer(Mute.cant_manage_msg)
            return

        # Obtener argumentos del mute
        mute_args = []
        if len(cmd.args) > 1:
            cmd.args[1:].split('|')

        # Obtener tiempo del mute
        until = None
        deltatime = None
        if len(mute_args) > 0:
            if not re.match(Mute.rx_timediff_all, mute_args[0]):
                cmd.answer('Tiempo de mute no válido')
                return
            deltatime = Mute.timediff_parse(mute_args[0])
            until = dt.now() + deltatime

        # Quitar mute de la db si ya está muteado
        MutedUser.delete().where(MutedUser.userid == member.id).execute()

        # Revisar si el usuario ya tiene el rol
        mutedrole = None
        for role in member.roles:
            if role.name == Mute.muted_role:
                mutedrole = role
                break

        # Si el usuario ya tiene el rol, no es necesario agregarlo
        if mutedrole is None:
            mutedrole = Command.get_server_role(server, Mute.muted_role)
            # if mutedrole is still None
            if mutedrole is None:
                self.log.warning('El rol "%s" no existe (server: %s)', Mute.muted_role, server)
                return
            pass

        reason = ''
        if len(mute_args) > 1:
            reason = ' '.join(cmd.args[2:])

        MutedUser.insert(userid=member.id, serverid=server.id, until=until, reason=reason,
                         author_name=str(cmd.author), author_id=cmd.author.id).execute()

        str_deltatime = ' por **{}**'.format(Mute.timediff_parse(deltatime)) if deltatime is not None else ''

        # Enviar PM con el aviso del mute
        self.bot.send_message(member, 'Hola! Lamentablemente has sido muteado en el servidor **{}**{}.'.format(
            server.name, str_deltatime))

        # Avisar por el canal donde se envió el comando
        await cmd.answer('Usuario **{}** muteado{}!'.format(member.display_name, str_deltatime))

    # Restaurar el rol de muteado una vez que el usuario ha reingresado
    async def on_member_join(self, member):
        server = member.server
        if not self.can_manage_roles(member):
            self.log.warning(Mute.cant_manage_msg)
            return

        role = Command.get_server_role(server, Mute.muted_role)
        if role is None:
            self.log.warning('El rol "%s" no existe (server: %s)', Mute.muted_role, server)
            return

        try:
            muted = MutedUser.get(MutedUser.until > dt.now(), MutedUser.userid == member.id)
            self.bot.add_role(member, role)
            return
        except MutedUser.DoesNotExist:
            pass

    # Elimina los roles de muteado una vez que ha terminado
    async def task(self):
        muted = MutedUser.select().where(MutedUser.until <= dt.now())
        for muteduser in muted:
            server = self.bot.get_server(muteduser.serverid)
            if server is None:
                continue

            self_member = server.get_member(self.bot.id)
            if not self_member.server_permissions.manage_roles():
                self.log.warning(Mute.cant_manage_msg + ' (server: %s)', server)
                continue

            member = server.get_member(muteduser.userid)
            role = Command.get_server_role(server, Mute.muted_role)
            if role is None:
                self.log.warning('El rol "%s" no existe (server: %s)', Mute.muted_role, server)
                continue
            else:
                self.bot.remove_role(member, role)

        await asyncio.sleep(5)
        if not self.bot.is_closed:
            self.bot.loop.create_task(self.task())

    @staticmethod
    def timediff_parse(timediff):
        # TODO: Buscar forma de interpretar duración
        result = datetime.timedelta(minutes=15)
        return result

    @staticmethod
    def deltatime_to_str(deltatime):
        # TODO: timediff a string
        return 'x tiempo'

class Unmute(Command):
    def __init__(self, bot):
        super().__init__(bot)
        self.name = 'unmute'
        self.help = 'Quita el mute de usuarios'
        self.owner_only = True
        self.allow_pm = False

    async def handle(self, message, cmd):
        if len(cmd.args) != 1 or len(message.mentions) != 1:
            await cmd.answer('Formato: !unmute <@mención>')
            return

        member = message.mentions[0]
        mutedrole = None
        for role in member.roles:
            if role.name == Mute.muted_role:
                mutedrole = role
                break

        if mutedrole is None:
            await cmd.answer('El usuario no tiene el rol de muteado ({})'.format(Mute.muted_role))
            return

        try:
            muteduser = MutedUser.get(MutedUser.userid == member.id)
            muteduser.delete()
        except MutedUser.DoesNotExist:
            pass

        self.bot.remove_role(member, mutedrole)
        await cmd.answer('Usuario desmuteado!')

class MutedUser(BaseModel):
    userid = peewee.TextField(null=False)
    serverid = peewee.TextField(null=False)
    until = peewee.DateTimeField()
    reason = peewee.TextField(default='')
    author_name = peewee.TextField(null=False)
    author_id = peewee.TextField(null=False)
<commit_msg>Agregar TODOs<commit_after>import asyncio
import re
import datetime
from datetime import datetime as dt

import peewee

from modules.base.command import Command
from modules.base.database import BaseModel

class Mute(Command):
    muted_role = 'Muted'
    rx_timediff_all = re.compile('([0-9]+[smhdSMDH]?)+')
    rx_timediff = re.compile('([0-9]+[smhdSMDH]?)')

    cant_manage_msg = 'No se puede agregar el rol de muteado a un usuario porque no se dispone del permiso necesario'

    def __init__(self, bot):
        super().__init__(bot)
        self.name = 'mute'
        self.help = 'Mutea usuarios'
        self.owner_only = True
        self.db_models = [MutedUser]
        self.run_task = True
        self.allow_pm = False

    async def handle(self, message, cmd):
        if len(cmd.args) < 1 or len(message.mentions) != 1:
            await cmd.answer('Formato: !mute <@mención> [duración]|[razón]')
            return

        member = message.mentions[0]
        server = message.server

        # Verificar si se pueden manejar roles
        if not self.can_manage_roles(server):
            self.log.warning(Mute.cant_manage_msg + ' (server: %s)', str(server))
            cmd.answer(Mute.cant_manage_msg)
            return

        # Obtener argumentos del mute
        mute_args = []
        if len(cmd.args) > 1:
            cmd.args[1:].split('|')

        # Obtener tiempo del mute
        until = None
        deltatime = None
        if len(mute_args) > 0:
            if not re.match(Mute.rx_timediff_all, mute_args[0]):
                cmd.answer('Tiempo de mute no válido')
                return
            deltatime = Mute.timediff_parse(mute_args[0])
            until = dt.now() + deltatime

        # Quitar mute de la db si ya está muteado
        MutedUser.delete().where(MutedUser.userid == member.id).execute()

        # Revisar si el usuario ya tiene el rol
        mutedrole = None
        for role in member.roles:
            if role.name == Mute.muted_role:
                mutedrole = role
                break

        # Si el usuario ya tiene el rol, no es necesario agregarlo
        if mutedrole is None:
            mutedrole = Command.get_server_role(server, Mute.muted_role)
            # if mutedrole is still None
            if mutedrole is None:
                self.log.warning('El rol "%s" no existe (server: %s)', Mute.muted_role, server)
                return
            pass

        reason = ''
        if len(mute_args) > 1:
            reason = ' '.join(cmd.args[2:])

        MutedUser.insert(userid=member.id, serverid=server.id, until=until, reason=reason,
                         author_name=str(cmd.author), author_id=cmd.author.id).execute()

        str_deltatime = ' por **{}**'.format(Mute.timediff_parse(deltatime)) if deltatime is not None else ''

        # Enviar PM con el aviso del mute
        self.bot.send_message(member, 'Hola! Lamentablemente has sido muteado en el servidor **{}**{}.'.format(
            server.name, str_deltatime))

        # Avisar por el canal donde se envió el comando
        await cmd.answer('Usuario **{}** muteado{}!'.format(member.display_name, str_deltatime))

    # Restaurar el rol de muteado una vez que el usuario ha reingresado
    async def on_member_join(self, member):
        server = member.server
        if not self.can_manage_roles(member):
            self.log.warning(Mute.cant_manage_msg)
            return

        role = Command.get_server_role(server, Mute.muted_role)
        if role is None:
            self.log.warning('El rol "%s" no existe (server: %s)', Mute.muted_role, server)
            return

        try:
            # TODO: Manejar until correctamente
            muted = MutedUser.get(MutedUser.until > dt.now(), MutedUser.userid == member.id)
            self.bot.add_role(member, role)
            return
        except MutedUser.DoesNotExist:
            pass

    # Elimina los roles de muteado una vez que ha terminado
    async def task(self):
        # TODO: Manejar until correctamente
        muted = MutedUser.select().where(MutedUser.until <= dt.now())
        for muteduser in muted:
            server = self.bot.get_server(muteduser.serverid)
            if server is None:
                continue

            self_member = server.get_member(self.bot.id)
            if not self_member.server_permissions.manage_roles():
                self.log.warning(Mute.cant_manage_msg + ' (server: %s)', server)
                continue

            member = server.get_member(muteduser.userid)
            role = Command.get_server_role(server, Mute.muted_role)
            if role is None:
                self.log.warning('El rol "%s" no existe (server: %s)', Mute.muted_role, server)
                continue
            else:
                self.bot.remove_role(member, role)

        await asyncio.sleep(5)
        if not self.bot.is_closed:
            self.bot.loop.create_task(self.task())

    @staticmethod
    def timediff_parse(timediff):
        # TODO: Buscar forma de interpretar duración
        result = datetime.timedelta(minutes=15)
        return result

    @staticmethod
    def deltatime_to_str(deltatime):
        # TODO: timediff a string
        return 'x tiempo'

class Unmute(Command):
    def __init__(self, bot):
        super().__init__(bot)
        self.name = 'unmute'
        self.help = 'Quita el mute de usuarios'
        self.owner_only = True
        self.allow_pm = False

    async def handle(self, message, cmd):
        if len(cmd.args) != 1 or len(message.mentions) != 1:
            await cmd.answer('Formato: !unmute <@mención>')
            return

        member = message.mentions[0]
        mutedrole = None
        for role in member.roles:
            if role.name == Mute.muted_role:
                mutedrole = role
                break

        if mutedrole is None:
            await cmd.answer('El usuario no tiene el rol de muteado ({})'.format(Mute.muted_role))
            return

        try:
            muteduser = MutedUser.get(MutedUser.userid == member.id)
            muteduser.delete()
        except MutedUser.DoesNotExist:
            pass

        self.bot.remove_role(member, mutedrole)
        await cmd.answer('Usuario desmuteado!')

class MutedUser(BaseModel):
    userid = peewee.TextField(null=False)
    serverid = peewee.TextField(null=False)
    until = peewee.DateTimeField()
    reason = peewee.TextField(default='')
    author_name = peewee.TextField(null=False)
    author_id = peewee.TextField(null=False)
<|endoftext|>


<commit_before># Note: Work in progress

import os
import re
import codecs
import textwrap
from xml.sax.saxutils import escape as html_escape
from StringIO import StringIO

import Version
from Code import CCodeWriter
from Cython import Utils

class AnnotationCCodeWriter(CCodeWriter):

    def __init__(self, create_from=None, buffer=None, copy_formatting=True):
        CCodeWriter.__init__(self, create_from, buffer, copy_formatting=True)
        if create_from is None:
            self.annotation_buffer = StringIO()
            self.annotations = []
            self.last_pos = None
            self.code = {}
        else:
            # When creating an insertion point, keep references to the same database
            self.annotation_buffer = create_from.annotation_buffer
            self.annotations = create_from.annotations
            self.code = create_from.code
            self.last_pos = create_from.last_pos

    def create_new(self, create_from, buffer, copy_formatting):
        return AnnotationCCodeWriter(create_from, buffer, copy_formatting)

    def write(self, s):
        CCodeWriter.write(self, s)
        self.annotation_buffer.write(s)

    def mark_pos(self, pos):
        if pos is not None:
            CCodeWriter.mark_pos(self, pos)
        if self.last_pos:
            pos_code = self.code.setdefault(self.last_pos[0].filename,{})
            code = pos_code.get(self.last_pos[1], "")
            pos_code[self.last_pos[1]] = code + self.annotation_buffer.getvalue()
        self.annotation_buffer = StringIO()
        self.last_pos = pos

    def annotate(self, pos, item):
        self.annotations.append((pos, item))

    def _css(self):
        """css template will later allow to choose a colormap"""
        css = [self._css_template]
        for i in range(255):
            color = u"FFFF%02x" % int(255/(1+i/10.0))
            css.append('\n.cython.score-%d {background-color: #%s;}' % (i, color))

        return ''.join(css)

    _js = """
    function toggleDiv(id) {
        theDiv = id.nextElementSibling
        if (theDiv.style.display != 'block') theDiv.style.display = 'block';
        else theDiv.style.display = 'none';
    }
    """.strip()

    _css_template = textwrap.dedent("""
        body.cython { font-family: courier; font-size: 12; }

        .cython.tag  {  }
        .cython.line { margin: 0em }
        .cython.code  { font-size: 9; color: #444444; display: none; margin-left: 20px; }

        .cython.code .py_c_api  { color: red; }
        .cython.code .py_macro_api  { color: #FF7000; }
        .cython.code .pyx_c_api  { color: #FF3000; }
        .cython.code .pyx_macro_api  { color: #FF7000; }
        .cython.code .refnanny  { color: #FFA000; }
        .cython.code .error_goto  { color: #FFA000; }

        .cython.code .coerce  { color: #008000; border: 1px dotted #008000 }
        .cython.code .py_attr { color: #FF0000; font-weight: bold; }
        .cython.code .c_attr  { color: #0000FF; }
        .cython.code .py_call { color: #FF0000; font-weight: bold; }
        .cython.code .c_call  { color: #0000FF; }
    """)

    def save_annotation(self, source_filename, target_filename):
        with Utils.open_source_file(source_filename) as f:
            lines = f.readlines()
        code_source_file = self.code.get(source_filename, {})
        c_file = Utils.decode_filename(os.path.basename(target_filename))
        html_filename = os.path.splitext(target_filename)[0] + ".html"
        with codecs.open(html_filename, "w", encoding="UTF-8") as out_buffer:
            out_buffer.write(self._save_annotation(lines, code_source_file, c_file))

    def _save_annotation_header(self, c_file):
        outlist = [
            textwrap.dedent(u'''\
            <!DOCTYPE html>

                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Generated by Cython {watermark}

            ''').format(css=self._css(), js=self._js, watermark=Version.watermark)
        ]
        if c_file:
            outlist.append(u'
Raw output: %s
\n' % (c_file, c_file))
        return outlist

    def _save_annotation_footer(self):
        return (u'\n',)

    def _save_annotation(self, lines, code_source_file, c_file=None):
        """
        lines : original cython source code split by lines
        code_source_file : generated c code keyed by line number in original file
        target filename : name of the file in which to store the generated html
        c_file : filename in which the c_code has been written
        """
        outlist = []
        outlist.extend(self._save_annotation_header(c_file))
        outlist.extend(self._save_annotation_body(lines, code_source_file))
        outlist.extend(self._save_annotation_footer())
        return ''.join(outlist)

    def _save_annotation_body(self, lines, code_source_file):
        outlist = []
        pos_comment_marker = u'/* \N{HORIZONTAL ELLIPSIS} */\n'
        new_calls_map = dict(
            (name, 0) for name in
            'refnanny py_macro_api py_c_api pyx_macro_api pyx_c_api error_goto'.split()
        ).copy

        self.mark_pos(None)

        def annotate(match):
            group_name = match.lastgroup
            calls[group_name] += 1
            return ur"%s" % (
                group_name, match.group(group_name))

        for k, line in enumerate(lines, 1):
            line = html_escape(line)
            try:
                code = code_source_file[k]
            except KeyError:
                code = ''
            else:
                code = _replace_pos_comment(pos_comment_marker, code)
                if code.startswith(pos_comment_marker):
                    code = code[len(pos_comment_marker):]
                code = html_escape(code)

            calls = new_calls_map()
            code = _parse_code(annotate, code)
            score = (5 * calls['py_c_api'] + 2 * calls['pyx_c_api'] +
                     calls['py_macro_api'] + calls['pyx_macro_api'])

            outlist.append(u"
" % score)

            outlist.append(u" %d: " % k)
            outlist.append(line.rstrip())

            outlist.append(u'
\n')
            outlist.append(u"
%s
" % (score, code))
        return outlist

_parse_code = re.compile(
    ur'(?P<refnanny>__Pyx_X?(?:GOT|GIVE)REF|__Pyx_RefNanny[A-Za-z]+)|'
    ur'(?:'
    ur'(?P<pyx_macro_api>__Pyx_[A-Z][A-Z_]+)|'
    ur'(?P<pyx_c_api>__Pyx_[A-Z][a-z_][A-Za-z_]+)|'
    ur'(?P<py_macro_api>Py[A-Z][a-z]+_[A-Z][A-Z_]+)|'
    ur'(?P<py_c_api>Py[A-Z][a-z]+_[A-Z][a-z][A-Za-z_]+)'
    ur')(?=\()|'       # look-ahead to exclude subsequent '(' from replacement
    ur'(?P<error_goto>(?:(?<=;) *if .* +)?\{__pyx_filename = .*goto __pyx_L\w+;\})'
).sub

_replace_pos_comment = re.compile(
    # this matches what Cython generates as code line marker comment
    ur'^\s*/\*(?:(?:[^*]|\*[^/])*\n)+\s*\*/\s*\n',
    re.M
).sub

class AnnotationItem(object):

    def __init__(self, style, text, tag="", size=0):
        self.style = style
        self.text = text
        self.tag = tag
        self.size = size

    def start(self):
        return u"%s" % (self.style, self.text, self.tag)

    def end(self):
        return self.size, u""
<commit_msg>tweak templating in annotated code writer some more<commit_after># Note: Work in progress

import os
import re
import codecs
import textwrap
from xml.sax.saxutils import escape as html_escape
from StringIO import StringIO

import Version
from Code import CCodeWriter
from Cython import Utils

class AnnotationCCodeWriter(CCodeWriter):

    def __init__(self, create_from=None, buffer=None, copy_formatting=True):
        CCodeWriter.__init__(self, create_from, buffer, copy_formatting=True)
        if create_from is None:
            self.annotation_buffer = StringIO()
            self.annotations = []
            self.last_pos = None
            self.code = {}
        else:
            # When creating an insertion point, keep references to the same database
            self.annotation_buffer = create_from.annotation_buffer
            self.annotations = create_from.annotations
            self.code = create_from.code
            self.last_pos = create_from.last_pos

    def create_new(self, create_from, buffer, copy_formatting):
        return AnnotationCCodeWriter(create_from, buffer, copy_formatting)

    def write(self, s):
        CCodeWriter.write(self, s)
        self.annotation_buffer.write(s)

    def mark_pos(self, pos):
        if pos is not None:
            CCodeWriter.mark_pos(self, pos)
        if self.last_pos:
            pos_code = self.code.setdefault(self.last_pos[0].filename,{})
            code = pos_code.get(self.last_pos[1], "")
            pos_code[self.last_pos[1]] = code + self.annotation_buffer.getvalue()
        self.annotation_buffer = StringIO()
        self.last_pos = pos

    def annotate(self, pos, item):
        self.annotations.append((pos, item))

    def _css(self):
        """css template will later allow to choose a colormap"""
        css = [self._css_template]
        for i in range(255):
            color = u"FFFF%02x" % int(255/(1+i/10.0))
            css.append('\n.cython.score-%d {background-color: #%s;}' % (i, color))

        return ''.join(css)

    _js = """
    function toggleDiv(id) {
        theDiv = id.nextElementSibling
        if (theDiv.style.display != 'block') theDiv.style.display = 'block';
        else theDiv.style.display = 'none';
    }
    """.strip()

    _css_template = textwrap.dedent("""
        body.cython { font-family: courier; font-size: 12; }

        .cython.tag  {  }
        .cython.line { margin: 0em }
        .cython.code  { font-size: 9; color: #444444; display: none; margin-left: 20px; }

        .cython.code .py_c_api  { color: red; }
        .cython.code .py_macro_api  { color: #FF7000; }
        .cython.code .pyx_c_api  { color: #FF3000; }
        .cython.code .pyx_macro_api  { color: #FF7000; }
        .cython.code .refnanny  { color: #FFA000; }
        .cython.code .error_goto  { color: #FFA000; }

        .cython.code .coerce  { color: #008000; border: 1px dotted #008000 }
        .cython.code .py_attr { color: #FF0000; font-weight: bold; }
        .cython.code .c_attr  { color: #0000FF; }
        .cython.code .py_call { color: #FF0000; font-weight: bold; }
        .cython.code .c_call  { color: #0000FF; }
    """)

    def save_annotation(self, source_filename, target_filename):
        with Utils.open_source_file(source_filename) as f:
            lines = f.readlines()
        code_source_file = self.code.get(source_filename, {})
        c_file = Utils.decode_filename(os.path.basename(target_filename))
        html_filename = os.path.splitext(target_filename)[0] + ".html"
        with codecs.open(html_filename, "w", encoding="UTF-8") as out_buffer:
            out_buffer.write(self._save_annotation(lines, code_source_file, c_file))

    def _save_annotation_header(self, c_file):
        outlist = [
            textwrap.dedent(u'''\
            <!DOCTYPE html>

                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Generated by Cython {watermark}

            ''').format(css=self._css(), js=self._js, watermark=Version.watermark)
        ]
        if c_file:
            outlist.append(u'
Raw output: %s
\n' % (c_file, c_file))
        return outlist

    def _save_annotation_footer(self):
        return (u'\n',)

    def _save_annotation(self, lines, code_source_file, c_file=None):
        """
        lines : original cython source code split by lines
        code_source_file : generated c code keyed by line number in original file
        target filename : name of the file in which to store the generated html
        c_file : filename in which the c_code has been written
        """
        outlist = []
        outlist.extend(self._save_annotation_header(c_file))
        outlist.extend(self._save_annotation_body(lines, code_source_file))
        outlist.extend(self._save_annotation_footer())
        return ''.join(outlist)

    def _save_annotation_body(self, lines, code_source_file):
        outlist = []
        pos_comment_marker = u'/* \N{HORIZONTAL ELLIPSIS} */\n'
        new_calls_map = dict(
            (name, 0) for name in
            'refnanny py_macro_api py_c_api pyx_macro_api pyx_c_api error_goto'.split()
        ).copy

        self.mark_pos(None)

        def annotate(match):
            group_name = match.lastgroup
            calls[group_name] += 1
            return ur"%s" % (
                group_name, match.group(group_name))

        for k, line in enumerate(lines, 1):
            line = html_escape(line)
            try:
                code = code_source_file[k]
            except KeyError:
                code = ''
            else:
                code = _replace_pos_comment(pos_comment_marker, code)
                if code.startswith(pos_comment_marker):
                    code = code[len(pos_comment_marker):]
                code = html_escape(code)

            calls = new_calls_map()
            code = _parse_code(annotate, code)
            score = (5 * calls['py_c_api'] + 2 * calls['pyx_c_api'] +
                     calls['py_macro_api'] + calls['pyx_macro_api'])

            outlist.append(u"
 %d: %s
\n" % (
                score, k, line.rstrip()))
            outlist.append(u"
%s
" % (score, code))
        return outlist

_parse_code = re.compile(
    ur'(?P<refnanny>__Pyx_X?(?:GOT|GIVE)REF|__Pyx_RefNanny[A-Za-z]+)|'
    ur'(?:'
    ur'(?P<pyx_macro_api>__Pyx_[A-Z][A-Z_]+)|'
    ur'(?P<pyx_c_api>__Pyx_[A-Z][a-z_][A-Za-z_]+)|'
    ur'(?P<py_macro_api>Py[A-Z][a-z]+_[A-Z][A-Z_]+)|'
    ur'(?P<py_c_api>Py[A-Z][a-z]+_[A-Z][a-z][A-Za-z_]+)'
    ur')(?=\()|'       # look-ahead to exclude subsequent '(' from replacement
    ur'(?P<error_goto>(?:(?<=;) *if .* +)?\{__pyx_filename = .*goto __pyx_L\w+;\})'
).sub

_replace_pos_comment = re.compile(
    # this matches what Cython generates as code line marker comment
    ur'^\s*/\*(?:(?:[^*]|\*[^/])*\n)+\s*\*/\s*\n',
    re.M
).sub

class AnnotationItem(object):

    def __init__(self, style, text, tag="", size=0):
        self.style = style
        self.text = text
        self.tag = tag
        self.size = size

    def start(self):
        return u"%s" % (self.style, self.text, self.tag)

    def end(self):
        return self.size, u""
<|endoftext|>


<commit_before># Note: Work in progress

import os
import re
import time
import codecs
from StringIO import StringIO

import Version
from Code import CCodeWriter
from Cython import Utils

# need one-characters subsitutions (for now) so offsets aren't off
special_chars = [
    (u'&', u'\xF2', u'&'),
    (u'<', u'\xF0', u'<'),
    (u'>', u'\xF1', u'>'),
]

line_pos_comment = re.compile(r'/\*.*?<<<<<<<<<<<<<<.*?\*/\n*', re.DOTALL)

class AnnotationCCodeWriter(CCodeWriter):

    def __init__(self, create_from=None, buffer=None, copy_formatting=True):
        CCodeWriter.__init__(self, create_from, buffer, copy_formatting=True)
        if create_from is None:
            self.annotation_buffer = StringIO()
            self.annotations = []
            self.last_pos = None
            self.code = {}
        else:
            # When creating an insertion point, keep references to the same database
            self.annotation_buffer = create_from.annotation_buffer
            self.annotations = create_from.annotations
            self.code = create_from.code
            self.last_pos = create_from.last_pos

    def create_new(self, create_from, buffer, copy_formatting):
        return AnnotationCCodeWriter(create_from, buffer, copy_formatting)

    def write(self, s):
        CCodeWriter.write(self, s)
        self.annotation_buffer.write(s)

    def mark_pos(self, pos):
        if pos is not None:
            CCodeWriter.mark_pos(self, pos)
        if self.last_pos:
            pos_code = self.code.setdefault(self.last_pos[0].filename,{})
            code = pos_code.get(self.last_pos[1], "")
            pos_code[self.last_pos[1]] = code + self.annotation_buffer.getvalue()
        self.annotation_buffer = StringIO()
        self.last_pos = pos

    def annotate(self, pos, item):
        self.annotations.append((pos, item))

    def save_annotation(self, source_filename, target_filename):
        self.mark_pos(None)
        f = Utils.open_source_file(source_filename)
        lines = f.readlines()
        for k, line in enumerate(lines):
            for c, cc, html in special_chars:
                line = line.replace(c, cc)
            lines[k] = line
        f.close()
        all = []
        if False:
            for pos, item in self.annotations:
                if pos[0].filename == source_filename:
                    start = item.start()
                    size, end = item.end()
                    if size:
                        all.append((pos, start))
                        all.append(((source_filename, pos[1], pos[2]+size), end))
                    else:
                        all.append((pos, start+end))

        all.sort()
        all.reverse()
        for pos, item in all:
            _, line_no, col = pos
            line_no -= 1
            col += 1
            line = lines[line_no]
            lines[line_no] = line[:col] + item + line[col:]

        html_filename = os.path.splitext(target_filename)[0] + ".html"
        f = codecs.open(html_filename, "w", encoding="UTF-8")
        f.write(u'<!DOCTYPE html>\n')
        f.write(u'\n' % Version.watermark)
        f.write(u'\n')
        f.write(u"""

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        """)
        f.write(u'\n')
        f.write(u'
Generated by Cython %s\n' % Version.watermark)
        c_file = Utils.decode_filename(os.path.basename(target_filename))
        f.write(u'
Raw output: %s\n' % (c_file, c_file))
        k = 0

        py_c_api = re.compile(u'(Py[A-Z][a-z]+_[A-Z][a-z][A-Za-z_]+)\(')
        py_marco_api = re.compile(u'(Py[A-Z][a-z]+_[A-Z][A-Z_]+)\(')
        pyx_c_api = re.compile(u'(__Pyx_[A-Z][a-z_][A-Za-z_]+)\(')
        pyx_macro_api = re.compile(u'(__Pyx_[A-Z][A-Z_]+)\(')
        error_goto = re.compile(ur'((; *if .*)? \{__pyx_filename = .*goto __pyx_L\w+;\})')
        refnanny = re.compile(u'(__Pyx_X?(GOT|GIVE)REF|__Pyx_RefNanny[A-Za-z]+)')

        code_source_file = self.code[source_filename]
        for line in lines:

            k += 1
            try:
                code = code_source_file[k]
            except KeyError:
                code = ''
            else:
                for c, cc, html in special_chars:
                    code = code.replace(c, html)

            code, py_c_api_calls = py_c_api.subn(ur"\1(", code)
            code, pyx_c_api_calls = pyx_c_api.subn(ur"\1(", code)
            code, py_macro_api_calls = py_marco_api.subn(ur"\1(", code)
            code, pyx_macro_api_calls = pyx_macro_api.subn(ur"\1(", code)
            code, refnanny_calls = refnanny.subn(ur"\1", code)
            code, error_goto_calls = error_goto.subn(ur"\1", code)

            code = code.replace(u";", u";")

            score = 5*py_c_api_calls + 2*pyx_c_api_calls + py_macro_api_calls + pyx_macro_api_calls - refnanny_calls
            color = u"FFFF%02x" % int(255/(1+score/10.0))
            f.write(u"
" % (color, k))

            f.write(u" %d: " % k)
            for c, cc, html in special_chars:
                line = line.replace(cc, html)
            f.write(line.rstrip())

            f.write(u'
\n')
            code = re.sub(line_pos_comment, '', code) # inline annotations are redundant
            f.write(u"
%s
" % (k, color, code))
        f.write(u'\n')
        f.close()

# TODO: make this cleaner
def escape(raw_string):
    raw_string = raw_string.replace(u"\'", ur"’")
    raw_string = raw_string.replace(u'\"', ur'"')
    raw_string = raw_string.replace(u'\n', ur'
\n')
    raw_string = raw_string.replace(u'\t', ur'\t')
    return raw_string

class AnnotationItem(object):

    def __init__(self, style, text, tag="", size=0):
        self.style = style
        self.text = text
        self.tag = tag
        self.size = size

    def start(self):
        return u"%s" % (self.style, self.text, self.tag)

    def end(self):
        return self.size, u""
<commit_msg>minor code cleanup<commit_after># Note: Work in progress

import os
import re
import time
import codecs
from StringIO import StringIO

import Version
from Code import CCodeWriter
from Cython import Utils

# need one-characters subsitutions (for now) so offsets aren't off
special_chars = [
    (u'&', u'\xF2', u'&'),
    (u'<', u'\xF0', u'<'),
    (u'>', u'\xF1', u'>'),
]

line_pos_comment = re.compile(r'/\*.*?<<<<<<<<<<<<<<.*?\*/\n*', re.DOTALL)

class AnnotationCCodeWriter(CCodeWriter):

    def __init__(self, create_from=None, buffer=None, copy_formatting=True):
        CCodeWriter.__init__(self, create_from, buffer, copy_formatting=True)
        if create_from is None:
            self.annotation_buffer = StringIO()
            self.annotations = []
            self.last_pos = None
            self.code = {}
        else:
            # When creating an insertion point, keep references to the same database
            self.annotation_buffer = create_from.annotation_buffer
            self.annotations = create_from.annotations
            self.code = create_from.code
            self.last_pos = create_from.last_pos

    def create_new(self, create_from, buffer, copy_formatting):
        return AnnotationCCodeWriter(create_from, buffer, copy_formatting)

    def write(self, s):
        CCodeWriter.write(self, s)
        self.annotation_buffer.write(s)

    def mark_pos(self, pos):
        if pos is not None:
            CCodeWriter.mark_pos(self, pos)
        if self.last_pos:
            pos_code = self.code.setdefault(self.last_pos[0].filename,{})
            code = pos_code.get(self.last_pos[1], "")
            pos_code[self.last_pos[1]] = code + self.annotation_buffer.getvalue()
        self.annotation_buffer = StringIO()
        self.last_pos = pos

    def annotate(self, pos, item):
        self.annotations.append((pos, item))

    def save_annotation(self, source_filename, target_filename):
        self.mark_pos(None)
        f = Utils.open_source_file(source_filename)
        lines = f.readlines()
        for k, line in enumerate(lines):
            for c, cc, html in special_chars:
                line = line.replace(c, cc)
            lines[k] = line
        f.close()
        all = []
        if False:
            for pos, item in self.annotations:
                if pos[0].filename == source_filename:
                    start = item.start()
                    size, end = item.end()
                    if size:
                        all.append((pos, start))
                        all.append(((source_filename, pos[1], pos[2]+size), end))
                    else:
                        all.append((pos, start+end))

        all.sort(reverse=True)
        for pos, item in all:
            _, line_no, col = pos
            line_no -= 1
            col += 1
            line = lines[line_no]
            lines[line_no] = line[:col] + item + line[col:]

        html_filename = os.path.splitext(target_filename)[0] + ".html"
        f = codecs.open(html_filename, "w", encoding="UTF-8")
        f.write(u'<!DOCTYPE html>\n')
        f.write(u'\n' % Version.watermark)
        f.write(u'\n')
        f.write(u"""

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        """)
        f.write(u'\n')
        f.write(u'
Generated by Cython %s\n' % Version.watermark)
        c_file = Utils.decode_filename(os.path.basename(target_filename))
        f.write(u'
Raw output: %s\n' % (c_file, c_file))
        k = 0

        py_c_api = re.compile(u'(Py[A-Z][a-z]+_[A-Z][a-z][A-Za-z_]+)\(')
        py_marco_api = re.compile(u'(Py[A-Z][a-z]+_[A-Z][A-Z_]+)\(')
        pyx_c_api = re.compile(u'(__Pyx_[A-Z][a-z_][A-Za-z_]+)\(')
        pyx_macro_api = re.compile(u'(__Pyx_[A-Z][A-Z_]+)\(')
        error_goto = re.compile(ur'((; *if .*)? \{__pyx_filename = .*goto __pyx_L\w+;\})')
        refnanny = re.compile(u'(__Pyx_X?(GOT|GIVE)REF|__Pyx_RefNanny[A-Za-z]+)')

        code_source_file = self.code[source_filename]
        for line in lines:

            k += 1
            try:
                code = code_source_file[k]
            except KeyError:
                code = ''
            else:
                for c, cc, html in special_chars:
                    code = code.replace(c, html)

            code, py_c_api_calls = py_c_api.subn(ur"\1(", code)
            code, pyx_c_api_calls = pyx_c_api.subn(ur"\1(", code)
            code, py_macro_api_calls = py_marco_api.subn(ur"\1(", code)
            code, pyx_macro_api_calls = pyx_macro_api.subn(ur"\1(", code)
            code, refnanny_calls = refnanny.subn(ur"\1", code)
            code, error_goto_calls = error_goto.subn(ur"\1", code)

            code = code.replace(u";", u";")

            score = 5*py_c_api_calls + 2*pyx_c_api_calls + py_macro_api_calls + pyx_macro_api_calls - refnanny_calls
            color = u"FFFF%02x" % int(255/(1+score/10.0))
            f.write(u"
" % (color, k))

            f.write(u" %d: " % k)
            for c, cc, html in special_chars:
                line = line.replace(cc, html)
            f.write(line.rstrip())

            f.write(u'
\n')
            code = re.sub(line_pos_comment, '', code) # inline annotations are redundant
            f.write(u"
%s
" % (k, color, code))
        f.write(u'\n')
        f.close()

# TODO: make this cleaner
def escape(raw_string):
    raw_string = raw_string.replace(u"\'", ur"’")
    raw_string = raw_string.replace(u'\"', ur'"')
    raw_string = raw_string.replace(u'\n', ur'
\n')
    raw_string = raw_string.replace(u'\t', ur'\t')
    return raw_string

class AnnotationItem(object):

    def __init__(self, style, text, tag="", size=0):
        self.style = style
        self.text = text
        self.tag = tag
        self.size = size

    def start(self):
        return u"%s" % (self.style, self.text, self.tag)

    def end(self):
        return self.size, u""
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from configs.module import Module
import moduleregistry
import running
import utils

def init():
    m = Module('reloader')
    m.set_help('Reload various parts of the bot.')
    m.add_timer_hook(2.5 * 1000, timer)
    m.add_command_hook('reloadall',
        {
            'function': reloadall,
            'rights': ['admin'],
            'help': 'Reload all possible modules.',
            'args': [],
        })
    m.add_command_hook('reload',
        {
            'function': reload_list,
            'rights': ['admin'],
            'help': 'Reload a comma seperated list of modules.',
            'args': [
                {
                    'name': 'modules',
                    'optional': False,
                    'help': 'Modules to reload.'
                    },
                ],
        })
    m.add_short_command_hook(reinit,
        'reinit::Reload all possible files.', [], 75)
    return m

def timer():
    if moduleregistry.reloadscheduled:
        moduleregistry.reloadscheduled = False
        moduleregistry.reload_all()
        for server in running.working_servers:
            server.reinit()

def reinit(fp, args):
    running.reinit = True
    return "Re-connecting to all servers."

def reloadall(fp, args):
    moduleregistry.reloadscheduled = False
    moduleregistry.reload_all()
    for server in running.working_servers:
        server.reinit()
    return(('Reloaded all registered modules.'))

def reload_list(fp, args):
    rlist = args.getlinstr('modules').split(',')
    badlist = []
    goodlist = []
    for server in running.working_servers:
        for i in rlist:
            try:
                server.add_module(i)
                if i not in goodlist:
                    goodlist.append(i)
            except ImportError:
                if i not in badlist:
                    badlist.append(i)
        server.load_commands()
    bt = '; could not reload: %s' % utils.ltos(badlist)
    if not badlist:
        bt = ''
    return('Reloaded: %s%s.' % (utils.ltos(goodlist), bt))

<commit_msg>fix missing admin right requirement in reinit<commit_after># -*- coding: utf-8 -*-
from configs.module import Module
import moduleregistry
import running
import utils

def init():
    m = Module('reloader')
    m.set_help('Reload various parts of the bot.')
    m.add_timer_hook(2.5 * 1000, timer)
    m.add_command_hook('reloadall',
        {
            'function': reloadall,
            'rights': ['admin'],
            'help': 'Reload all possible modules.',
            'args': [],
        })
    m.add_command_hook('reload',
        {
            'function': reload_list,
            'rights': ['admin'],
            'help': 'Reload a comma seperated list of modules.',
            'args': [
                {
                    'name': 'modules',
                    'optional': False,
                    'help': 'Modules to reload.'
                    },
                ],
        })
    m.add_short_command_hook(reinit,
        'reinit::Reload all possible files.', [], ['admin'])
    return m

def timer():
    if moduleregistry.reloadscheduled:
        moduleregistry.reloadscheduled = False
        moduleregistry.reload_all()
        for server in running.working_servers:
            server.reinit()

def reinit(fp, args):
    running.reinit = True
    return "Re-connecting to all servers."

def reloadall(fp, args):
    moduleregistry.reloadscheduled = False
    moduleregistry.reload_all()
    for server in running.working_servers:
        server.reinit()
    return(('Reloaded all registered modules.'))

def reload_list(fp, args):
    rlist = args.getlinstr('modules').split(',')
    badlist = []
    goodlist = []
    for server in running.working_servers:
        for i in rlist:
            try:
                server.add_module(i)
                if i not in goodlist:
                    goodlist.append(i)
            except ImportError:
                if i not in badlist:
                    badlist.append(i)
        server.load_commands()
    bt = '; could not reload: %s' % utils.ltos(badlist)
    if not badlist:
        bt = ''
    return('Reloaded: %s%s.' % (utils.ltos(goodlist), bt))

<|endoftext|>


<commit_before>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

from robot import utils
from robot.errors import DataError
from robot.output import LOGGER

_ERR = "Error in file '%s' in table '%s' in element on row %d: %s"

class _Table:

    def __init__(self, name, source, data):
        self._name = name
        self._source = source
        self._row = 0
        self._data = data

    def add_row(self, cells, repeat=1):
        if not cells:
            self._row += repeat
            return
        try:
            for i in range(repeat):
                self._row += 1
                self._add_row(cells[0], cells[1:])
        except:
            self.report_invalid_syntax(self._row, utils.get_error_message())

    def report_invalid_syntax(self, row, error, level='ERROR'):
        msg = _ERR % (self._source, self._name, row, error)
        LOGGER.write(msg, level)

class SimpleTable(_Table):

    def _add_row(self, name, value):
        if name == '...':
            try:
                self._data[-1].extend(value)
            except IndexError:
                raise DataError('Invalid multirow usage: No item started')
        else:
            item = SimpleItem(name, value, self._row, self)
            self._data.append(item)

class ComplexTable(_Table):

    def __init__(self, name, source, data):
        _Table.__init__(self, name, source, data)
        self._item = None

    def _add_row(self, name, data):
        if name != '':
            self._item = ComplexItem(name, self._row, self)
            self._data.append(self._item)
        if self._item is None:
            raise DataError('No name specified')
        self._item.add_subitem(data)

class _Item:

    def __init__(self, name, row, parent):
        self.name = name
        self._row = row
        self._parent = parent

    def report_invalid_syntax(self, error=None, level='ERROR'):
        if error is None:
            error = utils.get_error_message()
        self._parent.report_invalid_syntax(self._row, error, level)

class SimpleItem(_Item):

    def __init__(self, name, value, row, parent):
        _Item.__init__(self, name, row, parent)
        self.value = value

    def extend(self, value):
        self.value.extend(value)

    def copy(self):
        return SimpleItem(self.name, self.value[:], self._row, self._parent)

class ComplexItem(_Item):
    """Represents one item in Test Case or Keyword table"""

    def __init__(self, name, row, parent):
        _Item.__init__(self, name, row, parent)
        self.metadata = []
        self.keywords = []
        self._previous = None
        self._current_row = self._row - 1

    def add_subitem(self, data):
        self._current_row += 1
        if not data:
            return
        name = data[0]
        if name == '...':
            self._add_to_previous(data[1:])
        elif name == '' and len(data) > 1 and data[1] == '...':
            self._add_to_previous(data[2:])
        elif name.startswith('[') and name.endswith(']'):
            name = name[1:-1].strip()   # removes '[' and ']'
            item = SimpleItem(name, data[1:], self._current_row, self._parent)
            self.metadata.append(item)
            self._previous = self.metadata
        else:
            self.keywords.append(data)
            self._previous = self.keywords

    def _add_to_previous(self, data):
        if self._previous is None:
            raise DataError('Invalid multirow usage: No item started')
        self._previous[-1].extend(data)
<commit_msg>Allow first data cell to be escaped when continuing lines with '...' (issue 420)<commit_after>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

from robot import utils
from robot.errors import DataError
from robot.output import LOGGER

_ERR = "Error in file '%s' in table '%s' in element on row %d: %s"

class _Table:

    def __init__(self, name, source, data):
        self._name = name
        self._source = source
        self._row = 0
        self._data = data

    def add_row(self, cells, repeat=1):
        if not cells:
            self._row += repeat
            return
        try:
            for _ in range(repeat):
                self._row += 1
                self._add_row(cells[0], cells[1:])
        except:
            self.report_invalid_syntax(self._row, utils.get_error_message())

    def report_invalid_syntax(self, row, error, level='ERROR'):
        msg = _ERR % (self._source, self._name, row, error)
        LOGGER.write(msg, level)

class SimpleTable(_Table):

    def _add_row(self, name, value):
        if name == '...':
            try:
                self._data[-1].extend(value)
            except IndexError:
                raise DataError('Invalid multirow usage: No item started')
        else:
            item = SimpleItem(name, value, self._row, self)
            self._data.append(item)

class ComplexTable(_Table):

    def __init__(self, name, source, data):
        _Table.__init__(self, name, source, data)
        self._item = None

    def _add_row(self, name, data):
        if name != '':
            self._item = ComplexItem(name, self._row, self)
            self._data.append(self._item)
        if self._item is None:
            raise DataError('No name specified')
        self._item.add_subitem(data)

class _Item:

    def __init__(self, name, row, parent):
        self.name = name
        self._row = row
        self._parent = parent

    def report_invalid_syntax(self, error=None, level='ERROR'):
        if error is None:
            error = utils.get_error_message()
        self._parent.report_invalid_syntax(self._row, error, level)

class SimpleItem(_Item):

    def __init__(self, name, value, row, parent):
        _Item.__init__(self, name, row, parent)
        self.value = value

    def extend(self, value):
        self.value.extend(value)

    def copy(self):
        return SimpleItem(self.name, self.value[:], self._row, self._parent)

class ComplexItem(_Item):
    """Represents one item in Test Case or Keyword table"""

    def __init__(self, name, row, parent):
        _Item.__init__(self, name, row, parent)
        self.metadata = []
        self.keywords = []
        self._previous = None
        self._current_row = self._row - 1

    def add_subitem(self, data):
        self._current_row += 1
        if not data:
            return
        name = data[0]
        if name == '...':
            self._add_to_previous(data[1:])
        elif name in ['', '\\'] and len(data) > 1 and data[1] == '...':
            self._add_to_previous(data[2:])
        elif name.startswith('[') and name.endswith(']'):
            name = name[1:-1].strip()   # removes '[' and ']'
            item = SimpleItem(name, data[1:], self._current_row, self._parent)
            self.metadata.append(item)
            self._previous = self.metadata
        else:
            self.keywords.append(data)
            self._previous = self.keywords

    def _add_to_previous(self, data):
        if self._previous is None:
            raise DataError('Invalid multirow usage: No item started')
        self._previous[-1].extend(data)
<|endoftext|>


<commit_before>#!/usr/bin/python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ansible.module_utils.basic import *
from ansible.module_utils.dci_common import *

import os

try:
    from dciclient.v1.api import component as dci_component
    from dciclient.v1.api import topic as dci_topic
except ImportError:
    dciclient_found = False
else:
    dciclient_found = True

DOCUMENTATION = '''
---
module: dci_component
short_description: module to interact with the components endpoint of DCI
description:
  - DCI module to manage the component resources
version_added: 2.2
options:
  state:
    required: false
    default: present
    description: Desired state of the resource
  dci_login:
    required: false
    description: User's DCI login
  dci_password:
    required: false
    description: User's DCI password
  dci_cs_url:
    required: false
    description: DCI Control Server URL
  dest:
    required: true
    description: Path where to drop the retrieved component
  active:
    required: false
    description: Wether of not the resource should be active
  embed:
    required: false
    description:
      - List of field to embed within the retrieved resource
'''

EXAMPLES = '''
- name: Download a component
  dci_component:
    id: {{ component_id }}
    dest: /srv/dci/components/{{ component_id }}

- name: Retrieve component informations
  dci_component:
    id: {{ component_id }}

- name: Remove component
  dci_component:
    state: absent
    id: {{ component_id }}
'''

# TODO
RETURN = '''
'''

def download_file(module, ctx):
    if os.path.isdir(module.params['dest']):
        dest_file = os.path.join(
            module.params['dest'],
            module.params['id'] + '.tar')
    else:
        dest_file = module.params['dest']

    if os.path.isfile(dest_file):
        module.exit_json(changed=False)
    else:
        try:
            component_files = dci_component.file_list(
                ctx,
                module.params['id']).json()['component_files']
            component_file_id = component_files[0]['id']
        except (KeyError, IndexError):
            module.fail_json(
                msg='Failed to get the component_files from the server.')
        return dci_component.file_download(
            ctx,
            module.params['id'],
            component_file_id,
            dest_file)

def main():

    resource_argument_spec = dict(
        state=dict(
            default='present',
            choices=['present', 'absent'],
            type='str'),
        id=dict(type='str'),
        dest=dict(type='str'),
        name=dict(type='str'),
        type=dict(type='str'),
        canonical_project_name=dict(type='str'),
        url=dict(type='str'),
        data=dict(type='dict'),
        topic_id=dict(type='str'),
        path=dict(type='str'),
        active=dict(default=True, type='bool'),
        embed=dict(type='str'),
    )
    resource_argument_spec.update(authentication_argument_spec())

    module = AnsibleModule(
        argument_spec=resource_argument_spec,
        required_if=[['state', 'absent', ['id']]]
    )

    if not dciclient_found:
        module.fail_json(msg='The python dciclient module is required')

    ctx = build_dci_context(module)

    # Action required: Delete the component matching the component id
    # Endpoint called: /components/<component_id> DELETE
    #                  via dci_component.delete()
    #
    # If the component exist and it has been succesfully deleted the changed is
    # set to true, else if the file does not exist changed is set to False
    if module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_component.delete(ctx, module.params['id'])

    # Action required: Attach a file to a component
    # Endpoint called: /components/<component_id>/files/ POST
    #                  via dci_component.file_upload()
    #
    # Attach file to a component
    elif module.params['path']:
        res = dci_component.file_upload(
            ctx,
            module.params['id'],
            module.params['path'])

    # Action required: Download a component
    # Endpoint called: /components/<component_id>/files/<file_id>/content GET
    #          via dci_component.file_download()
    #
    # Download the component
    elif module.params['dest']:
        res = download_file(module, ctx)

    # Action required: Get component informations
    # Endpoint called: /components/<component_id> GET
    #                  via dci_component.get()
    #
    # Get component informations
    elif module.params['id']:
        kwargs = {}
        if module.params['embed']:
            kwargs['embed'] = module.params['embed']
        res = dci_component.get(ctx, module.params['id'], **kwargs)

    # Action required: Update an existing component
    # Endpoint called: /components/<component_id> PUT
    #                  via dci_component.update()
    #
    # Update the component with the specified parameters.
    elif module.params['id']:
        res = dci_component.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 409]:
            updated_kwargs = {
                'id': module.params['id'],
                'etag': res.json()['component']['etag']
            }
            if module.params['active']:
                updated_kwargs['state'] = 'active'
            else:
                updated_kwargs['state'] = 'inactive'

            res = dci_component.update(ctx, **updated_kwargs)

    # Action required: Create a new component
    # Endpoint called: /component POST via dci_component.create()
    #
    # Create a new component
    else:
        if not module.params['name']:
            module.fail_json(msg='name parameter must be speficied')
        if not module.params['type']:
            module.fail_json(msg='type parameter must be speficied')

        kwargs = {
            'name': module.params['name'],
            'type': module.params['type'],
        }

        if module.params['canonical_project_name']:
            canonical_project_name = module.params['canonical_project_name']
            kwargs['canonical_project_name'] = canonical_project_name
        if module.params['url']:
            kwargs['url'] = module.params['url']
        if module.params['data']:
            kwargs['data'] = module.params['data']
        if module.params['topic_id']:
            kwargs['topic_id'] = module.params['topic_id']
        if module.params['team_id']:
            kwargs['team_id'] = module.params['team_id']
        kwargs['state'] = 'active' if module.params['active'] else 'inactive'
        res = dci_component.create(ctx, **kwargs)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code == 409:
            result = {
                'component': dci_topic.list_components(
                    ctx, module.params['topic_id'],
                    where='name:' + module.params['name']
                ).json()['components'][0],
            }
        if res.status_code in [400, 401, 409]:
            result['changed'] = False
        else:
            result['changed'] = True
    except Exception:
        result = {}
        result['changed'] = False

    module.exit_json(**result)

if __name__ == '__main__':
    main()
<commit_msg>components: add missing team_id parameter<commit_after>#!/usr/bin/python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ansible.module_utils.basic import *
from ansible.module_utils.dci_common import *

import os

try:
    from dciclient.v1.api import component as dci_component
    from dciclient.v1.api import topic as dci_topic
except ImportError:
    dciclient_found = False
else:
    dciclient_found = True

DOCUMENTATION = '''
---
module: dci_component
short_description: module to interact with the components endpoint of DCI
description:
  - DCI module to manage the component resources
version_added: 2.2
options:
  state:
    required: false
    default: present
    description: Desired state of the resource
  dci_login:
    required: false
    description: User's DCI login
  dci_password:
    required: false
    description: User's DCI password
  dci_cs_url:
    required: false
    description: DCI Control Server URL
  dest:
    required: true
    description: Path where to drop the retrieved component
  active:
    required: false
    description: Wether of not the resource should be active
  embed:
    required: false
    description:
      - List of field to embed within the retrieved resource
'''

EXAMPLES = '''
- name: Download a component
  dci_component:
    id: {{ component_id }}
    dest: /srv/dci/components/{{ component_id }}

- name: Retrieve component informations
  dci_component:
    id: {{ component_id }}

- name: Remove component
  dci_component:
    state: absent
    id: {{ component_id }}
'''

# TODO
RETURN = '''
'''

def download_file(module, ctx):
    if os.path.isdir(module.params['dest']):
        dest_file = os.path.join(
            module.params['dest'],
            module.params['id'] + '.tar')
    else:
        dest_file = module.params['dest']

    if os.path.isfile(dest_file):
        module.exit_json(changed=False)
    else:
        try:
            component_files = dci_component.file_list(
                ctx,
                module.params['id']).json()['component_files']
            component_file_id = component_files[0]['id']
        except (KeyError, IndexError):
            module.fail_json(
                msg='Failed to get the component_files from the server.')
        return dci_component.file_download(
            ctx,
            module.params['id'],
            component_file_id,
            dest_file)

def main():

    resource_argument_spec = dict(
        state=dict(
            default='present',
            choices=['present', 'absent'],
            type='str'),
        id=dict(type='str'),
        dest=dict(type='str'),
        name=dict(type='str'),
        type=dict(type='str'),
        canonical_project_name=dict(type='str'),
        url=dict(type='str'),
        data=dict(type='dict'),
        topic_id=dict(type='str'),
        team_id=dict(type='str'),
        path=dict(type='str'),
        active=dict(default=True, type='bool'),
        embed=dict(type='str'),
    )
    resource_argument_spec.update(authentication_argument_spec())

    module = AnsibleModule(
        argument_spec=resource_argument_spec,
        required_if=[['state', 'absent', ['id']]]
    )

    if not dciclient_found:
        module.fail_json(msg='The python dciclient module is required')

    ctx = build_dci_context(module)

    # Action required: Delete the component matching the component id
    # Endpoint called: /components/<component_id> DELETE
    #                  via dci_component.delete()
    #
    # If the component exist and it has been succesfully deleted the changed is
    # set to true, else if the file does not exist changed is set to False
    if module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_component.delete(ctx, module.params['id'])

    # Action required: Attach a file to a component
    # Endpoint called: /components/<component_id>/files/ POST
    #                  via dci_component.file_upload()
    #
    # Attach file to a component
    elif module.params['path']:
        res = dci_component.file_upload(
            ctx,
            module.params['id'],
            module.params['path'])

    # Action required: Download a component
    # Endpoint called: /components/<component_id>/files/<file_id>/content GET
    #          via dci_component.file_download()
    #
    # Download the component
    elif module.params['dest']:
        res = download_file(module, ctx)

    # Action required: Get component informations
    # Endpoint called: /components/<component_id> GET
    #                  via dci_component.get()
    #
    # Get component informations
    elif module.params['id']:
        kwargs = {}
        if module.params['embed']:
            kwargs['embed'] = module.params['embed']
        res = dci_component.get(ctx, module.params['id'], **kwargs)

    # Action required: Update an existing component
    # Endpoint called: /components/<component_id> PUT
    #                  via dci_component.update()
    #
    # Update the component with the specified parameters.
    elif module.params['id']:
        res = dci_component.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 409]:
            updated_kwargs = {
                'id': module.params['id'],
                'etag': res.json()['component']['etag']
            }
            if module.params['active']:
                updated_kwargs['state'] = 'active'
            else:
                updated_kwargs['state'] = 'inactive'

            res = dci_component.update(ctx, **updated_kwargs)

    # Action required: Create a new component
    # Endpoint called: /component POST via dci_component.create()
    #
    # Create a new component
    else:
        if not module.params['name']:
            module.fail_json(msg='name parameter must be speficied')
        if not module.params['type']:
            module.fail_json(msg='type parameter must be speficied')

        kwargs = {
            'name': module.params['name'],
            'type': module.params['type'],
        }

        if module.params['canonical_project_name']:
            canonical_project_name = module.params['canonical_project_name']
            kwargs['canonical_project_name'] = canonical_project_name
        if module.params['url']:
            kwargs['url'] = module.params['url']
        if module.params['data']:
            kwargs['data'] = module.params['data']
        if module.params['topic_id']:
            kwargs['topic_id'] = module.params['topic_id']
        if module.params['team_id']:
            kwargs['team_id'] = module.params['team_id']
        kwargs['state'] = 'active' if module.params['active'] else 'inactive'
        res = dci_component.create(ctx, **kwargs)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code == 409:
            result = {
                'component': dci_topic.list_components(
                    ctx, module.params['topic_id'],
                    where='name:' + module.params['name']
                ).json()['components'][0],
            }
        if res.status_code in [400, 401, 409]:
            result['changed'] = False
        else:
            result['changed'] = True
    except Exception:
        result = {}
        result['changed'] = False

    module.exit_json(**result)

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


<commit_before># Note: Work in progress

import os
import re
import codecs
from xml.sax.saxutils import escape as html_escape
from StringIO import StringIO

import Version
from Code import CCodeWriter
from Cython import Utils

# need one-characters subsitutions (for now) so offsets aren't off
special_chars = [
    (u'&', u'\xF2', u'&'),
    (u'<', u'\xF0', u'<'),
    (u'>', u'\xF1', u'>'),
]

line_pos_comment = re.compile(r'/\*.*?<<<<<<<<<<<<<<.*?\*/\n*', re.DOTALL)

class AnnotationCCodeWriter(CCodeWriter):

    def __init__(self, create_from=None, buffer=None, copy_formatting=True):
        CCodeWriter.__init__(self, create_from, buffer, copy_formatting=True)
        if create_from is None:
            self.annotation_buffer = StringIO()
            self.annotations = []
            self.last_pos = None
            self.code = {}
        else:
            # When creating an insertion point, keep references to the same database
            self.annotation_buffer = create_from.annotation_buffer
            self.annotations = create_from.annotations
            self.code = create_from.code
            self.last_pos = create_from.last_pos

    def create_new(self, create_from, buffer, copy_formatting):
        return AnnotationCCodeWriter(create_from, buffer, copy_formatting)

    def write(self, s):
        CCodeWriter.write(self, s)
        self.annotation_buffer.write(s)

    def mark_pos(self, pos):
        if pos is not None:
            CCodeWriter.mark_pos(self, pos)
        if self.last_pos:
            pos_code = self.code.setdefault(self.last_pos[0].filename,{})
            code = pos_code.get(self.last_pos[1], "")
            pos_code[self.last_pos[1]] = code + self.annotation_buffer.getvalue()
        self.annotation_buffer = StringIO()
        self.last_pos = pos

    def annotate(self, pos, item):
        self.annotations.append((pos, item))

    def save_annotation(self, source_filename, target_filename):
        self.mark_pos(None)
        f = Utils.open_source_file(source_filename)
        lines = f.readlines()
        for k, line in enumerate(lines):
            for c, cc, html in special_chars:
                line = line.replace(c, cc)
            lines[k] = line
        f.close()
        all = []
        if False:
            for pos, item in self.annotations:
                if pos[0].filename == source_filename:
                    start = item.start()
                    size, end = item.end()
                    if size:
                        all.append((pos, start))
                        all.append(((source_filename, pos[1], pos[2]+size), end))
                    else:
                        all.append((pos, start+end))

        all.sort(reverse=True)
        for pos, item in all:
            _, line_no, col = pos
            line_no -= 1
            col += 1
            line = lines[line_no]
            lines[line_no] = line[:col] + item + line[col:]

        html_filename = os.path.splitext(target_filename)[0] + ".html"
        f = codecs.open(html_filename, "w", encoding="UTF-8")
        f.write(u'<!DOCTYPE html>\n')
        f.write(u'\n' % Version.watermark)
        f.write(u'\n')
        f.write(u"""

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        """)
        f.write(u'\n')
        f.write(u'
Generated by Cython %s\n' % Version.watermark)
        c_file = Utils.decode_filename(os.path.basename(target_filename))
        f.write(u'
Raw output: %s\n' % (c_file, c_file))

        zero_calls = dict((name, 0) for name in
                          'refnanny py_macro_api py_c_api pyx_macro_api pyx_c_api error_goto'.split())

        def annotate(match):
            group_name = match.lastgroup
            calls[group_name] += 1
            return ur"%s" % (
                group_name, match.group(group_name))

        k = 0
        code_source_file = self.code.get(source_filename, {})
        for line in lines:
            k += 1
            try:
                code = code_source_file[k]
            except KeyError:
                code = ''
            else:
                code = html_escape(code)

            calls = zero_calls.copy()
            code = _parse_code(annotate, code)
            score = (5 * calls['py_c_api'] + 2 * calls['pyx_c_api'] +
                     calls['py_macro_api'] + calls['pyx_macro_api'])
            color = u"FFFF%02x" % int(255/(1+score/10.0))
            f.write(u"
" % (color, k))

            f.write(u" %d: " % k)
            for c, cc, html in special_chars:
                line = line.replace(cc, html)
            f.write(line.rstrip())

            f.write(u'
\n')
            code = re.sub(line_pos_comment, '', code) # inline annotations are redundant
            f.write(u"
%s
" % (k, color, code))
        f.write(u'\n')
        f.close()

_parse_code = re.compile(
    ur'(?P<refnanny>__Pyx_X?(?:GOT|GIVE)REF|__Pyx_RefNanny[A-Za-z]+)|'
    ur'(?:'
    ur'(?P<pyx_macro_api>__Pyx_[A-Z][A-Z_]+)|'
    ur'(?P<pyx_c_api>__Pyx_[A-Z][a-z_][A-Za-z_]+)|'
    ur'(?P<py_macro_api>Py[A-Z][a-z]+_[A-Z][A-Z_]+)|'
    ur'(?P<py_c_api>Py[A-Z][a-z]+_[A-Z][a-z][A-Za-z_]+)'
    ur')(?=\()|'       # look-ahead to exclude subsequent '(' from replacement
    ur'(?P<error_goto>(?:(?<=;) *if .* +)?\{__pyx_filename = .*goto __pyx_L\w+;\})'
).sub

class AnnotationItem(object):

    def __init__(self, style, text, tag="", size=0):
        self.style = style
        self.text = text
        self.tag = tag
        self.size = size

    def start(self):
        return u"%s" % (self.style, self.text, self.tag)

    def end(self):
        return self.size, u""
<commit_msg>strip C code position marker comments from annotated HTML files to a) avoid false positives when parsing C-API calls and b) reduce the overall clutter (the HTML contains the same content already)<commit_after># Note: Work in progress

import os
import re
import codecs
from xml.sax.saxutils import escape as html_escape
from StringIO import StringIO

import Version
from Code import CCodeWriter
from Cython import Utils

# need one-characters subsitutions (for now) so offsets aren't off
special_chars = [
    (u'&', u'\xF2', u'&'),
    (u'<', u'\xF0', u'<'),
    (u'>', u'\xF1', u'>'),
]

line_pos_comment = re.compile(r'/\*.*?<<<<<<<<<<<<<<.*?\*/\n*', re.DOTALL)

class AnnotationCCodeWriter(CCodeWriter):

    def __init__(self, create_from=None, buffer=None, copy_formatting=True):
        CCodeWriter.__init__(self, create_from, buffer, copy_formatting=True)
        if create_from is None:
            self.annotation_buffer = StringIO()
            self.annotations = []
            self.last_pos = None
            self.code = {}
        else:
            # When creating an insertion point, keep references to the same database
            self.annotation_buffer = create_from.annotation_buffer
            self.annotations = create_from.annotations
            self.code = create_from.code
            self.last_pos = create_from.last_pos

    def create_new(self, create_from, buffer, copy_formatting):
        return AnnotationCCodeWriter(create_from, buffer, copy_formatting)

    def write(self, s):
        CCodeWriter.write(self, s)
        self.annotation_buffer.write(s)

    def mark_pos(self, pos):
        if pos is not None:
            CCodeWriter.mark_pos(self, pos)
        if self.last_pos:
            pos_code = self.code.setdefault(self.last_pos[0].filename,{})
            code = pos_code.get(self.last_pos[1], "")
            pos_code[self.last_pos[1]] = code + self.annotation_buffer.getvalue()
        self.annotation_buffer = StringIO()
        self.last_pos = pos

    def annotate(self, pos, item):
        self.annotations.append((pos, item))

    def save_annotation(self, source_filename, target_filename):
        self.mark_pos(None)
        f = Utils.open_source_file(source_filename)
        lines = f.readlines()
        for k, line in enumerate(lines):
            for c, cc, html in special_chars:
                line = line.replace(c, cc)
            lines[k] = line
        f.close()
        all = []
        if False:
            for pos, item in self.annotations:
                if pos[0].filename == source_filename:
                    start = item.start()
                    size, end = item.end()
                    if size:
                        all.append((pos, start))
                        all.append(((source_filename, pos[1], pos[2]+size), end))
                    else:
                        all.append((pos, start+end))

        all.sort(reverse=True)
        for pos, item in all:
            _, line_no, col = pos
            line_no -= 1
            col += 1
            line = lines[line_no]
            lines[line_no] = line[:col] + item + line[col:]

        html_filename = os.path.splitext(target_filename)[0] + ".html"
        f = codecs.open(html_filename, "w", encoding="UTF-8")
        f.write(u'<!DOCTYPE html>\n')
        f.write(u'\n' % Version.watermark)
        f.write(u'\n')
        f.write(u"""

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        """)
        f.write(u'\n')
        f.write(u'
Generated by Cython %s\n' % Version.watermark)
        c_file = Utils.decode_filename(os.path.basename(target_filename))
        f.write(u'
Raw output: %s\n' % (c_file, c_file))

        zero_calls = dict((name, 0) for name in
                          'refnanny py_macro_api py_c_api pyx_macro_api pyx_c_api error_goto'.split())

        def annotate(match):
            group_name = match.lastgroup
            calls[group_name] += 1
            return ur"%s" % (
                group_name, match.group(group_name))

        k = 0
        code_source_file = self.code.get(source_filename, {})
        for line in lines:
            k += 1
            try:
                code = code_source_file[k]
            except KeyError:
                code = ''
            else:
                code = html_escape(_replace_pos_comment('', code))

            calls = zero_calls.copy()
            code = _parse_code(annotate, code)
            score = (5 * calls['py_c_api'] + 2 * calls['pyx_c_api'] +
                     calls['py_macro_api'] + calls['pyx_macro_api'])
            color = u"FFFF%02x" % int(255/(1+score/10.0))
            f.write(u"
" % (color, k))

            f.write(u" %d: " % k)
            for c, cc, html in special_chars:
                line = line.replace(cc, html)
            f.write(line.rstrip())

            f.write(u'
\n')
            code = re.sub(line_pos_comment, '', code) # inline annotations are redundant
            f.write(u"
%s
" % (k, color, code))
        f.write(u'\n')
        f.close()

_parse_code = re.compile(
    ur'(?P<refnanny>__Pyx_X?(?:GOT|GIVE)REF|__Pyx_RefNanny[A-Za-z]+)|'
    ur'(?:'
    ur'(?P<pyx_macro_api>__Pyx_[A-Z][A-Z_]+)|'
    ur'(?P<pyx_c_api>__Pyx_[A-Z][a-z_][A-Za-z_]+)|'
    ur'(?P<py_macro_api>Py[A-Z][a-z]+_[A-Z][A-Z_]+)|'
    ur'(?P<py_c_api>Py[A-Z][a-z]+_[A-Z][a-z][A-Za-z_]+)'
    ur')(?=\()|'       # look-ahead to exclude subsequent '(' from replacement
    ur'(?P<error_goto>(?:(?<=;) *if .* +)?\{__pyx_filename = .*goto __pyx_L\w+;\})'
).sub

_replace_pos_comment = re.compile(
    # this matches what Cython generates as code line marker comment
    ur'^\s*/\*(?:(?:[^*]|\*[^/])*\n)+\s*\*/\s*\n',
    re.M
).sub

class AnnotationItem(object):

    def __init__(self, style, text, tag="", size=0):
        self.style = style
        self.text = text
        self.tag = tag
        self.size = size

    def start(self):
        return u"%s" % (self.style, self.text, self.tag)

    def end(self):
        return self.size, u""
<|endoftext|>


<commit_before>__author__ = "Jerome Leclanche"
__email__ = "jerome@leclan.ch"
__version__ = "1.9.12574"
<commit_msg>Release 1.10.12574<commit_after>__author__ = "Jerome Leclanche"
__email__ = "jerome@leclan.ch"
__version__ = "1.10.12574"
<|endoftext|>