Exemplo n.º 1
0
def main():
    t = Terminal()
    print(t.yellow('Fetching latest version...'))

    dl_link, version = get_latest_version()

    print(
        t.blue('Found CTRLib version ') + t.bold_blue(str(version)) +
        t.blue(' available at ') + t.bold_blue(dl_link))

    current_version = get_current_version()
    if current_version >= version:
        print(t.green('Repository already contains this version. Exiting.'))
        return
    print(t.yellow('Repository does not contain this version.'))
    print(t.yellow('Downloading...'))
    print(t.blue, end='')
    buffer = bytearray()
    with requests.get(dl_link, stream=True) as r:
        with tqdm(desc='CTRLib ' + str(version),
                  unit='B',
                  unit_scale=True,
                  unit_divisor=1024,
                  miniters=1,
                  file=sys.stdout) as progress_bar:
            for b in r.iter_content(chunk_size=None):
                progress_bar.update(len(b))
                buffer += b

    print(t.yellow('Unpacking...'))

    buffer_as_file = BytesIO(buffer)
    # pick out file name, then drop `.zip`
    folder_name = urlparse(dl_link).path.split('/')[-1][:-4]
    with ZipFile(buffer_as_file) as zipfile:
        ctr_java_version = CTR_JAVA / str(version)
        ctr_java_version.mkdir(exist_ok=True)

        unpack_zip_entry(zipfile,
                         name='java/lib/CTRE_Phoenix.jar',
                         to=(ctr_java_version /
                             ('ctrlib-java-' + str(version) + '.jar')))
        unpack_zip_entry(zipfile,
                         name='java/lib/CTRE_Phoenix-sources.jar',
                         to=(ctr_java_version /
                             ('ctrlib-java-' + str(version) + '-sources.jar')))

        so_file = _read_entry(zipfile, 'java/lib/libCTRE_PhoenixCCI.so')
        ctr_cpp_version = CTR_CPP / str(version)
        ctr_cpp_version.mkdir(exist_ok=True)
        zip_filename = (ctr_cpp_version /
                        ('ctrlib-cpp-' + str(version) + '-linuxathena.zip'))
        with ZipFile(zip_filename, mode='w') as cppzip:
            cppzip.writestr('libCTRE_PhoenixCCI.so', so_file)
    print(t.green('Unpacked!'))
Exemplo n.º 2
0
 def notify(self, tag: str, speaker: str, value):
     term = Terminal()
     if tag == 'board':
         self.show_board(value)
     elif tag == 'status':
         print('[%s] %s' % (speaker, value), file=self.stream)
     elif tag == 'winner':
         name = value.name.title()
         print('%s wins.' % name, file=self.stream)
     elif tag == 'clue':
         number, word = value
         print('[%s] Clue: %s %d' % (speaker, word, number),
               file=self.stream)
     elif tag == 'reveal':
         word, team = value
         if team is Team.red:
             shown_category = term.red('red')
         elif team is Team.blue:
             shown_category = term.blue('blue')
         elif team is Team.neutral:
             shown_category = term.yellow('neutral')
         elif team is Team.assassin:
             shown_category = term.reverse('the assassin')
         else:
             raise ValueError(team)
         status = '%s is %s.' % (word, shown_category)
         print('[%s] %s' % (speaker, status), file=self.stream)
     else:
         print('[%s] %s: %s' % (speaker, tag, value), file=self.stream)
     self.stream.flush()
Exemplo n.º 3
0
def set_str_format(in_string, FORMAT=None):
    ''' Setup a string with a specific format

    :param str in_string: string to be formated.
    :param str format: format to be applied.
    :return: formated string.
    :rtype: str.

    .. note:: valid formats are: Warning, Error, OkGreen and OkBlue
    '''
    printOut = Terminal()
    out_string = None
    if FORMAT is None:
        out_string = in_string
    elif FORMAT == 'Warning':
        out_string = printOut.yellow(in_string)
    elif FORMAT == 'Error':
        out_string = printOut.red(in_string)
    elif FORMAT == 'OkGreen':
        out_string = printOut.green(in_string)
    elif FORMAT == 'OkBlue':
        out_string = printOut.blue(in_string)
    elif FORMAT == 'OkCyan':
        out_string = printOut.cyan(in_string)

    return out_string
Exemplo n.º 4
0
def criar_disciplinas_medicina(sender, **kwargs):
    from servicos.models import Disciplina, Setor

    disciplinas = [
        'Prática em Clínica Cirurgica I',
        'Prática em Clínica Cirurgica II',
        'Prática em Clínica Cirurgica III',
    ]
    setor = Setor.objects.get(descricao='Medicina')

    for disciplina in disciplinas:
        Disciplina.objects.get_or_create(setor=setor, descricao=disciplina)

    t = Terminal()
    print (t.blue('\nAs seguintes disciplinas de Medicina foram criadas:'))
    for disciplina in Disciplina.objects.filter(setor__descricao='Medicina'):
        print (t.blue('  - ' + disciplina.descricao))
Exemplo n.º 5
0
def criar_disciplinas_nutricao(sender, **kwargs):
    from servicos.models import Disciplina, Setor

    disciplinas = [
        'Estágio Supervisonado I',
        'Estágio Supervisonado II',
        'Estágio Supervisonado III',
    ]
    setor = Setor.objects.get(descricao='Nutrição')

    for disciplina in disciplinas:
        Disciplina.objects.get_or_create(setor=setor, descricao=disciplina)

    t = Terminal()
    print (t.blue('\nAs seguintes disciplinas de Nutrição foram criadas:'))
    for disciplina in Disciplina.objects.filter(setor__descricao='Nutrição'):
        print (t.blue('  - ' + disciplina.descricao))
Exemplo n.º 6
0
def criar_disciplinas_psicologia(sender, **kwargs):
    from servicos.models import Disciplina, Setor

    disciplinas = [
        'Psicologia Social I',
        'Psicologia Social II',
        'Psicologia Social III',
    ]
    setor = Setor.objects.get(descricao='Psicologia')

    for disciplina in disciplinas:
        Disciplina.objects.get_or_create(setor=setor, descricao=disciplina)

    t = Terminal()
    print (t.blue('\nAs seguintes disciplinas de Psicologia foram criadas:'))
    for disciplina in Disciplina.objects.filter(setor__descricao='Psicologia'):
        print (t.blue('  - ' + disciplina.descricao))
Exemplo n.º 7
0
def criar_disciplinas_fisioterapia(sender, **kwargs):
    from servicos.models import Disciplina, Setor

    disciplinas = [
        'Estágio Geriatria',
        'Estágio Neurologia Adulto',
        'Estágio Neurologia Infantil',
        'Estágio Ortopedia',
        'Estágio Uroginecologia',
    ]
    setor = Setor.objects.get(descricao='Fisioterapia')

    for disciplina in disciplinas:
        Disciplina.objects.get_or_create(setor=setor, descricao=disciplina)

    t = Terminal()
    print (t.blue('\nAs seguintes disciplinas de Fisioterapia foram criadas:'))
    for disciplina in Disciplina.objects.filter(setor__descricao='Fisioterapia'):
        print (t.blue('  - ' + disciplina.descricao))
Exemplo n.º 8
0
def main():
    t = Terminal() # for colors
    memory = {}
    tmp = driver()
    data = tmp.getPrice()

    memory['my_usd'] = data['my_usd']
    memory['my_btc'] = data['my_btc']

    while True:
        if memory['my_usd'] == data['my_usd'] and memory['my_btc'] == data['my_btc']:
            print '{t.green}No Change{t.normal}:'.format(t=t),' My USD:', t.green(str(data['my_usd'])), ' My BTC:', t.green(str(data['my_btc']))
            pass
        else:
            if memory['my_usd'] > data['my_usd']:
                print t.green('Made Sale!')
                db.db.transhistory.insert({'previous':str(memory['my_usd']),
                                           'current':str(data['my_usd']),
                                           'action':'Sell'
                                           })
                                        
            else:
                print t.blue('Made Buy!')
                db.db.transhistory.insert({'previous':str(memory['my_usd']),
                                           'current':str(data['my_usd']),
                                           'action':'Buy'
                                           })
            
            memory['my_usd'] = data['my_usd']
            memory['my_btc'] = data['my_btc']

        print t.bold_blue('Current Price:'), t.bold_blue(str(data['buy']))
        time.sleep(10)

        try:
            data = tmp.getPrice()
        except:
            time.sleep(30)
            try:
                data = tmp.getPrice()
            except:
                pass
Exemplo n.º 9
0
def ask(choices):
    t = Terminal()
    for idx, item in enumerate(choices):
        num = t.red(str(idx+1).rjust(2))
        title = item['title'].decode('utf-8')
        title = t.white((title[:80] + (title[80:] and '..')).ljust(82)) # truncate
        date = t.yellow(item['date'].rjust(10))
        size = t.cyan(item['size'].rjust(10))
        seed = t.blue(item['seed'].rjust(6))
        peer = t.green(item['peer'].rjust(6))
        print '%s. %s %s %s %s %s' % (num, title, date, size, seed, peer)
    answers = raw_input('What items do you want? (seperated by commas) [1] ')
    if answers: return map(lambda x: int(x)-1, answers.split(r','))
    else: return[0]
Exemplo n.º 10
0
 def output(self, message, level="info"):
     """Print a message in the info format."""
     t = Terminal()
     if len(message) == 0:
         raise RuntimeError("No message supplied")
     # Get timestamp
     timestamp = t.white("{:%Y-%m-%d %H:%M:%S} ".format(datetime.datetime.now()))
     # Check level
     if level == "debug":
         prefix = t.blue("DEBUG: ")
     elif level == "info":
         prefix = t.green("INFO:  ")
     elif level == "warn":
         prefix = t.yellow("WARN:  ")
     elif level == "error":
         prefix = t.red("ERROR: ")
     else:
         raise RuntimeError("That level is not supported!")
     # Combine timestamp, refix and message
     print(timestamp + prefix + message)
     return
Exemplo n.º 11
0
 def show_board(self, items: List[Tuple[str, Team]]):
     term = Terminal()
     for i, (word, team) in enumerate(items):
         jword = word[:11]
         if team is Team.unknown:
             print(justify(jword), end='', file=self.stream)
         elif team is Team.red:
             print(term.red(justify(jword + ' [r]')),
                   end='',
                   file=self.stream)
         elif team is Team.blue:
             print(term.blue(justify(jword + ' [b]')),
                   end='',
                   file=self.stream)
         elif team is Team.neutral:
             print(term.yellow(justify(jword + ' [n]')),
                   end='',
                   file=self.stream)
         elif team is Team.assassin:
             print(term.reverse(justify(jword + ' [a]')),
                   end='',
                   file=self.stream)
         if i % 5 == 4:
             print('\n', file=self.stream)
Exemplo n.º 12
0
class LittleBrain():
    def __init__(self):
        self.t = Terminal()
        self.kernel = aiml.Kernel()
        self.analyzer = SentimentIntensityAnalyzer()
        if os.path.isfile("bot_brain.brn"):
            self.kernel.bootstrap(brainFile="bot_brain.brn")
        else:
            self.kernel.bootstrap(
                learnFiles=os.path.abspath("aiml/std-startup.xml"),
                commands="load aiml b")
            self.kernel.saveBrain("bot_brain.brn")

        for key, val in LB_FACTS.items():
            self.kernel.setBotPredicate(key, val)

    def talk(self):
        ## clears the terminal
        os.system('cls' if os.name == 'nt' else 'clear')

        while True:
            message = input(">> ")
            ## add spell check to user input to mitigate some faulty aiml responses
            cleanedmessage = " ".join([spell(w) for w in (message.split())])
            mood = self.score_input_sentiment(cleanedmessage)

            if cleanedmessage == "quit" or cleanedmessage == "exit" or cleanedmessage == "bye" or cleanedmessage == "gtg" or cleanedmessage == "ok bye":  # exit from chat
                print(self.t.blue("k bye..."))
                exit()
            elif cleanedmessage == "save" or message == "!save":
                self.kernel.saveBrain("bot_brain.brn")
            elif cleanedmessage == "help":
                helper.Features()
            ## need to use 'message' here because the spellchecker strips special chars
            elif message[:1] == "!":
                split_message = message.split()
                cmds.Commands(split_message)
            else:
                bot_response = self.kernel.respond(cleanedmessage)
                # self.improve_bot_response(bot_response)
                if bot_response == "":
                    bot_response = "no comment"

                if mood is None:
                    print(self.t.blue(bot_response).lower().strip())
                else:
                    print(mood, " ", self.t.blue(bot_response).lower().strip())

    def score_input_sentiment(self, message):
        sentiment_compound = self.analyzer.polarity_scores(message)['compound']
        mood = None

        if sentiment_compound <= -0.5:
            mood = emoji.emojize(':confounded:', use_aliases=True)
        elif sentiment_compound > -0.5 and sentiment_compound < 0:
            pass
            ## mood = emoji.emojize(':cold_sweat:', use_aliases=True)
        elif sentiment_compound == 0:
            pass
        elif sentiment_compound > 0 and sentiment_compound < 0.5:
            mood = emoji.emojize(':grin:', use_aliases=True)
        else:
            mood = emoji.emojize(':smile:', use_aliases=True)

        return mood

    def improve_bot_response(self, response):
        pass  ## TODO
Exemplo n.º 13
0
 wall1 = Wall()
 arr = wall1.buildwall(arr)     # populate the array with the wall.
 bricks = Brick(t.cyan('/'))
 arr = bricks.place(arr)       # populate the array with the bricks.
 bomber = Bomberman(t.green('B'))
 num = randint(3*level, 4*level)
 enarr = []
 var = False
 for i in range(num):
     enarr.append(Enemy(t.red('E')))         # enarr is populated with all the enemies.
 arr = bomber.spawn(arr)             # bomberman is spawned.
 for i in range(0, num):
     arr = enarr[i].randspawn(arr)           # all the enemies are randomly spawned.
 if level == 1:
     lives = 3
 print t.blue('SCORE :- '), SCORE, t.blue('LIVES :- '), lives, t.blue('LEVEL :- '), level, t.yellow('\t\tBOMBERMAN\n')
 for i in range(38):
     print ''.join(arr[i])
 while lives:        # loop terminates when user has no lives left.
     flag = 0
     time.sleep(.2)
     char = getch()          # get the input from the user.
     if char == 'q':         # 'q' terminates the game.
         qflag = 1
         break
     elif char != 'b':
         arr = bomber.move(arr, char)
         # time.sleep()
         for i in range(num):
             arr = enarr[i].move(arr)
             if enarr[i].killed():
