<commit_before>#License#
#bitHopper by Colin Rice is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#Based on a work at github.com.

import json
import work
from password import *
from diff import difficulty
import re

class Pool():
    def __init__(self):
        default_shares = difficulty

        self.servers = {
                'bclc':{'shares':default_shares, 'name':'bitcoins.lc',
                    'mine_address':'bitcoins.lc:8080', 'user':bclc_user, 'pass':bclc_pass,
                    'lag':False, 'LP':None,
                    'api_address':'https://www.bitcoins.lc/stats.json', 'role':'disable' },
                'mtred':{'shares':default_shares, 'name':'mtred',
                    'mine_address':'mtred.com:8337', 'user':mtred_user, 'pass':mtred_pass,
                    'lag':False, 'LP':None,
                    'api_address':'https://mtred.com/api/user/key/' + mtred_user_apikey,
                    'role':'mine'},
                'btcg':{'shares':default_shares, 'name':'BTC Guild',
                    'mine_address':'us.btcguild.com:8332', 'user':btcguild_user,
                    'pass':btcguild_pass, 'lag':False, 'LP':None,
                    'api_address':'https://www.btcguild.com/pool_stats.php',
                    'user_api_address':'https://www.btcguild.com/api.php?api_key='+btcguild_user_apikey,
                    'role':'disable'},
                'eligius':{'shares':difficulty*.41, 'name':'eligius',
                    'mine_address':'su.mining.eligius.st:8337', 'user':eligius_address,
                    'pass':'x', 'lag':False, 'LP':None, 'role':'backup'},
                'arsbitcoin':{'shares':difficulty*.41, 'name':'arsbitcoin',
                    'mine_address':'arsbitcoin.com:8344', 'user':ars_user,
                    'pass':ars_pass, 'lag':False, 'LP':None, 'role':'backup'},
                'mineco':{'shares': default_shares, 'name': 'mineco.in',
                    'mine_address': 'mineco.in:3000', 'user': mineco_user,
                    'pass': mineco_pass, 'lag': False, 'LP': None,
                    'api_address':'https://mineco.in/stats.json', 'role':'disable'},
                'bitclockers':{'shares': default_shares, 'name': 'bitclockers.com',
                    'mine_address': 'pool.bitclockers.com:8332', 'user': bitclockers_user,
                    'pass': bitclockers_pass, 'lag': False, 'LP': None,
                    'api_address':'https://bitclockers.com/api', 'role':'disable',
                    'user_api_address':'https://bitclockers.com/api/'+bitclockers_user_apikey},
               'eclipsemc':{'shares': default_shares, 'name': 'eclipsemc.com',
                    'mine_address': 'pacrim.eclipsemc.com:8337', 'user': eclipsemc_user,
                    'pass': eclipsemc_pass, 'lag': False, 'LP': None,
                    'api_address':'https://eclipsemc.com/api.php?key='+ eclipsemc_apikey
                     +'&action=poolstats', 'role':'disable'},
                'miningmainframe':{'shares': default_shares, 'name': 'mining.mainframe.nl',
                   'mine_address': 'mining.mainframe.nl:8343', 'user': miningmainframe_user,
                   'pass': miningmainframe_pass, 'lag': False, 'LP': None,
                    'api_address':'http://mining.mainframe.nl/api', 'role':'disable'},
                'bitp':{'shares': default_shares, 'name': 'bitp.it',
                   'mine_address': 'pool.bitp.it:8334', 'user': bitp_user,
                   'pass': bitp_pass, 'lag': False,
                   'api_address':'https://pool.bitp.it/leaderboard', 'role':'disable',
                   'user_api_address':'https://pool.bitp.it/api/user?token=' + bitp_user_apikey},
                'ozco':{'shares': default_shares, 'name': 'ozco.in',
                   'mine_address': 'ozco.in:8332', 'user': ozco_user,
                   'pass': ozco_pass, 'lag': False,
                   'api_address':'https://ozco.in/api.php', 'role':'mine'},
               'triple':{'shares': default_shares, 'name': 'triplemining.com',
                   'mine_address': 'eu1.triplemining.com:8344', 'user': triple_user,
                   'pass': triple_pass, 'lag': False,
                   'api_address':'https://www.triplemining.com/stats',
                    'role':'mine'},
                'x8s':{'shares': default_shares, 'name': 'btc.x8s.de',
                    'mine_address': 'pit.x8s.de:8337', 'user': x8s_user,
                    'pass': x8s_pass, 'lag': False, 'LP': None,
                    'api_address':'',
                    'role':'disable'},
                'rfc':{'shares': default_shares, 'name': 'rfcpool.com',
                    'mine_address': 'pool.rfcpool.com:8332', 'user': rfc_user,
                    'pass': 'x', 'lag': False, 'LP': None,
                    'api_address':'https://www.rfcpool.com/api/pool/stats',
                    'role':'mine'},
                }

        self.current_server = 'mtred'

    def setup(self,bitHopper):
        self.bitHopper = bitHopper
        for server in self.servers:
            self.servers[server]['refresh_time'] = 60
            self.servers[server]['rejects'] = self.bitHopper.db.get_rejects(server)
            self.servers[server]['user_shares']=self.bitHopper.db.get_shares(server)

    def get_entry(self, server):
        if server in self.servers:
            return self.servers[server]
        else:
            return None

    def get_servers(self, ):
        return self.servers

    def get_current(self, ):
        return self.current_server

    def set_current(self, server):
        self.current_server = server

    def UpdateShares(self, server, shares):

        prev_shares = self.servers[server]['shares']
        if shares == prev_shares:
            self.servers[server]['refresh_time'] += .10*self.servers[server]['refresh_time']
            self.bitHopper.reactor.callLater(5,self.update_api_server,server)
        else:
            self.servers[server]['refresh_time'] -= .10*self.servers[server]['refresh_time']
            time = self.servers[server]['refresh_time']
            if time <= 10:
                self.servers[server]['refresh_time'] = 10
            self.bitHopper.reactor.callLater(time,self.update_api_server,server)

        try:
            k =  str('{0:,d}'.format(shares))
        except Exception, e:
            self.bitHopper.log_dbg("Error formatting")
            self.bitHopper.log_dbg(e)
            k =  str(shares)
        if shares != prev_shares:
            self.bitHopper.log_msg(str(server) +": "+ k)
        self.servers[server]['shares'] = shares
        if self.servers[server]['refresh_time'] > 60*30:
            self.bitHopper.log_msg('Disabled due to unchanging api: ' + server)
            return

    def rfc_sharesResponse(self, response):
        round_shares = json.loads(response)['poolstats']['round_shares']
        self.UpdateShares('rfc',round_shares)

    def triple_sharesResponse(self, response):
        output = re.search('\d+	', response)
        match = output.group(0)
        match = match[4:-5]
        round_shares = int(match)
        self.UpdateShares('triple',round_shares)

    def ozco_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['shares'])
        self.UpdateShares('ozco',round_shares)

    def mmf_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['shares_this_round'])
        self.UpdateShares('miningmainframe',round_shares)

    def bitp_sharesResponse(self, response):
        output = re.search('Total	\n        \d+', response)
        match = output.group(0)
        match = match[match.find('')+4:]
        round_shares = int(match)
        self.UpdateShares('bitp',round_shares)

    def eclipsemc_sharesResponse(self, response):
        info = json.loads(response[:response.find('}')+1])
        round_shares = int(info['round_shares'])
        self.UpdateShares('eclipsemc',round_shares)

    def btcguild_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['round_shares'])
        self.UpdateShares('btcg',round_shares)

    def bclc_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['round_shares'])
        self.UpdateShares('bclc',round_shares)

    def mtred_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['server']['roundshares'])
        self.UpdateShares('mtred',round_shares)

    def mineco_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['shares_this_round'])
        self.UpdateShares('mineco',round_shares)

    def bitclockers_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['roundshares'])
        self.UpdateShares('bitclockers',round_shares)

    def errsharesResponse(self, error, args):
        self.bitHopper.log_msg('Error in pool api for ' + str(args))
        self.bitHopper.log_dbg(str(error))
        pool = args
        self.servers[pool]['shares'] = 10**10
        time = self.servers[pool]['refresh_time']
        self.bitHopper.reactor.callLater(time, self.update_api_server, pool)

    def selectsharesResponse(self, response, args):
        self.bitHopper.log_dbg('Calling sharesResponse for '+ args)
        func_map= {'bitclockers':self.bitclockers_sharesResponse,
            'mineco':self.mineco_sharesResponse,
            'mtred':self.mtred_sharesResponse,
            'bclc':self.bclc_sharesResponse,
           #'btcg':self.btcguild_sharesResponse,
            'eclipsemc':self.eclipsemc_sharesResponse,
            'miningmainframe':self.mmf_sharesResponse,
            'bitp':self.bitp_sharesResponse,
            'ozco':self.ozco_sharesResponse,
            'triple':self.triple_sharesResponse,
            'rfc':self.rfc_sharesResponse}
        func_map[args](response)
        self.bitHopper.server_update()

    def update_api_server(self,server):
        info = self.servers[server]
        d = work.get(self.bitHopper.json_agent,info['api_address'])
        d.addCallback(self.selectsharesResponse, (server))
        d.addErrback(self.errsharesResponse, (server))
        d.addErrback(self.bitHopper.log_msg)

    def update_api_servers(self, bitHopper):
        self.bitHopper = bitHopper
        for server in self.servers:
            info = self.servers[server]
            update = ['info','mine']
            if info['role'] in update:
                d = work.get(self.bitHopper.json_agent,info['api_address'])
                d.addCallback(self.selectsharesResponse, (server))
                d.addErrback(self.errsharesResponse, (server))
                d.addErrback(self.bitHopper.log_msg)

