<commit_before>import logging

import numpy as np
import pandas as pd
from scipy import interpolate

from tardis.plasma.properties.base import ProcessingPlasmaProperty
from tardis.plasma.exceptions import PlasmaIonizationError

logger = logging.getLogger(__name__)

__all__ = ['PhiSahaNebular', 'PhiSahaLTE', 'RadiationFieldCorrection',
           'IonNumberDensity']

class PhiSahaLTE(ProcessingPlasmaProperty):
    """
    Outputs:
    general_phi : Pandas DataFrame
        Used as basis for PhiSahaLTE and PhiSahaNebular. Identical output to
        PhiSahaLTE. Separate property required as PhiSahaNebular is based
        on PhiSahaLTE, but the code cannot deal with the inclusion of two
        properties that generate a property called 'phi'.
    """
    outputs = ('phi',)
    latex_name = ('\\Phi',)
    latex_formula = ('\\dfrac{2Z_{i,j+1}}{Z_{i,j}}\\Big(\
                     \\dfrac{2\\pi m_{e}/\\beta_{\\textrm{rad}}}{h^2}\
                     \\Big)^{3/2}e^{\\dfrac{-\\chi_{i,j}}{kT_{\
                     \\textrm{rad}}}}',)

    @staticmethod
    def calculate(g_electron, beta_rad, partition_function, ionization_data):
        def calculate_phis(group):
            return group[1:] / group[:-1].values
        phis = partition_function.groupby(level='atomic_number').apply(
            calculate_phis)
        phis = pd.DataFrame(phis.values, index=phis.index.droplevel(0))
        phi_coefficient = (2 * g_electron * np.exp(np.outer(
            ionization_data.ionization_energy.ix[phis.index].values,
            -beta_rad)))
        return phis * phi_coefficient

class PhiSahaNebular(ProcessingPlasmaProperty):
    """
    Outputs:
    phi_saha_nebular: Pandas DataFrame
        The ionization equilibrium as calculated using a modified version of
        the Saha equation that accounts for dilution of the radiation field.
    """
    outputs = ('phi',)
    latex_name = ('\\Phi',)
    latex_formula = ('W(\\delta\\zeta_{i,j}+W(1-\\zeta_{i,j}))\\left(\
                     \\dfrac{T_{\\textrm{electron}}}{T_{\\textrm{rad}}}\
                     \\right)^{1/2}',)

    def calculate(self, t_rad, w, zeta_data, t_electrons, delta,
            g_electron, beta_rad, partition_function, ionization_data):
        phi_lte = PhiSahaLTE.calculate(g_electron, beta_rad,
            partition_function, ionization_data)
        zeta = self.get_zeta_values(zeta_data, phi_lte, t_rad)
        phis = phi_lte * w * ((zeta * delta) + w * (1 - zeta)) * \
               (t_electrons/t_rad) ** .5
        return phis

    @staticmethod
    def get_zeta_values(zeta_data, general_phi, t_rad):
        try:
            zeta = interpolate.interp1d(zeta_data.columns.values, zeta_data.ix[
                general_phi.index].values)(t_rad)
            zeta = zeta.astype(float)
        except ValueError:
            raise ValueError('t_rads outside of zeta factor interpolation'
                             ' zeta_min={0:.2f} zeta_max={1:.2f} '
                             '- requested {2}'.format(
                zeta_data.columns.values.min(), zeta_data.columns.values.max(),
                t_rad))
        return zeta

class RadiationFieldCorrection(ProcessingPlasmaProperty):
    """
    Outputs:
    delta: Pandas DataFrame
        Calculates the radiation field correction (see Mazzali & Lucy, 1993) if
        not given as input in the config. file. The default chi_0_species is
        Ca II, which is good for type Ia supernovae. For type II supernovae,
        (1, 1) should be used.
    """
    outputs = ('delta',)
    latex_name = ('\\delta',)

    def __init__(self, plasma_parent, departure_coefficient=None):
        super(RadiationFieldCorrection, self).__init__(plasma_parent)
        self.departure_coefficient = departure_coefficient

    def calculate(self, w, ionization_data, beta_rad, t_electrons, t_rad,
        beta_electron, delta_input, chi_0):
        if delta_input is None:
            if self.departure_coefficient is None:
                departure_coefficient = 1. / w
            else:
                departure_coefficient = self.departure_coefficient
            radiation_field_correction = -np.ones((len(ionization_data), len(
                beta_rad)))
            less_than_chi_0 = (
                ionization_data.ionization_energy < chi_0).values
            factor_a = (t_electrons / (departure_coefficient * w * t_rad))
            radiation_field_correction[~less_than_chi_0] = factor_a * \
                np.exp(np.outer(ionization_data.ionization_energy.values[
                ~less_than_chi_0], beta_rad - beta_electron))
            radiation_field_correction[less_than_chi_0] = 1 - np.exp(np.outer(
                ionization_data.ionization_energy.values[less_than_chi_0],
                beta_rad) - beta_rad * chi_0)
            radiation_field_correction[less_than_chi_0] += factor_a * np.exp(
                np.outer(ionization_data.ionization_energy.values[
                less_than_chi_0],beta_rad) - chi_0 * beta_electron)
        else:
            radiation_field_correction = np.ones((len(ionization_data),
                len(beta_rad))) * delta_input
        delta = pd.DataFrame(radiation_field_correction,
            columns=np.arange(len(t_rad)), index=ionization_data.index)
        return delta

class IonNumberDensity(ProcessingPlasmaProperty):
    """
    Outputs:
    ion_number_density: Pandas DataFrame
    electron_densities: Numpy Array
        Convergence process to find the correct solution. A trial value for
        the electron density is initiated in a particular zone. The ion
        number densities are then calculated using the Saha equation. The
        electron density is then re-calculated by using the ion number
        densities to sum over the number of free electrons. If the two values
        for the electron densities are not similar to within the threshold
        value, a new guess for the value of the electron density is chosen
        and the process is repeated.
    """
    outputs = ('ion_number_density', 'electron_densities')
    latex_name = ('N_{i,j}','n_{e}',)

    def __init__(self, plasma_parent, ion_zero_threshold=1e-20):
        super(IonNumberDensity, self).__init__(plasma_parent)
        self.ion_zero_threshold = ion_zero_threshold

    def calculate_with_n_electron(self, phi, partition_function,
                                  number_density, n_electron):
        ion_populations = pd.DataFrame(data=0.0,
            index=partition_function.index.copy(),
            columns=partition_function.columns.copy(), dtype=np.float64)

        for atomic_number, groups in phi.groupby(level='atomic_number'):
            current_phis = (groups / n_electron).replace(np.nan, 0.0).values
            phis_product = np.cumproduct(current_phis, axis=0)
            neutral_atom_density = (number_density.ix[atomic_number] /
                                    (1 + np.sum(phis_product, axis=0)))
            ion_populations.ix[atomic_number, 0] = (
                neutral_atom_density.values)
            ion_populations.ix[atomic_number].values[1:] = (
                neutral_atom_density.values * phis_product)
            ion_populations[ion_populations < self.ion_zero_threshold] = 0.0
        return ion_populations

    def calculate(self, phi, partition_function, number_density):
        n_e_convergence_threshold = 0.05
        n_electron = number_density.sum(axis=0)
        n_electron_iterations = 0
        while True:
            ion_number_density = self.calculate_with_n_electron(
                phi, partition_function, number_density, n_electron)
            ion_numbers = ion_number_density.index.get_level_values(1).values
            ion_numbers = ion_numbers.reshape((ion_numbers.shape[0], 1))
            new_n_electron = (ion_number_density.values * ion_numbers).sum(
                axis=0)
            if np.any(np.isnan(new_n_electron)):
                raise PlasmaIonizationError('n_electron just turned "nan" -'
                                            ' aborting')
            n_electron_iterations += 1
            if n_electron_iterations > 100:
                logger.warn('n_electron iterations above 100 ({0}) -'
                            ' something is probably wrong'.format(
                    n_electron_iterations))
            if np.all(np.abs(new_n_electron - n_electron)
                              / n_electron < n_e_convergence_threshold):
                break
            n_electron = 0.5 * (new_n_electron + n_electron)
        return ion_number_density, n_electron
<commit_msg>Added Helium NLTE option to ion_number_density property.<commit_after>import logging

import numpy as np
import pandas as pd
from scipy import interpolate

from tardis.plasma.properties.base import ProcessingPlasmaProperty
from tardis.plasma.exceptions import PlasmaIonizationError

logger = logging.getLogger(__name__)

__all__ = ['PhiSahaNebular', 'PhiSahaLTE', 'RadiationFieldCorrection',
           'IonNumberDensity']

class PhiSahaLTE(ProcessingPlasmaProperty):
    """
    Outputs:
    general_phi : Pandas DataFrame
        Used as basis for PhiSahaLTE and PhiSahaNebular. Identical output to
        PhiSahaLTE. Separate property required as PhiSahaNebular is based
        on PhiSahaLTE, but the code cannot deal with the inclusion of two
        properties that generate a property called 'phi'.
    """
    outputs = ('phi',)
    latex_name = ('\\Phi',)
    latex_formula = ('\\dfrac{2Z_{i,j+1}}{Z_{i,j}}\\Big(\
                     \\dfrac{2\\pi m_{e}/\\beta_{\\textrm{rad}}}{h^2}\
                     \\Big)^{3/2}e^{\\dfrac{-\\chi_{i,j}}{kT_{\
                     \\textrm{rad}}}}',)

    @staticmethod
    def calculate(g_electron, beta_rad, partition_function, ionization_data):
        def calculate_phis(group):
            return group[1:] / group[:-1].values
        phis = partition_function.groupby(level='atomic_number').apply(
            calculate_phis)
        phis = pd.DataFrame(phis.values, index=phis.index.droplevel(0))
        phi_coefficient = (2 * g_electron * np.exp(np.outer(
            ionization_data.ionization_energy.ix[phis.index].values,
            -beta_rad)))
        return phis * phi_coefficient