Exemplo n.º 14
0
def run():

    print ('[' + T + '*' + W + '] Starting Wifiphisher %s at %s' %
           (VERSION, time.strftime("%Y-%m-%d %H:%M")))

    # Parse args
    global args, APs, clients_APs, mon_MAC, mac_matcher, hop_daemon_running
    args = parse_args()

    # Check args
    check_args(args)

    # Are you root?
    if os.geteuid():
        sys.exit('[' + R + '-' + W + '] Please run as root')

    # TODO: We should have more checks here:
    # Is anything binded to our HTTP(S) ports?
    # Maybe we should save current iptables rules somewhere

    network_manager = interfaces.NetworkManager()

    mac_matcher = macmatcher.MACMatcher(MAC_PREFIX_FILE)

    # get interfaces for monitor mode and AP mode and set the monitor interface
    # to monitor mode. shutdown on any errors
    try:
        if not args.nojamming:
            if args.jamminginterface and args.apinterface:
                mon_iface = network_manager.get_jam_iface(
                    args.jamminginterface)
                ap_iface = network_manager.get_ap_iface(args.apinterface)
            else:
                mon_iface, ap_iface = network_manager.find_interface_automatically()
            network_manager.set_jam_iface(mon_iface.get_name())
            network_manager.set_ap_iface(ap_iface.get_name())
            # display selected interfaces to the user
            print ("[{0}+{1}] Selecting {0}{2}{1} interface for the deauthentication "
                   "attack\n[{0}+{1}] Selecting {0}{3}{1} interface for creating the "
                   "rogue Access Point").format(G, W, mon_iface.get_name(), ap_iface.get_name())
        else:
            if args.apinterface:
                ap_iface = network_manager.get_ap_iface(
                    interface_name=args.apinterface)
            else:
                ap_iface = network_manager.get_ap_iface()
            mon_iface = ap_iface
            network_manager.set_ap_iface(ap_iface.get_name())
            print ("[{0}+{1}] Selecting {0}{2}{1} interface for creating the "
                   "rogue Access Point").format(G, W, ap_iface.get_name())

        kill_interfering_procs()

        # set monitor mode to monitor interface
        network_manager.set_interface_mode(mon_iface, "monitor")
    except (interfaces.NotEnoughInterfacesFoundError,
            interfaces.JammingInterfaceInvalidError,
            interfaces.ApInterfaceInvalidError,
            interfaces.NoApInterfaceFoundError,
            interfaces.NoMonitorInterfaceFoundError) as err:
        print ("[{0}!{1}] " + str(err)).format(R, W)
        time.sleep(2)
        shutdown()

    set_fw_rules()
    set_kernel_var()
    network_manager.up_ifaces([ap_iface, mon_iface])

    print '[' + T + '*' + W + '] Cleared leases, started DHCP, set up iptables'

    if args.essid:
        essid = args.essid
        channel = str(CHANNEL)
        args.accesspoint = False
        args.channel = False
        ap_mac = None
        enctype = None
    else:
        # Copy AP
        time.sleep(3)
        hop = Thread(target=channel_hop, args=(mon_iface,))
        hop.daemon = True
        hop.start()
        sniffing(mon_iface.get_name(), targeting_cb)
        channel, essid, ap_mac, enctype = copy_AP()
        args.accesspoint = ap_mac
        args.channel = channel
        hop_daemon_running = False

    # get the correct template
    template = select_template(args.phishingscenario)

    print ("[" + G + "+" + W + "] Selecting " + template.get_display_name() +
           " template")

    # payload selection for browser plugin update
    if template.has_payload():
        payload_path = False
        # copy payload to update directory
        while not payload_path or not os.path.isfile(payload_path):
            # get payload path
            payload_path = raw_input("[" + G + "+" + W +
                                     "] Enter the [" + G + "full path" + W +
                                     "] to the payload you wish to serve: ")
            if not os.path.isfile(payload_path):
                print '[' + R + '-' + W + '] Invalid file path!'
        print '[' + T + '*' + W + '] Using ' + G + payload_path + W + ' as payload '
        copyfile(payload_path, PHISHING_PAGES_DIR +
                 template.get_payload_path())

    APs_context = []
    for i in APs:
        APs_context.append({
            'channel': APs[i][0] or "",
            'essid': APs[i][1] or "",
            'bssid': APs[i][2] or "",
            'vendor': mac_matcher.get_vendor_name(APs[i][2]) or ""
        })

    template.merge_context({'APs': APs_context})

    ap_logo_path = template.use_file(mac_matcher.get_vendor_logo_path(ap_mac))

    template.merge_context({
        'target_ap_channel': args.channel or "",
        'target_ap_essid': essid or "",
        'target_ap_bssid': ap_mac or "",
        'target_ap_encryption': enctype or "",
        'target_ap_vendor': mac_matcher.get_vendor_name(ap_mac) or "",
        'target_ap_logo_path': ap_logo_path or ""
    })

    # We want to set this now for hostapd. Maybe the interface was in "monitor"
    # mode for network discovery before (e.g. when --nojamming is enabled).
    network_manager.set_interface_mode(ap_iface, "managed")
    # Start AP
    start_ap(ap_iface.get_name(), channel, essid, args)
    dhcpconf = dhcp_conf(ap_iface.get_name())
    if not dhcp(dhcpconf, ap_iface.get_name()):
        print('[' + G + '+' + W +
              '] Could not set IP address on %s!' % ap_iface.get_name()
              )
        shutdown(template)
    subprocess.call('clear', shell=True)
    print ('[' + T + '*' + W + '] ' + T +
           essid + W + ' set up on channel ' +
           T + channel + W + ' via ' + T + mon_iface.get_name() +
           W + ' on ' + T + str(ap_iface.get_name()) + W)

    # With configured DHCP, we may now start the web server
    # Start HTTP server in a background thread
    print '[' + T + '*' + W + '] Starting HTTP/HTTPS server at ports ' + str(PORT) + ", " + str(SSL_PORT)
    webserver = Thread(target=phishinghttp.runHTTPServer,
                       args=(NETWORK_GW_IP, PORT, SSL_PORT, template))
    webserver.daemon = True
    webserver.start()

    time.sleep(1.5)

    # We no longer need mac_matcher
    mac_matcher.unbind()

    clients_APs = []
    APs = []
    monitor_on = None
    conf.iface = mon_iface.get_name()
    mon_MAC = mon_mac(mon_iface.get_name())

    if not args.nojamming:
        monchannel = channel
        # Start channel hopping
        hop = Thread(target=channel_hop2, args=(mon_iface,))
        hop.daemon = True
        hop.start()

    # Start sniffing
    sniff_thread = Thread(target=sniff_dot11, args=(mon_iface.get_name(),))
    sniff_thread.daemon = True
    sniff_thread.start()

    # Main loop.
    try:
        term = Terminal()
        with term.fullscreen():
            while 1:
                term.clear()
                with term.hidden_cursor():
                    print term.move(0, term.width - 30) + "|"
                    print term.move(1, term.width - 30) + "|" + " " + term.bold_blue("Wifiphisher " + VERSION)
                    print term.move(2, term.width - 30) + "|" + " ESSID: " + essid
                    print term.move(3, term.width - 30) + "|" + " Channel: " + channel
                    print term.move(4, term.width - 30) + "|" + " AP interface: " + mon_iface.get_name() 
                    print term.move(5, term.width - 30) + "|" + "_"*29 
                    print term.move(1,0) + term.blue("Jamming devices: ")
                    if os.path.isfile('/tmp/wifiphisher-jammer.tmp'):
                        proc = check_output(['tail', '-5', '/tmp/wifiphisher-jammer.tmp'])
                        print term.move(4,0) + proc 
                    print term.move(9,0) + term.blue("DHCP Leases: ")
                    if os.path.isfile('/var/lib/misc/dnsmasq.leases'):
                        proc = check_output(['tail', '-5', '/var/lib/misc/dnsmasq.leases'])
                        print term.move(10,0) + proc
                    print term.move(17,0) + term.blue("HTTP requests: ")
                    if os.path.isfile('/tmp/wifiphisher-webserver.tmp'):
                        proc = check_output(['tail', '-5', '/tmp/wifiphisher-webserver.tmp'])
                        print term.move(18,0) + proc
                    if phishinghttp.terminate and args.quitonsuccess:
                        raise KeyboardInterrupt
    except KeyboardInterrupt:
        shutdown(template, network_manager)
from blessings import Terminal

import numpy as np
from sklearn.preprocessing import OneHotEncoder
from sklearn import cross_validation
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB

t = Terminal()

dataset = np.genfromtxt('balance-scale.data', delimiter=',', dtype=str)
#print(len(dataset))
n_samples = dataset.shape[0]
n_features = dataset.shape[1]
print(t.blue("Balance Scale dataset: %d amostras(%d características)" % (dataset.shape[0], n_features)))

def get_indexes_feature_equals(values_dict, rows = np.arange(len(dataset))):
	idxs = rows
	for idx_feature, value in values_dict.items():
		#x  = labels_encoders[idx_feature].transform([value])[0]
		x = value
		idxs = np.where(dataset[idxs,idx_feature] == x)[0]
	return idxs
	
def get_probability(value, given_value={}, rows = np.arange(len(dataset))):
	d = dict(list(value.items()) + list(given_value.items()))
	val = float(len(get_indexes_feature_equals(d, rows)))/len(get_indexes_feature_equals(given_value, rows))
	#print(len(get_indexes_feature_equals(d, rows)),len(get_indexes_feature_equals(given_value, rows)))
	str1 = ', '.join("{!s}={!r}".format(key,val) for (key,val) in value.items())
	str2 = ', '.join("{!s}={!r}".format(key,val) for (key,val) in given_value.items())
Exemplo n.º 16
0
config.read('storage.conf')
accName = config.get('storage_account', 'accName')
accKey = config.get('storage_account', 'accKey')
queueName = config.get('storage_account', 'queueName')

# sudo pip install blessings
from azure.storage.queue import QueueService, QueueMessageFormat
from blessings import Terminal
import sys
import time

queue_service = QueueService(account_name=accName, account_key=accKey)
queue_service.decode_function = QueueMessageFormat.text_base64decode

t = Terminal()
print(t.green('Connected to Azure Storage Queue ' + queueName + '...'))
# Get approximate number of messages in queue
queue_metadata = queue_service.get_queue_metadata(queueName)
count = queue_metadata.approximate_message_count
print('Approximate number of messages in queue: ', count, '\n')

while True:
    messages = queue_service.get_messages(queueName)
    if messages:
        # Get the next message
        for message in messages:
            print(t.bold_yellow(message.content))
            queue_service.delete_message(queueName, message.id, message.pop_receipt)
            print(t.blue('-' * 40 + '\n'))
        time.sleep(4)
Exemplo n.º 17
0
    # Thumbnail size
    '-thumbnail', '1500x1500',

    # Input directory
    photos_path + os.sep + 'source-latest/*.jpg',
]
(mogrify[mogrify_expanded_arguments])()

"""
Write image dimensions to a csv to make it quicker to load them into
the final application.

We write this into the MongoDB database so that we don't need to examine
each file on load
"""
print term.blue('Creating dimensions.csv')
identify_thumbnail_arguments = [
    # Write the filename (without extension, which matches the _id in Mongo),
    # width, height, and the aspect ratio for each image
    '-format', '%t,%w,%h,%[fx:w/h]\n',
    # Output
    photos_path + os.sep + 'processed-latest/thumbnails/*.jpg'
]
temp_thumb_dimensions = data_path + os.sep + 'temp-thumbnail-dimensions.csv'
(identify[identify_thumbnail_arguments] > temp_thumb_dimensions)()
identify_expanded_arguments = [
    # Write the filename (without extension, which matches the _id in Mongo),
    # width, height, and the aspect ratio for each image
    '-format', '%t,%w,%h,%[fx:w/h]\n',
    # Output
    photos_path + os.sep + 'processed-latest/expanded/*.jpg'
        "Conflicting ipv4 A records detected, site will not be reachable unless you update DNS records with the new server's IP which will cause later steps to fail (the LetsEncrypt cert generation)"
    )
    answerIPV4 = str(
        raw_input('Update ipv4 records? y/n (lowercase y or n): '))
    print term.yellow(
        "Conflicting ipv6 AAA records detected, site will not be reachable unless you update DNS records with the new server's IP which will cause later steps to fail (the LetsEncrypt cert generation)"
    )
    answerIPV6 = str(raw_input('Update ipv6 records? y/n (lowercase y/n): '))
    deleteRecords(ipv4DNSRecordExists, answerIPV4, zone, ipv4Record,
                  CLOUDFLARE_EMAIL, CLOUDFLARE_AUTH_KEY,
                  ipv4wwwDNSRecordExists, ipv4wwwRecord, answerIPV6,
                  ipv6DNSRecordExists, ipv6Record, ipv6wwwRecord,
                  ipv6wwwDNSRecordExists)

# Add Cloudflare records
print term.blue("Adding records to Cloudflare")
cf = CloudFlare(CLOUDFLARE_EMAIL, CLOUDFLARE_AUTH_KEY)
cf.create_dns_record('@', domain, ipv4)
cf.create_dns_record('www', domain, ipv4)
cf.create_dns_record('@', domain, ipv6, record_type="AAAA")
cf.create_dns_record('www', domain, ipv6, record_type="AAAA")
print term.green("Cloudflare records added successfully")

f = open("./ips.txt", 'w')
f.write("ipv4=" + ipv4 + '\n' + "ipv6=" + ipv6)
f.close()

