<commit_before># coding: utf-8

import copy
from datetime import datetime

class Grafo(object):
    '''Definiçoes da estrutura do grafo e funções de algoritmos auxiliares'''
    def __init__(self):
        self.n = 0      # numero de vertices
        self.m = 0      # numero de arestas
        self.d = 0      # desvio medio
        self.arestas = {}
        self.pesos = {}
        self.distribuicao_graus = {}

    '''
    Recebe arquivo que possui um grafo, na primeira linha consta o número total de vértices.
    Cada linha deve indicar um conjunto de vertices e o peso separados por espaço.
    Irá alimentar os atributos do objeto grafo.
    '''
    def ler_arquivo(self, arquivo):
        self.n = int(arquivo.readline())
        for line in arquivo:
            s = line.split()
            if len(s) == 3:
                u, v, p = s
                key = str(u) + '-' + str(v)
                if u not in self.arestas:
                    self.arestas[u] = [v]
                    self.pesos[key] = [p]
                    self.m = self.m + 1
                else:
                    self.arestas[u].append(v)
                    self.pesos[key] = [p]
                    self.m = self.m + 1
            else:
                u, v = s
                if u not in self.arestas:
                    self.arestas[u] = [v]
                    self.m = self.m + 1
                else:
                    self.arestas[u].append(v)
                    self.m = self.m + 1
            if v not in self.arestas:
                self.arestas[v] = [u]
            else:
                self.arestas[v].append(u)
        self.d = 2 * self.m/self.n
        self._graus_empiricos()

    '''
    Deve-se indicar o nome do arquivo onde será escrito as informações.
    Escreve as informações do grafo em arquivo.
    '''
    def escrever_arquivo(self, arquivo):
        texto = '# n = ' + str(self.n) + '\n'
        texto += '# m = ' + str(self.m) + '\n'
        texto += '# d_medio = ' + str(self.d) + '\n'
        for m in self.distribuicao_graus:
            grau = self.distribuicao_graus[m]/self.n
            texto += str(m) + ' ' + str(grau) + '\n'
        arquivo.write(texto)

    '''
    Opcionalmente pode receber um arquivo.
    Imprime no console ou em arquivo a representação do grafo em lista de adjacência.
    '''
    def lista(self, arquivo=None):
        texto = ''
        for u in self.arestas:
            texto += u + ' '
            for v in self.arestas.get(u):
                texto += '->' + v + ' '
            texto += '\n'
        if arquivo:
            arquivo.write(texto)
        else:
            print(texto)

    '''
    Opcionalmente pode receber um arquivo.
    Imprime no console ou em arquivo a representação do grafo em matriz de adjacência.
    '''
    def matriz(self, arquivo=None):
        texto = ''
        texto += '  '
        for u in self.arestas:
            texto += u + ' '
        texto += '\n'
        for u in self.arestas:
            texto += u + ' '
            for v in self.arestas:
                if v in self.arestas[u]:
                    texto += '1' + ' '
                else:
                    texto += '0' + ' '
            texto += '\n'
        if arquivo:
            arquivo.write(texto)
        else:
            print(texto)

    '''
    Executa a busca em largura.
    Recebe um vértice em STRING para iniciar a busca.
    Retorna a lista de pais de cada vértice e a lista com o nível de cada vértice em uma árvore.
    '''
    def bfs(self, inicio):
        tempo1 = datetime.now()
        q = []
        visitado = []
        pais = {}
        arvore = {}
        count = 0
        q.append(inicio)
        arvore[inicio] = count
        visitado.append(inicio)
        while len(q) != 0:
            u = q.pop(0)
            count += 1
            for vertice in self.arestas[u]:
                if vertice not in visitado:
                    pais.update({vertice: u})
                    arvore[vertice] = count
                    visitado.append(vertice)
                    q.append(vertice)
        tempo2 = datetime.now()
        tempo = tempo2 - tempo1
        print(str(tempo.total_seconds()) + 's')
        return pais, arvore

    '''
    Recebe um arquivo e o início em STRING.
    Escreve as informações dos pais e nível na árvore de cada vértice no arquivo.
    '''
    def bfs_arquivo(self, arquivo, inicio):
        pais, arvore = self.bfs(inicio)
        texto = _texto_imprimir_arvore(arvore, pais)
        arquivo.write(texto)

    '''
    Executa a busca em profundidade.
    Recebe um vértice em STRING para iniciar a busca.
    Retorna a lista de pais de cada vértice e a lista com o nível de cada vértice em uma árvore.
    '''
    def dfs(self, inicio):
        tempo1 = datetime.now()
        visitado = []
        arvore = {}
        pais = {}
        count = -1
        arvore[inicio] = count
        pai = inicio
        for vertice in self.arestas:
            if vertice not in visitado:
                if vertice in self.arestas.get(pai):
                    pais.update({vertice: pai})
                self._dfs_visita(vertice, visitado, count, arvore, pais)
        tempo2 = datetime.now()
        tempo = tempo2 - tempo1
        print(str(tempo.total_seconds()) + 's')
        return pais, arvore

    '''Método privado não deve ser utilizado isoladamente'''
    def _dfs_visita(self, vertice, visitado, count, arvore, pais):
        visitado.append(vertice)
        pai = vertice
        count += 1
        arvore[vertice] = count
        for vertice_aux in self.arestas[vertice]:
            if vertice_aux not in visitado:
                pais.update({vertice_aux: pai})
                self._dfs_visita(vertice_aux, visitado, count, arvore, pais)

    '''
    Recebe um arquivo e o início em STRING.
    Escreve as informações dos pais e nível na árvore de cada vértice no arquivo.
    '''
    def dfs_arquivo(self, arquivo, inicio):
        pais, arvore = self.dfs(inicio)
        texto = _texto_imprimir_arvore(arvore, pais)
        arquivo.write(texto)

    '''
    Recebe o início em STRING para efetuar a busca.
    Verifica em um grafo todas as componentes conexas que ele possui e imprime um relatório detalhado no console.
    '''
    def conexo(self, inicio):
        pais, arvore = self.dfs(inicio)
        grafos = []
        grafo = []
        for vertice in arvore:
            if arvore[vertice] == 0:
                if len(grafo) > 0:
                    grafos.append(grafo)
                grafo = []
                grafo.append(vertice)
            else:
                grafo.append(vertice)
        grafos.append(grafo)
        grafos_aux = sorted(grafos, key=len, reverse=True)
        print('Número de componentes conexas deste grafo: ' + str(len(grafos)))
        for lista in grafos_aux:
            print(str(len(lista)) + ' vértices: ' + str(lista))
        print('Grafos Conexos em ordem decrescente:')
        print(grafos_aux)

    '''
    Recebe o início da busca em STRING e opcionalmente o fim da busca também em STRING.
    Retorna o menor caminho entre o inicio e o fim.
    Caso o fim não seja informado retorna uma lista com o menor custo para chegar em cada um dos vértices do grafo.
    '''
    def dijkstra(self, inicio, fim=None):
        distancia = dict()
        anterior = dict()
        distancia[inicio] = 0
        Q = copy.deepcopy(self.arestas)
        def extract_min():
            min = None
            ret = None
            for key in distancia:
                if key in Q and ((distancia[key] < min) if min != None else True):
                    min = distancia[key]
                    ret = key
            if ret is not None:
                del Q[ret]
            return ret
        while Q:
            u = extract_min()
            for v in self.arestas[u]:
                par = str(u) + '-' + str(v)
                par2 = str(v) + '-' + str(u)
                if par in self.pesos:
                    alt = distancia[u] + int(self.pesos[par][0])
                    if v not in distancia or alt < distancia[v]:
                        distancia[v] = alt
                        anterior[v] = u
                elif  par2 in self.pesos:
                    alt = distancia[u] + int(self.pesos[par2][0])
                    if v not in distancia or alt < distancia[v]:
                        distancia[v] = alt
                        anterior[v] = u
        node_list = list()
        try:
            next = fim
            while True:
                node_list.insert(0,next)
                next = anterior[next]
        except:
            pass
        print('Relação vértice/menor custo possível: ' + str(distancia))
        if fim:
            print('Menor caminho: ' + str(node_list))

    '''
    Recebe o início da busca em STRING e opcionalmente o fim da busca também em STRING.
    Retorna o caminho entre o inicio e o fim.
    Caso o fim não seja informado retorna uma lista com o caminho percorrido em passar por todos os vértices do grafo.
    '''
    def bfs_caminho(self, inicio, fim=None):
        q = []
        visitado = []
        arvore = {}
        caminho = []
        q.append(inicio)
        arvore[inicio] = 'raiz'
        visitado.append(inicio)
        while len(q) != 0:
            u = q.pop(0)
            for vertice in self.arestas[u]:
                if vertice not in visitado:
                    arvore[vertice] = u
                    visitado.append(vertice)
                    q.append(vertice)
                if fim:
                    if vertice == fim:
                        caminho.insert(0, vertice)
                        v = arvore.get(vertice)
                        while arvore.get(v) != 'raiz':
                            caminho.insert(0, v)
                            v = arvore.get(v)
                        caminho.insert(0, v)
                        print('Menor Caminho: ' + str(caminho))
                        return
        print('Caminho para todos os vértices: ' + str(visitado))

    '''
    Recebe o início da busca em STRING e opcionalmente o fim da busca também em STRING.
    Verifica se o grafo possui arestas com peso ou não.
    Retorna o menor caminho ou a distância para cada vértice.
    '''
    def menor_caminho(self, inicio, fim=None):
        if len(self.pesos) > 0:
            self.dijkstra(inicio, fim)
        else:
            self.bfs_caminho(inicio, fim)

    '''Método privado não deve ser utilizado isoladamente'''
    def _graus_empiricos(self):
        for m in self.arestas:
            grau = len(self.arestas[m])
            if grau not in self.distribuicao_graus:
                self.distribuicao_graus[grau] = 1
            else:
                self.distribuicao_graus[grau] = self.distribuicao_graus[grau] + 1

    '''
    Imprime no console os atributos do grafo.
    '''
    def imprimir_grafo(self):
        print('*** GRAFO DETALHADO ***')
        print('# n = ' + str(self.n))
        print('# m = ' + str(self.m))
        print('# d_medio = ' + str(self.d) + '\n')
        print('# conjunto arestas:')
        print(self.arestas)
        print('\n')
        print('# relação arestas e peso:')
        print(self.pesos)
        print('\n')
        print('# distribuiçao dos graus:')
        print(self.distribuicao_graus)