class PhiSahaNebular(ProcessingPlasmaProperty):
    """
    Outputs:
    phi_saha_nebular: Pandas DataFrame
        The ionization equilibrium as calculated using a modified version of
        the Saha equation that accounts for dilution of the radiation field.
    """
    outputs = ('phi',)
    latex_name = ('\\Phi',)
    latex_formula = ('W(\\delta\\zeta_{i,j}+W(1-\\zeta_{i,j}))\\left(\
                     \\dfrac{T_{\\textrm{electron}}}{T_{\\textrm{rad}}}\
                     \\right)^{1/2}',)

    def calculate(self, t_rad, w, zeta_data, t_electrons, delta,
            g_electron, beta_rad, partition_function, ionization_data):
        phi_lte = PhiSahaLTE.calculate(g_electron, beta_rad,
            partition_function, ionization_data)
        zeta = self.get_zeta_values(zeta_data, phi_lte, t_rad)
        phis = phi_lte * w * ((zeta * delta) + w * (1 - zeta)) * \
               (t_electrons/t_rad) ** .5
        return phis

    @staticmethod
    def get_zeta_values(zeta_data, general_phi, t_rad):
        try:
            zeta = interpolate.interp1d(zeta_data.columns.values, zeta_data.ix[
                general_phi.index].values)(t_rad)
            zeta = zeta.astype(float)
        except ValueError:
            raise ValueError('t_rads outside of zeta factor interpolation'
                             ' zeta_min={0:.2f} zeta_max={1:.2f} '
                             '- requested {2}'.format(
                zeta_data.columns.values.min(), zeta_data.columns.values.max(),
                t_rad))
        return zeta

class RadiationFieldCorrection(ProcessingPlasmaProperty):
    """
    Outputs:
    delta: Pandas DataFrame
        Calculates the radiation field correction (see Mazzali & Lucy, 1993) if
        not given as input in the config. file. The default chi_0_species is
        Ca II, which is good for type Ia supernovae. For type II supernovae,
        (1, 1) should be used.
    """
    outputs = ('delta',)
    latex_name = ('\\delta',)

    def __init__(self, plasma_parent, departure_coefficient=None):
        super(RadiationFieldCorrection, self).__init__(plasma_parent)
        self.departure_coefficient = departure_coefficient

    def calculate(self, w, ionization_data, beta_rad, t_electrons, t_rad,
        beta_electron, delta_input, chi_0):
        if delta_input is None:
            if self.departure_coefficient is None:
                departure_coefficient = 1. / w
            else:
                departure_coefficient = self.departure_coefficient
            radiation_field_correction = -np.ones((len(ionization_data), len(
                beta_rad)))
            less_than_chi_0 = (
                ionization_data.ionization_energy < chi_0).values
            factor_a = (t_electrons / (departure_coefficient * w * t_rad))
            radiation_field_correction[~less_than_chi_0] = factor_a * \
                np.exp(np.outer(ionization_data.ionization_energy.values[
                ~less_than_chi_0], beta_rad - beta_electron))
            radiation_field_correction[less_than_chi_0] = 1 - np.exp(np.outer(
                ionization_data.ionization_energy.values[less_than_chi_0],
                beta_rad) - beta_rad * chi_0)
            radiation_field_correction[less_than_chi_0] += factor_a * np.exp(
                np.outer(ionization_data.ionization_energy.values[
                less_than_chi_0],beta_rad) - chi_0 * beta_electron)
        else:
            radiation_field_correction = np.ones((len(ionization_data),
                len(beta_rad))) * delta_input
        delta = pd.DataFrame(radiation_field_correction,
            columns=np.arange(len(t_rad)), index=ionization_data.index)
        return delta

class IonNumberDensity(ProcessingPlasmaProperty):
    """
    Outputs:
    ion_number_density: Pandas DataFrame
    electron_densities: Numpy Array
        Convergence process to find the correct solution. A trial value for
        the electron density is initiated in a particular zone. The ion
        number densities are then calculated using the Saha equation. The
        electron density is then re-calculated by using the ion number
        densities to sum over the number of free electrons. If the two values
        for the electron densities are not similar to within the threshold
        value, a new guess for the value of the electron density is chosen
        and the process is repeated.
    """
    outputs = ('ion_number_density', 'electron_densities')
    latex_name = ('N_{i,j}','n_{e}',)

    def __init__(self, plasma_parent, ion_zero_threshold=1e-20):
        super(IonNumberDensity, self).__init__(plasma_parent)
        self.ion_zero_threshold = ion_zero_threshold

    def update_helium_nlte(self, ion_number_density, number_density):
        ion_number_density.ix[2].ix[0] = 0.0
        ion_number_density.ix[2].ix[2] = 0.0
        ion_number_density.ix[2].ix[1].update(number_density.ix[2])
        return ion_number_density

    def calculate_with_n_electron(self, phi, partition_function,
                                  number_density, n_electron):
        ion_populations = pd.DataFrame(data=0.0,
            index=partition_function.index.copy(),
            columns=partition_function.columns.copy(), dtype=np.float64)

        for atomic_number, groups in phi.groupby(level='atomic_number'):
            current_phis = (groups / n_electron).replace(np.nan, 0.0).values
            phis_product = np.cumproduct(current_phis, axis=0)
            neutral_atom_density = (number_density.ix[atomic_number] /
                                    (1 + np.sum(phis_product, axis=0)))
            ion_populations.ix[atomic_number, 0] = (
                neutral_atom_density.values)
            ion_populations.ix[atomic_number].values[1:] = (
                neutral_atom_density.values * phis_product)
            ion_populations[ion_populations < self.ion_zero_threshold] = 0.0
        return ion_populations

    def calculate(self, phi, partition_function, number_density):
        n_e_convergence_threshold = 0.05
        n_electron = number_density.sum(axis=0)
        n_electron_iterations = 0
        while True:
            ion_number_density = self.calculate_with_n_electron(
                phi, partition_function, number_density, n_electron)
            if hasattr(self.plasma_parent, 'plasma_properties_dict'):
                if 'HeliumNLTE' in \
                    self.plasma_parent.plasma_properties_dict.keys():
                    ion_number_density = \
                        self.update_helium_nlte(ion_number_density,
                        number_density)
            ion_numbers = ion_number_density.index.get_level_values(1).values
            ion_numbers = ion_numbers.reshape((ion_numbers.shape[0], 1))
            new_n_electron = (ion_number_density.values * ion_numbers).sum(
                axis=0)
            if np.any(np.isnan(new_n_electron)):
                raise PlasmaIonizationError('n_electron just turned "nan" -'
                                            ' aborting')
            n_electron_iterations += 1
            if n_electron_iterations > 100:
                logger.warn('n_electron iterations above 100 ({0}) -'
                            ' something is probably wrong'.format(
                    n_electron_iterations))
            if np.all(np.abs(new_n_electron - n_electron)
                              / n_electron < n_e_convergence_threshold):
                break
            n_electron = 0.5 * (new_n_electron + n_electron)
        return ion_number_density, n_electron
<|endoftext|>


<commit_before>import json
import os
import requests
import time
from slackclient import SlackClient

#In Memory Storage for now
CARDS = None

def populate_cards():
    global CARDS
    r = requests.get('https://api.fiveringsdb.com/cards')
    if r.status_code == 200:
        CARDS = r.json()['records']
    else:
        file_path = os.path.join(os.path.dirname(__file__), 'cards.json')
        with open(file_path) as f:
            CARDS = json.load(f)['records']

def get_matching_card(name):
    global CARDS
    all_cards = [c for c in CARDS if name.lower() in c['name_canonical'].lower()]
    first_card = all_cards[0] if len(all_cards) else None
    return first_card

def slackify_text(text):
    text = text.replace('', '*')
    text = text.replace('', '*')
    text = text.replace('', '*_')
    text = text.replace('', '_*')
    text = text.replace('', '_')
    text = text.replace('', '_')
    text = text.replace('[', ':')
    text = text.replace(']', ':')
    return text

def make_key_value(key, value):
    if key is not None and key is not 'None' and value is not None and value is not 'None' and value != '':
        return '{}: {}\n'.format(key, value)
    else:
        return ''

def get_field(card, field, name = None, do_title = False):
    value = card.get(field)
    if value is None:
        return ''
    value = str(value).encode('utf-8')
    if do_title:
        value = value.title()
    name = name if name else field
    return make_key_value(name.title(), value)

def get_traits(card):
    traits = ", ".join(map(str,card.get('traits', []))).title().encode('utf-8')
    return make_key_value('Traits', traits)

def get_text(card):
    text = card.get('text')
    if text is None:
        return ''
    text = slackify_text(text.encode('utf-8'))
    return make_key_value('Text', text)

def get_image(card):
    try:
        image = str([c['image_url'] for c in card['pack_cards'] if 'image_url' in c][0])
        return make_key_value('Image', image)
    except:
        return ''

def get_pack(card):
    try:
        pack = str([c['pack']['id'] for c in card['pack_cards'] if 'id' in c['pack']][0])
        return make_key_value('Pack', pack.title())
    except:
        return ''

def pprint_card(card):
    pprinted = ''
    pprinted += get_field(card, 'name')
    pprinted += get_pack(card)
    pprinted += get_field(card, 'type', do_title = True)
    pprinted += get_field(card, 'side', name = 'deck', do_title = True)
    pprinted += '\n'
    pprinted += get_field(card, 'cost')
    pprinted += get_field(card, 'military')
    pprinted += get_field(card, 'military_bonus', 'military')
    pprinted += get_field(card, 'political')
    pprinted += get_field(card, 'political_bonus', 'political')
    pprinted += get_field(card, 'glory')
    pprinted += '\n'
    pprinted += get_traits(card)
    pprinted += '\n'
    pprinted += get_text(card)
    pprinted += '\n'
    pprinted += get_image(card)
    return pprinted.replace('\n\n\n', '\n\n')