print term.blue("Writing ipv4 to Ansible's host file for provisioning.")

f = open("./ansible/hosts", 'w')
f.write("[VPS]" + '\n' + ipv4)
Exemplo n.º 19
0
def main(run_path, argv):
    cmd = None
    p = None
    retcode = 0
    t = Terminal()

    try:
        opts, args = getopt.getopt(argv, "c:hv", ["help", "cmd=", "version"])
    except getopt.GetoptError:
        print __doc__
        return 2
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            print __doc__
            return 0
        elif opt in ("-v", "--version"):
            print "uwsgi console %s" % VERSION
            return 0
        elif opt in ("-c", "--cmd"):
            cmd = arg

    cmd = args[0]

    # start single portal2
    bufsize = 1024
    if (cmd == "start"):
        # verify if service already running
        try:
            service = args[1]
            # read pid from file
            pid_file = file('%s/run/%s.pid' % (run_path, service))
            print t.yellow("Service %s is already running" % service)
            return 0
        except:
            try:
                command = [
                    'uwsgi', '--ini',
                    '%s/etc/%s.ini' % (run_path, service)
                ]
                print t.blue("Start uwsgi instance : %s" % service)
                retcode = run_command(command)
            except:
                traceback.print_exc(file=sys.stdout)
    elif (cmd == "stop"):
        try:
            service = args[1]
            command = [
                'uwsgi', '--stop',
                '%s/run/%s.pid' % (run_path, service)
            ]
            print t.blue("Stop uwsgi portal2 : %s" % service)
            retcode = run_command(command)
        except:
            traceback.print_exc(file=sys.stdout)
    elif (cmd == "kill"):
        try:
            service = args[1]
            pid = None
            for p in psutil.process_iter():
                if p.name == 'uwsgi' and p.cmdline == [service
                                                       ] and p.ppid == 1:
                    pid = p.pid
                    print(p.name, p.cmdline, p.pid, p.ppid)
            if pid is None:
                print t.yellow("Service %s is not running" % service)
                return 0

            command = ['kill', '-9', str(pid)]
            print t.red("Kill uwsgi instance : %s" % service)
            retcode = run_command(command)
        except:
            traceback.print_exc(file=sys.stdout)
    elif (cmd == "reload"):
        try:
            service = args[1]
            command = [
                'uwsgi', '--reload',
                '%s/run/%s.pid' % (run_path, service)
            ]
            print t.yellow("Reload uwsgi portal2 : %s" % service)
            retcode = run_command(command)
        except:
            traceback.print_exc(file=sys.stdout)
    elif (cmd == "status"):
        try:
            service = args[1]
            # read pid from file
            pid_file = file('%s/run/%s.pid' % (run_path, service))
        except:
            print t.red("Service %s is not running" % service)
            return 1

        try:
            pid = int(pid_file.read())
            pid_file.close()
            # read proc info
            if psutil.pid_exists(pid):
                p = psutil.Process(pid)
                pts = ','.join([str(td.id) for td in p.threads()])
                parent = p.parent
                ppid = p.ppid()
                pp = psutil.Process(ppid)
                print t.blue("Process tree:")
                print t.blue(" %-6s %-8s %-20s" % (ppid, pp.name, pp.exe))
                print t.blue(" |- %-6s %-8s %-20s [%s]" %
                             (pid, p.name, p.exe, pts))

                for c in p.children(recursive=True):
                    pc = psutil.Process(c.pid)
                    pcts = ','.join([str(td.id) for td in pc.threads()])
                    print t.blue("    |- %-6s %-8s %-20s [%s]" %
                                 (c.pid, pc.name, pc.exe, pcts))

                print t.blue("\nProcess info:")
                print t.blue(" path : %s" % p.cwd())
                print t.blue(" status : %s" % str(p.status))
                print t.blue(" permissions : %s, %s" % (p.uids, p.gids))
                print t.blue(" memory : %s" % str(p.memory_info_ex()))
                #sys.stdout.write(" memory map: %s\n" % str(p.get_memory_maps()))
                print t.blue(" open files :")
                for fd in p.open_files():
                    print t.blue(" |- fd:%s, file:%s" % (fd.fd, fd.path))
                print t.blue(" connections :")
                for c in p.connections():
                    print t.blue(
                        " |- fd:%s, family:%s, type:%s, laddr:%s, raddr:%s, status:%s"
                        % (c.fd, c.family, c.type, c.laddr, c.raddr, c.status))

            #command = ['uwsgi', '--reload', '%s/run/%s.pid' % (run_path, service)]
            #print "Reload uwsgi portal2 : %s" % service

            #retcode = run_command(command)
        except:
            traceback.print_exc(file=sys.stdout)
    elif (cmd == "trace"):
        try:
            service = args[1]
            command = [
                'uwsgi', '--connect-and-read',
                '%s/run/%s.tbsocket.*' % (run_path, service)
            ]
            retcode = run_command(command)
        except:
            traceback.print_exc(file=sys.stdout)
    elif (cmd == "log-uwsgi"):
        try:
            service = args[1]
            from sh import tail
            # runs forever
            for line in tail("-f",
                             '%s/log/%s-uwsgi.log' % (run_path, service),
                             _iter=True):
                sys.stdout.write(line)
        except:
            traceback.print_exc(file=sys.stdout)
    elif (cmd == "log"):
        try:
            service = args[1]
            from sh import tail
            # runs forever
            for line in tail("-f",
                             '%s/log/%s.log' % (run_path, service),
                             _iter=True):
                sys.stdout.write(line)
        except:
            traceback.print_exc(file=sys.stdout)
    elif (cmd == "createdb"):
        try:
            command = ['uwsgi', '--ini', '%s/etc/shell.ini' % (run_path)]
            #sys.stdout.write("Create db")
            retcode = run_command(command)
        except:
            traceback.print_exc(file=sys.stdout)

    return retcode
Exemplo n.º 20
0
    def update(self):
        def find_match_color(_cell):
            # 寻找和 _cell 描述的颜色最接近的终端颜色

            # color 列表采用了 OS X 的自带终端的配色方案
            # Ref: http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
            colors = {"black": (0, 0, 0),
                      "red": (194, 54, 33),
                      "green": (37, 188, 36),
                      "yellow": (173, 173, 39),
                      "blue": (73, 46, 255),
                      "magenta": (211, 56, 211),
                      "cyan": (51, 187, 200),
                      "white": (203, 204, 205)}

            # 差异度按欧几里德距离计算
            best_color_name = "black"
            best_color_distance = sqrt(255**2 + 255**2 + 255**2)

            for name, color in colors.items():
                distance = sqrt(sum([(_cell[i] - color[i]) ** 2 for i in range(3)]))
                if best_color_distance > distance:
                    best_color_name = name
                    best_color_distance = distance
            return best_color_name
            # 算法完毕

        t = Terminal()

        self.clear_screen()

        # 将 active_block 贴到 canvas 上
        if self.active_block is not None:
            self.canvas = deepcopy(self.map.content)
            for y in range(len(self.active_block)):
                for x in range(len(self.active_block[0])):
                    if self.active_block[y][x] is not None:
                        self.canvas[y+self.active_block_position[1]][x+self.active_block_position[0]] = self.active_block[y][x]

        # 输出游戏地图

        # 打印顶部栏杆
        print t.red("     /-------") + t.yellow("TETRIS") + t.red("-------\\")
        # 打印 canvas
        for index, row in enumerate(self.canvas):
            print t.red("     |"),
            for cell in row:
                if cell is not None:
                    color_name = find_match_color(cell)
                    stdout.write(getattr(t, 'red_on_'+color_name)('  '))  # red_on_ 是随便选的
                else:
                    stdout.write('  ')
            print t.red("|"),

            print "     ",

            # 下一个方块
            if 0 < index:
                try:
                    for cell in self.next_block[index-1]:
                        if cell is not None:
                            color_name = find_match_color(cell)
                            stdout.write(getattr(t, 'red_on_'+color_name)('  '))  # red_on_ 是随便选的
                        else:
                            stdout.write('  ')
                except IndexError:
                    pass
            elif 0 == index:
                print "Next",

            print
        # 打印底部栏杆
        print t.red("     \\--------------------/")
        print
        print t.blue(self.information_bar)
        print
        print t.bold_blue(u"     成绩: " + str(self.score))
        print "     PyTetris 2.33"
Exemplo n.º 21
0
try:
    while True:
        numberOfIterations += 1
        x = numpy.dot(-1.0, numpy.dot(K, x)) + F
        norma = numpy.linalg.norm(numpy.dot(matrix, x) - b)
        if numberOfIterations % 1000 == 0:
            print(numberOfIterations)
            print("\n")
            print("Itreation number " + str(numberOfIterations))
            print("Norm is " + str(norma))
            print("\n")
        if norma < epsilon:
            break
except Exception as e:
    print("Problem is: \n" + str(e))

x_ex = numpy.linalg.solve(matrix, b)
err = x - x_ex
dt2 = time.clock()

print("\n")
print('\033[1m' + '\033[4m' + t.blue("In the issue: \n"))
print(t.cyan("Parameter tay is ") + t.red(str(tay)))
print(t.cyan("Nubmer of iterations ") + t.red(str(numberOfIterations)))
#print("Solution is ", x)
print(t.cyan("Error is ") + t.red(str(numpy.linalg.norm(err))))
print(
    t.cyan("Time of solve ") + t.red(str(dt2 - dt1)) + t.cyan(" with tay ") +
    t.red(str(tay)))
print("\n")
        while True:
            knob_val = adc.read_oneshot(POTI)
            print("Pot value is", hex(knob_val))
            pct_full_scale = min(1.0, knob_val / 0x4fffff)
            print("Pot % is", pct_full_scale)
            dac.write_dac(DAC_A, DEFAULT_LED_BIAS + int(LED_RANGE*pct_full_scale))

    except (KeyboardInterrupt):
        print("\n" * 8 + "User exit.\n")
    return


print(term.clear() + term.bold_red_on_bright_green(' Wavefront ADC/DAC Board with PiGPIO Demo '))
host = input('Raspberry Pi hostname (enter for localhost):')
hostName = host if host else 'localhost'
print(term.blue('Connecting to'), term.bold(hostName))

pi = io.pi(host)

if not pi.connected:
    print(term.white_on_red("Could not connect to PiGPIO target " + hostName + "!"))
    sys.exit(-1)

print(term.dim('...connection established'))
adc = ADS1256(pi=pi)
# calibrate
adc.cal_self()
dac = DAC8552(pi=pi)

led_bias = determine_led_bias_v()
print('Minimum LED forward bias is', hex(led_bias))
Exemplo n.º 23
0
try:
	while True:
		numberOfIterations += 1
		x = numpy.dot(-1.0, numpy.dot(K, x)) + F
		norma = numpy.linalg.norm(numpy.dot(matrix, x) - b)
		if numberOfIterations % 1000 == 0:
			print(numberOfIterations)
			print("\n")
			print("Itreation number " + str(numberOfIterations))
			print("Norm is " +  str(norma))
			print("\n")
		if norma < epsilon:
			break
except Exception as e:
	print("Problem is: \n" + str(e))

x_ex = numpy.linalg.solve(matrix, b)
err = x - x_ex
dt2 = time.clock()


print("\n")
print('\033[1m' + '\033[4m' + t.blue("In the issue: \n"))
print(t.cyan("Parameter tay is ") + t.red(str(tay)))
print(t.cyan("Nubmer of iterations ") + t.red(str(numberOfIterations)))
#print("Solution is ", x)
print(t.cyan("Error is ") + t.red(str(numpy.linalg.norm(err))))
print(t.cyan("Time of solve ") + t.red(str(dt2-dt1)) + t.cyan(" with tay ") + t.red(str(tay)))
print("\n")
Exemplo n.º 24
0
            hashes.add(h)
            unique.append(p)
    duplicates = len(ps) - len(hashes)
    if duplicates > 0:
        print(INFORMATIONAL + "Ignoring " + str(duplicates) +
              " duplicate protocols (identical hashes)")
    return unique


VERSION = "Tamarin Tester v1.0"
DESCRIPTION = "tamarin-tester is a tool for testing the correctness of tamarin-prover builds by comparing their output to known-good builds. For a more comprehensive overview, consult the README distributed with this program. In general, you may run tests against benchmark files or generate these benchmark files yourself. Authored by Dennis Jackson, Computer Science Dept, University of Oxford."

TERMINAL = Terminal()

ERROR = TERMINAL.bold(TERMINAL.red("ERROR "))
INFORMATIONAL = TERMINAL.bold(TERMINAL.blue("INFORMATIONAL "))
WARNING = TERMINAL.yellow(TERMINAL.bold("WARNING "))

CHECK_TIMEOUT = TERMINAL.red(TERMINAL.bold("CHECK TIMEOUT "))
MALFORMED = TERMINAL.bold(TERMINAL.red("MALFORMED "))
BENCH_TIMEOUT = TERMINAL.red(TERMINAL.bold("BENCH TIMEOUT "))
NO_LEMMAS = TERMINAL.yellow(TERMINAL.bold("NO LEMMAS "))

INCORRECT = TERMINAL.bold(TERMINAL.red("\t INCORRECT: "))
STEPSIZE_INC = TERMINAL.bold(TERMINAL.yellow("\t STEPSIZE INC: "))
STEPSIZE_DEC = TERMINAL.bold(TERMINAL.yellow("\t STEPSIZE DEC: "))
TIMEOUT = TERMINAL.bold(TERMINAL.red("\t TIMEOUT "))

OVERTIME = TERMINAL.yellow(TERMINAL.bold("OVERTIME "))
NO_BENCHMARK = TERMINAL.yellow(TERMINAL.bold("NO BENCHMARK "))
FAILED = TERMINAL.bold(TERMINAL.red("FAILED "))
Exemplo n.º 25
0
		else:
			hashes.add(h)
			unique.append(p)
	duplicates = len(ps)-len(hashes)
	if duplicates > 0:
		print(INFORMATIONAL + "Ignoring "+ str(duplicates) + " duplicate protocols (identical hashes)")
	return unique