'''Método privado não deve ser utilizado isoladamente'''
def _texto_imprimir_arvore(arvore, pais):
    texto = ''
    for chave in pais:
        texto += 'Vertice ' + str(chave) + ': pai = ' + str(pais[chave]) + '\n'
    texto += '\n'
    for chave in arvore:
        texto += 'Vertice ' + str(chave) + ': nível = ' + str(arvore[chave]) + '\n'
    return texto
<commit_msg>verificação de arestas negativas adicionada<commit_after># coding: utf-8

import copy
from datetime import datetime

class Grafo(object):
    '''Definiçoes da estrutura do grafo e funções de algoritmos auxiliares'''
    def __init__(self):
        self.n = 0      # numero de vertices
        self.m = 0      # numero de arestas
        self.d = 0      # desvio medio
        self.arestas = {}
        self.pesos = {}
        self.distribuicao_graus = {}

    '''
    Recebe arquivo que possui um grafo, na primeira linha consta o número total de vértices.
    Cada linha deve indicar um conjunto de vertices e o peso separados por espaço.
    Irá alimentar os atributos do objeto grafo.
    '''
    def ler_arquivo(self, arquivo):
        self.n = int(arquivo.readline())
        for line in arquivo:
            s = line.split()
            if len(s) == 3:
                u, v, p = s
                key = str(u) + '-' + str(v)
                if u not in self.arestas:
                    self.arestas[u] = [v]
                    self.pesos[key] = [p]
                    self.m = self.m + 1
                else:
                    self.arestas[u].append(v)
                    self.pesos[key] = [p]
                    self.m = self.m + 1
            else:
                u, v = s
                if u not in self.arestas:
                    self.arestas[u] = [v]
                    self.m = self.m + 1
                else:
                    self.arestas[u].append(v)
                    self.m = self.m + 1
            if v not in self.arestas:
                self.arestas[v] = [u]
            else:
                self.arestas[v].append(u)
        self.d = 2 * self.m/self.n
        self._graus_empiricos()

    '''
    Deve-se indicar o nome do arquivo onde será escrito as informações.
    Escreve as informações do grafo em arquivo.
    '''
    def escrever_arquivo(self, arquivo):
        texto = '# n = ' + str(self.n) + '\n'
        texto += '# m = ' + str(self.m) + '\n'
        texto += '# d_medio = ' + str(self.d) + '\n'
        for m in self.distribuicao_graus:
            grau = self.distribuicao_graus[m]/self.n
            texto += str(m) + ' ' + str(grau) + '\n'
        arquivo.write(texto)

    '''
    Opcionalmente pode receber um arquivo.
    Imprime no console ou em arquivo a representação do grafo em lista de adjacência.
    '''
    def lista(self, arquivo=None):
        texto = ''
        for u in self.arestas:
            texto += u + ' '
            for v in self.arestas.get(u):
                texto += '->' + v + ' '
            texto += '\n'
        if arquivo:
            arquivo.write(texto)
        else:
            print(texto)

    '''
    Opcionalmente pode receber um arquivo.
    Imprime no console ou em arquivo a representação do grafo em matriz de adjacência.
    '''
    def matriz(self, arquivo=None):
        texto = ''
        texto += '  '
        for u in self.arestas:
            texto += u + ' '
        texto += '\n'
        for u in self.arestas:
            texto += u + ' '
            for v in self.arestas:
                if v in self.arestas[u]:
                    texto += '1' + ' '
                else:
                    texto += '0' + ' '
            texto += '\n'
        if arquivo:
            arquivo.write(texto)
        else:
            print(texto)

    '''
    Executa a busca em largura.
    Recebe um vértice em STRING para iniciar a busca.
    Retorna a lista de pais de cada vértice e a lista com o nível de cada vértice em uma árvore.
    '''
    def bfs(self, inicio):
        tempo1 = datetime.now()
        q = []
        visitado = []
        pais = {}
        arvore = {}
        count = 0
        q.append(inicio)
        arvore[inicio] = count
        visitado.append(inicio)
        while len(q) != 0:
            u = q.pop(0)
            count += 1
            for vertice in self.arestas[u]:
                if vertice not in visitado:
                    pais.update({vertice: u})
                    arvore[vertice] = count
                    visitado.append(vertice)
                    q.append(vertice)
        tempo2 = datetime.now()
        tempo = tempo2 - tempo1
        print(str(tempo.total_seconds()) + 's')
        return pais, arvore

    '''
    Recebe um arquivo e o início em STRING.
    Escreve as informações dos pais e nível na árvore de cada vértice no arquivo.
    '''
    def bfs_arquivo(self, arquivo, inicio):
        pais, arvore = self.bfs(inicio)
        texto = _texto_imprimir_arvore(arvore, pais)
        arquivo.write(texto)

    '''
    Executa a busca em profundidade.
    Recebe um vértice em STRING para iniciar a busca.
    Retorna a lista de pais de cada vértice e a lista com o nível de cada vértice em uma árvore.
    '''
    def dfs(self, inicio):
        tempo1 = datetime.now()
        visitado = []
        arvore = {}
        pais = {}
        count = -1
        arvore[inicio] = count
        pai = inicio
        for vertice in self.arestas:
            if vertice not in visitado:
                if vertice in self.arestas.get(pai):
                    pais.update({vertice: pai})
                self._dfs_visita(vertice, visitado, count, arvore, pais)
        tempo2 = datetime.now()
        tempo = tempo2 - tempo1
        print(str(tempo.total_seconds()) + 's')
        return pais, arvore

    '''Método privado não deve ser utilizado isoladamente'''
    def _dfs_visita(self, vertice, visitado, count, arvore, pais):
        visitado.append(vertice)
        pai = vertice
        count += 1
        arvore[vertice] = count
        for vertice_aux in self.arestas[vertice]:
            if vertice_aux not in visitado:
                pais.update({vertice_aux: pai})
                self._dfs_visita(vertice_aux, visitado, count, arvore, pais)

    '''
    Recebe um arquivo e o início em STRING.
    Escreve as informações dos pais e nível na árvore de cada vértice no arquivo.
    '''
    def dfs_arquivo(self, arquivo, inicio):
        pais, arvore = self.dfs(inicio)
        texto = _texto_imprimir_arvore(arvore, pais)
        arquivo.write(texto)

    '''
    Recebe o início em STRING para efetuar a busca.
    Verifica em um grafo todas as componentes conexas que ele possui e imprime um relatório detalhado no console.
    '''
    def conexo(self, inicio):
        pais, arvore = self.dfs(inicio)
        grafos = []
        grafo = []
        for vertice in arvore:
            if arvore[vertice] == 0:
                if len(grafo) > 0:
                    grafos.append(grafo)
                grafo = []
                grafo.append(vertice)
            else:
                grafo.append(vertice)
        grafos.append(grafo)
        grafos_aux = sorted(grafos, key=len, reverse=True)
        print('Número de componentes conexas deste grafo: ' + str(len(grafos)))
        for lista in grafos_aux:
            print(str(len(lista)) + ' vértices: ' + str(lista))
        print('Grafos Conexos em ordem decrescente:')
        print(grafos_aux)

    '''
    Recebe o início da busca em STRING e opcionalmente o fim da busca também em STRING.
    Retorna o menor caminho entre o inicio e o fim.
    Caso o fim não seja informado retorna uma lista com o menor custo para chegar em cada um dos vértices do grafo.
    '''
    def dijkstra(self, inicio, fim=None):
        for p in self.pesos:
            peso = self.pesos.get(p)[0]
            if int(peso) < 0:
                print('Essa operação não suporta grafos com arestas negativas!')
                return

        distancia = dict()
        anterior = dict()
        distancia[inicio] = 0
        Q = copy.deepcopy(self.arestas)
        def extract_min():
            min = None
            ret = None
            for key in distancia:
                if key in Q and ((distancia[key] < min) if min != None else True):
                    min = distancia[key]
                    ret = key
            if ret is not None:
                del Q[ret]
            return ret
        while Q:
            u = extract_min()
            for v in self.arestas[u]:
                par = str(u) + '-' + str(v)
                par2 = str(v) + '-' + str(u)
                if par in self.pesos:
                    alt = distancia[u] + int(self.pesos[par][0])
                    if v not in distancia or alt < distancia[v]:
                        distancia[v] = alt
                        anterior[v] = u
                elif  par2 in self.pesos:
                    alt = distancia[u] + int(self.pesos[par2][0])
                    if v not in distancia or alt < distancia[v]:
                        distancia[v] = alt
                        anterior[v] = u
        node_list = list()
        try:
            next = fim
            while True:
                node_list.insert(0,next)
                next = anterior[next]
        except:
            pass
        print('Relação vértice/menor custo possível: ' + str(distancia))
        if fim:
            print('Menor caminho: ' + str(node_list))

    '''
    Recebe o início da busca em STRING e opcionalmente o fim da busca também em STRING.
    Retorna o caminho entre o inicio e o fim.
    Caso o fim não seja informado retorna uma lista com o caminho percorrido em passar por todos os vértices do grafo.
    '''
    def bfs_caminho(self, inicio, fim=None):
        q = []
        visitado = []
        arvore = {}
        caminho = []
        q.append(inicio)
        arvore[inicio] = 'raiz'
        visitado.append(inicio)
        while len(q) != 0:
            u = q.pop(0)
            for vertice in self.arestas[u]:
                if vertice not in visitado:
                    arvore[vertice] = u
                    visitado.append(vertice)
                    q.append(vertice)
                if fim:
                    if vertice == fim:
                        caminho.insert(0, vertice)
                        v = arvore.get(vertice)
                        while arvore.get(v) != 'raiz':
                            caminho.insert(0, v)
                            v = arvore.get(v)
                        caminho.insert(0, v)
                        print('Menor Caminho: ' + str(caminho))
                        return
        print('Caminho para todos os vértices: ' + str(visitado))

    '''
    Recebe o início da busca em STRING e opcionalmente o fim da busca também em STRING.
    Verifica se o grafo possui arestas com peso ou não.
    Retorna o menor caminho ou a distância para cada vértice.
    '''
    def menor_caminho(self, inicio, fim=None):
        if len(self.pesos) > 0:
            self.dijkstra(inicio, fim)
        else:
            self.bfs_caminho(inicio, fim)

    '''Método privado não deve ser utilizado isoladamente'''
    def _graus_empiricos(self):
        for m in self.arestas:
            grau = len(self.arestas[m])
            if grau not in self.distribuicao_graus:
                self.distribuicao_graus[grau] = 1
            else:
                self.distribuicao_graus[grau] = self.distribuicao_graus[grau] + 1

    '''
    Imprime no console os atributos do grafo.
    '''
    def imprimir_grafo(self):
        print('*** GRAFO DETALHADO ***')
        print('# n = ' + str(self.n))
        print('# m = ' + str(self.m))
        print('# d_medio = ' + str(self.d) + '\n')
        print('# conjunto arestas:')
        print(self.arestas)
        print('\n')
        print('# relação arestas e peso:')
        print(self.pesos)
        print('\n')
        print('# distribuiçao dos graus:')
        print(self.distribuicao_graus)