if __name__ == "__main__":
    print 'Run python bitHopper.py instead.'
<commit_msg>Another API polling fix<commit_after>#License#
#bitHopper by Colin Rice is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
#Based on a work at github.com.

import json
import work
from password import *
from diff import difficulty
import re

class Pool():
    def __init__(self):
        default_shares = difficulty

        self.servers = {
                'bclc':{'shares':default_shares, 'name':'bitcoins.lc',
                    'mine_address':'bitcoins.lc:8080', 'user':bclc_user, 'pass':bclc_pass,
                    'lag':False, 'LP':None,
                    'api_address':'https://www.bitcoins.lc/stats.json', 'role':'disable' },
                'mtred':{'shares':default_shares, 'name':'mtred',
                    'mine_address':'mtred.com:8337', 'user':mtred_user, 'pass':mtred_pass,
                    'lag':False, 'LP':None,
                    'api_address':'https://mtred.com/api/user/key/' + mtred_user_apikey,
                    'role':'mine'},
                'btcg':{'shares':default_shares, 'name':'BTC Guild',
                    'mine_address':'us.btcguild.com:8332', 'user':btcguild_user,
                    'pass':btcguild_pass, 'lag':False, 'LP':None,
                    'api_address':'https://www.btcguild.com/pool_stats.php',
                    'user_api_address':'https://www.btcguild.com/api.php?api_key='+btcguild_user_apikey,
                    'role':'disable'},
                'eligius':{'shares':difficulty*.41, 'name':'eligius',
                    'mine_address':'su.mining.eligius.st:8337', 'user':eligius_address,
                    'pass':'x', 'lag':False, 'LP':None, 'role':'backup'},
                'arsbitcoin':{'shares':difficulty*.41, 'name':'arsbitcoin',
                    'mine_address':'arsbitcoin.com:8344', 'user':ars_user,
                    'pass':ars_pass, 'lag':False, 'LP':None, 'role':'backup'},
                'mineco':{'shares': default_shares, 'name': 'mineco.in',
                    'mine_address': 'mineco.in:3000', 'user': mineco_user,
                    'pass': mineco_pass, 'lag': False, 'LP': None,
                    'api_address':'https://mineco.in/stats.json', 'role':'disable'},
                'bitclockers':{'shares': default_shares, 'name': 'bitclockers.com',
                    'mine_address': 'pool.bitclockers.com:8332', 'user': bitclockers_user,
                    'pass': bitclockers_pass, 'lag': False, 'LP': None,
                    'api_address':'https://bitclockers.com/api', 'role':'disable',
                    'user_api_address':'https://bitclockers.com/api/'+bitclockers_user_apikey},
               'eclipsemc':{'shares': default_shares, 'name': 'eclipsemc.com',
                    'mine_address': 'pacrim.eclipsemc.com:8337', 'user': eclipsemc_user,
                    'pass': eclipsemc_pass, 'lag': False, 'LP': None,
                    'api_address':'https://eclipsemc.com/api.php?key='+ eclipsemc_apikey
                     +'&action=poolstats', 'role':'disable'},
                'miningmainframe':{'shares': default_shares, 'name': 'mining.mainframe.nl',
                   'mine_address': 'mining.mainframe.nl:8343', 'user': miningmainframe_user,
                   'pass': miningmainframe_pass, 'lag': False, 'LP': None,
                    'api_address':'http://mining.mainframe.nl/api', 'role':'disable'},
                'bitp':{'shares': default_shares, 'name': 'bitp.it',
                   'mine_address': 'pool.bitp.it:8334', 'user': bitp_user,
                   'pass': bitp_pass, 'lag': False,
                   'api_address':'https://pool.bitp.it/leaderboard', 'role':'disable',
                   'user_api_address':'https://pool.bitp.it/api/user?token=' + bitp_user_apikey},
                'ozco':{'shares': default_shares, 'name': 'ozco.in',
                   'mine_address': 'ozco.in:8332', 'user': ozco_user,
                   'pass': ozco_pass, 'lag': False,
                   'api_address':'https://ozco.in/api.php', 'role':'mine'},
               'triple':{'shares': default_shares, 'name': 'triplemining.com',
                   'mine_address': 'eu1.triplemining.com:8344', 'user': triple_user,
                   'pass': triple_pass, 'lag': False,
                   'api_address':'https://www.triplemining.com/stats',
                    'role':'mine'},
                'x8s':{'shares': default_shares, 'name': 'btc.x8s.de',
                    'mine_address': 'pit.x8s.de:8337', 'user': x8s_user,
                    'pass': x8s_pass, 'lag': False, 'LP': None,
                    'api_address':'',
                    'role':'disable'},
                'rfc':{'shares': default_shares, 'name': 'rfcpool.com',
                    'mine_address': 'pool.rfcpool.com:8332', 'user': rfc_user,
                    'pass': 'x', 'lag': False, 'LP': None,
                    'api_address':'https://www.rfcpool.com/api/pool/stats',
                    'role':'mine'},
                }

        self.current_server = 'mtred'

    def setup(self,bitHopper):
        self.bitHopper = bitHopper
        for server in self.servers:
            self.servers[server]['refresh_time'] = 60
            self.servers[server]['rejects'] = self.bitHopper.db.get_rejects(server)
            self.servers[server]['user_shares']=self.bitHopper.db.get_shares(server)

    def get_entry(self, server):
        if server in self.servers:
            return self.servers[server]
        else:
            return None

    def get_servers(self, ):
        return self.servers

    def get_current(self, ):
        return self.current_server

    def set_current(self, server):
        self.current_server = server

    def UpdateShares(self, server, shares):

        prev_shares = self.servers[server]['shares']
        if shares == prev_shares:
            time = .10*self.servers[server]['refresh_time']
            if time <= 10:
                time = 10.
            self.servers[server]['refresh_time'] += .10*self.servers[server]['refresh_time']
            self.bitHopper.reactor.callLater(time,self.update_api_server,server)
        else:
            self.servers[server]['refresh_time'] -= .10*self.servers[server]['refresh_time']
            time = self.servers[server]['refresh_time']
            if time <= 10:
                self.servers[server]['refresh_time'] = 10
            self.bitHopper.reactor.callLater(time,self.update_api_server,server)

        try:
            k =  str('{0:,d}'.format(shares))
        except Exception, e:
            self.bitHopper.log_dbg("Error formatting")
            self.bitHopper.log_dbg(e)
            k =  str(shares)
        if shares != prev_shares:
            self.bitHopper.log_msg(str(server) +": "+ k)
        self.servers[server]['shares'] = shares
        if self.servers[server]['refresh_time'] > 60*30:
            self.bitHopper.log_msg('Disabled due to unchanging api: ' + server)
            return

    def rfc_sharesResponse(self, response):
        round_shares = json.loads(response)['poolstats']['round_shares']
        self.UpdateShares('rfc',round_shares)

    def triple_sharesResponse(self, response):
        output = re.search('\d+	', response)
        match = output.group(0)
        match = match[4:-5]
        round_shares = int(match)
        self.UpdateShares('triple',round_shares)

    def ozco_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['shares'])
        self.UpdateShares('ozco',round_shares)

    def mmf_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['shares_this_round'])
        self.UpdateShares('miningmainframe',round_shares)

    def bitp_sharesResponse(self, response):
        output = re.search('Total	\n        \d+', response)
        match = output.group(0)
        match = match[match.find('')+4:]
        round_shares = int(match)
        self.UpdateShares('bitp',round_shares)

    def eclipsemc_sharesResponse(self, response):
        info = json.loads(response[:response.find('}')+1])
        round_shares = int(info['round_shares'])
        self.UpdateShares('eclipsemc',round_shares)

    def btcguild_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['round_shares'])
        self.UpdateShares('btcg',round_shares)

    def bclc_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['round_shares'])
        self.UpdateShares('bclc',round_shares)

    def mtred_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['server']['roundshares'])
        self.UpdateShares('mtred',round_shares)

    def mineco_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['shares_this_round'])
        self.UpdateShares('mineco',round_shares)

    def bitclockers_sharesResponse(self, response):
        info = json.loads(response)
        round_shares = int(info['roundshares'])
        self.UpdateShares('bitclockers',round_shares)

    def errsharesResponse(self, error, args):
        self.bitHopper.log_msg('Error in pool api for ' + str(args))
        self.bitHopper.log_dbg(str(error))
        pool = args
        self.servers[pool]['shares'] = 10**10
        time = self.servers[pool]['refresh_time']
        self.bitHopper.reactor.callLater(time, self.update_api_server, pool)

    def selectsharesResponse(self, response, args):
        self.bitHopper.log_dbg('Calling sharesResponse for '+ args)
        func_map= {'bitclockers':self.bitclockers_sharesResponse,
            'mineco':self.mineco_sharesResponse,
            'mtred':self.mtred_sharesResponse,
            'bclc':self.bclc_sharesResponse,
           #'btcg':self.btcguild_sharesResponse,
            'eclipsemc':self.eclipsemc_sharesResponse,
            'miningmainframe':self.mmf_sharesResponse,
            'bitp':self.bitp_sharesResponse,
            'ozco':self.ozco_sharesResponse,
            'triple':self.triple_sharesResponse,
            'rfc':self.rfc_sharesResponse}
        func_map[args](response)
        self.bitHopper.server_update()

    def update_api_server(self,server):
        info = self.servers[server]
        d = work.get(self.bitHopper.json_agent,info['api_address'])
        d.addCallback(self.selectsharesResponse, (server))
        d.addErrback(self.errsharesResponse, (server))
        d.addErrback(self.bitHopper.log_msg)

    def update_api_servers(self, bitHopper):
        self.bitHopper = bitHopper
        for server in self.servers:
            info = self.servers[server]
            update = ['info','mine']
            if info['role'] in update:
                d = work.get(self.bitHopper.json_agent,info['api_address'])
                d.addCallback(self.selectsharesResponse, (server))
                d.addErrback(self.errsharesResponse, (server))
                d.addErrback(self.bitHopper.log_msg)