VERSION = "Tamarin Tester v1.0"
DESCRIPTION = "tamarin-tester is a tool for testing the correctness of tamarin-prover builds by comparing their output to known-good builds. For a more comprehensive overview, consult the README distributed with this program. In general, you may run tests against benchmark files or generate these benchmark files yourself. Authored by Dennis Jackson, Computer Science Dept, University of Oxford."

TERMINAL = Terminal()

ERROR = TERMINAL.bold(TERMINAL.red("ERROR "))
INFORMATIONAL = TERMINAL.bold(TERMINAL.blue("INFORMATIONAL "))
WARNING = TERMINAL.yellow(TERMINAL.bold("WARNING "))

CHECK_TIMEOUT= TERMINAL.red(TERMINAL.bold("CHECK TIMEOUT "))
MALFORMED= TERMINAL.bold(TERMINAL.red("MALFORMED "))
BENCH_TIMEOUT= TERMINAL.red(TERMINAL.bold("BENCH TIMEOUT "))
NO_LEMMAS= TERMINAL.yellow(TERMINAL.bold("NO LEMMAS "))

INCORRECT= TERMINAL.bold(TERMINAL.red("\t INCORRECT: "))
STEPSIZE_INC= TERMINAL.bold(TERMINAL.yellow("\t STEPSIZE INC: "))
STEPSIZE_DEC= TERMINAL.bold(TERMINAL.yellow("\t STEPSIZE DEC: "))
TIMEOUT= TERMINAL.bold(TERMINAL.red("\t TIMEOUT "))

OVERTIME= TERMINAL.yellow(TERMINAL.bold("OVERTIME "))
NO_BENCHMARK= TERMINAL.yellow(TERMINAL.bold("NO BENCHMARK "))
FAILED= TERMINAL.bold(TERMINAL.red("FAILED "))
Exemplo n.º 26
0
class Plotter(object):
    '''
    This is a semi-generic plotting interface that has a built in curses based terminal plotter.
    It's fairly specific to what we're using it for here, but we could (and maybe should) build it out into
    a little library that we can use via the command line to plot things.  Might be useful for looking at data later.
    That would also cut the size of this tool down by a good bit.
    '''
    #def __init__(self, kinavg, kinrw, iteration, bin_labels, state_labels, state_pops, bin_pops, interface='matplotlib'):
    def __init__(self, h5file, h5key, iteration=-1, interface='matplotlib'):
        # Need to sort through and fix all this, but hey.
        self.iteration = iteration
        # These two are important for... reasons.
        try:
            self.bin_labels = list(bin_labels[...])
            self.state_labels = list(state_labels[...]) + ['unknown']
        except:
            try:
                self.state_labels = list(h5file['state_labels'][...]) + ['unknown']
            except:
                self.state_labels = None
        # unless we totally fail out.
        self.interface = interface
        # What we should ACTUALLY do is just... yeah, just have it sub in what we need.
        # We'll need to throw in the state labels or whatever, but.
        self.h5file = h5file
        self.h5key = h5key
        # We should determine the number of dimensions of our dataset...
        # This has time data, so an i to j is a 3 dim, and an i is 2.
        try:
            self.dim = len(h5file[h5key].shape)
        except:
            self.dim = 1
        try:
            # does the ci exist?
            a = h5file[h5key]['expected']
        except:
            self.dim = 1

    def plot(self, i=0, j=1, tau=1, iteration=None, dim=0, interface=None):
        if iteration == None:
            iteration = self.iteration
            self.__generic_ci__(self.h5file, iteration, i, j, tau=tau, h5key=self.h5key, dim=dim, interface=interface)

    def __generic_ci__(self, h5file, iteration, i, j, tau, h5key='rate_evolution', dim=0, interface=None):
        # This function just calls the appropriate plot function for our available
        # interface.
        if (interface == None and self.interface == 'text') or interface == 'text':
            if self.dim > 1:
                self.__terminal_ci__(h5file, iteration, i, j, tau, h5key)
            else:
                self.__terminal_expected__(h5file, iteration, i, j, tau, h5key, dim)
        else:
            try:
                import matplotlib
                matplotlib.use('TkAgg')
                from matplotlib import pyplot as plt
                if self.dim == 3:
                    plt.plot(h5file[h5key]['expected'][:iteration, i, j] / tau, color='black')
                    plt.plot(h5file[h5key]['ci_ubound'][:iteration, i, j] / tau, color='grey')
                    plt.plot(h5file[h5key]['ci_lbound'][:iteration, i, j] / tau, color='grey')
                else:
                    plt.plot(h5file[h5key]['expected'][:iteration, i] / tau, color='black')
                    plt.plot(h5file[h5key]['ci_ubound'][:iteration, i] / tau, color='grey')
                    plt.plot(h5file[h5key]['ci_lbound'][:iteration, i] / tau, color='grey')
                plt.show()
            except:
                print('Unable to import plotting interface.  An X server ($DISPLAY) is required.')
                if self.dim > 1:
                    self.__terminal_ci__(h5file, iteration, i, j, tau)
                else:
                    self.__terminal_expected__(h5file, iteration, i, j, tau, h5key, dim)
                return 1

    def __generic_histo__(self, vector, labels):
        # This function just calls the appropriate plot function for our available
        # interface.  Same thing as generic_ci, but for a histogram.
        if self.interface == 'text':
            self.__terminal_histo__(vector, labels)
        else:
            try:
                import matplotlib
                matplotlib.use('TkAgg')
                from matplotlib import pyplot as plt
                plt.bar(list(range(0, np.array(vector).shape[0])), vector, linewidth=0, align='center', color='gold', tick_label=labels)
                plt.show()
            except:
                print('Unable to import plotting interface.  An X server ($DISPLAY) is required.')
                self.__terminal_histo__(h5file, vector, labels)
                return 1

    def __terminal_histo__(self, vector, labels, fullscreen_mode=True):
        from blessings import Terminal

        self.t = Terminal()
        h = int(self.t.height / 4) * 3
        w = self.t.width
        cols = np.array(vector).shape[0]
        # Let's print this business!

        colwidth = w / cols
        with self.t.fullscreen():
            for y in range(0, h):
                for x in range(0, cols):
                    if x == 0:
                        with self.t.location(0, y):
                            print(self.t.red('{0:.4f}|'.format(float(h-y)/float(h))))
                    with self.t.location((x*colwidth)+8+len(labels[x])/2, y):
                        if vector[x] >= (float(h-y)/float(h)):
                            #print(float(h-y)/float(h))
                            print(self.t.on_blue(' '))
            for x in range(0, cols):
                if x == 0:
                    with self.t.location(x, h):
                        print('States| ')
                with self.t.location((x*colwidth)+8, h):
                    print(self.t.blue(labels[x]))

            if fullscreen_mode:
                input("Press enter to continue.")

    def __terminal_ci__(self, h5file, iteration, si, sj, tau, h5key):
        from blessings import Terminal

        self.t = Terminal()
        h = int(self.t.height / 4 * 3.75)
        # We'll figure out how to subsample the timepoints...
        w = self.t.width
        if self.dim == 3:
            in_tup = (iteration-1, si, sj)
        else:
            in_tup = (iteration-1, si)
        yupper = (h5file[h5key]['ci_ubound'][in_tup] / tau) * 2
        ylower = (h5file[h5key]['ci_lbound'][in_tup] / tau) / 2
        # Here are points pertaining to height.
        scale = np.array([0.0] + [ylower+i*(yupper-ylower)/np.float(h) for i in range(0, h)])[::-1]
        if iteration > w:
            block_size = iteration / w
        else:
            block_size = 1

        with self.t.fullscreen():
            try:
                for x in range(0, w-12):
                    iter = x * block_size
                    if self.dim == 3:
                        in_tup = (iter-1, si, sj)
                    else:
                        in_tup = (iter-1, si)
                    yupper = (h5file[h5key]['ci_ubound'][in_tup] / tau)
                    ylower = (h5file[h5key]['ci_lbound'][in_tup] / tau)
                    ci = np.digitize([yupper, ylower], scale)
                    if x == 0:
                        for y in range(0, h+1):
                            with self.t.location(0, y):
                                print(self.t.bold(self.t.red('{0:.7f}|'.format(scale[y]))))
                    for y in range(ci[0], ci[1]):
                        #with self.t.location(x+12, y):
                        print(self.t.move(y, x+12) + self.t.on_blue(' '))
                            #print(self.t.on_blue(' '))
                    #with self.t.location(x+12, np.digitize(h5file['rate_evolution']['expected'][iter-1, si, sj]/tau, scale)):
                    #        print(self.t.on_blue('-'))
                    print(self.t.move(np.digitize(h5file[h5key]['expected'][in_tup]/tau, scale), x+12) + self.t.on_blue('-'))

                for x in range(0, w-12, w/10):
                    if x == 0:
                        with self.t.location(x, h+1):
                            print('Iteration| ')
                    with self.t.location(x+12, h+1):
                        iter = x * block_size
                        print(self.t.blue(str(iter)))
            except:
                pass

            with self.t.location(0, h+2):
                # We need to improve this.
                #if h5key == 'rate_evolution':
                #    print("k_ij from {} to {} from iter 1 to {}".format(self.state_labels[si], self.state_labels[sj], self.iteration))
                #elif h5key == 'conditional_flux_evolution':
                #    print("i->j flux from {} to {} from iter 1 to {}".format(self.state_labels[si], self.state_labels[sj], self.iteration))
                if self.dim == 3:
                    print("{} from {} to {} from iter 1 to {}".format(h5key, self.state_labels[si], self.state_labels[sj], self.iteration))
                else:
                    print("{} of state {} from iter 1 to {}".format(h5key, self.state_labels[si], self.iteration))
            with self.t.location(0, h+3):
                input("Press enter to continue.")

    def __terminal_expected__(self, h5file, iteration, si, sj, tau, h5key, dim):
        from blessings import Terminal

        self.t = Terminal()
        h = int(self.t.height / 4 * 3.75)
        # We'll figure out how to subsample the timepoints...
        w = self.t.width
        if self.dim == 3:
            in_tup = (iteration-1, si, sj)
        else:
            in_tup = (iteration-1, si)
        in_tup = (iteration-1, dim)
        try:
            yupper = (np.max(h5file) / tau) * 2
        except:
            in_tup = (iteration-1)
            yupper = (np.max(h5file) / tau) * 2
        ylower = (np.min(h5file) / tau) * 2
        # Here are points pertaining to height.
        if yupper > 0:
            yupper = (np.max(h5file) / tau) * 1.2
            ylower = (np.min(h5file) / tau) / 2
            scale = np.array([0.0] + [ylower+i*(yupper-ylower)/np.float(h) for i in range(0, h)])[::-1]
        else:
            yupper = (np.max(h5file) / tau) / 2
            ylower = (np.min(h5file) / tau) * 1.2
            scale = np.array([ylower+i*(yupper-ylower)/np.float(h) for i in range(0, h)] + [0.0])[::-1]
        if iteration > w:
            block_size = iteration / w
        else:
            block_size = 1

        with self.t.fullscreen():
            try:
                for x in range(0, w-12):
                    iter = x * block_size
                    if self.dim == 3:
                        in_tup = (iter-1, si, sj)
                    else:
                        in_tup = (iter-1, si)
                    in_tup = (iter-1, dim)
                    try:
                        yupper = (h5file[in_tup] / tau)
                    except:
                        in_tup = (iter-1)
                        yupper = (h5file[in_tup] / tau)
                    ylower = (h5file[in_tup] / tau)
                    ci = np.digitize([yupper, ylower], scale)
                    if x == 0:
                        for y in range(0, h+1):
                            with self.t.location(0, y):
                                print(self.t.bold(self.t.red('{0:.7f}|'.format(scale[y]))))
                    for y in range(ci[0], ci[1]):
                        print(self.t.move(y, x+12) + self.t.on_blue(' '))
                            #print(self.t.on_blue(' '))
                    print(self.t.move(np.digitize(h5file[in_tup]/tau, scale), x+12) + self.t.on_blue('-'))

                for x in range(0, w-12, w/10):
                    if x == 0:
                        with self.t.location(x, h+1):
                            print('Iteration| ')
                    with self.t.location(x+12, h+1):
                        iter = x * block_size
                        print(self.t.blue(str(iter)))
            except:
                pass

            with self.t.location(0, h+2):
                # We need to improve this.
                print("{} from iter 1 to {}".format(h5key, self.iteration))
            with self.t.location(0, h+3):
                input("Press enter to continue.")
Exemplo n.º 27
0
# I might remove this in production.
# This should work but is producing an error.
#
#    ERROR: Character value 194 too big
#
# So I'm leaving it out for now.
# csvfix edit -e 's/^$/ /g' temp-stripped.csv > temp-dashed.csv

# Split the lat,long column into two columns
# We want to store this as separate fields in the data base.
print term.yellow("Splitting out latitude and longitude")
temp_latlong = os.path.abspath("temp-latlong.csv")
(csvfix["split_char", "-f", "12", "-c", ",", temp_dashless] > temp_latlong)()

# Parse the data uncertainties into ISO dates
print term.blue("Fixing dates")
temp_dates = os.path.abspath("temp-dates.csv")
input = open(temp_latlong, "rb")
output = open(temp_dates, "wb")
writer = csv.writer(output)
for row in csv.reader(input):
    if row:
        date_string = row[8]

        parts = date_string.split("-")

        year = parts[0]

        # Year only
        if len(parts) == 1:
            # Remove circa
