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

import os
from os import path
import contextlib
import pip
from pip import get_installed_distributions
import sys
from cStringIO import StringIO
from ast import parse, Import, ImportFrom

@contextlib.contextmanager
def capture():
    """for capturing stdout,  stolen from http://stackoverflow.com/a/10743550"""

    oldout,olderr = sys.stdout, sys.stderr
    try:
        out=[StringIO(), StringIO()]
        sys.stdout,sys.stderr = out
        yield out
    finally:
        sys.stdout,sys.stderr = oldout, olderr
        out[0] = out[0].getvalue()
        out[1] = out[1].getvalue()

def get_top_levels():
    '''returns a dict with the keys from all of the top_level.txt entries, and the freeze name as the value'''
    output = {}

    for d in get_installed_distributions():
        freeze_name = '=='.join(str(d).split())
        top_level = path.join(d.egg_info, 'top_level.txt')
        if path.exists(top_level):
            with open(top_level) as f:
                a = f.read()
            for line in a.splitlines():
                output[line.strip()] = freeze_name
    return output

def get_freeze():
    with capture() as out:
        pip.main(['freeze'])
    if not out[1]:
        return out[0].splitlines()

def get_py_files(project_path = None):
    if not project_path:
        project_path = os.getcwd()
    project_path = os.path.join(os.getcwd(), project_path)

    for current_directory, directory_names, file_names in os.walk(project_path):
        for file_name in file_names:
            if file_name.split('.')[-1] == 'py':
                yield os.path.join(current_directory,file_name)

def get_imported_modules(py_file):
    """Takes a python file and yields all the modules imoprted
    currently swallows all parsing errors, such as invalid syntax. """
    with open(py_file) as f:
        text = f.read()
    try:
        tree = parse(text,filename = 'parseerrors.txt')
        for c in tree.body:
            if type(c) == Import:
                for alias in c.names:
                    yield alias.name
            elif type(c) == ImportFrom:
                if c.module != None:
                    yield c.module
    except:
        pass  #put specific parsing errors here

def get_imported_top_levels(project_path):
    """takes a path, yields all the top level modules that are imported in all the python files"""
    imported_top_levels = set()
    for py_file in get_py_files(project_path):
        this_itl = set(module.split('.')[0] for module in get_imported_modules(py_file))
        new = this_itl.difference(imported_top_levels)
        imported_top_levels.update(new)
        for itl in new:
            yield itl

def make_freeze(project_path):
    tld = get_top_levels()
    for itl in get_imported_top_levels(project_path):

        if tld.get(itl):
            yield tld.get(itl)
def freeze(project_path):
    for r in make_freeze(project_path):
        print(r)
def main():
    freeze(project_path = None)

if __name__ == '__main__':
    main()<commit_msg>added argument parsing<commit_after>#!/usr/bin/env python
"""A simple script for showing requirements actually in use in the same format as pip freeze"""
import os
from os import path
import contextlib
import pip
from pip import get_installed_distributions
import sys
from cStringIO import StringIO
from ast import parse, Import, ImportFrom
import argparse

#todo: add a way to choose virtual env
#todo: show a warning for unfound packages

@contextlib.contextmanager
def capture():
    """for capturing stdout,  stolen from http://stackoverflow.com/a/10743550"""

    oldout,olderr = sys.stdout, sys.stderr
    try:
        out=[StringIO(), StringIO()]
        sys.stdout,sys.stderr = out
        yield out
    finally:
        sys.stdout,sys.stderr = oldout, olderr
        out[0] = out[0].getvalue()
        out[1] = out[1].getvalue()

def get_top_levels():
    '''returns a dict with the keys from all of the top_level.txt entries, and the freeze name as the value'''
    output = {}

    for d in get_installed_distributions():
        freeze_name = '=='.join(str(d).split())
        if d.egg_info:
            top_level = path.join(d.egg_info, 'top_level.txt')
            if path.exists(top_level):
                with open(top_level) as f:
                    a = f.read()
                for line in a.splitlines():
                    output[line.strip()] = freeze_name
    return output

def get_freeze():
    with capture() as out:
        pip.main(['freeze'])
    if not out[1]:
        return out[0].splitlines()

def get_py_files(project_path = None):
    if not project_path:
        project_path = os.getcwd()
    project_path = os.path.join(os.getcwd(), project_path)

    for current_directory, directory_names, file_names in os.walk(project_path):
        for file_name in file_names:
            if file_name.split('.')[-1] == 'py':
                yield os.path.join(current_directory,file_name)

def get_imported_modules(py_file):
    """Takes a python file and yields all the modules imoprted
    currently swallows all parsing errors, such as invalid syntax. """
    with open(py_file) as f:
        text = f.read()
    try:
        tree = parse(text,filename = 'parseerrors.txt')
        for c in tree.body:
            if type(c) == Import:
                for alias in c.names:
                    yield alias.name
            elif type(c) == ImportFrom:
                if c.module != None:
                    yield c.module
    except:
        pass  #put specific parsing errors here

def get_imported_top_levels(project_path):
    """takes a path, yields all the top level modules that are imported in all the python files"""
    imported_top_levels = set()
    for py_file in get_py_files(project_path):
        this_itl = set(module.split('.')[0] for module in get_imported_modules(py_file))
        new = this_itl.difference(imported_top_levels)
        imported_top_levels.update(new)
        for itl in new:
            yield itl

def make_freeze(project_path):
    tld = get_top_levels()
    for itl in get_imported_top_levels(project_path):

        if tld.get(itl):
            yield tld.get(itl)
def freeze(project_path):
    for r in make_freeze(project_path):
        print(r)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("project_path",
                        type=str,
                        nargs='?',
                        default=os.getcwd(),
                        help="path of python project, defaults to current path ")
    parser.add_argument("-v", "--verbose", action="store_true",
                        help="increase output verbosity")
    args = parser.parse_args()

    if args.project_path:
        project_path = args.project_path
    else:
        project_path = '.'
    freeze(project_path = project_path)

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


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

import sys
import os
import subprocess

"""
Runs all veros tests back to back and compares the results with the legacy (pyOM2) backend.
Expects path to the compiled Fortran library as first command line argument.
"""