if __name__ == "__main__":
    print 'Run python bitHopper.py instead.'
<|endoftext|>


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

import sys
reload(sys)
sys.setdefaultencoding('UTF-8')

import nltk
import re
import subprocess
import time
import urllib2

# Todos
# rename the file and variables
# remove accepting command line calls

def google(language='en', text='Service provided by Baidu', output_path='out.mp3'):
    """
    1. download mp3 from google tts api
    2. convert it to wav
    3. speed up the wav file, if necessary
    4. convert to mp3
    5. store in some location
    6. return the path
    """
    # generate out.mp3
    download(language, text, output_path)
    subprocess.Popen(
        'tts="%s"; lame --decode $tts - | sox -t wav - -t wav - speed 1.1 | lame - $tts' % output_path, stderr=subprocess.PIPE, shell=True)

def download(language='en', text='Service provided by Baidu', output='out.mp3'):
    '''
    languange = ja, en, pt, zh_CN, ar, th
    '''
    text = text.replace('\n', '')
    sents = nltk.sent_tokenize(text)
    text_list = re.split('(\,|\.)', text)
    combined_text = []
    for idx, val in enumerate(text_list):
        if idx % 2 == 0:
            combined_text.append(val)
        else:
            joined_text = ''.join((combined_text.pop(), val))
            if len(joined_text) < 100:
                combined_text.append(joined_text)
            else:
                subparts = re.split('( )', joined_text)
                temp_string = ""
                temp_array = []
                for part in subparts:
                    temp_string = temp_string + part
                    if len(temp_string) > 80:
                        temp_array.append(temp_string)
                        temp_string = ""
                # append final part
                temp_array.append(temp_string)
                combined_text.extend(temp_array)

    # Todos
    # rewrite: multi-threaded

    # download chunks and write them to the output file
    if isinstance(output, str):
        output = open(output, 'w')
    for idx, val in enumerate(combined_text):
        print 'transmitting ... %s' % val
        mp3url = "http://translate.google.com/translate_tts?tl=%s&q=%s&total=%s&idx=%s" % (
            language, urllib2.quote(val), len(combined_text), idx)
        headers = {"Host": "translate.google.com",
                   "Referer": "http://www.gstatic.com/translate/sound_player2.swf",
                   "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.163 Safari/535.19"}
        req = urllib2.Request(mp3url, '', headers)
        sys.stdout.write('.')
        sys.stdout.flush()
        if len(val) > 0:
            try:
                response = urllib2.urlopen(req)
                output.write(response.read())
                time.sleep(.5)
            except urllib2.HTTPError as e:
                print ('%s' % e)
    output.close()
    print
<commit_msg>simplified sentences segment algorithm, made only right now for latins<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
reload(sys)
sys.setdefaultencoding('UTF-8')

import nltk
import re
import subprocess
import time
import urllib2

# Todos
# rename the file and variables
# remove accepting command line calls

def google(language='en', text='Service provided by Baidu', output_path='out.mp3'):
    """
    1. download mp3 from google tts api
    2. convert it to wav
    3. speed up the wav file, if necessary
    4. convert to mp3
    5. store in some location
    6. return the path
    """
    # generate out.mp3
    download(language, text, output_path)
    subprocess.Popen(
        'tts="%s"; lame --decode $tts - | sox -t wav - -t wav - speed 1.1 | lame - $tts' % output_path, stderr=subprocess.PIPE, shell=True)

def download(language='en', text='Service provided by Baidu', output='out.mp3'):
    '''
    languange = ja, en, pt, zh_CN, ar, th
    '''
    text = text.replace('\n', '')
    # simplified case only for latins
    combined_text = nltk.sent_tokenize(text)
    """
    text_list = re.split('(\,)', text)
    combined_text = []
    for idx, val in enumerate(text_list):
        if idx % 2 == 0:
            combined_text.append(val)
        else:
            joined_text = ''.join((combined_text.pop(), val))
            if len(joined_text) < 100:
                combined_text.append(joined_text)
            else:
                subparts = re.split('( )', joined_text)
                temp_string = ""
                temp_array = []
                for part in subparts:
                    temp_string = temp_string + part
                    if len(temp_string) > 80:
                        temp_array.append(temp_string)
                        temp_string = ""
                # append final part
                temp_array.append(temp_string)
                combined_text.extend(temp_array)
    """

    # Todos
    # rewrite: multi-threaded

    # download chunks and write them to the output file
    if isinstance(output, str):
        output = open(output, 'w')
    for idx, val in enumerate(combined_text):
        print 'transmitting ... %s' % val
        mp3url = "http://translate.google.com/translate_tts?tl=%s&q=%s&total=%s&idx=%s" % (
            language, urllib2.quote(val), len(combined_text), idx)
        headers = {"Host": "translate.google.com",
                   "Referer": "http://www.gstatic.com/translate/sound_player2.swf",
                   "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.163 Safari/535.19"}
        req = urllib2.Request(mp3url, '', headers)
        sys.stdout.write('.')
        sys.stdout.flush()
        if len(val) > 0:
            try:
                response = urllib2.urlopen(req)
                output.write(response.read())
                time.sleep(.5)
            except urllib2.HTTPError as e:
                print ('%s' % e)
    output.close()
    print
<|endoftext|>


<commit_before># encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import absolute_import
import math

from ._align_getter import align_getter
from ._container import MinMaxContainer
from ._interface import DataPeropertyInterface
from ._typecode import Typecode
from ._type_checker import FloatTypeChecker

from ._function import is_nan
from ._function import get_number_of_digit
from ._function import get_text_len

from ._factory import NoneTypeFactory
from ._factory import IntegerTypeFactory
from ._factory import FloatTypeFactory
from ._factory import BoolTypeFactory
from ._factory import DateTimeTypeFactory
from ._factory import InfinityTypeFactory
from ._factory import NanTypeFactory

def default_bool_converter(value):
    return value

