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

# Copyright (c) 2015, Ecole Polytechnique Federale de Lausanne, Blue Brain Project
# All rights reserved.
#
# This file is part of NeuroM <https://github.com/BlueBrain/NeuroM>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     1. Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#     3. Neither the name of the copyright holder nor the names of
#        its contributors may be used to endorse or promote products
#        derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'''Extract the optimal distributions for the following features of the population of neurons:
   soma: radius
   basal dendrites: n_neurites
   '''

from neurom import ezy
from neurom import stats
from neurom.io.utils import get_morph_files
import numpy as np
import argparse
import json
from collections import OrderedDict
from collections import defaultdict
import os

def parse_args():
    '''Parse command line arguments'''
    parser = argparse.ArgumentParser(
        description='Morphology fit distribution extractor',
        epilog='Note: Outputs json of the optimal distribution \
                and corresponding parameters.')

    parser.add_argument('datapaths',
                        nargs='+',
                        help='Morphology data directory paths')

    return parser.parse_args()

def extract_data(files, feature, params=None):
    '''Loads a list of neurons, extracts feature
       and transforms the fitted distribution in the correct format.
       Returns the optimal distribution and corresponding parameters.
       Normal distribution params (mean, std)
       Exponential distribution params (loc, scale)
       Uniform distribution params (min, range)
    '''
    population = ezy.load_neurons(files)

    if params is None:
        params = {}

    feature_data = [getattr(n, 'get_' + feature)(**params) for n in population]

    try:
        opt_fit = stats.optimal_distribution(feature_data)
    except ValueError:
        from itertools import chain
        feature_data = list(chain(*feature_data))
        opt_fit = stats.optimal_distribution(feature_data)

    return opt_fit

def transform_header(mtype_name, components):
    '''Add header to json output to wrap around distribution data.
    '''
    head_dict = OrderedDict()

    head_dict["m-type"] = mtype_name
    head_dict["components"] = {}

    for comp in np.unique(components):
        head_dict["components"].setdefault(comp)

    return head_dict

def transform_package(mtype, files, components, feature_list):
    '''Put together header and list of data into one json output.
       feature_list contains all the information about the data to be extracted:
       features, feature_names, feature_components, feature_min, feature_max
    '''
    data_dict = transform_header(mtype, components)

    for feature, name, comp, fmin, fmax, fparam in feature_list:

        result = stats.fit_results_to_dict(extract_data(files, feature, fparam),
                                           fmin, fmax)

        data_dict["components"][comp] = {name: result}

    return data_dict

def get_mtype_from_filename(filename, sep='_'):
    '''Get mtype of a morphology file from file name

    Assumes file name has structure 'a/b/c/d/mtype_xyx.abc'
    '''
    return os.path.basename(filename).split(sep)[0]

if __name__ == '__main__':
    args = parse_args()

    data_dirs = args.datapaths

    mtype_files = defaultdict(list)

    flist = [["soma_radius", "radius", "soma", None, None, None],
             ["n_neurites", "number", "basal_dendrite", 1, None,
              {"neurite_type": ezy.TreeType.basal_dendrite}]]

    comps = ["soma"]

    for d in data_dirs:
        for f in get_morph_files(d):
            mtype_files[get_mtype_from_filename(f)].append(f)

        _results = [transform_package(mtype_, files_, comps, flist)
                    for mtype_, files_ in mtype_files.iteritems()]

        for res in _results:
            print json.dumps(res, indent=2, separators=(', \n', ': ')), '\n'
<commit_msg>Add axon and apical dendrites information in synthesis_json<commit_after>#!/usr/bin/env python

# Copyright (c) 2015, Ecole Polytechnique Federale de Lausanne, Blue Brain Project
# All rights reserved.
#
# This file is part of NeuroM <https://github.com/BlueBrain/NeuroM>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     1. Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#     3. Neither the name of the copyright holder nor the names of
#        its contributors may be used to endorse or promote products
#        derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'''Extract the optimal distributions for the following features of the population of neurons:
   soma: radius
   basal dendrites: n_neurites
   '''

from neurom import ezy
from neurom import stats
from neurom.io.utils import get_morph_files
import numpy as np
import argparse
import json
from collections import OrderedDict
from collections import defaultdict
import os

def parse_args():
    '''Parse command line arguments'''
    parser = argparse.ArgumentParser(
        description='Morphology fit distribution extractor',
        epilog='Note: Outputs json of the optimal distribution \
                and corresponding parameters.')

    parser.add_argument('datapaths',
                        nargs='+',
                        help='Morphology data directory paths')

    return parser.parse_args()

def extract_data(files, feature, params=None):
    '''Loads a list of neurons, extracts feature
       and transforms the fitted distribution in the correct format.
       Returns the optimal distribution and corresponding parameters.
       Normal distribution params (mean, std)
       Exponential distribution params (loc, scale)
       Uniform distribution params (min, range)
    '''
    population = ezy.load_neurons(files)

    if params is None:
        params = {}

    feature_data = [getattr(n, 'get_' + feature)(**params) for n in population]

    try:
        opt_fit = stats.optimal_distribution(feature_data)
    except ValueError:
        from itertools import chain
        feature_data = list(chain(*feature_data))
        opt_fit = stats.optimal_distribution(feature_data)

    return opt_fit

def transform_header(mtype_name, components):
    '''Add header to json output to wrap around distribution data.
    '''
    head_dict = OrderedDict()

    head_dict["m-type"] = mtype_name
    head_dict["components"] = {}

    for comp in np.unique(components):
        head_dict["components"].setdefault(comp)

    return head_dict

def transform_package(mtype, files, components, feature_list):
    '''Put together header and list of data into one json output.
       feature_list contains all the information about the data to be extracted:
       features, feature_names, feature_components, feature_min, feature_max
    '''
    data_dict = transform_header(mtype, components)

    for feature, name, comp, fmin, fmax, fparam in feature_list:

        result = stats.fit_results_to_dict(extract_data(files, feature, fparam),
                                           fmin, fmax)

        data_dict["components"][comp] = {name: result}

    return data_dict

def get_mtype_from_filename(filename, sep='_'):
    '''Get mtype of a morphology file from file name

    Assumes file name has structure 'a/b/c/d/mtype_xyx.abc'
    '''
    return os.path.basename(filename).split(sep)[0]

if __name__ == '__main__':
    args = parse_args()

    data_dirs = args.datapaths

    mtype_files = defaultdict(list)

    flist = [["soma_radius", "radius", "soma", None, None, None],
             ["n_neurites", "number", "basal_dendrite", 1, None,
              {"neurite_type": ezy.TreeType.basal_dendrite}],
             ["n_neurites", "number", "apical_dendrite", 0, None,
              {"neurite_type": ezy.TreeType.apical_dendrite}],
             ["n_neurites", "number", "axon", 1, None,
              {"neurite_type": ezy.TreeType.axon}]]

    comps = ["soma"]

    for d in data_dirs:
        for f in get_morph_files(d):
            mtype_files[get_mtype_from_filename(f)].append(f)

        _results = [transform_package(mtype_, files_, comps, flist)
                    for mtype_, files_ in mtype_files.iteritems()]

        for res in _results:
            print json.dumps(res, indent=2, separators=(', \n', ': ')), '\n'
<|endoftext|>


<commit_before>from seleniumbase import BaseCase

class DocsSiteTests(BaseCase):
    def test_docs(self):
        self.open("https://seleniumbase.io/")
        self.delete_all_cookies()
        self.assert_text("SeleniumBase", "h1")
        self.js_click('a[href="help_docs/features_list/"]')
        self.assert_exact_text("Features List", "h1")
        self.js_click('a[href="../../examples/ReadMe/"]')
        self.assert_exact_text("Running Example Tests", "h1")
        self.js_click('a[href="../../help_docs/customizing_test_runs/"]')
        self.assert_exact_text("Command Line Options", "h1")
        self.js_click('a[href="../../examples/example_logs/ReadMe/"]')
        self.assert_exact_text("Dashboard / Reports", "h1")
        self.js_click('a[href="../../../seleniumbase/console_scripts/ReadMe/"]')
        self.assert_exact_text("Console Scripts", "h1")
        self.js_click('a[href="../../../help_docs/syntax_formats/"]')
        self.assert_exact_text("Syntax Formats", "h1")
        self.js_click('a[href="../recorder_mode/"]')
        self.assert_exact_text("Recorder Mode", "h1")
        self.js_click('a[href="../method_summary/"]')
        self.assert_exact_text("API Reference", "h1")
        self.js_click('img[alt="logo"]')
        self.assert_text("SeleniumBase", "h1")
<commit_msg>Update an example test<commit_after>from seleniumbase import BaseCase

class DocsSiteTests(BaseCase):
    def test_docs(self):
        self.open("https://seleniumbase.io/")
        self.delete_all_cookies()
        self.assert_text("SeleniumBase", "h1")
        self.js_click('a[href="help_docs/features_list/"]')
        self.assert_exact_text("Features List", "h1")
        self.js_click('a[href="../../examples/ReadMe/"]')
        self.assert_exact_text("Running Example Tests", "h1")
        self.js_click('a[href="../../help_docs/customizing_test_runs/"]')
        self.assert_exact_text("Command Line Options", "h1")
        self.js_click('a[href="../../examples/example_logs/ReadMe/"]')
        self.assert_exact_text("Dashboard / Reports", "h1")
        self.js_click('[href="../../../seleniumbase/console_scripts/ReadMe/"]')
        self.assert_exact_text("Console Scripts", "h1")
        self.js_click('a[href="../../../help_docs/syntax_formats/"]')
        self.assert_exact_text("Syntax Formats", "h1")
        self.js_click('a[href="../recorder_mode/"]')
        self.assert_exact_text("Recorder Mode", "h1")
        self.js_click('a[href="../method_summary/"]')
        self.assert_exact_text("API Reference", "h1")
        self.js_click('img[alt="logo"]')
        self.assert_text("SeleniumBase", "h1")
<|endoftext|>


<commit_before>from pprint import pprint

from utils.functions.models.dictlist import (
    dictlist_to_lower,
    dictlist,
)
from utils.functions.queries import debug_cursor_execute

def os_lotes(cursor, os):
    # Lotes ordenados por OS + referência + estágio
    return get_lotes(cursor, os=os)

def get_imprime_lotes(cursor, op='', tam='', cor='', order='',
                      oc_ini='', oc_fim='', pula=None, qtd_lotes=None):
    # get dados de lotes
    data = get_lotes(
        cursor, op=op, tam=tam, cor=cor, order=order,
        oc_ini=oc_ini, oc_fim=oc_fim, pula=pula, qtd_lotes=qtd_lotes)
    data = dictlist_to_lower(data)
    return data

def get_lotes(cursor, op='', os='', tam='', cor='', order='',
              oc_ini='', oc_fim='', pula=None, qtd_lotes=None, oc=''):
    # Lotes
    if oc != '':
        oc_ini = oc
        oc_fim = oc
    if pula is None:
        pula = 0

    filtra_op = ''
    if op is not None and op != '':
      filtra_op = f'''--
          AND os.ORDEM_PRODUCAO = '{op}'
      '''

    filtra_os = ''
    if os is not None and os != '':
      filtra_os = f'''--
          AND os.NUMERO_ORDEM = '{os}'
      '''

    filtra_oc_ini = ''
    if oc_ini is not None and oc_ini != '':
      filtra_oc_ini = f'''--
          AND os.ORDEM_CONFECCAO >= '{oc_ini}'
      '''

    filtra_oc_fim = ''
    if oc_fim is not None and oc_fim != '':
      filtra_oc_fim = f'''--
          AND os.ORDEM_CONFECCAO <= '{oc_fim}'
      '''

    filtra_tam = ''
    if tam is not None and tam != '':
      filtra_tam = f'''--
          AND os.PROCONF_SUBGRUPO = '{tam}'
      '''

    filtra_cor = ''
    if cor is not None and cor != '':
      filtra_cor = f'''--
          AND os.PROCONF_ITEM = '{cor}'
      '''

    sql_pre_qtd_lotes = ''
    sql_pos_qtd_lotes = ''
    if qtd_lotes is not None:
        qtd_rows = pula + qtd_lotes
        sql_pre_qtd_lotes = 'WITH Table_qtd_lotes AS ('
        sql_pos_qtd_lotes = """)
            select * from Table_qtd_lotes where rownum <= {}""".format(
                qtd_rows)

    # order by
    if order == 'o':  # OC
        sql_order = '''ORDER BY
              l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''
    elif order == 't':  # OS + referência + tamanho + cor + OC
        sql_order = '''ORDER BY
              l.ORDEM_PRODUCAO
            , l.NUMERO_ORDEM
            , l.PROCONF_GRUPO
            , t.ORDEM_TAMANHO
            , l.PROCONF_ITEM
            , l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''
    elif order == 'e':  # estágio + OS + referência + cor + tamanho + OC
        sql_order = '''ORDER BY
              1
            , l.ORDEM_PRODUCAO
            , l.NUMERO_ORDEM
            , l.PROCONF_GRUPO
            , l.PROCONF_ITEM
            , t.ORDEM_TAMANHO
            , l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''
    elif order == 'r':  # referência + cor + tamanho + OC
        sql_order = '''ORDER BY
              l.ORDEM_PRODUCAO
            , l.PROCONF_GRUPO
            , l.PROCONF_ITEM
            , t.ORDEM_TAMANHO
            , l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''
    elif order == 'w':  # referência + cor + tamanho + ROWID
        sql_order = '''ORDER BY
              l.ORDEM_PRODUCAO
            , l.PROCONF_GRUPO
            , l.PROCONF_ITEM
            , t.ORDEM_TAMANHO
            , l.ROW_ID'''
    else:  # elif order == '':  # OS + referência + cor + tamanho + OC
        sql_order = '''ORDER BY
              l.ORDEM_PRODUCAO
            , l.NUMERO_ORDEM
            , l.PROCONF_GRUPO
            , l.PROCONF_ITEM
            , t.ORDEM_TAMANHO
            , l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''

    sql = f'''
        {sql_pre_qtd_lotes} -- sql_pre_qtd_lotes
        SELECT
          COALESCE(
          ( SELECT
              MIN( le.CODIGO_ESTAGIO ) CODIGO_ESTAGIO
            FROM PCPC_040 le
            JOIN MQOP_005 ed
              ON ed.CODIGO_ESTAGIO = le.CODIGO_ESTAGIO
            WHERE le.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
              AND le.ORDEM_CONFECCAO = l.ORDEM_CONFECCAO
              AND le.QTDE_EM_PRODUCAO_PACOTE <> 0
          )
          , 9999
          ) EST_NUM
        , l.ORDEM_PRODUCAO OP
        , op.SITUACAO OP_SITUACAO
        , CASE WHEN dos.NUMERO_ORDEM IS NULL
          THEN '0'
          ELSE l.NUMERO_ORDEM || ' (' || eos.DESCRICAO || ')'
          END OS
        , l.PROCONF_GRUPO REF
        , l.PROCONF_SUBGRUPO TAM
        , t.ORDEM_TAMANHO
        , l.PROCONF_ITEM COR
        , COALESCE(
            ( SELECT
                LISTAGG(le.CODIGO_ESTAGIO || ' - ' || ed.DESCRICAO, ' & ')
                WITHIN GROUP (ORDER BY le.CODIGO_ESTAGIO)
              FROM PCPC_040 le
              JOIN MQOP_005 ed
                ON ed.CODIGO_ESTAGIO = le.CODIGO_ESTAGIO
              WHERE le.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
                AND le.ORDEM_CONFECCAO = l.ORDEM_CONFECCAO
                --AND le.QTDE_EM_PRODUCAO_PACOTE <> 0
                AND le.QTDE_DISPONIVEL_BAIXA + le.QTDE_CONSERTO <> 0
            )
          , 'FINALIZADO'
          ) EST
        , COALESCE(
            ( SELECT
                LISTAGG(le.QTDE_EM_PRODUCAO_PACOTE, ';')
                WITHIN GROUP (ORDER BY le.CODIGO_ESTAGIO)
              FROM PCPC_040 le
              JOIN MQOP_005 ed
                ON ed.CODIGO_ESTAGIO = le.CODIGO_ESTAGIO
              WHERE le.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
                AND le.ORDEM_CONFECCAO = l.ORDEM_CONFECCAO
                AND le.QTDE_EM_PRODUCAO_PACOTE <> 0
            )
          , ' '
          ) QUANTS
        , l.PERIODO_PRODUCAO PERIODO
        , l.ORDEM_CONFECCAO OC
        , ( SELECT
              count(*)
            FROM (
              SELECT DISTINCT
                l_n.PERIODO_PRODUCAO
              , l_n.ORDEM_CONFECCAO
              , l_n.ORDEM_PRODUCAO
              FROM PCPC_040 l_n
            ) l_nn
            WHERE l_nn.PERIODO_PRODUCAO <= l.PERIODO_PRODUCAO
              AND l_nn.ORDEM_CONFECCAO <= l.ORDEM_CONFECCAO
              AND l_nn.ORDEM_PRODUCAO = l.ORDEM_PRODUCAO
          ) NLOTE
        , ( SELECT
              count(*)
            FROM (
              SELECT DISTINCT
                l_t.PERIODO_PRODUCAO
              , l_t.ORDEM_CONFECCAO
              , l_t.ORDEM_PRODUCAO
              FROM PCPC_040 l_t
            ) l_tt
            WHERE l_tt.ORDEM_PRODUCAO = l.ORDEM_PRODUCAO
          ) TOTLOTES
        , l.QTDE_PECAS_PROG QTD
        , l.QTDE_PECAS_PROD PROD1Q
        , l.QTDE_CONSERTO CONSERTO
        , l.QTDE_PECAS_2A PROD2Q
        , l.QTDE_PERDAS PERDA
        , r.NARRATIVA
        , ref.DESCR_REFERENCIA
        , tam.DESCR_TAM_REFER DESCR_TAMANHO
        , r.DESCRICAO_15 DESCR_COR
        , ref.COLECAO
        , CASE WHEN l.DIVISAO = 0 THEN dp.DIVISAO_PRODUCAO
          ELSE l.DIVISAO END DIVISAO
        , CASE WHEN l.DIVISAO = 0 THEN dp.DESCRICAO
          ELSE di.DESCRICAO END DESCRICAO_DIVISAO
        , op.DATA_ENTRADA_CORTE
        , ( SELECT
              MIN( l1.ORDEM_CONFECCAO )
            FROM PCPC_040 l1
            WHERE l1.ORDEM_PRODUCAO   = l.ORDEM_PRODUCAO
              AND l1.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
              AND l1.PROCONF_GRUPO    = l.PROCONF_GRUPO
              AND l1.PROCONF_SUBGRUPO = l.PROCONF_SUBGRUPO
              AND l1.PROCONF_ITEM     = l.PROCONF_ITEM
          ) OC1
        FROM (
          SELECT
            os.PERIODO_PRODUCAO
          , os.ORDEM_CONFECCAO
          , os.PROCONF_GRUPO
          , os.PROCONF_SUBGRUPO
          , os.PROCONF_ITEM
          , os.ORDEM_PRODUCAO
          , max( os.NUMERO_ORDEM ) NUMERO_ORDEM
          , max( os.QTDE_PECAS_PROG ) QTDE_PECAS_PROG
          , max( os.QTDE_PECAS_PROD ) QTDE_PECAS_PROD
          , max( os.QTDE_CONSERTO ) QTDE_CONSERTO
          , max( os.QTDE_PECAS_2A ) QTDE_PECAS_2A
          , max( os.QTDE_PERDAS ) QTDE_PERDAS
          , max( CASE WHEN os.CODIGO_FAMILIA < 1000
                 THEN os.CODIGO_FAMILIA
                 ELSE 0 END ) DIVISAO
          , max( os.ROWID ) ROW_ID
          FROM PCPC_040 os
          WHERE 1=1
            {filtra_op} -- filtra_op
            {filtra_os} -- filtra_os
            {filtra_oc_ini} -- filtra_oc_ini
            {filtra_oc_fim} -- filtra_oc_fim
            {filtra_tam} -- filtra_tam
            {filtra_cor} -- filtra_cor
          GROUP BY
            os.PERIODO_PRODUCAO
          , os.ORDEM_CONFECCAO
          , os.PROCONF_GRUPO
          , os.PROCONF_SUBGRUPO
          , os.PROCONF_ITEM
          , os.ORDEM_PRODUCAO
        ) l
        LEFT JOIN PCPC_040 dos
          ON l.NUMERO_ORDEM <> 0
         AND dos.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
         AND dos.ORDEM_CONFECCAO = l.ORDEM_CONFECCAO
         AND dos.NUMERO_ORDEM = l.NUMERO_ORDEM
        LEFT JOIN MQOP_005 eos
          ON eos.CODIGO_ESTAGIO = dos.CODIGO_ESTAGIO
        LEFT JOIN BASI_220 t
          ON t.TAMANHO_REF = l.PROCONF_SUBGRUPO
        JOIN BASI_030 ref
          ON ref.NIVEL_ESTRUTURA = '1'
         AND ref.REFERENCIA = l.PROCONF_GRUPO
        JOIN BASI_020 tam
          ON tam.BASI030_NIVEL030 = '1'
         AND tam.BASI030_REFERENC = l.PROCONF_GRUPO
         AND tam.TAMANHO_REF = l.PROCONF_SUBGRUPO
        JOIN BASI_010 r
          ON r.NIVEL_ESTRUTURA = '1'
         AND r.GRUPO_ESTRUTURA = l.PROCONF_GRUPO
         AND r.SUBGRU_ESTRUTURA = l.PROCONF_SUBGRUPO
         AND r.ITEM_ESTRUTURA = l.PROCONF_ITEM
        LEFT JOIN BASI_180 di -- divisao de producao - unidade
          ON di.DIVISAO_PRODUCAO = l.DIVISAO
        LEFT JOIN OBRF_080 osc -- OS capa
          ON l.NUMERO_ORDEM <> 0
         AND osc.NUMERO_ORDEM = l.NUMERO_ORDEM
        LEFT JOIN BASI_180 dp -- divisao de producao - unidade
          ON dp.FACCIONISTA9 = osc.CGCTERC_FORNE9
         AND dp.FACCIONISTA4 = osc.CGCTERC_FORNE4
         AND dp.FACCIONISTA2 = osc.CGCTERC_FORNE2
        JOIN PCPC_020 op -- OP capa
          ON op.ordem_producao = l.ORDEM_PRODUCAO
        {sql_order} -- sql_order
        {sql_pos_qtd_lotes} -- sql_pos_qtd_lotes
    '''
    debug_cursor_execute(cursor, sql)
    data = dictlist(cursor)
    for i in range(0, pula):
        if len(data) != 0:
            del(data[0])
    return data