'''Método privado não deve ser utilizado isoladamente'''
def _texto_imprimir_arvore(arvore, pais):
    texto = ''
    for chave in pais:
        texto += 'Vertice ' + str(chave) + ': pai = ' + str(pais[chave]) + '\n'
    texto += '\n'
    for chave in arvore:
        texto += 'Vertice ' + str(chave) + ': nível = ' + str(arvore[chave]) + '\n'
    return texto
<|endoftext|>


<commit_before>from .dataObject import DatatypeProperty
from .class_registry import Module, ModuleAccess, ClassDescription

class PythonModule(Module):
    '''
    A Python module
    '''

    name = DatatypeProperty(multiple=False)
    ''' The full name of the module '''

    def defined_augment(self):
        return self.name.has_defined_value()

    def identifier_augment(self):
        return self.make_identifier_direct(self.name.defined_values[0].identifier)

class PyPIPackage(ModuleAccess):

    '''
    Describes a package hosted on the Python Package Index (PyPI)
    '''

    name = DatatypeProperty()
    version = DatatypeProperty()

class PythonClassDescription(ClassDescription):
    name = DatatypeProperty()
    ''' Local name of the class (i.e., relative to the module name) '''

    def defined_augment(self):
        return self.name.has_defined_value() and self.module.has_defined_value()

    def identifier_augment(self):
        return self.make_identifier(self.name.defined_values[0].identifier.n3() +
                                    self.module.defined_values[0].identifier.n3())

__yarom_mapped_classes__ = (PythonModule, PyPIPackage, PythonClassDescription)
<commit_msg>Making stringifying because Python 2<commit_after>from .dataObject import DatatypeProperty
from .class_registry import Module, ModuleAccess, ClassDescription

class PythonModule(Module):
    '''
    A Python module
    '''

    name = DatatypeProperty(multiple=False)
    ''' The full name of the module '''

    def defined_augment(self):
        return self.name.has_defined_value()

    def identifier_augment(self):
        return self.make_identifier_direct(str(self.name.defined_values[0].identifier))

class PyPIPackage(ModuleAccess):

    '''
    Describes a package hosted on the Python Package Index (PyPI)
    '''

    name = DatatypeProperty()
    version = DatatypeProperty()

class PythonClassDescription(ClassDescription):
    name = DatatypeProperty()
    ''' Local name of the class (i.e., relative to the module name) '''

    def defined_augment(self):
        return self.name.has_defined_value() and self.module.has_defined_value()

    def identifier_augment(self):
        return self.make_identifier(self.name.defined_values[0].identifier.n3() +
                                    self.module.defined_values[0].identifier.n3())

__yarom_mapped_classes__ = (PythonModule, PyPIPackage, PythonClassDescription)
<|endoftext|>


<commit_before># encoding: utf-8
"""
An embedded IPython shell.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import with_statement
from __future__ import print_function

import sys
import warnings

from IPython.core import ultratb, compilerop
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.core.interactiveshell import DummyMod
from IPython.core.interactiveshell import InteractiveShell
from IPython.terminal.interactiveshell import TerminalInteractiveShell
from IPython.terminal.ipapp import load_default_config

from traitlets import Bool, CBool, Unicode
from IPython.utils.io import ask_yes_no

class KillEmbeded(Exception):pass

# This is an additional magic that is exposed in embedded shells.
@magics_class
class EmbeddedMagics(Magics):

    @line_magic
    def kill_embedded(self, parameter_s=''):
        """%kill_embedded : deactivate for good the current embedded IPython.

        This function (after asking for confirmation) sets an internal flag so
        that an embedded IPython will never activate again.  This is useful to
        permanently disable a shell that is being called inside a loop: once
        you've figured out what you needed from it, you may then kill it and
        the program will then continue to run without the interactive shell
        interfering again.
        """

        kill = ask_yes_no("Are you sure you want to kill this embedded instance "
                         "(y/n)? [y/N] ",'n')
        if kill:
            self.shell.embedded_active = False
            print ("This embedded IPython will not reactivate anymore "
                   "once you exit.")

    @line_magic
    def exit_raise(self, parameter_s=''):
        """%exit_raise Make the current embedded kernel exit and raise and exception.

        This function sets an internal flag so that an embedded IPython will
        raise a `IPython.terminal.embed.KillEmbeded` Exception on exit, and then exit the current I. This is
        useful to permanently exit a loop that create IPython embed instance.
        """

        self.shell.should_raise = True
        self.shell.ask_exit()

class InteractiveShellEmbed(TerminalInteractiveShell):

    dummy_mode = Bool(False)
    exit_msg = Unicode('')
    embedded = CBool(True)
    embedded_active = CBool(True)
    should_raise = CBool(False)
    # Like the base class display_banner is not configurable, but here it
    # is True by default.
    display_banner = CBool(True)
    exit_msg = Unicode()

    def __init__(self, **kw):

        if kw.get('user_global_ns', None) is not None:
            warnings.warn("user_global_ns has been replaced by user_module. The\
                           parameter will be ignored, and removed in IPython 5.0", DeprecationWarning)

        super(InteractiveShellEmbed,self).__init__(**kw)

        # don't use the ipython crash handler so that user exceptions aren't
        # trapped
        sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
                                             mode=self.xmode,
                                             call_pdb=self.pdb)

    def init_sys_modules(self):
        pass

    def init_magics(self):
        super(InteractiveShellEmbed, self).init_magics()
        self.register_magics(EmbeddedMagics)

    def __call__(self, header='', local_ns=None, module=None, dummy=None,
                 stack_depth=1, global_ns=None, compile_flags=None):
        """Activate the interactive interpreter.

        __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
        the interpreter shell with the given local and global namespaces, and
        optionally print a header string at startup.

        The shell can be globally activated/deactivated using the
        dummy_mode attribute. This allows you to turn off a shell used
        for debugging globally.

        However, *each* time you call the shell you can override the current
        state of dummy_mode with the optional keyword parameter 'dummy'. For
        example, if you set dummy mode on with IPShell.dummy_mode = True, you
        can still have a specific call work by making it as IPShell(dummy=False).
        """

        # If the user has turned it off, go away
        if not self.embedded_active:
            return

        # Normal exits from interactive mode set this flag, so the shell can't
        # re-enter (it checks this variable at the start of interactive mode).
        self.exit_now = False

        # Allow the dummy parameter to override the global __dummy_mode
        if dummy or (dummy != 0 and self.dummy_mode):
            return

        if self.has_readline:
            self.set_readline_completer()

        # self.banner is auto computed
        if header:
            self.old_banner2 = self.banner2
            self.banner2 = self.banner2 + '\n' + header + '\n'
        else:
            self.old_banner2 = ''

        # Call the embedding code with a stack depth of 1 so it can skip over
        # our call and get the original caller's namespaces.
        self.mainloop(local_ns, module, stack_depth=stack_depth,
                      global_ns=global_ns, compile_flags=compile_flags)

        self.banner2 = self.old_banner2

        if self.exit_msg is not None:
            print(self.exit_msg)

        if self.should_raise:
            raise KillEmbeded('Embedded IPython raising error, as user requested.')

    def mainloop(self, local_ns=None, module=None, stack_depth=0,
                 display_banner=None, global_ns=None, compile_flags=None):
        """Embeds IPython into a running python program.

        Parameters
        ----------

        local_ns, module
          Working local namespace (a dict) and module (a module or similar
          object). If given as None, they are automatically taken from the scope
          where the shell was called, so that program variables become visible.

        stack_depth : int
          How many levels in the stack to go to looking for namespaces (when
          local_ns or module is None). This allows an intermediate caller to
          make sure that this function gets the namespace from the intended
          level in the stack. By default (0) it will get its locals and globals
          from the immediate caller.

        compile_flags
          A bit field identifying the __future__ features
          that are enabled, as passed to the builtin :func:`compile` function.
          If given as None, they are automatically taken from the scope where
          the shell was called.

        """

        if (global_ns is not None) and (module is None):
            warnings.warn("global_ns is deprecated, and will be removed in IPython 5.0 use module instead.", DeprecationWarning)
            module = DummyMod()
            module.__dict__ = global_ns

        # Get locals and globals from caller
        if ((local_ns is None or module is None or compile_flags is None)
            and self.default_user_namespaces):
            call_frame = sys._getframe(stack_depth).f_back

            if local_ns is None:
                local_ns = call_frame.f_locals
            if module is None:
                global_ns = call_frame.f_globals
                module = sys.modules[global_ns['__name__']]
            if compile_flags is None:
                compile_flags = (call_frame.f_code.co_flags &
                                 compilerop.PyCF_MASK)

        # Save original namespace and module so we can restore them after
        # embedding; otherwise the shell doesn't shut down correctly.
        orig_user_module = self.user_module
        orig_user_ns = self.user_ns
        orig_compile_flags = self.compile.flags

        # Update namespaces and fire up interpreter

        # The global one is easy, we can just throw it in
        if module is not None:
            self.user_module = module

        # But the user/local one is tricky: ipython needs it to store internal
        # data, but we also need the locals. We'll throw our hidden variables
        # like _ih and get_ipython() into the local namespace, but delete them
        # later.
        if local_ns is not None:
            reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()}
            self.user_ns = reentrant_local_ns
            self.init_user_ns()

        # Compiler flags
        if compile_flags is not None:
            self.compile.flags = compile_flags

        # make sure the tab-completer has the correct frame information, so it
        # actually completes using the frame's locals/globals
        self.set_completer_frame()

        with self.builtin_trap, self.display_trap:
            self.interact(display_banner=display_banner)

        # now, purge out the local namespace of IPython's hidden variables.
        if local_ns is not None:
            local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()})

        # Restore original namespace so shell can shut down when we exit.
        self.user_module = orig_user_module
        self.user_ns = orig_user_ns
        self.compile.flags = orig_compile_flags

def embed(**kwargs):
    """Call this to embed IPython at the current point in your program.

    The first invocation of this will create an :class:`InteractiveShellEmbed`
    instance and then call it.  Consecutive calls just call the already
    created instance.

    If you don't want the kernel to initialize the namespace
    from the scope of the surrounding function,
    and/or you want to load full IPython configuration,
    you probably want `IPython.start_ipython()` instead.

    Here is a simple example::

        from IPython import embed
        a = 10
        b = 20
        embed(header='First time')
        c = 30
        d = 40
        embed()

    Full customization can be done by passing a :class:`Config` in as the
    config argument.
    """
    config = kwargs.get('config')
    header = kwargs.pop('header', u'')
    compile_flags = kwargs.pop('compile_flags', None)
    if config is None:
        config = load_default_config()
        config.InteractiveShellEmbed = config.TerminalInteractiveShell
        config.InteractiveShellEmbed.colors='nocolor'
        kwargs['config'] = config
    #save ps1/ps2 if defined
    ps1 = None
    ps2 = None
    try:
        ps1 = sys.ps1
        ps2 = sys.ps2
    except AttributeError:
        pass
    #save previous instance
    saved_shell_instance = InteractiveShell._instance
    if saved_shell_instance is not None:
        cls = type(saved_shell_instance)
        cls.clear_instance()
    shell = InteractiveShellEmbed.instance(**kwargs)
    shell(header=header, stack_depth=2, compile_flags=compile_flags)
    InteractiveShellEmbed.clear_instance()
    #restore previous instance
    if saved_shell_instance is not None:
        cls = type(saved_shell_instance)
        cls.clear_instance()
        for subclass in cls._walk_mro():
            subclass._instance = saved_shell_instance
    if ps1 is not None:
        sys.ps1 = ps1
        sys.ps2 = ps2
<commit_msg>Switch embedded IPython over to prompt_toolkit shell<commit_after># encoding: utf-8
"""
An embedded IPython shell.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import with_statement
from __future__ import print_function