class DataProperty(DataPeropertyInterface):
    __slots__ = (
        "__data",
        "__typecode",
        "__align",
        "__integer_digits",
        "__decimal_places",
        "__additional_format_len",
        "__str_len",
    )

    __type_factory_list = [
        NoneTypeFactory(),
        IntegerTypeFactory(),
        InfinityTypeFactory(),
        NanTypeFactory(),
        FloatTypeFactory(),
        BoolTypeFactory(),
        DateTimeTypeFactory(),
    ]

    @property
    def align(self):
        return self.__align

    @property
    def decimal_places(self):
        """
        :return:
            Decimal places if the ``data`` is ``float``.
            Returns ``0`` if the ``data`` is ``int``.
            Otherwise, returns ``float("nan")``.
        :rtype: int
        """

        return self.__decimal_places

    @property
    def typecode(self):
        """
        Return the type code that corresponds to the type of the ``data``.

        :return:
            One of the constants that are defined in the ``Typecode`` class.
        :rtype: int
        """

        return self.__typecode

    @property
    def format_str(self):
        if self.typecode == Typecode.INT:
            return "d"

        if self.typecode == Typecode.FLOAT:
            if is_nan(self.decimal_places):
                return "f"

            return ".%df" % (self.decimal_places)

        return "s"

    @property
    def data(self):
        """
        :return: Original data.
        :rtype: Original data type.
        """

        return self.__data

    @property
    def str_len(self):
        """
        :return: Length of the ``data`` as a string.
        :rtype: int
        """

        return self.__str_len

    @property
    def integer_digits(self):
        """
        :return:
            Integer digits if the ``data`` is ``int``/``float``.
            Otherwise, returns ``float("nan")``.
        :rtype: int
        """

        return self.__integer_digits

    @property
    def additional_format_len(self):
        return self.__additional_format_len

    def __init__(
            self, data,
            none_value=None, inf_value=float("inf"), nan_value=float("nan"),
            bool_converter=default_bool_converter,
            is_convert=True,
            replace_tabs_with_spaces=True, tab_length=2):
        super(DataProperty, self).__init__()

        self.__set_data(
            data, none_value, inf_value, nan_value, bool_converter, is_convert)
        self.__replace_tabs(replace_tabs_with_spaces, tab_length)
        self.__align = align_getter.get_align_from_typecode(self.typecode)

        try:
            integer_digits, decimal_places = get_number_of_digit(data)
        except OverflowError:
            integer_digits = float("nan")
            decimal_places = float("nan")
        self.__integer_digits = integer_digits
        self.__decimal_places = decimal_places
        self.__additional_format_len = self.__get_additional_format_len()
        self.__str_len = self.__get_str_len()

    def __repr__(self):
        return ", ".join([
            ("data=%" + self.format_str) % (self.data),
            "typename=" + Typecode.get_typename(self.typecode),
            "align=" + str(self.align),
            "str_len=" + str(self.str_len),
            "integer_digits=" + str(self.integer_digits),
            "decimal_places=" + str(self.decimal_places),
            "additional_format_len=" + str(self.additional_format_len),
        ])

    def __get_additional_format_len(self):
        if not FloatTypeChecker(self.data).is_type():
            return 0

        format_len = 0

        if float(self.data) < 0:
            # for minus character
            format_len += 1

        return format_len

    def __get_base_float_len(self):
        if any([self.integer_digits < 0, self.decimal_places < 0]):
            raise ValueError()

        float_len = self.integer_digits + self.decimal_places
        if self.decimal_places > 0:
            # for dot
            float_len += 1

        return float_len

    def __get_str_len(self):
        if self.typecode == Typecode.INT:
            return self.integer_digits + self.additional_format_len

        if self.typecode == Typecode.FLOAT:
            return self.__get_base_float_len() + self.additional_format_len

        return get_text_len(self.data)

    def __set_data(
            self, data, none_value, inf_value, nan_value, bool_converter, is_convert):
        special_value_table = {
            Typecode.NONE: none_value,
            Typecode.INFINITY: inf_value,
            Typecode.NAN: nan_value,
        }

        for type_factory in self.__type_factory_list:
            checker = type_factory.type_checker_factory.create(
                data, is_convert)
            if not checker.is_type():
                continue

            self.__typecode = checker.typecode

            special_value = special_value_table.get(self.__typecode)
            if special_value is not None:
                self.__data = special_value
                return

            self.__data = type_factory.value_converter_factory.create(
                data).convert()

            if self.typecode == Typecode.BOOL:
                self.__data = bool_converter(self.__data)

            return

        self.__typecode = Typecode.STRING
        self.__data = str(data)

    def __replace_tabs(self, replace_tabs_with_spaces, tab_length):
        if not replace_tabs_with_spaces:
            return

        try:
            self.__data = self.__data.replace("\t", " " * tab_length)
        except (TypeError, AttributeError):
            pass

class ColumnDataProperty(DataPeropertyInterface):
    __slots__ = (
        "__typecode_bitmap",
        "__str_len",
        "__minmax_integer_digits",
        "__minmax_decimal_places",
        "__minmax_additional_format_len",
    )

    @property
    def align(self):
        return align_getter.get_align_from_typecode(self.typecode)

    @property
    def decimal_places(self):
        try:
            avg = self.minmax_decimal_places.mean()
        except TypeError:
            return float("nan")

        if is_nan(avg):
            return float("nan")

        return int(math.ceil(avg))

    @property
    def typecode(self):
        return self.__get_typecode_from_bitmap(self.__typecode_bitmap)
        return self.__get_typecode_from_bitmap()

    @property
    def padding_len(self):
        return self.__str_len

    @property
    def minmax_integer_digits(self):
        return self.__minmax_integer_digits

    @property
    def minmax_decimal_places(self):
        return self.__minmax_decimal_places

    @property
    def minmax_additional_format_len(self):
        return self.__minmax_additional_format_len

    def __init__(self, min_padding_len=0):
        self.__typecode_bitmap = Typecode.NONE
        self.__str_len = min_padding_len
        self.__minmax_integer_digits = MinMaxContainer()
        self.__minmax_decimal_places = MinMaxContainer()
        self.__minmax_additional_format_len = MinMaxContainer()

    def __repr__(self):
        return ", ".join([
            "typename=" + Typecode.get_typename(self.typecode),
            "align=" + str(self.align),
            "padding_len=" + str(self.padding_len),
            "integer_digits=(%s)" % (str(self.minmax_integer_digits)),
            "decimal_places=(%s)" % (str(self.minmax_decimal_places)),
            "additional_format_len=(%s)" % (
                str(self.minmax_additional_format_len)),
        ])

    def update_header(self, dataprop):
        self.__update(dataprop)

    def update_body(self, dataprop):
        self.__typecode_bitmap |= dataprop.typecode

        if all([
            self.__typecode_bitmap & Typecode.DATETIME,
            self.__typecode_bitmap & ~Typecode.DATETIME,
        ]):
            self.__typecode_bitmap |= Typecode.STRING

        self.__update(dataprop)

    def __get_typecode_from_bitmap(self):
        typecode_list = [
            Typecode.STRING,
            Typecode.FLOAT,
            Typecode.INT,
            Typecode.DATETIME,
        ]

        for typecode in typecode_list:
            if self.__typecode_bitmap & typecode:
                return typecode

        return Typecode.STRING

    def __update(self, dataprop):
        self.__str_len = max(self.__str_len, dataprop.str_len)

        if dataprop.typecode in (Typecode.FLOAT, Typecode.INT):
            self.__minmax_integer_digits.update(dataprop.integer_digits)

        if dataprop.typecode == Typecode.FLOAT:
            self.__minmax_decimal_places.update(dataprop.decimal_places)

        self.__minmax_additional_format_len.update(
            dataprop.additional_format_len)
<commit_msg>Change __get_typecode_from_bitmap from static method to instance method<commit_after># encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import absolute_import
import math

from ._align_getter import align_getter
from ._container import MinMaxContainer
from ._interface import DataPeropertyInterface
from ._typecode import Typecode
from ._type_checker import FloatTypeChecker

from ._function import is_nan
from ._function import get_number_of_digit
from ._function import get_text_len

from ._factory import NoneTypeFactory
from ._factory import IntegerTypeFactory
from ._factory import FloatTypeFactory
from ._factory import BoolTypeFactory
from ._factory import DateTimeTypeFactory
from ._factory import InfinityTypeFactory
from ._factory import NanTypeFactory

def default_bool_converter(value):
    return value

class DataProperty(DataPeropertyInterface):
    __slots__ = (
        "__data",
        "__typecode",
        "__align",
        "__integer_digits",
        "__decimal_places",
        "__additional_format_len",
        "__str_len",
    )

    __type_factory_list = [
        NoneTypeFactory(),
        IntegerTypeFactory(),
        InfinityTypeFactory(),
        NanTypeFactory(),
        FloatTypeFactory(),
        BoolTypeFactory(),
        DateTimeTypeFactory(),
    ]

    @property
    def align(self):
        return self.__align

    @property
    def decimal_places(self):
        """
        :return:
            Decimal places if the ``data`` is ``float``.
            Returns ``0`` if the ``data`` is ``int``.
            Otherwise, returns ``float("nan")``.
        :rtype: int
        """

        return self.__decimal_places

    @property
    def typecode(self):
        """
        Return the type code that corresponds to the type of the ``data``.

        :return:
            One of the constants that are defined in the ``Typecode`` class.
        :rtype: int
        """

        return self.__typecode

    @property
    def format_str(self):
        if self.typecode == Typecode.INT:
            return "d"

        if self.typecode == Typecode.FLOAT:
            if is_nan(self.decimal_places):
                return "f"

            return ".%df" % (self.decimal_places)

        return "s"

    @property
    def data(self):
        """
        :return: Original data.
        :rtype: Original data type.
        """

        return self.__data

    @property
    def str_len(self):
        """
        :return: Length of the ``data`` as a string.
        :rtype: int
        """

        return self.__str_len

    @property
    def integer_digits(self):
        """
        :return:
            Integer digits if the ``data`` is ``int``/``float``.
            Otherwise, returns ``float("nan")``.
        :rtype: int
        """

        return self.__integer_digits

    @property
    def additional_format_len(self):
        return self.__additional_format_len

    def __init__(
            self, data,
            none_value=None, inf_value=float("inf"), nan_value=float("nan"),
            bool_converter=default_bool_converter,
            is_convert=True,
            replace_tabs_with_spaces=True, tab_length=2):
        super(DataProperty, self).__init__()

        self.__set_data(
            data, none_value, inf_value, nan_value, bool_converter, is_convert)
        self.__replace_tabs(replace_tabs_with_spaces, tab_length)
        self.__align = align_getter.get_align_from_typecode(self.typecode)

        try:
            integer_digits, decimal_places = get_number_of_digit(data)
        except OverflowError:
            integer_digits = float("nan")
            decimal_places = float("nan")
        self.__integer_digits = integer_digits
        self.__decimal_places = decimal_places
        self.__additional_format_len = self.__get_additional_format_len()
        self.__str_len = self.__get_str_len()

    def __repr__(self):
        return ", ".join([
            ("data=%" + self.format_str) % (self.data),
            "typename=" + Typecode.get_typename(self.typecode),
            "align=" + str(self.align),
            "str_len=" + str(self.str_len),
            "integer_digits=" + str(self.integer_digits),
            "decimal_places=" + str(self.decimal_places),
            "additional_format_len=" + str(self.additional_format_len),
        ])

    def __get_additional_format_len(self):
        if not FloatTypeChecker(self.data).is_type():
            return 0

        format_len = 0

        if float(self.data) < 0:
            # for minus character
            format_len += 1

        return format_len

    def __get_base_float_len(self):
        if any([self.integer_digits < 0, self.decimal_places < 0]):
            raise ValueError()

        float_len = self.integer_digits + self.decimal_places
        if self.decimal_places > 0:
            # for dot
            float_len += 1

        return float_len

    def __get_str_len(self):
        if self.typecode == Typecode.INT:
            return self.integer_digits + self.additional_format_len

        if self.typecode == Typecode.FLOAT:
            return self.__get_base_float_len() + self.additional_format_len

        return get_text_len(self.data)

    def __set_data(
            self, data, none_value, inf_value, nan_value, bool_converter, is_convert):
        special_value_table = {
            Typecode.NONE: none_value,
            Typecode.INFINITY: inf_value,
            Typecode.NAN: nan_value,
        }

        for type_factory in self.__type_factory_list:
            checker = type_factory.type_checker_factory.create(
                data, is_convert)
            if not checker.is_type():
                continue

            self.__typecode = checker.typecode

            special_value = special_value_table.get(self.__typecode)
            if special_value is not None:
                self.__data = special_value
                return

            self.__data = type_factory.value_converter_factory.create(
                data).convert()

            if self.typecode == Typecode.BOOL:
                self.__data = bool_converter(self.__data)

            return

        self.__typecode = Typecode.STRING
        self.__data = str(data)

    def __replace_tabs(self, replace_tabs_with_spaces, tab_length):
        if not replace_tabs_with_spaces:
            return

        try:
            self.__data = self.__data.replace("\t", " " * tab_length)
        except (TypeError, AttributeError):
            pass