Exemplo n.º 28
0
     if not username == "backup" and passwd == "backup":
         run = False
     logged = True
     print("Successfully logged in.")
     print("Connecting...")
     time.sleep(1)
     print("Connected with options '--public_key %s' " %
           hashlib.sha256("Hello gaming!".encode()).hexdigest())
     print("[INFO] $" + wrap.bold("su") +
           " - use this for get super user permissions.")
     print("[INFO] $" + wrap.bold("sudo") +
           " - use this for execute command with root permissions.")
     print("[INFO] $" + wrap.bold("exit") + " - use this for quit program.")
 if logged:
     if not root:
         cmd = input(wrap.blue("MMCnet:/ ") + wrap.bright_green("$ "))
     else:
         cmd = input("MMCnet:/ # ")
     if cmd.split(" ")[0] == "su":
         tries = 3
         while tries > 0 and not root:
             supass = input(cmd.split(" ")[0] + ": ")
             if supass == "backup":
                 root = True
             else:
                 tries -= 1
                 print("You enter wrong password.")
     elif cmd.split(" ")[0] == "sudo":
         if not root:
             tries = 3
             while tries > 0 and not root:
Exemplo n.º 29
0
    def start(self):
        # Parse args
        global args, APs
        args = parse_args()

        # Check args
        check_args(args)

        # Are you root?
        if os.geteuid():
            sys.exit('[' + R + '-' + W + '] Please run as root')

        self.network_manager.start()

        # TODO: We should have more checks here:
        # Is anything binded to our HTTP(S) ports?
        # Maybe we should save current iptables rules somewhere

        # get interfaces for monitor mode and AP mode and set the monitor interface
        # to monitor mode. shutdown on any errors
        try:
            if args.internetinterface:
                if self.network_manager.is_interface_valid(
                        args.internetinterface, "internet"):
                    internet_interface = args.internetinterface
                    self.network_manager.unblock_interface(internet_interface)
            if not args.nojamming:
                if args.jamminginterface and args.apinterface:
                    if self.network_manager.is_interface_valid(
                            args.jamminginterface, "monitor"):
                        mon_iface = args.jamminginterface
                        self.network_manager.unblock_interface(mon_iface)
                    if self.network_manager.is_interface_valid(
                            args.apinterface, "AP"):
                        ap_iface = args.apinterface
                else:
                    mon_iface, ap_iface = self.network_manager.get_interface_automatically(
                    )
                # display selected interfaces to the user
                print(
                    "[{0}+{1}] Selecting {0}{2}{1} interface for the deauthentication "
                    "attack\n[{0}+{1}] Selecting {0}{3}{1} interface for creating the "
                    "rogue Access Point").format(G, W, mon_iface, ap_iface)

                # randomize the mac addresses
                if not args.no_mac_randomization:
                    if args.mac_ap_interface:
                        self.network_manager.set_interface_mac(
                            ap_iface, args.mac_ap_interface)
                    else:
                        self.network_manager.set_interface_mac_random(ap_iface)
                    if args.mac_deauth_interface:
                        self.network_manager.set_interface_mac(
                            mon_iface, args.mac_deauth_interface)
                    else:
                        self.network_manager.set_interface_mac_random(
                            mon_iface)
            else:
                if args.apinterface:
                    if self.network_manager.is_interface_valid(
                            args.apinterface, "AP"):
                        ap_iface = args.apinterface
                else:
                    ap_iface = self.network_manager.get_interface(True, False)
                mon_iface = ap_iface

                if not args.no_mac_randomization:
                    if args.mac_ap_interface:
                        self.network_manager.set_interface_mac(
                            ap_iface, args.mac_ap_interface)
                    else:
                        self.network_manager.set_interface_mac_random(ap_iface)

                print(
                    "[{0}+{1}] Selecting {0}{2}{1} interface for creating the "
                    "rogue Access Point").format(G, W, ap_iface)
                # randomize the mac addresses
                if not args.no_mac_randomization:
                    self.network_manager.set_interface_mac_random(ap_iface)

            # make sure interfaces are not blocked
            self.network_manager.unblock_interface(ap_iface)
            self.network_manager.unblock_interface(mon_iface)

            if not args.internetinterface:
                kill_interfering_procs()

            self.network_manager.set_interface_mode(mon_iface, "monitor")
        except (interfaces.InvalidInterfaceError,
                interfaces.InterfaceCantBeFoundError,
                interfaces.InterfaceManagedByNetworkManagerError) as err:
            print("[{0}!{1}] {2}").format(R, W, err)

            time.sleep(1)
            self.stop()

        if not args.no_mac_randomization:
            ap_mac = self.network_manager.get_interface_mac(ap_iface)
            print "[{0}+{1}] {2} mac address becomes is now {3} ".format(
                G, W, ap_iface, ap_mac)

            if not args.nojamming:
                mon_mac = self.network_manager.get_interface_mac(mon_iface)
                print("[{0}+{1}] {2} mac address becomes {3}".format(
                    G, W, mon_iface, mon_mac))

        if args.internetinterface:
            self.fw.nat(ap_iface, args.internetinterface)
            set_ip_fwd()
        else:
            self.fw.redirect_requests_localhost()
        set_route_localnet()

        print '[' + T + '*' + W + '] Cleared leases, started DHCP, set up iptables'
        time.sleep(1)

        if args.essid:
            essid = args.essid
            channel = str(CHANNEL)
            ap_mac = None
            enctype = None
        else:
            # let user choose access point
            access_point = curses.wrapper(select_access_point, mon_iface,
                                          self.mac_matcher,
                                          self.network_manager)

            # if the user has chosen a access point continue
            # otherwise shutdown
            if access_point:
                # store choosen access point's information
                essid = access_point.get_name()
                channel = access_point.get_channel()
                ap_mac = access_point.get_mac_address()
                enctype = access_point.get_encryption()
            else:
                self.stop()
        # create a template manager object
        self.template_manager = phishingpage.TemplateManager()
        # get the correct template
        template = select_template(args.phishingscenario,
                                   self.template_manager)

        print("[" + G + "+" + W + "] Selecting " +
              template.get_display_name() + " template")

        # payload selection for browser plugin update
        if template.has_payload():
            payload_path = False
            # copy payload to update directory
            while not payload_path or not os.path.isfile(payload_path):
                # get payload path
                payload_path = raw_input(
                    "[" + G + "+" + W + "] Enter the [" + G + "full path" + W +
                    "] to the payload you wish to serve: ")
                if not os.path.isfile(payload_path):
                    print '[' + R + '-' + W + '] Invalid file path!'
            print '[' + T + '*' + W + '] Using ' + G + payload_path + W + ' as payload '
            copyfile(payload_path,
                     PHISHING_PAGES_DIR + template.get_payload_path())

        APs_context = []
        for i in APs:
            APs_context.append({
                'channel':
                APs[i][0] or "",
                'essid':
                APs[i][1] or "",
                'bssid':
                APs[i][2] or "",
                'vendor':
                self.mac_matcher.get_vendor_name(APs[i][2]) or ""
            })

        template.merge_context({'APs': APs_context})

        # only get logo path if MAC address is present
        ap_logo_path = False
        if ap_mac:
            ap_logo_path = template.use_file(
                self.mac_matcher.get_vendor_logo_path(ap_mac))

        template.merge_context({
            'target_ap_channel':
            channel or "",
            'target_ap_essid':
            essid or "",
            'target_ap_bssid':
            ap_mac or "",
            'target_ap_encryption':
            enctype or "",
            'target_ap_vendor':
            self.mac_matcher.get_vendor_name(ap_mac) or "",
            'target_ap_logo_path':
            ap_logo_path or ""
        })

        # We want to set this now for hostapd. Maybe the interface was in "monitor"
        # mode for network discovery before (e.g. when --nojamming is enabled).
        self.network_manager.set_interface_mode(ap_iface, "managed")
        # Start AP
        self.access_point.set_interface(ap_iface)
        self.access_point.set_channel(channel)
        self.access_point.set_essid(essid)
        if args.presharedkey:
            self.access_point.set_psk(args.presharedkey)
        if args.internetinterface:
            self.access_point.set_internet_interface(args.internetinterface)
        print '[' + T + '*' + W + '] Starting the fake access point...'
        try:
            self.access_point.start()
            self.access_point.start_dhcp_dns()
        except BaseException:
            self.stop()

        # With configured DHCP, we may now start the web server
        if not args.internetinterface:
            # Start HTTP server in a background thread
            print '[' + T + '*' + W + '] Starting HTTP/HTTPS server at ports ' + str(
                PORT) + ", " + str(SSL_PORT)
            webserver = Thread(target=phishinghttp.runHTTPServer,
                               args=(NETWORK_GW_IP, PORT, SSL_PORT, template))
            webserver.daemon = True
            webserver.start()

            time.sleep(1.5)

        # We no longer need mac_matcher
        self.mac_matcher.unbind()

        clients_APs = []
        APs = []

        if not args.nojamming:
            # Start Extension Manager
            shared_data = {
                'target_ap_channel': channel or "",
                'target_ap_essid': essid or "",
                'target_ap_bssid': ap_mac or "",
                'target_ap_encryption': enctype or "",
                'target_ap_logo_path': ap_logo_path or "",
                'rogue_ap_mac': ap_mac,
                'APs': APs_context,
                'args': args
            }
            self.em.set_interface(mon_iface)
            extensions = DEFAULT_EXTENSIONS

            if args.lure10_exploit:
                extensions.append(LURE10_EXTENSION)
            self.em.set_extensions(extensions)
            self.em.init_extensions(shared_data)
            self.em.start_extensions()

        # Main loop.
        try:
            term = Terminal()
            with term.fullscreen():
                while True:
                    term.clear()
                    with term.hidden_cursor():
                        print term.move(0, term.width - 30) + "|"
                        print term.move(
                            1, term.width -
                            30) + "|" + " " + term.bold_blue("Wifiphisher " +
                                                             VERSION)
                        print term.move(
                            2, term.width - 30) + "|" + " ESSID: " + essid
                        print term.move(
                            3, term.width - 30) + "|" + " Channel: " + channel
                        print term.move(
                            4, term.width -
                            30) + "|" + " AP interface: " + ap_iface
                        print term.move(5, term.width - 30) + "|" + "_" * 29
                        print term.move(
                            1, 0) + term.blue("Deauthenticating clients: ")
                        if not args.nojamming:
                            # show the 5 most recent entries
                            for line in self.em.get_output()[-5:]:
                                print line
                        print term.move(7, 0) + term.blue("DHCP Leases: ")
                        if os.path.isfile('/var/lib/misc/dnsmasq.leases'):
                            proc = check_output(
                                ['tail', '-5', '/var/lib/misc/dnsmasq.leases'])
                            print term.move(8, 0) + proc
                        print term.move(14, 0) + term.blue("HTTP requests: ")
                        if os.path.isfile('/tmp/wifiphisher-webserver.tmp'):
                            proc = check_output([
                                'tail', '-5', '/tmp/wifiphisher-webserver.tmp'
                            ])
                            print term.move(15, 0) + proc
                        if phishinghttp.terminate and args.quitonsuccess:
                            raise KeyboardInterrupt
        except KeyboardInterrupt:
            self.stop()
Exemplo n.º 30
0
def dump(**kwargs):
    common = common_args(**kwargs)

    t = Terminal()
    ranking_line = "   {prob:6.2f}% → {color}{text}{t.normal}"
    actual_line = "{t.red}Actual{t.normal}: {t.bold}{actual_text}{t.normal}"

    sent_forwards = Sentences(common.file_vector,
                              size=SENTENCE_LENGTH,
                              backwards=False)
    sent_backwards = Sentences(common.file_vector,
                               size=SENTENCE_LENGTH,
                               backwards=True)

    least_agreement = []
    forwards_predictions = []
    backwards_predictions = []
    ranks = []

    contexts = enumerate(zip(chop_prefix(common.tokens, PREFIX_LENGTH),
                             sent_forwards, chop_prefix(sent_backwards)))

    # Note, the index is offset from the true start; i.e., when
    # index == 0, the true index is SENTENCE_LENGTH
    for index, (token, (prefix, x1), (suffix, x2)) in contexts:
        assert x1 == x2
        actual = x1
        print(unvocabularize(prefix[-5:]),
              t.bold_underline(token.value),
              unvocabularize(suffix[:5]))

        prefix_pred = common.forwards_model.predict(prefix)
        suffix_pred = common.backwards_model.predict(suffix)

        # harmonic mean
        mean = consensus(suffix_pred, prefix_pred)

        forwards_predictions.append(index_of_max(prefix_pred))
        backwards_predictions.append(index_of_max(suffix_pred))

        paired_rankings = rank(mean)
        ranked_vocab = list(tuple(zip(*paired_rankings))[0])
        top_5 = paired_rankings[:5]
        top_5_words = ranked_vocab[:5]

        for token_id, weight in top_5:
            color = t.green if token_id == actual else ''
            text = vocabulary.to_text(token_id)
            prob = weight * 100.0
            print(ranking_line.format_map(locals()))

        ranks.append(ranked_vocab.index(actual) + 1)
        min_token_id, min_prob = paired_rankings[0]
        least_agreement.append(Agreement(min_prob, index))

        if actual not in top_5_words:
            actual_text = vocabulary.to_text(actual)
            print(actual_line.format_map(locals()))

        print()

        if not ranks:
            print(t.red("Could not analyze file!"), file=sys.stderr)
            return

    print("MRR: ", mean_reciprocal_rank(ranks))
    print("Lowest rank:", max(ranks))
    print("Time at #1: {:.2f}%".format(
          100 * sum(1 for rank in ranks if rank == 1) / len(ranks)))
    print()

    forwards_text = [vocabulary.to_text(num) for num in forwards_predictions]
    backwards_text = [vocabulary.to_text(num) for num in backwards_predictions]

    least_agreement.sort()
    # Compensate for offset indices
    file_vector = common.file_vector[SENTENCE_LENGTH:]
    tokens_text = [tok.value for tok in common.tokens[PREFIX_LENGTH:]]
    for disagreement in least_agreement[:5]:
        print(disagreement.probability)
        prefix = ' '.join(disagreement.prefix(tokens_text))
        suffix = ' '.join(disagreement.suffix(tokens_text))

        print("   ", prefix, t.yellow(forwards_text @ disagreement), suffix)
        print("   ", prefix, t.underline(tokens_text @ disagreement), suffix)
        print("   ", prefix, t.blue(backwards_text @ disagreement), suffix)
        print()
