<commit_before>#!/usr/bin/python

import sys
import os
import base64

PKG=os.popen("opkg list-installed | grep \"python-requests\" | wc -l")
if PKG == 0:
	print "Updating opkg repository..."
	os.system("opkg update > /dev/null")
	print "Installing packages for python"
	os.system("opkg install python-requests > /dev/null")
else:
	print "Required packages are already installed..."

import requests
import json

HELP="Usage: python "+sys.argv[0]+" -h|id \n\
\t -h - optional - prints this help message\n\
\t id - id of adapter"

serverAddress = "http://ant-2.fit.vutbr.cz:1338"

# return MAC address of eth0
def getMAC():
	MAC=os.popen("ifconfig -a | grep \"eth[0-9]*\" | grep -o \"HWaddr [0-9A-F:]*\" | grep -o \"[0-9A-F:]*\"").read()
	MAC = MAC.replace('\n', '')

	return MAC

# return security ID of CPU
def getSID():
	SID_1=os.popen("devmem2 0x01c23800 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_2=os.popen("devmem2 0x01c23804 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_3=os.popen("devmem2 0x01c23808 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_4=os.popen("devmem2 0x01c2380c | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()

	SID_1 = SID_1.replace('\n', '')
	SID_2 = SID_2.replace('\n', '')
	SID_3 = SID_3.replace('\n', '')
	SID_4 = SID_4.replace('\n', '')
	SID=SID_1[2:] + SID_2[2:] + SID_3[2:] + SID_4[2:]

	return SID

# saves adapter specified by id, mac and secure_id
def save_adapter(mac, sid):
	headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
	data = {
		"secure_id": sid,
		"lan_mac": mac
	}

	res = requests.post(serverAddress + "/api/adapter/create", data = json.dumps(data), headers = headers)

	return res.json() # return adapter json

if __name__ == '__main__':
	if len(sys.argv) > 1 and sys.argv[1] == "-h":
		print HELP
		sys.exit(0)

	MAC = getMAC()
	SID = getSID()
	adapter = save_adapter(MAC, SID)

	ID = adapter["adapter_id"]
	cert = base64.b64decode(adapter["cert"])
	key = base64.b64decode(adapter["key"])
	pan_id = adapter["pan_id"]  # [3E, 29. C3, 39]

	with open("/etc/openvpn/client.crt", "w") as cert_file:
		cert_file.write(cert)

	with open("/etc/openvpn/client.key", "w") as key_file:
		key_file.write(key)

	with open("/etc/beeeon/fitprotocold.ini", "w") as conf_file:
		edids = "edid0=0x{0:02x}\nedid0=0x{1:02x}\nedid0=0x{2:02x}\nedid0=0x{3:02x}".format(pan_id[0], pan_id[1], pan_id[2], pan_id[3])
		conf_file.write("[net_config]\nchannel=28\n"+edids)

	print "\tID adapteru je:", ID
	print "\tMAC adresa je: ", MAC
	print "\tSID cislo je:  ", SID
	print "\tPAN id je:     ", pan_id

	EEPROM_DATA_VERIFICATION = "ad"
	EEPROM_DATA_VERSION = "01"

	EEPROM_DATA = EEPROM_DATA_VERIFICATION + EEPROM_DATA_VERSION

	coded_adapter_id = "{0:#0{1}x}".format(int(ID), 16)[2:]

	length_adapter_id = len(coded_adapter_id)/2
	if ((len(coded_adapter_id)%2)  == 1) :
		length_adapter_id += 1

	EEPROM_DATA += "01" + str(format(length_adapter_id, '02x')) + coded_adapter_id
	EEPROM_DATA += "020100fe"

	#print "EEPROM data: ",  EEPROM_DATA, "(ID ADA HEX 0x" + coded_adapter_id + ")"
	eeprom = open("/sys/devices/platform/soc@01c00000/1c2b000.i2c/i2c-1/1-0050/eeprom", 'w')

	hex_data = EEPROM_DATA.decode("hex")
	#print "\tEEPROM_DATA_VERIFICATION: ", EEPROM_DATA_VERIFICATION
	#print "\tEEPROM_DATA_VERSION     : ", EEPROM_DATA_VERSION
	#print "\tEEPROM_DATA - ADAPTER_ID: ", (ID + " (0x" + coded_adapter_id + ")")
	#print "\tEEPROM_DATA TOGETHER    : ", EEPROM_DATA
	print "Zapisuji AdapterID + dalsi atributy do EEPROM pameti:"
	print "\tEEPROM data: ",  EEPROM_DATA
	eeprom.write(hex_data)
	eeprom.close()

	print "Hotovo, finish!"
<commit_msg>[PAN] Corrected print to file<commit_after>#!/usr/bin/python

import sys
import os
import base64

PKG=os.popen("opkg list-installed | grep \"python-requests\" | wc -l")
if PKG == 0:
	print "Updating opkg repository..."
	os.system("opkg update > /dev/null")
	print "Installing packages for python"
	os.system("opkg install python-requests > /dev/null")
else:
	print "Required packages are already installed..."

import requests
import json

HELP="Usage: python "+sys.argv[0]+" -h|id \n\
\t -h - optional - prints this help message\n\
\t id - id of adapter"

serverAddress = "http://ant-2.fit.vutbr.cz:1338"

# return MAC address of eth0
def getMAC():
	MAC=os.popen("ifconfig -a | grep \"eth[0-9]*\" | grep -o \"HWaddr [0-9A-F:]*\" | grep -o \"[0-9A-F:]*\"").read()
	MAC = MAC.replace('\n', '')

	return MAC

# return security ID of CPU
def getSID():
	SID_1=os.popen("devmem2 0x01c23800 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_2=os.popen("devmem2 0x01c23804 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_3=os.popen("devmem2 0x01c23808 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_4=os.popen("devmem2 0x01c2380c | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()

	SID_1 = SID_1.replace('\n', '')
	SID_2 = SID_2.replace('\n', '')
	SID_3 = SID_3.replace('\n', '')
	SID_4 = SID_4.replace('\n', '')
	SID=SID_1[2:] + SID_2[2:] + SID_3[2:] + SID_4[2:]

	return SID

# saves adapter specified by id, mac and secure_id
def save_adapter(mac, sid):
	headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
	data = {
		"secure_id": sid,
		"lan_mac": mac
	}

	res = requests.post(serverAddress + "/api/adapter/create", data = json.dumps(data), headers = headers)

	return res.json() # return adapter json

if __name__ == '__main__':
	if len(sys.argv) > 1 and sys.argv[1] == "-h":
		print HELP
		sys.exit(0)

	MAC = getMAC()
	SID = getSID()
	adapter = save_adapter(MAC, SID)

	ID = adapter["adapter_id"]
	cert = base64.b64decode(adapter["cert"])
	key = base64.b64decode(adapter["key"])
	pan_id = adapter["pan_id"]  # [3E, 29. C3, 39]

	with open("/etc/openvpn/client.crt", "w") as cert_file:
		cert_file.write(cert)

	with open("/etc/openvpn/client.key", "w") as key_file:
		key_file.write(key)

	with open("/etc/beeeon/fitprotocold.ini", "w") as conf_file:
		edids = "edid0=0x{0}\nedid0=0x{1}\nedid0=0x{2}\nedid0=0x{3}".format(pan_id[0], pan_id[1], pan_id[2], pan_id[3])
		conf_file.write("[net_config]\nchannel=28\n"+edids)

	print "\tID adapteru je:", ID
	print "\tMAC adresa je: ", MAC
	print "\tSID cislo je:  ", SID
	print "\tPAN id je:     ", pan_id

	EEPROM_DATA_VERIFICATION = "ad"
	EEPROM_DATA_VERSION = "01"

	EEPROM_DATA = EEPROM_DATA_VERIFICATION + EEPROM_DATA_VERSION

	coded_adapter_id = "{0:#0{1}x}".format(int(ID), 16)[2:]

	length_adapter_id = len(coded_adapter_id)/2
	if ((len(coded_adapter_id)%2)  == 1) :
		length_adapter_id += 1

	EEPROM_DATA += "01" + str(format(length_adapter_id, '02x')) + coded_adapter_id
	EEPROM_DATA += "020100fe"

	#print "EEPROM data: ",  EEPROM_DATA, "(ID ADA HEX 0x" + coded_adapter_id + ")"
	eeprom = open("/sys/devices/platform/soc@01c00000/1c2b000.i2c/i2c-1/1-0050/eeprom", 'w')

	hex_data = EEPROM_DATA.decode("hex")
	#print "\tEEPROM_DATA_VERIFICATION: ", EEPROM_DATA_VERIFICATION
	#print "\tEEPROM_DATA_VERSION     : ", EEPROM_DATA_VERSION
	#print "\tEEPROM_DATA - ADAPTER_ID: ", (ID + " (0x" + coded_adapter_id + ")")
	#print "\tEEPROM_DATA TOGETHER    : ", EEPROM_DATA
	print "Zapisuji AdapterID + dalsi atributy do EEPROM pameti:"
	print "\tEEPROM data: ",  EEPROM_DATA
	eeprom.write(hex_data)
	eeprom.close()

	print "Hotovo, finish!"
<|endoftext|>


<commit_before>import sqlite3
import urllib2
import lxml
import xml.etree.cElementTree as ET
from lxml import etree
import sys
import math

db = sqlite3.connect('data.db')

def sections_to_faresections(journey):
    fare_sections = []

    from_station = None
    to_station = None
    last_operator = None
    for section in journey:
        if from_station is None:
            from_station = section['fromStation']
            to_station = section['toStation']
            last_operator = section['operator']
        elif last_operator != section['operator']:
            fare_sections.append( {'fromStation' : from_station, 'toStation' : to_station, 'operator' : last_operator} )
            from_station = section['fromStation']
            to_station = section['toStation']
            last_operator = section['operator']
        else:
            to_station = section['toStation']
    fare_sections.append( {'fromStation' : from_station, 'toStation' : to_station, 'operator' : last_operator} )
    return fare_sections

def fare_for_section(fare_section,first_section=False):
    c = db.cursor()
    c.execute("""
SELECT d.distance,fareunits,price_2ndfull,price_1stfull,
       max(d.distance,coalesce(d.min_distance,0)) * c.price_first  as totalkmprice_1,
       max(d.distance,coalesce(d.min_distance,0)) * c.price_second as totalkmprice_2,
       c.price_first,
       c.price_second,
       min_fare,
       entrance_fee
FROM distance d JOIN fareunit_price fp ON (fp.distance = d.distance OR (fp.distance = 250 AND d.distance > 250))
                JOIN concession c USING (concession)
WHERE from_station = ? AND to_station = ? AND operator = ?; """,(fare_section['fromStation'],
                                                                 fare_section['toStation'],
                                                                 fare_section['operator'])   )
    res = c.fetchall()
    if len(res) == 0:
        return None #No fare found
    if len(res) > 1:
        raise Exception('NS Multiple fares found')
    distance,fareunits,price_2ndfull,price_1stfull,totalkmprice_1,totalkmprice_2,price_first,price_second,min_fare,entrance_fee = res[0]
    c.close()
    if fareunits:
        fare_section['price_first'] = price_1stfull
        fare_section['price_second'] = price_2ndfull
        fare_section['fare_distance'] = distance
        fare_section['_fare_units'] = True
        fare_section['_km-price_first'] = None
        fare_section['_km-price_second'] = None
        return fare_section
    else:
        if first_section:
           totalkmprice_2 += entrance_fee
        if first_section and price_first != 'NULL':
           totalkmprice_1 += entrance_fee
        if min_fare != 'NULL' and min_fare > totalkmprice_1:
            totalkmprice_1 = min_fare
        if min_fare != 'NULL' and min_fare > totalkmprice_2:
            totalkmprice_2 = min_fare
        if price_first == 'NULL' or totalkmprice_1 is None:
            totalkmprice_1 = None
        else:
            fare_section['price_first'] = int(round(totalkmprice_1,0))
        fare_section['price_second'] = int(round(totalkmprice_2,0))
        fare_section['fare_distance'] = distance
        fare_section['_fare_units'] = False
        fare_section['_km-price_first'] = price_first
        fare_section['_km-price_second'] = price_second
        return fare_section

def calculate_fare_of_sections(journey):
    journey['fare_distance'] = 0
    journey['price_second'] = 0
    journey['price_first'] = 0

    for i,section in enumerate(journey['faresections']):
        fare_section = fare_for_section(section,first_section=(i==0))
        if fare_section is None:
            print section
            raise Exception('fare not found')
        journey['fare_distance'] += section['fare_distance']
        journey['price_second'] += section['price_second']
        if 'price_first' in section:
            journey['price_first'] += section['price_first']
        else:
            journey['price_first'] += section['price_second']

def unitprice(distance):
    c = db.cursor()
    c.execute("""
SELECT price_1stfull,price_2ndfull FROM fareunit_price WHERE distance = ? or (? > 250 and distance = 250)""",(distance,)*2)
    return c.fetchone()

def lak(after_fareunits):
    if after_fareunits <= 40:
        return 1
    elif after_fareunits <= 80:
        return 0.9680
    elif after_fareunits <= 100:
        return 0.8470
    elif after_fareunits <= 120:
        return 0.7
    elif after_fareunits <= 150:
        return 0.48
    elif after_fareunits <= 200:
        return 0.4
    elif after_fareunits <= 250:
        return 0.15
    elif after_fareunits > 250:
        return 0

def apply_discount(journey):
    fareunits_passed = 0
    journey['price_second'] = 0
    journey['price_first'] = 0

    for i,fare_section in enumerate(journey['faresections']):
        if i != 0:
            if fare_section['_fare_units']:
                first_full,second_full = unitprice(fareunits_passed+fare_section['fare_distance'])
                first_passed,second_passed = unitprice(fareunits_passed)
                fare_section['price_second'] = second_full-second_passed
                fare_section['price_first'] = first_full-first_passed
            else:
                km_price_second = fare_section['_km-price_second'] * lak(fareunits_passed)
                fare_section['price_second'] = int(km_price_second * fare_section['fare_distance'])

                km_price_first = fare_section['_km-price_first'] * lak(fareunits_passed)
                fare_section['price_first'] = int(km_price_first * fare_section['fare_distance'])

        fareunits_passed += fare_section['fare_distance']
        journey['price_second'] += fare_section['price_second']
        journey['price_first'] += fare_section['price_first']
        #for key,value in fare_section.items() :
            #if key.startswith('_') : del fare_section[key] #Remove hidden values
    return journey

"""
Input a journey, a list of sections. A section is a dict, containing fromStation, toStation, operator.
Stationcodes have to be lowercase
"""
def calculate_fare(journey):
    journey['faresections'] = sections_to_faresections(journey['sections'])
    calculate_fare_of_sections(journey)
    if len(journey['faresections']) == 1:
        for key,value in journey['faresections'][0].items() :
            if key.startswith('_') : del journey['faresections'][0][key] #Remove hidden values
        return journey
    return apply_discount(journey)

def calculate_journey(from_station,to_station):
    url = 'http://ews-rpx.ns.nl/mobile-api-planner?fromStation=%s&toStation=%s&departure=true&dateTime=2014-09-11T18:23&previousAdvices=6&nextAdvices=6&passing=true' % (from_station,to_station)
    req = urllib2.Request(url)
    req.add_header("Authorization", "Basic %s" % 'YW5kcm9pZDptdmR6aWc=')
    response = urllib2.urlopen(req)
    root = etree.parse(response)
    for reismogelijkheid in root.findall(".//ReisMogelijkheid"):
        journey = {'sections' : []}
        for reisdeel in reismogelijkheid.findall(".//ReisDeel"):
           reisstops = reisdeel.findall(".//ReisStop")
           section = {'fromStation' : reisstops[0].find('Code').text.lower(),
                      'toStation'   : reisstops[-1].find('Code').text.lower(),
                      'operator'    : reisdeel.find('Vervoerder').text.replace('Arriva','ARR').replace('Syntus','SYNTUS').replace('Valleilijn','CXX').replace('NS International','NS').replace('GVB','NS').replace('R-net','NS').replace('Breng','BRENG').replace('Veolia','VTN')}
           journey['sections'].append(section)
        fare = calculate_fare(journey)
        print '------------------'
        for prijs in reismogelijkheid.findall(".//Prijs"):
            if prijs.get('korting') != '0':
                continue
            second_matches=False
            first_matches=False
            if prijs.get('klasse') == '1':
                ns_prijs = int(float(prijs.text)*100)
                print 'NS-prijs %s Eigenprijs %s Match = %s, diff %s' % (ns_prijs,fare['price_first'],ns_prijs==fare['price_first'],ns_prijs-fare['price_first'])
            if prijs.get('klasse') == '2':
                ns_prijs = int(float(prijs.text)*100)
                print 'NS-prijs %s Eigenprijs %s Match = %s, diff %s' % (ns_prijs,fare['price_second'],ns_prijs==fare['price_second'],ns_prijs-fare['price_second'])

calculate_journey(sys.argv[1],sys.argv[2])
<commit_msg>Refactor<commit_after>import sqlite3
import urllib2
import lxml
import xml.etree.cElementTree as ET
from lxml import etree
import sys
import math

db = sqlite3.connect('data.db')

def sections_to_faresections(journey):
    fare_sections = []

    from_station = None
    to_station = None
    last_operator = None
    for section in journey:
        if from_station is None:
            from_station = section['fromStation']
            to_station = section['toStation']
            last_operator = section['operator']
        elif last_operator != section['operator']:
            fare_sections.append( {'fromStation' : from_station, 'toStation' : to_station, 'operator' : last_operator} )
            from_station = section['fromStation']
            to_station = section['toStation']
            last_operator = section['operator']
        else:
            to_station = section['toStation']
    fare_sections.append( {'fromStation' : from_station, 'toStation' : to_station, 'operator' : last_operator} )
    return fare_sections

def unitprice(distance):
    c = db.cursor()
    c.execute("""
SELECT price_1stfull,price_2ndfull FROM fareunit_price WHERE distance = ? or (? > 250 and distance = 250)""",(distance,)*2)
    return c.fetchone()

def fare_for_section(fare_section,first_section=False):
    c = db.cursor()
    c.execute("""
SELECT d.distance,fareunits,price_2ndfull,price_1stfull,
       c.price_first,
       c.price_second,
       min_fare,
       entrance_fee,
       coalesce(min_distance,0)
FROM distance d JOIN fareunit_price fp ON (fp.distance = d.distance OR (fp.distance = 250 AND d.distance > 250))
                JOIN concession c USING (concession)
WHERE from_station = ? AND to_station = ? AND operator = ?; """,(fare_section['fromStation'],
                                                                 fare_section['toStation'],
                                                                 fare_section['operator'])   )
    res = c.fetchall()
    if len(res) == 0:
        return None #No fare found
    if len(res) > 1:
        raise Exception('NS Multiple fares found')
    distance,fareunits,price_2ndfull,price_1stfull,price_first,price_second,min_fare,entrance_fee,min_distance = res[0]
    c.close()
    if fareunits:
        return (True,distance,price_1stfull,price_2ndfull,None,None,None,None,None)
    else:
        if price_first is None or price_first == 'NULL':
            price_first = None
        return (False,distance,None,None,price_first,price_second,int(entrance_fee),min_fare,min_distance)

def lak_factor(ceiling):
    if ceiling <= 40:
        return 1
    elif ceiling <= 80:
        return 0.9680
    elif ceiling <= 100:
        return 0.8470
    elif ceiling <= 120:
        return 0.7
    elif ceiling <= 150:
        return 0.48
    elif ceiling <= 200:
        return 0.4
    elif ceiling <= 250:
        return 0.15
    elif ceiling > 250:
        return 0

def compute_total_km_fare(km_price,distance,units_passed):
    fare = 0.0
    for stage_ceiling in [40,80,100,120,150,200,250]:
        if distance == 0:
            break
        capacity = stage_ceiling-units_passed
        if capacity < 0:
            continue
        fare += lak_factor(stage_ceiling)*km_price*min(capacity,distance)
        distance -= min(capacity,distance)
    #Above 250 free
    return fare

def magic_round(price,operator):
    return int(round(price))

"""
Input a journey, a list of sections. A section is a dict, containing fromStation, toStation, operator.
Stationcodes have to be lowercase
"""
def calculate_fare(journey):
    journey['faresections'] = sections_to_faresections(journey['sections'])
    fareunits_passed = 0
    price_second = 0
    price_first = 0
    for i,fare_section in enumerate(journey['faresections']):
        fare_unit,distance,price_1stfull,price_2ndfull,kmprice_first,kmprice_second,entrance_free,min_fare,min_distance = fare_for_section(fare_section)
        fare_section['fare_distance'] = distance
        if fare_unit:
            if fareunits_passed > 0:
                complete_1stfull,complete_2ndfull = unitprice(distance+fareunits_passed)
                passed_1stfull,passed_2ndfull = unitprice(fareunits_passed)
                fare_section['price_first'] = complete_1stfull-passed_1stfull
                fare_section['price_second'] = complete_2ndfull-passed_2ndfull
            else:
                fare_section['price_first'] = price_1stfull
                fare_section['price_second'] = price_2ndfull
        else:
            section_distance = distance
            if distance+fareunits_passed < min_distance:
                section_distance = min_distance
            if kmprice_first is None: #No known KM price for 1st class, fallback to 2nd
                fare_section['price_first']  = magic_round(compute_total_km_fare(kmprice_second,section_distance,fareunits_passed),fare_section['operator'])
            else:
                fare_section['price_first']  = magic_round(compute_total_km_fare(kmprice_first,section_distance,fareunits_passed),fare_section['operator'])
            fare_section['price_second'] = magic_round(compute_total_km_fare(kmprice_second,section_distance,fareunits_passed),fare_section['operator'])
            if i==0:
                fare_section['price_first'] += entrance_free
                fare_section['price_second'] += entrance_free

        fareunits_passed += fare_section['fare_distance']
        price_first += fare_section['price_first']
        price_second += fare_section['price_second']

    journey['fare_distance'] = fareunits_passed
    journey['price_second'] = price_second
    journey['price_first'] = price_first
    return journey

def calculate_journey(from_station,to_station):
    url = 'http://ews-rpx.ns.nl/mobile-api-planner?fromStation=%s&toStation=%s&departure=true&dateTime=2014-09-11T18:23&previousAdvices=6&nextAdvices=6&passing=true' % (from_station,to_station)
    req = urllib2.Request(url)
    req.add_header("Authorization", "Basic %s" % 'YW5kcm9pZDptdmR6aWc=')
    response = urllib2.urlopen(req)
    root = etree.parse(response)
    for reismogelijkheid in root.findall(".//ReisMogelijkheid"):
        journey = {'sections' : []}
        for reisdeel in reismogelijkheid.findall(".//ReisDeel"):
           reisstops = reisdeel.findall(".//ReisStop")
           section = {'fromStation' : reisstops[0].find('Code').text.lower(),
                      'toStation'   : reisstops[-1].find('Code').text.lower(),
                      'operator'    : reisdeel.find('Vervoerder').text.replace('Arriva','ARR').replace('Syntus','SYNTUS').replace('Valleilijn','CXX').replace('NS International','NS').replace('GVB','NS').replace('R-net','NS').replace('Breng','BRENG').replace('Veolia','VTN')}
           journey['sections'].append(section)
        fare = calculate_fare(journey)
        print '------------------'
        print fare
        for prijs in reismogelijkheid.findall(".//Prijs"):
            if prijs.get('korting') != '0':
                continue
            second_matches=False
            first_matches=False
            if prijs.get('klasse') == '1':
                ns_prijs = int(round(float(prijs.text)*100))
                print '1: NS-prijs %s Eigenprijs %s Match = %s, diff %s' % (ns_prijs,fare['price_first'],ns_prijs==fare['price_first'],ns_prijs-fare['price_first'])
            if prijs.get('klasse') == '2':
                ns_prijs = int(round(float(prijs.text)*100))
                print '2: NS-prijs %s Eigenprijs %s Match = %s, diff %s' % (ns_prijs,fare['price_second'],ns_prijs==fare['price_second'],ns_prijs-fare['price_second'])

calculate_journey(sys.argv[1],sys.argv[2])
<|endoftext|>


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

# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
# Copyright 2011 OpenStack Foundation.
#
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import collections
import contextlib
import threading

from fasteners import _utils

import six

try:
    # Used for the reader-writer lock get the right
    # thread 'hack' (needed below).
    import eventlet
    from eventlet import patcher as eventlet_patcher
except ImportError:
    eventlet = None
    eventlet_patcher = None

def read_locked(*args, **kwargs):
    """Acquires & releases a read lock around call into decorated method.

    NOTE(harlowja): if no attribute name is provided then by default the
    attribute named '_lock' is looked for (this attribute is expected to be
    the rw-lock object) in the instance object this decorator is attached to.
    """

    def decorator(f):
        attr_name = kwargs.get('lock', '_lock')

        @six.wraps(f)
        def wrapper(self, *args, **kwargs):
            rw_lock = getattr(self, attr_name)
            with rw_lock.read_lock():
                return f(self, *args, **kwargs)

        return wrapper

    # This is needed to handle when the decorator has args or the decorator
    # doesn't have args, python is rather weird here...
    if kwargs or not args:
        return decorator
    else:
        if len(args) == 1:
            return decorator(args[0])
        else:
            return decorator

def write_locked(*args, **kwargs):
    """Acquires & releases a write lock around call into decorated method.

    NOTE(harlowja): if no attribute name is provided then by default the
    attribute named '_lock' is looked for (this attribute is expected to be
    the rw-lock object) in the instance object this decorator is attached to.
    """

    def decorator(f):
        attr_name = kwargs.get('lock', '_lock')

        @six.wraps(f)
        def wrapper(self, *args, **kwargs):
            rw_lock = getattr(self, attr_name)
            with rw_lock.write_lock():
                return f(self, *args, **kwargs)

        return wrapper

    # This is needed to handle when the decorator has args or the decorator
    # doesn't have args, python is rather weird here...
    if kwargs or not args:
        return decorator
    else:
        if len(args) == 1:
            return decorator(args[0])
        else:
            return decorator

class ReaderWriterLock(object):
    """A reader/writer lock.

    This lock allows for simultaneous readers to exist but only one writer
    to exist for use-cases where it is useful to have such types of locks.

    Currently a reader can not escalate its read lock to a write lock and
    a writer can not acquire a read lock while it is waiting on the write
    lock.

    In the future these restrictions may be relaxed.

    This can be eventually removed if http://bugs.python.org/issue8800 ever
    gets accepted into the python standard threading library...
    """

    #: Writer owner type/string constant.
    WRITER = 'w'

    #: Reader owner type/string constant.
    READER = 'r'

    @staticmethod
    def _fetch_current_thread_functor():
        # Until https://github.com/eventlet/eventlet/issues/172 is resolved
        # or addressed we have to use complicated workaround to get a object
        # that will not be recycled; the usage of threading.current_thread()
        # doesn't appear to currently be monkey patched and therefore isn't
        # reliable to use (and breaks badly when used as all threads share
        # the same current_thread() object)...
        if eventlet is not None and eventlet_patcher is not None:
            if eventlet_patcher.is_monkey_patched('thread'):
                return lambda: eventlet.getcurrent()
        return lambda: threading.current_thread()

    def __init__(self):
        self._writer = None
        self._pending_writers = collections.deque()
        self._readers = collections.deque()
        self._cond = threading.Condition()
        self._current_thread = self._fetch_current_thread_functor()

    @property
    def has_pending_writers(self):
        """Returns if there are writers waiting to become the *one* writer."""
        return bool(self._pending_writers)

    def is_writer(self, check_pending=True):
        """Returns if the caller is the active writer or a pending writer."""
        me = self._current_thread()
        if self._writer == me:
            return True
        if check_pending:
            return me in self._pending_writers
        else:
            return False

    @property
    def owner(self):
        """Returns whether the lock is locked by a writer or reader."""
        with self._cond:
            # Obtain the lock to ensure we get a accurate view of the actual
            # owner that isn't likely to change when we are reading it...
            if self._writer is not None:
                return self.WRITER
            if self._readers:
                return self.READER
            return None

    def is_reader(self):
        """Returns if the caller is one of the readers."""
        me = self._current_thread()
        return me in self._readers

    @contextlib.contextmanager
    def read_lock(self):
        """Context manager that grants a read lock.

        Will wait until no active or pending writers.

        Raises a ``RuntimeError`` if a pending writer tries to acquire
        a read lock.
        """
        me = self._current_thread()
        if me in self._pending_writers:
            raise RuntimeError("Writer %s can not acquire a read lock"
                               " while waiting for the write lock"
                               % me)
        with self._cond:
            while True:
                # No active writer, or we are the writer;
                # we are good to become a reader.
                if self._writer is None or self._writer == me:
                    self._readers.append(me)
                    break
                # An active writer; guess we have to wait.
                self._cond.wait()
        try:
            yield self
        finally:
            # I am no longer a reader, remove *one* occurrence of myself.
            # If the current thread acquired two read locks, then it will
            # still have to remove that other read lock; this allows for
            # basic reentrancy to be possible.
            with self._cond:
                self._readers.remove(me)
                self._cond.notify_all()

    @contextlib.contextmanager
    def write_lock(self):
        """Context manager that grants a write lock.

        Will wait until no active readers. Blocks readers after acquiring.

        Raises a ``RuntimeError`` if an active reader attempts to acquire
        a lock.
        """
        me = self._current_thread()
        if self.is_reader():
            raise RuntimeError("Reader %s to writer privilege"
                               " escalation not allowed" % me)
        if self.is_writer(check_pending=False):
            # Already the writer; this allows for basic reentrancy.
            yield self
        else:
            with self._cond:
                self._pending_writers.append(me)
                while True:
                    # No readers, and no active writer, am I next??
                    if len(self._readers) == 0 and self._writer is None:
                        if self._pending_writers[0] == me:
                            self._writer = self._pending_writers.popleft()
                            break
                    self._cond.wait()
            try:
                yield self
            finally:
                with self._cond:
                    self._writer = None
                    self._cond.notify_all()

@contextlib.contextmanager
def try_lock(lock):
    """Attempts to acquire a lock, and auto releases if acquired (on exit)."""
    # NOTE(harlowja): the keyword argument for 'blocking' does not work
    # in py2.x and only is fixed in py3.x (this adjustment is documented
    # and/or debated in http://bugs.python.org/issue10789); so we'll just
    # stick to the format that works in both (oddly the keyword argument
    # works in py2.x but only with reentrant locks).
    was_locked = lock.acquire(False)
    try:
        yield was_locked
    finally:
        if was_locked:
            lock.release()

def locked(*args, **kwargs):
    """A locking **method** decorator.

    It will look for a provided attribute (typically a lock or a list
    of locks) on the first argument of the function decorated (typically this
    is the 'self' object) and before executing the decorated function it
    activates the given lock or list of locks as a context manager,
    automatically releasing that lock on exit.

    NOTE(harlowja): if no attribute name is provided then by default the
    attribute named '_lock' is looked for (this attribute is expected to be
    the lock/list of locks object/s) in the instance object this decorator
    is attached to.
    """

    def decorator(f):
        attr_name = kwargs.get('lock', '_lock')

        @six.wraps(f)
        def wrapper(self, *args, **kwargs):
            attr_value = getattr(self, attr_name)
            if isinstance(attr_value, (tuple, list)):
                with _utils.LockStack() as stack:
                    for i, lock in enumerate(attr_value):
                        if not stack.acquire_lock(lock):
                            raise threading.ThreadError("Unable to acquire"
                                                        " lock %s" % (i + 1))
                    return f(self, *args, **kwargs)
            else:
                lock = attr_value
                with lock:
                    return f(self, *args, **kwargs)

        return wrapper

    # This is needed to handle when the decorator has args or the decorator
    # doesn't have args, python is rather weird here...
    if kwargs or not args:
        return decorator
    else:
        if len(args) == 1:
            return decorator(args[0])
        else:
            return decorator
<commit_msg>Allow not searching for eventlet in the r/w lock<commit_after># -*- coding: utf-8 -*-

# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
# Copyright 2011 OpenStack Foundation.
#
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import collections
import contextlib
import threading

from fasteners import _utils

import six

try:
    # Used for the reader-writer lock get the right
    # thread 'hack' (needed below).
    import eventlet
    from eventlet import patcher as eventlet_patcher
except ImportError:
    eventlet = None
    eventlet_patcher = None

def read_locked(*args, **kwargs):
    """Acquires & releases a read lock around call into decorated method.

    NOTE(harlowja): if no attribute name is provided then by default the
    attribute named '_lock' is looked for (this attribute is expected to be
    the rw-lock object) in the instance object this decorator is attached to.
    """

    def decorator(f):
        attr_name = kwargs.get('lock', '_lock')

        @six.wraps(f)
        def wrapper(self, *args, **kwargs):
            rw_lock = getattr(self, attr_name)
            with rw_lock.read_lock():
                return f(self, *args, **kwargs)

        return wrapper

    # This is needed to handle when the decorator has args or the decorator
    # doesn't have args, python is rather weird here...
    if kwargs or not args:
        return decorator
    else:
        if len(args) == 1:
            return decorator(args[0])
        else:
            return decorator

def write_locked(*args, **kwargs):
    """Acquires & releases a write lock around call into decorated method.

    NOTE(harlowja): if no attribute name is provided then by default the
    attribute named '_lock' is looked for (this attribute is expected to be
    the rw-lock object) in the instance object this decorator is attached to.
    """

    def decorator(f):
        attr_name = kwargs.get('lock', '_lock')

        @six.wraps(f)
        def wrapper(self, *args, **kwargs):
            rw_lock = getattr(self, attr_name)
            with rw_lock.write_lock():
                return f(self, *args, **kwargs)

        return wrapper

    # This is needed to handle when the decorator has args or the decorator
    # doesn't have args, python is rather weird here...
    if kwargs or not args:
        return decorator
    else:
        if len(args) == 1:
            return decorator(args[0])
        else:
            return decorator

class ReaderWriterLock(object):
    """A reader/writer lock.

    This lock allows for simultaneous readers to exist but only one writer
    to exist for use-cases where it is useful to have such types of locks.

    Currently a reader can not escalate its read lock to a write lock and
    a writer can not acquire a read lock while it is waiting on the write
    lock.

    In the future these restrictions may be relaxed.

    This can be eventually removed if http://bugs.python.org/issue8800 ever
    gets accepted into the python standard threading library...
    """

    #: Writer owner type/string constant.
    WRITER = 'w'

    #: Reader owner type/string constant.
    READER = 'r'

    @staticmethod
    def _fetch_current_thread_functor(find_eventlet=True):
        if not find_eventlet:
            return threading.current_thread
        # Until https://github.com/eventlet/eventlet/issues/172 is resolved
        # or addressed we have to use complicated workaround to get a object
        # that will not be recycled; the usage of threading.current_thread()
        # doesn't appear to currently be monkey patched and therefore isn't
        # reliable to use (and breaks badly when used as all threads share
        # the same current_thread() object)...
        if eventlet is not None and eventlet_patcher is not None:
            if eventlet_patcher.is_monkey_patched('thread'):
                return eventlet.getcurrent
        return threading.current_thread

    def __init__(self, find_eventlet=True):
        self._writer = None
        self._pending_writers = collections.deque()
        self._readers = collections.deque()
        self._cond = threading.Condition()
        self._current_thread = self._fetch_current_thread_functor(
            find_eventlet=find_eventlet)

    @property
    def has_pending_writers(self):
        """Returns if there are writers waiting to become the *one* writer."""
        return bool(self._pending_writers)

    def is_writer(self, check_pending=True):
        """Returns if the caller is the active writer or a pending writer."""
        me = self._current_thread()
        if self._writer == me:
            return True
        if check_pending:
            return me in self._pending_writers
        else:
            return False

    @property
    def owner(self):
        """Returns whether the lock is locked by a writer or reader."""
        with self._cond:
            # Obtain the lock to ensure we get a accurate view of the actual
            # owner that isn't likely to change when we are reading it...
            if self._writer is not None:
                return self.WRITER
            if self._readers:
                return self.READER
            return None

    def is_reader(self):
        """Returns if the caller is one of the readers."""
        me = self._current_thread()
        return me in self._readers

    @contextlib.contextmanager
    def read_lock(self):
        """Context manager that grants a read lock.

        Will wait until no active or pending writers.

        Raises a ``RuntimeError`` if a pending writer tries to acquire
        a read lock.
        """
        me = self._current_thread()
        if me in self._pending_writers:
            raise RuntimeError("Writer %s can not acquire a read lock"
                               " while waiting for the write lock"
                               % me)
        with self._cond:
            while True:
                # No active writer, or we are the writer;
                # we are good to become a reader.
                if self._writer is None or self._writer == me:
                    self._readers.append(me)
                    break
                # An active writer; guess we have to wait.
                self._cond.wait()
        try:
            yield self
        finally:
            # I am no longer a reader, remove *one* occurrence of myself.
            # If the current thread acquired two read locks, then it will
            # still have to remove that other read lock; this allows for
            # basic reentrancy to be possible.
            with self._cond:
                self._readers.remove(me)
                self._cond.notify_all()

    @contextlib.contextmanager
    def write_lock(self):
        """Context manager that grants a write lock.

        Will wait until no active readers. Blocks readers after acquiring.

        Raises a ``RuntimeError`` if an active reader attempts to acquire
        a lock.
        """
        me = self._current_thread()
        if self.is_reader():
            raise RuntimeError("Reader %s to writer privilege"
                               " escalation not allowed" % me)
        if self.is_writer(check_pending=False):
            # Already the writer; this allows for basic reentrancy.
            yield self
        else:
            with self._cond:
                self._pending_writers.append(me)
                while True:
                    # No readers, and no active writer, am I next??
                    if len(self._readers) == 0 and self._writer is None:
                        if self._pending_writers[0] == me:
                            self._writer = self._pending_writers.popleft()
                            break
                    self._cond.wait()
            try:
                yield self
            finally:
                with self._cond:
                    self._writer = None
                    self._cond.notify_all()

@contextlib.contextmanager
def try_lock(lock):
    """Attempts to acquire a lock, and auto releases if acquired (on exit)."""
    # NOTE(harlowja): the keyword argument for 'blocking' does not work
    # in py2.x and only is fixed in py3.x (this adjustment is documented
    # and/or debated in http://bugs.python.org/issue10789); so we'll just
    # stick to the format that works in both (oddly the keyword argument
    # works in py2.x but only with reentrant locks).
    was_locked = lock.acquire(False)
    try:
        yield was_locked
    finally:
        if was_locked:
            lock.release()

def locked(*args, **kwargs):
    """A locking **method** decorator.

    It will look for a provided attribute (typically a lock or a list
    of locks) on the first argument of the function decorated (typically this
    is the 'self' object) and before executing the decorated function it
    activates the given lock or list of locks as a context manager,
    automatically releasing that lock on exit.

    NOTE(harlowja): if no attribute name is provided then by default the
    attribute named '_lock' is looked for (this attribute is expected to be
    the lock/list of locks object/s) in the instance object this decorator
    is attached to.
    """

    def decorator(f):
        attr_name = kwargs.get('lock', '_lock')

        @six.wraps(f)
        def wrapper(self, *args, **kwargs):
            attr_value = getattr(self, attr_name)
            if isinstance(attr_value, (tuple, list)):
                with _utils.LockStack() as stack:
                    for i, lock in enumerate(attr_value):
                        if not stack.acquire_lock(lock):
                            raise threading.ThreadError("Unable to acquire"
                                                        " lock %s" % (i + 1))
                    return f(self, *args, **kwargs)
            else:
                lock = attr_value
                with lock:
                    return f(self, *args, **kwargs)

        return wrapper

    # This is needed to handle when the decorator has args or the decorator
    # doesn't have args, python is rather weird here...
    if kwargs or not args:
        return decorator
    else:
        if len(args) == 1:
            return decorator(args[0])
        else:
            return decorator
<|endoftext|>


<commit_before>from multiprocessing import Process, Queue
class InvalidArguments(ValueError):
    pass

class Plugin(Process):
    """A Plugin is a self-contained bit of functionality that runs in it's own
    process, and runs via listening for messages and sending messages through
    Queues.
    """
    listeners = None
    messengers = None
    name = 'Base Plugin'

    def __init__(self):
        """Set us up to run as a separate process, initialze our listener Queue,
        and set our runnable attribute.
        """
        super(Plugin, self).__init__()
        self.listener = Queue()
        self.runnable = True

    def set_messenger(self, messenger):
        """Set our messenger"""
        self.messenger = messenger

    def send(self, message, payload=None):
        """Send a message through our messenger Queue.
        Messages are presumably descriptions of a task that just got completed,
        or a notification of status, or whatnot.
        """
        self.messenger.put((message, payload))

    def recieve(self, message, payload=None):
        """Get a message from our listener Queue.
        This should currently be used in a subclasses self.run loop.
        """
        self.listener.put((message, payload))

    def shutdown(self, payload):
        """Set self.runnable to false.
        This should cause a subclass to break out of it's run loop.
        """
        self.runnable=False

    def run(self):
        """This should be implemented by subclasses, and currently should loop
        until self.runnable is no longer true.
        """
        raise NotImplementedError()

<commit_msg>add a line<commit_after>from multiprocessing import Process, Queue

class InvalidArguments(ValueError):
    pass

class Plugin(Process):
    """A Plugin is a self-contained bit of functionality that runs in it's own
    process, and runs via listening for messages and sending messages through
    Queues.
    """
    listeners = None
    messengers = None
    name = 'Base Plugin'

    def __init__(self):
        """Set us up to run as a separate process, initialze our listener Queue,
        and set our runnable attribute.
        """
        super(Plugin, self).__init__()
        self.listener = Queue()
        self.runnable = True

    def set_messenger(self, messenger):
        """Set our messenger"""
        self.messenger = messenger

    def send(self, message, payload=None):
        """Send a message through our messenger Queue.
        Messages are presumably descriptions of a task that just got completed,
        or a notification of status, or whatnot.
        """
        self.messenger.put((message, payload))

    def recieve(self, message, payload=None):
        """Get a message from our listener Queue.
        This should currently be used in a subclasses self.run loop.
        """
        self.listener.put((message, payload))

    def shutdown(self, payload):
        """Set self.runnable to false.
        This should cause a subclass to break out of it's run loop.
        """
        self.runnable=False

    def run(self):
        """This should be implemented by subclasses, and currently should loop
        until self.runnable is no longer true.
        """
        raise NotImplementedError()

<|endoftext|>


<commit_before>"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import unittest
import sys
sys.path.insert(0, ".")
from coalib.output.NullPrinter import NullPrinter

class NullPrinterTestCase(unittest.TestCase):
    def test_non_printing(self):
        self.uut = NullPrinter()
        self.assertEqual(self.uut.print("anything"), None)
        self.assertEqual(self.uut.print("anything", color="red"), None)

if __name__ == '__main__':
    unittest.main(verbosity=2)
<commit_msg>NullPrinter: Test logging capability too<commit_after>"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import unittest
import sys
sys.path.insert(0, ".")
from coalib.output.NullPrinter import NullPrinter

class NullPrinterTestCase(unittest.TestCase):
    def test_non_printing(self):
        self.uut = NullPrinter()
        self.assertEqual(self.uut.print("anything"), None)
        self.assertEqual(self.uut.print("anything", color="red"), None)
        self.assertEqual(self.uut.debug("message"), None)

if __name__ == '__main__':
    unittest.main(verbosity=2)
<|endoftext|>


<commit_before>from collections import defaultdict, OrderedDict

kFEATURES = OrderedDict([
    # ("ir", None),
    ("lm", None),
    ("deep", None),
    ("answer_present", None),
    ("text", None),
    # ("classifier", None),
    ("wikilinks", None),
    # ("mentions", None),
    ])

if __name__ == "__main__":
    import argparse
    # Generate a list of all of the relevant files given fold and granularity
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('--fold', type=str, default='dev',
                        help='Data fold to give feature files for')
    parser.add_argument('--granularity', type=str, default='sentence',
                        help='Granularity to give feature files for')
    flags = parser.parse_args()

    print(" ".join("features/%s/%s.%s.feat" % (flags.fold, flags.granularity, x)
                   for x in kFEATURES))

<commit_msg>features<commit_after>from collections import defaultdict, OrderedDict

kFEATURES = OrderedDict([
    # ("ir", None),
    ("lm", None),
    ("deep", None),
    ("answer_present", None),
    ("text", None),
    ("classifier", None),
    ("wikilinks", None),
    ("mentions", None),
    ])

if __name__ == "__main__":
    import argparse
    # Generate a list of all of the relevant files given fold and granularity
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('--fold', type=str, default='dev',
                        help='Data fold to give feature files for')
    parser.add_argument('--granularity', type=str, default='sentence',
                        help='Granularity to give feature files for')
    flags = parser.parse_args()

    print(" ".join("features/%s/%s.%s.feat" % (flags.fold, flags.granularity, x)
                   for x in kFEATURES))

<|endoftext|>


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

import argparse
import json
import os
import signal
import sys
import time

import numpy as np
import cv2   as cv

def main():

    # Parse arguments
    parser = argparse.ArgumentParser(description='Classify images in a directory as good or bad.')
    parser.add_argument('images-path', nargs='?', default='.', help='Path to directory of input images.')
    args = vars(parser.parse_args())

    # Select directory
    global if_path
    if_path = args['images-path']

    # Get list of files
    global files
    valid_suffices = ('png', 'jpg', 'jpeg')
    files = [f for f in os.listdir(if_path) if \
             os.path.isfile('%s/%s' % (if_path, f)) and \
             f.lower().endswith(valid_suffices)]
    files = sorted(files)
    if len(files) == 0:
        print('No images to read in: %s' % if_path)
        return

    # Main loop
    global idx, data
    idx = -1
    load()
    next_image()
    if idx >= len(files):
        print('All %d images have been classified.' % len(files))
        idx = 0
    show_image(idx)

    signal.signal(signal.SIGINT, cleanup)

    while True:
        # Show current image
        show_image(idx)

        # Stop if past last image
        if idx >= len(files):
            break

        # Wait for key for 50ms
        k = cv.waitKey(50)

        # Ignore if no key captured
        if k is -1:
            continue

        # If Esc, break loop
        if k == 27:
            break

        # If up button, upvote
        if k == 65362:
            upvote()

        # If down button, downvote
        if k == 65364:
            downvote()

        # If left button, show previous image
        if k == 65361:
            prev_image()

        # If right button, show next image
        if k == 65363:
            next_image()

    cleanup()

def cleanup(signum=signal.SIGTERM, frame=None):
    # Save data
    save()

    # Close all windows
    cv.destroyAllWindows()
    cv.waitKey(0) # Let windows close

    # Exit programme
    sys.exit(0)

def prev_image():
    global idx
    idx -= 1
    idx = 0 if idx < 0 else idx
    show_image(idx)

def next_image():
    global idx, files
    idx += 1

    show_image(idx)

def next_unclassified_image():
    global idx, files

    idx += 1
    while idx < len(files) and files[idx] in data.keys():
        idx += 1

    show_image(idx)

cur_img_path = ''
def show_image(idx):
    global cur_img, cur_img_path, if_path, files
    if idx < 0 or idx >= len(files):
        return

    fpath = files[idx]
    new_img_path = '%s/%s' % (if_path, fpath)
    if new_img_path == cur_img_path:
        return

    cur_img_path = new_img_path
    cur_img = cv.imread(cur_img_path)
    imshow(fpath, cur_img)

    if fpath in data:
        print('[%03d/%03d] Showing %s (%s)' % (idx, len(files), fpath,
            'GOOD' if data[fpath] else 'BAD'))
    else:
        print('[%03d/%03d] Showing %s' % (idx, len(files), fpath))

cv.namedWindow('main', cv.WINDOW_OPENGL | cv.WINDOW_KEEPRATIO)
def imshow(name, img):
    # Make it 800px wide on major axis
    (h, w, _) = img.shape
    if h < w:
        ratio = 800. / w
    else:
        ratio = 500. / h
    h *= ratio
    w *= ratio

    cv.imshow('main', img)
    cv.setWindowTitle('main', name)
    cv.resizeWindow('main', int(w), int(h))

def upvote():
    global cur_img, data, files, idx

    # Set value in data dict
    fn = files[idx]
    data[fn] = 1;

    # Show green tinted image
    img = cur_img
    img[:, :, 1] = 180 # Set G pixels to 180
    imshow(fn, img)

    # Pause then move to next image
    cv.waitKey(500)
    next_unclassified_image()

def downvote():
    global cur_img, data, files, idx

    # Set value in data dict
    fn = files[idx]
    data[fn] = 0;

    # Show red tinted image
    img = cur_img
    img[:, :, 2] = 180 # Set R pixels to 180
    imshow(fn, img)

    # Pause then move to next image
    cv.waitKey(500)
    next_unclassified_image()

data = {} # Is dict with filenames as key
outfile = 'classifications.json'

def load():
    global data, if_path
    out_path = '%s/%s' % (if_path, outfile)
    try:
        with open(out_path, 'r') as f:
            data = json.load(f)
    except:
        data = {}

def save():
    global data, if_path
    out_path = '%s/%s' % (if_path, outfile)
    with open(out_path, 'w') as f:
        json.dump(data, f)
        print('Saved output to %s' % out_path)

if __name__ == '__main__':
    main()
<commit_msg>Indent output in label_images<commit_after>#!/usr/bin/env python3

import argparse
import json
import os
import signal
import sys
import time

import numpy as np
import cv2   as cv

def main():

    # Parse arguments
    parser = argparse.ArgumentParser(description='Classify images in a directory as good or bad.')
    parser.add_argument('images-path', nargs='?', default='.', help='Path to directory of input images.')
    args = vars(parser.parse_args())

    # Select directory
    global if_path
    if_path = args['images-path']

    # Get list of files
    global files
    valid_suffices = ('png', 'jpg', 'jpeg')
    files = [f for f in os.listdir(if_path) if \
             os.path.isfile('%s/%s' % (if_path, f)) and \
             f.lower().endswith(valid_suffices)]
    files = sorted(files)
    if len(files) == 0:
        print('No images to read in: %s' % if_path)
        return

    # Main loop
    global idx, data
    idx = -1
    load()
    next_image()
    if idx >= len(files):
        print('All %d images have been classified.' % len(files))
        idx = 0
    show_image(idx)

    signal.signal(signal.SIGINT, cleanup)

    while True:
        # Show current image
        show_image(idx)

        # Stop if past last image
        if idx >= len(files):
            break

        # Wait for key for 50ms
        k = cv.waitKey(50)

        # Ignore if no key captured
        if k is -1:
            continue

        # If Esc, break loop
        if k == 27:
            break

        # If up button, upvote
        if k == 65362:
            upvote()

        # If down button, downvote
        if k == 65364:
            downvote()

        # If left button, show previous image
        if k == 65361:
            prev_image()

        # If right button, show next image
        if k == 65363:
            next_image()

    cleanup()

def cleanup(signum=signal.SIGTERM, frame=None):
    # Save data
    save()

    # Close all windows
    cv.destroyAllWindows()
    cv.waitKey(0) # Let windows close

    # Exit programme
    sys.exit(0)

def prev_image():
    global idx
    idx -= 1
    idx = 0 if idx < 0 else idx
    show_image(idx)

def next_image():
    global idx, files
    idx += 1

    show_image(idx)

def next_unclassified_image():
    global idx, files

    idx += 1
    while idx < len(files) and files[idx] in data.keys():
        idx += 1

    show_image(idx)

cur_img_path = ''
def show_image(idx):
    global cur_img, cur_img_path, if_path, files
    if idx < 0 or idx >= len(files):
        return

    fpath = files[idx]
    new_img_path = '%s/%s' % (if_path, fpath)
    if new_img_path == cur_img_path:
        return

    cur_img_path = new_img_path
    cur_img = cv.imread(cur_img_path)
    imshow(fpath, cur_img)

    if fpath in data:
        print('[%03d/%03d] Showing %s (%s)' % (idx, len(files), fpath,
            'GOOD' if data[fpath] else 'BAD'))
    else:
        print('[%03d/%03d] Showing %s' % (idx, len(files), fpath))

cv.namedWindow('main', cv.WINDOW_OPENGL | cv.WINDOW_KEEPRATIO)
def imshow(name, img):
    # Make it 800px wide on major axis
    (h, w, _) = img.shape
    if h < w:
        ratio = 800. / w
    else:
        ratio = 500. / h
    h *= ratio
    w *= ratio

    cv.imshow('main', img)
    cv.setWindowTitle('main', name)
    cv.resizeWindow('main', int(w), int(h))

def upvote():
    global cur_img, data, files, idx

    # Set value in data dict
    fn = files[idx]
    data[fn] = 1;

    # Show green tinted image
    img = cur_img
    img[:, :, 1] = 180 # Set G pixels to 180
    imshow(fn, img)

    # Pause then move to next image
    cv.waitKey(500)
    next_unclassified_image()

def downvote():
    global cur_img, data, files, idx

    # Set value in data dict
    fn = files[idx]
    data[fn] = 0;

    # Show red tinted image
    img = cur_img
    img[:, :, 2] = 180 # Set R pixels to 180
    imshow(fn, img)

    # Pause then move to next image
    cv.waitKey(500)
    next_unclassified_image()

data = {} # Is dict with filenames as key
outfile = 'classifications.json'

def load():
    global data, if_path
    out_path = '%s/%s' % (if_path, outfile)
    try:
        with open(out_path, 'r') as f:
            data = json.load(f)
    except:
        data = {}

def save():
    global data, if_path
    out_path = '%s/%s' % (if_path, outfile)
    with open(out_path, 'w') as f:
        json.dump(data, f, indent=2)
        print('Saved output to %s' % out_path)

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


<commit_before>#!/usr/bin/env python
"""
photosorter - https://github.com/dbader/photosorter
---------------------------------------------------

A little Python daemon to keep my photos organized on Dropbox.

It watches a *source directory* for modifications and moves new image
files to a *target directory* depending on when the photo was taken,
using EXIF data and creation date as a fallback.

The result looks like this:

├── 2013
│   ├── 2013-01
│   ├── 2013-02
│   ├── ...
│   └── 2013-12
├── 2014
│   ├── 2014-01
│   ├── 2014-02
│   ├── ...
│   └── 2014-12
├── ...

Inspired by
    - http://simplicitybliss.com/exporting-your-iphoto-library-to-dropbox/
    - https://github.com/wting/exifrenamer
    - http://chambersdaily.com/learning-to-love-photo-management/

"""
import collections
import datetime
import hashlib
import os
import re
import shutil
import time

import exifread
import watchdog
import watchdog.events
import watchdog.observers

class HashCache(object):
    """
    Gives a quick answer to the question if there's an identical file
    in the given target folder.

    """
    def __init__(self):
        # folder -> (hashes, filename -> hash)
        self.hashes = collections.defaultdict(lambda: (set(), dict()))

    def has_file(self, target_folder, path):
        # Strip trailing slashes etc.
        target_folder = os.path.normpath(target_folder)

        # Update the cache by ensuring that we have the hashes of all
        # files in the target folder. `_add_file` is smart enough to
        # skip any files we already hashed.
        for f in self._files_in_folder(target_folder):
            self._add_file(f)

        # Hash the new file at `path`.
        file_hash = self._hash(path)

        # Check if we already have an identical file in the target folder.
        return file_hash in self.hashes[target_folder][0]

    def _add_file(self, path):
        # Bail out if we already have a hash for the file at `path`.
        folder = self._target_folder(path)
        if path in self.hashes[folder][1]:
            return

        file_hash = self._hash(path)

        basename = os.path.basename(path)
        self.hashes[folder][0].add(file_hash)
        self.hashes[folder][1][basename] = file_hash

    @staticmethod
    def _hash(path):
        hasher = hashlib.sha1()
        with open(path, 'rb') as f:
            data = f.read()
            hasher.update(data)
        return hasher.hexdigest()

    @staticmethod
    def _target_folder(path):
        return os.path.dirname(path)

    @staticmethod
    def _files_in_folder(folder_path):
        """
        Iterable with full paths to all files in `folder_path`.
        """
        try:
            names = (
                os.path.join(folder_path, f) for f in os.listdir(folder_path)
            )
            return [f for f in names if os.path.isfile(f)]
        except OSError:
            return []

hash_cache = HashCache()

def move_file(root_folder, path):
    if not os.path.exists(path):
        return

    if not is_valid_filename(path):
        return

    dst = dest_path(root_folder, path)
    dirs = os.path.dirname(dst)

    if hash_cache.has_file(dirs, path):
        print('%s is a duplicate, skipping' % path)
        return

    try:
        os.makedirs(dirs)
        print('Created folder %s' % dirs)
    except OSError as e:
        # Catch "File exists"
        if e.errno != 17:
            raise e

    print('Moving %s to %s' % (path, dst))
    shutil.move(path, dst)

def resolve_duplicate(path):
    if not os.path.exists(path):
        return path

    basename = os.path.basename(path)
    filename, ext = os.path.splitext(basename)
    dirname = os.path.dirname(path)
    dedup_index = 1

    while True:
        new_fname = '%s-%i%s' % (filename, dedup_index, ext)
        new_path = os.path.join(dirname, new_fname)
        if not os.path.exists(new_path):
            # print('Deduplicating %s to %s' % (path, new_path))
            break
        dedup_index += 1

    return new_path

def is_valid_filename(path):
    ext = os.path.splitext(path)[1].lower()
    return ext in ['.jpg', '.jpeg', '.png']

def dest_path(root_folder, path):
    cdate = creation_date(path)
    path = path_from_datetime(root_folder, cdate, path)
    return resolve_duplicate(path)

def path_from_datetime(root_folder, dt, path):
    folder = folder_from_datetime(dt)
    filename = filename_from_datetime(dt, path)
    return os.path.join(root_folder, folder, filename)

def folder_from_datetime(dt):
    return dt.strftime('%Y' + os.sep + '%Y-%m')

def filename_from_datetime(dt, path):
    '''
    Returns basename + original extension.
    '''
    base = basename_from_datetime(dt)
    ext = os.path.splitext(path)[1]
    return base + ext.lower()

def basename_from_datetime(dt):
    '''
    Returns a string formatted like this '2004-05-07 20.16.31'.
    '''
    return dt.strftime('%Y-%m-%d %H.%M.%S')

def creation_date(path):
    exif_date = exif_creation_date(path)
    if exif_date:
        return exif_date
    return file_creation_date(path)

def file_creation_date(path):
    # Use mtime as creation date because ctime returns the
    # the time when the file's inode was last modified; which is
    # wrong and almost always later.
    mtime = os.path.getmtime(path)
    return datetime.datetime.fromtimestamp(mtime)

def exif_creation_date(path):
    try:
        ts = exif_creation_timestamp(path)
    except MissingExifTimestampError as e:
        print(e)
        return None

    try:
        return exif_timestamp_to_datetime(ts)
    except BadExifTimestampError:
        print(e)
        return None

class BadExifTimestampError(Exception):
    pass

class MissingExifTimestampError(Exception):
    pass

def exif_creation_timestamp(path):
    with open(path, 'rb') as f:
        tags = exifread.process_file(f, details=False)

    if 'EXIF DateTimeOriginal' in tags:
        return str(tags['EXIF DateTimeOriginal'])
    elif 'EXIF DateTimeDigitized' in tags:
        return str(tags['EXIF DateTimeDigitized'])

    raise MissingExifTimestampError()

def exif_timestamp_to_datetime(ts):
    elements = map(int, re.split(':| ', ts))

    if len(elements) != 6:
        raise BadExifTimestampError

    return datetime.datetime(*elements)

class EventHandler(watchdog.events.PatternMatchingEventHandler):
    def __init__(self, target_folder):
        self.target_folder = target_folder
        super(EventHandler, self).__init__(ignore_directories=True)

    # def on_any_event(self, event):
        # print event

    def on_created(self, event):
        # print event.src_path
        move_file(self.target_folder, event.src_path)

    def on_modified(self, event):
        # print event.src_path
        move_file(self.target_folder, event.src_path)

    def on_moved(self, event):
        # print event.dest_path
        move_file(self.target_folder, event.dest_path)

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('src_folder')
    parser.add_argument('dest_folder')
    args = parser.parse_args()

    event_handler = EventHandler(args.dest_folder)
    observer = watchdog.observers.Observer()
    observer.schedule(event_handler, args.src_folder, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()
<commit_msg>Fix encoding<commit_after>#!/usr/bin/env python
"""
photosorter - https://github.com/dbader/photosorter
---------------------------------------------------

A little Python daemon to keep my photos organized on Dropbox.

It watches a *source directory* for modifications and moves new image
files to a *target directory* depending on when the photo was taken,
using EXIF data and creation date as a fallback.

Inspired by
    - http://simplicitybliss.com/exporting-your-iphoto-library-to-dropbox/
    - https://github.com/wting/exifrenamer
    - http://chambersdaily.com/learning-to-love-photo-management/

"""
import collections
import datetime
import hashlib
import os
import re
import shutil
import time

import exifread
import watchdog
import watchdog.events
import watchdog.observers

class HashCache(object):
    """
    Gives a quick answer to the question if there's an identical file
    in the given target folder.

    """
    def __init__(self):
        # folder -> (hashes, filename -> hash)
        self.hashes = collections.defaultdict(lambda: (set(), dict()))

    def has_file(self, target_folder, path):
        # Strip trailing slashes etc.
        target_folder = os.path.normpath(target_folder)

        # Update the cache by ensuring that we have the hashes of all
        # files in the target folder. `_add_file` is smart enough to
        # skip any files we already hashed.
        for f in self._files_in_folder(target_folder):
            self._add_file(f)

        # Hash the new file at `path`.
        file_hash = self._hash(path)

        # Check if we already have an identical file in the target folder.
        return file_hash in self.hashes[target_folder][0]

    def _add_file(self, path):
        # Bail out if we already have a hash for the file at `path`.
        folder = self._target_folder(path)
        if path in self.hashes[folder][1]:
            return

        file_hash = self._hash(path)

        basename = os.path.basename(path)
        self.hashes[folder][0].add(file_hash)
        self.hashes[folder][1][basename] = file_hash

    @staticmethod
    def _hash(path):
        hasher = hashlib.sha1()
        with open(path, 'rb') as f:
            data = f.read()
            hasher.update(data)
        return hasher.hexdigest()

    @staticmethod
    def _target_folder(path):
        return os.path.dirname(path)

    @staticmethod
    def _files_in_folder(folder_path):
        """
        Iterable with full paths to all files in `folder_path`.
        """
        try:
            names = (
                os.path.join(folder_path, f) for f in os.listdir(folder_path)
            )
            return [f for f in names if os.path.isfile(f)]
        except OSError:
            return []

hash_cache = HashCache()

def move_file(root_folder, path):
    if not os.path.exists(path):
        return

    if not is_valid_filename(path):
        return

    dst = dest_path(root_folder, path)
    dirs = os.path.dirname(dst)

    if hash_cache.has_file(dirs, path):
        print('%s is a duplicate, skipping' % path)
        return

    try:
        os.makedirs(dirs)
        print('Created folder %s' % dirs)
    except OSError as e:
        # Catch "File exists"
        if e.errno != 17:
            raise e

    print('Moving %s to %s' % (path, dst))
    shutil.move(path, dst)

def resolve_duplicate(path):
    if not os.path.exists(path):
        return path

    basename = os.path.basename(path)
    filename, ext = os.path.splitext(basename)
    dirname = os.path.dirname(path)
    dedup_index = 1

    while True:
        new_fname = '%s-%i%s' % (filename, dedup_index, ext)
        new_path = os.path.join(dirname, new_fname)
        if not os.path.exists(new_path):
            # print('Deduplicating %s to %s' % (path, new_path))
            break
        dedup_index += 1

    return new_path

def is_valid_filename(path):
    ext = os.path.splitext(path)[1].lower()
    return ext in ['.jpg', '.jpeg', '.png']

def dest_path(root_folder, path):
    cdate = creation_date(path)
    path = path_from_datetime(root_folder, cdate, path)
    return resolve_duplicate(path)

def path_from_datetime(root_folder, dt, path):
    folder = folder_from_datetime(dt)
    filename = filename_from_datetime(dt, path)
    return os.path.join(root_folder, folder, filename)

def folder_from_datetime(dt):
    return dt.strftime('%Y' + os.sep + '%Y-%m')

def filename_from_datetime(dt, path):
    '''
    Returns basename + original extension.
    '''
    base = basename_from_datetime(dt)
    ext = os.path.splitext(path)[1]
    return base + ext.lower()

def basename_from_datetime(dt):
    '''
    Returns a string formatted like this '2004-05-07 20.16.31'.
    '''
    return dt.strftime('%Y-%m-%d %H.%M.%S')

def creation_date(path):
    exif_date = exif_creation_date(path)
    if exif_date:
        return exif_date
    return file_creation_date(path)

def file_creation_date(path):
    # Use mtime as creation date because ctime returns the
    # the time when the file's inode was last modified; which is
    # wrong and almost always later.
    mtime = os.path.getmtime(path)
    return datetime.datetime.fromtimestamp(mtime)

def exif_creation_date(path):
    try:
        ts = exif_creation_timestamp(path)
    except MissingExifTimestampError as e:
        print(e)
        return None

    try:
        return exif_timestamp_to_datetime(ts)
    except BadExifTimestampError:
        print(e)
        return None

class BadExifTimestampError(Exception):
    pass

class MissingExifTimestampError(Exception):
    pass

def exif_creation_timestamp(path):
    with open(path, 'rb') as f:
        tags = exifread.process_file(f, details=False)

    if 'EXIF DateTimeOriginal' in tags:
        return str(tags['EXIF DateTimeOriginal'])
    elif 'EXIF DateTimeDigitized' in tags:
        return str(tags['EXIF DateTimeDigitized'])

    raise MissingExifTimestampError()

def exif_timestamp_to_datetime(ts):
    elements = map(int, re.split(':| ', ts))

    if len(elements) != 6:
        raise BadExifTimestampError

    return datetime.datetime(*elements)

class EventHandler(watchdog.events.PatternMatchingEventHandler):
    def __init__(self, target_folder):
        self.target_folder = target_folder
        super(EventHandler, self).__init__(ignore_directories=True)

    # def on_any_event(self, event):
        # print event

    def on_created(self, event):
        # print event.src_path
        move_file(self.target_folder, event.src_path)

    def on_modified(self, event):
        # print event.src_path
        move_file(self.target_folder, event.src_path)

    def on_moved(self, event):
        # print event.dest_path
        move_file(self.target_folder, event.dest_path)

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('src_folder')
    parser.add_argument('dest_folder')
    args = parser.parse_args()

    event_handler = EventHandler(args.dest_folder)
    observer = watchdog.observers.Observer()
    observer.schedule(event_handler, args.src_folder, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()
<|endoftext|>


<commit_before>import lettuce

from flask import url_for
from journal import app, init_db
from test_journal import clear_db, TEST_DSN

@lettuce.before.all
def setup_app():
    print "This happens before all the lettuce tests begin"
    app.config['DATABASE'] = TEST_DSN
    app.config['TESTING'] = True

    init_db()

@lettuce.after.all
def teardown_app(total):
    print "This happens after all the lettuce tests have run"
    clear_db()

@lettuce.step('a logged-in user')
def logged_in_user(step):
    with app.test_client() as client:
        with client.session_transaction() as sess:
            sess['logged_in'] = True
        lettuce.world.client = client

@lettuce.step('an anonymous user')
def anonymous_user(step):
    with app.test_client() as client:
        with client.session_transaction() as sess:
            sess['logged_in'] = False
        lettuce.world.client = client

@lettuce.step('any text')
def random_text(step):
    lettuce.world.text = "This is my sample text."

@lettuce.step('an existing entry')
def existing_entry(step):
    lettuce.world.title = "My Title"
    entry_data = {
        'title': lettuce.world.title,
        'text': lettuce.world.text,
    }
    lettuce.world.response = lettuce.world.client.post(
        '/add', data=entry_data, follow_redirects=False
    )

@lettuce.step('text containing markdown and plain English')
def text_with_markdown_and_prose(step):
    lettuce.world.text = 'This is the first line of text.\n\n     `code samples here`'

@lettuce.step('I submit the edit form')
def edit_entry(step):
    lettuce.world.title = "My Title"

    entry_data = {
        'title': lettuce.world.title,
        'text': lettuce.world.text,
    }
    lettuce.world.response = lettuce.world.client.post(
        '/edit', data=entry_data, follow_redirects=False
    )

@lettuce.step('I view the home page')
def view_the_homepage(step):
    with app.test_request_context('/'):
        home_url = url_for('show_entries')
    lettuce.world.response = lettuce.world.client.get(home_url)

@lettuce.step('I see a button to edit posts')
def see_edit_button(step):
    body = lettuce.world.response.data
    msg = " 'edit' button in %s"
    assert 'Edit' in body, msg % body
    assert 'class="edit-links"' in body, msg % body

@lettuce.step('I do not see a button to edit posts')
def do_not_see_edit_button(step):
    body = lettuce.world.response.data
    msg = " 'edit' button in %s"
    assert 'Edit' not in body, msg % body
    assert 'class="edit-links"' not in body, msg % body

@lettuce.step("I append '/edit/1' to the home url")
def append_edit_to_url(step):
    with app.test_request_context('/'):
        home_url = url_for('show_entries')
    lettuce.world.manual_url = home_url + "/edit/1"

@lettuce.step('I do not see the edit entry form')
def do_not_see_edit_entry_form(step):
    body = lettuce.world.response.data
    msg = 'value="Edit" not in %s'
    assert 'value="Edit"' not in body, msg % body

@lettuce.step('I see my updated entry')
def see_updated_entry(step):
    body = lettuce.world.response.data
    for val in [lettuce.world.title, lettuce.world.text]:
        assert val in body

@lettuce.step('I see my code highlighted in color')
def see_highlighted_code(step):
    body = lettuce.world.response.data
    msg = '
 in %s'
    assert '
' in body, msg % body

@lettuce.step('I see plain text that is not code')
def do_not_see_colorized_English_text(step):
    body = lettuce.world.response.data
    msg = '
"This is the first line of text.
" in %s'
    assert 'This is the first line of text.' in body, msg % body

@lettuce.step('I click on the edit button')
def click_on_edit_button(step):
    with app.test_request_context('/'):
        home_url = url_for('show_entries')
    edit_url = 'http://localhost' + home_url + "/edit/1"
    lettuce.world.response = lettuce.world.client.get(edit_url)

@lettuce.step('I see the edit entry form')
def see_edit_entry_form(step):
    body = lettuce.world.response.data
    msg = 'value="Edit" in %s'
    assert 'value="Edit"' in body, msg % body
<commit_msg>updated before/after all messages: missing lettuce test may be highlighting code problem<commit_after>import lettuce

from flask import url_for
from journal import app, init_db
from test_journal import clear_db, TEST_DSN

@lettuce.before.all
def setup_app():
    print "Database  initialized."
    app.config['DATABASE'] = TEST_DSN
    app.config['TESTING'] = True

    init_db()

@lettuce.after.all
def teardown_app(total):
    print "Database cleared."
    clear_db()

@lettuce.step('a logged-in user')
def logged_in_user(step):
    with app.test_client() as client:
        with client.session_transaction() as sess:
            sess['logged_in'] = True
        lettuce.world.client = client

@lettuce.step('an anonymous user')
def anonymous_user(step):
    with app.test_client() as client:
        with client.session_transaction() as sess:
            sess['logged_in'] = False
        lettuce.world.client = client

@lettuce.step('any text')
def random_text(step):
    lettuce.world.text = "This is my sample text."

@lettuce.step('an existing entry')
def existing_entry(step):
    lettuce.world.title = "My Title"
    entry_data = {
        'title': lettuce.world.title,
        'text': lettuce.world.text,
    }
    lettuce.world.response = lettuce.world.client.post(
        '/add', data=entry_data, follow_redirects=False
    )

@lettuce.step('text containing markdown and plain English')
def text_with_markdown_and_prose(step):
    lettuce.world.text = 'This is the first line of text.\n\n     `code samples here`'

@lettuce.step('I submit the edit form')
def edit_entry(step):
    lettuce.world.title = "My Title"

    entry_data = {
        'title': lettuce.world.title,
        'text': lettuce.world.text,
    }
    lettuce.world.response = lettuce.world.client.post(
        '/edit', data=entry_data, follow_redirects=False
    )

@lettuce.step('I view the home page')
def view_the_homepage(step):
    with app.test_request_context('/'):
        home_url = url_for('show_entries')
    lettuce.world.response = lettuce.world.client.get(home_url)

@lettuce.step('I see a button to edit posts')
def see_edit_button(step):
    body = lettuce.world.response.data
    msg = " 'edit' button in %s"
    assert 'Edit' in body, msg % body
    assert 'class="edit-links"' in body, msg % body

@lettuce.step('I do not see a button to edit posts')
def do_not_see_edit_button(step):
    body = lettuce.world.response.data
    msg = " 'edit' button in %s"
    assert 'Edit' not in body, msg % body
    assert 'class="edit-links"' not in body, msg % body

@lettuce.step("I append '/edit/1' to the home url")
def append_edit_to_url(step):
    with app.test_request_context('/'):
        home_url = url_for('show_entries')
    lettuce.world.manual_url = home_url + "/edit/1"

@lettuce.step('I do not see the edit entry form')
def do_not_see_edit_entry_form(step):
    body = lettuce.world.response.data
    msg = 'value="Edit" not in %s'
    assert 'value="Edit"' not in body, msg % body

@lettuce.step('I see my updated entry')
def see_updated_entry(step):
    body = lettuce.world.response.data
    for val in [lettuce.world.title, lettuce.world.text]:
        assert val in body

@lettuce.step('I see my code highlighted in color')
def see_highlighted_code(step):
    body = lettuce.world.response.data
    msg = '
 in %s'
    assert '
' in body, msg % body

@lettuce.step('I see plain text that is not code')
def do_not_see_colorized_English_text(step):
    body = lettuce.world.response.data
    msg = '
"This is the first line of text.
" in %s'
    assert 'This is the first line of text.' in body, msg % body

@lettuce.step('I click on the edit button')
def click_on_edit_button(step):
    with app.test_request_context('/'):
        home_url = url_for('show_entries')
    edit_url = 'http://localhost' + home_url + "/edit/1"
    lettuce.world.response = lettuce.world.client.get(edit_url)

@lettuce.step('I see the edit entry form')
def see_edit_entry_form(step):
    body = lettuce.world.response.data
    msg = 'value="Edit" in %s'
    assert 'value="Edit"' in body, msg % body
<|endoftext|>


<commit_before>from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render

from .models import Fellow, Event, Expense, Blog
from .forms import FellowForm, EventForm, ExpenseForm, BlogForm

def index(request):
    context = {
            'fellows': Fellow.objects.all(),
            'events': Event.objects.all(),
            }
    return render(request, 'fellowms/index.html', context)

def dashboard(request):
    return render(request, 'fellowms/dashboard.html')

@login_required
def fellow(request):
    if request.POST:
        # Handle submission
        formset = FellowForm(request.POST, request.FILES)

        if formset.is_valid():
            fellow = formset.save()
            return HttpResponseRedirect(reverse('fellow_detail',
                args=[fellow.id,]))
    else:
        formset = FellowForm

    # Show submission form.
    context = {
            "title": "Create fellow",
            "formset": formset,
            "submit_text": "Save",
            }
    return render(request, 'fellowms/form.html', context)

def fellow_detail(request, fellow_id):
    context = {
            'fellow': Fellow.objects.get(id=fellow_id),
            }

    return render(request, 'fellowms/fellow_detail.html', context)

def event(request):
    if request.POST:
        # Handle submission
        post = request.POST.copy()
        print(post)
        fellow = Fellow.objects.get(email=post['fellow'])
        print(fellow)
        post['fellow'] = fellow.id
        formset = EventForm(post)

        if formset.is_valid():
            event = formset.save()
            return HttpResponseRedirect(reverse('event_detail',
                args=[event.id,]))
    else:
        formset = EventForm

    # Show submission form.
    context = {
            "title": "Submit event",
            "formset": formset,
            "submit_text": "Submit",
            }
    return render(request, 'fellowms/form.html', context)

def event_detail(request, event_id):
    context = {
            'event': Event.objects.get(id=event_id),
            }

    return render(request, 'fellowms/event_detail.html', context)

def expense(request):
    if request.POST:
        # Handle submission
        formset = ExpenseForm(request.POST, request.FILES)

        if formset.is_valid():
            expense = formset.save()
            return HttpResponseRedirect(reverse('expense_detail',
                args=[expense.id,]))
    else:
        formset = ExpenseForm

    # Show submission form.
    context = {
            "title": "Submit expenses",
            "formset": formset,
            "submit_text": "Submit",
            }
    return render(request, 'fellowms/form.html', context)

def expense_detail(request, expense_id):
    context = {
            'expense': Expense.objects.get(id=expense_id),
            }

    return render(request, 'fellowms/expense_detail.html', context)

def blog(request):
    if request.POST:
        # Handle submission
        formset = BlogForm(request.POST)

        if formset.is_valid():
            blog = formset.save()
            return HttpResponseRedirect(reverse('blog_detail',
                args=[blog.id,]))
    else:
        formset = BlogForm

    # Show submission form.
    context = {
            "title": "Submit blog",
            "formset": formset,
            "submit_text": "Submit",
            }
    return render(request, 'fellowms/form.html', context)

def blog_detail(request, blog_id):
    context = {
            'blog': Blog.objects.get(id=blog_id),
            }

    return render(request, 'fellowms/blog_detail.html', context)
<commit_msg>Remove print functions<commit_after>from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render

from .models import Fellow, Event, Expense, Blog
from .forms import FellowForm, EventForm, ExpenseForm, BlogForm

def index(request):
    context = {
            'fellows': Fellow.objects.all(),
            'events': Event.objects.all(),
            }
    return render(request, 'fellowms/index.html', context)

def dashboard(request):
    return render(request, 'fellowms/dashboard.html')

@login_required
def fellow(request):
    if request.POST:
        # Handle submission
        formset = FellowForm(request.POST, request.FILES)

        if formset.is_valid():
            fellow = formset.save()
            return HttpResponseRedirect(reverse('fellow_detail',
                args=[fellow.id,]))
    else:
        formset = FellowForm

    # Show submission form.
    context = {
            "title": "Create fellow",
            "formset": formset,
            "submit_text": "Save",
            }
    return render(request, 'fellowms/form.html', context)

def fellow_detail(request, fellow_id):
    context = {
            'fellow': Fellow.objects.get(id=fellow_id),
            }

    return render(request, 'fellowms/fellow_detail.html', context)

def event(request):
    if request.POST:
        # Handle submission
        post = request.POST.copy()
        fellow = Fellow.objects.get(email=post['fellow'])
        post['fellow'] = fellow.id
        formset = EventForm(post)

        if formset.is_valid():
            event = formset.save()
            return HttpResponseRedirect(reverse('event_detail',
                args=[event.id,]))
    else:
        formset = EventForm

    # Show submission form.
    context = {
            "title": "Submit event",
            "formset": formset,
            "submit_text": "Submit",
            }
    return render(request, 'fellowms/form.html', context)

def event_detail(request, event_id):
    context = {
            'event': Event.objects.get(id=event_id),
            }

    return render(request, 'fellowms/event_detail.html', context)

def expense(request):
    if request.POST:
        # Handle submission
        formset = ExpenseForm(request.POST, request.FILES)

        if formset.is_valid():
            expense = formset.save()
            return HttpResponseRedirect(reverse('expense_detail',
                args=[expense.id,]))
    else:
        formset = ExpenseForm

    # Show submission form.
    context = {
            "title": "Submit expenses",
            "formset": formset,
            "submit_text": "Submit",
            }
    return render(request, 'fellowms/form.html', context)

def expense_detail(request, expense_id):
    context = {
            'expense': Expense.objects.get(id=expense_id),
            }

    return render(request, 'fellowms/expense_detail.html', context)

def blog(request):
    if request.POST:
        # Handle submission
        formset = BlogForm(request.POST)

        if formset.is_valid():
            blog = formset.save()
            return HttpResponseRedirect(reverse('blog_detail',
                args=[blog.id,]))
    else:
        formset = BlogForm

    # Show submission form.
    context = {
            "title": "Submit blog",
            "formset": formset,
            "submit_text": "Submit",
            }
    return render(request, 'fellowms/form.html', context)

def blog_detail(request, blog_id):
    context = {
            'blog': Blog.objects.get(id=blog_id),
            }

    return render(request, 'fellowms/blog_detail.html', context)
<|endoftext|>


<commit_before>from contextlib import contextmanager
import os
import re

__version__ = '0.2.0'

@contextmanager
def fever_open(file, mode='r'):
    f = open(_get_next_version(file), mode)
    try:
        yield f
    finally:
        f.close()

def _get_next_version(file):
    """Return the next version name of the file."""
    # file = /path/file.ext
    if os.path.exists(file):
        # root = '/path/file'
        # ext = '.ext'
        root, ext = os.path.splitext(file)
        # parentdir = '/path'
        parentdir = os.path.dirname(root)
        # basename = 'file'
        basename = os.path.basename(root)
        # _get_next_version of the file
        # file (1).ext -> 1, f (2).ext -> 2, ...
        v = _get_version(basename)
        # if we are on _get_next_version 0, add a space between the ending of the
        # basename of the file and the _get_next_version number.
        # E.g. file.ext -> file (1).ext
        if v == 0:
            basename += ' '
        # remove any prior _get_next_version expression (e.g. (1)) from root
        basename = basename.replace('(' + str(v) + ')', '')
        # increment _get_next_version
        v += 1
        v = '(%d)' % v
        return _get_next_version(os.path.join(parentdir, basename + v + ext))

    return file

def _get_version(basename):
    """Returns the _get_next_version of a file."""
    match = re.search(r"\(\d*\)", basename)
    if match:
        v = int(match.group(0)
                .replace('(', '')
                .replace(')', ''))
        return v

    return 0
<commit_msg>updated version number.<commit_after>from contextlib import contextmanager
import os
import re

__version__ = '0.2.1'

@contextmanager
def fever_open(file, mode='r'):
    f = open(_get_next_version(file), mode)
    try:
        yield f
    finally:
        f.close()

def _get_next_version(file):
    """Return the next version name of the file."""
    # file = /path/file.ext
    if os.path.exists(file):
        # root = '/path/file'
        # ext = '.ext'
        root, ext = os.path.splitext(file)
        # parentdir = '/path'
        parentdir = os.path.dirname(root)
        # basename = 'file'
        basename = os.path.basename(root)
        # _get_next_version of the file
        # file (1).ext -> 1, f (2).ext -> 2, ...
        v = _get_version(basename)
        # if we are on _get_next_version 0, add a space between the ending of the
        # basename of the file and the _get_next_version number.
        # E.g. file.ext -> file (1).ext
        if v == 0:
            basename += ' '
        # remove any prior _get_next_version expression (e.g. (1)) from root
        basename = basename.replace('(' + str(v) + ')', '')
        # increment _get_next_version
        v += 1
        v = '(%d)' % v
        return _get_next_version(os.path.join(parentdir, basename + v + ext))

    return file

def _get_version(basename):
    """Returns the _get_next_version of a file."""
    match = re.search(r"\(\d*\)", basename)
    if match:
        v = int(match.group(0)
                .replace('(', '')
                .replace(')', ''))
        return v

    return 0
<|endoftext|>