class ColumnDataProperty(DataPeropertyInterface):
    __slots__ = (
        "__typecode_bitmap",
        "__str_len",
        "__minmax_integer_digits",
        "__minmax_decimal_places",
        "__minmax_additional_format_len",
    )

    @property
    def align(self):
        return align_getter.get_align_from_typecode(self.typecode)

    @property
    def decimal_places(self):
        try:
            avg = self.minmax_decimal_places.mean()
        except TypeError:
            return float("nan")

        if is_nan(avg):
            return float("nan")

        return int(math.ceil(avg))

    @property
    def typecode(self):
        return self.__get_typecode_from_bitmap()

    @property
    def padding_len(self):
        return self.__str_len

    @property
    def minmax_integer_digits(self):
        return self.__minmax_integer_digits

    @property
    def minmax_decimal_places(self):
        return self.__minmax_decimal_places

    @property
    def minmax_additional_format_len(self):
        return self.__minmax_additional_format_len

    def __init__(self, min_padding_len=0):
        self.__typecode_bitmap = Typecode.NONE
        self.__str_len = min_padding_len
        self.__minmax_integer_digits = MinMaxContainer()
        self.__minmax_decimal_places = MinMaxContainer()
        self.__minmax_additional_format_len = MinMaxContainer()

    def __repr__(self):
        return ", ".join([
            "typename=" + Typecode.get_typename(self.typecode),
            "align=" + str(self.align),
            "padding_len=" + str(self.padding_len),
            "integer_digits=(%s)" % (str(self.minmax_integer_digits)),
            "decimal_places=(%s)" % (str(self.minmax_decimal_places)),
            "additional_format_len=(%s)" % (
                str(self.minmax_additional_format_len)),
        ])

    def update_header(self, dataprop):
        self.__update(dataprop)

    def update_body(self, dataprop):
        self.__typecode_bitmap |= dataprop.typecode

        if all([
            self.__typecode_bitmap & Typecode.DATETIME,
            self.__typecode_bitmap & ~Typecode.DATETIME,
        ]):
            self.__typecode_bitmap |= Typecode.STRING

        self.__update(dataprop)

    def __get_typecode_from_bitmap(self):
        typecode_list = [
            Typecode.STRING,
            Typecode.FLOAT,
            Typecode.INT,
            Typecode.DATETIME,
        ]

        for typecode in typecode_list:
            if self.__typecode_bitmap & typecode:
                return typecode

        return Typecode.STRING

    def __update(self, dataprop):
        self.__str_len = max(self.__str_len, dataprop.str_len)

        if dataprop.typecode in (Typecode.FLOAT, Typecode.INT):
            self.__minmax_integer_digits.update(dataprop.integer_digits)

        if dataprop.typecode == Typecode.FLOAT:
            self.__minmax_decimal_places.update(dataprop.decimal_places)

        self.__minmax_additional_format_len.update(
            dataprop.additional_format_len)
<|endoftext|>


<commit_before># encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import absolute_import
import math

from ._align_getter import align_getter
from ._container import MinMaxContainer
from ._error import TypeConversionError
from ._interface import DataPeropertyInterface
from ._factory import (
    NoneTypeFactory,
    StringTypeFactory,
    IntegerTypeFactory,
    FloatTypeFactory,
    BoolTypeFactory,
    DateTimeTypeFactory,
    InfinityTypeFactory,
    NanTypeFactory
)
from ._function import (
    is_nan,
    get_number_of_digit,
    get_text_len
)
from ._typecode import Typecode
from ._type import FloatType

def default_bool_converter(value):
    return value

def default_datetime_converter(value):
    return value

class DataPeropertyBase(DataPeropertyInterface):
    __slots__ = ("__datetime_format_str")

    @property
    def typename(self):
        return Typecode.get_typename(self.typecode)

    @property
    def format_str(self):
        format_str = {
            Typecode.NONE: "",
            Typecode.INTEGER: "d",
            Typecode.BOOL: "",
            Typecode.DATETIME: self.__datetime_format_str,
        }.get(self.typecode)

        if format_str is not None:
            return format_str

        if self.typecode in (Typecode.FLOAT, Typecode.INFINITY, Typecode.NAN):
            if is_nan(self.decimal_places):
                return "f"

            return ".{:d}f".format(self.decimal_places)

        return "s"

    def __init__(self, datetime_format_str):
        self.__datetime_format_str = datetime_format_str

class DataProperty(DataPeropertyBase):
    __slots__ = (
        "__data",
        "__typecode",
        "__align",
        "__integer_digits",
        "__decimal_places",
        "__additional_format_len",
        "__str_len",
    )

    __type_factory_class_list = [
        NoneTypeFactory,
        IntegerTypeFactory,
        InfinityTypeFactory,
        NanTypeFactory,
        FloatTypeFactory,
        BoolTypeFactory,
        DateTimeTypeFactory,
        StringTypeFactory,
    ]

    @property
    def align(self):
        return self.__align

    @property
    def decimal_places(self):
        """
        :return:
            Decimal places if the ``data`` is ``float``.
            Returns ``0`` if the ``data`` is ``int``.
            Otherwise, returns ``float("nan")``.
        :rtype: int
        """

        return self.__decimal_places

    @property
    def typecode(self):
        """
        Return the type code that corresponds to the type of the ``data``.

        :return:
            One of the constants that are defined in the ``Typecode`` class.
        :rtype: int
        """

        return self.__typecode

    @property
    def data(self):
        """
        :return: Original data.
        :rtype: Original data type.
        """

        return self.__data

    @property
    def str_len(self):
        """
        :return: Length of the ``data`` as a string.
        :rtype: int
        """

        return self.__str_len

    @property
    def integer_digits(self):
        """
        :return:
            Integer digits if the ``data`` is ``int``/``float``.
            Otherwise, returns ``float("nan")``.
        :rtype: int
        """

        return self.__integer_digits

    @property
    def additional_format_len(self):
        return self.__additional_format_len

    def __init__(
            self, data,
            none_value=None, inf_value=float("inf"), nan_value=float("nan"),
            bool_converter=default_bool_converter,
            datetime_converter=default_datetime_converter,
            datetime_format_str="%Y-%m-%dT%H:%M:%S%z",
            is_strict=False,
            replace_tabs_with_spaces=True, tab_length=2):
        super(DataProperty, self).__init__(datetime_format_str)

        self.__set_data(data, none_value, inf_value, nan_value, is_strict)
        self.__convert_data(bool_converter, datetime_converter)
        self.__replace_tabs(replace_tabs_with_spaces, tab_length)
        self.__align = align_getter.get_align_from_typecode(self.typecode)

        try:
            integer_digits, decimal_places = get_number_of_digit(data)
        except OverflowError:
            integer_digits = float("nan")
            decimal_places = float("nan")
        self.__integer_digits = integer_digits
        self.__decimal_places = decimal_places
        self.__additional_format_len = self.__get_additional_format_len()
        self.__str_len = self.__get_str_len()

    def __repr__(self):
        element_list = []

        if self.typecode == Typecode.DATETIME:
            element_list.append(
                "data={:s}".format(str(self.data)))
        else:
            element_list.append(
                ("data={:" + self.format_str + "}").format(self.data))

        element_list.extend([
            "typename=" + self.typename,
            "align=" + str(self.align),
            "str_len=" + str(self.str_len),
            "integer_digits=" + str(self.integer_digits),
            "decimal_places=" + str(self.decimal_places),
            "additional_format_len=" + str(self.additional_format_len),
        ])

        return ", ".join(element_list)

    def __get_additional_format_len(self):
        if not FloatType(self.data).is_type():
            return 0

        format_len = 0

        if float(self.data) < 0:
            # for minus character
            format_len += 1

        return format_len

    def __get_base_float_len(self):
        if any([self.integer_digits < 0, self.decimal_places < 0]):
            raise ValueError()

        float_len = self.integer_digits + self.decimal_places
        if self.decimal_places > 0:
            # for dot
            float_len += 1

        return float_len

    def __get_str_len(self):
        if self.typecode == Typecode.INTEGER:
            return self.integer_digits + self.additional_format_len

        if self.typecode == Typecode.FLOAT:
            return self.__get_base_float_len() + self.additional_format_len

        if self.typecode == Typecode.DATETIME:
            full_format_str = "{:" + self.format_str + "}"
            return len(full_format_str.format(self.data))

        return get_text_len(self.data)

    def __set_data(
            self, data, none_value, inf_value, nan_value, is_strict):
        special_value_table = {
            Typecode.NONE: none_value,
            Typecode.INFINITY: inf_value,
            Typecode.NAN: nan_value,
        }

        for type_factory_class in self.__type_factory_class_list:
            type_factory = type_factory_class(data, is_strict)
            checker = type_factory.create_type_checker()

            if not checker.is_type():
                continue

            self.__typecode = checker.typecode

            special_value = special_value_table.get(self.__typecode)
            if special_value is not None:
                self.__data = special_value
                return

            self.__data = type_factory.create_type_converter().convert()

            return

        raise TypeConversionError("failed to convert: " + self.typename)

    def __convert_data(self, bool_converter, datetime_converter):
        if self.typecode == Typecode.BOOL:
            self.__data = bool_converter(self.__data)
        elif self.typecode == Typecode.DATETIME:
            self.__data = datetime_converter(self.__data)

    def __replace_tabs(self, replace_tabs_with_spaces, tab_length):
        if not replace_tabs_with_spaces:
            return

        try:
            self.__data = self.__data.replace("\t", " " * tab_length)
        except (TypeError, AttributeError):
            pass