<commit_msg>__all__<commit_after>from pprint import pprint

from utils.functions.models.dictlist import (
    dictlist_to_lower,
    dictlist,
)
from utils.functions.queries import debug_cursor_execute

__all__ = ['os_lotes', 'get_imprime_lotes', 'get_lotes']

def os_lotes(cursor, os):
    # Lotes ordenados por OS + referência + estágio
    return get_lotes(cursor, os=os)

def get_imprime_lotes(cursor, op='', tam='', cor='', order='',
                      oc_ini='', oc_fim='', pula=None, qtd_lotes=None):
    # get dados de lotes
    data = get_lotes(
        cursor, op=op, tam=tam, cor=cor, order=order,
        oc_ini=oc_ini, oc_fim=oc_fim, pula=pula, qtd_lotes=qtd_lotes)
    data = dictlist_to_lower(data)
    return data

def get_lotes(cursor, op='', os='', tam='', cor='', order='',
              oc_ini='', oc_fim='', pula=None, qtd_lotes=None, oc=''):
    # Lotes
    if oc != '':
        oc_ini = oc
        oc_fim = oc
    if pula is None:
        pula = 0

    filtra_op = ''
    if op is not None and op != '':
      filtra_op = f'''--
          AND os.ORDEM_PRODUCAO = '{op}'
      '''

    filtra_os = ''
    if os is not None and os != '':
      filtra_os = f'''--
          AND os.NUMERO_ORDEM = '{os}'
      '''

    filtra_oc_ini = ''
    if oc_ini is not None and oc_ini != '':
      filtra_oc_ini = f'''--
          AND os.ORDEM_CONFECCAO >= '{oc_ini}'
      '''

    filtra_oc_fim = ''
    if oc_fim is not None and oc_fim != '':
      filtra_oc_fim = f'''--
          AND os.ORDEM_CONFECCAO <= '{oc_fim}'
      '''

    filtra_tam = ''
    if tam is not None and tam != '':
      filtra_tam = f'''--
          AND os.PROCONF_SUBGRUPO = '{tam}'
      '''

    filtra_cor = ''
    if cor is not None and cor != '':
      filtra_cor = f'''--
          AND os.PROCONF_ITEM = '{cor}'
      '''

    sql_pre_qtd_lotes = ''
    sql_pos_qtd_lotes = ''
    if qtd_lotes is not None:
        qtd_rows = pula + qtd_lotes
        sql_pre_qtd_lotes = 'WITH Table_qtd_lotes AS ('
        sql_pos_qtd_lotes = """)
            select * from Table_qtd_lotes where rownum <= {}""".format(
                qtd_rows)

    # order by
    if order == 'o':  # OC
        sql_order = '''ORDER BY
              l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''
    elif order == 't':  # OS + referência + tamanho + cor + OC
        sql_order = '''ORDER BY
              l.ORDEM_PRODUCAO
            , l.NUMERO_ORDEM
            , l.PROCONF_GRUPO
            , t.ORDEM_TAMANHO
            , l.PROCONF_ITEM
            , l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''
    elif order == 'e':  # estágio + OS + referência + cor + tamanho + OC
        sql_order = '''ORDER BY
              1
            , l.ORDEM_PRODUCAO
            , l.NUMERO_ORDEM
            , l.PROCONF_GRUPO
            , l.PROCONF_ITEM
            , t.ORDEM_TAMANHO
            , l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''
    elif order == 'r':  # referência + cor + tamanho + OC
        sql_order = '''ORDER BY
              l.ORDEM_PRODUCAO
            , l.PROCONF_GRUPO
            , l.PROCONF_ITEM
            , t.ORDEM_TAMANHO
            , l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''
    elif order == 'w':  # referência + cor + tamanho + ROWID
        sql_order = '''ORDER BY
              l.ORDEM_PRODUCAO
            , l.PROCONF_GRUPO
            , l.PROCONF_ITEM
            , t.ORDEM_TAMANHO
            , l.ROW_ID'''
    else:  # elif order == '':  # OS + referência + cor + tamanho + OC
        sql_order = '''ORDER BY
              l.ORDEM_PRODUCAO
            , l.NUMERO_ORDEM
            , l.PROCONF_GRUPO
            , l.PROCONF_ITEM
            , t.ORDEM_TAMANHO
            , l.PERIODO_PRODUCAO
            , l.ORDEM_CONFECCAO'''

    sql = f'''
        {sql_pre_qtd_lotes} -- sql_pre_qtd_lotes
        SELECT
          COALESCE(
          ( SELECT
              MIN( le.CODIGO_ESTAGIO ) CODIGO_ESTAGIO
            FROM PCPC_040 le
            JOIN MQOP_005 ed
              ON ed.CODIGO_ESTAGIO = le.CODIGO_ESTAGIO
            WHERE le.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
              AND le.ORDEM_CONFECCAO = l.ORDEM_CONFECCAO
              AND le.QTDE_EM_PRODUCAO_PACOTE <> 0
          )
          , 9999
          ) EST_NUM
        , l.ORDEM_PRODUCAO OP
        , op.SITUACAO OP_SITUACAO
        , CASE WHEN dos.NUMERO_ORDEM IS NULL
          THEN '0'
          ELSE l.NUMERO_ORDEM || ' (' || eos.DESCRICAO || ')'
          END OS
        , l.PROCONF_GRUPO REF
        , l.PROCONF_SUBGRUPO TAM
        , t.ORDEM_TAMANHO
        , l.PROCONF_ITEM COR
        , COALESCE(
            ( SELECT
                LISTAGG(le.CODIGO_ESTAGIO || ' - ' || ed.DESCRICAO, ' & ')
                WITHIN GROUP (ORDER BY le.CODIGO_ESTAGIO)
              FROM PCPC_040 le
              JOIN MQOP_005 ed
                ON ed.CODIGO_ESTAGIO = le.CODIGO_ESTAGIO
              WHERE le.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
                AND le.ORDEM_CONFECCAO = l.ORDEM_CONFECCAO
                --AND le.QTDE_EM_PRODUCAO_PACOTE <> 0
                AND le.QTDE_DISPONIVEL_BAIXA + le.QTDE_CONSERTO <> 0
            )
          , 'FINALIZADO'
          ) EST
        , COALESCE(
            ( SELECT
                LISTAGG(le.QTDE_EM_PRODUCAO_PACOTE, ';')
                WITHIN GROUP (ORDER BY le.CODIGO_ESTAGIO)
              FROM PCPC_040 le
              JOIN MQOP_005 ed
                ON ed.CODIGO_ESTAGIO = le.CODIGO_ESTAGIO
              WHERE le.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
                AND le.ORDEM_CONFECCAO = l.ORDEM_CONFECCAO
                AND le.QTDE_EM_PRODUCAO_PACOTE <> 0
            )
          , ' '
          ) QUANTS
        , l.PERIODO_PRODUCAO PERIODO
        , l.ORDEM_CONFECCAO OC
        , ( SELECT
              count(*)
            FROM (
              SELECT DISTINCT
                l_n.PERIODO_PRODUCAO
              , l_n.ORDEM_CONFECCAO
              , l_n.ORDEM_PRODUCAO
              FROM PCPC_040 l_n
            ) l_nn
            WHERE l_nn.PERIODO_PRODUCAO <= l.PERIODO_PRODUCAO
              AND l_nn.ORDEM_CONFECCAO <= l.ORDEM_CONFECCAO
              AND l_nn.ORDEM_PRODUCAO = l.ORDEM_PRODUCAO
          ) NLOTE
        , ( SELECT
              count(*)
            FROM (
              SELECT DISTINCT
                l_t.PERIODO_PRODUCAO
              , l_t.ORDEM_CONFECCAO
              , l_t.ORDEM_PRODUCAO
              FROM PCPC_040 l_t
            ) l_tt
            WHERE l_tt.ORDEM_PRODUCAO = l.ORDEM_PRODUCAO
          ) TOTLOTES
        , l.QTDE_PECAS_PROG QTD
        , l.QTDE_PECAS_PROD PROD1Q
        , l.QTDE_CONSERTO CONSERTO
        , l.QTDE_PECAS_2A PROD2Q
        , l.QTDE_PERDAS PERDA
        , r.NARRATIVA
        , ref.DESCR_REFERENCIA
        , tam.DESCR_TAM_REFER DESCR_TAMANHO
        , r.DESCRICAO_15 DESCR_COR
        , ref.COLECAO
        , CASE WHEN l.DIVISAO = 0 THEN dp.DIVISAO_PRODUCAO
          ELSE l.DIVISAO END DIVISAO
        , CASE WHEN l.DIVISAO = 0 THEN dp.DESCRICAO
          ELSE di.DESCRICAO END DESCRICAO_DIVISAO
        , op.DATA_ENTRADA_CORTE
        , ( SELECT
              MIN( l1.ORDEM_CONFECCAO )
            FROM PCPC_040 l1
            WHERE l1.ORDEM_PRODUCAO   = l.ORDEM_PRODUCAO
              AND l1.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
              AND l1.PROCONF_GRUPO    = l.PROCONF_GRUPO
              AND l1.PROCONF_SUBGRUPO = l.PROCONF_SUBGRUPO
              AND l1.PROCONF_ITEM     = l.PROCONF_ITEM
          ) OC1
        FROM (
          SELECT
            os.PERIODO_PRODUCAO
          , os.ORDEM_CONFECCAO
          , os.PROCONF_GRUPO
          , os.PROCONF_SUBGRUPO
          , os.PROCONF_ITEM
          , os.ORDEM_PRODUCAO
          , max( os.NUMERO_ORDEM ) NUMERO_ORDEM
          , max( os.QTDE_PECAS_PROG ) QTDE_PECAS_PROG
          , max( os.QTDE_PECAS_PROD ) QTDE_PECAS_PROD
          , max( os.QTDE_CONSERTO ) QTDE_CONSERTO
          , max( os.QTDE_PECAS_2A ) QTDE_PECAS_2A
          , max( os.QTDE_PERDAS ) QTDE_PERDAS
          , max( CASE WHEN os.CODIGO_FAMILIA < 1000
                 THEN os.CODIGO_FAMILIA
                 ELSE 0 END ) DIVISAO
          , max( os.ROWID ) ROW_ID
          FROM PCPC_040 os
          WHERE 1=1
            {filtra_op} -- filtra_op
            {filtra_os} -- filtra_os
            {filtra_oc_ini} -- filtra_oc_ini
            {filtra_oc_fim} -- filtra_oc_fim
            {filtra_tam} -- filtra_tam
            {filtra_cor} -- filtra_cor
          GROUP BY
            os.PERIODO_PRODUCAO
          , os.ORDEM_CONFECCAO
          , os.PROCONF_GRUPO
          , os.PROCONF_SUBGRUPO
          , os.PROCONF_ITEM
          , os.ORDEM_PRODUCAO
        ) l
        LEFT JOIN PCPC_040 dos
          ON l.NUMERO_ORDEM <> 0
         AND dos.PERIODO_PRODUCAO = l.PERIODO_PRODUCAO
         AND dos.ORDEM_CONFECCAO = l.ORDEM_CONFECCAO
         AND dos.NUMERO_ORDEM = l.NUMERO_ORDEM
        LEFT JOIN MQOP_005 eos
          ON eos.CODIGO_ESTAGIO = dos.CODIGO_ESTAGIO
        LEFT JOIN BASI_220 t
          ON t.TAMANHO_REF = l.PROCONF_SUBGRUPO
        JOIN BASI_030 ref
          ON ref.NIVEL_ESTRUTURA = '1'
         AND ref.REFERENCIA = l.PROCONF_GRUPO
        JOIN BASI_020 tam
          ON tam.BASI030_NIVEL030 = '1'
         AND tam.BASI030_REFERENC = l.PROCONF_GRUPO
         AND tam.TAMANHO_REF = l.PROCONF_SUBGRUPO
        JOIN BASI_010 r
          ON r.NIVEL_ESTRUTURA = '1'
         AND r.GRUPO_ESTRUTURA = l.PROCONF_GRUPO
         AND r.SUBGRU_ESTRUTURA = l.PROCONF_SUBGRUPO
         AND r.ITEM_ESTRUTURA = l.PROCONF_ITEM
        LEFT JOIN BASI_180 di -- divisao de producao - unidade
          ON di.DIVISAO_PRODUCAO = l.DIVISAO
        LEFT JOIN OBRF_080 osc -- OS capa
          ON l.NUMERO_ORDEM <> 0
         AND osc.NUMERO_ORDEM = l.NUMERO_ORDEM
        LEFT JOIN BASI_180 dp -- divisao de producao - unidade
          ON dp.FACCIONISTA9 = osc.CGCTERC_FORNE9
         AND dp.FACCIONISTA4 = osc.CGCTERC_FORNE4
         AND dp.FACCIONISTA2 = osc.CGCTERC_FORNE2
        JOIN PCPC_020 op -- OP capa
          ON op.ordem_producao = l.ORDEM_PRODUCAO
        {sql_order} -- sql_order
        {sql_pos_qtd_lotes} -- sql_pos_qtd_lotes
    '''
    debug_cursor_execute(cursor, sql)
    data = dictlist(cursor)
    for i in range(0, pula):
        if len(data) != 0:
            del(data[0])
    return data