if __name__ == '__main__':
    trigger = "!card"
    offset = len(trigger) + 1
    populate_cards()
    slack_token = os.environ.get('SLACK_BOT_TOKEN')
    sc = SlackClient(slack_token)
    if sc.rtm_connect():
        while True:
            msgs = sc.rtm_read()
            for msg in msgs:
                if msg.get('text') is not None and '!card' in msg.get('text'):
                    txt = msg['text']
                    name = txt[txt.find(trigger)+offset:]
                    card = get_matching_card(name)
                    response = 'Card not found'
                    if card is not None:
                        response = pprint_card(card)
                        print response
                    sc.api_call(
                        'chat.postMessage',
                        channel=msg.get('channel'),
                        text=response
                    )
            time.sleep(1)
    else:
        print "Connection failed"
<commit_msg>move to attachments instead of messages<commit_after>import json
import os
import requests
import time
from slackclient import SlackClient

#In Memory Storage for now
CARDS = None

colors = {
        'crab': "#001c94",
        'crane': "#0089de",
        'dragon': "#00a472",
        'lion': "#cb9d00",
        'phoenix': "#c16400",
        'scorpion': "#a61600",
        'unicorn': "#780098",
        }

def populate_cards():
    global CARDS
    r = requests.get('https://api.fiveringsdb.com/cards')
    if r.status_code == 200:
        CARDS = r.json()['records']
    else:
        file_path = os.path.join(os.path.dirname(__file__), 'cards.json')
        with open(file_path) as f:
            CARDS = json.load(f)['records']

def get_matching_card(name):
    global CARDS
    all_cards = [c for c in CARDS if name.lower() in c['name_canonical'].lower()]
    first_card = all_cards[0] if len(all_cards) else None
    return first_card

def slackify_text(text):
    text = text.replace('', '*')
    text = text.replace('', '*')
    text = text.replace('', '*_')
    text = text.replace('', '_*')
    text = text.replace('', '_')
    text = text.replace('', '_')
    text = text.replace('[', ':')
    text = text.replace(']', ':')
    return text

def make_key_value(key, value):
    if key is not None and key is not 'None' and value is not None and value is not 'None' and value != '':
        return '{}: {}\n'.format(key, value)
    else:
        return ''

def get_field(card, field, do_title = False):
    value = card.get(field)
    if value is None:
        return ''
    value = str(value).encode('utf-8')
    if do_title:
        value = value.title()
    return value

def get_traits(card):
    traits = ", ".join(map(str,card.get('traits', []))).title().encode('utf-8')
    return traits

def get_text(card):
    text = card.get('text')
    if text is None:
        return ''
    text = slackify_text(text.encode('utf-8'))
    return text

def get_image(card):
    try:
        image = str([c['image_url'] for c in card['pack_cards'] if 'image_url' in c][0])
        return image
    except:
        return ''

def get_pack(card):
    try:
        pack = str([c['pack']['id'] for c in card['pack_cards'] if 'id' in c['pack']][0])
        return pack.title()
    except:
        return ''

def get_number(card):
    try:
        pack = str([c['position'] for c in card['pack_cards'] if 'position' in c][0])
        return pack.title()
    except:
        return ''

def get_color(card):
    clan = card.get('clan')
    return colors.get(clan, '#3c3c3c')

def pprint_card(card):
    pprinted = ''
    pprinted += make_key_value('Name', get_field(card, 'name'))
    pprinted += make_key_value('Pack', get_field(card, 'pack'))
    pprinted += make_key_value('Type', get_field(card, 'type', do_title = True))
    pprinted += make_key_value('Deck', get_field(card, 'side', do_title = True))
    pprinted += '\n'
    pprinted += make_key_value('Cost', get_field(card, 'cost'))
    pprinted += make_key_value('Military', get_field(card, 'military'))
    pprinted += make_key_value('Military', get_field(card, 'military_bonus'))
    pprinted += make_key_value('Political', get_field(card, 'political'))
    pprinted += make_key_value('Political', get_field(card, 'political_bonus'))
    pprinted += make_key_value('Glory', get_field(card, 'glory'))
    pprinted += '\n'
    pprinted += make_key_value('Traits', get_traits(card))
    pprinted += '\n'
    pprinted += make_key_value('Text', get_text(card))
    pprinted += '\n'
    pprinted += make_key_value('Image', get_image(card))
    return pprinted.replace('\n\n\n', '\n\n')

def make_title_value(card, key, name = None, value = None, short = True):
    name = name if name else key.title()
    value = value if value else get_field(card, key)
    if value is not None and value != 'None' and value != '':
        return {
                "title": name,
                "value": value,
                "short": short
                }
    else:
        return None

def make_attachment(card):
    fields = ['cost', ('military', 'military_bonus'), 'glory',
            ('political', 'political_bonus'), 'influence']
    attachment_fields = []
    for field in fields:
        if isinstance(field, tuple):
            k,v = field
            res = get_field(card, k)
            if res is not None and res != 'None' and res != '':
                attachment_fields.append(make_title_value(card, k))
            else:
                attachment_fields.append(make_title_value(card, v, name = k.title()))
        else:
            attachment_fields.append(make_title_value(card, field))
    attachment_fields.append(make_title_value(card, 'traits', name = 'Traits', value = get_traits(card)))
    attachment_fields.append(make_title_value(card, 'text', name = 'Text', value = get_text(card), short = False))
    attachment_fields = [f for f in attachment_fields if f is not None]
    return {
            "fallback": get_field(card, 'name'),
            "color": get_color(card),
            "author_name": "{clan} - {side} - {card_type} [{pack} {number}]".format(
                clan = get_field(card, 'clan', do_title = True),
                side = get_field(card, 'side', do_title = True),
                card_type = get_field(card, 'type', do_title = True),
                pack = get_pack(card),
                number = get_number(card)
                ),
            "title": get_field(card, 'name'),
            "title_link": "https://fiveringsdb.com/card/{}".format(
                get_field(card, 'name_canonical').replace(' ', '-')
                ),
            "fields": attachment_fields,
            "image_url": get_image(card)
            }

if __name__ == '__main__':
    trigger = "!card"
    offset = len(trigger) + 1
    populate_cards()
    slack_token = os.environ.get('SLACK_BOT_TOKEN')
    sc = SlackClient(slack_token)
    if sc.rtm_connect():
        while True:
            msgs = sc.rtm_read()
            for msg in msgs:
                if msg.get('text') is not None and '!card' in msg.get('text'):
                    txt = msg['text']
                    name = txt[txt.find(trigger)+offset:]
                    card = get_matching_card(name)
                    response = 'Card not found'
                    if card is not None:
                        response = make_attachment(card)
                        print response
                    sc.api_call(
                        'chat.postMessage',
                        channel=msg.get('channel'),
                        attachments=[response]
                    )
            time.sleep(1)
    else:
        print "Connection failed"
<|endoftext|>


<commit_before>
from teafacto.util import argprun
from collections import OrderedDict
import numpy as np, pickle
from teafacto.blocks.seqproc import SimpleSeq2Idx

def readdata(p):
    x = pickle.load(open(p))
    def preprocessforreldet(x, numents):
        goldmat = x[1]
        return x[0], goldmat[:, 1] - numents
    entdic = x["entdic"]
    numents = x["numents"]
    newdic = {}
    for k, v in entdic.items():
        if v >= numents:
            newdic[k] = v - numents
    train = preprocessforreldet(x["train"], numents)
    valid = preprocessforreldet(x["valid"], numents)
    test  = preprocessforreldet(x["test"],  numents)
    return train, valid, test, x["worddic"], newdic

def evaluate(pred, gold):
    return np.sum(gold == pred) * 100. / gold.shape[0]

def run(
        epochs=10,
        numbats=100,
        numsam=10000,
        lr=0.1,
        wdicp="../../../data/mfqa/clean/reldet/mfqa.reldet.worddic.sample.big",
        rdicp="../../../data/mfqa/clean/reldet/mfqa.reldet.reldic.sample.big",
        datap="../../../data/simplequestions/datamat.word.pkl",
        embdim=100,
        innerdim=200,
        wreg=0.00005,
        bidir=False,
        keepmincount=5,
        ):
    #wdic = readdic(wdicp)
    #rdic = readdic(rdicp)

    (traindata, traingold), (validdata, validgold), (testdata, testgold), worddic, entdic\
        = readdata(datap)

    print traindata.shape, testdata.shape

    numwords = max(worddic.values()) - min(worddic.values())
    numrels = max(entdic.values()) - min(entdic.values())
    print numwords, numrels

    if bidir:
        innerdim /= 2

    m = SimpleSeq2Idx(
        indim=numwords,
        outdim=numrels,
        inpembdim=embdim,
        innerdim=innerdim,
        maskid=-1,
        bidir=bidir,
    )

    m = m.train([traindata], traingold).adagrad(lr=lr).l2(wreg).grad_total_norm(1.0).cross_entropy()\
        .validate_on([validdata], validgold).accuracy().cross_entropy().takebest()\
        .train(numbats=numbats, epochs=epochs)

    pred = m.predict(testdata)
    print pred.shape
    evalres = evaluate(np.argmax(pred, axis=1), testgold)
    print str(evalres) + "%"

if __name__ == "__main__":
    argprun(run)<commit_msg>reldet for simplequestions<commit_after>
from teafacto.util import argprun
from collections import OrderedDict
import numpy as np, pickle
from teafacto.blocks.seqproc import SimpleSeq2Idx

def readdata(p):
    x = pickle.load(open(p))
    def preprocessforreldet(x, numents):
        goldmat = x[1]
        return x[0], goldmat[:, 1] - numents
    entdic = x["entdic"]
    numents = x["numents"]
    newdic = {}
    for k, v in entdic.items():
        if v >= numents:
            newdic[k] = v - numents
    train = preprocessforreldet(x["train"], numents)
    valid = preprocessforreldet(x["valid"], numents)
    test  = preprocessforreldet(x["test"],  numents)
    return train, valid, test, x["worddic"], newdic

def evaluate(pred, gold):
    return np.sum(gold == pred) * 100. / gold.shape[0]