class ColumnDataProperty(DataPeropertyBase):
    __slots__ = (
        "__typecode_bitmap",
        "__str_len",
        "__minmax_integer_digits",
        "__minmax_decimal_places",
        "__minmax_additional_format_len",
        "__data_prop_list",
    )

    __FACTORY_TABLE = {
        Typecode.NONE: NoneTypeFactory,
        Typecode.STRING: StringTypeFactory,
        Typecode.INTEGER: IntegerTypeFactory,
        Typecode.INFINITY: InfinityTypeFactory,
        Typecode.NAN: NanTypeFactory,
        Typecode.FLOAT: FloatTypeFactory,
        Typecode.BOOL: BoolTypeFactory,
        Typecode.DATETIME: DateTimeTypeFactory,
    }

    @property
    def align(self):
        return align_getter.get_align_from_typecode(self.typecode)

    @property
    def decimal_places(self):
        try:
            avg = self.minmax_decimal_places.mean()
        except TypeError:
            return float("nan")

        if is_nan(avg):
            return float("nan")

        return int(math.ceil(avg))

    @property
    def typecode(self):
        return self.__get_typecode_from_bitmap()

    @property
    def padding_len(self):
        if self.typecode != Typecode.FLOAT:
            return self.__str_len

        max_len = self.__str_len
        col_format_str = "{:" + self.format_str + "}"

        for data_prop in self.__data_prop_list:
            if data_prop.typecode in [Typecode.INFINITY, Typecode.NAN]:
                continue

            try:
                formatted_value = col_format_str.format(data_prop.data)
            except (TypeError, ValueError):
                continue

            max_len = max(max_len, len(formatted_value))

        return max_len

    @property
    def minmax_integer_digits(self):
        return self.__minmax_integer_digits

    @property
    def minmax_decimal_places(self):
        return self.__minmax_decimal_places

    @property
    def minmax_additional_format_len(self):
        return self.__minmax_additional_format_len

    """
    @property
    def type_factory(self):
        return self.__FACTORY_TABLE.get(self.typecode)
    """

    def __init__(
            self,
            min_padding_len=0,
            datetime_format_str="%Y-%m-%dT%H:%M:%S%z"):
        super(ColumnDataProperty, self).__init__(datetime_format_str)

        self.__typecode_bitmap = Typecode.NONE
        self.__str_len = min_padding_len
        self.__minmax_integer_digits = MinMaxContainer()
        self.__minmax_decimal_places = MinMaxContainer()
        self.__minmax_additional_format_len = MinMaxContainer()
        self.__data_prop_list = []

    def __repr__(self):
        return ", ".join([
            "typename=" + self.typename,
            "align=" + str(self.align),
            "padding_len=" + str(self.padding_len),
            "integer_digits=({:s})".format(str(self.minmax_integer_digits)),
            "decimal_places=({:s})".format(str(self.minmax_decimal_places)),
            "additional_format_len=({:s})".format(
                str(self.minmax_additional_format_len)),
        ])

    def update_header(self, dataprop):
        self.__str_len = max(self.__str_len, dataprop.str_len)

    def update_body(self, dataprop):
        self.__typecode_bitmap |= dataprop.typecode
        self.__str_len = max(self.__str_len, dataprop.str_len)

        if dataprop.typecode in (Typecode.FLOAT, Typecode.INTEGER):
            self.__minmax_integer_digits.update(dataprop.integer_digits)
            self.__minmax_decimal_places.update(dataprop.decimal_places)

        self.__minmax_additional_format_len.update(
            dataprop.additional_format_len)

        self.__data_prop_list.append(dataprop)

    def __is_not_single_typecode(self, typecode):
        return all([
            self.__typecode_bitmap & typecode,
            self.__typecode_bitmap & ~typecode,
        ])

    def __is_float_typecode(self):
        number_typecode = (
            Typecode.INTEGER | Typecode.FLOAT | Typecode.INFINITY | Typecode.NAN)

        if self.__is_not_single_typecode(number_typecode):
            return False

        if bin(self.__typecode_bitmap & number_typecode).count("1") >= 2:
            return True

        return False

    def __get_typecode_from_bitmap(self):
        if self.__is_float_typecode():
            return Typecode.FLOAT

        if any([
            self.__is_not_single_typecode(Typecode.BOOL),
            self.__is_not_single_typecode(Typecode.DATETIME),
        ]):
            return Typecode.STRING

        typecode_list = [
            Typecode.STRING,
            Typecode.FLOAT,
            Typecode.INTEGER,
            Typecode.DATETIME,
            Typecode.BOOL,
            Typecode.INFINITY,
            Typecode.NAN,
        ]

        for typecode in typecode_list:
            if self.__typecode_bitmap & typecode:
                return typecode

        if self.__typecode_bitmap == Typecode.NONE:
            return Typecode.NONE

        return Typecode.STRING
<commit_msg>Comment in<commit_after># encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import absolute_import
import math

from ._align_getter import align_getter
from ._container import MinMaxContainer
from ._error import TypeConversionError
from ._interface import DataPeropertyInterface
from ._factory import (
    NoneTypeFactory,
    StringTypeFactory,
    IntegerTypeFactory,
    FloatTypeFactory,
    BoolTypeFactory,
    DateTimeTypeFactory,
    InfinityTypeFactory,
    NanTypeFactory
)
from ._function import (
    is_nan,
    get_number_of_digit,
    get_text_len
)
from ._typecode import Typecode
from ._type import FloatType

def default_bool_converter(value):
    return value

def default_datetime_converter(value):
    return value

class DataPeropertyBase(DataPeropertyInterface):
    __slots__ = ("__datetime_format_str")

    @property
    def typename(self):
        return Typecode.get_typename(self.typecode)

    @property
    def format_str(self):
        format_str = {
            Typecode.NONE: "",
            Typecode.INTEGER: "d",
            Typecode.BOOL: "",
            Typecode.DATETIME: self.__datetime_format_str,
        }.get(self.typecode)

        if format_str is not None:
            return format_str

        if self.typecode in (Typecode.FLOAT, Typecode.INFINITY, Typecode.NAN):
            if is_nan(self.decimal_places):
                return "f"

            return ".{:d}f".format(self.decimal_places)

        return "s"

    def __init__(self, datetime_format_str):
        self.__datetime_format_str = datetime_format_str