import sys
import warnings

from IPython.core import ultratb, compilerop
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.core.interactiveshell import DummyMod
from IPython.core.interactiveshell import InteractiveShell
from IPython.terminal.ptshell import TerminalInteractiveShell
from IPython.terminal.ipapp import load_default_config

from traitlets import Bool, CBool, Unicode
from IPython.utils.io import ask_yes_no

class KillEmbeded(Exception):pass

# This is an additional magic that is exposed in embedded shells.
@magics_class
class EmbeddedMagics(Magics):

    @line_magic
    def kill_embedded(self, parameter_s=''):
        """%kill_embedded : deactivate for good the current embedded IPython.

        This function (after asking for confirmation) sets an internal flag so
        that an embedded IPython will never activate again.  This is useful to
        permanently disable a shell that is being called inside a loop: once
        you've figured out what you needed from it, you may then kill it and
        the program will then continue to run without the interactive shell
        interfering again.
        """

        kill = ask_yes_no("Are you sure you want to kill this embedded instance "
                         "(y/n)? [y/N] ",'n')
        if kill:
            self.shell.embedded_active = False
            print ("This embedded IPython will not reactivate anymore "
                   "once you exit.")

    @line_magic
    def exit_raise(self, parameter_s=''):
        """%exit_raise Make the current embedded kernel exit and raise and exception.

        This function sets an internal flag so that an embedded IPython will
        raise a `IPython.terminal.embed.KillEmbeded` Exception on exit, and then exit the current I. This is
        useful to permanently exit a loop that create IPython embed instance.
        """

        self.shell.should_raise = True
        self.shell.ask_exit()

class InteractiveShellEmbed(TerminalInteractiveShell):

    dummy_mode = Bool(False)
    exit_msg = Unicode('')
    embedded = CBool(True)
    embedded_active = CBool(True)
    should_raise = CBool(False)
    # Like the base class display_banner is not configurable, but here it
    # is True by default.
    display_banner = CBool(True)
    exit_msg = Unicode()

    def __init__(self, **kw):

        if kw.get('user_global_ns', None) is not None:
            warnings.warn("user_global_ns has been replaced by user_module. The\
                           parameter will be ignored, and removed in IPython 5.0", DeprecationWarning)

        super(InteractiveShellEmbed,self).__init__(**kw)

        # don't use the ipython crash handler so that user exceptions aren't
        # trapped
        sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
                                             mode=self.xmode,
                                             call_pdb=self.pdb)

    def init_sys_modules(self):
        pass

    def init_magics(self):
        super(InteractiveShellEmbed, self).init_magics()
        self.register_magics(EmbeddedMagics)

    def __call__(self, header='', local_ns=None, module=None, dummy=None,
                 stack_depth=1, global_ns=None, compile_flags=None):
        """Activate the interactive interpreter.

        __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
        the interpreter shell with the given local and global namespaces, and
        optionally print a header string at startup.

        The shell can be globally activated/deactivated using the
        dummy_mode attribute. This allows you to turn off a shell used
        for debugging globally.

        However, *each* time you call the shell you can override the current
        state of dummy_mode with the optional keyword parameter 'dummy'. For
        example, if you set dummy mode on with IPShell.dummy_mode = True, you
        can still have a specific call work by making it as IPShell(dummy=False).
        """

        # If the user has turned it off, go away
        if not self.embedded_active:
            return

        # Normal exits from interactive mode set this flag, so the shell can't
        # re-enter (it checks this variable at the start of interactive mode).
        self.exit_now = False

        # Allow the dummy parameter to override the global __dummy_mode
        if dummy or (dummy != 0 and self.dummy_mode):
            return

        if self.has_readline:
            self.set_readline_completer()

        # self.banner is auto computed
        if header:
            self.old_banner2 = self.banner2
            self.banner2 = self.banner2 + '\n' + header + '\n'
        else:
            self.old_banner2 = ''

        if self.display_banner:
            self.show_banner()

        # Call the embedding code with a stack depth of 1 so it can skip over
        # our call and get the original caller's namespaces.
        self.mainloop(local_ns, module, stack_depth=stack_depth,
                      global_ns=global_ns, compile_flags=compile_flags)

        self.banner2 = self.old_banner2

        if self.exit_msg is not None:
            print(self.exit_msg)

        if self.should_raise:
            raise KillEmbeded('Embedded IPython raising error, as user requested.')

    def mainloop(self, local_ns=None, module=None, stack_depth=0,
                 display_banner=None, global_ns=None, compile_flags=None):
        """Embeds IPython into a running python program.

        Parameters
        ----------

        local_ns, module
          Working local namespace (a dict) and module (a module or similar
          object). If given as None, they are automatically taken from the scope
          where the shell was called, so that program variables become visible.

        stack_depth : int
          How many levels in the stack to go to looking for namespaces (when
          local_ns or module is None). This allows an intermediate caller to
          make sure that this function gets the namespace from the intended
          level in the stack. By default (0) it will get its locals and globals
          from the immediate caller.

        compile_flags
          A bit field identifying the __future__ features
          that are enabled, as passed to the builtin :func:`compile` function.
          If given as None, they are automatically taken from the scope where
          the shell was called.

        """

        if (global_ns is not None) and (module is None):
            warnings.warn("global_ns is deprecated, and will be removed in IPython 5.0 use module instead.", DeprecationWarning)
            module = DummyMod()
            module.__dict__ = global_ns

        if (display_banner is not None):
            warnings.warn("The display_banner parameter is deprecated.", DeprecationWarning)

        # Get locals and globals from caller
        if ((local_ns is None or module is None or compile_flags is None)
            and self.default_user_namespaces):
            call_frame = sys._getframe(stack_depth).f_back

            if local_ns is None:
                local_ns = call_frame.f_locals
            if module is None:
                global_ns = call_frame.f_globals
                module = sys.modules[global_ns['__name__']]
            if compile_flags is None:
                compile_flags = (call_frame.f_code.co_flags &
                                 compilerop.PyCF_MASK)

        # Save original namespace and module so we can restore them after
        # embedding; otherwise the shell doesn't shut down correctly.
        orig_user_module = self.user_module
        orig_user_ns = self.user_ns
        orig_compile_flags = self.compile.flags

        # Update namespaces and fire up interpreter

        # The global one is easy, we can just throw it in
        if module is not None:
            self.user_module = module

        # But the user/local one is tricky: ipython needs it to store internal
        # data, but we also need the locals. We'll throw our hidden variables
        # like _ih and get_ipython() into the local namespace, but delete them
        # later.
        if local_ns is not None:
            reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()}
            self.user_ns = reentrant_local_ns
            self.init_user_ns()

        # Compiler flags
        if compile_flags is not None:
            self.compile.flags = compile_flags

        # make sure the tab-completer has the correct frame information, so it
        # actually completes using the frame's locals/globals
        self.set_completer_frame()

        with self.builtin_trap, self.display_trap:
            self.interact()

        # now, purge out the local namespace of IPython's hidden variables.
        if local_ns is not None:
            local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()})

        # Restore original namespace so shell can shut down when we exit.
        self.user_module = orig_user_module
        self.user_ns = orig_user_ns
        self.compile.flags = orig_compile_flags