def run(
        epochs=10,
        numbats=100,
        numsam=10000,
        lr=0.1,
        wdicp="../../../data/mfqa/clean/reldet/mfqa.reldet.worddic.sample.big",
        rdicp="../../../data/mfqa/clean/reldet/mfqa.reldet.reldic.sample.big",
        datap="../../../data/simplequestions/datamat.word.pkl",
        embdim=100,
        innerdim=200,
        wreg=0.00005,
        bidir=False,
        keepmincount=5,
        ):
    #wdic = readdic(wdicp)
    #rdic = readdic(rdicp)

    (traindata, traingold), (validdata, validgold), (testdata, testgold), worddic, entdic\
        = readdata(datap)

    print traindata.shape, testdata.shape

    numwords = max(worddic.values()) + 1
    numrels = max(entdic.values()) + 1
    print numwords, numrels

    if bidir:
        innerdim /= 2

    m = SimpleSeq2Idx(
        indim=numwords,
        outdim=numrels,
        inpembdim=embdim,
        innerdim=innerdim,
        maskid=-1,
        bidir=bidir,
    )

    m = m.train([traindata], traingold).adagrad(lr=lr).l2(wreg).grad_total_norm(1.0).cross_entropy()\
        .validate_on([validdata], validgold).accuracy().cross_entropy().takebest()\
        .train(numbats=numbats, epochs=epochs)

    pred = m.predict(testdata)
    print pred.shape
    evalres = evaluate(np.argmax(pred, axis=1), testgold)
    print str(evalres) + "%"

if __name__ == "__main__":
    argprun(run)<|endoftext|>


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

"""
6. ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:
(you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
"""

class Solution(object):
    """
    Time: O(n)
    Space: O(n^2)
    """

    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """

        mat = [[] for i in range(numRows)]
        idx = 0
        stop = False
        while idx < len(s):
            for i in range(numRows):
                mat[i].append(s[idx])
                idx += 1
                if idx >= len(s):
                    stop = True
                    break

            if stop == True:
                break

            for j in range(numRows - 2):
                target_row = numRows - 2 - j
                for k in range(numRows):
                    if k == target_row:
                        mat[k].append(s[idx])
                        idx += 1
                        if idx >= len(s):
                            stop = True
                            break
                    else:
                        mat[k].append(' ')

                if stop == True:
                    break

            if stop == True:
                break

        s = ''
        for i in range(numRows):
            for c in mat[i]:
                if c != ' ':
                    s += c

        return s

class Solution2(object):
    """
    Use the regularization
    Time: O(n)
    Space: O(n)
    """

    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """

        if len(s) <= numRows or numRows == 1:
            return s

        res = []

        # first row
        idx = 0
        gap = numRows * 2 - 2
        while idx < len(s):
            res.append(s[idx])
            idx += gap

        # middle row
        for i in range(1, numRows - 1):
            idx = i

            gap1 = numRows * 2 - 2 - i * 2
            gap2 = numRows * 2 - 2 - gap1

            while idx < len(s):
                res.append(s[idx])
                idx += gap1
                if idx < len(s):
                    res.append(s[idx])
                    idx += gap2

        # last row
        idx = numRows - 1
        while idx < len(s):
            res.append(s[idx])
            idx += gap

        return ''.join(res)