<|endoftext|>


<commit_before>#!/usr/bin/python

import json
import signal
import subprocess
import socket
from time import sleep

import RPi.GPIO as GPIO
from socketIO_client import SocketIO

import MFRC522

from TTLeague import Player, Match, Game

from Adafruit_CharLCD import Adafruit_CharLCD

from evdev import InputDevice, list_devices, categorize, ecodes, KeyEvent

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal, frame):
    global notFound
    print "Ctrl+C captured, ending read."
    notFound = False
    lcd.clear()
    GPIO.cleanup()

def hex_string_from_nfc_tag(tag):
    return '{:x}{:x}{:x}{:x}'.format(tag[0], tag[1], tag[2], tag[3])

def wait_for_tag():
    global notFound
    global oldTag
    notFound = True
    while notFound:

        # Scan for cards
        (status, TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

        # If a card is found
        if status == MIFAREReader.MI_OK:
            # print "tag detected"

            # Get the UID of the card
            (status, uid) = MIFAREReader.MFRC522_Anticoll()

            # If we have the UID, continue
            if status == MIFAREReader.MI_OK:
                # print "tag id read"
                if oldTag != uid:
                    if hex_string_from_nfc_tag(uid) == config['endTagId']:
                        print('Exit tag scanned, shutting down in 2 seconds ...')
                        lcd.clear()
                        lcd.message('Time to say\n\n      goodbye')
                        sleep(4)
                        lcd.clear()
                        subprocess.call("sudo shutdown -hP now", shell=True)
                    notFound = False
                    oldTag = uid
                    sleep(0.1)
                    return uid
    return None

def on_result_player(*args):
    _player_data = args[0]
    print("received Player name: " + _player_data['name'] + " with tagId: " + _player_data['tagId'])
    lcd.clear()
    fStr = 'found player:\n{:^' + str(_lcd_cols) + '}\nElo: {:d}'
    lcd.message(fStr.format(_player_data['name'], _player_data['elo']))
    players.append(Player(_player_data['tagId'], _player_data['name']))

def on_ack_match():
    print('match was submitted successfully')

def search_player(nfcTag):
    socketIO = SocketIO(config['url'], verify=False)
    socketIO.on('resultPlayer', on_result_player)
    socketIO.on('error', on_error)
    socketIO.emit('requestPlayerByTagId', {'tagId': nfcTag})
    socketIO.wait(seconds=3)

def on_error(*args):
    print('Error received: ' + str(args[0]))
    lcd.clear()
    lcd.message('Error received:\n' + str(args[0]))
    sleep(2)

def on_refreshed_data(*args):
    data = args[0]
    elo_changes = {}
    for player in data['players']:
        if player['name'] in last_players_names:
            print(player['name'] + ' ' + str(player['eloChange']))
            elo_changes.update({player['name']: player['eloChange']})

    if len(elo_changes.keys()) > 0:
        show_elo_change(elo_changes)

def add_match(match):
    socketIO = SocketIO(config['url'], verify=False)
    socketIO.on('ackMatch', on_ack_match)
    socketIO.on('error', on_error)
    socketIO.on('refreshedData', on_refreshed_data)
    socketIO.emit('addMatch', match.get_match_data())
    socketIO.wait(seconds=5)

def clear_points_display():
    # clear old points
    for row in [1, 2]:
        lcd.set_cursor(int(_lcd_cols) - 2, row)
        lcd.write8(ord(' '), True)
        lcd.write8(ord(' '), True)

def wait_for_points(set, row):
    # print current set
    init_position = int(_lcd_cols) - 2
    lcd.set_cursor(init_position - 2, 0)
    lcd.write8(ord('S'), True)
    lcd.set_cursor(init_position - 2, 1)
    lcd.write8(ord(str(set)), True)
    # clear old points in this row
    lcd.set_cursor(init_position, row)
    lcd.write8(ord(' '), True)
    lcd.write8(ord(' '), True)
    # reposition
    lcd.set_cursor(init_position, row)
    lcd.blink(True)
    typed = 0
    points = 0
    digits = []
    # wait for keyboard input
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            key = categorize(event)
            if key.keystate == KeyEvent.key_down:
                if key.keycode == 'KEY_KPENTER':
                    points = int(digits.pop())
                    if len(digits) > 0:
                        points += int(digits.pop()) * 10
                    break
                elif key.keycode == 'KEY_DELETE' or key.keycode == 'KEY_BACKSPACE':
                    if len(digits) > 0:
                        digits.pop()
                        new_position = init_position + (typed - 1)
                        lcd.set_cursor(new_position, row)
                        lcd.write8(ord(' '), True)
                        lcd.set_cursor(new_position, row)
                        typed -= 1
                elif key.keycode in keypad_map and typed < 2:
                    typed += 1
                    mapped = keypad_map[key.keycode]
                    digits.append(chr(mapped))
                    lcd.write8(mapped, True)
                    if typed == 2:
                        # set cursor on last digit to wait for enter
                        lcd.set_cursor(init_position + 1, row)
    lcd.blink(False)
    return points

def clear_players():
    global players
    players = []

def show_sets_on_display(games):
    # called after a set was added to the game
    home = ''
    guest = ''
    for game in games:
        home += '{:2d} '.format(game.home)
        guest += '{:2d} '.format(game.guest)

    lcd.set_cursor(0, 2)
    for c in home:
        lcd.write8(ord(c), True)
    lcd.set_cursor(0, 3)
    for c in guest:
        lcd.write8(ord(c), True)

def show_match_on_display(match):
    lcd.clear()
    player_str = '{:6s} - {:6s}\n'.format(match.player1.name, match.player2.name)

    for game in match.games:
        # home points
        player_str += "{:2d} ".format(game.home)
    player_str += "\n"
    for game in match.games:
        # guest points
        player_str += "{:2d} ".format(game.guest)
    lcd.message(player_str)

def show_elo_change(elo_changes):
    row = 2
    lcd.clear()
    lcd.message('Elo changes:')
    for player, change in elo_changes.items():
        lcd.set_cursor(0, row)
        fStr = '{:' + str(_lcd_cols - 5) + 's} {:+4d}'
        msg = fStr.format(player, change)
        print(msg)
        for c in msg:
            lcd.write8(ord(c), True)
        row += 1

def get_ip_address():
    return [
        (s.connect(('8.8.8.8', 53)),
         s.getsockname()[0],
         s.close()) for s in
        [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]
    ][0][1]

########################################################################
#  Script start here
########################################################################

socketIO = None
players = []
last_players_names = []

# init empty configuration
config = {}

try:
    # try to load config from file
    config = json.load(open('config.json'))
except Exception, e:
    print("Error while getting config: " + str(e))
    exit()

lcdConfig = config['lcd']
_lcd_rows = lcdConfig['lines']
_lcd_cols = lcdConfig['cols']

notFound = True

dev = InputDevice(config['keyboardDevice'])

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

GPIO.setwarnings(False)

keypad_map = {'KEY_KP0': 48, 'KEY_KP1': 49, 'KEY_KP2': 50, 'KEY_KP3': 51,
              'KEY_KP4': 52, 'KEY_KP5': 53, 'KEY_KP6': 54, 'KEY_KP7': 55,
              'KEY_KP8': 56, 'KEY_KP9': 57}

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

lcd = Adafruit_CharLCD(lcdConfig['rs'], lcdConfig['en'], lcdConfig['d4'],
                       lcdConfig['d5'], lcdConfig['d6'], lcdConfig['d7'],
                       _lcd_cols, _lcd_rows)

ip = get_ip_address()
welcome = "Welcome to the\nTTLeagueTerminal\n\nIP: {}".format(ip)
lcd.clear()
lcd.message(welcome)
print(welcome)
sleep(3)

oldTag = ''

while True:
    lcd.clear()
    while (2 - len(players)) > 0:
        print('waiting for {:d} players to scan ...'.format((2 - len(players))))
        lcd.clear()
        lcd.message('- Agido TT League -\nTerminal ready\nwaiting for {:d}\nplayers to scan'.format((2 - len(players))))
        rawTag = wait_for_tag()
        nfcTag = hex_string_from_nfc_tag(rawTag)
        print('player tagId scanned - {}'.format(nfcTag))
        lcd.clear()
        lcd.message('scan successful\nsearching player ...')
        search_player(nfcTag)

    print('seems we have both player: ' + str([p.name for p in players]))
    last_players_names = [players[0].name, players[1].name]
    sleep(1)
    lcd.clear()
    lcd.message('Players found\n{:s}\n{:s}\ncreating match ...'.format(players[0].name, players[1].name))
    print('creating match')
    match = Match(players[0], players[1])
    sleep(1)
    lcd.clear()
    lcd.message('{}\n{}'.format(players[0].name, players[1].name))
    for x in range(1, 6):
        home = wait_for_points(x, 0)
        guest = wait_for_points(x, 1)
        clear_points_display()
        if home == 0 and guest == 0:
            break
        match.add_game(Game(int(home), int(guest)))
        show_sets_on_display(match.games)
    print('Match finished: ' + str(match.get_match_data()))
    if len(match.games) > 0:
        show_match_on_display(match)
        sleep(2)
        add_match(match)
    clear_players()
<commit_msg>check for empty digits<commit_after>#!/usr/bin/python

import json
import signal
import subprocess
import socket
from time import sleep

import RPi.GPIO as GPIO
from socketIO_client import SocketIO

import MFRC522

from TTLeague import Player, Match, Game

from Adafruit_CharLCD import Adafruit_CharLCD

from evdev import InputDevice, list_devices, categorize, ecodes, KeyEvent

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal, frame):
    global notFound
    print "Ctrl+C captured, ending read."
    notFound = False
    lcd.clear()
    GPIO.cleanup()