def embed(**kwargs):
    """Call this to embed IPython at the current point in your program.

    The first invocation of this will create an :class:`InteractiveShellEmbed`
    instance and then call it.  Consecutive calls just call the already
    created instance.

    If you don't want the kernel to initialize the namespace
    from the scope of the surrounding function,
    and/or you want to load full IPython configuration,
    you probably want `IPython.start_ipython()` instead.

    Here is a simple example::

        from IPython import embed
        a = 10
        b = 20
        embed(header='First time')
        c = 30
        d = 40
        embed()

    Full customization can be done by passing a :class:`Config` in as the
    config argument.
    """
    config = kwargs.get('config')
    header = kwargs.pop('header', u'')
    compile_flags = kwargs.pop('compile_flags', None)
    if config is None:
        config = load_default_config()
        config.InteractiveShellEmbed = config.TerminalInteractiveShell
        config.InteractiveShellEmbed.colors='nocolor'
        kwargs['config'] = config
    #save ps1/ps2 if defined
    ps1 = None
    ps2 = None
    try:
        ps1 = sys.ps1
        ps2 = sys.ps2
    except AttributeError:
        pass
    #save previous instance
    saved_shell_instance = InteractiveShell._instance
    if saved_shell_instance is not None:
        cls = type(saved_shell_instance)
        cls.clear_instance()
    shell = InteractiveShellEmbed.instance(**kwargs)
    shell(header=header, stack_depth=2, compile_flags=compile_flags)
    InteractiveShellEmbed.clear_instance()
    #restore previous instance
    if saved_shell_instance is not None:
        cls = type(saved_shell_instance)
        cls.clear_instance()
        for subclass in cls._walk_mro():
            subclass._instance = saved_shell_instance
    if ps1 is not None:
        sys.ps1 = ps1
        sys.ps2 = ps2
<|endoftext|>


<commit_before>"""
Given a text txt[0...n-1] and pattern pat[0...m-1], write a function to search all occurrences
of pattern and its permutations (or anagrams) in text. Expected time complexity is O(n).
Input:
txt: "ABAABA"
pat: "BA"

Output: 2 occurrences at indices: 0, 4
"""

"""
Approach:
1. The idea is to use a sliding window in text string.
2. Maintain two arrays to count the number of characters in pattern and current window of text.
3. If the count values are same, its a match. (For permutation, we are only concerned with counts).
4. To slide the window by one position, we just decrement count of leftmost character by 1, and increment
   count of next character by 1. (Idea similar to Rabin-Karp pattern search rolling hash function).
NOTE: Assuming we are dealing with same character set of txt and pattern of size 256.
"""

CHARSET_SIZE = 256

def compare(arr1, arr2):
    for i in range(CHARSET_SIZE):
        if arr1[i] != arr2[i]:
            return False

    return True

def search_anagrams(txt, pat):
    m = len(pat)

    countP = [0] * CHARSET_SIZE
    countT = [0] * CHARSET_SIZE

    for i in range(m):
        countP[ord(pat[i])] += 1
        countT[ord(txt[i])] += 1

    for i in range(m, len(txt)):

        if compare(countP, countT):
            print "Found at index ", i - m

        countT[ord(txt[i])] += 1
        countT[ord(txt[i - m])] -= 1

    if compare(countP, countT):
        print "Found at index ", len(txt) - m

if __name__ == '__main__':
    txt = 'BACDGABCDA'
    pat = 'ABCD'
    search_anagrams(txt, pat)
<commit_msg>Corrected example<commit_after>"""
Given a text txt[0...n-1] and pattern pat[0...m-1], write a function to search all occurrences
of pattern and its permutations (or anagrams) in text. Expected time complexity is O(n).
Input:
txt: "ABAABA"
pat: "BA"

Output: 4 occurrences at indices: 0, 1, 3, 4
"""

"""
Approach:
1. The idea is to use a sliding window in text string.
2. Maintain two arrays to count the number of characters in pattern and current window of text.
3. If the count values are same, its a match. (For permutation, we are only concerned with counts).
4. To slide the window by one position, we just decrement count of leftmost character by 1, and increment
   count of next character by 1. (Idea similar to Rabin-Karp pattern search rolling hash function).
NOTE: Assuming we are dealing with same character set of txt and pattern of size 256.
"""

CHARSET_SIZE = 256

def compare(arr1, arr2):
    for i in range(CHARSET_SIZE):
        if arr1[i] != arr2[i]:
            return False

    return True

def search_anagrams(txt, pat):
    m = len(pat)

    countP = [0] * CHARSET_SIZE
    countT = [0] * CHARSET_SIZE

    for i in range(m):
        countP[ord(pat[i])] += 1
        countT[ord(txt[i])] += 1

    for i in range(m, len(txt)):

        if compare(countP, countT):
            print "Found at index ", i - m

        countT[ord(txt[i])] += 1
        countT[ord(txt[i - m])] -= 1

    if compare(countP, countT):
        print "Found at index ", len(txt) - m

if __name__ == '__main__':
    txt = 'ABAABA'
    pat = 'BA'
    search_anagrams(txt, pat)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# encoding: utf-8
"""
The :class:`~IPython.core.application.Application` object for the command
line :command:`ipython` program.
"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import logging
import os
import sys
import warnings

from traitlets.config.loader import Config
from traitlets.config.application import boolean_flag, catch_config_error
from IPython.core import release
from IPython.core import usage
from IPython.core.completer import IPCompleter
from IPython.core.crashhandler import CrashHandler
from IPython.core.formatters import PlainTextFormatter
from IPython.core.history import HistoryManager
from IPython.core.application import (
    ProfileDir, BaseIPythonApplication, base_flags, base_aliases
)
from IPython.core.magics import (
    ScriptMagics, LoggingMagics
)
from IPython.core.shellapp import (
    InteractiveShellApp, shell_flags, shell_aliases
)
from IPython.extensions.storemagic import StoreMagics
from .interactiveshell import TerminalInteractiveShell
from IPython.paths import get_ipython_dir
from traitlets import (
    Bool, List, default, observe, Type
)

#-----------------------------------------------------------------------------
# Globals, utilities and helpers
#-----------------------------------------------------------------------------

_examples = """
ipython --matplotlib       # enable matplotlib integration
ipython --matplotlib=qt    # enable matplotlib integration with qt4 backend

ipython --log-level=DEBUG  # set logging to DEBUG
ipython --profile=foo      # start with profile foo

ipython profile create foo # create profile foo w/ default config files
ipython help profile       # show the help for the profile subcmd