class Solution3(object):

    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """

        if numRows == 1 or len(s) <= numRows:
            return s

        cycle = 2 * (numRows - 1)
        res = []
        for i in xrange(numRows):
            idx = i
            step1 = cycle - 2 * i
            step2 = cycle - step1
            while idx < len(s):
                res.append(s[idx])
                if step1 == 0 or step2 == 0:
                    idx += cycle
                    continue

                idx += step1
                if idx < len(s):
                    res.append(s[idx])
                    idx += step2

        return ''.join(res)

if __name__ == '__main__':
    so = Solution3()
    print(so.convert('PAYPALISHIRING', 3))
    print(so.convert('ABCDE', 4))
    print(so.convert('A', 1))
<commit_msg>update<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-

"""
6. ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:
(you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
"""

class Solution(object):
    """
    Time: O(n)
    Space: O(n^2)
    """

    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """

        mat = [[] for i in range(numRows)]
        idx = 0
        stop = False
        while idx < len(s):
            for i in range(numRows):
                mat[i].append(s[idx])
                idx += 1
                if idx >= len(s):
                    stop = True
                    break

            if stop == True:
                break

            for j in range(numRows - 2):
                target_row = numRows - 2 - j
                for k in range(numRows):
                    if k == target_row:
                        mat[k].append(s[idx])
                        idx += 1
                        if idx >= len(s):
                            stop = True
                            break
                    else:
                        mat[k].append(' ')

                if stop == True:
                    break

            if stop == True:
                break

        s = ''
        for i in range(numRows):
            for c in mat[i]:
                if c != ' ':
                    s += c

        return s

class Solution2(object):
    """
    Use the regularization
    Time: O(n)
    Space: O(n)
    """

    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """

        if len(s) <= numRows or numRows == 1:
            return s

        res = []

        # first row
        idx = 0
        gap = numRows * 2 - 2
        while idx < len(s):
            res.append(s[idx])
            idx += gap

        # middle row
        for i in range(1, numRows - 1):
            idx = i

            gap1 = numRows * 2 - 2 - i * 2
            gap2 = numRows * 2 - 2 - gap1

            while idx < len(s):
                res.append(s[idx])
                idx += gap1
                if idx < len(s):
                    res.append(s[idx])
                    idx += gap2

        # last row
        idx = numRows - 1
        while idx < len(s):
            res.append(s[idx])
            idx += gap

        return ''.join(res)

class Solution3(object):
    """
    This is the best solution
    """

    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """

        if numRows == 1 or len(s) <= numRows:
            return s

        cycle = 2 * (numRows - 1)
        res = []
        for i in xrange(numRows):
            idx = i
            step1 = cycle - 2 * i
            step2 = cycle - step1
            while idx < len(s):
                res.append(s[idx])
                if step1 == 0 or step2 == 0:
                    idx += cycle
                    continue

                idx += step1
                if idx < len(s):
                    res.append(s[idx])
                    idx += step2

        return ''.join(res)

if __name__ == '__main__':
    so = Solution3()
    print(so.convert('PAYPALISHIRING', 3))
    print(so.convert('ABCDE', 4))
    print(so.convert('A', 1))
<|endoftext|>


<commit_before>from django.conf.urls import patterns, url
from system.controllers import topView
from system.controllers import mypageView
from system.controllers import logView
from system.controllers import requestView
from system.controllers import borrowReturnPost, reservePost, postFinishView
from system.controllers import userRegister

urlpatterns = [
    url(r'^$', topView.topView, name='top'),
    url(r'^user_register/$', userRegister.userRegister,
        name='user_register'),
    url(r'^user_register/finish$', userRegister.finishUserRegisterView,
        name='finish_register'),
    url(r'^mypage/$', mypageView.mypageView),
    url(r'^log/$', logView.logView),
    url(r'^request/$', requestView.requestView, name='requestView'),
    url(r'^vote/$', requestView.vote, name='vote'),

    url(r'^manage/borrow/$', borrowReturnPost.borrowPost,
        name='manage-borrow'),
    url(r'^manage/return/$', borrowReturnPost.returnPost,
        name='manage-return'),

    url(r'^manage/reserve/$', reservePost.reservePost,
        name='manage-reserve'),
    url(r'^manage/cancel_reserve/$', reservePost.cancelPost,
        name='manage-cancel'),

    url(r'^manage/$', postFinishView.postFinishView, name='manage'),
]
<commit_msg>log, mypageにname付与<commit_after>from django.conf.urls import patterns, url
from system.controllers import topView
from system.controllers import mypageView
from system.controllers import logView
from system.controllers import requestView
from system.controllers import borrowReturnPost, reservePost, postFinishView
from system.controllers import userRegister

urlpatterns = [
    url(r'^$', topView.topView, name='top'),
    url(r'^user_register/$', userRegister.userRegister,
        name='user_register'),
    url(r'^user_register/finish$', userRegister.finishUserRegisterView,
        name='finish_register'),
    url(r'^mypage/$', mypageView.mypageView, name='mypage'),
    url(r'^log/$', logView.logView, name='log'),
    url(r'^request/$', requestView.requestView, name='requestView'),
    url(r'^vote/$', requestView.vote, name='vote'),

    url(r'^manage/borrow/$', borrowReturnPost.borrowPost,
        name='manage-borrow'),
    url(r'^manage/return/$', borrowReturnPost.returnPost,
        name='manage-return'),

    url(r'^manage/reserve/$', reservePost.reservePost,
        name='manage-reserve'),
    url(r'^manage/cancel_reserve/$', reservePost.cancelPost,
        name='manage-cancel'),

    url(r'^manage/$', postFinishView.postFinishView, name='manage'),
]
<|endoftext|>


<commit_before>import os.path
from struct import unpack

ISO = ('AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ',
       'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS',
       'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN',
       'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE',
       'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF',
       'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM',
       'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM',
       'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC',
       'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK',
       'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA',
       'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG',
       'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW',
       'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS',
       'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO',
       'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI',
       'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW', 'XA', 'YU', 'CS', 'AN', 'AA', 'EU', 'AP',
       )

def split_str_fix(s, l):
    """ Split a string into chunks of length l """
    return [s[i:i+l] for i in range(0, len(s), l)]

def tabgeo_bs(data_array, ip, step):
    u = {}
    u_prev = {}
    start = 0
    end = len(data_array) - 1

    while True:
        mid = (start + end) / 2

        if step:
            (u['offset'], u['ip'], u['cc_id']) = unpack(">IBB", b'\x00'+data_array[mid])
        else:
            (u['ip'], u['cc_id']) = unpack(">BB", b'\x00'+data_array[mid])
        if u['ip'] == ip:
            return u
        if end - start < 0:
            return u if ip > u['ip'] else u_prev
        if u['ip'] > ip:
            end = mid-1
        else:
            start = mid + 1

        u_prev = u

def getbyip(ip):
    ip_ = map(int, ip.split('.'))
    with open(os.path.join(os.path.dirname(__file__), 'data/tabgeo_country_v4.dat'), 'rb', 1) as fh:
        fh.seek((ip_[0]*256 + ip_[1])*4)
        index_bin = fh.read(4)
        (i_offset, i_length) = unpack(">IB", b'\x00'+index_bin)
        if i_offset == 16777215:
            return ISO[i_length]

        fh.seek(i_offset*5 + 262144)
        bin_data = fh.read((i_length+1)*5)
        d = tabgeo_bs(split_str_fix(bin_data, 5), ip_[2], True)
        if d['offset'] == 16777215:
            return ISO[d['cc_id']]

        if ip_[2] > d['ip']:
            ip_[3] = 255
        fh.seek(-((d['offset'] + 1 + d['cc_id']) * 2), 2)
        bin_data = fh.read((d['cc_id'] + 1) * 2)
        d = tabgeo_bs(split_str_fix(bin_data, 2), ip_[3], False)
        return ISO[d['cc_id']]

<commit_msg>Add support for python 3.<commit_after>import os.path
from struct import unpack

ISO = ('AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ',
       'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS',
       'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN',
       'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE',
       'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF',
       'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM',
       'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM',
       'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC',
       'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK',
       'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA',
       'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG',
       'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW',
       'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS',
       'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO',
       'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI',
       'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW', 'XA', 'YU', 'CS', 'AN', 'AA', 'EU', 'AP',
       )

def split_str_fix(s, l):
    """ Split a string into chunks of length l """
    return [s[i:i+l] for i in range(0, len(s), l)]

def tabgeo_bs(data_array, ip, step):
    u = {}
    u_prev = {}
    start = 0
    end = len(data_array) - 1

    while True:
        mid = (start + end) // 2

        if step:
            (u['offset'], u['ip'], u['cc_id']) = unpack(">IBB", b'\x00'+data_array[mid])
        else:
            (u['ip'], u['cc_id']) = unpack(">BB", b'\x00'+data_array[mid])
        if u['ip'] == ip:
            return u
        if end - start < 0:
            return u if ip > u['ip'] else u_prev
        if u['ip'] > ip:
            end = mid-1
        else:
            start = mid + 1

        u_prev = u

def getbyip(ip):
    ip_ = list(map(int, ip.split('.')))
    with open(os.path.join(os.path.dirname(__file__), 'data/tabgeo_country_v4.dat'), 'rb', 1) as fh:
        fh.seek((ip_[0]*256 + ip_[1])*4)
        index_bin = fh.read(4)
        (i_offset, i_length) = unpack(">IB", b'\x00'+index_bin)
        if i_offset == 16777215:
            return ISO[i_length]

        fh.seek(i_offset*5 + 262144)
        bin_data = fh.read((i_length+1)*5)
        d = tabgeo_bs(split_str_fix(bin_data, 5), ip_[2], True)
        if d['offset'] == 16777215:
            return ISO[d['cc_id']]

        if ip_[2] > d['ip']:
            ip_[3] = 255
        fh.seek(-((d['offset'] + 1 + d['cc_id']) * 2), 2)
        bin_data = fh.read((d['cc_id'] + 1) * 2)
        d = tabgeo_bs(split_str_fix(bin_data, 2), ip_[3], False)
        return ISO[d['cc_id']]

<|endoftext|>


<commit_before>import taco.bottle as bottle
import taco.settings
import taco.globals
import taco.constants
import taco.filesystem
import taco.commands
import urllib
import re,time
import os,uuid
import logging
import json
from collections import defaultdict

#static content
@bottle.route('/static/<filename:path>')
def send_file(filename):
  return bottle.static_file(filename, root=os.path.normpath(os.getcwd() + '/static/'))

#terminate taconet
@bottle.route('/shutitdown')
def taco_page():
  taco.globals.properexit(1,1)
  return

#template routes
@bottle.route('/')
def index():
  return bottle.template('templates/home.tpl')

@bottle.route('/<name>.taco')
def taco_page(name):
  return bottle.template('templates/'+str(name)+'.tpl')

@bottle.post('/api.post')
def index():
  try:
    api_call = bottle.request.json
  except:
    return "0"
  if not "action" in bottle.request.json: return "0"
  if not "data" in bottle.request.json: return "0"
  if bottle.request.json[u"action"] == u"apistatus":
    return json.dumps({"status":1})
  if bottle.request.json[u"action"] == u"threadstatus":
    output = {}
    output["threads"] = {}
    output["threads"]["clients"] = {}
    output["threads"]["server"] = {}

    output["threads"]["clients"]["alive"] = taco.globals.clients.is_alive()
    (output["threads"]["clients"]["status"],output["threads"]["clients"]["lastupdate"]) = taco.globals.clients.get_status()
    output["threads"]["clients"]["lastupdate"] = abs(time.time() - float(output["threads"]["clients"]["lastupdate"]))

    output["threads"]["server"]["alive"] = taco.globals.server.is_alive()
    (output["threads"]["server"]["status"],output["threads"]["server"]["lastupdate"]) = taco.globals.server.get_status()
    output["threads"]["server"]["lastupdate"] = abs(time.time() - float(output["threads"]["server"]["lastupdate"]))

    return json.dumps(output)

  if bottle.request.json[u"action"] == u"speed":
    with taco.globals.download_limiter_lock: down = taco.globals.download_limiter.get_rate()
    with taco.globals.upload_limiter_lock:   up   = taco.globals.upload_limiter.get_rate()
    return json.dumps([up,down])

  if bottle.request.json[u"action"] == u"downloadqadd":
    if type(bottle.request.json[u"data"]) == type({}):
      try:
        peer_uuid = bottle.request.json[u"data"][u"uuid"]
        sharedir = bottle.request.json[u"data"][u"sharedir"]
        filename = bottle.request.json[u"data"][u"filename"]
        filesize = int(bottle.request.json[u"data"][u"filesize"])
        filemod = float(bottle.request.json[u"data"][u"filemodtime"])
      except:
        return "-1"

      with taco.globals.download_q_lock:
        logging.debug("Adding File to Download Q:" + str((peer_uuid,sharedir,filename,filesize,filemod)))
        if not peer_uuid in taco.globals.download_q: taco.globals.download_q[peer_uuid] = []
        if (sharedir,filename,filesize,filemod) not in taco.globals.download_q[peer_uuid]:
          taco.globals.download_q[peer_uuid].append((sharedir,filename,filesize,filemod))
          return "1"
        return "2"

  if bottle.request.json[u"action"] == u"downloadqremove":
    if type(bottle.request.json[u"data"]) == type({}):
      try:
        peer_uuid = bottle.request.json[u"data"][u"uuid"]
        sharedir = bottle.request.json[u"data"][u"sharedir"]
        filename = bottle.request.json[u"data"][u"filename"]
        filesize = int(bottle.request.json[u"data"][u"filesize"])
        filemod = float(bottle.request.json[u"data"][u"filemodtime"])
      except:
        return "-1"

      with taco.globals.download_q_lock:
        logging.debug("Removing File to Download Q:" + str((peer_uuid,sharedir,filename,filesize,filemod)))
        if peer_uuid in taco.globals.download_q:
          while (sharedir,filename,filesize,filemod) in taco.globals.download_q[peer_uuid]:
            taco.globals.download_q[peer_uuid].remove((sharedir,filename,filesize,filemod))
          if len(taco.globals.download_q[peer_uuid]) == 0: del taco.globals.download_q[peer_uuid]
          return "1"
        return "2"

  if bottle.request.json[u"action"] == u"downloadqmove":
    if type(bottle.request.json[u"data"]) == type({}):
      try:
        peer_uuid = bottle.request.json[u"data"][u"uuid"]
        sharedir = bottle.request.json[u"data"][u"sharedir"]
        filename = bottle.request.json[u"data"][u"filename"]
        filesize = int(bottle.request.json[u"data"][u"filesize"])
        filemod = float(bottle.request.json[u"data"][u"filemodtime"])
        newloc =  int(bottle.request.json[u"data"][u"newloc"])
      except:
        return "-1"
    with taco.globals.download_q_lock:
      logging.debug("Moving File in Download Q:" + str((peer_uuid,sharedir,filename,filesize,filemod,newloc)))
      if peer_uuid in taco.globals.download_q:
        while (sharedir,filename,filesize,filemod) in taco.globals.download_q[peer_uuid]:
          taco.globals.download_q[peer_uuid].remove((sharedir,filename,filesize,filemod))
        taco.globals.download_q[peer_uuid].insert(min(newloc,len(taco.globals.download_q[peer_uuid])),((sharedir,filename,filesize,filemod)))
        return "1"
      return "2"

  if bottle.request.json[u"action"] == u"downloadqget":
    output = {}
    with taco.globals.settings_lock:
      local_copy_download_directory = os.path.normpath(taco.globals.settings["Download Location"])
      with taco.globals.download_q_lock:
        peerinfo = {}
        fileinfo = defaultdict(dict)
        for peer_uuid in taco.globals.settings["Peers"]:
          peerinfo[peer_uuid] = [taco.globals.settings["Peers"][peer_uuid]["nickname"],taco.globals.settings["Peers"][peer_uuid]["localnick"]]
        for peer_uuid in taco.globals.download_q:
          for (sharedir,filename,filesize,modtime) in taco.globals.download_q[peer_uuid]:
            filename_incomplete = os.path.normpath(local_copy_download_directory + u"/" + filename + taco.constants.FILESYSTEM_WORKINPROGRESS_SUFFIX)
            try:
              current_size = os.path.getsize(filename_incomplete)
            except:
              current_size = 0
            fileinfo[peer_uuid][filename] = current_size
        output = {"result":taco.globals.download_q,"peerinfo":peerinfo,"fileinfo":fileinfo}
    return json.dumps(output)
  if bottle.request.json[u"action"] == u"completedqclear":
    with taco.globals.completed_q_lock:
      taco.globals.completed_q = []
    return "1"

  if bottle.request.json[u"action"] == u"completedqget":
    output = {}
    with taco.globals.settings_lock:
      with taco.globals.completed_q_lock:
        peerinfo = {}
        for peer_uuid in taco.globals.settings["Peers"].keys():
          peerinfo[peer_uuid] = [taco.globals.settings["Peers"][peer_uuid]["nickname"],taco.globals.settings["Peers"][peer_uuid]["localnick"]]
        output = {"result":taco.globals.completed_q[::-1],"peerinfo":peerinfo}
    return json.dumps(output)

  if bottle.request.json[u"action"] == u"uploadqget":
    pass

  if bottle.request.json[u"action"] == u"browseresult":
    output = {}
    if type(bottle.request.json[u"data"]) == type({}):
      if u"sharedir" in bottle.request.json[u"data"] and u"uuid" in bottle.request.json[u"data"]:
        with taco.globals.share_listings_lock:
          if (bottle.request.json[u"data"][u"uuid"],bottle.request.json[u"data"][u"sharedir"]) in taco.globals.share_listings:
            output = {"result":taco.globals.share_listings[(bottle.request.json[u"data"][u"uuid"],bottle.request.json[u"data"][u"sharedir"])][1]}
    return json.dumps(output)

  if bottle.request.json[u"action"] == u"browse":
    if type(bottle.request.json[u"data"]) == type({}):
      if u"uuid" in bottle.request.json[u"data"] and u"sharedir" in bottle.request.json[u"data"]:
        peer_uuid = bottle.request.json[u"data"][u"uuid"]
        sharedir = bottle.request.json[u"data"][u"sharedir"]
        browse_result_uuid = uuid.uuid4().hex
        logging.critical("Getting Directory Listing from: " + peer_uuid + " for share: " + sharedir)
        request = taco.commands.Request_Share_Listing(peer_uuid,sharedir,browse_result_uuid)
        taco.globals.Add_To_Output_Queue(peer_uuid,request,2)
        return json.dumps({"sharedir":sharedir,"result":browse_result_uuid})

  if bottle.request.json[u"action"] == u"peerstatus":
    output = {}
    with taco.globals.settings_lock:
      for peer_uuid in taco.globals.settings["Peers"].keys():
        if taco.globals.settings["Peers"][peer_uuid]["enabled"]:
          incoming = taco.globals.server.get_client_last_request(peer_uuid)
          outgoing = taco.globals.clients.get_client_last_reply(peer_uuid)
          timediffinc = abs(time.time()-incoming)
          timediffout = abs(time.time()-outgoing)
          output[peer_uuid] = [incoming,outgoing,timediffinc,timediffout,taco.globals.settings["Peers"][peer_uuid]["nickname"],taco.globals.settings["Peers"][peer_uuid]["localnick"]]
    return json.dumps(output)

  if bottle.request.json[u"action"] == u"settingssave":
    if type(bottle.request.json[u"data"]) == type([]):
      if len(bottle.request.json[u"data"]) >= 0:
        with taco.globals.settings_lock:
          logging.info("API Access: SETTINGS -- Action: SAVE")
          for (keyname,value) in bottle.request.json[u"data"]:
            taco.globals.settings[keyname] = value
          taco.settings.Save_Settings(False)
          return "1"

  if bottle.request.json[u"action"] == u"sharesave":
    if type(bottle.request.json[u"data"]) == type([]):
      if len(bottle.request.json[u"data"]) >= 0:
        with taco.globals.settings_lock:
          logging.info("API Access: SHARE -- Action: SAVE")
          taco.globals.settings["Shares"] = []
          for (sharename,sharelocation) in bottle.request.json[u"data"]:
            taco.globals.settings["Shares"].append([sharename,sharelocation])
          taco.settings.Save_Settings(False)
          return "1"

  if bottle.request.json[u"action"] == u"getchat":
    output_chat = []
    with taco.globals.settings_lock:
      localuuid  = taco.globals.settings["Local UUID"]
      with taco.globals.chat_log_lock:
        for [puuid,thetime,msg] in taco.globals.chat_log:
          if puuid in taco.globals.settings["Peers"] and "nickname" in taco.globals.settings["Peers"][puuid]:
            nickname = taco.globals.settings["Peers"][puuid]["nickname"]
          elif taco.globals.settings["Local UUID"] == puuid:
            nickname = taco.globals.settings["Nickname"]
          else:
            nickname = puuid
          if puuid==localuuid:
            output_chat.append([0,nickname,puuid,thetime,msg])
          else:
            output_chat.append([1,nickname,puuid,thetime,msg])
    return json.dumps(output_chat)

  if bottle.request.json[u"action"] == u"sendchat":
    if type(bottle.request.json[u"data"]) == type(u""):
      if len(bottle.request.json[u"data"]) > 0:
        taco.commands.Request_Chat(bottle.request.json[u"data"])
        return "1"

  if bottle.request.json[u"action"] == u"chatuuid":
    with taco.globals.chat_uuid_lock:
      return json.dumps([taco.globals.chat_uuid])

  if bottle.request.json[u"action"] == u"peersave":
    if type(bottle.request.json[u"data"]) == type([]):
      if len(bottle.request.json[u"data"]) >= 0:
        with taco.globals.settings_lock:
          logging.info("API Access: PEER -- Action: SAVE")
          taco.globals.settings["Peers"] = {}
          for (hostname,port,localnick,peeruuid,clientpub,serverpub,dynamic,enabled) in bottle.request.json[u"data"]:
            taco.globals.settings["Peers"][unicode(peeruuid)] = {"hostname":hostname,"port": int(port),"localnick":localnick,"dynamic":int(dynamic),"enabled":int(enabled),"clientkey":clientpub,"serverkey":serverpub}
          taco.settings.Save_Settings(False)
        taco.globals.server.stop_running()
        taco.globals.clients.stop_running()
        taco.globals.server.join()
        taco.globals.clients.join()
        taco.globals.server = taco.server.TacoServer()
        taco.globals.clients = taco.clients.TacoClients()
        taco.globals.server.start()
        taco.globals.clients.start()
        return "1"

  return "-1"

@bottle.route('/browselocaldirs/')
@bottle.route('/browselocaldirs/<browse_path:path>')
def index(browse_path="/"):
  if browse_path=="": browse_path ="/"
  browse_path = "/" + browse_path
  browse_path = unicode(browse_path)
  contents = os.listdir(browse_path)
  final_contents = []
  for item in contents:
    try:
      if os.path.isdir(os.path.join(browse_path,item)): final_contents.append(item)
    except:
      continue
  final_contents.sort()
  return json.dumps(final_contents)

@bottle.route('/get/<what>')
def getData(what):
  output = ""
  logging.debug("Route -- Getting your: " + what)
  if what=="uuid":
    return uuid.uuid4().hex
  if what=="ip":
    data = urllib.urlopen("http://checkip.dyndns.org/").read()
    m = re.match(r'.*Current IP Address: (.*)</body>',data)
    if m:
      output = m.group(1)
  if what =="diskfree":
    with taco.globals.settings_lock:
      down_dir = taco.globals.settings["Download Location"]
    if os.path.isdir(down_dir):
      (free,total) = taco.filesystem.Get_Free_Space(down_dir)
      if free == 0 and total == 0:
        output = 0.0
      else:
        output = free/taco.constants.GB
  return str(output)
<commit_msg>got rid of old references to stop running<commit_after>import taco.bottle as bottle
import taco.settings
import taco.globals
import taco.constants
import taco.filesystem
import taco.commands
import urllib
import re,time
import os,uuid
import logging
import json
from collections import defaultdict

#static content
@bottle.route('/static/<filename:path>')
def send_file(filename):
  return bottle.static_file(filename, root=os.path.normpath(os.getcwd() + '/static/'))

#terminate taconet
@bottle.route('/shutitdown')
def taco_page():
  taco.globals.properexit(1,1)
  return

#template routes
@bottle.route('/')
def index():
  return bottle.template('templates/home.tpl')

@bottle.route('/<name>.taco')
def taco_page(name):
  return bottle.template('templates/'+str(name)+'.tpl')

@bottle.post('/api.post')
def index():
  try:
    api_call = bottle.request.json
  except:
    return "0"
  if not "action" in bottle.request.json: return "0"
  if not "data" in bottle.request.json: return "0"
  if bottle.request.json[u"action"] == u"apistatus":
    return json.dumps({"status":1})
  if bottle.request.json[u"action"] == u"threadstatus":
    output = {}
    output["threads"] = {}
    output["threads"]["clients"] = {}
    output["threads"]["server"] = {}

    output["threads"]["clients"]["alive"] = taco.globals.clients.is_alive()
    (output["threads"]["clients"]["status"],output["threads"]["clients"]["lastupdate"]) = taco.globals.clients.get_status()
    output["threads"]["clients"]["lastupdate"] = abs(time.time() - float(output["threads"]["clients"]["lastupdate"]))

    output["threads"]["server"]["alive"] = taco.globals.server.is_alive()
    (output["threads"]["server"]["status"],output["threads"]["server"]["lastupdate"]) = taco.globals.server.get_status()
    output["threads"]["server"]["lastupdate"] = abs(time.time() - float(output["threads"]["server"]["lastupdate"]))

    return json.dumps(output)

  if bottle.request.json[u"action"] == u"speed":
    with taco.globals.download_limiter_lock: down = taco.globals.download_limiter.get_rate()
    with taco.globals.upload_limiter_lock:   up   = taco.globals.upload_limiter.get_rate()
    return json.dumps([up,down])

  if bottle.request.json[u"action"] == u"downloadqadd":
    if type(bottle.request.json[u"data"]) == type({}):
      try:
        peer_uuid = bottle.request.json[u"data"][u"uuid"]
        sharedir = bottle.request.json[u"data"][u"sharedir"]
        filename = bottle.request.json[u"data"][u"filename"]
        filesize = int(bottle.request.json[u"data"][u"filesize"])
        filemod = float(bottle.request.json[u"data"][u"filemodtime"])
      except:
        return "-1"

      with taco.globals.download_q_lock:
        logging.debug("Adding File to Download Q:" + str((peer_uuid,sharedir,filename,filesize,filemod)))
        if not peer_uuid in taco.globals.download_q: taco.globals.download_q[peer_uuid] = []
        if (sharedir,filename,filesize,filemod) not in taco.globals.download_q[peer_uuid]:
          taco.globals.download_q[peer_uuid].append((sharedir,filename,filesize,filemod))
          return "1"
        return "2"

  if bottle.request.json[u"action"] == u"downloadqremove":
    if type(bottle.request.json[u"data"]) == type({}):
      try:
        peer_uuid = bottle.request.json[u"data"][u"uuid"]
        sharedir = bottle.request.json[u"data"][u"sharedir"]
        filename = bottle.request.json[u"data"][u"filename"]
        filesize = int(bottle.request.json[u"data"][u"filesize"])
        filemod = float(bottle.request.json[u"data"][u"filemodtime"])
      except:
        return "-1"

      with taco.globals.download_q_lock:
        logging.debug("Removing File to Download Q:" + str((peer_uuid,sharedir,filename,filesize,filemod)))
        if peer_uuid in taco.globals.download_q:
          while (sharedir,filename,filesize,filemod) in taco.globals.download_q[peer_uuid]:
            taco.globals.download_q[peer_uuid].remove((sharedir,filename,filesize,filemod))
          if len(taco.globals.download_q[peer_uuid]) == 0: del taco.globals.download_q[peer_uuid]
          return "1"
        return "2"

  if bottle.request.json[u"action"] == u"downloadqmove":
    if type(bottle.request.json[u"data"]) == type({}):
      try:
        peer_uuid = bottle.request.json[u"data"][u"uuid"]
        sharedir = bottle.request.json[u"data"][u"sharedir"]
        filename = bottle.request.json[u"data"][u"filename"]
        filesize = int(bottle.request.json[u"data"][u"filesize"])
        filemod = float(bottle.request.json[u"data"][u"filemodtime"])
        newloc =  int(bottle.request.json[u"data"][u"newloc"])
      except:
        return "-1"
    with taco.globals.download_q_lock:
      logging.debug("Moving File in Download Q:" + str((peer_uuid,sharedir,filename,filesize,filemod,newloc)))
      if peer_uuid in taco.globals.download_q:
        while (sharedir,filename,filesize,filemod) in taco.globals.download_q[peer_uuid]:
          taco.globals.download_q[peer_uuid].remove((sharedir,filename,filesize,filemod))
        taco.globals.download_q[peer_uuid].insert(min(newloc,len(taco.globals.download_q[peer_uuid])),((sharedir,filename,filesize,filemod)))
        return "1"
      return "2"

  if bottle.request.json[u"action"] == u"downloadqget":
    output = {}
    with taco.globals.settings_lock:
      local_copy_download_directory = os.path.normpath(taco.globals.settings["Download Location"])
      with taco.globals.download_q_lock:
        peerinfo = {}
        fileinfo = defaultdict(dict)
        for peer_uuid in taco.globals.settings["Peers"]:
          peerinfo[peer_uuid] = [taco.globals.settings["Peers"][peer_uuid]["nickname"],taco.globals.settings["Peers"][peer_uuid]["localnick"]]
        for peer_uuid in taco.globals.download_q:
          for (sharedir,filename,filesize,modtime) in taco.globals.download_q[peer_uuid]:
            filename_incomplete = os.path.normpath(local_copy_download_directory + u"/" + filename + taco.constants.FILESYSTEM_WORKINPROGRESS_SUFFIX)
            try:
              current_size = os.path.getsize(filename_incomplete)
            except:
              current_size = 0
            fileinfo[peer_uuid][filename] = current_size
        output = {"result":taco.globals.download_q,"peerinfo":peerinfo,"fileinfo":fileinfo}
    return json.dumps(output)
  if bottle.request.json[u"action"] == u"completedqclear":
    with taco.globals.completed_q_lock:
      taco.globals.completed_q = []
    return "1"

  if bottle.request.json[u"action"] == u"completedqget":
    output = {}
    with taco.globals.settings_lock:
      with taco.globals.completed_q_lock:
        peerinfo = {}
        for peer_uuid in taco.globals.settings["Peers"].keys():
          peerinfo[peer_uuid] = [taco.globals.settings["Peers"][peer_uuid]["nickname"],taco.globals.settings["Peers"][peer_uuid]["localnick"]]
        output = {"result":taco.globals.completed_q[::-1],"peerinfo":peerinfo}
    return json.dumps(output)

  if bottle.request.json[u"action"] == u"uploadqget":
    pass

  if bottle.request.json[u"action"] == u"browseresult":
    output = {}
    if type(bottle.request.json[u"data"]) == type({}):
      if u"sharedir" in bottle.request.json[u"data"] and u"uuid" in bottle.request.json[u"data"]:
        with taco.globals.share_listings_lock:
          if (bottle.request.json[u"data"][u"uuid"],bottle.request.json[u"data"][u"sharedir"]) in taco.globals.share_listings:
            output = {"result":taco.globals.share_listings[(bottle.request.json[u"data"][u"uuid"],bottle.request.json[u"data"][u"sharedir"])][1]}
    return json.dumps(output)

  if bottle.request.json[u"action"] == u"browse":
    if type(bottle.request.json[u"data"]) == type({}):
      if u"uuid" in bottle.request.json[u"data"] and u"sharedir" in bottle.request.json[u"data"]:
        peer_uuid = bottle.request.json[u"data"][u"uuid"]
        sharedir = bottle.request.json[u"data"][u"sharedir"]
        browse_result_uuid = uuid.uuid4().hex
        logging.critical("Getting Directory Listing from: " + peer_uuid + " for share: " + sharedir)
        request = taco.commands.Request_Share_Listing(peer_uuid,sharedir,browse_result_uuid)
        taco.globals.Add_To_Output_Queue(peer_uuid,request,2)
        return json.dumps({"sharedir":sharedir,"result":browse_result_uuid})

  if bottle.request.json[u"action"] == u"peerstatus":
    output = {}
    with taco.globals.settings_lock:
      for peer_uuid in taco.globals.settings["Peers"].keys():
        if taco.globals.settings["Peers"][peer_uuid]["enabled"]:
          incoming = taco.globals.server.get_client_last_request(peer_uuid)
          outgoing = taco.globals.clients.get_client_last_reply(peer_uuid)
          timediffinc = abs(time.time()-incoming)
          timediffout = abs(time.time()-outgoing)
          output[peer_uuid] = [incoming,outgoing,timediffinc,timediffout,taco.globals.settings["Peers"][peer_uuid]["nickname"],taco.globals.settings["Peers"][peer_uuid]["localnick"]]
    return json.dumps(output)

  if bottle.request.json[u"action"] == u"settingssave":
    if type(bottle.request.json[u"data"]) == type([]):
      if len(bottle.request.json[u"data"]) >= 0:
        with taco.globals.settings_lock:
          logging.info("API Access: SETTINGS -- Action: SAVE")
          for (keyname,value) in bottle.request.json[u"data"]:
            taco.globals.settings[keyname] = value
          taco.settings.Save_Settings(False)
          return "1"

  if bottle.request.json[u"action"] == u"sharesave":
    if type(bottle.request.json[u"data"]) == type([]):
      if len(bottle.request.json[u"data"]) >= 0:
        with taco.globals.settings_lock:
          logging.info("API Access: SHARE -- Action: SAVE")
          taco.globals.settings["Shares"] = []
          for (sharename,sharelocation) in bottle.request.json[u"data"]:
            taco.globals.settings["Shares"].append([sharename,sharelocation])
          taco.settings.Save_Settings(False)
          return "1"

  if bottle.request.json[u"action"] == u"getchat":
    output_chat = []
    with taco.globals.settings_lock:
      localuuid  = taco.globals.settings["Local UUID"]
      with taco.globals.chat_log_lock:
        for [puuid,thetime,msg] in taco.globals.chat_log:
          if puuid in taco.globals.settings["Peers"] and "nickname" in taco.globals.settings["Peers"][puuid]:
            nickname = taco.globals.settings["Peers"][puuid]["nickname"]
          elif taco.globals.settings["Local UUID"] == puuid:
            nickname = taco.globals.settings["Nickname"]
          else:
            nickname = puuid
          if puuid==localuuid:
            output_chat.append([0,nickname,puuid,thetime,msg])
          else:
            output_chat.append([1,nickname,puuid,thetime,msg])
    return json.dumps(output_chat)

  if bottle.request.json[u"action"] == u"sendchat":
    if type(bottle.request.json[u"data"]) == type(u""):
      if len(bottle.request.json[u"data"]) > 0:
        taco.commands.Request_Chat(bottle.request.json[u"data"])
        return "1"

  if bottle.request.json[u"action"] == u"chatuuid":
    with taco.globals.chat_uuid_lock:
      return json.dumps([taco.globals.chat_uuid])

  if bottle.request.json[u"action"] == u"peersave":
    if type(bottle.request.json[u"data"]) == type([]):
      if len(bottle.request.json[u"data"]) >= 0:
        with taco.globals.settings_lock:
          logging.info("API Access: PEER -- Action: SAVE")
          taco.globals.settings["Peers"] = {}
          for (hostname,port,localnick,peeruuid,clientpub,serverpub,dynamic,enabled) in bottle.request.json[u"data"]:
            taco.globals.settings["Peers"][unicode(peeruuid)] = {"hostname":hostname,"port": int(port),"localnick":localnick,"dynamic":int(dynamic),"enabled":int(enabled),"clientkey":clientpub,"serverkey":serverpub}
          taco.settings.Save_Settings(False)
        taco.globals.server.stop.set()
        taco.globals.clients.stop.set()
        taco.globals.server.join()
        taco.globals.clients.join()
        taco.globals.server = taco.server.TacoServer()
        taco.globals.clients = taco.clients.TacoClients()
        taco.globals.server.start()
        taco.globals.clients.start()
        return "1"

  return "-1"

@bottle.route('/browselocaldirs/')
@bottle.route('/browselocaldirs/<browse_path:path>')
def index(browse_path="/"):
  if browse_path=="": browse_path ="/"
  browse_path = "/" + browse_path
  browse_path = unicode(browse_path)
  contents = os.listdir(browse_path)
  final_contents = []
  for item in contents:
    try:
      if os.path.isdir(os.path.join(browse_path,item)): final_contents.append(item)
    except:
      continue
  final_contents.sort()
  return json.dumps(final_contents)

@bottle.route('/get/<what>')
def getData(what):
  output = ""
  logging.debug("Route -- Getting your: " + what)
  if what=="uuid":
    return uuid.uuid4().hex
  if what=="ip":
    data = urllib.urlopen("http://checkip.dyndns.org/").read()
    m = re.match(r'.*Current IP Address: (.*)</body>',data)
    if m:
      output = m.group(1)
  if what =="diskfree":
    with taco.globals.settings_lock:
      down_dir = taco.globals.settings["Download Location"]
    if os.path.isdir(down_dir):
      (free,total) = taco.filesystem.Get_Free_Space(down_dir)
      if free == 0 and total == 0:
        output = 0.0
      else:
        output = free/taco.constants.GB
  return str(output)
<|endoftext|>


<commit_before>from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template import defaultfilters

import tags

import json

colours_enum = [
    ("white", "White"),
    ("black", "Black"),
    ("darkblue", "Dark Blue"),
    ("darkgreen", "Dark Green"),
    ("darkred", "Dark Red"),
    ("blue", "Blue"),
    ("green", "Green"),
    ("red", "Red"),
    ("yellow", "Yellow"),
    ("magenta", "Magenta"),
    ("cyan", "Cyan"),
    ("orange", "Orange"),
]

class Taggable(models.Model):
    class Meta:
        abstract = True

    def tag(self, tag):
        if isinstance(tag, (int, long)):
            try:
                tag = Tag.objects.get(pk=tag, owner=self.owner)
            except Tag.DoesNotExist:
                #Handle this better?
                return

        if isinstance(tag, (str, unicode)):
            try:
                tag = Tag.objects.get(slug=defaultfilters.slugify(tag), owner=self.owner)
            except Tag.DoesNotExist:
                tag = Tag(owner=self.owner, name=tag)
                tag.save()

        tag.owner = self.owner
        tag.save()
        self.tags.add(tag)

    def untag(self, tag):
        if isinstance(tag, (int, long)):
            try:
                tag = Tag.objects.get(pk=tag, owner=self.owner)
            except Tag.DoesNotExist:
                return

        if type(tag) == str:
            try:
                tag = Tag.objects.get(name__iexact=tag, owner=self.owner)
            except Tag.DoesNotExist:
                return

        self.tags.remove(tag)

    @classmethod
    def get_by_tag(cls, tag):
        out = []

        tags = Tag.expand_implied_by([tag])

        for t in tags:
            results = cls.objects.filter(owner=tag.owner, tags=t)

            for b in results:
                if b not in out:
                    out.append(b)

        return out

@tags.taggable("tag")
class Tag(Taggable):
    class Meta:
        ordering = ["slug"]

    owner = models.ForeignKey(User)
    name = models.TextField(max_length=100)
    colour = models.CharField(max_length=20, choices=colours_enum, default='white')
    slug = models.SlugField()
    tags = models.ManyToManyField("self", related_name="tags_to", symmetrical=False, db_table="tags_tag_implies")

    def __str__(self):
        return ("Tag '"+self.name+"' for "+self.owner.username).encode("ascii", "ignore")

    def to_dir(self):
        out = {}
        out["name"] = self.name
        out["colour"] = self.colour
        out["slug"] = self.slug
        out["id"] = self.pk

        return out

    def to_json(self):
        return json.dumps(self.to_dir())

    @staticmethod
    def get_or_create_with_slug(owner, instance):
        try:
            return Tag.objects.get(owner=owner, slug=defaultfilters.slugify(instance.name))
        except Tag.DoesNotExist:
            instance.owner = owner
            instance.save()
            return instance

    @staticmethod
    def expand_implies(tags):
        out = list(tags)

        i = 0
        while i < len(out):
            for implied in out[i].tags.all():
                if implied not in out:
                    out.append(implied)

            i += 1

        out.sort(lambda a, b: cmp(a.slug, b.slug))
        return out

    @staticmethod
    def expand_implies_check(tags):
        """ Same as expand_implies, but as a pair, second element is whether the given tag is attached directly rather
        than implied. """
        out = map(lambda x : (x, True), list(tags))
        tlist = list(tags)

        i = 0
        while i < len(tlist):
            for implied in tlist[i].tags.all():
                if implied not in tlist:
                    out.append((implied, False))
                    tlist.append(implied)

            i += 1

        out.sort(lambda a, b: cmp(a[0].slug, b[0].slug))
        return out

    @staticmethod
    def expand_implied_by(tags):
        out = list(tags)

        i = 0
        while i < len(out):
            for implies in out[i].tags_to.all():
                if implies not in out:
                    out.append(implies)

            i += 1

        out.sort(lambda a, b: cmp(a.slug, b.slug))
        return out

    @staticmethod
    def expand_implied_by_check(tags):
        """ Same as expand_implied_by, but as a pair, second element is whether the given tag is attached directly
        rather than implied. """
        out = map(lambda x : (x, True), list(tags))
        tlist = list(tags)

        i = 0
        while i < len(tlist):
            for implies in tlist[i].tags_to.all():
                if implies not in out:
                    out.append((implies, False))
                    tlist.append(implies)

            i += 1

        out.sort(lambda a, b: cmp(a[0].slug, b[0].slug))
        return out

    @staticmethod
    def by_user(user):
        return Tag.objects.all().filter(owner=user)

@receiver(post_save, sender=Tag)
def tag_save(sender, instance, created, **kwargs):
    if instance.slug != defaultfilters.slugify(instance.name):
        instance.slug = defaultfilters.slugify(instance.name)
        instance.save()
<commit_msg>untag uses same argument types as tag<commit_after>from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template import defaultfilters

import tags

import json

colours_enum = [
    ("white", "White"),
    ("black", "Black"),
    ("darkblue", "Dark Blue"),
    ("darkgreen", "Dark Green"),
    ("darkred", "Dark Red"),
    ("blue", "Blue"),
    ("green", "Green"),
    ("red", "Red"),
    ("yellow", "Yellow"),
    ("magenta", "Magenta"),
    ("cyan", "Cyan"),
    ("orange", "Orange"),
]

class Taggable(models.Model):
    class Meta:
        abstract = True

    def tag(self, tag):
        if isinstance(tag, (int, long)):
            try:
                tag = Tag.objects.get(pk=tag, owner=self.owner)
            except Tag.DoesNotExist:
                #Handle this better?
                return

        if isinstance(tag, (str, unicode)):
            try:
                tag = Tag.objects.get(slug=defaultfilters.slugify(tag), owner=self.owner)
            except Tag.DoesNotExist:
                tag = Tag(owner=self.owner, name=tag)
                tag.save()

        tag.owner = self.owner
        tag.save()
        self.tags.add(tag)

    def untag(self, tag):
        if isinstance(tag, (int, long)):
            try:
                tag = Tag.objects.get(pk=tag, owner=self.owner)
            except Tag.DoesNotExist:
                return

        if isinstance(tag, (str, unicode)):
            try:
                tag = Tag.objects.get(slug=defaultfilters.slugify(tag), owner=self.owner)
            except Tag.DoesNotExist:
                return

        self.tags.remove(tag)

    @classmethod
    def get_by_tag(cls, tag):
        out = []

        tags = Tag.expand_implied_by([tag])

        for t in tags:
            results = cls.objects.filter(owner=tag.owner, tags=t)

            for b in results:
                if b not in out:
                    out.append(b)

        return out

@tags.taggable("tag")
class Tag(Taggable):
    class Meta:
        ordering = ["slug"]

    owner = models.ForeignKey(User)
    name = models.TextField(max_length=100)
    colour = models.CharField(max_length=20, choices=colours_enum, default='white')
    slug = models.SlugField()
    tags = models.ManyToManyField("self", related_name="tags_to", symmetrical=False, db_table="tags_tag_implies")

    def __str__(self):
        return ("Tag '"+self.name+"' for "+self.owner.username).encode("ascii", "ignore")

    def to_dir(self):
        out = {}
        out["name"] = self.name
        out["colour"] = self.colour
        out["slug"] = self.slug
        out["id"] = self.pk

        return out

    def to_json(self):
        return json.dumps(self.to_dir())

    @staticmethod
    def get_or_create_with_slug(owner, instance):
        try:
            return Tag.objects.get(owner=owner, slug=defaultfilters.slugify(instance.name))
        except Tag.DoesNotExist:
            instance.owner = owner
            instance.save()
            return instance

    @staticmethod
    def expand_implies(tags):
        out = list(tags)

        i = 0
        while i < len(out):
            for implied in out[i].tags.all():
                if implied not in out:
                    out.append(implied)

            i += 1

        out.sort(lambda a, b: cmp(a.slug, b.slug))
        return out

    @staticmethod
    def expand_implies_check(tags):
        """ Same as expand_implies, but as a pair, second element is whether the given tag is attached directly rather
        than implied. """
        out = map(lambda x : (x, True), list(tags))
        tlist = list(tags)

        i = 0
        while i < len(tlist):
            for implied in tlist[i].tags.all():
                if implied not in tlist:
                    out.append((implied, False))
                    tlist.append(implied)

            i += 1

        out.sort(lambda a, b: cmp(a[0].slug, b[0].slug))
        return out

    @staticmethod
    def expand_implied_by(tags):
        out = list(tags)

        i = 0
        while i < len(out):
            for implies in out[i].tags_to.all():
                if implies not in out:
                    out.append(implies)

            i += 1

        out.sort(lambda a, b: cmp(a.slug, b.slug))
        return out

    @staticmethod
    def expand_implied_by_check(tags):
        """ Same as expand_implied_by, but as a pair, second element is whether the given tag is attached directly
        rather than implied. """
        out = map(lambda x : (x, True), list(tags))
        tlist = list(tags)

        i = 0
        while i < len(tlist):
            for implies in tlist[i].tags_to.all():
                if implies not in out:
                    out.append((implies, False))
                    tlist.append(implies)

            i += 1

        out.sort(lambda a, b: cmp(a[0].slug, b[0].slug))
        return out

    @staticmethod
    def by_user(user):
        return Tag.objects.all().filter(owner=user)

@receiver(post_save, sender=Tag)
def tag_save(sender, instance, created, **kwargs):
    if instance.slug != defaultfilters.slugify(instance.name):
        instance.slug = defaultfilters.slugify(instance.name)
        instance.save()
<|endoftext|>