if __name__ == "__main__":
    try:
        fortran_path = sys.argv[1]
    except IndexError:
        raise RuntimeError("First command line argument must be path to pyOM Fortran library")

    success, fail = "passed", "failed"
    if sys.stdout.isatty():
        success = "\x1b[{}m{}\x1b[0m".format("32",success)
        fail = "\x1b[{}m{}\x1b[0m".format("31",fail)

    for f in os.listdir("./tests"):
        if f.endswith("_test.py"):
            sys.stdout.write("Running test {} ... ".format(f))
            sys.stdout.flush()
            try: # must run each test in its own Python subprocess to reload the Fortran library
    	        output = subprocess.check_output(["python", os.path.join("./tests", f), fortran_path],
                                                  stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                sys.stdout.write(fail + "\n\n")
                print(e.output)
                continue
            sys.stdout.write(success + "\n")

            sys.stdout.write("Running test {} with Bohrium ... ".format(f))
            sys.stdout.flush()
            try: # must run each test in its own Python subprocess to reload the Fortran library
    	        output = subprocess.check_output(["python", os.path.join("./tests", f), fortran_path, "-b", "bohrium"],
                                                  stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                sys.stdout.write(fail + "\n\n")
                print(e.output)
                continue
            sys.stdout.write(success + "\n")
<commit_msg>fix path handling in run_tests<commit_after>#! /usr/bin/env python

import sys
import os
import subprocess

"""
Runs all veros tests back to back and compares the results with the legacy (pyOM2) backend.
Expects path to the compiled Fortran library as first command line argument.
"""

testdir = os.path.join(os.path.dirname(__file__), os.path.relpath("tests"))

if __name__ == "__main__":
    try:
        fortran_path = sys.argv[1]
    except IndexError:
        raise RuntimeError("First command line argument must be path to pyOM Fortran library")

    success, fail = "passed", "failed"
    if sys.stdout.isatty():
        success = "\x1b[{}m{}\x1b[0m".format("32",success)
        fail = "\x1b[{}m{}\x1b[0m".format("31",fail)

    for f in os.listdir(testdir):
        if f.endswith("_test.py"):
            sys.stdout.write("Running test {} ... ".format(f))
            sys.stdout.flush()
            try: # must run each test in its own Python subprocess to reload the Fortran library
    	        output = subprocess.check_output(["python", os.path.join(testdir, f), fortran_path],
                                                  stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                sys.stdout.write(fail + "\n\n")
                print(e.output)
                continue
            sys.stdout.write(success + "\n")

            sys.stdout.write("Running test {} with Bohrium ... ".format(f))
            sys.stdout.flush()
            try: # must run each test in its own Python subprocess to reload the Fortran library
    	        output = subprocess.check_output(["python", os.path.join("./tests", f), fortran_path, "-b", "bohrium"],
                                                  stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                sys.stdout.write(fail + "\n\n")
                print(e.output)
                continue
            sys.stdout.write(success + "\n")
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# Copyright (c) 2013-2018 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
# Generate seeds.txt from Pieter's DNS seeder
#

import re
import sys
import dns.resolver
import collections

NSEEDS=512

MAX_SEEDS_PER_ASN=2

MIN_BLOCKS = 337600

# These are hosts that have been observed to be behaving strangely (e.g.
# aggressively connecting to every node).
SUSPICIOUS_HOSTS = {
    "130.211.129.106", "178.63.107.226",
    "83.81.130.26", "88.198.17.7", "148.251.238.178", "176.9.46.6",
    "54.173.72.127", "54.174.10.182", "54.183.64.54", "54.194.231.211",
    "54.66.214.167", "54.66.220.137", "54.67.33.14", "54.77.251.214",
    "54.94.195.96", "54.94.200.247"
}

PATTERN_IPV4 = re.compile(r"^((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})):(\d+)$")
PATTERN_IPV6 = re.compile(r"^\[([0-9a-z:]+)\]:(\d+)$")
PATTERN_ONION = re.compile(r"^([abcdefghijklmnopqrstuvwxyz234567]{16}\.onion):(\d+)$")
PATTERN_AGENT = re.compile(
    r"^/Satoshi:("
    r"0.14.(0|1|2|3|99)|"
    r"0.15.(0|1|2|99)|"
    r"0.16.(0|1|2|3|99)|"
    r"0.17.(0|0.1|1|2|99)|"
    r"0.18.(0|1|99)|"
    r"0.19.99"
    r")")

def parseline(line):
    sline = line.split()
    if len(sline) < 11:
       return None
    m = PATTERN_IPV4.match(sline[0])
    sortkey = None
    ip = None
    if m is None:
        m = PATTERN_IPV6.match(sline[0])
        if m is None:
            m = PATTERN_ONION.match(sline[0])
            if m is None:
                return None
            else:
                net = 'onion'
                ipstr = sortkey = m.group(1)
                port = int(m.group(2))
        else:
            net = 'ipv6'
            if m.group(1) in ['::']: # Not interested in localhost
                return None
            ipstr = m.group(1)
            sortkey = ipstr # XXX parse IPv6 into number, could use name_to_ipv6 from generate-seeds
            port = int(m.group(2))
    else:
        # Do IPv4 sanity check
        ip = 0
        for i in range(0,4):
            if int(m.group(i+2)) < 0 or int(m.group(i+2)) > 255:
                return None
            ip = ip + (int(m.group(i+2)) << (8*(3-i)))
        if ip == 0:
            return None
        net = 'ipv4'
        sortkey = ip
        ipstr = m.group(1)
        port = int(m.group(6))
    # Skip bad results.
    if sline[1] == 0:
        return None
    # Extract uptime %.
    uptime30 = float(sline[7][:-1])
    # Extract Unix timestamp of last success.
    lastsuccess = int(sline[2])
    # Extract protocol version.
    version = int(sline[10])
    # Extract user agent.
    agent = sline[11][1:-1]
    # Extract service flags.
    service = int(sline[9], 16)
    # Extract blocks.
    blocks = int(sline[8])
    # Construct result.
    return {
        'net': net,
        'ip': ipstr,
        'port': port,
        'ipnum': ip,
        'uptime': uptime30,
        'lastsuccess': lastsuccess,
        'version': version,
        'agent': agent,
        'service': service,
        'blocks': blocks,
        'sortkey': sortkey,
    }

def dedup(ips):
    '''deduplicate by address'''
    d = {}
    for ip in ips:
        d[ip['ip']] = ip
    return list(d.values())

def filtermultiport(ips):
    '''Filter out hosts with more nodes per IP'''
    hist = collections.defaultdict(list)
    for ip in ips:
        hist[ip['sortkey']].append(ip)
    return [value[0] for (key,value) in list(hist.items()) if len(value)==1]

def lookup_asn(net, ip):
    '''
    Look up the asn for an IP (4 or 6) address by querying cymry.com, or None
    if it could not be found.
    '''
    try:
        if net == 'ipv4':
            ipaddr = ip
            prefix = '.origin'
        else:                  # http://www.team-cymru.com/IP-ASN-mapping.html
            res = str()                         # 2001:4860:b002:23::68
            for nb in ip.split(':')[:4]:  # pick the first 4 nibbles
                for c in nb.zfill(4):           # right padded with '0'
                    res += c + '.'              # 2001 4860 b002 0023
            ipaddr = res.rstrip('.')            # 2.0.0.1.4.8.6.0.b.0.0.2.0.0.2.3
            prefix = '.origin6'

        asn = int([x.to_text() for x in dns.resolver.query('.'.join(
                   reversed(ipaddr.split('.'))) + prefix + '.asn.cymru.com',
                   'TXT').response.answer][0].split('\"')[1].split(' ')[0])
        return asn
    except Exception:
        sys.stderr.write('ERR: Could not resolve ASN for "' + ip + '"\n')
        return None

# Based on Greg Maxwell's seed_filter.py
def filterbyasn(ips, max_per_asn, max_total):
    # Sift out ips by type
    ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']]
    ips_onion = [ip for ip in ips if ip['net'] == 'onion']

    # Filter IPv46 by ASN
    result = []
    asn_count = collections.defaultdict(int)
    for ip in ips_ipv46:
        if len(result) == max_total:
            break
        asn = lookup_asn(ip['net'], ip['ip'])
        if asn is None or asn_count[asn] == max_per_asn:
            continue
        asn_count[asn] += 1
        result.append(ip)

    # Add back Onions
    result.extend(ips_onion)
    return result

def ip_stats(ips):
    hist = collections.defaultdict(int)
    for ip in ips:
        if ip is not None:
            hist[ip['net']] += 1

    return 'IPv4 %d, IPv6 %d, Onion %d' % (hist['ipv4'], hist['ipv6'], hist['onion'])

def main():
    lines = sys.stdin.readlines()
    ips = [parseline(line) for line in lines]

    print('Initial: %s' % (ip_stats(ips)), file=sys.stderr)
    # Skip entries with invalid address.
    ips = [ip for ip in ips if ip is not None]
    print('Skip entries with invalid address: %s' % (ip_stats(ips)), file=sys.stderr)
    # Skip duplicattes (in case multiple seeds files were concatenated)
    ips = dedup(ips)
    print('After removing duplicates: %s' % (ip_stats(ips)), file=sys.stderr)
    # Skip entries from suspicious hosts.
    ips = [ip for ip in ips if ip['ip'] not in SUSPICIOUS_HOSTS]
    print('Skip entries from suspicious hosts: %s' % (ip_stats(ips)), file=sys.stderr)
    # Enforce minimal number of blocks.
    ips = [ip for ip in ips if ip['blocks'] >= MIN_BLOCKS]
    print('Enforce minimal number of blocks: %s' % (ip_stats(ips)), file=sys.stderr)
    # Require service bit 1.
    ips = [ip for ip in ips if (ip['service'] & 1) == 1]
    print('Require service bit 1: %s' % (ip_stats(ips)), file=sys.stderr)
    # Require at least 50% 30-day uptime for clearnet, 10% for onion.
    req_uptime = {
        'ipv4': 50,
        'ipv6': 50,
        'onion': 10,
    }
    ips = [ip for ip in ips if ip['uptime'] > req_uptime[ip['net']]]
    print('Require minimum uptime: %s' % (ip_stats(ips)), file=sys.stderr)
    # Require a known and recent user agent.
    ips = [ip for ip in ips if PATTERN_AGENT.match(ip['agent'])]
    print('Require a known and recent user agent: %s' % (ip_stats(ips)), file=sys.stderr)
    # Sort by availability (and use last success as tie breaker)
    ips.sort(key=lambda x: (x['uptime'], x['lastsuccess'], x['ip']), reverse=True)
    # Filter out hosts with multiple bitcoin ports, these are likely abusive
    ips = filtermultiport(ips)
    print('Filter out hosts with multiple bitcoin ports: %s' % (ip_stats(ips)), file=sys.stderr)
    # Look up ASNs and limit results, both per ASN and globally.
    ips = filterbyasn(ips, MAX_SEEDS_PER_ASN, NSEEDS)
    # Sort the results by IP address (for deterministic output).
    print('Look up ASNs and limit results, both per ASN and globally: %s' % (ip_stats(ips)), file=sys.stderr)
    ips.sort(key=lambda x: (x['net'], x['sortkey']))

    for ip in ips:
        if ip['net'] == 'ipv6':
            print('[%s]:%i' % (ip['ip'], ip['port']))
        else:
            print('%s:%i' % (ip['ip'], ip['port']))

if __name__ == '__main__':
    main()
<commit_msg>contrib: makeseeds: dedup by ip,port<commit_after>#!/usr/bin/env python3
# Copyright (c) 2013-2018 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
# Generate seeds.txt from Pieter's DNS seeder
#

import re
import sys
import dns.resolver
import collections

NSEEDS=512

MAX_SEEDS_PER_ASN=2

MIN_BLOCKS = 337600

# These are hosts that have been observed to be behaving strangely (e.g.
# aggressively connecting to every node).
SUSPICIOUS_HOSTS = {
    "130.211.129.106", "178.63.107.226",
    "83.81.130.26", "88.198.17.7", "148.251.238.178", "176.9.46.6",
    "54.173.72.127", "54.174.10.182", "54.183.64.54", "54.194.231.211",
    "54.66.214.167", "54.66.220.137", "54.67.33.14", "54.77.251.214",
    "54.94.195.96", "54.94.200.247"
}

PATTERN_IPV4 = re.compile(r"^((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})):(\d+)$")
PATTERN_IPV6 = re.compile(r"^\[([0-9a-z:]+)\]:(\d+)$")
PATTERN_ONION = re.compile(r"^([abcdefghijklmnopqrstuvwxyz234567]{16}\.onion):(\d+)$")
PATTERN_AGENT = re.compile(
    r"^/Satoshi:("
    r"0.14.(0|1|2|3|99)|"
    r"0.15.(0|1|2|99)|"
    r"0.16.(0|1|2|3|99)|"
    r"0.17.(0|0.1|1|2|99)|"
    r"0.18.(0|1|99)|"
    r"0.19.99"
    r")")

def parseline(line):
    sline = line.split()
    if len(sline) < 11:
       return None
    m = PATTERN_IPV4.match(sline[0])
    sortkey = None
    ip = None
    if m is None:
        m = PATTERN_IPV6.match(sline[0])
        if m is None:
            m = PATTERN_ONION.match(sline[0])
            if m is None:
                return None
            else:
                net = 'onion'
                ipstr = sortkey = m.group(1)
                port = int(m.group(2))
        else:
            net = 'ipv6'
            if m.group(1) in ['::']: # Not interested in localhost
                return None
            ipstr = m.group(1)
            sortkey = ipstr # XXX parse IPv6 into number, could use name_to_ipv6 from generate-seeds
            port = int(m.group(2))
    else:
        # Do IPv4 sanity check
        ip = 0
        for i in range(0,4):
            if int(m.group(i+2)) < 0 or int(m.group(i+2)) > 255:
                return None
            ip = ip + (int(m.group(i+2)) << (8*(3-i)))
        if ip == 0:
            return None
        net = 'ipv4'
        sortkey = ip
        ipstr = m.group(1)
        port = int(m.group(6))
    # Skip bad results.
    if sline[1] == 0:
        return None
    # Extract uptime %.
    uptime30 = float(sline[7][:-1])
    # Extract Unix timestamp of last success.
    lastsuccess = int(sline[2])
    # Extract protocol version.
    version = int(sline[10])
    # Extract user agent.
    agent = sline[11][1:-1]
    # Extract service flags.
    service = int(sline[9], 16)
    # Extract blocks.
    blocks = int(sline[8])
    # Construct result.
    return {
        'net': net,
        'ip': ipstr,
        'port': port,
        'ipnum': ip,
        'uptime': uptime30,
        'lastsuccess': lastsuccess,
        'version': version,
        'agent': agent,
        'service': service,
        'blocks': blocks,
        'sortkey': sortkey,
    }

def dedup(ips):
    '''deduplicate by address,port'''
    d = {}
    for ip in ips:
        d[ip['ip'],ip['port']] = ip
    return list(d.values())

def filtermultiport(ips):
    '''Filter out hosts with more nodes per IP'''
    hist = collections.defaultdict(list)
    for ip in ips:
        hist[ip['sortkey']].append(ip)
    return [value[0] for (key,value) in list(hist.items()) if len(value)==1]

def lookup_asn(net, ip):
    '''
    Look up the asn for an IP (4 or 6) address by querying cymry.com, or None
    if it could not be found.
    '''
    try:
        if net == 'ipv4':
            ipaddr = ip
            prefix = '.origin'
        else:                  # http://www.team-cymru.com/IP-ASN-mapping.html
            res = str()                         # 2001:4860:b002:23::68
            for nb in ip.split(':')[:4]:  # pick the first 4 nibbles
                for c in nb.zfill(4):           # right padded with '0'
                    res += c + '.'              # 2001 4860 b002 0023
            ipaddr = res.rstrip('.')            # 2.0.0.1.4.8.6.0.b.0.0.2.0.0.2.3
            prefix = '.origin6'

        asn = int([x.to_text() for x in dns.resolver.query('.'.join(
                   reversed(ipaddr.split('.'))) + prefix + '.asn.cymru.com',
                   'TXT').response.answer][0].split('\"')[1].split(' ')[0])
        return asn
    except Exception:
        sys.stderr.write('ERR: Could not resolve ASN for "' + ip + '"\n')
        return None

# Based on Greg Maxwell's seed_filter.py
def filterbyasn(ips, max_per_asn, max_total):
    # Sift out ips by type
    ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']]
    ips_onion = [ip for ip in ips if ip['net'] == 'onion']

    # Filter IPv46 by ASN
    result = []
    asn_count = collections.defaultdict(int)
    for ip in ips_ipv46:
        if len(result) == max_total:
            break
        asn = lookup_asn(ip['net'], ip['ip'])
        if asn is None or asn_count[asn] == max_per_asn:
            continue
        asn_count[asn] += 1
        result.append(ip)

    # Add back Onions
    result.extend(ips_onion)
    return result

def ip_stats(ips):
    hist = collections.defaultdict(int)
    for ip in ips:
        if ip is not None:
            hist[ip['net']] += 1

    return 'IPv4 %d, IPv6 %d, Onion %d' % (hist['ipv4'], hist['ipv6'], hist['onion'])

def main():
    lines = sys.stdin.readlines()
    ips = [parseline(line) for line in lines]

    print('Initial: %s' % (ip_stats(ips)), file=sys.stderr)
    # Skip entries with invalid address.
    ips = [ip for ip in ips if ip is not None]
    print('Skip entries with invalid address: %s' % (ip_stats(ips)), file=sys.stderr)
    # Skip duplicattes (in case multiple seeds files were concatenated)
    ips = dedup(ips)
    print('After removing duplicates: %s' % (ip_stats(ips)), file=sys.stderr)
    # Skip entries from suspicious hosts.
    ips = [ip for ip in ips if ip['ip'] not in SUSPICIOUS_HOSTS]
    print('Skip entries from suspicious hosts: %s' % (ip_stats(ips)), file=sys.stderr)
    # Enforce minimal number of blocks.
    ips = [ip for ip in ips if ip['blocks'] >= MIN_BLOCKS]
    print('Enforce minimal number of blocks: %s' % (ip_stats(ips)), file=sys.stderr)
    # Require service bit 1.
    ips = [ip for ip in ips if (ip['service'] & 1) == 1]
    print('Require service bit 1: %s' % (ip_stats(ips)), file=sys.stderr)
    # Require at least 50% 30-day uptime for clearnet, 10% for onion.
    req_uptime = {
        'ipv4': 50,
        'ipv6': 50,
        'onion': 10,
    }
    ips = [ip for ip in ips if ip['uptime'] > req_uptime[ip['net']]]
    print('Require minimum uptime: %s' % (ip_stats(ips)), file=sys.stderr)
    # Require a known and recent user agent.
    ips = [ip for ip in ips if PATTERN_AGENT.match(ip['agent'])]
    print('Require a known and recent user agent: %s' % (ip_stats(ips)), file=sys.stderr)
    # Sort by availability (and use last success as tie breaker)
    ips.sort(key=lambda x: (x['uptime'], x['lastsuccess'], x['ip']), reverse=True)
    # Filter out hosts with multiple bitcoin ports, these are likely abusive
    ips = filtermultiport(ips)
    print('Filter out hosts with multiple bitcoin ports: %s' % (ip_stats(ips)), file=sys.stderr)
    # Look up ASNs and limit results, both per ASN and globally.
    ips = filterbyasn(ips, MAX_SEEDS_PER_ASN, NSEEDS)
    # Sort the results by IP address (for deterministic output).
    print('Look up ASNs and limit results, both per ASN and globally: %s' % (ip_stats(ips)), file=sys.stderr)
    ips.sort(key=lambda x: (x['net'], x['sortkey']))

    for ip in ips:
        if ip['net'] == 'ipv6':
            print('[%s]:%i' % (ip['ip'], ip['port']))
        else:
            print('%s:%i' % (ip['ip'], ip['port']))

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


<commit_before>"""
picture.py
Author: <your name here>
Credit: <list sources used, if any>

Assignment:

Use the ggame library to "paint" a graphical picture of something (e.g. a house, a face or landscape).

Use at least:
1. Three different Color objects.
2. Ten different Sprite objects.
3. One (or more) RectangleAsset objects.
4. One (or more) CircleAsset objects.
5. One (or more) EllipseAsset objects.
6. One (or more) PolygonAsset objects.

See:
https://github.com/HHS-IntroProgramming/Standards-and-Syllabus/wiki/Displaying-Graphics
for general information on how to use ggame.

See:
http://brythonserver.github.io/ggame/
for detailed information on ggame.

"""
from ggame import App, Color, LineStyle, Sprite, RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset

# add your code here \/  \/  \/

# add your code here /\  /\  /\

myapp = App()
myapp.run()
<commit_msg>Updated from Brython Server: September 9, 2016 1:58:02 PM EDT<commit_after>"""
picture.py
Author: Andy Kotz
Credit: Kezar will probably do something

Assignment:

Use the ggame library to "paint" a graphical picture of something (e.g. a house, a face or landscape).

Use at least:
1. Three different Color objects.
2. Ten different Sprite objects.
3. One (or more) RectangleAsset objects.
4. One (or more) CircleAsset objects.
5. One (or more) EllipseAsset objects.
6. One (or more) PolygonAsset objects.

See:
https://github.com/HHS-IntroProgramming/Standards-and-Syllabus/wiki/Displaying-Graphics
for general information on how to use ggame.

See:
http://brythonserver.github.io/ggame/
for detailed information on ggame.

"""
from ggame import App, Color, LineStyle, Sprite, RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset

# add your code here \/  \/  \/

red =
green =
blue =
black =

# add your code here /\  /\  /\

myapp = App()
myapp.run()
<|endoftext|>


<commit_before>"""
picture.py
Author: Andy Kotz
Credit: Kezar helped me kn

Assignment:

Use the ggame library to "paint" a graphical picture of something (e.g. a house, a face or landscape).

Use at least:
1. Three different Color objects.
2. Ten different Sprite objects.
3. One (or more) RectangleAsset objects.
4. One (or more) CircleAsset objects.
5. One (or more) EllipseAsset objects.
6. One (or more) PolygonAsset objects.

See:
https://github.com/HHS-IntroProgramming/Standards-and-Syllabus/wiki/Displaying-Graphics
for general information on how to use ggame.

See:
http://brythonserver.github.io/ggame/
for detailed information on ggame.

"""
from ggame import App, Color, LineStyle, Sprite, RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset

# add your code here \/  \/  \/

red = Color(0xff0000, 1.0)
yellow = Color(0xdbd781, 1.0)
gray = Color(0xa7a7a7, 1.0)
black = Color(0x000000, 1.0)
green = Color(0x82DA30, 1.0)
brown = Color(0x977200, 1.0)

thinline = LineStyle(1, black)

circlecaverock = CircleAsset (150, thinline, gray)
rectanglesand = RectangleAsset (3000, 4000, thinline, yellow)
polygonhouse = PolygonAsset ([(450,450), (500,450), (500,400), (475,375), (450,400)], thinline, red)
ellipsecaveentr = EllipseAsset (75, 125, thinline, black)
ellipsetrunk = EllipseAsset(10, 100, thinline, brown)

Sprite (ellipsetrunk, (575, 450))
Sprite (circlecaverock, (900, 450))
Sprite (polygonhouse)
Sprite (ellipsecaveentr, (900, 450))
Sprite (rectanglesand, (300, 450))

# add your code here /\  /\  /\

myapp = App()
myapp.run()
<commit_msg>Updated from Brython Server: September 13, 2016 2:01:33 PM EDT<commit_after>"""
picture.py
Author: Andy Kotz
Credit: Kezar, Dan

Assignment:

Use the ggame library to "paint" a graphical picture of something (e.g. a house, a face or landscape).

Use at least:
1. Three different Color objects.
2. Ten different Sprite objects.
3. One (or more) RectangleAsset objects.
4. One (or more) CircleAsset objects.
5. One (or more) EllipseAsset objects.
6. One (or more) PolygonAsset objects.

See:
https://github.com/HHS-IntroProgramming/Standards-and-Syllabus/wiki/Displaying-Graphics
for general information on how to use ggame.

See:
http://brythonserver.github.io/ggame/
for detailed information on ggame.

"""
from ggame import App, Color, LineStyle, Sprite, RectangleAsset, CircleAsset, EllipseAsset, PolygonAsset

# add your code here \/  \/  \/

red = Color(0xff0000, 1.0)
yellow = Color(0xdbd781, 1.0)
gray = Color(0xa7a7a7, 1.0)
black = Color(0x000000, 1.0)
green = Color(0x82DA30, 1.0)
brown = Color(0x977200, 1.0)
orange = Color(0xE47B03, 1.0)
blue = Color(0x00B9FF, 1.0)
indigo = Color(0x0042FF, 1.0)
violet = Color(0x6400FF, 1.0)

thinline = LineStyle(1, black)

circlecaverock = CircleAsset (150, thinline, gray)
rectanglesand = RectangleAsset (3000, 4000, thinline, yellow)
polygonhouse = PolygonAsset ([(450,450), (500,450), (500,400), (475,375), (450,400)], thinline, red)
ellipsecaveentr = EllipseAsset (75, 125, thinline, black)
ellipsetrunk = EllipseAsset(10, 100, thinline, brown)
ellipseleaf = EllipseAsset(40, 12, thinline, green)
circleleaf = CircleAsset(8, thinline, brown)
rectangledoor = RectangleAsset(15, 30, thinline, black)
rectanglewindow = RectangleAsset(12, 12, thinline, black)

Sprite (ellipsetrunk, (575, 450))
Sprite (ellipsetrunk, (1400, 450))
Sprite (circlecaverock, (900, 450))
Sprite (polygonhouse)
Sprite (ellipsecaveentr, (900, 450))
Sprite (rectanglesand, (300, 450))
Sprite (ellipseleaf, (610, 360))
Sprite (ellipseleaf, (540, 360))
Sprite (ellipseleaf, (1435, 360))
Sprite (ellipseleaf, (1365, 360))
Sprite (circleleaf, (575, 360))
Sprite (circleleaf, (1400, 360))
Sprite (rectangledoor, (455, 420))
Sprite (rectanglewindow, (480, 425))

# add your code here /\  /\  /\

myapp = App()
myapp.run()
<|endoftext|>


<commit_before>#     Copyright 2014, Kay Hayen, mailto:kay.hayen@gmail.com
#
#     Python tests originally created or extracted from other peoples work. The
#     parts were too small to be protected.
#
#     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.
#

def simple_comparisons(x, y):
    if 'a' <= x <= y <= 'z':
        print "One"

    if 'a' <= x <= 'z':
        print "Two"

    if 'a' <= x > 'z':
        print "Three"

print "Simple comparisons:"

simple_comparisons( "c", "d" )

def side_effect():
    print "<side_effect>"

    return 7

def side_effect_comparisons():
    print "Should have side effect:"

    print 1 < side_effect() < 9

    print "Should not have side effect due to short circuit"

    print 3 < 2 < side_effect() < 9

print "Check for expected side effects only:"

side_effect_comparisons()

def function_torture_is():
    a = ( 1, 2, 3 )

    for x in a:
        for y in a:
            for z in a:
                print x, y, z, ":", x is y is z, x is not y is not z

function_torture_is()

print "Check if lambda can have expression chains:",

def function_lambda_with_chain():

    a = ( 1, 2, 3 )

    x = lambda x : x[0] < x[1] < x[2]

    print x(a)

function_lambda_with_chain()

print "Check if generators can have expression chains:",

def generator_function_with_chain():
    x = ( 1, 2, 3 )

    yield x[0] < x[1] < x[2]

print list( generator_function_with_chain() )

print "Check if list contractions can have expression chains:",

def contraction_with_chain():
    return [ x[0] < x[1] < x[2] for x in [ ( 1, 2, 3 ) ] ]

print contraction_with_chain()

def genexpr_with_chain():
    return ( x[0] < x[1] < x[2] for x in [ ( 1, 2, 3 ) ] )

print list( genexpr_with_chain() )

class class_with_chain:
    x = ( 1, 2, 3 )
    print x[0] < x[1] < x[2]

x = ( 1, 2, 3 )
print x[0] < x[1] < x[2]

class CustomOps(int):
    def __lt__(self, other):
        print "<", self, other

        return True

    def __gt__(self, other):
        print ">", self, other

        return False

print "Custom ops, to enforce chain eval order and short circuit:"
print CustomOps( 7 ) < CustomOps( 8 ) > CustomOps( 6 )

print "Custom ops, do short circuit:"
print CustomOps( 8 ) > CustomOps( 7 ) < CustomOps( 6 )

def inOperatorChain():
    print "In operator chains:"
    print 3 in [3,4] in [[3,4]]
    print 3 in [3,4] not in [[3,4]]

    if 3 in [3,4] in [[3,4]]:
       print "Yes"
    else:
       print "No"

    if 3 in [3,4] not in [[3,4]]:
       print "Yes"
    else:
       print "No"

inOperatorChain()

# Make sure the values are called and order is correct:

class A(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def __repr__(self):
        return "<Value %s %d>" % ( self.name, self.value )

    def __lt__(self, other):
        print "less than called for:", self, other, self.value, other.value, self.value < other.value

        if self.value < other.value:
            print "good"
            return 7
        else:
            print "bad"
            return 0

a = A("a",1)
b = A("b",2)
c = A("c",0)

print a < b < c
print "*" * 80

a = A("a",2)
b = A("b",1)
c = A("c",0)

print a < b < c
<commit_msg>Tests: Made the comparison chain test executable with Python3 directly too.<commit_after>#     Copyright 2014, Kay Hayen, mailto:kay.hayen@gmail.com
#
#     Python tests originally created or extracted from other peoples work. The
#     parts were too small to be protected.
#
#     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.
#

from __future__ import print_function

def simple_comparisons(x, y):
    if 'a' <= x <= y <= 'z':
        print("One")

    if 'a' <= x <= 'z':
        print("Two")

    if 'a' <= x > 'z':
        print("Three")

print("Simple comparisons:")

simple_comparisons("c", "d")

def side_effect():
    print("<side_effect>")

    return 7

def side_effect_comparisons():
    print("Should have side effect:")
    print(1 < side_effect() < 9)

    print("Should not have side effect due to short circuit:")
    print(3 < 2 < side_effect() < 9)

print("Check for expected side effects only:")

side_effect_comparisons()

def function_torture_is():
    a = (1, 2, 3)

    for x in a:
        for y in a:
            for z in a:
                print(x, y, z, ":", x is y is z, x is not y is not z)

function_torture_is()

print("Check if lambda can have expression chains:", end="")

def function_lambda_with_chain():

    a = (1, 2, 3)

    x = lambda x : x[0] < x[1] < x[2]

    print("lambda result is", x(a))

function_lambda_with_chain()

print("Check if generators can have expression chains:", end = "")

def generator_function_with_chain():
    x = (1, 2, 3)

    yield x[0] < x[1] < x[2]

print(list(generator_function_with_chain()))

print("Check if list contractions can have expression chains:", end = "")

def contraction_with_chain():
    return [ x[0] < x[1] < x[2] for x in [ ( 1, 2, 3 ) ] ]

print(contraction_with_chain())

print("Check if generator expressions can have expression chains:", end = "")

def genexpr_with_chain():
    return ( x[0] < x[1] < x[2] for x in [ ( 1, 2, 3 ) ] )

print(list(genexpr_with_chain()))

print("Check if class bodies can have expression chains:", end = "")

class class_with_chain:
    x = ( 1, 2, 3 )
    print(x[0] < x[1] < x[2])

x = ( 1, 2, 3 )
print(x[0] < x[1] < x[2])

class CustomOps(int):
    def __lt__(self, other):
        print("enter <", self, other)

        return True

    def __gt__(self, other):
        print("enter >", self, other)

        return False

print("Custom ops, to enforce chain eval order and short circuit:", end = "")
print(CustomOps(7) < CustomOps(8) > CustomOps(6))

print("Custom ops, doing short circuit:", end = "")
print(CustomOps(8) > CustomOps(7) < CustomOps(6))

def inOperatorChain():
    print("In operator chains:")
    print(3 in [3,4] in [[3,4]])
    print(3 in [3,4] not in [[3,4]])

    if 3 in [3,4] in [[3,4]]:
       print("Yes")
    else:
       print("No")

    if 3 in [3,4] not in [[3,4]]:
       print("Yes")
    else:
       print("No")

inOperatorChain()

# Make sure the values are called and order is correct:

class A(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value

    def __repr__(self):
        return "<Value %s %d>" % ( self.name, self.value )

    def __lt__(self, other):
        print("less than called for:", self, other, self.value, other.value, self.value < other.value)

        if self.value < other.value:
            print("good")
            return 7
        else:
            print("bad")
            return 0

a = A("a",1)
b = A("b",2)
c = A("c",0)

print(a < b < c)
print("*" * 80)

a = A("a",2)
b = A("b",1)
c = A("c",0)

print(a < b < c)
<|endoftext|>


<commit_before>import unittest
from constraint_system import ConstraintSystem
from models.constraint import Constraint
from models.strengths import Strength

class ConstraintSystemTest(unittest.TestCase):
  """
    This only tests basic getters and setters, no constraint solving technology.
  """
  def setUp(self):
    self.cs = ConstraintSystem()
    self.v1 = self.cs.create_variable("v1", 15)
    self.c1 = Constraint(lambda x: True, Strength.REQUIRED, [self.v1], [])

  def test_create_variable(self):
    self.assertTrue(self.v1 in self.cs.variables)

  def test_get_and_set_variable(self):
    self.assertEqual(self.v1.get_value, 15)
    self.v1.set_value(10)
    self.assertEqual(10, self.v1.get_value())

  def test_variable_constraints_is_set_after_add_constraint(self):
    self.cs.add_constraint(self.c1)
    self.assertTrue(self.c1 in self.v1.constraints)

  def test_variable_constraints_is_set_after_remove_constraint(self):
    self.cs.add_constraint(self.c1)
    self.cs.remove_constraint(self.c1)
    self.assertEqual([], self.v1.constraints)
<commit_msg>fixed tests<commit_after>import unittest
from constraint_system import ConstraintSystem
from models.constraint import Constraint
from models.strengths import Strength

class ConstraintSystemTest(unittest.TestCase):
  """
    This only tests basic getters and setters, no constraint solving technology.
  """
  def setUp(self):
    self.cs = ConstraintSystem()
    self.v1 = self.cs.create_variable("v1", 15)
    self.c1 = Constraint(lambda x: True, Strength.REQUIRED, [self.v1], [])

  def test_create_variable(self):
    self.assertTrue(self.v1 in self.cs.variables)

  def test_get_and_set_variable(self):
    self.assertEqual(self.v1.get_value(), 15)
    self.v1.set_value(10)
    self.assertEqual(10, self.v1.get_value())

  def test_variable_constraints_is_set_after_add_constraint(self):
    self.cs.add_constraint(self.c1)
    self.assertTrue(self.c1 in self.v1.constraints)

  def test_variable_constraints_is_set_after_remove_constraint(self):
    self.cs.add_constraint(self.c1)
    self.cs.remove_constraint(self.c1)
    self.assertEqual([], self.v1.constraints)
<|endoftext|>


<commit_before>"""
Form Class for Promo Modules
"""

from flask_wtf import FlaskForm
from wtforms import StringField, FloatField, IntegerField, SubmitField
from wtforms.validators import InputRequired, DataRequired, Length, Email
from wtforms.fields.html5 import DateField

class FlightForm(FlaskForm):
    """
    Form Class for Flights

    email           =   Email
    place_from      =   String
    place_to        =   String
    departure_date  =   Date
    arrival_date    =   Date
    budget_range    =   Float
    head_count      =   Integer
    """

    email = StringField('Email Address',
                        validators=[InputRequired(message='Please insert an Email Address'),
                                    Email(
                                        message='Please input a valid Email Address'),
                                    Length(max=100)])
    place_from = StringField('Departure Place',
                             validators=[InputRequired(message='Please insert a Departure Place'),
                                         Length(max=100)])
    place_to = StringField('Arrival Place',
                           validators=[InputRequired(message='Please insert an Arrival Place'),
                                       Length(max=100)])
    departure_date = DateField('Departure Date',
                               validators=[InputRequired(message='Please enter a Departure Date')])
    arrival_date = DateField('Arrival Date',
                             validators=[InputRequired(message='Please enter an Arrival Date')])
    budget_range = FloatField('Budget Range',
                              validators=[InputRequired(message='Please enter a Budget Range')])
    head_count = IntegerField('Head Count',
                              validators=[InputRequired(message='Please enter the head count')])
    submit = SubmitField('Submit')

class HotelForm(FlaskForm):
    """
    Form Class for Hotels.

    Email           =   Email
    Hotel Location  =   String
    Check in Date   =   Date
    Check out Date  =   Date
    Number of Rooms =   Integer
    Budget Range    =   Float
    """
    email = StringField('Email Address',
                        validators=[DataRequired('Please insert an Email Address'),
                                    Email(
                                        message='Please insert a valid Email Address'),
                                    Length(max=100)])
    hotel_location = StringField('Hotel Location',
                                 [DataRequired('Please insert a Departure Place'),
                                  Length(max=100)])
    check_in_date = DateField('Departure Date',
                              [DataRequired('Please enter a Departure Date')])
    check_out_date = DateField('Arrival Date',
                               [DataRequired('Please enter an Arrival Date')])
    number_of_rooms = IntegerField('Number of Rooms',
                                   [DataRequired('Please Enter number of Rooms needed')])
    budget_range = FloatField('Budget Range',
                              [DataRequired('Please enter a Budget Range')])
    submit = SubmitField('Submit')
<commit_msg>Fixed date field for Promo module<commit_after>"""
Form Class for Promo Modules
"""

from flask_wtf import FlaskForm
from wtforms import StringField, FloatField, IntegerField, SubmitField
from wtforms.validators import InputRequired, DataRequired, Length, Email
from wtforms.fields import DateField

class FlightForm(FlaskForm):
    """
    Form Class for Flights

    email           =   Email
    place_from      =   String
    place_to        =   String
    departure_date  =   Date
    arrival_date    =   Date
    budget_range    =   Float
    head_count      =   Integer
    """

    email = StringField('Email Address',
                        validators=[InputRequired(message='Please insert an Email Address'),
                                    Email(
                                        message='Please input a valid Email Address'),
                                    Length(max=100)])
    place_from = StringField('Departure Place',
                             validators=[InputRequired(message='Please insert a Departure Place'),
                                         Length(max=100)])
    place_to = StringField('Arrival Place',
                           validators=[InputRequired(message='Please insert an Arrival Place'),
                                       Length(max=100)])
    departure_date = DateField('Departure Date',
                               validators=[InputRequired(message='Please enter a Departure Date')])
    arrival_date = DateField('Arrival Date',
                             validators=[InputRequired(message='Please enter an Arrival Date')])
    budget_range = FloatField('Budget Range',
                              validators=[InputRequired(message='Please enter a Budget Range')])
    head_count = IntegerField('Head Count',
                              validators=[InputRequired(message='Please enter the head count')])
    submit = SubmitField('Submit')

class HotelForm(FlaskForm):
    """
    Form Class for Hotels.

    Email           =   Email
    Hotel Location  =   String
    Check in Date   =   Date
    Check out Date  =   Date
    Number of Rooms =   Integer
    Budget Range    =   Float
    """
    email = StringField('Email Address',
                        validators=[DataRequired('Please insert an Email Address'),
                                    Email(
                                        message='Please insert a valid Email Address'),
                                    Length(max=100)])
    hotel_location = StringField('Hotel Location',
                                 [DataRequired('Please insert a Departure Place'),
                                  Length(max=100)])
    check_in_date = DateField('Departure Date',
                              [DataRequired('Please enter a Departure Date')])
    check_out_date = DateField('Arrival Date',
                               [DataRequired('Please enter an Arrival Date')])
    number_of_rooms = IntegerField('Number of Rooms',
                                   [DataRequired('Please Enter number of Rooms needed')])
    budget_range = FloatField('Budget Range',
                              [DataRequired('Please enter a Budget Range')])
    submit = SubmitField('Submit')
<|endoftext|>


<commit_before>from settings.base import *
from wheelcms_project.settings.base.util import get_env_variable

DEBUG=False

STRACKS_URL = None # get_env_variable('STRACKS_URL', '')

STRACKS_CONNECTOR = None

if STRACKS_URL:
    from stracks_api.connector import ASyncHTTPConnector
    STRACKS_CONNECTOR = ASyncHTTPConnector(STRACKS_URL)
    MIDDLEWARE_CLASSES += (
        'stracks_api.middleware.StracksMiddleware',
    )

ALLOWED_HOSTS = ["wheelcms.io", "demo.wheelcms.io", "www.wheelcms.io", "demo2.wheelcms.io"]
<commit_msg>include the middleware fixer<commit_after>from settings.base import *
from wheelcms_project.settings.base.util import get_env_variable

DEBUG=False

MIDDLEWARE_CLASSES += (
    'wheelcms_axle.middleware.FixMessageMiddleware',
)

STRACKS_URL = None # get_env_variable('STRACKS_URL', '')

STRACKS_CONNECTOR = None

if STRACKS_URL:
    from stracks_api.connector import ASyncHTTPConnector
    STRACKS_CONNECTOR = ASyncHTTPConnector(STRACKS_URL)
    MIDDLEWARE_CLASSES += (
        'stracks_api.middleware.StracksMiddleware',
    )

ALLOWED_HOSTS = ["wheelcms.io", "demo.wheelcms.io", "www.wheelcms.io", "demo2.wheelcms.io"]
<|endoftext|>


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

from datetime import datetime
import logging
import sys
from xml.etree import ElementTree

import argparse

import giraffe.friends.models
from giraffe.publisher.models import Asset

def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    parser = argparse.ArgumentParser(description='Import posts from a Vox Atom XML export.')
    parser.add_argument('source', metavar='FILE', help='The filename of the XML export (or - for stdin)')
    parser.add_argument('-v', dest='verbosity', action='append_const', const=1,
        help='Be more verbose (stackable)', default=[2])
    parser.add_argument('-q', dest='verbosity', action='append_const', const=-1,
        help='Be less verbose (stackable)')

    args = parser.parse_args(argv)

    verbosity = sum(args.verbosity)
    verbosity = 0 if verbosity < 0 else verbosity if verbosity < 4 else 4
    log_level = [logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG][verbosity]
    logging.getLogger().setLevel(level=log_level)
    logging.info('Set log level to %s', logging.getLevelName(log_level))

    import_assets(sys.stdin if args.source == '-' else args.source)

    return 0

def import_assets(source):
    tree = ElementTree.parse(source)

    groups = dict()

    for asset_el in tree.findall('{http://www.w3.org/2005/Atom}entry'):
        # Skip comments this go-round.
        if asset_el.find('{http://purl.org/syndication/thread/1.0}in-reply-to') is not None:
            continue

        atom_id = asset_el.findtext('{http://www.w3.org/2005/Atom}id')
        logging.debug('Parsing asset %s', atom_id)

        try:
            asset = Asset.objects.get(atom_id=atom_id)
        except Asset.DoesNotExist:
            asset = Asset(atom_id=atom_id)

        asset.title = asset_el.findtext('{http://www.w3.org/2005/Atom}title')

        publ = asset_el.findtext('{http://www.w3.org/2005/Atom}published')
        publ_dt = datetime.strptime(publ, '%Y-%m-%dT%H:%M:%SZ')
        asset.published = publ_dt

        content_el = asset_el.find('{http://www.w3.org/2005/Atom}content')
        content_type = content_el.get('type')
        if content_type == 'html':
            asset.content = content_el.text
        elif content_type == 'xhtml':
            html_el = content_el.find('{http://www.w3.org/1999/xhtml}div')
            html = ''.join(ElementTree.tostring(el, 'utf8') for el in html_el.getchildren())
            asset.content = html

        asset.imported = True
        asset.save()
        logging.info('Saved new asset %s (%s) as #%d', atom_id, asset.title, asset.pk)

        asset_groups = list()
        privacies = asset_el.findall('{http://www.sixapart.com/ns/atom/privacy}privacy/{http://www.sixapart.com/ns/atom/privacy}allow')
        for privacy in privacies:
            assert privacy.get('policy') == 'http://www.sixapart.com/ns/atom/permissions#read', 'Privacy policy for post is not about reading :('
            group_ref = privacy.get('ref')
            if group_ref == 'http://www.sixapart.com/ns/atom/groups#everyone':
                # Ignore this one.
                continue
            try:
                group = groups[group_ref]
            except KeyError:
                group, created = giraffe.friends.models.Group.objects.get_or_create(tag=group_ref,
                    defaults={'display_name': privacy.get('name')})
                groups[group_ref] = group
            asset_groups.append(group)
        logging.debug('Assigning asset %s to %d groups', atom_id, len(asset_groups))
        asset.private_to = asset_groups

    # TODO: import comments
    # TODO: import friends?

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>Import comments from the Vox export too<commit_after>#!/usr/bin/env python

from datetime import datetime
import logging
import sys
from xml.etree import ElementTree

import argparse

import giraffe.friends.models
from giraffe.publisher.models import Asset

def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    parser = argparse.ArgumentParser(description='Import posts from a Vox Atom XML export.')
    parser.add_argument('source', metavar='FILE', help='The filename of the XML export (or - for stdin)')
    parser.add_argument('-v', dest='verbosity', action='append_const', const=1,
        help='Be more verbose (stackable)', default=[2])
    parser.add_argument('-q', dest='verbosity', action='append_const', const=-1,
        help='Be less verbose (stackable)')

    args = parser.parse_args(argv)

    verbosity = sum(args.verbosity)
    verbosity = 0 if verbosity < 0 else verbosity if verbosity < 4 else 4
    log_level = [logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG][verbosity]
    logging.getLogger().setLevel(level=log_level)
    logging.info('Set log level to %s', logging.getLevelName(log_level))

    import_assets(sys.stdin if args.source == '-' else args.source)

    return 0

def person_for_openid(openid, display_name):
    try:
        ident_obj = giraffe.friends.models.Identity.objects.get(openid=openid)
    except giraffe.friends.models.Identity.DoesNotExist:
        # Who? I guess we need to make a Person too.
        person = giraffe.friends.models.Person()
        person.display_name = display_name
        person.save()
        ident_obj = giraffe.friends.models.Identity(openid=openid)
        ident_obj.person = person
        ident_obj.save()

    return ident_obj.person

def basic_asset_for_element(asset_el):
    atom_id = asset_el.findtext('{http://www.w3.org/2005/Atom}id')
    logging.debug('Parsing asset %s', atom_id)

    try:
        asset = Asset.objects.get(atom_id=atom_id)
    except Asset.DoesNotExist:
        asset = Asset(atom_id=atom_id)
    asset.imported = True

    publ = asset_el.findtext('{http://www.w3.org/2005/Atom}published')
    publ_dt = datetime.strptime(publ, '%Y-%m-%dT%H:%M:%SZ')
    asset.published = publ_dt

    content_el = asset_el.find('{http://www.w3.org/2005/Atom}content')
    content_type = content_el.get('type')
    if content_type == 'html':
        asset.content = content_el.text
    elif content_type == 'xhtml':
        html_el = content_el.find('{http://www.w3.org/1999/xhtml}div')
        html = html_el.text or u''
        html += u''.join(ElementTree.tostring(el) for el in html_el.getchildren())
        asset.content = html

    author_el = asset_el.find('{http://www.w3.org/2005/Atom}author')
    author_name = author_el.findtext('{http://www.w3.org/2005/Atom}name')
    openid = author_el.findtext('{http://www.w3.org/2005/Atom}uri')
    if openid != 'http://www.vox.com/gone/':
        asset.author = person_for_openid(openid, author_name)

    return asset

def import_assets(source):
    tree = ElementTree.parse(source)

    groups = dict()

    for asset_el in tree.findall('{http://www.w3.org/2005/Atom}entry'):
        # Skip comments this go-round.
        if asset_el.find('{http://purl.org/syndication/thread/1.0}in-reply-to') is not None:
            continue

        asset = basic_asset_for_element(asset_el)

        asset.title = asset_el.findtext('{http://www.w3.org/2005/Atom}title')

        asset.save()
        logging.info('Saved new asset %s (%s) as #%d', asset.atom_id, asset.title, asset.pk)

        asset_groups = list()
        privacies = asset_el.findall('{http://www.sixapart.com/ns/atom/privacy}privacy/{http://www.sixapart.com/ns/atom/privacy}allow')
        private_group = giraffe.friends.models.Group.objects.get(tag='private')
        for privacy in privacies:
            assert privacy.get('policy') == 'http://www.sixapart.com/ns/atom/permissions#read', 'Privacy policy for post is not about reading :('
            group_ref = privacy.get('ref')

            # Ignore "everyone" since that's the absence of a group.
            if group_ref == 'http://www.sixapart.com/ns/atom/groups#everyone':
                # Ignore this one.
                continue

            # Use the special private group for "self".
            if group_ref == 'http://www.sixapart.com/ns/atom/groups#self':
                asset_groups.append(private_group)
                continue

            try:
                group = groups[group_ref]
            except KeyError:
                group, created = giraffe.friends.models.Group.objects.get_or_create(tag=group_ref,
                    defaults={'display_name': privacy.get('name')})
                groups[group_ref] = group
            asset_groups.append(group)

        logging.debug('Assigning asset %s to %d groups', asset.atom_id, len(asset_groups))
        asset.private_to = asset_groups

    for comment_el in tree.findall('{http://www.w3.org/2005/Atom}entry'):
        # This time, *only* comments.
        reply_el = asset_el.find('{http://purl.org/syndication/thread/1.0}in-reply-to')
        if reply_el is None:
            continue

        parent_ref = reply_el.get('ref')
        try:
            parent_asset = Asset.objects.get(atom_id=parent_ref)
        except Asset.DoesNotExist:
            # OOPS
            logging.warn('Referenced parent asset %s does not exist; skipping comment :(', reply_el.get('href'))
            continue

        asset = basic_asset_for_element(comment_el)

        asset.in_reply_to = parent_asset
        asset.in_thread_of = parent_asset.in_thread_of or parent_asset

        asset.save()

        asset.private_to = parent_asset.private_to.all()

    # TODO: import friends?
    # TODO: import favorites :(

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


<commit_before>from torchtext import data
import os
import pdb
import random
import math

class VP(data.Dataset):
    """modeled after Shawn1993 github user's Pytorch implementation of Kim2014 - cnn for text categorization"""

    filename = "wilkins_corrected.shuffled.tsv"

    @staticmethod
    def sort_key(ex):
        return len(ex.text)

    def __init__(self, text_field, label_field, path=None, examples=None, **kwargs):
        """Create a virtual patient (VP) dataset instance given a path and fields.

        Arguments:
            text_field: The field that will be used for text data.
            label_field: The field that will be used for label data.
            path: Path to the data file.
            examples: The examples contain all the data.
            Remaining keyword arguments: Passed to the constructor of
                data.Dataset.
        """
        #no preprocessing needed
        fields = [('text', text_field), ('label', label_field)]

        if examples is None:
                path = self.dirname if path is None else path
                examples = []
                with open(os.path.join(path, self.filename)) as f:
                    lines = f.readlines()
                    #pdb.set_trace()
                    examples += [
                        data.Example.fromlist([line.split("\t")[1], line.split("\t")[0]], fields) for line in lines] #in f]
                    #assume "target \t source", one instance per line
        # print(examples[0].text)
        super(VP, self).__init__(examples, fields, **kwargs)

    @classmethod
    #def splits(cls, text_field, label_field, dev_ratio=.1, shuffle=True ,root='.', **kwargs):
    def splits(cls, text_field, label_field, numfolds=10, foldid=None, dev_ratio=.1, shuffle=False ,root='.', **kwargs):

        """Create dataset objects for splits of the VP dataset.

        Arguments:
            text_field: The field that will be used for the sentence.
            label_field: The field that will be used for label data.
            dev_ratio: The ratio that will be used to get split validation dataset.
            shuffle: Whether to shuffle the data before split.
            root: The root directory that the dataset's zip archive will be
                expanded into; therefore the directory in whose trees
                subdirectory the data files will be stored.
            train: The filename of the train data. Default: 'train.txt'.
            Remaining keyword arguments: Passed to the splits method of
                Dataset.
        """
        #path = cls.download_or_unzip(root)
        #examples = cls(text_field, label_field, path=path, **kwargs).examples
        examples = cls(text_field, label_field, path=root, **kwargs).examples
        if shuffle: random.shuffle(examples)

        if foldid==None:
            dev_index = -1 * int(dev_ratio*len(examples))
            return (cls(text_field, label_field, examples=examples[:dev_index]),
                    cls(text_field, label_field, examples=examples[dev_index:]))
        else:
            #get all folds
            fold_size = math.floor(len(examples)/numfolds)
            folds = []
            for fold in range(numfolds):
                startidx = fold*fold_size
                endidx = startidx+fold_size
                folds += [examples[startidx:endidx]]

            #take all folds except foldid as training/dev
            traindev = [fold for idx, fold in enumerate(folds) if idx != foldid]
            traindev = [item for sublist in traindev for item in sublist]
            dev_index = -1 * int(dev_ratio*len(traindev))

            #test will be entire held out section (foldid)
            test = folds[foldid]

            return (cls(text_field, label_field, examples=traindev[:dev_index]),
                    cls(text_field, label_field, examples=traindev[dev_index:]),
                    cls(text_field, label_field, examples=test))
<commit_msg>cleaning<commit_after>from torchtext import data
import os
import pdb
import random
import math
import re

class VP(data.Dataset):
    """modeled after Shawn1993 github user's Pytorch implementation of Kim2014 - cnn for text categorization"""

    filename = "wilkins_corrected.shuffled.tsv"

    @staticmethod
    def sort_key(ex):
        return len(ex.text)

    def __init__(self, text_field, label_field, path=None, examples=None, **kwargs):
        """Create a virtual patient (VP) dataset instance given a path and fields.

        Arguments:
            text_field: The field that will be used for text data.
            label_field: The field that will be used for label data.
            path: Path to the data file.
            examples: The examples contain all the data.
            Remaining keyword arguments: Passed to the constructor of
                data.Dataset.
        """
        #no preprocessing needed
        fields = [('text', text_field), ('label', label_field)]

        if examples is None:
                path = self.dirname if path is None else path
                examples = []
                with open(os.path.join(path, self.filename)) as f:
                    lines = f.readlines()
                    #pdb.set_trace()
                    examples += [
                        data.Example.fromlist([clean_str(line.split("\t")[1]), line.split("\t")[0]], fields) for line in lines] #in f]
                    #assume "target \t source", one instance per line
        # print(examples[0].text)
        super(VP, self).__init__(examples, fields, **kwargs)

    @classmethod
    #def splits(cls, text_field, label_field, dev_ratio=.1, shuffle=True ,root='.', **kwargs):
    def splits(cls, text_field, label_field, numfolds=10, foldid=None, dev_ratio=.1, shuffle=False ,root='.', **kwargs):

        """Create dataset objects for splits of the VP dataset.

        Arguments:
            text_field: The field that will be used for the sentence.
            label_field: The field that will be used for label data.
            dev_ratio: The ratio that will be used to get split validation dataset.
            shuffle: Whether to shuffle the data before split.
            root: The root directory that the dataset's zip archive will be
                expanded into; therefore the directory in whose trees
                subdirectory the data files will be stored.
            train: The filename of the train data. Default: 'train.txt'.
            Remaining keyword arguments: Passed to the splits method of
                Dataset.
        """
        #path = cls.download_or_unzip(root)
        #examples = cls(text_field, label_field, path=path, **kwargs).examples
        examples = cls(text_field, label_field, path=root, **kwargs).examples
        if shuffle: random.shuffle(examples)

        if foldid==None:
            dev_index = -1 * int(dev_ratio*len(examples))
            return (cls(text_field, label_field, examples=examples[:dev_index]),
                    cls(text_field, label_field, examples=examples[dev_index:]))
        else:
            #get all folds
            fold_size = math.floor(len(examples)/numfolds)
            folds = []
            for fold in range(numfolds):
                startidx = fold*fold_size
                endidx = startidx+fold_size
                folds += [examples[startidx:endidx]]

            #take all folds except foldid as training/dev
            traindev = [fold for idx, fold in enumerate(folds) if idx != foldid]
            traindev = [item for sublist in traindev for item in sublist]
            dev_index = -1 * int(dev_ratio*len(traindev))

            #test will be entire held out section (foldid)
            test = folds[foldid]

            return (cls(text_field, label_field, examples=traindev[:dev_index]),
                    cls(text_field, label_field, examples=traindev[dev_index:]),
                    cls(text_field, label_field, examples=test))

def clean_str(string):
  """
  Tokenization/string cleaning for all datasets except for SST.
  """
  string = re.sub("[^A-Za-z0-9(),!?\'\`]", " ", string)
  string = re.sub("\'s", " \'s", string)
  string = re.sub("\'m", " \'m", string)
  string = re.sub("\'ve", " \'ve", string)
  string = re.sub("n\'t", " n\'t", string)
  string = re.sub("\'re", " \'re", string)
  string = re.sub("\'d", " \'d", string)
  string = re.sub("\'ll", " \'ll", string)
  string = re.sub(",", " , ", string)
  string = re.sub("!", " ! ", string)
  string = re.sub("\(", " ( ", string)
  string = re.sub("\)", " ) ", string)
  string = re.sub("\?", " ? ", string)
  string = re.sub("\s{2,}", " ", string)
  return string.strip().lower()<|endoftext|>


<commit_before>#   Copyright (c) 2006-2007 Open Source Applications Foundation
#
#   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 logging, os, sys, tempfile, shutil

def findInPath(fileName, path=os.environ['PATH']):
    dirs = path.split(os.pathsep)
    for dir in dirs:
        if os.path.isfile(os.path.join(dir, fileName)):
            return os.path.join(dir, fileName)
        if os.name == 'nt' or sys.platform == 'cygwin':
            if os.path.isfile(os.path.join(dir, fileName + ".exe")):
                return os.path.join(dir, fileName + ".exe")
    return None

CONSOLE_LOG_LEVEL = logging.INFO
FILE_LOG_LEVEL    = logging.INFO

SERVER_HTTP_PORT = 4444
PLATFORM         = sys.platform
WINDMILL_PATH    = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
JS_PATH          = os.path.join(WINDMILL_PATH, 'html')
SAVES_PATH       = None

TEST_URL  = 'http://windmill.osafoundation.org/tutorial'
TEST_FILE = None
TEST_DIR  = None
FORWARDING_TEST_URL = None

USECODE             = False
EXIT_ON_DONE        = False
CONTINUE_ON_FAILURE = False
ENABLE_PDB          = False
BROWSER_DEBUGGING   = False
START_FIREFOX       = False

PYTHON_TEST_FRAME = False
PYTHON_TEST_FILE  = None

JAVASCRIPT_TEST_DIR = None

# Browser prefs
MOZILLA_COMMAND = None
SAFARI_BINARY   = None
SAFARI_COMMAND  = None

# Mozilla prefs
MOZILLA_CREATE_NEW_PROFILE     = True

MOZILLA_PROFILE_PATH = tempfile.mkdtemp(suffix='.windmill')

while MOZILLA_PROFILE_PATH.find('-') is not -1:
    shutil.rmtree(MOZILLA_PROFILE_PATH)
    MOZILLA_PROFILE_PATH = tempfile.mkdtemp(suffix='.windmill')

if PLATFORM == 'darwin':
    firefoxApp = os.path.join('Applications', 'Firefox.app')
    firefoxDir = os.path.join(os.path.expanduser('~/'), firefoxApp)

    if not os.path.isdir(firefoxDir):
        firefoxDir = os.path.join('/', firefoxApp)

    MOZILLA_DEFAULT_PROFILE = os.path.join(firefoxDir, 'Contents', 'MacOS', 'defaults', 'profile')
    MOZILLA_BINARY          = os.path.join(firefoxDir, 'Contents', 'MacOS', 'firefox-bin')
    SAFARI_BINARY           = '/Applications/Safari.app/Contents/MacOS/Safari'
    SAFARI_COMMAND          = ['open', '-a', SAFARI_BINARY, TEST_URL+'/windmill-serv/start.html']
    NETWORKSETUP_BINARY = '/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/networksetup'

elif sys.platform == 'linux2':
    firefoxBin = findInPath('firefox')

    if firefoxBin is not None and os.path.isfile(firefoxBin):
        MOZILLA_BINARY = firefoxBin

    if os.path.isdir('/usr/share/firefox/defaults/profile'):
        MOZILLA_DEFAULT_PROFILE = '/usr/share/firefox/defaults/profile'
    if os.path.isdir('/usr/lib/mozilla-firefox/defaults/profile'):
        MOZILLA_DEFAULT_PROFILE = '/usr/lib/mozilla-firefox/defaults/profile'

elif os.name == 'nt' or sys.platform == 'cygwin':
    IE_BINARY  = findInPath('iexplore')
    firefoxBin = findInPath('firefox')

    if firefoxBin is None:
        try:
            firefoxBin = os.path.join(os.environ['ProgramFiles'], 'Mozilla Firefox', 'firefox.exe')
        except:
            firefoxBin = None

    if firefoxBin is not None and os.path.isfile(firefoxBin):
        firefoxDir = os.path.dirname(firefoxBin)

        MOZILLA_BINARY          = firefoxBin
        MOZILLA_DEFAULT_PROFILE = os.path.join(firefoxDir, 'defaults', 'profile')

if __name__ == '__main__':
    if '--test' in sys.argv:
        print 'running on           ', PLATFORM
        print 'we are at            ', WINDMILL_PATH
        print 'our JS is at         ', JS_PATH
        print 'firefox is at        ', MOZILLA_BINARY
        print 'default profile is at', MOZILLA_DEFAULT_PROFILE

<commit_msg>This was to fix a perceived problem in the firefox launcher on linux which is no longer an issue.<commit_after>#   Copyright (c) 2006-2007 Open Source Applications Foundation
#
#   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 logging, os, sys, tempfile, shutil

def findInPath(fileName, path=os.environ['PATH']):
    dirs = path.split(os.pathsep)
    for dir in dirs:
        if os.path.isfile(os.path.join(dir, fileName)):
            return os.path.join(dir, fileName)
        if os.name == 'nt' or sys.platform == 'cygwin':
            if os.path.isfile(os.path.join(dir, fileName + ".exe")):
                return os.path.join(dir, fileName + ".exe")
    return None

CONSOLE_LOG_LEVEL = logging.INFO
FILE_LOG_LEVEL    = logging.INFO

SERVER_HTTP_PORT = 4444
PLATFORM         = sys.platform
WINDMILL_PATH    = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
JS_PATH          = os.path.join(WINDMILL_PATH, 'html')
SAVES_PATH       = None

TEST_URL  = 'http://windmill.osafoundation.org/tutorial'
TEST_FILE = None
TEST_DIR  = None
FORWARDING_TEST_URL = None

USECODE             = False
EXIT_ON_DONE        = False
CONTINUE_ON_FAILURE = False
ENABLE_PDB          = False
BROWSER_DEBUGGING   = False
START_FIREFOX       = False

PYTHON_TEST_FRAME = False
PYTHON_TEST_FILE  = None

JAVASCRIPT_TEST_DIR = None

# Browser prefs
MOZILLA_COMMAND = None
SAFARI_BINARY   = None
SAFARI_COMMAND  = None

# Mozilla prefs
MOZILLA_CREATE_NEW_PROFILE     = True

MOZILLA_PROFILE_PATH = tempfile.mkdtemp(suffix='.windmill')

if PLATFORM == 'darwin':
    firefoxApp = os.path.join('Applications', 'Firefox.app')
    firefoxDir = os.path.join(os.path.expanduser('~/'), firefoxApp)

    if not os.path.isdir(firefoxDir):
        firefoxDir = os.path.join('/', firefoxApp)

    MOZILLA_DEFAULT_PROFILE = os.path.join(firefoxDir, 'Contents', 'MacOS', 'defaults', 'profile')
    MOZILLA_BINARY          = os.path.join(firefoxDir, 'Contents', 'MacOS', 'firefox-bin')
    SAFARI_BINARY           = '/Applications/Safari.app/Contents/MacOS/Safari'
    SAFARI_COMMAND          = ['open', '-a', SAFARI_BINARY, TEST_URL+'/windmill-serv/start.html']
    NETWORKSETUP_BINARY = '/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/networksetup'

elif sys.platform == 'linux2':
    firefoxBin = findInPath('firefox')

    if firefoxBin is not None and os.path.isfile(firefoxBin):
        MOZILLA_BINARY = firefoxBin

    if os.path.isdir('/usr/share/firefox/defaults/profile'):
        MOZILLA_DEFAULT_PROFILE = '/usr/share/firefox/defaults/profile'
    if os.path.isdir('/usr/lib/mozilla-firefox/defaults/profile'):
        MOZILLA_DEFAULT_PROFILE = '/usr/lib/mozilla-firefox/defaults/profile'

elif os.name == 'nt' or sys.platform == 'cygwin':
    IE_BINARY  = findInPath('iexplore')
    firefoxBin = findInPath('firefox')

    if firefoxBin is None:
        try:
            firefoxBin = os.path.join(os.environ['ProgramFiles'], 'Mozilla Firefox', 'firefox.exe')
        except:
            firefoxBin = None

    if firefoxBin is not None and os.path.isfile(firefoxBin):
        firefoxDir = os.path.dirname(firefoxBin)

        MOZILLA_BINARY          = firefoxBin
        MOZILLA_DEFAULT_PROFILE = os.path.join(firefoxDir, 'defaults', 'profile')

if __name__ == '__main__':
    if '--test' in sys.argv:
        print 'running on           ', PLATFORM
        print 'we are at            ', WINDMILL_PATH
        print 'our JS is at         ', JS_PATH
        print 'firefox is at        ', MOZILLA_BINARY
        print 'default profile is at', MOZILLA_DEFAULT_PROFILE

<|endoftext|>


<commit_before>"""
.. module:: base
    :synopsis: All routes on the ``base`` Blueprint, as well as error handlers,
        before and after request handlers, and context processors.

.. moduleauthor:: Dan Schlosser <dan@danrs.ch>
"""

import sys
from flask import g, session, render_template, request, redirect, Blueprint
from mongoengine.queryset import DoesNotExist
import requests

from app import app
from app.models import User

SUPER_USER_GPLUS_ID = 'super'

base = Blueprint('base', __name__)

@app.errorhandler(Exception)
def exceptionHandler(error):
    """Handle uncaught exceptions."""
    app.logger.error("Uncaught Exception", exc_info=sys.exc_info())
    app.handle_exception(error) # default error handler

@app.errorhandler(400)
def bad_request(error):
    """Handle 400 errors."""
    return render_template('error/400.html'), 400

@app.errorhandler(401)
def not_authorized(error):
    """Handle 401 errors."""
    return render_template('error/401.html'), 401

@app.errorhandler(403)
def forbidden(error):
    """Handle 403 errors."""
    return render_template('error/403.html'), 403

@app.errorhandler(404)
def not_found(error):
    """Handle 404 errors."""
    old_site_url = 'http://adicu.github.com' + request.path
    try:
        response = requests.head(old_site_url, allow_redirects=True)
        if response.status_code == 200:
            return redirect(old_site_url)
    except requests.exceptions.ConnectionError:
        pass

    return render_template('error/404.html'), 404

@app.errorhandler(405)
def method_not_allowed(error):
    """Handle 405 errors."""
    return render_template('error/405.html', method=request.method), 405

@app.errorhandler(500)
def internal_server_error(error):
    """Handle 500 errors."""
    return render_template('error/500.html'), 500

@base.before_request
def lookup_current_user():
    """Set the g.user variable to the User in the database that shares
    openid with the session, if one exists.

    Note that it gets called before all requests, but not before decorators.
    """
    g.user = None
    if not app.config.get('GOOGLE_AUTH_ENABLED'):
        # bypass auth by mocking a super user
        session['gplus_id'] = SUPER_USER_GPLUS_ID
        try:
            g.user = User.objects.get(gplus_id=SUPER_USER_GPLUS_ID)
        except DoesNotExist:
            user = User(name='Super User',
                        gplus_id=SUPER_USER_GPLUS_ID,
                        user_type='admin',
                        email='email@email.com')
            user.save()

    if 'gplus_id' in session:
        gplus_id = session['gplus_id']
        try:
            g.user = User.objects().get(gplus_id=gplus_id)
        except DoesNotExist:
            pass  # Fail gracefully if the user is not in the database yet

@app.context_processor
def inject_user():
    """Injects a variable named ``current_user`` into all of the Jinja
    templates, so that it can be used at will.
    """
    return dict(current_user=g.user)

@base.after_request
def add_header(response):
    """
    Add headers to both force latest IE rendering engine or Chrome Frame,
    and also to cache the rendered page for 10 minutes.
    """
    response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
    response.headers['Cache-Control'] = 'public, max-age=0'
    return response
<commit_msg>unbreak things<commit_after>"""
.. module:: base
    :synopsis: All routes on the ``base`` Blueprint, as well as error handlers,
        before and after request handlers, and context processors.

.. moduleauthor:: Dan Schlosser <dan@danrs.ch>
"""

import sys
from flask import g, session, render_template, request, redirect, Blueprint
from mongoengine.queryset import DoesNotExist
import requests

from app import app
from app.models import User

SUPER_USER_GPLUS_ID = 'super'

base = Blueprint('base', __name__)

@app.errorhandler(Exception)
def exceptionHandler(error):
    """Handle uncaught exceptions."""
    app.logger.error("Uncaught Exception", exc_info=sys.exc_info())
    app.handle_exception(error) # default error handler

@app.errorhandler(400)
def bad_request(error):
    """Handle 400 errors."""
    return render_template('error/400.html'), 400

@app.errorhandler(401)
def not_authorized(error):
    """Handle 401 errors."""
    return render_template('error/401.html'), 401

@app.errorhandler(403)
def forbidden(error):
    """Handle 403 errors."""
    return render_template('error/403.html'), 403

@app.errorhandler(404)
def not_found(error):
    """Handle 404 errors."""
    old_site_url = 'http://adicu.github.com' + request.path
    try:
        response = requests.head(old_site_url, allow_redirects=True)
        if response.status_code == 200:
            return redirect(old_site_url)
    except requests.exceptions.ConnectionError:
        pass

    return render_template('error/404.html'), 404

@app.errorhandler(405)
def method_not_allowed(error):
    """Handle 405 errors."""
    return render_template('error/405.html', method=request.method), 405

@app.errorhandler(500)
def internal_server_error(error):
    """Handle 500 errors."""
    return render_template('error/500.html'), 500

@base.before_request
def lookup_current_user():
    """Set the g.user variable to the User in the database that shares
    openid with the session, if one exists.

    Note that it gets called before all requests, but not before decorators.
    """
    g.user = None
    if not app.config.get('GOOGLE_AUTH_ENABLED'):
        # bypass auth by mocking a super user
        session['gplus_id'] = SUPER_USER_GPLUS_ID
        try:
            g.user = User.objects.get(gplus_id=SUPER_USER_GPLUS_ID)
        except DoesNotExist:
            user = User(name='Super User',
                        gplus_id=SUPER_USER_GPLUS_ID,
                        user_type='admin',
                        email='email@email.com')
            user.save()

    if 'gplus_id' in session:
        gplus_id = session['gplus_id']
        try:
            g.user = User.objects().get(gplus_id=gplus_id)
        except DoesNotExist:
            pass  # Fail gracefully if the user is not in the database yet

@app.context_processor
def inject_user():
    """Injects a variable named ``current_user`` into all of the Jinja
    templates, so that it can be used at will.
    """
    if hasattr(g, 'user'):
        return dict(current_user=g.user)
    return dict(current_user=None)

@base.after_request
def add_header(response):
    """
    Add headers to both force latest IE rendering engine or Chrome Frame,
    and also to cache the rendered page for 10 minutes.
    """
    response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
    response.headers['Cache-Control'] = 'public, max-age=0'
    return response
<|endoftext|>