ipython locate             # print the path to the IPython directory
ipython locate profile foo # print the path to the directory for profile `foo`
"""

#-----------------------------------------------------------------------------
# Crash handler for this application
#-----------------------------------------------------------------------------

class IPAppCrashHandler(CrashHandler):
    """sys.excepthook for IPython itself, leaves a detailed report on disk."""

    def __init__(self, app):
        contact_name = release.author
        contact_email = release.author_email
        bug_tracker = 'https://github.com/ipython/ipython/issues'
        super(IPAppCrashHandler,self).__init__(
            app, contact_name, contact_email, bug_tracker
        )

    def make_report(self,traceback):
        """Return a string containing a crash report."""

        sec_sep = self.section_sep
        # Start with parent report
        report = [super(IPAppCrashHandler, self).make_report(traceback)]
        # Add interactive-specific info we may have
        rpt_add = report.append
        try:
            rpt_add(sec_sep+"History of session input:")
            for line in self.app.shell.user_ns['_ih']:
                rpt_add(line)
            rpt_add('\n*** Last line of input (may not be in above history):\n')
            rpt_add(self.app.shell._last_input_line+'\n')
        except:
            pass

        return ''.join(report)

#-----------------------------------------------------------------------------
# Aliases and Flags
#-----------------------------------------------------------------------------
flags = dict(base_flags)
flags.update(shell_flags)
frontend_flags = {}
addflag = lambda *args: frontend_flags.update(boolean_flag(*args))
addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax',
        'Turn on auto editing of files with syntax errors.',
        'Turn off auto editing of files with syntax errors.'
)
addflag('simple-prompt', 'TerminalInteractiveShell.simple_prompt',
        "Force simple minimal prompt using `raw_input`",
        "Use a rich interactive prompt with prompt_toolkit",
)

addflag('banner', 'TerminalIPythonApp.display_banner',
        "Display a banner upon starting IPython.",
        "Don't display a banner upon starting IPython."
)
addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit',
    """Set to confirm when you try to exit IPython with an EOF (Control-D
    in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
    you can force a direct exit without any confirmation.""",
    "Don't prompt the user when exiting."
)
addflag('term-title', 'TerminalInteractiveShell.term_title',
    "Enable auto setting the terminal title.",
    "Disable auto setting the terminal title."
)
classic_config = Config()
classic_config.InteractiveShell.cache_size = 0
classic_config.PlainTextFormatter.pprint = False
classic_config.TerminalInteractiveShell.prompts_class='IPython.terminal.prompts.ClassicPrompts'
classic_config.InteractiveShell.separate_in = ''
classic_config.InteractiveShell.separate_out = ''
classic_config.InteractiveShell.separate_out2 = ''
classic_config.InteractiveShell.colors = 'NoColor'
classic_config.InteractiveShell.xmode = 'Plain'

frontend_flags['classic']=(
    classic_config,
    "Gives IPython a similar feel to the classic Python prompt."
)
# # log doesn't make so much sense this way anymore
# paa('--log','-l',
#     action='store_true', dest='InteractiveShell.logstart',
#     help="Start logging to the default log file (./ipython_log.py).")
#
# # quick is harder to implement
frontend_flags['quick']=(
    {'TerminalIPythonApp' : {'quick' : True}},
    "Enable quick startup with no config files."
)

frontend_flags['i'] = (
    {'TerminalIPythonApp' : {'force_interact' : True}},
    """If running code from the command line, become interactive afterwards.
    It is often useful to follow this with `--` to treat remaining flags as
    script arguments.
    """
)
flags.update(frontend_flags)

aliases = dict(base_aliases)
aliases.update(shell_aliases)

#-----------------------------------------------------------------------------
# Main classes and functions
#-----------------------------------------------------------------------------

class LocateIPythonApp(BaseIPythonApplication):
    description = """print the path to the IPython dir"""
    subcommands = dict(
        profile=('IPython.core.profileapp.ProfileLocate',
            "print the path to an IPython profile directory",
        ),
    )
    def start(self):
        if self.subapp is not None:
            return self.subapp.start()
        else:
            print(self.ipython_dir)

class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):
    name = u'ipython'
    description = usage.cl_usage
    crash_handler_class = IPAppCrashHandler
    examples = _examples

    flags = flags
    aliases = aliases
    classes = List()

    interactive_shell_class = Type(
        klass=object,   # use default_value otherwise which only allow subclasses.
        default_value=TerminalInteractiveShell,
        help="Class to use to instantiate the TerminalInteractiveShell object. Useful for custom Frontends"
    ).tag(config=True)

    @default('classes')
    def _classes_default(self):
        """This has to be in a method, for TerminalIPythonApp to be available."""
        return [
            InteractiveShellApp, # ShellApp comes before TerminalApp, because
            self.__class__,      # it will also affect subclasses (e.g. QtConsole)
            TerminalInteractiveShell,
            HistoryManager,
            ProfileDir,
            PlainTextFormatter,
            IPCompleter,
            ScriptMagics,
            LoggingMagics,
            StoreMagics,
        ]

    deprecated_subcommands = dict(
        qtconsole=('qtconsole.qtconsoleapp.JupyterQtConsoleApp',
            """DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter Qt Console."""
        ),
        notebook=('notebook.notebookapp.NotebookApp',
            """DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter HTML Notebook Server."""
        ),
        console=('jupyter_console.app.ZMQTerminalIPythonApp',
            """DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter terminal-based Console."""
        ),
        nbconvert=('nbconvert.nbconvertapp.NbConvertApp',
            "DEPRECATED, Will be removed in IPython 6.0 : Convert notebooks to/from other formats."
        ),
        trust=('nbformat.sign.TrustNotebookApp',
            "DEPRECATED, Will be removed in IPython 6.0 : Sign notebooks to trust their potentially unsafe contents at load."
        ),
        kernelspec=('jupyter_client.kernelspecapp.KernelSpecApp',
            "DEPRECATED, Will be removed in IPython 6.0 : Manage Jupyter kernel specifications."
        ),
    )
    subcommands = dict(
        profile = ("IPython.core.profileapp.ProfileApp",
            "Create and manage IPython profiles."
        ),
        kernel = ("ipykernel.kernelapp.IPKernelApp",
            "Start a kernel without an attached frontend."
        ),
        locate=('IPython.terminal.ipapp.LocateIPythonApp',
            LocateIPythonApp.description
        ),
        history=('IPython.core.historyapp.HistoryApp',
            "Manage the IPython history database."
        ),
    )
    deprecated_subcommands['install-nbextension'] = (
        "notebook.nbextensions.InstallNBExtensionApp",
        "DEPRECATED, Will be removed in IPython 6.0 : Install Jupyter notebook extension files"
    )
    subcommands.update(deprecated_subcommands)

    # *do* autocreate requested profile, but don't create the config file.
    auto_create=Bool(True)
    # configurables
    quick = Bool(False,
        help="""Start IPython quickly by skipping the loading of config files."""
    ).tag(config=True)
    @observe('quick')
    def _quick_changed(self, change):
        if change['new']:
            self.load_config_file = lambda *a, **kw: None

    display_banner = Bool(True,
        help="Whether to display a banner upon starting IPython."
    ).tag(config=True)

    # if there is code of files to run from the cmd line, don't interact
    # unless the --i flag (App.force_interact) is true.
    force_interact = Bool(False,
        help="""If a command or file is given via the command-line,
        e.g. 'ipython foo.py', start an interactive shell after executing the
        file or command."""
    ).tag(config=True)
    @observe('force_interact')
    def _force_interact_changed(self, change):
        if change['new']:
            self.interact = True

    @observe('file_to_run', 'code_to_run', 'module_to_run')
    def _file_to_run_changed(self, change):
        new = change['new']
        if new:
            self.something_to_run = True
        if new and not self.force_interact:
                self.interact = False

    # internal, not-configurable
    something_to_run=Bool(False)

    def parse_command_line(self, argv=None):
        """override to allow old '-pylab' flag with deprecation warning"""

        argv = sys.argv[1:] if argv is None else argv

        if '-pylab' in argv:
            # deprecated `-pylab` given,
            # warn and transform into current syntax
            argv = argv[:] # copy, don't clobber
            idx = argv.index('-pylab')
            warnings.warn("`-pylab` flag has been deprecated.\n"
            "    Use `--matplotlib <backend>` and import pylab manually.")
            argv[idx] = '--pylab'

        return super(TerminalIPythonApp, self).parse_command_line(argv)

    @catch_config_error
    def initialize(self, argv=None):
        """Do actions after construct, but before starting the app."""
        super(TerminalIPythonApp, self).initialize(argv)
        if self.subapp is not None:
            # don't bother initializing further, starting subapp
            return
        # print self.extra_args
        if self.extra_args and not self.something_to_run:
            self.file_to_run = self.extra_args[0]
        self.init_path()
        # create the shell
        self.init_shell()
        # and draw the banner
        self.init_banner()
        # Now a variety of things that happen after the banner is printed.
        self.init_gui_pylab()
        self.init_extensions()
        self.init_code()

    def init_shell(self):
        """initialize the InteractiveShell instance"""
        # Create an InteractiveShell instance.
        # shell.display_banner should always be False for the terminal
        # based app, because we call shell.show_banner() by hand below
        # so the banner shows *before* all extension loading stuff.
        self.shell = self.interactive_shell_class.instance(parent=self,
                        profile_dir=self.profile_dir,
                        ipython_dir=self.ipython_dir, user_ns=self.user_ns)
        self.shell.configurables.append(self)

    def init_banner(self):
        """optionally display the banner"""
        if self.display_banner and self.interact:
            self.shell.show_banner()
        # Make sure there is a space below the banner.
        if self.log_level <= logging.INFO: print()

    def _pylab_changed(self, name, old, new):
        """Replace --pylab='inline' with --pylab='auto'"""
        if new == 'inline':
            warnings.warn("'inline' not available as pylab backend, "
                      "using 'auto' instead.")
            self.pylab = 'auto'

    def start(self):
        if self.subapp is not None:
            return self.subapp.start()
        # perform any prexec steps:
        if self.interact:
            self.log.debug("Starting IPython's mainloop...")
            self.shell.mainloop()
        else:
            self.log.debug("IPython not interactive...")

def load_default_config(ipython_dir=None):
    """Load the default config file from the default ipython_dir.

    This is useful for embedded shells.
    """
    if ipython_dir is None:
        ipython_dir = get_ipython_dir()

    profile_dir = os.path.join(ipython_dir, 'profile_default')
    app = TerminalIPythonApp()
    app.config_file_paths.append(profile_dir)
    app.load_config_file()
    return app.config

launch_new_instance = TerminalIPythonApp.launch_instance

if __name__ == '__main__':
    launch_new_instance()
<commit_msg>Exit code 1 if non-interactive execution fails<commit_after>#!/usr/bin/env python
# encoding: utf-8
"""
The :class:`~IPython.core.application.Application` object for the command
line :command:`ipython` program.
"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import logging
import os
import sys
import warnings

from traitlets.config.loader import Config
from traitlets.config.application import boolean_flag, catch_config_error
from IPython.core import release
from IPython.core import usage
from IPython.core.completer import IPCompleter
from IPython.core.crashhandler import CrashHandler
from IPython.core.formatters import PlainTextFormatter
from IPython.core.history import HistoryManager
from IPython.core.application import (
    ProfileDir, BaseIPythonApplication, base_flags, base_aliases
)
from IPython.core.magics import (
    ScriptMagics, LoggingMagics
)
from IPython.core.shellapp import (
    InteractiveShellApp, shell_flags, shell_aliases
)
from IPython.extensions.storemagic import StoreMagics
from .interactiveshell import TerminalInteractiveShell
from IPython.paths import get_ipython_dir
from traitlets import (
    Bool, List, default, observe, Type
)

#-----------------------------------------------------------------------------
# Globals, utilities and helpers
#-----------------------------------------------------------------------------

_examples = """
ipython --matplotlib       # enable matplotlib integration
ipython --matplotlib=qt    # enable matplotlib integration with qt4 backend

ipython --log-level=DEBUG  # set logging to DEBUG
ipython --profile=foo      # start with profile foo

ipython profile create foo # create profile foo w/ default config files
ipython help profile       # show the help for the profile subcmd

ipython locate             # print the path to the IPython directory
ipython locate profile foo # print the path to the directory for profile `foo`
"""

#-----------------------------------------------------------------------------
# Crash handler for this application
#-----------------------------------------------------------------------------

class IPAppCrashHandler(CrashHandler):
    """sys.excepthook for IPython itself, leaves a detailed report on disk."""

    def __init__(self, app):
        contact_name = release.author
        contact_email = release.author_email
        bug_tracker = 'https://github.com/ipython/ipython/issues'
        super(IPAppCrashHandler,self).__init__(
            app, contact_name, contact_email, bug_tracker
        )

    def make_report(self,traceback):
        """Return a string containing a crash report."""

        sec_sep = self.section_sep
        # Start with parent report
        report = [super(IPAppCrashHandler, self).make_report(traceback)]
        # Add interactive-specific info we may have
        rpt_add = report.append
        try:
            rpt_add(sec_sep+"History of session input:")
            for line in self.app.shell.user_ns['_ih']:
                rpt_add(line)
            rpt_add('\n*** Last line of input (may not be in above history):\n')
            rpt_add(self.app.shell._last_input_line+'\n')
        except:
            pass

        return ''.join(report)

#-----------------------------------------------------------------------------
# Aliases and Flags
#-----------------------------------------------------------------------------
flags = dict(base_flags)
flags.update(shell_flags)
frontend_flags = {}
addflag = lambda *args: frontend_flags.update(boolean_flag(*args))
addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax',
        'Turn on auto editing of files with syntax errors.',
        'Turn off auto editing of files with syntax errors.'
)
addflag('simple-prompt', 'TerminalInteractiveShell.simple_prompt',
        "Force simple minimal prompt using `raw_input`",
        "Use a rich interactive prompt with prompt_toolkit",
)