Exemplo n.º 31
0
        if line.startswith('[INFO] +-') or line.startswith('[INFO] \\-'):
            importer = line.split()[2]

        index = line.find(dependency_name)
        if index != -1:
            target = line[index:]
            dependencies.append((module, importer, target))

    return dependencies


if __name__ == '__main__':

    # Parse CLI options
    root_dir, marker_file, dependency_name = parse_options(sys.argv[1:])
    print(term.blue('Options:'))
    print(term.green('  Root folder: ') + root_dir)
    print(term.green('  Marker file: ') + marker_file)
    print(term.green('  Dependency name: ') + dependency_name)
    print()

    # Find project dirs
    projects_dirs = find_projects(root_dir, marker_file)
    print(term.blue('Found projects:'))
    for project_dir in projects_dirs:
        print('  ' + project_dir)
    print()

    # Check project dependencies
    print(term.blue('Dependencies:'))
    for project_dir in projects_dirs:
Exemplo n.º 32
0
# I might remove this in production.
# This should work but is producing an error.
#
#    ERROR: Character value 194 too big
#
# So I'm leaving it out for now.
# csvfix edit -e 's/^$/ /g' temp-stripped.csv > temp-dashed.csv

# Split the lat,long column into two columns
# We want to store this as separate fields in the data base.
print term.yellow('Splitting out latitude and longitude')
temp_latlong = os.path.abspath('temp-latlong.csv')
(csvfix['split_char', '-f', '12', '-c', ',', temp_dashless] > temp_latlong)()

# Parse the data uncertainties into ISO dates
print term.blue('Fixing dates')
temp_dates = os.path.abspath('temp-dates.csv')
input = open(temp_latlong, 'rb')
output = open(temp_dates, 'wb')
writer = csv.writer(output)
for row in csv.reader(input):
    if row:
        date_string = row[8]

        parts = date_string.split('-')

        year = parts[0]

        # Year only
        if len(parts) == 1:
            # Remove circa