def hex_string_from_nfc_tag(tag):
    return '{:x}{:x}{:x}{:x}'.format(tag[0], tag[1], tag[2], tag[3])

def wait_for_tag():
    global notFound
    global oldTag
    notFound = True
    while notFound:

        # Scan for cards
        (status, TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

        # If a card is found
        if status == MIFAREReader.MI_OK:
            # print "tag detected"

            # Get the UID of the card
            (status, uid) = MIFAREReader.MFRC522_Anticoll()

            # If we have the UID, continue
            if status == MIFAREReader.MI_OK:
                # print "tag id read"
                if oldTag != uid:
                    if hex_string_from_nfc_tag(uid) == config['endTagId']:
                        print('Exit tag scanned, shutting down in 2 seconds ...')
                        lcd.clear()
                        lcd.message('Time to say\n\n      goodbye')
                        sleep(4)
                        lcd.clear()
                        subprocess.call("sudo shutdown -hP now", shell=True)
                    notFound = False
                    oldTag = uid
                    sleep(0.1)
                    return uid
    return None

def on_result_player(*args):
    _player_data = args[0]
    print("received Player name: " + _player_data['name'] + " with tagId: " + _player_data['tagId'])
    lcd.clear()
    fStr = 'found player:\n{:^' + str(_lcd_cols) + '}\nElo: {:d}'
    lcd.message(fStr.format(_player_data['name'], _player_data['elo']))
    players.append(Player(_player_data['tagId'], _player_data['name']))

def on_ack_match():
    print('match was submitted successfully')

def search_player(nfcTag):
    socketIO = SocketIO(config['url'], verify=False)
    socketIO.on('resultPlayer', on_result_player)
    socketIO.on('error', on_error)
    socketIO.emit('requestPlayerByTagId', {'tagId': nfcTag})
    socketIO.wait(seconds=3)

def on_error(*args):
    print('Error received: ' + str(args[0]))
    lcd.clear()
    lcd.message('Error received:\n' + str(args[0]))
    sleep(2)

def on_refreshed_data(*args):
    data = args[0]
    elo_changes = {}
    for player in data['players']:
        if player['name'] in last_players_names:
            print(player['name'] + ' ' + str(player['eloChange']))
            elo_changes.update({player['name']: player['eloChange']})

    if len(elo_changes.keys()) > 0:
        show_elo_change(elo_changes)

def add_match(match):
    socketIO = SocketIO(config['url'], verify=False)
    socketIO.on('ackMatch', on_ack_match)
    socketIO.on('error', on_error)
    socketIO.on('refreshedData', on_refreshed_data)
    socketIO.emit('addMatch', match.get_match_data())
    socketIO.wait(seconds=5)

def clear_points_display():
    # clear old points
    for row in [1, 2]:
        lcd.set_cursor(int(_lcd_cols) - 2, row)
        lcd.write8(ord(' '), True)
        lcd.write8(ord(' '), True)

def wait_for_points(set, row):
    # print current set
    init_position = int(_lcd_cols) - 2
    lcd.set_cursor(init_position - 2, 0)
    lcd.write8(ord('S'), True)
    lcd.set_cursor(init_position - 2, 1)
    lcd.write8(ord(str(set)), True)
    # clear old points in this row
    lcd.set_cursor(init_position, row)
    lcd.write8(ord(' '), True)
    lcd.write8(ord(' '), True)
    # reposition
    lcd.set_cursor(init_position, row)
    lcd.blink(True)
    typed = 0
    points = 0
    digits = []
    # wait for keyboard input
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            key = categorize(event)
            if key.keystate == KeyEvent.key_down:
                if key.keycode == 'KEY_KPENTER':
                    if len(digits) > 0:
                        points = int(digits.pop())
                        if len(digits) > 0:
                            points += int(digits.pop()) * 10
                    break
                elif key.keycode == 'KEY_DELETE' or key.keycode == 'KEY_BACKSPACE':
                    if len(digits) > 0:
                        digits.pop()
                        new_position = init_position + (typed - 1)
                        lcd.set_cursor(new_position, row)
                        lcd.write8(ord(' '), True)
                        lcd.set_cursor(new_position, row)
                        typed -= 1
                elif key.keycode in keypad_map and typed < 2:
                    typed += 1
                    mapped = keypad_map[key.keycode]
                    digits.append(chr(mapped))
                    lcd.write8(mapped, True)
                    if typed == 2:
                        # set cursor on last digit to wait for enter
                        lcd.set_cursor(init_position + 1, row)
    lcd.blink(False)
    return points

def clear_players():
    global players
    players = []

def show_sets_on_display(games):
    # called after a set was added to the game
    home = ''
    guest = ''
    for game in games:
        home += '{:2d} '.format(game.home)
        guest += '{:2d} '.format(game.guest)

    lcd.set_cursor(0, 2)
    for c in home:
        lcd.write8(ord(c), True)
    lcd.set_cursor(0, 3)
    for c in guest:
        lcd.write8(ord(c), True)

def show_match_on_display(match):
    lcd.clear()
    player_str = '{:6s} - {:6s}\n'.format(match.player1.name, match.player2.name)

    for game in match.games:
        # home points
        player_str += "{:2d} ".format(game.home)
    player_str += "\n"
    for game in match.games:
        # guest points
        player_str += "{:2d} ".format(game.guest)
    lcd.message(player_str)

def show_elo_change(elo_changes):
    row = 2
    lcd.clear()
    lcd.message('Elo changes:')
    for player, change in elo_changes.items():
        lcd.set_cursor(0, row)
        fStr = '{:' + str(_lcd_cols - 5) + 's} {:+4d}'
        msg = fStr.format(player, change)
        print(msg)
        for c in msg:
            lcd.write8(ord(c), True)
        row += 1

def get_ip_address():
    return [
        (s.connect(('8.8.8.8', 53)),
         s.getsockname()[0],
         s.close()) for s in
        [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]
    ][0][1]

########################################################################
#  Script start here
########################################################################

socketIO = None
players = []
last_players_names = []

# init empty configuration
config = {}

try:
    # try to load config from file
    config = json.load(open('config.json'))
except Exception, e:
    print("Error while getting config: " + str(e))
    exit()

lcdConfig = config['lcd']
_lcd_rows = lcdConfig['lines']
_lcd_cols = lcdConfig['cols']

notFound = True

dev = InputDevice(config['keyboardDevice'])

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

GPIO.setwarnings(False)

keypad_map = {'KEY_KP0': 48, 'KEY_KP1': 49, 'KEY_KP2': 50, 'KEY_KP3': 51,
              'KEY_KP4': 52, 'KEY_KP5': 53, 'KEY_KP6': 54, 'KEY_KP7': 55,
              'KEY_KP8': 56, 'KEY_KP9': 57}

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

lcd = Adafruit_CharLCD(lcdConfig['rs'], lcdConfig['en'], lcdConfig['d4'],
                       lcdConfig['d5'], lcdConfig['d6'], lcdConfig['d7'],
                       _lcd_cols, _lcd_rows)

ip = get_ip_address()
welcome = "Welcome to the\nTTLeagueTerminal\n\nIP: {}".format(ip)
lcd.clear()
lcd.message(welcome)
print(welcome)
sleep(3)

oldTag = ''

while True:
    lcd.clear()
    while (2 - len(players)) > 0:
        print('waiting for {:d} players to scan ...'.format((2 - len(players))))
        lcd.clear()
        lcd.message('- Agido TT League -\nTerminal ready\nwaiting for {:d}\nplayers to scan'.format((2 - len(players))))
        rawTag = wait_for_tag()
        nfcTag = hex_string_from_nfc_tag(rawTag)
        print('player tagId scanned - {}'.format(nfcTag))
        lcd.clear()
        lcd.message('scan successful\nsearching player ...')
        search_player(nfcTag)

    print('seems we have both player: ' + str([p.name for p in players]))
    last_players_names = [players[0].name, players[1].name]
    sleep(1)
    lcd.clear()
    lcd.message('Players found\n{:s}\n{:s}\ncreating match ...'.format(players[0].name, players[1].name))
    print('creating match')
    match = Match(players[0], players[1])
    sleep(1)
    lcd.clear()
    lcd.message('{}\n{}'.format(players[0].name, players[1].name))
    for x in range(1, 6):
        home = wait_for_points(x, 0)
        guest = wait_for_points(x, 1)
        clear_points_display()
        if home == 0 and guest == 0:
            break
        match.add_game(Game(int(home), int(guest)))
        show_sets_on_display(match.games)
    print('Match finished: ' + str(match.get_match_data()))
    if len(match.games) > 0:
        show_match_on_display(match)
        sleep(2)
        add_match(match)
    clear_players()
<|endoftext|>


<commit_before>"""Routes WSGI Middleware"""
import re
import logging

try:
    from webob import Request
except:
    pass

from routes.base import request_config
from routes.util import URLGenerator, url_for

log = logging.getLogger('routes.middleware')

class RoutesMiddleware(object):
    """Routing middleware that handles resolving the PATH_INFO in
    addition to optionally recognizing method overriding."""
    def __init__(self, wsgi_app, mapper, use_method_override=True,
                 path_info=True):
        """Create a Route middleware object

        Using the use_method_override keyword will require Paste to be
        installed, and your application should use Paste's WSGIRequest
        object as it will properly handle POST issues with wsgi.input
        should Routes check it.

        If path_info is True, then should a route var contain
        path_info, the SCRIPT_NAME and PATH_INFO will be altered
        accordingly. This should be used with routes like:

        .. code-block:: python

            map.connect('blog/*path_info', controller='blog', path_info='')

        """
        self.app = wsgi_app
        self.mapper = mapper
        self.use_method_override = use_method_override
        self.path_info = path_info
        log_debug = self.log_debug = logging.DEBUG >= log.getEffectiveLevel()
        if self.log_debug:
            log.debug("Initialized with method overriding = %s, and path "
                  "info altering = %s", use_method_override, path_info)

    def __call__(self, environ, start_response):
        """Resolves the URL in PATH_INFO, and uses wsgi.routing_args
        to pass on URL resolver results."""
        config = request_config()
        config.mapper = self.mapper

        old_method = None
        if self.use_method_override:
            req = None
            if '_method' in environ['QUERY_STRING']:
                req = Request(environ)
                req.errors = 'ignore'
                if '_method' in req.GET:
                    old_method = environ['REQUEST_METHOD']
                    environ['REQUEST_METHOD'] = req.GET['_method'].upper()
                    if self.log_debug:
                        log.debug("_method found in QUERY_STRING, altering request"
                                " method to %s", environ['REQUEST_METHOD'])
            elif environ['REQUEST_METHOD'] == 'POST' and is_form_post(environ):
                if req is None:
                    req = Request(environ)
                    req.errors = 'ignore'
                if '_method' in req.POST:
                    old_method = environ['REQUEST_METHOD']
                    environ['REQUEST_METHOD'] = req.POST['_method'].upper()
                    if self.log_debug:
                        log.debug("_method found in POST data, altering request "
                                  "method to %s", environ['REQUEST_METHOD'])

        # Run the actual route matching
        # -- Assignment of environ to config triggers route matching
        config.environ = environ

        match = config.mapper_dict
        route = config.route

        if old_method:
            environ['REQUEST_METHOD'] = old_method

        if not match:
            match = {}
            if self.log_debug:
                urlinfo = "%s %s" % (environ['REQUEST_METHOD'], environ['PATH_INFO'])
                log.debug("No route matched for %s", urlinfo)
        elif self.log_debug:
            urlinfo = "%s %s" % (environ['REQUEST_METHOD'], environ['PATH_INFO'])
            log.debug("Matched %s", urlinfo)
            log.debug("Route path: '%s', defaults: %s", route.routepath,
                      route.defaults)
            log.debug("Match dict: %s", match)

        url = URLGenerator(self.mapper, environ)
        environ['wsgiorg.routing_args'] = ((url), match)
        environ['routes.route'] = route
        environ['routes.url'] = url

        if route and route.redirect:
            route_name = '_redirect_%s' % id(route)
            location = url_for(route_name, **match)
            log.debug("Using redirect route, redirect to '%s' with status"
                      "code: %s", location, route.redirect_status)
            start_response(route.redirect_status,
                           [('Content-Type', 'text/plain; charset=utf8'),
                            ('Location', location)])
            return []

        # If the route included a path_info attribute and it should be used to
        # alter the environ, we'll pull it out
        if self.path_info and 'path_info' in match:
            oldpath = environ['PATH_INFO']
            newpath = match.get('path_info') or ''
            environ['PATH_INFO'] = newpath
            if not environ['PATH_INFO'].startswith('/'):
                environ['PATH_INFO'] = '/' + environ['PATH_INFO']
            environ['SCRIPT_NAME'] += re.sub(r'^(.*?)/' + re.escape(newpath) + '$',
                                             r'\1', oldpath)
            if environ['SCRIPT_NAME'].endswith('/'):
                environ['SCRIPT_NAME'] = environ['SCRIPT_NAME'][:-1]

        response = self.app(environ, start_response)

        # Wrapped in try as in rare cases the attribute will be gone already
        try:
            del config.environ
            del self.mapper.environ
        except AttributeError:
            pass
        return response

def is_form_post(environ):
    """Determine whether the request is a POSTed html form"""
    content_type = environ.get('CONTENT_TYPE', '').lower()
    if ';' in content_type:
        content_type = content_type.split(';', 1)[0]
    return content_type in ('application/x-www-form-urlencoded',
                            'multipart/form-data')
<commit_msg>Account for lack of query string<commit_after>"""Routes WSGI Middleware"""
import re
import logging

try:
    from webob import Request
except:
    pass

from routes.base import request_config
from routes.util import URLGenerator, url_for

log = logging.getLogger('routes.middleware')

class RoutesMiddleware(object):
    """Routing middleware that handles resolving the PATH_INFO in
    addition to optionally recognizing method overriding."""
    def __init__(self, wsgi_app, mapper, use_method_override=True,
                 path_info=True):
        """Create a Route middleware object

        Using the use_method_override keyword will require Paste to be
        installed, and your application should use Paste's WSGIRequest
        object as it will properly handle POST issues with wsgi.input
        should Routes check it.

        If path_info is True, then should a route var contain
        path_info, the SCRIPT_NAME and PATH_INFO will be altered
        accordingly. This should be used with routes like:

        .. code-block:: python

            map.connect('blog/*path_info', controller='blog', path_info='')

        """
        self.app = wsgi_app
        self.mapper = mapper
        self.use_method_override = use_method_override
        self.path_info = path_info
        log_debug = self.log_debug = logging.DEBUG >= log.getEffectiveLevel()
        if self.log_debug:
            log.debug("Initialized with method overriding = %s, and path "
                  "info altering = %s", use_method_override, path_info)

    def __call__(self, environ, start_response):
        """Resolves the URL in PATH_INFO, and uses wsgi.routing_args
        to pass on URL resolver results."""
        config = request_config()
        config.mapper = self.mapper

        old_method = None
        if self.use_method_override:
            req = None

            # In some odd cases, there's no query string
            try:
                qs = environ['QUERY_STRING']
            except KeyError:
                qs = ''
            if '_method' in qs:
                req = Request(environ)
                req.errors = 'ignore'
                if '_method' in req.GET:
                    old_method = environ['REQUEST_METHOD']
                    environ['REQUEST_METHOD'] = req.GET['_method'].upper()
                    if self.log_debug:
                        log.debug("_method found in QUERY_STRING, altering request"
                                " method to %s", environ['REQUEST_METHOD'])
            elif environ['REQUEST_METHOD'] == 'POST' and is_form_post(environ):
                if req is None:
                    req = Request(environ)
                    req.errors = 'ignore'
                if '_method' in req.POST:
                    old_method = environ['REQUEST_METHOD']
                    environ['REQUEST_METHOD'] = req.POST['_method'].upper()
                    if self.log_debug:
                        log.debug("_method found in POST data, altering request "
                                  "method to %s", environ['REQUEST_METHOD'])

        # Run the actual route matching
        # -- Assignment of environ to config triggers route matching
        config.environ = environ

        match = config.mapper_dict
        route = config.route

        if old_method:
            environ['REQUEST_METHOD'] = old_method

        if not match:
            match = {}
            if self.log_debug:
                urlinfo = "%s %s" % (environ['REQUEST_METHOD'], environ['PATH_INFO'])
                log.debug("No route matched for %s", urlinfo)
        elif self.log_debug:
            urlinfo = "%s %s" % (environ['REQUEST_METHOD'], environ['PATH_INFO'])
            log.debug("Matched %s", urlinfo)
            log.debug("Route path: '%s', defaults: %s", route.routepath,
                      route.defaults)
            log.debug("Match dict: %s", match)

        url = URLGenerator(self.mapper, environ)
        environ['wsgiorg.routing_args'] = ((url), match)
        environ['routes.route'] = route
        environ['routes.url'] = url

        if route and route.redirect:
            route_name = '_redirect_%s' % id(route)
            location = url_for(route_name, **match)
            log.debug("Using redirect route, redirect to '%s' with status"
                      "code: %s", location, route.redirect_status)
            start_response(route.redirect_status,
                           [('Content-Type', 'text/plain; charset=utf8'),
                            ('Location', location)])
            return []

        # If the route included a path_info attribute and it should be used to
        # alter the environ, we'll pull it out
        if self.path_info and 'path_info' in match:
            oldpath = environ['PATH_INFO']
            newpath = match.get('path_info') or ''
            environ['PATH_INFO'] = newpath
            if not environ['PATH_INFO'].startswith('/'):
                environ['PATH_INFO'] = '/' + environ['PATH_INFO']
            environ['SCRIPT_NAME'] += re.sub(r'^(.*?)/' + re.escape(newpath) + '$',
                                             r'\1', oldpath)
            if environ['SCRIPT_NAME'].endswith('/'):
                environ['SCRIPT_NAME'] = environ['SCRIPT_NAME'][:-1]

        response = self.app(environ, start_response)

        # Wrapped in try as in rare cases the attribute will be gone already
        try:
            del config.environ
            del self.mapper.environ
        except AttributeError:
            pass
        return response

def is_form_post(environ):
    """Determine whether the request is a POSTed html form"""
    content_type = environ.get('CONTENT_TYPE', '').lower()
    if ';' in content_type:
        content_type = content_type.split(';', 1)[0]
    return content_type in ('application/x-www-form-urlencoded',
                            'multipart/form-data')
<|endoftext|>


<commit_before># Copyright (c) 2016 Civic Knowledge. This file is licensed under the terms of the
# Revised BSD License, included in this distribution as LICENSE

from __future__ import print_function

import sys
from itertools import islice

from tabulate import tabulate

from rowgenerators import enumerate_contents, SourceSpec, SourceError
from rowgenerators._meta import __version__
from rowgenerators import RowGenerator
from tableintuit import RowIntuiter
from itertools import islice
from rowgenerators import TextEncodingError

#Change the row cache name
from rowgenerators.util import  get_cache

def prt(*args):
    print(*args)

def warn( *args):
    print('WARN:',*args)

def err(*args):
    import sys
    print("ERROR:", *args)
    sys.exit(1)

def run_row_intuit(path, cache):

    for encoding in ('ascii', 'utf8', 'latin1'):
        try:
            rows = list(islice(RowGenerator(url=path, encoding=encoding, cache=cache), 5000))
            return encoding, RowIntuiter().run(rows)
        except TextEncodingError:
            pass

    raise Exception('Failed to convert with any encoding')

def rowgen():
    import argparse

    parser = argparse.ArgumentParser(
        prog='rowgen',
        description='Return CSV rows of data from a rowgenerator URL'.format(__version__))

    parser.add_argument('-H', '--head', default=False, action='store_true',
                        help='Display only the first 20 lines, in tabular format')

    parser.add_argument('-e', '--encoding', default=None, action='store_true',
                        help='Force the encoding')

    parser.add_argument('-f', '--format', default=None, action='store_true',
                        help="Force the file format. Typical values are 'csv', 'xls', 'xlsx' ")

    parser.add_argument('-u', '--urlfiletype', default=None, action='store_true',
                        help="Force the type of the file downloaded from the url. Equivalent to changing the file extension ")

    parser.add_argument('-s', '--start',
                        help='Line number where data starts')

    parser.add_argument('-d', '--headers', default=None, action='store_true',
                        help="Comma seperated list of header line numebrs")

    parser.add_argument('-E', '--enumerate', default=None, action='store_true',
                        help="Download the URL and enumerate it's contents as URLs")

    parser.add_argument('-i', '--intuit', default=None, action='store_true',
                        help="Intuit headers, start lines, etc")

    parser.add_argument('-I', '--info', default=None, action='store_true',
                        help="Print information about the url")

    parser.add_argument('url')

    cache = get_cache()

    args = parser.parse_args(sys.argv[1:])

    ss = SourceSpec(url=args.url, target_format=args.format, encoding=args.encoding, resource_format=args.urlfiletype)

    contents = list(enumerate_contents(ss, cache_fs=cache))

    if args.info:
        prt(tabulate(ss.dict.items()))
        sys.exit(0)

    if args.enumerate:
        for s in contents:
            print(s.rebuild_url())
    elif args.intuit:
        for s in contents:

            try:
                encoding, ri = run_row_intuit(s.rebuild_url(),cache=cache)

                prt("{} headers={} start={} encoding={}".format(
                        s.rebuild_url(),
                        ','.join(str(e) for e in ri.header_lines),
                        ri.start_line,
                        encoding))
            except SourceError as e:
                warn("{}: {}".format(s.rebuild_url(), e))

    elif len(contents) == 1:
        s = contents.pop(0)

        rg = s.get_generator(cache=cache)

        print(tabulate(islice(rg,20)))

    elif len(contents) > 1 and not args.enumerate:
        warn("URL has multiple content files; enumerating instead")
        for s in contents:
            print(s.rebuild_url())

<commit_msg>Fixed option errors<commit_after># Copyright (c) 2016 Civic Knowledge. This file is licensed under the terms of the
# Revised BSD License, included in this distribution as LICENSE

from __future__ import print_function

import sys
from itertools import islice

from tabulate import tabulate

from rowgenerators import enumerate_contents, SourceSpec, SourceError
from rowgenerators._meta import __version__
from rowgenerators import RowGenerator
from tableintuit import RowIntuiter
from itertools import islice
from rowgenerators import TextEncodingError

#Change the row cache name
from rowgenerators.util import  get_cache

def prt(*args):
    print(*args)

def warn( *args):
    print('WARN:',*args)

def err(*args):
    import sys
    print("ERROR:", *args)
    sys.exit(1)

def run_row_intuit(path, cache):

    for encoding in ('ascii', 'utf8', 'latin1'):
        try:
            rows = list(islice(RowGenerator(url=path, encoding=encoding, cache=cache), 5000))
            return encoding, RowIntuiter().run(rows)
        except TextEncodingError:
            pass

    raise Exception('Failed to convert with any encoding')

def rowgen():
    import argparse

    parser = argparse.ArgumentParser(
        prog='rowgen',
        description='Return CSV rows of data from a rowgenerator URL'.format(__version__))

    parser.add_argument('-H', '--head', default=False, action='store_true',
                        help='Display only the first 20 lines, in tabular format')

    parser.add_argument('-e', '--encoding',
                        help='Force the encoding')

    parser.add_argument('-f', '--format',
                        help="Force the file format. Typical values are 'csv', 'xls', 'xlsx' ")

    parser.add_argument('-u', '--urlfiletype',
                        help="Force the type of the file downloaded from the url. Equivalent to changing the file extension ")

    parser.add_argument('-s', '--start',
                        help='Line number where data starts')

    parser.add_argument('-d', '--headers', default=None, action='store_true',
                        help="Comma seperated list of header line numebrs")

    parser.add_argument('-E', '--enumerate', default=None, action='store_true',
                        help="Download the URL and enumerate it's contents as URLs")

    parser.add_argument('-i', '--intuit', default=None, action='store_true',
                        help="Intuit headers, start lines, etc")

    parser.add_argument('-I', '--info', default=None, action='store_true',
                        help="Print information about the url")

    parser.add_argument('url')

    cache = get_cache()

    args = parser.parse_args(sys.argv[1:])

    ss = SourceSpec(url=args.url, target_format=args.format, encoding=args.encoding, resource_format=args.urlfiletype)

    contents = list(enumerate_contents(ss, cache_fs=cache))

    if args.info:
        prt(tabulate(ss.dict.items()))
        sys.exit(0)

    if args.enumerate:
        for s in contents:
            print(s.rebuild_url())

    elif args.intuit:
        for s in contents:

            try:
                encoding, ri = run_row_intuit(s.rebuild_url(),cache=cache)

                prt("{} headers={} start={} encoding={}".format(
                        s.rebuild_url(),
                        ','.join(str(e) for e in ri.header_lines),
                        ri.start_line,
                        encoding))
            except SourceError as e:
                warn("{}: {}".format(s.rebuild_url(), e))

    elif len(contents) == 1:
        s = contents.pop(0)

        rg = s.get_generator(cache=cache)

        print(tabulate(islice(rg,20)))

    elif len(contents) > 1 and not args.enumerate:
        warn("URL has multiple content files; enumerating instead")
        for s in contents:
            print(s.rebuild_url())

<|endoftext|>


<commit_before>import string
import discord
import datetime
import traceback
from discord.ext import commands
import roxbot
from roxbot import guild_settings

class ErrHandle:
	def __init__(self, bot_client):
		self.bot = bot_client
		self.dev = False  # For debugging

	async def on_error(self, event):
		if self.dev:
			traceback.print_exc()
		else:
			embed = discord.Embed(title="Roxbot Error", colour=roxbot.EmbedColours.red) # Red
			embed.add_field(name='Event', value=event)
			embed.description = '```py\n{}\n```'.format(traceback.format_exc())
			embed.timestamp = datetime.datetime.utcnow()
			await self.owner.send(embed=embed)

	async def on_command_error(self, ctx, error):
		self.owner = self.bot.get_user(self.bot.owner_id)
		if self.dev:
			raise error
		elif isinstance(error, commands.CommandInvokeError):
			embed = discord.Embed(title='Command Error', colour=roxbot.EmbedColours.dark_red)
			embed.description = str(error)
			embed.add_field(name='Server', value=ctx.guild)
			embed.add_field(name='Channel', value=ctx.channel.mention)
			embed.add_field(name='User', value=ctx.author)
			embed.add_field(name='Message', value=ctx.message.content)
			embed.timestamp = datetime.datetime.utcnow()
			await ctx.send(embed=embed)
		else:
			if isinstance(error, commands.NoPrivateMessage):
				embed = discord.Embed(description="This command cannot be used in private messages.")
			elif isinstance(error, commands.DisabledCommand):
				embed = discord.Embed(description="This command is disabled.")
			elif isinstance(error, commands.MissingRequiredArgument):
				embed = discord.Embed(description="Argument missing. {}".format(error.args[0]))
			elif isinstance(error, commands.BadArgument):
				embed = discord.Embed(description="Bad Argument given. {}".format(error.args[0]))
			elif isinstance(error, commands.TooManyArguments):
				embed = discord.Embed(description="Too many arguments given.")
			elif isinstance(error, commands.CommandNotFound):
				cc = guild_settings.get(ctx.guild).custom_commands
				if ctx.invoked_with in cc["1"]:
					embed = None
				elif len(ctx.message.content) <= 2:
					embed = None
				elif any(x in string.punctuation for x in ctx.message.content.strip(ctx.prefix)[0]):
					# Should avoid punctuation emoticons. Checks all of the command for punctuation in the string.
					embed = None
				else:
					embed = discord.Embed(description="That Command doesn't exist.")
			elif isinstance(error, commands.BotMissingPermissions):
				embed = discord.Embed(description="{}".format(error.args[0].replace("Bot", "roxbot")))
			elif isinstance(error, commands.MissingPermissions):
				embed = discord.Embed(description="{}".format(error.args[0]))
			elif isinstance(error, commands.NotOwner):
				embed = discord.Embed(description="You do not have permission to do this. You are not Roxie!")
			elif isinstance(error, commands.CommandOnCooldown):
				embed = discord.Embed(description="This command is on cooldown, please wait {:.2f} seconds before trying again.".format(error.retry_after))
			elif isinstance(error, commands.CheckFailure):
				embed = discord.Embed(description="You do not have permission to do this. Back off, thot!")
			elif isinstance(error, commands.CommandError):
				embed = discord.Embed(description="Command Error. {}".format(error.args[0]))
			else:
				embed = discord.Embed(
					description="Placeholder embed. If you see this please message {}.".format(str(self.owner)))
			if embed:
				embed.colour = roxbot.EmbedColours.dark_red
				await ctx.send(embed=embed, delete_after=8)

def setup(bot_client):
	bot_client.add_cog(ErrHandle(bot_client))
<commit_msg>fixed error in unhandled errors in the error handler so they should be dmed to me now<commit_after>import string
import discord
import datetime
import traceback
from discord.ext import commands
import roxbot
from roxbot import guild_settings

class ErrHandle:
	def __init__(self, bot_client):
		self.bot = bot_client
		self.dev = False  # For debugging

	async def on_error(self, event):
		if self.dev:
			traceback.print_exc()
		else:
			embed = discord.Embed(title="Roxbot Error", colour=roxbot.EmbedColours.red) # Red
			embed.add_field(name='Event', value=event)
			embed.description = '```py\n{}\n```'.format(traceback.format_exc())
			embed.timestamp = datetime.datetime.utcnow()
			await self.bot.get_user(self.bot.owner_id).send(embed=embed)

	async def on_command_error(self, ctx, error):
		self.owner = self.bot.get_user(self.bot.owner_id)
		if self.dev:
			raise error
		elif isinstance(error, commands.CommandInvokeError):
			embed = discord.Embed(title='Command Error', colour=roxbot.EmbedColours.dark_red)
			embed.description = str(error)
			embed.add_field(name='Server', value=ctx.guild)
			embed.add_field(name='Channel', value=ctx.channel.mention)
			embed.add_field(name='User', value=ctx.author)
			embed.add_field(name='Message', value=ctx.message.content)
			embed.timestamp = datetime.datetime.utcnow()
			await ctx.send(embed=embed)
		else:
			if isinstance(error, commands.NoPrivateMessage):
				embed = discord.Embed(description="This command cannot be used in private messages.")
			elif isinstance(error, commands.DisabledCommand):
				embed = discord.Embed(description="This command is disabled.")
			elif isinstance(error, commands.MissingRequiredArgument):
				embed = discord.Embed(description="Argument missing. {}".format(error.args[0]))
			elif isinstance(error, commands.BadArgument):
				embed = discord.Embed(description="Bad Argument given. {}".format(error.args[0]))
			elif isinstance(error, commands.TooManyArguments):
				embed = discord.Embed(description="Too many arguments given.")
			elif isinstance(error, commands.CommandNotFound):
				cc = guild_settings.get(ctx.guild).custom_commands
				if ctx.invoked_with in cc["1"]:
					embed = None
				elif len(ctx.message.content) <= 2:
					embed = None
				elif any(x in string.punctuation for x in ctx.message.content.strip(ctx.prefix)[0]):
					# Should avoid punctuation emoticons. Checks all of the command for punctuation in the string.
					embed = None
				else:
					embed = discord.Embed(description="That Command doesn't exist.")
			elif isinstance(error, commands.BotMissingPermissions):
				embed = discord.Embed(description="{}".format(error.args[0].replace("Bot", "roxbot")))
			elif isinstance(error, commands.MissingPermissions):
				embed = discord.Embed(description="{}".format(error.args[0]))
			elif isinstance(error, commands.NotOwner):
				embed = discord.Embed(description="You do not have permission to do this. You are not Roxie!")
			elif isinstance(error, commands.CommandOnCooldown):
				embed = discord.Embed(description="This command is on cooldown, please wait {:.2f} seconds before trying again.".format(error.retry_after))
			elif isinstance(error, commands.CheckFailure):
				embed = discord.Embed(description="You do not have permission to do this. Back off, thot!")
			elif isinstance(error, commands.CommandError):
				embed = discord.Embed(description="Command Error. {}".format(error.args[0]))
			else:
				embed = discord.Embed(
					description="Placeholder embed. If you see this please message {}.".format(str(self.owner)))
			if embed:
				embed.colour = roxbot.EmbedColours.dark_red
				await ctx.send(embed=embed, delete_after=8)

def setup(bot_client):
	bot_client.add_cog(ErrHandle(bot_client))
<|endoftext|>


<commit_before>import boto
import boto.ec2

def is_region_allowed(region):
    return region.lower().startswith("eu")

def is_region_ignored(region):
    return region.lower() in ["cn-north-1", "us-gov-west-1"]

def make_registrar():
    registry = {}

    def registrar(cls):
        registry[cls.SERVICE_NAME] = cls
        print("registering aws service %s" % cls.SERVICE_NAME)
        return cls

    registrar.all = registry
    return registrar

aws_handler = make_registrar()

@aws_handler
class EC2(object):
    SERVICE_NAME = "ec2"

    def __init__(self):
        regions = boto.ec2.regions() or []
        self.regions = [region for region in regions
                if not is_region_allowed(region.name) and not is_region_ignored(region.name)]

    def fetch_all_resources(self):
        for region in self.regions:
            connection = boto.ec2.connect_to_region(region.name)
            resources = self.fetch_all_resources_in_region(connection) or []
            for resource in resources:
                print("ec2 instance found in {region.name} -> MUST BE REMOVED\n\t{id} [{image_id}] - {instance_type}, since {launch_time}\n\tip {public_dns_name}, key {key_name}".format(**vars(resource)))

    def fetch_all_resources_in_region(self, connection):
        try:
            return connection.get_only_instances()
        except BaseException as e:
            print(e)
            print(connection)  # TODO more infos here

    def delete(self, instance):
        pass

@aws_handler
class S3(object):
    SERVICE_NAME = "s3"

    def fetch_all_resources(self):
        connection = boto.connect_s3()
        for bucket in connection.get_all_buckets():
            try:
                location = bucket.get_location()
            except:
                location = "__error__"
            if not is_region_allowed(location):
                print("s3 bucket found in {0} -> {1}\n\t{2}, created {3}".format(
                    location,
                    "okay" if is_region_allowed(location) else "MUST BE REMOVED!",  # TODO more generic
                    bucket.name,
                    bucket.creation_date))

    def delete(self, instance):
        pass

<commit_msg>implements workaround to prevent matching S3 buckets in Frankfurt (eu-central-1) due to a known AWS bug. See https://github.com/boto/boto/issues/2741<commit_after>import boto
import boto.ec2
from boto.exception import S3ResponseError

def is_region_allowed(region):
    return region.lower().startswith("eu")

def is_region_ignored(region):
    return region.lower() in ["cn-north-1", "us-gov-west-1"]

def make_registrar():
    registry = {}

    def registrar(cls):
        registry[cls.SERVICE_NAME] = cls
        print("registering aws service %s" % cls.SERVICE_NAME)
        return cls

    registrar.all = registry
    return registrar

aws_handler = make_registrar()

@aws_handler
class EC2(object):
    SERVICE_NAME = "ec2"

    def __init__(self):
        regions = boto.ec2.regions() or []
        self.regions = [region for region in regions
                if not is_region_allowed(region.name) and not is_region_ignored(region.name)]

    def fetch_all_resources(self):
        for region in self.regions:
            connection = boto.ec2.connect_to_region(region.name)
            resources = self.fetch_all_resources_in_region(connection) or []
            for resource in resources:
                print("ec2 instance found in {region.name} -> MUST BE REMOVED\n\t{id} [{image_id}] - {instance_type}, since {launch_time}\n\tip {public_dns_name}, key {key_name}".format(**vars(resource)))

    def fetch_all_resources_in_region(self, connection):
        try:
            return connection.get_only_instances()
        except BaseException as e:
            print(e)
            print(connection)  # TODO more infos here

    def delete(self, instance):
        pass

@aws_handler
class S3(object):
    SERVICE_NAME = "s3"

    def fetch_all_resources(self):
        connection = boto.connect_s3()
        for bucket in connection.get_all_buckets():
            try:
                location = bucket.get_location()
            except S3ResponseError as e:
                # See https://github.com/boto/boto/issues/2741
                if e.status == 400:
                    continue
                location = "__error__"
            if not is_region_allowed(location):
                print("s3 bucket found in {0} -> {1}\n\t{2}, created {3}".format(
                    location,
                    "okay" if is_region_allowed(location) else "MUST BE REMOVED!",  # TODO more generic
                    bucket.name,
                    bucket.creation_date))

    def delete(self, instance):
        pass

<|endoftext|>