addflag('banner', 'TerminalIPythonApp.display_banner',
        "Display a banner upon starting IPython.",
        "Don't display a banner upon starting IPython."
)
addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit',
    """Set to confirm when you try to exit IPython with an EOF (Control-D
    in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
    you can force a direct exit without any confirmation.""",
    "Don't prompt the user when exiting."
)
addflag('term-title', 'TerminalInteractiveShell.term_title',
    "Enable auto setting the terminal title.",
    "Disable auto setting the terminal title."
)
classic_config = Config()
classic_config.InteractiveShell.cache_size = 0
classic_config.PlainTextFormatter.pprint = False
classic_config.TerminalInteractiveShell.prompts_class='IPython.terminal.prompts.ClassicPrompts'
classic_config.InteractiveShell.separate_in = ''
classic_config.InteractiveShell.separate_out = ''
classic_config.InteractiveShell.separate_out2 = ''
classic_config.InteractiveShell.colors = 'NoColor'
classic_config.InteractiveShell.xmode = 'Plain'

frontend_flags['classic']=(
    classic_config,
    "Gives IPython a similar feel to the classic Python prompt."
)
# # log doesn't make so much sense this way anymore
# paa('--log','-l',
#     action='store_true', dest='InteractiveShell.logstart',
#     help="Start logging to the default log file (./ipython_log.py).")
#
# # quick is harder to implement
frontend_flags['quick']=(
    {'TerminalIPythonApp' : {'quick' : True}},
    "Enable quick startup with no config files."
)

frontend_flags['i'] = (
    {'TerminalIPythonApp' : {'force_interact' : True}},
    """If running code from the command line, become interactive afterwards.
    It is often useful to follow this with `--` to treat remaining flags as
    script arguments.
    """
)
flags.update(frontend_flags)

aliases = dict(base_aliases)
aliases.update(shell_aliases)

#-----------------------------------------------------------------------------
# Main classes and functions
#-----------------------------------------------------------------------------

class LocateIPythonApp(BaseIPythonApplication):
    description = """print the path to the IPython dir"""
    subcommands = dict(
        profile=('IPython.core.profileapp.ProfileLocate',
            "print the path to an IPython profile directory",
        ),
    )
    def start(self):
        if self.subapp is not None:
            return self.subapp.start()
        else:
            print(self.ipython_dir)

class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):
    name = u'ipython'
    description = usage.cl_usage
    crash_handler_class = IPAppCrashHandler
    examples = _examples

    flags = flags
    aliases = aliases
    classes = List()

    interactive_shell_class = Type(
        klass=object,   # use default_value otherwise which only allow subclasses.
        default_value=TerminalInteractiveShell,
        help="Class to use to instantiate the TerminalInteractiveShell object. Useful for custom Frontends"
    ).tag(config=True)

    @default('classes')
    def _classes_default(self):
        """This has to be in a method, for TerminalIPythonApp to be available."""
        return [
            InteractiveShellApp, # ShellApp comes before TerminalApp, because
            self.__class__,      # it will also affect subclasses (e.g. QtConsole)
            TerminalInteractiveShell,
            HistoryManager,
            ProfileDir,
            PlainTextFormatter,
            IPCompleter,
            ScriptMagics,
            LoggingMagics,
            StoreMagics,
        ]

    deprecated_subcommands = dict(
        qtconsole=('qtconsole.qtconsoleapp.JupyterQtConsoleApp',
            """DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter Qt Console."""
        ),
        notebook=('notebook.notebookapp.NotebookApp',
            """DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter HTML Notebook Server."""
        ),
        console=('jupyter_console.app.ZMQTerminalIPythonApp',
            """DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter terminal-based Console."""
        ),
        nbconvert=('nbconvert.nbconvertapp.NbConvertApp',
            "DEPRECATED, Will be removed in IPython 6.0 : Convert notebooks to/from other formats."
        ),
        trust=('nbformat.sign.TrustNotebookApp',
            "DEPRECATED, Will be removed in IPython 6.0 : Sign notebooks to trust their potentially unsafe contents at load."
        ),
        kernelspec=('jupyter_client.kernelspecapp.KernelSpecApp',
            "DEPRECATED, Will be removed in IPython 6.0 : Manage Jupyter kernel specifications."
        ),
    )
    subcommands = dict(
        profile = ("IPython.core.profileapp.ProfileApp",
            "Create and manage IPython profiles."
        ),
        kernel = ("ipykernel.kernelapp.IPKernelApp",
            "Start a kernel without an attached frontend."
        ),
        locate=('IPython.terminal.ipapp.LocateIPythonApp',
            LocateIPythonApp.description
        ),
        history=('IPython.core.historyapp.HistoryApp',
            "Manage the IPython history database."
        ),
    )
    deprecated_subcommands['install-nbextension'] = (
        "notebook.nbextensions.InstallNBExtensionApp",
        "DEPRECATED, Will be removed in IPython 6.0 : Install Jupyter notebook extension files"
    )
    subcommands.update(deprecated_subcommands)

    # *do* autocreate requested profile, but don't create the config file.
    auto_create=Bool(True)
    # configurables
    quick = Bool(False,
        help="""Start IPython quickly by skipping the loading of config files."""
    ).tag(config=True)
    @observe('quick')
    def _quick_changed(self, change):
        if change['new']:
            self.load_config_file = lambda *a, **kw: None

    display_banner = Bool(True,
        help="Whether to display a banner upon starting IPython."
    ).tag(config=True)

    # if there is code of files to run from the cmd line, don't interact
    # unless the --i flag (App.force_interact) is true.
    force_interact = Bool(False,
        help="""If a command or file is given via the command-line,
        e.g. 'ipython foo.py', start an interactive shell after executing the
        file or command."""
    ).tag(config=True)
    @observe('force_interact')
    def _force_interact_changed(self, change):
        if change['new']:
            self.interact = True

    @observe('file_to_run', 'code_to_run', 'module_to_run')
    def _file_to_run_changed(self, change):
        new = change['new']
        if new:
            self.something_to_run = True
        if new and not self.force_interact:
                self.interact = False

    # internal, not-configurable
    something_to_run=Bool(False)

    def parse_command_line(self, argv=None):
        """override to allow old '-pylab' flag with deprecation warning"""

        argv = sys.argv[1:] if argv is None else argv

        if '-pylab' in argv:
            # deprecated `-pylab` given,
            # warn and transform into current syntax
            argv = argv[:] # copy, don't clobber
            idx = argv.index('-pylab')
            warnings.warn("`-pylab` flag has been deprecated.\n"
            "    Use `--matplotlib <backend>` and import pylab manually.")
            argv[idx] = '--pylab'

        return super(TerminalIPythonApp, self).parse_command_line(argv)

    @catch_config_error
    def initialize(self, argv=None):
        """Do actions after construct, but before starting the app."""
        super(TerminalIPythonApp, self).initialize(argv)
        if self.subapp is not None:
            # don't bother initializing further, starting subapp
            return
        # print self.extra_args
        if self.extra_args and not self.something_to_run:
            self.file_to_run = self.extra_args[0]
        self.init_path()
        # create the shell
        self.init_shell()
        # and draw the banner
        self.init_banner()
        # Now a variety of things that happen after the banner is printed.
        self.init_gui_pylab()
        self.init_extensions()
        self.init_code()

    def init_shell(self):
        """initialize the InteractiveShell instance"""
        # Create an InteractiveShell instance.
        # shell.display_banner should always be False for the terminal
        # based app, because we call shell.show_banner() by hand below
        # so the banner shows *before* all extension loading stuff.
        self.shell = self.interactive_shell_class.instance(parent=self,
                        profile_dir=self.profile_dir,
                        ipython_dir=self.ipython_dir, user_ns=self.user_ns)
        self.shell.configurables.append(self)

    def init_banner(self):
        """optionally display the banner"""
        if self.display_banner and self.interact:
            self.shell.show_banner()
        # Make sure there is a space below the banner.
        if self.log_level <= logging.INFO: print()

    def _pylab_changed(self, name, old, new):
        """Replace --pylab='inline' with --pylab='auto'"""
        if new == 'inline':
            warnings.warn("'inline' not available as pylab backend, "
                      "using 'auto' instead.")
            self.pylab = 'auto'

    def start(self):
        if self.subapp is not None:
            return self.subapp.start()
        # perform any prexec steps:
        if self.interact:
            self.log.debug("Starting IPython's mainloop...")
            self.shell.mainloop()
        else:
            self.log.debug("IPython not interactive...")
            if not self.shell.last_execution_succeeded:
                sys.exit(1)

def load_default_config(ipython_dir=None):
    """Load the default config file from the default ipython_dir.

    This is useful for embedded shells.
    """
    if ipython_dir is None:
        ipython_dir = get_ipython_dir()

    profile_dir = os.path.join(ipython_dir, 'profile_default')
    app = TerminalIPythonApp()
    app.config_file_paths.append(profile_dir)
    app.load_config_file()
    return app.config

launch_new_instance = TerminalIPythonApp.launch_instance

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


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for `scopus.AffiliationSearch` module."""

from collections import namedtuple

from nose.tools import assert_equal, assert_true

from pybliometrics.scopus import AffiliationSearch

s1 = AffiliationSearch('af-id(60021784)', refresh=30)
s2 = AffiliationSearch('affil(Max Planck Munich)', download=False)

def test_affiliations():
    received1 = s1.affiliations
    assert_true(isinstance(received1, list))
    order = 'eid name variant documents city country parent'
    Affiliation = namedtuple('Affiliation', order)
    expected = Affiliation(eid='10-s2.0-60021784', name='New York University',
        variant='', documents='0', city='New York', country='United States',
        parent='0')
    assert_true(int(received1[0].documents) >= 99000)
    assert_equal(received1[0]._replace(documents="0"), expected)

def test_affiliations_nodownload():
    assert_equal(s2.affiliations, None)

def test_get_results_size():
    received1 = s1.get_results_size()
    assert_true(received1 >= 1)
    received2 = s2.get_results_size()
    assert_true(received2 >= 90)
<commit_msg>Upper case search; Update value<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for `scopus.AffiliationSearch` module."""

from collections import namedtuple

from nose.tools import assert_equal, assert_true

from pybliometrics.scopus import AffiliationSearch

s1 = AffiliationSearch('AF-ID(60021784)', refresh=30)
s2 = AffiliationSearch('AFFIL(Max Planck Munich)', download=False)