Exemplo n.º 33
0
        items = response.json()['list'].values()
        item_count = len(items)
        print('{item_count} items retrieved!'.format(item_count=item_count))

        random.shuffle(items)
        while items:
            picked_item = items.pop(0)
            item_id = picked_item.get('item_id')
            item_title = picked_item.get('resolved_title')
            item_url = picked_item.get('resolved_url', '')
            item_timestamp = int(picked_item.get('time_added', 0))

            id_field = t.yellow(u'[#{id}]'.format(id=item_id))
            title_field = t.white(u'"{title}"'.format(title=item_title))
            url_field = t.green(u'{url}'.format(url=truncate(item_url, t.width)))
            date_field = t.blue(u'Added at {date}'.format(date=pretty_date(item_timestamp)))

            print(u'')
            print(u'{id} {title}'.format(id=id_field, title=title_field))
            print(u'{url}'.format(url=url_field))
            print(u'{date}'.format(date=date_field))

            # FIXME: kinda dirty
            def _item_action(action):
                assert(action == 'archive' or action == 'delete')
                request_url = '{api_base}send'.format(api_base=API_BASE)
                request_data = {
                    'consumer_key': cfg.api_key,
                    'access_token': cfg.user_token,
                    'actions': json.dumps([{
                        'action': action,
Exemplo n.º 34
0
    def update(self):
        def find_match_color(_cell):
            # 寻找和 _cell 描述的颜色最接近的终端颜色

            # color 列表采用了 OS X 的自带终端的配色方案
            # Ref: http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
            colors = {
                "black": (0, 0, 0),
                "red": (194, 54, 33),
                "green": (37, 188, 36),
                "yellow": (173, 173, 39),
                "blue": (73, 46, 255),
                "magenta": (211, 56, 211),
                "cyan": (51, 187, 200),
                "white": (203, 204, 205)
            }

            # 差异度按欧几里德距离计算
            best_color_name = "black"
            best_color_distance = sqrt(255**2 + 255**2 + 255**2)

            for name, color in colors.items():
                distance = sqrt(
                    sum([(_cell[i] - color[i])**2 for i in range(3)]))
                if best_color_distance > distance:
                    best_color_name = name
                    best_color_distance = distance
            return best_color_name
            # 算法完毕

        t = Terminal()

        self.clear_screen()

        # 将 active_block 贴到 canvas 上
        if self.active_block is not None:
            self.canvas = deepcopy(self.map.content)
            for y in range(len(self.active_block)):
                for x in range(len(self.active_block[0])):
                    if self.active_block[y][x] is not None:
                        self.canvas[y + self.active_block_position[1]][
                            x + self.
                            active_block_position[0]] = self.active_block[y][x]

        # 输出游戏地图

        # 打印顶部栏杆
        print t.red("     /-------") + t.yellow("TETRIS") + t.red("-------\\")
        # 打印 canvas
        for index, row in enumerate(self.canvas):
            print t.red("     |"),
            for cell in row:
                if cell is not None:
                    color_name = find_match_color(cell)
                    stdout.write(getattr(t, 'red_on_' +
                                         color_name)('  '))  # red_on_ 是随便选的
                else:
                    stdout.write('  ')
            print t.red("|"),

            print "     ",

            # 下一个方块
            if 0 < index:
                try:
                    for cell in self.next_block[index - 1]:
                        if cell is not None:
                            color_name = find_match_color(cell)
                            stdout.write(
                                getattr(t, 'red_on_' +
                                        color_name)('  '))  # red_on_ 是随便选的
                        else:
                            stdout.write('  ')
                except IndexError:
                    pass
            elif 0 == index:
                print "Next",

            print
        # 打印底部栏杆
        print t.red("     \\--------------------/")
        print
        print t.blue(self.information_bar)
        print
        print t.bold_blue(u"     成绩: " + str(self.score))
        print "     PyTetris 2.33"
Exemplo n.º 35
0
class Repo(object):
    """
        Repo object. Perform git operations on a given repo.
    """

    def __init__(self, repo_dir="", name="", url=""):
        """todo: to be defined

        :param repo_dir: arg description
        :type repo_dir: type description
        :param name: arg description
        :type name: type description
        :param url: arg description
        :type url: type description
        """
        logger.debug("repo_dir: %s, name: %s, url: %s", repo_dir, name, url)

        if not (repo_dir or name or url):
            estr = "You must supply at least one of: repo_dir, name, or url."
            logger.error(estr)
            raise InvalidRepo(estr)

        self._repo_dir = repo_dir
        self._name = nice_pkg_name(name)
        self._url = url
        self._basename = ""

        self.supported_schemes = ('git', 'https', 'http', 'file', '')


        self._ctx = upkg_ctx.get_ctx()
        self.term = Terminal()
    #__init__()

    def __repr__(self):
        """todo: Docstring for __repr__
        :return:
        :rtype:
        """

        return "{} URL:{} LOCATION: {}".format(
            self._name,
            self._url,
            self._repo_dir
        )
    #__repr__()

    def _build_writer(self, output, color=None, prefix=""):
        if color:

            def _cwriter(line):
                go = getattr(self.term, color)
                output(go(self.name + ": " + prefix + line))

            return _cwriter

        def _writer(line):
            output(self.name + ": " + prefix + line)

        return _writer
    #_build_writer()

    def _sh_stdout(self, *args, **kwargs):
        return self._build_writer(sys.stdout.write, *args, **kwargs)
    #_sh_stdout()

    def _sh_stderr(self, *args, **kwargs):
        return self._build_writer(sys.stderr.write, *args, **kwargs)
    #_sh_stderr()

    def __str__(self):
        """todo: Docstring for __str__
        :return:
        :rtype:
        """

        return "{} : {}".format(self.name, self.url)
    #__str__()

    @property
    def name(self):
        logger.debug("name: %s", self._name)

        if not self._name:
            self._name = nice_pkg_name(self.basename)

        return self._name

    @property
    def repo_dir(self):
        logger.debug("repo_dir: %s", self._repo_dir)

        if not self._repo_dir:
            bn = self.basename
            if bn:
                self._repo_dir = os.path.join(settings.upkg_destdir, bn)

        return self._repo_dir

    @property
    def url(self):
        logger.debug("url: %s", self._url)

        if not self._url:
            rd = self.repo_dir
            out = StringIO()
            cwd = os.getcwd()
            os.chdir(rd)

            p = git('ls-remote', '--get-url',
                        _out=out, _err=self._sh_stderr('red'))
            p.wait()
            os.chdir(cwd)
            self._url = out.getvalue().strip()
            logger.debug("URL? %s", self._url)
            out.close()

        return self._url

    @property
    def basename(self):
        logger.debug("basename: %s, repo_dir: %s", self._basename, self._repo_dir)

        if self._repo_dir:
            self._basename = os.path.basename(self._repo_dir)
        elif self._url:
            url = urlparse(self._url)
            self._basename = os.path.basename(url.path)
        else:
            self._basename = os.path.basename(pkg_name_to_path(self._name))

        return self._basename

    @property
    def installed(self):
        rd = self.repo_dir
        if rd:
            return os.path.exists(rd)

        return rd
    #installed()

    @classmethod
    def installed_list(cls):
        """todo: Docstring for list
        :return:
        :rtype:
        """
        logger.debug("")

        res = []
        for x in repo_dirlist():
            d = x['path']
            try:
                res.append(Repo(repo_dir=d, name=x['base']))
            except InvalidRepo:
                logger.warning("Invalid repo '%s' in the installation directory.", d)

        return res
    #installed_list()

    def pr_pass(self, fmt, *args, **kwargs):
        print(self.term.green(fmt.format(*args, **kwargs)))
    #pr_pass()

    def pr_info(self, fmt, *args, **kwargs):
        print(self.term.blue(fmt.format(*args, **kwargs)))
    #pr_info()

    def pr_fail(self, fmt, *args, **kwargs):
        print(self.term.red(fmt.format(*args, **kwargs)))
    #pr_fail()

    def pr_atten(self, fmt, *args, **kwargs):
        print(self.term.red(fmt.format(*args, **kwargs)))
    #pr_atten()

    def clone(self):
        """todo: Docstring for clone
        :return:
        :rtype:
        """
        logger.debug("")
    
        if not self.url:
            estr = "Cannot install this repos without a URL. %s" % self.info()
            logger.warning(estr)
            raise ValueError(estr)

        # check if the already exists, if so, don't install, update it?
        url = urlparse(self.url)
        url_path = url[2]
        path_end = url_path.split('/')
        path_end = path_end[len(path_end) - 1]

        if url.scheme not in self.supported_schemes:
            raise ValueError("Unsupported scheme '{}' for {}".format(url.scheme, self.url))

        assert self.repo_dir, "Invalid repo directory."

        # Clone it.
        logger.debug("cloning %s into %s .", self.url, self.repo_dir)
        self.pr_pass("\nInstalling %s ... " % self.url)

        p = git.clone('--progress', self.url, self.repo_dir,
                      _out=self._sh_stdout('blue'), _err=self._sh_stderr('blue'))
        p.wait()
    #clone()

    def install_update_deps(self):
        """todo: Docstring for install_update_deps
        :return:
        :rtype:
        """
        logger.debug("")
        self._ctx.installed(self.name)

        # are there any dependencies?
        depfile = os.path.join(self.repo_dir, '_upkg', 'depends')
        logger.debug("depfile? %s", depfile)
        if os.path.exists(depfile):
            logger.debug("Found depends file at %s", depfile)
            deps = open(depfile, 'r')
            dep = deps.readline()
            while dep:
                dep = dep.strip()
                logger.debug("depends: %s", dep)
                self._ctx.add_dep(nice_pkg_name(os.path.basename(dep)), dep)
                dep = deps.readline()
            deps.close()

        for rep in self._ctx.deps_needed:
            repo = Repo(url=rep)
            if repo.installed:
                repo.update()
            else:
                repo.install()
    #install_update_deps()

    def install(self):
        """todo: Docstring for install
        :return:
        :rtype:
        """
        logger.debug("")

        # If we're already installed, don't do anything.
        if self.installed:
            self.pr_info("pkg {} is already installed. Perhaps you want to update it?",
                    self.name)
            return

        self.clone()
        self.install_update_deps()

        logger.debug("Checking for install script")

        inst = os.path.join(self.repo_dir, '_upkg', 'install')
        if os.path.exists(inst):
            cwd = os.getcwd()
            logger.debug("chdir to %s", os.path.join(self.repo_dir, '_upkg'))
            logger.debug("install script is %s", inst)
            self.pr_info("Running install script at {}", inst)
            logger.debug("runnin script %s", inst)
            # We use subprocess instead of the sh module due to problems with
            # runing shell scripts with sh
            os.chdir(os.path.join(self.repo_dir, '_upkg'))
            subprocess.check_call(inst, shell=True)
            os.chdir(cwd)
            self.pr_pass("install script finished")
    #install()

    def remove(self):
        """todo: Docstring for remove
        :return:
        :rtype:
        """
        logger.debug("")

        rd = self.repo_dir

        logger.debug("pkg path %s", rd)
        if not rd:
            print(
                "unable to find pkg '%s'. %s" % (self.name, did_u_mean(self.name))
            )
            return

        # Does the repo have any uncommitted changes?
        # Is the repo out of sync(needs a push?)

        # Are you sure?
        resp = input(self.term.red("Are you sure you want to remove the '%s' pkg? [y|N] " %
                                   self.name))

        if resp == 'y' or resp == 'yes':
            self.pr_atten('removing {}...', self.name)
            shutil.rmtree(rd)

    #remove()

    def update(self):
        """todo: Docstring for update
        :return:
        :rtype:
        """
        logger.debug("")

        rd = self.repo_dir

        logger.debug("pkg path %s", rd)
        if not rd:
            print(
                "unable to find pkg '%s'. %s" % (self.name, did_u_mean(self.name))
            )

        cwd = os.getcwd()
        os.chdir(self.repo_dir)
        logger.debug("cwd: %s, updating %s ", cwd, self.repo_dir)
        try:
            p = git.pull('--rebase', '--progress',
                         _out=self._sh_stdout('blue'),
                         _err=self._sh_stderr('red'))
            p.wait()
        except Exception as e:
            pass
            # logger.warn(e)

        os.chdir(cwd)

        # Update or install any dependancies before running the
        # update script.
        self.install_update_deps()

        up = os.path.join(self.repo_dir, '_upkg', 'update')
        if os.path.exists(up):
            # We use subprocess instead of the sh module due to problems with
            # runing shell scripts with sh
            cwd = os.getcwd()
            os.chdir(os.path.join(self.repo_dir, '_upkg'))
            self.pr_info("Running update script for {} @ {}", self.name, up)
            subprocess.check_call(up, shell=True)
            os.chdir(cwd)
    #update()

    def push(self):
        """todo: Docstring for push
        :return:
        :rtype:
        """

        pass
    #push()

    def status(self):
        """Get status on the repo.
        :return:
        :rtype:
        """

        rd = self.repo_dir

        logger.debug("pkg path %s", rd)
        if not rd:
            print(
                "unable to find pkg '%s'. %s" % (self.name, did_u_mean(self.name))
            )

        cwd = os.getcwd()
        os.chdir(self.repo_dir)
        logger.debug("cwd: %s, getting status %s ", cwd, self.repo_dir)
        try:
            p = git.status(_out=self._sh_stdout('blue'),
                           _err=self._sh_stderr('red'))
            p.wait()
        except Exception:
            pass
            # logger.warn(e)
        os.chdir(cwd)
Exemplo n.º 36
0
#if session.login(self.fail('ErrorType.BAD_USERNAME_OR_PASSWORD')):
    #session = spotify.Session()
    #username = raw_input('Enter username: '******'Enter password: '******'Logging in...')


# port audio sink:

if _platform == "darwin":
    #osx
    audio = spotify.PortAudioSink(session)
elif _platform == "linux" or _platform == "linux2":
    #linux
    audio = spotify.AlsaSink(session)

# Events for coordination
logged_in = threading.Event()
end_of_track = threading.Event()
Exemplo n.º 37
0
class Repo(object):
    """
        Repo object. Perform git operations on a given repo.
    """
    def __init__(self, repo_dir="", name="", url=""):
        """todo: to be defined

        :param repo_dir: arg description
        :type repo_dir: type description
        :param name: arg description
        :type name: type description
        :param url: arg description
        :type url: type description
        """
        logger.debug("repo_dir: %s, name: %s, url: %s", repo_dir, name, url)

        if not (repo_dir or name or url):
            estr = "You must supply at least one of: repo_dir, name, or url."
            logger.error(estr)
            raise InvalidRepo(estr)

        self._repo_dir = repo_dir
        self._name = nice_pkg_name(name)
        self._url = url
        self._basename = ""

        self.supported_schemes = ('git', 'https', 'http', 'file', '')

        self._ctx = upkg_ctx.get_ctx()
        self.term = Terminal()

    #__init__()

    def __repr__(self):
        """todo: Docstring for __repr__
        :return:
        :rtype:
        """

        return "{} URL:{} LOCATION: {}".format(self._name, self._url,
                                               self._repo_dir)

    #__repr__()

    def _build_writer(self, output, color=None, prefix=""):
        if color:

            def _cwriter(line):
                go = getattr(self.term, color)
                output(go(self.name + ": " + prefix + line))

            return _cwriter

        def _writer(line):
            output(self.name + ": " + prefix + line)

        return _writer

    #_build_writer()

    def _sh_stdout(self, *args, **kwargs):
        return self._build_writer(sys.stdout.write, *args, **kwargs)

    #_sh_stdout()

    def _sh_stderr(self, *args, **kwargs):
        return self._build_writer(sys.stderr.write, *args, **kwargs)

    #_sh_stderr()

    def __str__(self):
        """todo: Docstring for __str__
        :return:
        :rtype:
        """

        return "{} : {}".format(self.name, self.url)

    #__str__()

    @property
    def name(self):
        logger.debug("name: %s", self._name)

        if not self._name:
            self._name = nice_pkg_name(self.basename)

        return self._name

    @property
    def repo_dir(self):
        logger.debug("repo_dir: %s", self._repo_dir)

        if not self._repo_dir:
            bn = self.basename
            if bn:
                self._repo_dir = os.path.join(settings.upkg_destdir, bn)

        return self._repo_dir

    @property
    def url(self):
        logger.debug("url: %s", self._url)

        if not self._url:
            rd = self.repo_dir
            out = StringIO()
            cwd = os.getcwd()
            os.chdir(rd)

            p = git('ls-remote',
                    '--get-url',
                    _out=out,
                    _err=self._sh_stderr('red'))
            p.wait()
            os.chdir(cwd)
            self._url = out.getvalue().strip()
            logger.debug("URL? %s", self._url)
            out.close()

        return self._url

    @property
    def basename(self):
        logger.debug("basename: %s, repo_dir: %s", self._basename,
                     self._repo_dir)

        if self._repo_dir:
            self._basename = os.path.basename(self._repo_dir)
        elif self._url:
            url = urlparse(self._url)
            self._basename = os.path.basename(url.path)
        else:
            self._basename = os.path.basename(pkg_name_to_path(self._name))

        return self._basename

    @property
    def installed(self):
        rd = self.repo_dir
        if rd:
            return os.path.exists(rd)

        return rd

    #installed()

    @classmethod
    def installed_list(cls):
        """todo: Docstring for list
        :return:
        :rtype:
        """
        logger.debug("")

        res = []
        for x in repo_dirlist():
            d = x['path']
            try:
                res.append(Repo(repo_dir=d, name=x['base']))
            except InvalidRepo:
                logger.warning(
                    "Invalid repo '%s' in the installation directory.", d)

        return res

    #installed_list()

    def pr_pass(self, fmt, *args, **kwargs):
        print(self.term.green(fmt.format(*args, **kwargs)))

    #pr_pass()

    def pr_info(self, fmt, *args, **kwargs):
        print(self.term.blue(fmt.format(*args, **kwargs)))

    #pr_info()

    def pr_fail(self, fmt, *args, **kwargs):
        print(self.term.red(fmt.format(*args, **kwargs)))

    #pr_fail()

    def pr_atten(self, fmt, *args, **kwargs):
        print(self.term.red(fmt.format(*args, **kwargs)))

    #pr_atten()

    def clone(self):
        """todo: Docstring for clone
        :return:
        :rtype:
        """
        logger.debug("")

        if not self.url:
            estr = "Cannot install this repos without a URL. %s" % self.info()
            logger.warning(estr)
            raise ValueError(estr)

        # check if the already exists, if so, don't install, update it?
        url = urlparse(self.url)
        url_path = url[2]
        path_end = url_path.split('/')
        path_end = path_end[len(path_end) - 1]

        if url.scheme not in self.supported_schemes:
            raise ValueError("Unsupported scheme '{}' for {}".format(
                url.scheme, self.url))

        assert self.repo_dir, "Invalid repo directory."

        # Clone it.
        logger.debug("cloning %s into %s .", self.url, self.repo_dir)
        self.pr_pass("\nInstalling %s ... " % self.url)

        p = git.clone('--progress',
                      self.url,
                      self.repo_dir,
                      _out=self._sh_stdout('blue'),
                      _err=self._sh_stderr('blue'))
        p.wait()

    #clone()

    def install_update_deps(self):
        """todo: Docstring for install_update_deps
        :return:
        :rtype:
        """
        logger.debug("")
        self._ctx.installed(self.name)

        # are there any dependencies?
        depfile = os.path.join(self.repo_dir, '_upkg', 'depends')
        logger.debug("depfile? %s", depfile)
        if os.path.exists(depfile):
            logger.debug("Found depends file at %s", depfile)
            deps = open(depfile, 'r')
            dep = deps.readline()
            while dep:
                dep = dep.strip()
                logger.debug("depends: %s", dep)
                self._ctx.add_dep(nice_pkg_name(os.path.basename(dep)), dep)
                dep = deps.readline()
            deps.close()

        for rep in self._ctx.deps_needed:
            repo = Repo(url=rep)
            if repo.installed:
                repo.update()
            else:
                repo.install()

    #install_update_deps()

    def install(self):
        """todo: Docstring for install
        :return:
        :rtype:
        """
        logger.debug("")

        # If we're already installed, don't do anything.
        if self.installed:
            self.pr_info(
                "pkg {} is already installed. Perhaps you want to update it?",
                self.name)
            return

        self.clone()
        self.install_update_deps()

        logger.debug("Checking for install script")

        inst = os.path.join(self.repo_dir, '_upkg', 'install')
        if os.path.exists(inst):
            cwd = os.getcwd()
            logger.debug("chdir to %s", os.path.join(self.repo_dir, '_upkg'))
            logger.debug("install script is %s", inst)
            self.pr_info("Running install script at {}", inst)
            logger.debug("runnin script %s", inst)
            # We use subprocess instead of the sh module due to problems with
            # runing shell scripts with sh
            os.chdir(os.path.join(self.repo_dir, '_upkg'))
            subprocess.check_call(inst, shell=True)
            os.chdir(cwd)
            self.pr_pass("install script finished")

    #install()

    def remove(self):
        """todo: Docstring for remove
        :return:
        :rtype:
        """
        logger.debug("")

        rd = self.repo_dir

        logger.debug("pkg path %s", rd)
        if not rd:
            print("unable to find pkg '%s'. %s" %
                  (self.name, did_u_mean(self.name)))
            return

        # Does the repo have any uncommitted changes?
        # Is the repo out of sync(needs a push?)

        # Are you sure?
        resp = input(
            self.term.red(
                "Are you sure you want to remove the '%s' pkg? [y|N] " %
                self.name))

        if resp == 'y' or resp == 'yes':
            self.pr_atten('removing {}...', self.name)
            shutil.rmtree(rd)

    #remove()

    def update(self):
        """todo: Docstring for update
        :return:
        :rtype:
        """
        logger.debug("")

        rd = self.repo_dir

        logger.debug("pkg path %s", rd)
        if not rd:
            print("unable to find pkg '%s'. %s" %
                  (self.name, did_u_mean(self.name)))

        cwd = os.getcwd()
        os.chdir(self.repo_dir)
        logger.debug("cwd: %s, updating %s ", cwd, self.repo_dir)
        try:
            p = git.pull('--rebase',
                         '--progress',
                         _out=self._sh_stdout('blue'),
                         _err=self._sh_stderr('red'))
            p.wait()
        except Exception as e:
            pass
            # logger.warn(e)

        os.chdir(cwd)

        # Update or install any dependancies before running the
        # update script.
        self.install_update_deps()

        up = os.path.join(self.repo_dir, '_upkg', 'update')
        if os.path.exists(up):
            # We use subprocess instead of the sh module due to problems with
            # runing shell scripts with sh
            cwd = os.getcwd()
            os.chdir(os.path.join(self.repo_dir, '_upkg'))
            self.pr_info("Running update script for {} @ {}", self.name, up)
            subprocess.check_call(up, shell=True)
            os.chdir(cwd)

    #update()

    def push(self):
        """todo: Docstring for push
        :return:
        :rtype:
        """

        pass

    #push()

    def status(self):
        """Get status on the repo.
        :return:
        :rtype:
        """

        rd = self.repo_dir

        logger.debug("pkg path %s", rd)
        if not rd:
            print("unable to find pkg '%s'. %s" %
                  (self.name, did_u_mean(self.name)))

        cwd = os.getcwd()
        os.chdir(self.repo_dir)
        logger.debug("cwd: %s, getting status %s ", cwd, self.repo_dir)
        try:
            p = git.status(_out=self._sh_stdout('blue'),
                           _err=self._sh_stderr('red'))
            p.wait()
        except Exception:
            pass
            # logger.warn(e)
        os.chdir(cwd)
Exemplo n.º 38
0
parser = argparse.ArgumentParser(description='KMUX Installation Helper.')
parser.add_argument('--list', help='list all modules', action="store_true")
parser.add_argument('--showdeps', action='store_true',
                    help='show depency structure')
parser.add_argument(
    '--genkmux', nargs=2,
    type=str,
    help='generate kmux.config.json from kmux-config-ini.json')
parser.add_argument(
    '--genini',
    help='generate kmux.config.ini',
    action="store_true")
args = parser.parse_args()

if (args.list):
    modules = searchModules()
    print(t.green(str(modules)))
elif (args.showdeps):
    moddict = loadModules()
    graph = getDepGraph(moddict)
    print(t.blue(graph.toDot()))
elif (args.genini):
    globconf = {}
    outdict = {}
    loopOverModules(globconf, Mode.GENINI)
    outdict['config'] = globconf
    print(json.dumps(outdict, indent=True))
elif (args.genkmux):
    config = Json.readJSONFile(args.genkmux[0])
    loopOverModules(config, Mode.GENKMUX, args.genkmux[1])
Exemplo n.º 39
0
class Plotter(object):
    '''
    This is a semi-generic plotting interface that has a built in curses based terminal plotter.
    It's fairly specific to what we're using it for here, but we could (and maybe should) build it out into
    a little library that we can use via the command line to plot things.  Might be useful for looking at data later.
    That would also cut the size of this tool down by a good bit.
    '''
    #def __init__(self, kinavg, kinrw, iteration, bin_labels, state_labels, state_pops, bin_pops, interface='matplotlib'):
    def __init__(self, h5file, h5key, iteration=-1, interface='matplotlib'):
        # Need to sort through and fix all this, but hey.
        self.iteration = iteration
        # These two are important for... reasons.
        try:
            self.bin_labels = list(bin_labels[...])
            self.state_labels = list(state_labels[...]) + ['unknown']
        except:
            try:
                self.state_labels = list(h5file['state_labels'][...]) + ['unknown']
            except:
                self.state_labels = None
        # unless we totally fail out.
        self.interface = interface
        # What we should ACTUALLY do is just... yeah, just have it sub in what we need.
        # We'll need to throw in the state labels or whatever, but.
        self.h5file = h5file
        self.h5key = h5key
        # We should determine the number of dimensions of our dataset...
        # This has time data, so an i to j is a 3 dim, and an i is 2.
        try:
            self.dim = len(h5file[h5key].shape)
        except:
            self.dim = 1
        try:
            # does the ci exist?
            a = h5file[h5key]['expected']
        except:
            self.dim = 1

    def plot(self, i=0, j=1, tau=1, iteration=None, dim=0, interface=None):
        if iteration == None:
            iteration = self.iteration
            self.__generic_ci__(self.h5file, iteration, i, j, tau=tau, h5key=self.h5key, dim=dim, interface=interface)

    def __generic_ci__(self, h5file, iteration, i, j, tau, h5key='rate_evolution', dim=0, interface=None):
        # This function just calls the appropriate plot function for our available
        # interface.
        if (interface == None and self.interface == 'text') or interface == 'text':
            if self.dim > 1:
                self.__terminal_ci__(h5file, iteration, i, j, tau, h5key)
            else:
                self.__terminal_expected__(h5file, iteration, i, j, tau, h5key, dim)
        else:
            try:
                import matplotlib
                matplotlib.use('TkAgg')
                from matplotlib import pyplot as plt
                if self.dim == 3:
                    plt.plot(h5file[h5key]['expected'][:iteration, i, j] / tau, color='black')
                    plt.plot(h5file[h5key]['ci_ubound'][:iteration, i, j] / tau, color='grey')
                    plt.plot(h5file[h5key]['ci_lbound'][:iteration, i, j] / tau, color='grey')
                else:
                    plt.plot(h5file[h5key]['expected'][:iteration, i] / tau, color='black')
                    plt.plot(h5file[h5key]['ci_ubound'][:iteration, i] / tau, color='grey')
                    plt.plot(h5file[h5key]['ci_lbound'][:iteration, i] / tau, color='grey')
                plt.show()
            except:
                print('Unable to import plotting interface.  An X server ($DISPLAY) is required.')
                if self.dim > 1:
                    self.__terminal_ci__(h5file, iteration, i, j, tau)
                else:
                    self.__terminal_expected__(h5file, iteration, i, j, tau, h5key, dim)
                return 1

    def __generic_histo__(self, vector, labels):
        # This function just calls the appropriate plot function for our available
        # interface.  Same thing as generic_ci, but for a histogram.
        if self.interface == 'text':
            self.__terminal_histo__(vector, labels)
        else:
            try:
                import matplotlib
                matplotlib.use('TkAgg')
                from matplotlib import pyplot as plt
                plt.bar(range(0, np.array(vector).shape[0]), vector, linewidth=0, align='center', color='gold', tick_label=labels)
                plt.show()
            except:
                print('Unable to import plotting interface.  An X server ($DISPLAY) is required.')
                self.__terminal_histo__(h5file, vector, labels)
                return 1

    def __terminal_histo__(self, vector, labels, fullscreen_mode=True):
        from blessings import Terminal

        self.t = Terminal()
        h = int(self.t.height / 4) * 3
        w = self.t.width
        cols = np.array(vector).shape[0]
        # Let's print this business!

        colwidth = w / cols
        with self.t.fullscreen():
            for y in range(0, h):
                for x in range(0, cols):
                    if x == 0:
                        with self.t.location(0, y):
                            print(self.t.red('{0:.4f}|'.format(float(h-y)/float(h))))
                    with self.t.location((x*colwidth)+8+len(labels[x])/2, y):
                        if vector[x] >= (float(h-y)/float(h)):
                            #print(float(h-y)/float(h))
                            print(self.t.on_blue(' '))
            for x in range(0, cols):
                if x == 0:
                    with self.t.location(x, h):
                        print('States| ')
                with self.t.location((x*colwidth)+8, h):
                    print(self.t.blue(labels[x]))

            if fullscreen_mode:
                raw_input("Press enter to continue.")

    def __terminal_ci__(self, h5file, iteration, si, sj, tau, h5key):
        from blessings import Terminal

        self.t = Terminal()
        h = int(self.t.height / 4 * 3.75)
        # We'll figure out how to subsample the timepoints...
        w = self.t.width
        if self.dim == 3:
            in_tup = (iteration-1, si, sj)
        else:
            in_tup = (iteration-1, si)
        yupper = (h5file[h5key]['ci_ubound'][in_tup] / tau) * 2
        ylower = (h5file[h5key]['ci_lbound'][in_tup] / tau) / 2
        # Here are points pertaining to height.
        scale = np.array([0.0] + [ylower+i*(yupper-ylower)/np.float(h) for i in range(0, h)])[::-1]
        if iteration > w:
            block_size = iteration / w
        else:
            block_size = 1

        with self.t.fullscreen():
            try:
                for x in range(0, w-12):
                    iter = x * block_size
                    if self.dim == 3:
                        in_tup = (iter-1, si, sj)
                    else:
                        in_tup = (iter-1, si)
                    yupper = (h5file[h5key]['ci_ubound'][in_tup] / tau)
                    ylower = (h5file[h5key]['ci_lbound'][in_tup] / tau)
                    ci = np.digitize([yupper, ylower], scale)
                    if x == 0:
                        for y in range(0, h+1):
                            with self.t.location(0, y):
                                print(self.t.bold(self.t.red('{0:.7f}|'.format(scale[y]))))
                    for y in range(ci[0], ci[1]):
                        #with self.t.location(x+12, y):
                        print self.t.move(y, x+12) + self.t.on_blue(' ')
                            #print(self.t.on_blue(' '))
                    #with self.t.location(x+12, np.digitize(h5file['rate_evolution']['expected'][iter-1, si, sj]/tau, scale)):
                    #        print(self.t.on_blue('-'))
                    print self.t.move(np.digitize(h5file[h5key]['expected'][in_tup]/tau, scale), x+12) + self.t.on_blue('-')

                for x in range(0, w-12, w/10):
                    if x == 0:
                        with self.t.location(x, h+1):
                            print('Iteration| ')
                    with self.t.location(x+12, h+1):
                        iter = x * block_size
                        print(self.t.blue(str(iter)))
            except:
                pass

            with self.t.location(0, h+2):
                # We need to improve this.
                #if h5key == 'rate_evolution':
                #    print("k_ij from {} to {} from iter 1 to {}".format(self.state_labels[si], self.state_labels[sj], self.iteration))
                #elif h5key == 'conditional_flux_evolution':
                #    print("i->j flux from {} to {} from iter 1 to {}".format(self.state_labels[si], self.state_labels[sj], self.iteration))
                if self.dim == 3:
                    print("{} from {} to {} from iter 1 to {}".format(h5key, self.state_labels[si], self.state_labels[sj], self.iteration))
                else:
                    print("{} of state {} from iter 1 to {}".format(h5key, self.state_labels[si], self.iteration))
            with self.t.location(0, h+3):
                raw_input("Press enter to continue.")

    def __terminal_expected__(self, h5file, iteration, si, sj, tau, h5key, dim):
        from blessings import Terminal

        self.t = Terminal()
        h = int(self.t.height / 4 * 3.75)
        # We'll figure out how to subsample the timepoints...
        w = self.t.width
        if self.dim == 3:
            in_tup = (iteration-1, si, sj)
        else:
            in_tup = (iteration-1, si)
        in_tup = (iteration-1, dim)
        try:
            yupper = (np.max(h5file) / tau) * 2
        except:
            in_tup = (iteration-1)
            yupper = (np.max(h5file) / tau) * 2
        ylower = (np.min(h5file) / tau) * 2
        # Here are points pertaining to height.
        if yupper > 0:
            yupper = (np.max(h5file) / tau) * 1.2
            ylower = (np.min(h5file) / tau) / 2
            scale = np.array([0.0] + [ylower+i*(yupper-ylower)/np.float(h) for i in range(0, h)])[::-1]
        else:
            yupper = (np.max(h5file) / tau) / 2
            ylower = (np.min(h5file) / tau) * 1.2
            scale = np.array([ylower+i*(yupper-ylower)/np.float(h) for i in range(0, h)] + [0.0])[::-1]
        if iteration > w:
            block_size = iteration / w
        else:
            block_size = 1

        with self.t.fullscreen():
            try:
                for x in range(0, w-12):
                    iter = x * block_size
                    if self.dim == 3:
                        in_tup = (iter-1, si, sj)
                    else:
                        in_tup = (iter-1, si)
                    in_tup = (iter-1, dim)
                    try:
                        yupper = (h5file[in_tup] / tau)
                    except:
                        in_tup = (iter-1)
                        yupper = (h5file[in_tup] / tau)
                    ylower = (h5file[in_tup] / tau)
                    ci = np.digitize([yupper, ylower], scale)
                    if x == 0:
                        for y in range(0, h+1):
                            with self.t.location(0, y):
                                print(self.t.bold(self.t.red('{0:.7f}|'.format(scale[y]))))
                    for y in range(ci[0], ci[1]):
                        print self.t.move(y, x+12) + self.t.on_blue(' ')
                            #print(self.t.on_blue(' '))
                    print self.t.move(np.digitize(h5file[in_tup]/tau, scale), x+12) + self.t.on_blue('-')

                for x in range(0, w-12, w/10):
                    if x == 0:
                        with self.t.location(x, h+1):
                            print('Iteration| ')
                    with self.t.location(x+12, h+1):
                        iter = x * block_size
                        print(self.t.blue(str(iter)))
            except:
                pass

            with self.t.location(0, h+2):
                # We need to improve this.
                print("{} from iter 1 to {}".format(h5key, self.iteration))
            with self.t.location(0, h+3):
                raw_input("Press enter to continue.")
Exemplo n.º 40
0
    s = State(x0, y0, False)
    next(controller)
    for i in range(rounds):
        yield s
        a = controller.send(s)
        s = move(s, a)


def unpack_stim_result(out):
    xy = out.splitlines()[0].split()[1]
    return int(xy[:3], 2), int(xy[3:], 2)


# Necessary because of magic TERM object does.
COLORIZE = (
    lambda x: TERM.blue(x),
    lambda x: TERM.yellow(x),
    lambda x: TERM.magenta(x),
    lambda x: TERM.red(x),
    lambda x: x,
)


def unpack_sense_stim_result(out):
    val = out.splitlines()[0].split()[1]
    if val.count(b'1') > 1:
        raise RuntimeError("unexpected sensor value")
    idx = 4 if val.count(b'1') == 0 else val.index(b'1')
    return COLORIZE[idx]

Exemplo n.º 41
0
class Cmd(object):
    """Docstring for PkgrExec """

    command = ''

    def __init__(self, *args, command=None):
        """todo: to be defined

        :param pkgr: arg description
        :type pkgr: type description
        :param args: arg description
        :type args: type description
        """
        self.command = command
        self._args = args

        self.term = Terminal()
    # __init__()

    def __repr__(self):
        return "{} {}".format(
            self._command,
            self._args,
        )
    # __repr__()

    def __str__(self):
        return self.__repr__()
    # __str__()

    def _build_writer(self, output, color=None, prefix=""):
        if color:

            def _cwriter(line):
                go = getattr(self.term, color)
                output.write(go(prefix + line))
                output.flush()

            return _cwriter

        def _writer(line):
            output.write(prefix + line)
            output.flush()

        return _writer
    # _build_writer()

    def _sh_stdout(self, *args, **kwargs):
        return self._build_writer(sys.stdout, *args, **kwargs)
    # _sh_stdout()

    def _sh_stderr(self, *args, **kwargs):
        return self._build_writer(sys.stderr, *args, **kwargs)
    # _sh_stderr()

    def pr_pass(self, fmt, *args, **kwargs):
        print(self.term.green(fmt.format(*args, **kwargs)))
    # pr_pass()

    def pr_info(self, fmt, *args, **kwargs):
        print(self.term.blue(fmt.format(*args, **kwargs)))
    # pr_info()

    def pr_fail(self, fmt, *args, **kwargs):
        print(self.term.red(fmt.format(*args, **kwargs)))
    # pr_fail()

    def pr_atten(self, fmt, *args, **kwargs):
        print(self.term.red(fmt.format(*args, **kwargs)))
    # pr_atten()

    @property
    def command(self):
        return self.command
    # cmd()

    def execute(self):
        logger.debug("%s %s", self.command, self._args)
        try:
            subprocess.check_call(
                (self.command,) + self._args,
            )
        except subprocess.CalledProcessError as e:
            sys.exit(e.returncode)
Exemplo n.º 42
0

if __name__ == "__main__":
    current_datetime = datetime.now(pytz.timezone('US/Eastern'))
    github_sha = os.getenv("GITHUB_SHA")
    if github_sha:
        try:
            commit_date = subprocess.check_output(
                "git show -s --format=%cd --date=iso-strict {}".format(
                    github_sha),
                shell=True,
                stderr=subprocess.STDOUT,
                universal_newlines=True)
            commit_date = commit_date.strip()
            print(
                term.blue("Commit timestamp: " + commit_date + " @" +
                          github_sha))
            current_datetime = datetime.fromisoformat(commit_date)
        except subprocess.CalledProcessError as e:
            print(
                term.blue("Get commit timestamp error: \"" + e.output +
                          "\". Using current date and time."))

    tests = read_json()
    total_pts = 0.0
    available_pts = 0.0
    idx = 0
    for t in tests['tests']:
        total_pts += run_test(t, idx)
        available_pts += t['points']
        idx += 1
    print()
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import accuracy_score
from sklearn.cross_validation import StratifiedKFold
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import LabelEncoder

t = Terminal()

dataset = np.genfromtxt('car.data', delimiter=',', dtype=None)

n_samples = dataset.shape[0]
n_features = dataset.shape[1]
print(t.blue("Car Evaluation dataset: %d amostras(%d características)" % (dataset.shape[0], n_features)))

target = dataset[:,-1]
dataset = dataset[:,0:-1]

tle = LabelEncoder()
target = tle.fit_transform(target)
#print(np.unique(target))
#print(list(tle.classes_))
#exit()

labels_encoders = []
for idx in range(0, n_features-1):
    le = LabelEncoder()
    labels_encoders += [le]
    e = le.fit_transform(dataset[:,idx])