class DataProperty(DataPeropertyBase):
    __slots__ = (
        "__data",
        "__typecode",
        "__align",
        "__integer_digits",
        "__decimal_places",
        "__additional_format_len",
        "__str_len",
    )

    __type_factory_class_list = [
        NoneTypeFactory,
        IntegerTypeFactory,
        InfinityTypeFactory,
        NanTypeFactory,
        FloatTypeFactory,
        BoolTypeFactory,
        DateTimeTypeFactory,
        StringTypeFactory,
    ]

    @property
    def align(self):
        return self.__align

    @property
    def decimal_places(self):
        """
        :return:
            Decimal places if the ``data`` is ``float``.
            Returns ``0`` if the ``data`` is ``int``.
            Otherwise, returns ``float("nan")``.
        :rtype: int
        """

        return self.__decimal_places

    @property
    def typecode(self):
        """
        Return the type code that corresponds to the type of the ``data``.

        :return:
            One of the constants that are defined in the ``Typecode`` class.
        :rtype: int
        """

        return self.__typecode

    @property
    def data(self):
        """
        :return: Original data.
        :rtype: Original data type.
        """

        return self.__data

    @property
    def str_len(self):
        """
        :return: Length of the ``data`` as a string.
        :rtype: int
        """

        return self.__str_len

    @property
    def integer_digits(self):
        """
        :return:
            Integer digits if the ``data`` is ``int``/``float``.
            Otherwise, returns ``float("nan")``.
        :rtype: int
        """

        return self.__integer_digits

    @property
    def additional_format_len(self):
        return self.__additional_format_len

    def __init__(
            self, data,
            none_value=None, inf_value=float("inf"), nan_value=float("nan"),
            bool_converter=default_bool_converter,
            datetime_converter=default_datetime_converter,
            datetime_format_str="%Y-%m-%dT%H:%M:%S%z",
            is_strict=False,
            replace_tabs_with_spaces=True, tab_length=2):
        super(DataProperty, self).__init__(datetime_format_str)

        self.__set_data(data, none_value, inf_value, nan_value, is_strict)
        self.__convert_data(bool_converter, datetime_converter)
        self.__replace_tabs(replace_tabs_with_spaces, tab_length)
        self.__align = align_getter.get_align_from_typecode(self.typecode)

        try:
            integer_digits, decimal_places = get_number_of_digit(data)
        except OverflowError:
            integer_digits = float("nan")
            decimal_places = float("nan")
        self.__integer_digits = integer_digits
        self.__decimal_places = decimal_places
        self.__additional_format_len = self.__get_additional_format_len()
        self.__str_len = self.__get_str_len()

    def __repr__(self):
        element_list = []

        if self.typecode == Typecode.DATETIME:
            element_list.append(
                "data={:s}".format(str(self.data)))
        else:
            element_list.append(
                ("data={:" + self.format_str + "}").format(self.data))

        element_list.extend([
            "typename=" + self.typename,
            "align=" + str(self.align),
            "str_len=" + str(self.str_len),
            "integer_digits=" + str(self.integer_digits),
            "decimal_places=" + str(self.decimal_places),
            "additional_format_len=" + str(self.additional_format_len),
        ])

        return ", ".join(element_list)

    def __get_additional_format_len(self):
        if not FloatType(self.data).is_type():
            return 0

        format_len = 0

        if float(self.data) < 0:
            # for minus character
            format_len += 1

        return format_len

    def __get_base_float_len(self):
        if any([self.integer_digits < 0, self.decimal_places < 0]):
            raise ValueError()

        float_len = self.integer_digits + self.decimal_places
        if self.decimal_places > 0:
            # for dot
            float_len += 1

        return float_len

    def __get_str_len(self):
        if self.typecode == Typecode.INTEGER:
            return self.integer_digits + self.additional_format_len

        if self.typecode == Typecode.FLOAT:
            return self.__get_base_float_len() + self.additional_format_len

        if self.typecode == Typecode.DATETIME:
            full_format_str = "{:" + self.format_str + "}"
            return len(full_format_str.format(self.data))

        return get_text_len(self.data)

    def __set_data(
            self, data, none_value, inf_value, nan_value, is_strict):
        special_value_table = {
            Typecode.NONE: none_value,
            Typecode.INFINITY: inf_value,
            Typecode.NAN: nan_value,
        }

        for type_factory_class in self.__type_factory_class_list:
            type_factory = type_factory_class(data, is_strict)
            checker = type_factory.create_type_checker()

            if not checker.is_type():
                continue

            self.__typecode = checker.typecode

            special_value = special_value_table.get(self.__typecode)
            if special_value is not None:
                self.__data = special_value
                return

            self.__data = type_factory.create_type_converter().convert()

            return

        raise TypeConversionError("failed to convert: " + self.typename)

    def __convert_data(self, bool_converter, datetime_converter):
        if self.typecode == Typecode.BOOL:
            self.__data = bool_converter(self.__data)
        elif self.typecode == Typecode.DATETIME:
            self.__data = datetime_converter(self.__data)

    def __replace_tabs(self, replace_tabs_with_spaces, tab_length):
        if not replace_tabs_with_spaces:
            return

        try:
            self.__data = self.__data.replace("\t", " " * tab_length)
        except (TypeError, AttributeError):
            pass

class ColumnDataProperty(DataPeropertyBase):
    __slots__ = (
        "__typecode_bitmap",
        "__str_len",
        "__minmax_integer_digits",
        "__minmax_decimal_places",
        "__minmax_additional_format_len",
        "__data_prop_list",
    )

    __FACTORY_TABLE = {
        Typecode.NONE: NoneTypeFactory,
        Typecode.STRING: StringTypeFactory,
        Typecode.INTEGER: IntegerTypeFactory,
        Typecode.INFINITY: InfinityTypeFactory,
        Typecode.NAN: NanTypeFactory,
        Typecode.FLOAT: FloatTypeFactory,
        Typecode.BOOL: BoolTypeFactory,
        Typecode.DATETIME: DateTimeTypeFactory,
    }

    @property
    def align(self):
        return align_getter.get_align_from_typecode(self.typecode)

    @property
    def decimal_places(self):
        try:
            avg = self.minmax_decimal_places.mean()
        except TypeError:
            return float("nan")

        if is_nan(avg):
            return float("nan")

        return int(math.ceil(avg))

    @property
    def typecode(self):
        return self.__get_typecode_from_bitmap()

    @property
    def padding_len(self):
        if self.typecode != Typecode.FLOAT:
            return self.__str_len

        max_len = self.__str_len
        col_format_str = "{:" + self.format_str + "}"

        for data_prop in self.__data_prop_list:
            if data_prop.typecode in [Typecode.INFINITY, Typecode.NAN]:
                continue

            try:
                formatted_value = col_format_str.format(data_prop.data)
            except (TypeError, ValueError):
                continue

            max_len = max(max_len, len(formatted_value))

        return max_len

    @property
    def minmax_integer_digits(self):
        return self.__minmax_integer_digits

    @property
    def minmax_decimal_places(self):
        return self.__minmax_decimal_places

    @property
    def minmax_additional_format_len(self):
        return self.__minmax_additional_format_len

    @property
    def type_factory(self):
        return self.__FACTORY_TABLE.get(self.typecode)

    def __init__(
            self,
            min_padding_len=0,
            datetime_format_str="%Y-%m-%dT%H:%M:%S%z"):
        super(ColumnDataProperty, self).__init__(datetime_format_str)

        self.__typecode_bitmap = Typecode.NONE
        self.__str_len = min_padding_len
        self.__minmax_integer_digits = MinMaxContainer()
        self.__minmax_decimal_places = MinMaxContainer()
        self.__minmax_additional_format_len = MinMaxContainer()
        self.__data_prop_list = []

    def __repr__(self):
        return ", ".join([
            "typename=" + self.typename,
            "align=" + str(self.align),
            "padding_len=" + str(self.padding_len),
            "integer_digits=({:s})".format(str(self.minmax_integer_digits)),
            "decimal_places=({:s})".format(str(self.minmax_decimal_places)),
            "additional_format_len=({:s})".format(
                str(self.minmax_additional_format_len)),
        ])

    def update_header(self, dataprop):
        self.__str_len = max(self.__str_len, dataprop.str_len)

    def update_body(self, dataprop):
        self.__typecode_bitmap |= dataprop.typecode
        self.__str_len = max(self.__str_len, dataprop.str_len)

        if dataprop.typecode in (Typecode.FLOAT, Typecode.INTEGER):
            self.__minmax_integer_digits.update(dataprop.integer_digits)
            self.__minmax_decimal_places.update(dataprop.decimal_places)

        self.__minmax_additional_format_len.update(
            dataprop.additional_format_len)

        self.__data_prop_list.append(dataprop)

    def __is_not_single_typecode(self, typecode):
        return all([
            self.__typecode_bitmap & typecode,
            self.__typecode_bitmap & ~typecode,
        ])

    def __is_float_typecode(self):
        number_typecode = (
            Typecode.INTEGER | Typecode.FLOAT | Typecode.INFINITY | Typecode.NAN)

        if self.__is_not_single_typecode(number_typecode):
            return False

        if bin(self.__typecode_bitmap & number_typecode).count("1") >= 2:
            return True

        return False

    def __get_typecode_from_bitmap(self):
        if self.__is_float_typecode():
            return Typecode.FLOAT

        if any([
            self.__is_not_single_typecode(Typecode.BOOL),
            self.__is_not_single_typecode(Typecode.DATETIME),
        ]):
            return Typecode.STRING

        typecode_list = [
            Typecode.STRING,
            Typecode.FLOAT,
            Typecode.INTEGER,
            Typecode.DATETIME,
            Typecode.BOOL,
            Typecode.INFINITY,
            Typecode.NAN,
        ]

        for typecode in typecode_list:
            if self.__typecode_bitmap & typecode:
                return typecode

        if self.__typecode_bitmap == Typecode.NONE:
            return Typecode.NONE

        return Typecode.STRING
<|endoftext|>


<commit_before>from threading import RLock

from weavelib.exceptions import ObjectAlreadyExists, ObjectNotFound

class BaseApplication(object):
    def __init__(self, name, app_id):
        self.name = name
        self.app_id = app_id

class SystemApplication(BaseApplication):
    pass

class Plugin(BaseApplication):
    def __init__(self, name, app_id, url):
        super(Plugin, self).__init__(name, app_id)
        self.url = url