def test_affiliations():
    received1 = s1.affiliations
    assert_true(isinstance(received1, list))
    order = 'eid name variant documents city country parent'
    Affiliation = namedtuple('Affiliation', order)
    expected = Affiliation(eid='10-s2.0-60021784', name='New York University',
        variant='', documents='0', city='New York', country='United States',
        parent='0')
    assert_true(int(received1[0].documents) >= 99000)
    assert_equal(received1[0]._replace(documents="0"), expected)

def test_affiliations_nodownload():
    assert_equal(s2.affiliations, None)

def test_get_results_size():
    received1 = s1.get_results_size()
    assert_true(received1 >= 1)
    received2 = s2.get_results_size()
    assert_true(received2 >= 80)
<|endoftext|>


<commit_before>#!/usr/bin/python3
#
# Copyright (c) 2013 Mikkel Schubert <MikkelSch@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import os
import multiprocessing

import paleomix
import paleomix.common.logging

from paleomix.common.argparse import ArgumentParser

_DEFAULT_CONFIG_FILES = [
    "/etc/paleomix/phylo_pipeline.ini",
    "~/.paleomix/phylo_pipeline.ini",
]

def build_parser():
    parser = ArgumentParser(
        prog="paleomix phylo_pipeline", default_config_files=_DEFAULT_CONFIG_FILES,
    )
    parser.add_argument(
        "commands",
        help="One or more commands separated by '+'. Available commands are 'help', to "
        "display this message; 'example', to create an example project; 'makefile', to "
        "print a makefile template; 'genotype' to perform genotyping on a makefile; "
        "'msa' to perform multiple sequence alignment on a makefile; and 'phylogeny', "
        "to carry out phylogenetic inference on a makefile.",
    )
    parser.add_argument("files", nargs="*", help="One or more YAML files")

    parser.add_argument(
        "--version", action="version", version="%(prog)s v" + paleomix.__version__,
    )

    paleomix.common.logging.add_argument_group(parser, default="info")

    group = parser.add_argument_group("Scheduling")
    group.add_argument(
        "--examl-max-threads",
        default=1,
        type=int,
        help="Maximum number of threads for each instance of ExaML [%(default)s]",
    )
    group.add_argument(
        "--max-threads",
        type=int,
        default=max(2, multiprocessing.cpu_count()),
        help="Max number of threads to use in total [%(default)s]",
    )
    group.add_argument(
        "--dry-run",
        default=False,
        action="store_true",
        help="If passed, only a dry-run in performed, the dependency tree is printed, "
        "and no tasks are executed.",
    )

    group = parser.add_argument_group("Required paths")
    group.add_argument(
        "--temp-root",
        default="./temp",
        type=os.path.abspath,
        help="Location for temporary files and folders [%(default)s]",
    )
    group.add_argument(
        "--samples-root",
        default="./data/samples",
        help="Location of BAM files for each sample [%(default)s]",
    )
    group.add_argument(
        "--regions-root",
        default="./data/regions",
        help="Location of BED files containing regions of interest [%(default)s]",
    )
    group.add_argument(
        "--prefix-root",
        default="./data/prefixes",
        help="Location of prefixes (FASTAs) [%(default)s]",
    )
    group.add_argument(
        "--refseq-root",
        default="./data/refseqs",
        help="Location of reference sequences (FASTAs) [%(default)s]",
    )
    group.add_argument(
        "--destination",
        default="./results",
        help="The destination folder for result files [%(default)s]",
    )

    group = parser.add_argument_group("Files and executables")
    group.add_argument(
        "--list-input-files",
        action="store_true",
        default=False,
        help="List all input files used by pipeline for the "
        "makefile(s), excluding any generated by the "
        "pipeline itself.",
    )
    group.add_argument(
        "--list-output-files",
        action="store_true",
        default=False,
        help="List all output files generated by pipeline for " "the makefile(s).",
    )
    group.add_argument(
        "--list-executables",
        action="store_true",
        default=False,
        help="List all executables required by the pipeline, "
        "with version requirements (if any).",
    )

    return parser
<commit_msg>hide deprecated command-line option<commit_after>#!/usr/bin/python3
#
# Copyright (c) 2013 Mikkel Schubert <MikkelSch@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import os
import multiprocessing

import paleomix
import paleomix.common.logging

from paleomix.common.argparse import ArgumentParser, SUPPRESS

_DEFAULT_CONFIG_FILES = [
    "/etc/paleomix/phylo_pipeline.ini",
    "~/.paleomix/phylo_pipeline.ini",
]

def build_parser():
    parser = ArgumentParser(
        prog="paleomix phylo_pipeline", default_config_files=_DEFAULT_CONFIG_FILES,
    )
    parser.add_argument(
        "commands",
        help="One or more commands separated by '+'. Available commands are 'help', to "
        "display this message; 'example', to create an example project; 'makefile', to "
        "print a makefile template; 'genotype' to perform genotyping on a makefile; "
        "'msa' to perform multiple sequence alignment on a makefile; and 'phylogeny', "
        "to carry out phylogenetic inference on a makefile.",
    )
    parser.add_argument("files", nargs="*", help="One or more YAML files")

    parser.add_argument(
        "--version", action="version", version="%(prog)s v" + paleomix.__version__,
    )

    paleomix.common.logging.add_argument_group(parser, default="info")

    group = parser.add_argument_group("Scheduling")
    group.add_argument(
        "--examl-max-threads",
        default=1,
        type=int,
        help="Maximum number of threads for each instance of ExaML [%(default)s]",
    )
    group.add_argument(
        "--max-threads",
        type=int,
        default=max(2, multiprocessing.cpu_count()),
        help="Max number of threads to use in total [%(default)s]",
    )
    group.add_argument(
        "--dry-run",
        default=False,
        action="store_true",
        help="If passed, only a dry-run in performed, the dependency tree is printed, "
        "and no tasks are executed.",
    )

    group = parser.add_argument_group("Required paths")
    group.add_argument(
        "--temp-root",
        default="./temp",
        type=os.path.abspath,
        help="Location for temporary files and folders [%(default)s]",
    )
    group.add_argument(
        "--samples-root",
        default="./data/samples",
        help="Location of BAM files for each sample [%(default)s]",
    )
    group.add_argument(
        "--regions-root",
        default="./data/regions",
        help="Location of BED files containing regions of interest [%(default)s]",
    )
    group.add_argument(
        "--prefix-root",
        default="./data/prefixes",
        help="Location of prefixes (FASTAs) [%(default)s]",
    )
    group.add_argument("--refseq-root", help=SUPPRESS)
    group.add_argument(
        "--destination",
        default="./results",
        help="The destination folder for result files [%(default)s]",
    )

    group = parser.add_argument_group("Files and executables")
    group.add_argument(
        "--list-input-files",
        action="store_true",
        default=False,
        help="List all input files used by pipeline for the "
        "makefile(s), excluding any generated by the "
        "pipeline itself.",
    )
    group.add_argument(
        "--list-output-files",
        action="store_true",
        default=False,
        help="List all output files generated by pipeline for " "the makefile(s).",
    )
    group.add_argument(
        "--list-executables",
        action="store_true",
        default=False,
        help="List all executables required by the pipeline, "
        "with version requirements (if any).",
    )

    return parser
<|endoftext|>


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

"""Contains functions to establish scp connections"""

# Compatibility 2.7-3.4
from __future__ import absolute_import
from __future__ import unicode_literals

import os, re

def parse(originalcmd):
    """Parse the original scp command written by the user.
       Obtain the target, create the command to pass"""
    # We wait a command like :
    # scp [option] /path/to/my/file user@bastion:targetname//path/on/destination
    # or
    # scp [option] user@bastion:targetname//path/on/destination/file /local/path
    # and we should obtain the target name a line like
    # scp -t /path/on/destination/file
    # scp -f /path/on/destination/file /local/path
    target = re.findall("([^\s]*)//", originalcmd)
    if target:
        target = target[0]

    cmd = re.sub("\[^\s]*//", "/", originalcmd)

    return [target, cmd]

def connect(target, filelog, login, sshoptions, originalcmd):
    """ Simply launch the scp connection """

    # The final command should be like
    # ssh -q -t login@target scp -t /path/to/target
    # or to receive
    # ssh -q -t login@target scp -f /path/to/target /local/path/
    # and ssh/scp do magic after that
    cmd = re.sub("scp -(.) ", "ssh -q -t " + login + "@" + target +
                 " scp -\\1 ", originalcmd )
    os.system(cmd)

    # Print the command on a logfile
    filelog = open(filelog + "-scp.log", "a")
    filelog.write("Original command: " + originalcmd + " - " + "cmd: " + cmd)
    filelog.close()

<commit_msg>Raphux \ pro... For real!<commit_after># -*-coding:Utf-8 -*-

"""Contains functions to establish scp connections"""

# Compatibility 2.7-3.4
from __future__ import absolute_import
from __future__ import unicode_literals

import os, re

def parse(originalcmd):
    """Parse the original scp command written by the user.
       Obtain the target, create the command to pass"""
    # We wait a command like :
    # scp [option] /path/to/my/file user@bastion:targetname//path/on/destination
    # or
    # scp [option] user@bastion:targetname//path/on/destination/file /local/path
    # and we should obtain the target name a line like
    # scp -t /path/on/destination/file
    # scp -f /path/on/destination/file /local/path
    target = re.findall("([^\s]*)//", originalcmd)
    if target:
        target = target[0]

    cmd = re.sub("[^\s]*//", "/", originalcmd)

    return [target, cmd]

def connect(target, filelog, login, sshoptions, originalcmd):
    """ Simply launch the scp connection """

    # The final command should be like
    # ssh -q -t login@target scp -t /path/to/target
    # or to receive
    # ssh -q -t login@target scp -f /path/to/target /local/path/
    # and ssh/scp do magic after that
    cmd = re.sub("scp -(.) ", "ssh -q -t " + login + "@" + target +
                 " scp -\\1 ", originalcmd )
    os.system(cmd)

    # Print the command on a logfile
    filelog = open(filelog + "-scp.log", "a")
    filelog.write("Original command: " + originalcmd + " - " + "cmd: " + cmd)
    filelog.close()

<|endoftext|>