class ApplicationRegistry(object):
    def __init__(self, apps=None):
        self.apps = {x: SystemApplication(x, y) for x, y in (apps or [])}
        self.apps_lock = RLock()

    def register_plugin(self, name, app_id, url):
        with self.apps_lock:
            if app_id in self.apps:
                raise ObjectAlreadyExists(name)

            plugin = Plugin(name, app_id, url)
            self.apps[app_id] = plugin

    def unregister_plugin(self, app_id):
        with self.apps_lock:
            try:
                self.apps.pop(app_id)
            except KeyError:
                raise ObjectNotFound(app_id)

    def get_app_info(self, app_id):
        with self.apps_lock:
            try:
                app = self.apps[app_id]
            except KeyError:
                raise ObjectNotFound(app_id)

        return {
            "app_name": app.name,
            "app_type": "plugin" if isinstance(app, Plugin) else "system",
            "app_id": app_id,
            "app_url": app.url if isinstance(app, Plugin) else None
        }
<commit_msg>Generate app_tokens in the ApplicationRegistry instead of being passed one from outside.<commit_after>from threading import RLock
from uuid import uuid4

from weavelib.exceptions import ObjectAlreadyExists, ObjectNotFound

class BaseApplication(object):
    def __init__(self, name, url, app_token):
        self.name = name
        self.url = url
        self.app_token = app_token

class SystemApplication(BaseApplication):
    pass

class Plugin(BaseApplication):
    pass

class ApplicationRegistry(object):
    def __init__(self, apps=None):
        self.apps_by_token = {}
        self.apps_lock = RLock()

        for name, url, token in (apps or []):
            self.apps_by_token[token] = SystemApplication(name, url, token)

    def register_plugin(self, name, url):
        with self.apps_lock:
            token = "app-token-" + str(uuid4())
            self.apps_by_token[token] = Plugin(name, url, token)
            return token

    def unregister_plugin(self, token):
        with self.apps_lock:
            try:
                self.apps_by_token.pop(token)
            except KeyError:
                raise ObjectNotFound(token)

    def get_app_info(self, app_token):
        with self.apps_lock:
            try:
                app = self.apps[app_token]
            except KeyError:
                raise ObjectNotFound(app_token)

        return {
            "app_name": app.name,
            "app_type": "plugin" if isinstance(app, Plugin) else "system",
            "app_url": app.url if isinstance(app, Plugin) else "WEAVE-ENV"
        }
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2015,2016 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
# Copyright © 2015,2016 Collabora Ltd
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

# FIXME: this is horrible code. horrible.
# Ideally we would want pandoc to rescue us ..

import sys
import os

dir_ = os.path.dirname(os.path.abspath(__file__))
sys.path.append (os.path.abspath(os.path.join(dir_, os.pardir, "src")))

import StringIO
from lxml import etree as ET
import subprocess

def db_to_md (content):
    with open ("tmpfile", 'w') as f:
        f.write (content)
    cmd = ['pandoc', '-s', '-f', 'docbook', '-t', 'markdown']
    cmd.append ("tmpfile")
    converted = subprocess.check_output (cmd)
    os.unlink ('tmpfile')
    return converted

def convert_file (filename, new_name, output):
    with open(os.devnull, 'w') as f:
        xincluded = subprocess.check_output (["xmllint", "--xinclude",
            filename], stderr=f)

    converted = db_to_md (xincluded)
    with open (os.path.join (output, new_name + ".markdown"), 'w') as f:
        f.write (converted)
    return os.path.join (output, new_name + ".markdown")

def create_symbol_file (section_node, name, output, ported_comment=None):
    name = '_'.join(name.split())
    child = section_node.getchildren()[-1]
    symbols = child.tail.split("\n")
    with open (os.path.join (output, name + ".markdown"), 'w') as f:
        f.write ("### %s\n\n" % name)

        if ported_comment:
            if ported_comment.short_description:
                f.write('%s\n' % ported_comment.short_description)

            f.write('\n%s\n\n' % ported_comment.description)

        for symbol in symbols:
            if symbol:
                f.write ("* [%s]()\n" % symbol)
    return os.path.join (output, name + ".markdown")

def parse_sgml_chapters (part, filename, path, level, sections, output,
        section_comments):
    out = ""
    out += "# %s\n\n" % part.find("title").text
    for elem in part.iterchildren ("{http://www.w3.org/2003/XInclude}include", "chapter"):
        if elem.tag == "{http://www.w3.org/2003/XInclude}include":
            nested_filename = elem.attrib["href"]
            title = os.path.basename (nested_filename)
            title = os.path.splitext (title)[0]
            if not title in sections:
                new_filename = convert_file (os.path.join (path,
                    nested_filename), title, output)
            else:
                section_node = sections[title]
                title_node = section_node.find("TITLE")
                file_node = section_node.find('FILE')
                if file_node is not None:
                    ported_comment = section_comments.get(file_node.text)
                else:
                    ported_comment = None
                if title_node is not None:
                    new_filename = create_symbol_file (sections[title],
                            title_node.text, output, ported_comment=ported_comment)
                    title = title_node.text
                else:
                    new_filename = create_symbol_file (sections[title], title,
                            output, ported_comment=ported_comment)
            out += "#### [%s](%s)\n\n" % (title, os.path.basename(new_filename))
        else:
            title = elem.find("title").text
            nested_filename = '_'.join(title.split()) + ".markdown"
            out += "### [%s](%s)\n" % (title, nested_filename)
            parse_sgml_chapters (elem, os.path.join (output,
                nested_filename), path, level + 1, sections, output,
                section_comments)

    with open (filename, 'w') as f:
        f.write (out)

def parse_sgml_parts (parent, path, sections, output, section_comments,
        index_name):
    out = ""
    for part in parent.findall ("part"):
        title = part.find("title").text
        filename = '_'.join(title.split()) + ".markdown"
        out += "### [%s](%s)\n\n" % (title, filename)

        parse_sgml_chapters (part, os.path.join (output,
            filename), path, 2, sections, output, section_comments)

    with open (os.path.join (output, index_name), 'w') as f:
        f.write (out)

def parse_sgml_book_chapters (parent, path, sections, output, section_comments,
        index_name):
    out = ""
    for part in parent.findall (".//chapter"):
        title = part.find("title").text
        filename = '_'.join(title.split()) + ".markdown"
        out += "### [%s](%s)\n\n" % (title, filename)

        parse_sgml_chapters (part, os.path.join (output,
            filename), path, 2, sections, output, section_comments)

    with open (os.path.join (output, index_name), 'w') as f:
        f.write (out)

def parse_sections(sections_file):
    sections_root = ET.parse (sections_file).getroot()
    sections = {}
    for section in sections_root.findall ('.//SECTION'):
        sections[section.find('FILE').text] = section

    return sections

def convert_to_markdown(sgml_path, sections_path, output, section_comments,
        index_name):
    prefix_map = {'xi': 'http://www.w3.org/2003/XInclude'}
    parser = ET.XMLParser(load_dtd=True, resolve_entities=False)

    root = ET.parse (sgml_path, parser=parser).getroot()

    sections = parse_sections(sections_path)
    path = os.path.dirname (os.path.abspath(sgml_path))
    if root.findall("part"):
        parse_sgml_parts (root, path, sections, output, section_comments,
                index_name)
    else:
        parse_sgml_book_chapters (root, path, sections, output,
                section_comments, index_name)
<commit_msg>transition-scripts: remove outdated script<commit_after><|endoftext|>


<commit_before># encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import absolute_import

class Typecode(object):
    NONE = 0
    INT = 1 << 0  # delete in the future
    INTEGER = 1 << 0
    FLOAT = 1 << 1
    STRING = 1 << 2
    DATETIME = 1 << 3
    INFINITY = 1 << 4
    NAN = 1 << 5
    BOOL = 1 << 6

    DEFAULT_TYPENAME_TABLE = {
        NONE:   "NONE",
        INT:    "INTEGER",
        FLOAT:  "FLOAT",
        INTEGER: "INTEGER",
        STRING: "STRING",
        DATETIME: "DATETIME",
        INFINITY: "INFINITY",
        NAN: "NAN",
        BOOL: "BOOL",
    }

    TYPENAME_TABLE = DEFAULT_TYPENAME_TABLE

    @classmethod
    def get_typename(cls, typecode):
        type_name = cls.TYPENAME_TABLE.get(typecode)
        if type_name is None:
            raise ValueError("unknown typecode: {}".format(typecode))

        return type_name
<commit_msg>Remove spaces<commit_after># encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

from __future__ import absolute_import

class Typecode(object):
    NONE = 0
    INT = 1 << 0  # delete in the future
    INTEGER = 1 << 0
    FLOAT = 1 << 1
    STRING = 1 << 2
    DATETIME = 1 << 3
    INFINITY = 1 << 4
    NAN = 1 << 5
    BOOL = 1 << 6

    DEFAULT_TYPENAME_TABLE = {
        NONE: "NONE",
        INT: "INTEGER",
        INTEGER: "INTEGER",
        FLOAT: "FLOAT",
        STRING: "STRING",
        DATETIME: "DATETIME",
        INFINITY: "INFINITY",
        NAN: "NAN",
        BOOL: "BOOL",
    }

    TYPENAME_TABLE = DEFAULT_TYPENAME_TABLE

    @classmethod
    def get_typename(cls, typecode):
        type_name = cls.TYPENAME_TABLE.get(typecode)
        if type_name is None:
            raise ValueError("unknown typecode: {}".format(typecode))

        return type_name
<|endoftext|>