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

import subprocess
import sys
import os
import argparse
import json

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1) # line buffering
sys.path.insert(0, ".")

from prettify import color as pretty
import validate_test
import validate_all

startdir = os.getcwd()

test_categories = ['fs', 'hw', 'kernel', 'mod', 'net', 'performance', 'platform', 'stl', 'util']
test_types = ['integration', 'stress', 'unit', 'examples']

"""
Script used for running all the valid tests in the terminal.
"""

parser = argparse.ArgumentParser(
  description="IncludeOS testrunner. By default runs all the valid integration tests \
  found in subfolders, the stress test and all unit tests.")

parser.add_argument("-c", "--clean-all", dest="clean", action="store_true",
                    help="Run make clean before building test")

parser.add_argument("-s", "--skip", nargs="*", dest="skip", default=[],
                    help="Tests to skip. Valid names: 'unit' (all unit tests), \
                  'stress' (stresstest), 'integration' (all integration tests), examples \
                    or the name of a single integration test folder (leaf node name, e.g. 'udp') ")

parser.add_argument("-t", "--tests", nargs="*", dest="tests", default=[],
                    help="Tests to do. Valid names: see '--skip' ")

parser.add_argument("-f", "--fail-early", dest="fail", action="store_true",
                    help="Exit on first failed test")

args = parser.parse_args()

test_count = 0

def print_skipped(tests):
    for test in tests:
        if test.skip_:
            print pretty.WARNING("* Skipping " + test.name_)
            print "  Reason: {0:40}".format(test.skip_reason_)

def valid_tests(subfolder=None):
  """Returns a list of all the valid integration tests in */integration/*"""
  if "integration" in args.skip:
    return []

  skip_json = json.loads(open("skipped_tests.json").read())
  for skip in skip_json:
    print_skipped(skip["name"], skip["reason"])
    args.skip.append(skip["name"])

  print

  valid_tests = [ x for x in validate_all.valid_tests() if  not x in args.skip ]

  if subfolder:
    return [x for x in valid_tests if x.split('/')[0] == subfolder]

  if args.tests:
    return [x for x in valid_tests if x.split("/")[-1] in args.tests]

  return valid_tests

class Test:
  """ A class to start a test as a subprocess and pretty-print status """
  def __init__(self, path, clean=False, command=['sudo', '-E', 'python', 'test.py'], name=None):

    self.command_ = command
    self.proc_ = None
    self.path_ = path
    self.output_ = None

    # Extract category and type from the path variable
    # Category is linked to the top level folder e.g. net, fs, hw
    # Type is linked to the type of test e.g. integration, unit, stress
    if self.path_ == 'stress':
      self.category_ = 'stress'
      self.type_ = 'stress'
    elif self.path_ == 'examples':
      self.category_ = 'examples'
      self.type_ = 'examples'
    elif self.path_ == 'mod/gsl':
      self.category_ = 'mod'
      self.type_ = 'mod'
    elif self.path_ == '.':
      self.category_ = 'unit'
      self.type_ = 'unit'
    else:
      self.category_ = self.path_.split('/')[-3]
      self.type_ = self.path_.split('/')[-2]

    if not name:
      self.name_ = path
    else:
      self.name_ = name

    # Check if the test is valid or not
    self.check_valid()

    if clean:
      subprocess.check_output(["make","clean"])
      print pretty.C_GRAY + "\t Cleaned, now start... ", pretty.C_ENDC

  def __str__(self):
      """ Print output about the test object """

      return ('name_: {x[name_]} \n'
              'path_: {x[path_]} \n'
              'command_: {x[command_]} \n'
              'proc_: {x[proc_]} \n'
              'output_: {x[output_]} \n'
              'category_: {x[category_]} \n'
              'type_: {x[type_]} \n'
              'skip: {x[skip_]} \n'
              'skip_reason: {x[skip_reason_]} \n'
              ).format(x=self.__dict__)

  def start(self):
    os.chdir(startdir + "/" + self.path_)
    self.proc_ = subprocess.Popen(self.command_, shell=False,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
    os.chdir(startdir)
    return self

  def print_start(self):
    print "* {0:66} ".format(self.name_),
    sys.stdout.flush()

  def wait_status(self):

    self.print_start()

    # Start and wait for the process
    self.output_ = self.proc_.communicate()

    if self.proc_.returncode == 0:
      print pretty.PASS_INLINE()
    else:
      print pretty.FAIL_INLINE()
      print pretty.INFO("Process stdout")
      print pretty.DATA(self.output_[0])
      print pretty.INFO("Process stderr")
      print pretty.DATA(self.output_[1])

    return self.proc_.returncode

  def check_valid(self):
    """ Will check if a test is valid. The following points can declare a test valid:
        1. Contains the files required
        2. Not listed in the skipped_tests.json
        3. Not listed in the args.skip cmd line argument

    Arguments:
        self: Class function
    """
    # Test 1
    if not validate_test.validate_path(self.path_, verb = False):
        self.skip_ = True
        self.skip_reason_ = 'Failed validate_test, missing files'
        return

    # Test 2
    # Figure out if the test should be skipped
    skip_json = json.loads(open("skipped_tests.json").read())
    for skip in skip_json:
        if self.path_ == skip['name']:
            self.skip_ = True
            self.skip_reason_ = 'Defined in skipped_tests.json'
            return

    # Test 3
    if self.path_ in args.skip or self.category_ in args.skip:
            self.skip_ = True
            self.skip_reason_ = 'Defined by cmd line argument'
            return

    self.skip_ = False
    self.skip_reason_ = None
    return

def unit_tests():
  """Perform unit tests"""
  global test_count
  test_count += 1
  if ("unit" in args.skip):
    print pretty.WARNING("Unit tests skipped")
    return 0
  print pretty.HEADER("Building and running unit tests")
  build_status = Test(".", name="Unit test build", command=["make"], clean = args.clean).start().wait_status()
  unit_status = Test(".", name="Unit tests", command = ["./test.lest"]).start().wait_status()

  if (build_status or unit_status) and args.fail:
    print pretty.FAIL("Unit tests failed")
    sys.exit(max(build_status, unit_status))

  return max(build_status, unit_status)

def stress_test():
  """Perform stresstest"""
  global test_count
  test_count += 1
  if ("stress" in args.skip):
    print pretty.WARNING("Stress test skipped")
    return 0

  if (not validate_test.validate_path("stress")):
    raise Exception("Stress test failed validation")

  print pretty.HEADER("Starting stress test")
  stress = Test("stress", clean = args.clean).start()

  if (stress and args.fail):
    print pretty.FAIL("Stress test failed")
    sys.exit(stress)

  return 1 if stress.wait_status() else 0

def examples_working():
  global test_count
  if ("examples" in args.skip):
    print pretty.WARNING("Examples test skipped")
    return 0

  examples_dir = '../examples'
  dirs = os.walk(examples_dir).next()[1]
  print pretty.HEADER("Building " + str(len(dirs)) + " examples")
  test_count += len(dirs)
  fail_count = 0
  for directory in dirs:
    example = examples_dir + "/" + directory
    print "Building Example ", example
    build = Test(example, command = ["make"], name = directory + " build").start().wait_status()
    run = 0 #TODO: Make a 'test' folder for each example, containing test.py, vm.json etc.
    fail_count += 1 if build or run else 0
  return fail_count

def main():
  global test_count

  # Warned about skipped tests
  # @note : doesn't warn if you use '-t a b c ...' to run specific tests
  if args.skip:
    for skip in args.skip:
      print_skipped(skip, "marked skipped on command line")

  test_categories = ["integration", "examples", "unit", "stress"]
  if "integration" not in args.tests:
    test_folders = ["fs", "hw", "kernel", "net", "platform", "stl", "util"]
  else:
    test_folders = []
  tests_combined = test_categories + test_folders
  if args.tests:
    test_categories = [x for x in tests_combined if x in args.tests ]
  if args.skip:
    test_categories = [x for x in tests_combined if not x in args.skip]

  integration = integration_tests() if "integration" in test_categories else 0
  stress = stress_test() if "stress" in test_categories else 0
  unit = unit_tests() if "unit" in test_categories else 0
  examples = examples_working() if "examples" in test_categories else 0
  folders = integration_tests(subfolder=test_categories[0]) if test_categories[0] in test_folders else 0

  status = max(integration, stress, unit, examples, folders)

  if (not test_count):
    print "No tests selected"
    exit(0)

  if (status == 0):
    print pretty.SUCCESS(str(test_count - status) + " / " + str(test_count)
                         +  " tests passed, exiting with code 0")
  else:
    print pretty.FAIL(str(status) + " / " + str(test_count) + " tests failed ")

  sys.exit(status)

def integration_tests(tests):
    """ Function that runs the tests that are passed to it.
    Filters out any invalid tests before running

    Arguments:
        tests: List containing test objects to be run

    Returns:
        integer: Number of tests that failed
    """

    # Only run the valid tests
    tests = [ x for x in tests if not x.skip_ and x.type_ == 'integration' ]

    # Print info before starting to run
    print pretty.HEADER("Starting " + str(len(tests)) + " integration test(s)")
    for test in tests:
        print pretty.INFO("Test"), "starting", test.name_

    processes = []
    fail_count = 0
    global test_count
    test_count += len(tests)

    # Start running tests in parallell
    for test in tests:
        processes.append(test.start())

	# Collect test results
    print pretty.HEADER("Collecting integration test results")

    for p in processes:
        fail_count += 1 if p.wait_status() else 0

    # Exit early if any tests failed
    if fail_count and args.fail:
        print pretty.FAIL(str(fail_count) + "integration tests failed")
        sys.exit(fail_count)

    return fail_count

def find_leaf_nodes():
    """ Used to find all leaf nodes in the test directory,
    this is to help identify all possible test directories.
    Only looks in folders that actually store tests

    Returns:
        List: list of string with path to all leaf nodes
    """
    leaf_nodes = []

    for dirpath, dirnames, filenames in os.walk('.'):
        # Will now skip any path that is not defined as a test category
        # or ends with unit or integration -> no tests in those folders were
        # created
        if dirpath[2:].split('/')[0] in test_categories and dirpath.split('/')[-1] not in ['unit', 'integration']:
            if not dirnames:
                leaf_nodes.append(dirpath[2:])

    return leaf_nodes

def main():
    # Find leaf nodes
    leaves = find_leaf_nodes()

    # Populate test objects
    all_tests = [ Test(path) for path in leaves ]

    # Figure out which tests are to be run
    test_categories_to_run = []
    test_types_to_run = []
    if args.tests:
        for argument in args.tests:
            if argument in test_categories and argument not in args.skip:
                test_categories_to_run.append(argument)
            elif argument in test_types and argument not in args.skip:
                test_types_to_run.append(argument)
            else:
                print 'Test specified is not recognised, exiting'
                sys.exit(1)
    else:
        test_types_to_run = test_types

    if test_categories_to_run:
        # This means that a specific category has been requested
        specific_tests = [ test for test in all_tests if test.category_ in test_categories_to_run ]

        # Print which tests are skipped
        print_skipped(specific_tests)

        # Run the tests
        integration = integration_tests(specific_tests)
    else:
        # Print which tests are skipped
        print_skipped(all_tests)

        # Run the tests
        integration = integration_tests(all_tests) if "integration" in test_types_to_run else 0

    stress = stress_test() if "stress" in test_types_to_run else 0
    unit = unit_tests() if "unit" in test_types_to_run else 0
    examples = examples_working() if "examples" in test_types_to_run else 0

    status = max(integration, stress, unit, examples)
    if (status == 0):
        print pretty.SUCCESS(str(test_count - status) + " / " + str(test_count)
                            +  " tests passed, exiting with code 0")
    else:
        print pretty.FAIL(str(status) + " / " + str(test_count) + " tests failed ")

    sys.exit(status)

if  __name__ == '__main__':
    main2()
<commit_msg>Test.py: removed now unused functions<commit_after>#!/usr/bin/env python

import subprocess
import sys
import os
import argparse
import json

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1) # line buffering
sys.path.insert(0, ".")

from prettify import color as pretty
import validate_test
import validate_all

startdir = os.getcwd()

test_categories = ['fs', 'hw', 'kernel', 'mod', 'net', 'performance', 'platform', 'stl', 'util']
test_types = ['integration', 'stress', 'unit', 'examples']

"""
Script used for running all the valid tests in the terminal.
"""

parser = argparse.ArgumentParser(
  description="IncludeOS testrunner. By default runs all the valid integration tests \
  found in subfolders, the stress test and all unit tests.")

parser.add_argument("-c", "--clean-all", dest="clean", action="store_true",
                    help="Run make clean before building test")

parser.add_argument("-s", "--skip", nargs="*", dest="skip", default=[],
                    help="Tests to skip. Valid names: 'unit' (all unit tests), \
                  'stress' (stresstest), 'integration' (all integration tests), examples \
                    or the name of a single integration test folder (leaf node name, e.g. 'udp') ")

parser.add_argument("-t", "--tests", nargs="*", dest="tests", default=[],
                    help="Tests to do. Valid names: see '--skip' ")

parser.add_argument("-f", "--fail-early", dest="fail", action="store_true",
                    help="Exit on first failed test")

args = parser.parse_args()

test_count = 0

def print_skipped(tests):
    for test in tests:
        if test.skip_:
            print pretty.WARNING("* Skipping " + test.name_)
            print "  Reason: {0:40}".format(test.skip_reason_)

class Test:
  """ A class to start a test as a subprocess and pretty-print status """
  def __init__(self, path, clean=False, command=['sudo', '-E', 'python', 'test.py'], name=None):

    self.command_ = command
    self.proc_ = None
    self.path_ = path
    self.output_ = None

    # Extract category and type from the path variable
    # Category is linked to the top level folder e.g. net, fs, hw
    # Type is linked to the type of test e.g. integration, unit, stress
    if self.path_ == 'stress':
      self.category_ = 'stress'
      self.type_ = 'stress'
    elif self.path_ == 'examples':
      self.category_ = 'examples'
      self.type_ = 'examples'
    elif self.path_ == 'mod/gsl':
      self.category_ = 'mod'
      self.type_ = 'mod'
    elif self.path_ == '.':
      self.category_ = 'unit'
      self.type_ = 'unit'
    else:
      self.category_ = self.path_.split('/')[-3]
      self.type_ = self.path_.split('/')[-2]

    if not name:
      self.name_ = path
    else:
      self.name_ = name

    # Check if the test is valid or not
    self.check_valid()

    if clean:
      subprocess.check_output(["make","clean"])
      print pretty.C_GRAY + "\t Cleaned, now start... ", pretty.C_ENDC

  def __str__(self):
      """ Print output about the test object """

      return ('name_: {x[name_]} \n'
              'path_: {x[path_]} \n'
              'command_: {x[command_]} \n'
              'proc_: {x[proc_]} \n'
              'output_: {x[output_]} \n'
              'category_: {x[category_]} \n'
              'type_: {x[type_]} \n'
              'skip: {x[skip_]} \n'
              'skip_reason: {x[skip_reason_]} \n'
              ).format(x=self.__dict__)

  def start(self):
    os.chdir(startdir + "/" + self.path_)
    self.proc_ = subprocess.Popen(self.command_, shell=False,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
    os.chdir(startdir)
    return self

  def print_start(self):
    print "* {0:66} ".format(self.name_),
    sys.stdout.flush()

  def wait_status(self):

    self.print_start()

    # Start and wait for the process
    self.output_ = self.proc_.communicate()

    if self.proc_.returncode == 0:
      print pretty.PASS_INLINE()
    else:
      print pretty.FAIL_INLINE()
      print pretty.INFO("Process stdout")
      print pretty.DATA(self.output_[0])
      print pretty.INFO("Process stderr")
      print pretty.DATA(self.output_[1])

    return self.proc_.returncode

  def check_valid(self):
    """ Will check if a test is valid. The following points can declare a test valid:
        1. Contains the files required
        2. Not listed in the skipped_tests.json
        3. Not listed in the args.skip cmd line argument

    Arguments:
        self: Class function
    """
    # Test 1
    if not validate_test.validate_path(self.path_, verb = False):
        self.skip_ = True
        self.skip_reason_ = 'Failed validate_test, missing files'
        return

    # Test 2
    # Figure out if the test should be skipped
    skip_json = json.loads(open("skipped_tests.json").read())
    for skip in skip_json:
        if self.path_ == skip['name']:
            self.skip_ = True
            self.skip_reason_ = 'Defined in skipped_tests.json'
            return

    # Test 3
    if self.path_ in args.skip or self.category_ in args.skip:
            self.skip_ = True
            self.skip_reason_ = 'Defined by cmd line argument'
            return

    self.skip_ = False
    self.skip_reason_ = None
    return

def unit_tests():
  """Perform unit tests"""
  global test_count
  test_count += 1
  if ("unit" in args.skip):
    print pretty.WARNING("Unit tests skipped")
    return 0
  print pretty.HEADER("Building and running unit tests")
  build_status = Test(".", name="Unit test build", command=["make"], clean = args.clean).start().wait_status()
  unit_status = Test(".", name="Unit tests", command = ["./test.lest"]).start().wait_status()

  if (build_status or unit_status) and args.fail:
    print pretty.FAIL("Unit tests failed")
    sys.exit(max(build_status, unit_status))

  return max(build_status, unit_status)

def stress_test():
  """Perform stresstest"""
  global test_count
  test_count += 1
  if ("stress" in args.skip):
    print pretty.WARNING("Stress test skipped")
    return 0

  if (not validate_test.validate_path("stress")):
    raise Exception("Stress test failed validation")

  print pretty.HEADER("Starting stress test")
  stress = Test("stress", clean = args.clean).start()

  if (stress and args.fail):
    print pretty.FAIL("Stress test failed")
    sys.exit(stress)

  return 1 if stress.wait_status() else 0

def examples_working():
  global test_count
  if ("examples" in args.skip):
    print pretty.WARNING("Examples test skipped")
    return 0

  examples_dir = '../examples'
  dirs = os.walk(examples_dir).next()[1]
  print pretty.HEADER("Building " + str(len(dirs)) + " examples")
  test_count += len(dirs)
  fail_count = 0
  for directory in dirs:
    example = examples_dir + "/" + directory
    print "Building Example ", example
    build = Test(example, command = ["make"], name = directory + " build").start().wait_status()
    run = 0 #TODO: Make a 'test' folder for each example, containing test.py, vm.json etc.
    fail_count += 1 if build or run else 0
  return fail_count

def integration_tests(tests):
    """ Function that runs the tests that are passed to it.
    Filters out any invalid tests before running

    Arguments:
        tests: List containing test objects to be run

    Returns:
        integer: Number of tests that failed
    """

    # Only run the valid tests
    tests = [ x for x in tests if not x.skip_ and x.type_ == 'integration' ]

    # Print info before starting to run
    print pretty.HEADER("Starting " + str(len(tests)) + " integration test(s)")
    for test in tests:
        print pretty.INFO("Test"), "starting", test.name_

    processes = []
    fail_count = 0
    global test_count
    test_count += len(tests)

    # Start running tests in parallell
    for test in tests:
        processes.append(test.start())

	# Collect test results
    print pretty.HEADER("Collecting integration test results")

    for p in processes:
        fail_count += 1 if p.wait_status() else 0

    # Exit early if any tests failed
    if fail_count and args.fail:
        print pretty.FAIL(str(fail_count) + "integration tests failed")
        sys.exit(fail_count)

    return fail_count

def find_leaf_nodes():
    """ Used to find all leaf nodes in the test directory,
    this is to help identify all possible test directories.
    Only looks in folders that actually store tests

    Returns:
        List: list of string with path to all leaf nodes
    """
    leaf_nodes = []

    for dirpath, dirnames, filenames in os.walk('.'):
        # Will now skip any path that is not defined as a test category
        # or ends with unit or integration -> no tests in those folders were
        # created
        if dirpath[2:].split('/')[0] in test_categories and dirpath.split('/')[-1] not in ['unit', 'integration']:
            if not dirnames:
                leaf_nodes.append(dirpath[2:])

    return leaf_nodes

def main():
    # Find leaf nodes
    leaves = find_leaf_nodes()

    # Populate test objects
    all_tests = [ Test(path) for path in leaves ]

    # Figure out which tests are to be run
    test_categories_to_run = []
    test_types_to_run = []
    if args.tests:
        for argument in args.tests:
            if argument in test_categories and argument not in args.skip:
                test_categories_to_run.append(argument)
            elif argument in test_types and argument not in args.skip:
                test_types_to_run.append(argument)
            else:
                print 'Test specified is not recognised, exiting'
                sys.exit(1)
    else:
        test_types_to_run = test_types

    if test_categories_to_run:
        # This means that a specific category has been requested
        specific_tests = [ test for test in all_tests if test.category_ in test_categories_to_run ]

        # Print which tests are skipped
        print_skipped(specific_tests)

        # Run the tests
        integration = integration_tests(specific_tests)
    else:
        # Print which tests are skipped
        print_skipped(all_tests)

        # Run the tests
        integration = integration_tests(all_tests) if "integration" in test_types_to_run else 0

    stress = stress_test() if "stress" in test_types_to_run else 0
    unit = unit_tests() if "unit" in test_types_to_run else 0
    examples = examples_working() if "examples" in test_types_to_run else 0

    status = max(integration, stress, unit, examples)
    if (status == 0):
        print pretty.SUCCESS(str(test_count - status) + " / " + str(test_count)
                            +  " tests passed, exiting with code 0")
    else:
        print pretty.FAIL(str(status) + " / " + str(test_count) + " tests failed ")

    sys.exit(status)

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


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

import os
import subprocess

def execute(cmd):
    try:
        retcode = subprocess.call(cmd.split(' '))
        if retcode < 0:
            raise RuntimeError, "Child was terminated by signal: %d" % (-retcode,)
        else:
            raise RuntimeError, "Child returned retcode: %d" % (retcode,)
    except OSError as exc:
        raise RuntimeError, "Execution failed: %s" % exc
    return True

if not os.path.exists("examples"):
    os.mkdir("examples")
for template in ("mini", "mini_8fin", "mini_8fin_spin", "std", "d", "e"):
    cmd = "atlas -t %s -o examples/%s -p -s -x" % (template, template)
    execute(cmd)
<commit_msg>updated test code<commit_after>#!/usr/bin/env python

import os
import sys
import subprocess

def execute(cmd):
    try:
        retcode = subprocess.call(cmd.split(' '))
        if retcode < 0:
            raise RuntimeError, "Child was terminated by signal: %d" % (-retcode,)
        else:
            raise RuntimeError, "Child returned retcode: %d" % (retcode,)
    except OSError as exc:
        raise RuntimeError, "Execution failed: %s" % exc
    return True

if not os.path.exists("examples"):
    os.mkdir("examples")
for template in ("mini", "mini_8fin", "mini_8fin_spin", "std", "d", "e"):
    cmd = "atlas -t %s -o examples/%s -p -s -x" % (template, template)
    try:
        execute(cmd)
    except RuntimeError, err:
        sys.stderr.write(str(err))
        sys.stderr.flush()
<|endoftext|>


<commit_before>""" Contains methods to emulate LightSweeper floors on your computer """

import lsfloor
import Colors

import sys
import time
import types

import pygame
from pygame.locals import *

class Move():
    def __init__(self, row, col, val):
        self.row = row
        self.col = col
        self.val = val

wait = time.sleep

class LSEmulateFloor(lsfloor.LSFloor):

    def heartbeat(self):
        """
            This function gets called at the end of each heartbeat. Use this
            to update your display.
        """
        pass

    def pollEvents(self):
        """
            This method gets called before every heartbeat. Trigger your touch
            events here. Should return a list of tiles that were activated.
        """
        pass

def loadImage(self):
    image = pygame.image.load("images/segments.png")
    horizontal = (42,10)
    vertical = (10,30)
    segMap = [(29,10),(71,17),(71,52),(29,79),(19,52),(19,17),(29,45)]
    image.fill(Colors.intToRGB(self.segments["a"]), pygame.Rect(segMap[0],horizontal))
    image.fill(Colors.intToRGB(self.segments["b"]), pygame.Rect(segMap[1],vertical))
    image.fill(Colors.intToRGB(self.segments["c"]), pygame.Rect(segMap[2],vertical))
    image.fill(Colors.intToRGB(self.segments["d"]), pygame.Rect(segMap[3],horizontal))
    image.fill(Colors.intToRGB(self.segments["e"]), pygame.Rect(segMap[4],vertical))
    image.fill(Colors.intToRGB(self.segments["f"]), pygame.Rect(segMap[5],vertical))
    image.fill(Colors.intToRGB(self.segments["g"]), pygame.Rect(segMap[6],horizontal))
    return image

# Tweaks LSFloor to update pygame emulator
class LSPygameFloor(LSEmulateFloor):

    def _initEmulator(self):
        print("Using Pygame emulator...")
        width=self.cols*100
        height=self.rows*100
        print("Making the screen ({:d}x{:d})".format(width,height))
        pygame.init()
        self.screen = pygame.display.set_mode((width, height))
        for tile in self.tileList:
            tile.loadImage = types.MethodType(loadImage, tile) # Bind the loadImage function to each tile

    def heartbeat(self):
        #gets the images from the individual tiles, blits them in succession
        #print("heartbeat drawing floor")
        background = pygame.Surface(((self.cols*100), (self.rows*100)))
        background.fill(Colors.BLACK)
        self.screen.blit(background, (0,0))
        for r in range(self.rows):
            for c in range(self.cols):
                print("update {:d}.{:d}".format(r,c))
                tile = self.tiles[r][c]
                image = tile.loadImage()
                self.screen.blit(image, (100 * c, 100 * r))
        pygame.display.update()
        super().heartbeat()

 #   def pollEvents(self):
    def pollSensors(self):
        print("Pollevents")
        sensorsChanged = []
        reading = 1
        for event in pygame.event.get():
            rowCol = self._whereDidIPutMyMouse(pygame.mouse.get_pos())
            if event.type == QUIT:
                sys.exit()
            if event.type == KEYDOWN and event.key == K_ESCAPE:
                sys.exit()
            if event.type == MOUSEBUTTONUP:
                print("Clicked off {:d},{:d} ({:d})".format(rowCol[0], rowCol[1],reading)) # Debugging
            if event.type == MOUSEBUTTONDOWN:
                print("Clicked on {:d},{:d} ({:d})".format(rowCol[0], rowCol[1],reading)) # Debugging
                move = Move(rowCol[0], rowCol[1], reading)
                sensorsChanged.append(move)
                self.handleTileStepEvent(rowCol[0], rowCol[1], reading)
        return sensorsChanged

    def _whereDidIPutMyMouse(self, mousePointer):
        (x, y) = mousePointer
        col = int(x/100)
        row = int(y/100)
        return (row,col)
<commit_msg>I dunno<commit_after>""" Contains methods to emulate LightSweeper floors on your computer """

import lsfloor
import Colors

import sys
import time
import types

import pygame
from pygame.locals import *

class Move():
    def __init__(self, row, col, val):
        self.row = row
        self.col = col
        self.val = val

wait = time.sleep

class LSEmulateFloor(lsfloor.LSFloor):

    def heartbeat(self):
        """
            This function gets called at the end of each heartbeat. Use this
            to update your display.
        """
        pass

    def pollEvents(self):
        """
            This method gets called before every heartbeat. Trigger your touch
            events here. Should return a list of tiles that were activated.
        """
        pass

def loadImage(self):
    image = pygame.image.load("images/segments.png")
    horizontal = (42,10)
    vertical = (10,30)
    segMap = [(29,10),(71,17),(71,52),(29,79),(19,52),(19,17),(29,45)]
    image.fill(Colors.intToRGB(self.segments["a"]), pygame.Rect(segMap[0],horizontal))
    image.fill(Colors.intToRGB(self.segments["b"]), pygame.Rect(segMap[1],vertical))
    image.fill(Colors.intToRGB(self.segments["c"]), pygame.Rect(segMap[2],vertical))
    image.fill(Colors.intToRGB(self.segments["d"]), pygame.Rect(segMap[3],horizontal))
    image.fill(Colors.intToRGB(self.segments["e"]), pygame.Rect(segMap[4],vertical))
    image.fill(Colors.intToRGB(self.segments["f"]), pygame.Rect(segMap[5],vertical))
    image.fill(Colors.intToRGB(self.segments["g"]), pygame.Rect(segMap[6],horizontal))
    return image

# Tweaks LSFloor to update pygame emulator
class LSPygameFloor(LSEmulateFloor):

    def _initEmulator(self):
        print("Using Pygame emulator...")
        width=self.cols*100
        height=self.rows*100
        print("Making the screen ({:d}x{:d})".format(width,height))
        pygame.init()
        self.screen = pygame.display.set_mode((width, height))
        for tile in self.tileList:
            tile.loadImage = types.MethodType(loadImage, tile) # Bind the loadImage function to each tile

    def heartbeat(self):
        #gets the images from the individual tiles, blits them in succession
        #print("heartbeat drawing floor")
        background = pygame.Surface(((self.cols*100), (self.rows*100)))
        background.fill(Colors.BLACK)
        self.screen.blit(background, (0,0))
        for r in range(self.rows):
            for c in range(self.cols):
                tile = self.tiles[r][c]
                image = tile.loadImage()
                self.screen.blit(image, (100 * c, 100 * r))
        pygame.display.update()
        super().heartbeat()

 #   def pollEvents(self):
    def pollSensors(self):
        print("Pollevents")
        sensorsChanged = []
        reading = 1
        for event in pygame.event.get():
            rowCol = self._whereDidIPutMyMouse(pygame.mouse.get_pos())
            if event.type == QUIT:
                sys.exit()
            if event.type == KEYDOWN and event.key == K_ESCAPE:
                sys.exit()
            if event.type == MOUSEBUTTONUP:
                print("Clicked off {:d},{:d} ({:d})".format(rowCol[0], rowCol[1],reading)) # Debugging
            if event.type == MOUSEBUTTONDOWN:
                print("Clicked on {:d},{:d} ({:d})".format(rowCol[0], rowCol[1],reading)) # Debugging
                move = Move(rowCol[0], rowCol[1], reading)
                sensorsChanged.append(move)
                self.handleTileStepEvent(rowCol[0], rowCol[1], reading)
        return sensorsChanged

    def _whereDidIPutMyMouse(self, mousePointer):
        (x, y) = mousePointer
        col = int(x/100)
        row = int(y/100)
        return (row,col)
<|endoftext|>


<commit_before>#! /usr/bin/python3
# -*- coding: utf-8 -*-
''' mailmerge.py

Accept a CSV and an email template
The first line of the CSV are fields to substitute into the email template.

TODO: The program is currently hard-coded to relay through gmail instead of sending direct. At some point it should take that information as arguments.
'''

import sys
import email
import time

def prepare_email_message(template, value_dict):
    '''
    Take the email template and build an Email Message object.
    Substitute the value_dict for keys in the message.
    '''
    new_template="" # Needs to be a string to build the message
    for line in template:
        newline = line
        for key, value in value_dict.items():
            newline=newline.replace("#{0}#".format(key), value)
        new_template=new_template + newline
    prepared_message = email.message_from_string(new_template)
    return prepared_message

def send_emails(template, values_dict_array):
    '''
    Send a personalized email for each entry in values_dict_array.
    Substitute into the template the values in the dict

    template: an array of strings containing the lines in the email.
      First lines are (in order):
        From
        To
        Subject
      Remaining lines make up the body.
      At any time a value of #keyname# will be substituted with the matching
      value from the value_dict.

    value_dict_array: Each row contains values that match keys used in the email.
    '''
    # TODO: This is hard coded to go through GMail.
    # smtplib doesn't support the with statement until 3.3
    #with SMTP_SSL(host="smtp.gmail.com", port="465") as smtp:
    from smtplib import SMTP_SSL
    import smtplib
    send_count=0

    smtp_host="smtp.gmail.com"
    smtp_port="465"
    smtp_login=""
    smtp_pass=""

    try:
        smtp=SMTP_SSL(host=smtp_host, port=smtp_port)
        smtp.login(smtp_login, smtp_pass)

        for values_dict in values_dict_array:
            email_message = prepare_email_message(template, values_dict)
            to_addr = [t[1] for t in email_message.items() if t[0]=='To'][0]
            print("Sending to {0}".format(to_addr))
            try:
                email_message.set_charset('utf-8')
                smtp.send_message(email_message)
            except smtplib.SMTPServerDisconnected:
                print("Reconnecting to SMTP Server")
                smtp=SMTP_SSL(host=smtp_host, port=smtp_port)
                smtp.login(smtp_login, smtp_pass)
                smtp.send_message(email_message)
            except smtplib.SMTPSenderRefused:
                print("Pausing for Google rate-limit")
                time.sleep(180)
                print("Reconnecting to SMTP Server")
                smtp=SMTP_SSL(host=smtp_host, port=smtp_port)
                smtp.login(smtp_login, smtp_pass)
                smtp.send_message(email_message)
            send_count+=1
    finally:
        smtp.quit()
    print("Emails sent: {0}".format(send_count))

def main(arg_list):
    # Setup argument parsing
    import argparse
    parser = argparse.ArgumentParser(description="Send a personalized email to each person in a CSV.")
    parser.add_argument("csv_filename",
                        help="Filename of the CSV file defining the key / value "
                             "pairs. One row per email. First row lists the "
                             "keys, each additional row lists the values.")
    parser.add_argument("email_template_filename",
                        help="Text file containing the email. #keyname# will "
                             "be substituted for values in the CSV. Required "
                             "lines: 'From:', 'To:', and 'Subject:'")
    args=parser.parse_args(arg_list)

    # Read the email template
    with open(args.email_template_filename, encoding='utf-8-sig') \
            as email_template_file:
        email_template = email_template_file.readlines()

    # Read the CSV
    import csv
    with open(args.csv_filename) as csvfile:
        dialect = csv.Sniffer().sniff(csvfile.read(4098))
        csvfile.seek(0)
        email_dict_array = csv.DictReader(csvfile, dialect=dialect)
        # Send the emails, substituting the variables for the placeholders
        send_emails(email_template, email_dict_array)

    return(0)

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:])) #Arg list needs to drop program name

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
<commit_msg>Add explanation about with statement<commit_after>#! /usr/bin/python3
# -*- coding: utf-8 -*-
''' mailmerge.py

Accept a CSV and an email template
The first line of the CSV are fields to substitute into the email template.

TODO: The program is currently hard-coded to relay through gmail instead of sending direct. At some point it should take that information as arguments.
'''

import sys
import email
import time

def prepare_email_message(template, value_dict):
    '''
    Take the email template and build an Email Message object.
    Substitute the value_dict for keys in the message.
    '''
    new_template="" # Needs to be a string to build the message
    for line in template:
        newline = line
        for key, value in value_dict.items():
            newline=newline.replace("#{0}#".format(key), value)
        new_template=new_template + newline
    prepared_message = email.message_from_string(new_template)
    return prepared_message

def send_emails(template, values_dict_array):
    '''
    Send a personalized email for each entry in values_dict_array.
    Substitute into the template the values in the dict

    template: an array of strings containing the lines in the email.
      First lines are (in order):
        From
        To
        Subject
      Remaining lines make up the body.
      At any time a value of #keyname# will be substituted with the matching
      value from the value_dict.

    value_dict_array: Each row contains values that match keys used in the email.
    '''
    from smtplib import SMTP_SSL
    import smtplib
    send_count=0

    # TODO: This is hard coded to go through GMail.
    smtp_host="smtp.gmail.com"
    smtp_port="465"
    smtp_login=""
    smtp_pass=""

    try:
        # Can't use SMTP "with" statement, or loop will break on disconnect
        smtp=SMTP_SSL(host=smtp_host, port=smtp_port)
        smtp.login(smtp_login, smtp_pass)

        for values_dict in values_dict_array:
            email_message = prepare_email_message(template, values_dict)
            to_addr = [t[1] for t in email_message.items() if t[0]=='To'][0]
            print("Sending to {0}".format(to_addr))
            try:
                email_message.set_charset('utf-8')
                smtp.send_message(email_message)
            except smtplib.SMTPServerDisconnected:
                print("Reconnecting to SMTP Server")
                smtp=SMTP_SSL(host=smtp_host, port=smtp_port)
                smtp.login(smtp_login, smtp_pass)
                smtp.send_message(email_message)
            except smtplib.SMTPSenderRefused:
                print("Pausing for Google rate-limit")
                time.sleep(180)
                print("Reconnecting to SMTP Server")
                smtp=SMTP_SSL(host=smtp_host, port=smtp_port)
                smtp.login(smtp_login, smtp_pass)
                smtp.send_message(email_message)
            send_count+=1
    finally:
        smtp.quit()
    print("Emails sent: {0}".format(send_count))

def main(arg_list):
    # Setup argument parsing
    import argparse
    parser = argparse.ArgumentParser(description="Send a personalized email to each person in a CSV.")
    parser.add_argument("csv_filename",
                        help="Filename of the CSV file defining the key / value "
                             "pairs. One row per email. First row lists the "
                             "keys, each additional row lists the values.")
    parser.add_argument("email_template_filename",
                        help="Text file containing the email. #keyname# will "
                             "be substituted for values in the CSV. Required "
                             "lines: 'From:', 'To:', and 'Subject:'")
    args=parser.parse_args(arg_list)

    # Read the email template
    with open(args.email_template_filename, encoding='utf-8-sig') \
            as email_template_file:
        email_template = email_template_file.readlines()

    # Read the CSV
    import csv
    with open(args.csv_filename) as csvfile:
        dialect = csv.Sniffer().sniff(csvfile.read(4098))
        csvfile.seek(0)
        email_dict_array = csv.DictReader(csvfile, dialect=dialect)
        # Send the emails, substituting the variables for the placeholders
        send_emails(email_template, email_dict_array)

    return(0)

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:])) #Arg list needs to drop program name

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
<|endoftext|>


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

import functools
import re

from babel import localedata
from flask.ext import login
from flask.ext import oauth
from flask.ext.babel import gettext as __
from google.appengine.api import users
from google.appengine.ext import ndb
import flask

import config
import model
import util

from main import app
from main import babel

###############################################################################
# Babel stuff - i18n
###############################################################################
def check_locale(locale):
  locale = locale.lower()
  if locale not in config.LOCALE:
    locale = config.LOCALE_DEFAULT
  return locale if localedata.exists(locale) else 'en'

@babel.localeselector
def get_locale():
  locale = flask.session.pop('locale', None)
  if not locale:
    locale = flask.request.cookies.get('locale', config.LOCALE_DEFAULT)
  return check_locale(locale)

@flask.request_started.connect_via(app)
def request_started(sender, **extra):
  flask.request.locale = get_locale()
  flask.request.locale_html = get_locale().replace('_', '-')

@app.route('/l/<path:locale>/')
def set_locale(locale):
  response = flask.redirect(util.get_next_url())
  util.set_locale(check_locale(locale), response)
  return response

###############################################################################
# Flask Login
###############################################################################
login_manager = login.LoginManager()

class AnonymousUser(login.AnonymousUserMixin):
  id = 0
  admin = False
  name = 'Anonymous'
  user_db = None

  def key(self):
    return None

login_manager.anonymous_user = AnonymousUser

class FlaskUser(AnonymousUser):
  def __init__(self, user_db):
    self.user_db = user_db
    self.id = user_db.key.id()
    self.name = user_db.name
    self.admin = user_db.admin

  def key(self):
    return self.user_db.key.urlsafe()

  def get_id(self):
    return self.user_db.key.urlsafe()

  def is_authenticated(self):
    return True

  def is_active(self):
    return self.user_db.active

  def is_anonymous(self):
    return False

@login_manager.user_loader
def load_user(key):
  user_db = ndb.Key(urlsafe=key).get()
  if user_db:
    return FlaskUser(user_db)
  return None

login_manager.init_app(app)

def current_user_id():
  return login.current_user.id

def current_user_key():
  return login.current_user.user_db.key if login.current_user.user_db else None

def current_user_db():
  return login.current_user.user_db

def is_logged_in():
  return login.current_user.id != 0

###############################################################################
# Decorators
###############################################################################
def login_required(f):
  @functools.wraps(f)
  def decorated_function(*args, **kws):
    if is_logged_in():
      return f(*args, **kws)
    if flask.request.path.startswith('/_s/'):
      return flask.abort(401)
    return flask.redirect(flask.url_for('signin', next=flask.request.url))
  return decorated_function

def admin_required(f):
  @functools.wraps(f)
  def decorated_function(*args, **kws):
    if is_logged_in() and current_user_db().admin:
      return f(*args, **kws)
    if not is_logged_in() and flask.request.path.startswith('/_s/'):
      return flask.abort(401)
    if not is_logged_in():
      return flask.redirect(flask.url_for('signin', next=flask.request.url))
    return flask.abort(403)
  return decorated_function

###############################################################################
# Sign in stuff
###############################################################################
@app.route('/login/')
@app.route('/signin/')
def signin():
  next_url = util.get_next_url()
  if flask.url_for('signin') in next_url:
    next_url = flask.url_for('welcome')

  google_signin_url = flask.url_for('signin_google', next=next_url)
  twitter_signin_url = flask.url_for('signin_twitter', next=next_url)
  facebook_signin_url = flask.url_for('signin_facebook', next=next_url)

  return flask.render_template(
      'signin.html',
      title=__('Please sign in'),
      html_class='signin',
      google_signin_url=google_signin_url,
      twitter_signin_url=twitter_signin_url,
      facebook_signin_url=facebook_signin_url,
      next_url=next_url,
    )

@app.route('/signout/')
def signout():
  login.logout_user()
  flask.flash(__('You have been signed out.'), category='success')
  return flask.redirect(flask.url_for('welcome'))

###############################################################################
# Google
###############################################################################
@app.route('/signin/google/')
def signin_google():
  google_url = users.create_login_url(
      flask.url_for('google_authorized', next=util.get_next_url())
    )
  return flask.redirect(google_url)

@app.route('/_s/callback/google/authorized/')
def google_authorized():
  google_user = users.get_current_user()
  if google_user is None:
    flask.flash(__('You denied the request to sign in.'))
    return flask.redirect(util.get_next_url())

  user_db = retrieve_user_from_google(google_user)
  return signin_user_db(user_db)

def retrieve_user_from_google(google_user):
  auth_id = 'federated_%s' % google_user.user_id()
  user_db = model.User.retrieve_one_by('auth_ids', auth_id)
  if user_db:
    if not user_db.admin and users.is_current_user_admin():
      user_db.admin = True
      user_db.put()
    return user_db

  return create_user_db(
      auth_id,
      re.sub(r'_+|-+|\.+', ' ', google_user.email().split('@')[0]).title(),
      google_user.email(),
      google_user.email(),
      admin=users.is_current_user_admin(),
    )

###############################################################################
# Twitter
###############################################################################
twitter_oauth = oauth.OAuth()

twitter = twitter_oauth.remote_app(
    'twitter',
    base_url='https://api.twitter.com/1.1/',
    request_token_url='https://api.twitter.com/oauth/request_token',
    access_token_url='https://api.twitter.com/oauth/access_token',
    authorize_url='https://api.twitter.com/oauth/authorize',
    consumer_key=config.CONFIG_DB.twitter_consumer_key,
    consumer_secret=config.CONFIG_DB.twitter_consumer_secret,
  )

@app.route('/_s/callback/twitter/oauth-authorized/')
@twitter.authorized_handler
def twitter_authorized(resp):
  if resp is None:
    flask.flash(__('You denied the request to sign in.'))
    return flask.redirect(util.get_next_url())

  flask.session['oauth_token'] = (
    resp['oauth_token'],
    resp['oauth_token_secret']
  )
  user_db = retrieve_user_from_twitter(resp)
  return signin_user_db(user_db)

@twitter.tokengetter
def get_twitter_token():
  return flask.session.get('oauth_token')

@app.route('/signin/twitter/')
def signin_twitter():
  flask.session.pop('oauth_token', None)
  try:
    return twitter.authorize(
        callback=flask.url_for('twitter_authorized',
        next=util.get_next_url()),
      )
  except:
    flask.flash(
        __('Something went wrong with Twitter sign in. Please try again.'),
        category='danger',
      )
    return flask.redirect(flask.url_for('signin', next=util.get_next_url()))

def retrieve_user_from_twitter(response):
  auth_id = 'twitter_%s' % response['user_id']
  user_db = model.User.retrieve_one_by('auth_ids', auth_id)
  if user_db:
    return user_db

  return create_user_db(
      auth_id,
      response['screen_name'],
      response['screen_name'],
    )

###############################################################################
# Facebook
###############################################################################
facebook_oauth = oauth.OAuth()

facebook = facebook_oauth.remote_app(
    'facebook',
    base_url='https://graph.facebook.com/',
    request_token_url=None,
    access_token_url='/oauth/access_token',
    authorize_url='https://www.facebook.com/dialog/oauth',
    consumer_key=config.CONFIG_DB.facebook_app_id,
    consumer_secret=config.CONFIG_DB.facebook_app_secret,
    request_token_params={'scope': 'email'},
  )

@app.route('/_s/callback/facebook/oauth-authorized/')
@facebook.authorized_handler
def facebook_authorized(resp):
  if resp is None:
    return _('Access denied: reason=%(reason)s error=%(error)s',
      reason=flask.request.args['error_reason'],
      error=flask.request.args['error_description']
    )
  flask.session['oauth_token'] = (resp['access_token'], '')
  me = facebook.get('/me')
  user_db = retrieve_user_from_facebook(me.data)
  return signin_user_db(user_db)

@facebook.tokengetter
def get_facebook_oauth_token():
  return flask.session.get('oauth_token')

@app.route('/signin/facebook/')
def signin_facebook():
  return facebook.authorize(callback=flask.url_for('facebook_authorized',
      next=util.get_next_url(),
      _external=True),
    )

def retrieve_user_from_facebook(response):
  auth_id = 'facebook_%s' % response['id']
  user_db = model.User.retrieve_one_by('auth_ids', auth_id)
  if user_db:
    return user_db
  return create_user_db(
      auth_id,
      response['name'],
      response['username'] if 'username' in response else response['id'],
      response['email'],
    )

###############################################################################
# Helpers
###############################################################################
def create_user_db(auth_id, name, username, email='', **params):
  username = re.sub(r'_+|-+|\s+', '.', username.split('@')[0].lower().strip())
  new_username = username
  n = 1
  while model.User.retrieve_one_by('username', new_username) is not None:
    new_username = '%s%d' % (username, n)
    n += 1

  user_db = model.User(
      name=name,
      email=email.lower(),
      username=new_username,
      auth_ids=[auth_id],
      locale=get_locale(),
      **params
    )
  user_db.put()
  return user_db

@ndb.toplevel
def signin_user_db(user_db):
  if not user_db:
    return flask.redirect(flask.url_for('signin'))
  flask_user_db = FlaskUser(user_db)
  if login.login_user(flask_user_db):
    user_db.put_async()
    response = flask.redirect(util.get_next_url())
    util.set_locale(user_db.locale, response)
    flask.flash(__(
        'Hello %(name)s, welcome to %(brand)s.',
        name=user_db.name, brand=config.CONFIG_DB.brand_name,
      ), category='success')
    return response
  else:
    flask.flash(__('Sorry, but you could not sign in.'), category='danger')
    return flask.redirect(flask.url_for('signin'))
<commit_msg>Inform user when Facebook access denied<commit_after># -*- coding: utf-8 -*-

import functools
import re

from babel import localedata
from flask.ext import login
from flask.ext import oauth
from flask.ext.babel import gettext as __
from google.appengine.api import users
from google.appengine.ext import ndb
import flask

import config
import model
import util

from main import app
from main import babel

###############################################################################
# Babel stuff - i18n
###############################################################################
def check_locale(locale):
  locale = locale.lower()
  if locale not in config.LOCALE:
    locale = config.LOCALE_DEFAULT
  return locale if localedata.exists(locale) else 'en'

@babel.localeselector
def get_locale():
  locale = flask.session.pop('locale', None)
  if not locale:
    locale = flask.request.cookies.get('locale', config.LOCALE_DEFAULT)
  return check_locale(locale)

@flask.request_started.connect_via(app)
def request_started(sender, **extra):
  flask.request.locale = get_locale()
  flask.request.locale_html = get_locale().replace('_', '-')

@app.route('/l/<path:locale>/')
def set_locale(locale):
  response = flask.redirect(util.get_next_url())
  util.set_locale(check_locale(locale), response)
  return response

###############################################################################
# Flask Login
###############################################################################
login_manager = login.LoginManager()

class AnonymousUser(login.AnonymousUserMixin):
  id = 0
  admin = False
  name = 'Anonymous'
  user_db = None

  def key(self):
    return None

login_manager.anonymous_user = AnonymousUser

class FlaskUser(AnonymousUser):
  def __init__(self, user_db):
    self.user_db = user_db
    self.id = user_db.key.id()
    self.name = user_db.name
    self.admin = user_db.admin

  def key(self):
    return self.user_db.key.urlsafe()

  def get_id(self):
    return self.user_db.key.urlsafe()

  def is_authenticated(self):
    return True

  def is_active(self):
    return self.user_db.active

  def is_anonymous(self):
    return False

@login_manager.user_loader
def load_user(key):
  user_db = ndb.Key(urlsafe=key).get()
  if user_db:
    return FlaskUser(user_db)
  return None

login_manager.init_app(app)

def current_user_id():
  return login.current_user.id

def current_user_key():
  return login.current_user.user_db.key if login.current_user.user_db else None

def current_user_db():
  return login.current_user.user_db

def is_logged_in():
  return login.current_user.id != 0

###############################################################################
# Decorators
###############################################################################
def login_required(f):
  @functools.wraps(f)
  def decorated_function(*args, **kws):
    if is_logged_in():
      return f(*args, **kws)
    if flask.request.path.startswith('/_s/'):
      return flask.abort(401)
    return flask.redirect(flask.url_for('signin', next=flask.request.url))
  return decorated_function

def admin_required(f):
  @functools.wraps(f)
  def decorated_function(*args, **kws):
    if is_logged_in() and current_user_db().admin:
      return f(*args, **kws)
    if not is_logged_in() and flask.request.path.startswith('/_s/'):
      return flask.abort(401)
    if not is_logged_in():
      return flask.redirect(flask.url_for('signin', next=flask.request.url))
    return flask.abort(403)
  return decorated_function

###############################################################################
# Sign in stuff
###############################################################################
@app.route('/login/')
@app.route('/signin/')
def signin():
  next_url = util.get_next_url()
  if flask.url_for('signin') in next_url:
    next_url = flask.url_for('welcome')

  google_signin_url = flask.url_for('signin_google', next=next_url)
  twitter_signin_url = flask.url_for('signin_twitter', next=next_url)
  facebook_signin_url = flask.url_for('signin_facebook', next=next_url)

  return flask.render_template(
      'signin.html',
      title=__('Please sign in'),
      html_class='signin',
      google_signin_url=google_signin_url,
      twitter_signin_url=twitter_signin_url,
      facebook_signin_url=facebook_signin_url,
      next_url=next_url,
    )

@app.route('/signout/')
def signout():
  login.logout_user()
  flask.flash(__('You have been signed out.'), category='success')
  return flask.redirect(flask.url_for('welcome'))

###############################################################################
# Google
###############################################################################
@app.route('/signin/google/')
def signin_google():
  google_url = users.create_login_url(
      flask.url_for('google_authorized', next=util.get_next_url())
    )
  return flask.redirect(google_url)

@app.route('/_s/callback/google/authorized/')
def google_authorized():
  google_user = users.get_current_user()
  if google_user is None:
    flask.flash(__('You denied the request to sign in.'))
    return flask.redirect(util.get_next_url())

  user_db = retrieve_user_from_google(google_user)
  return signin_user_db(user_db)

def retrieve_user_from_google(google_user):
  auth_id = 'federated_%s' % google_user.user_id()
  user_db = model.User.retrieve_one_by('auth_ids', auth_id)
  if user_db:
    if not user_db.admin and users.is_current_user_admin():
      user_db.admin = True
      user_db.put()
    return user_db

  return create_user_db(
      auth_id,
      re.sub(r'_+|-+|\.+', ' ', google_user.email().split('@')[0]).title(),
      google_user.email(),
      google_user.email(),
      admin=users.is_current_user_admin(),
    )

###############################################################################
# Twitter
###############################################################################
twitter_oauth = oauth.OAuth()

twitter = twitter_oauth.remote_app(
    'twitter',
    base_url='https://api.twitter.com/1.1/',
    request_token_url='https://api.twitter.com/oauth/request_token',
    access_token_url='https://api.twitter.com/oauth/access_token',
    authorize_url='https://api.twitter.com/oauth/authorize',
    consumer_key=config.CONFIG_DB.twitter_consumer_key,
    consumer_secret=config.CONFIG_DB.twitter_consumer_secret,
  )

@app.route('/_s/callback/twitter/oauth-authorized/')
@twitter.authorized_handler
def twitter_authorized(resp):
  if resp is None:
    flask.flash(__('You denied the request to sign in.'))
    return flask.redirect(util.get_next_url())

  flask.session['oauth_token'] = (
    resp['oauth_token'],
    resp['oauth_token_secret']
  )
  user_db = retrieve_user_from_twitter(resp)
  return signin_user_db(user_db)

@twitter.tokengetter
def get_twitter_token():
  return flask.session.get('oauth_token')

@app.route('/signin/twitter/')
def signin_twitter():
  flask.session.pop('oauth_token', None)
  try:
    return twitter.authorize(
        callback=flask.url_for('twitter_authorized',
        next=util.get_next_url()),
      )
  except:
    flask.flash(
        __('Something went wrong with Twitter sign in. Please try again.'),
        category='danger',
      )
    return flask.redirect(flask.url_for('signin', next=util.get_next_url()))

def retrieve_user_from_twitter(response):
  auth_id = 'twitter_%s' % response['user_id']
  user_db = model.User.retrieve_one_by('auth_ids', auth_id)
  if user_db:
    return user_db

  return create_user_db(
      auth_id,
      response['screen_name'],
      response['screen_name'],
    )

###############################################################################
# Facebook
###############################################################################
facebook_oauth = oauth.OAuth()

facebook = facebook_oauth.remote_app(
    'facebook',
    base_url='https://graph.facebook.com/',
    request_token_url=None,
    access_token_url='/oauth/access_token',
    authorize_url='https://www.facebook.com/dialog/oauth',
    consumer_key=config.CONFIG_DB.facebook_app_id,
    consumer_secret=config.CONFIG_DB.facebook_app_secret,
    request_token_params={'scope': 'email'},
  )

@app.route('/_s/callback/facebook/oauth-authorized/')
@facebook.authorized_handler
def facebook_authorized(resp):
  if resp is None:
    flask.flash(__('You denied the request to sign in.'))
    return flask.redirect(util.get_next_url())

  flask.session['oauth_token'] = (resp['access_token'], '')
  me = facebook.get('/me')
  user_db = retrieve_user_from_facebook(me.data)
  return signin_user_db(user_db)

@facebook.tokengetter
def get_facebook_oauth_token():
  return flask.session.get('oauth_token')

@app.route('/signin/facebook/')
def signin_facebook():
  return facebook.authorize(callback=flask.url_for('facebook_authorized',
      next=util.get_next_url(),
      _external=True),
    )

def retrieve_user_from_facebook(response):
  auth_id = 'facebook_%s' % response['id']
  user_db = model.User.retrieve_one_by('auth_ids', auth_id)
  if user_db:
    return user_db
  return create_user_db(
      auth_id,
      response['name'],
      response['username'] if 'username' in response else response['id'],
      response['email'],
    )

###############################################################################
# Helpers
###############################################################################
def create_user_db(auth_id, name, username, email='', **params):
  username = re.sub(r'_+|-+|\s+', '.', username.split('@')[0].lower().strip())
  new_username = username
  n = 1
  while model.User.retrieve_one_by('username', new_username) is not None:
    new_username = '%s%d' % (username, n)
    n += 1

  user_db = model.User(
      name=name,
      email=email.lower(),
      username=new_username,
      auth_ids=[auth_id],
      locale=get_locale(),
      **params
    )
  user_db.put()
  return user_db

@ndb.toplevel
def signin_user_db(user_db):
  if not user_db:
    return flask.redirect(flask.url_for('signin'))
  flask_user_db = FlaskUser(user_db)
  if login.login_user(flask_user_db):
    user_db.put_async()
    response = flask.redirect(util.get_next_url())
    util.set_locale(user_db.locale, response)
    flask.flash(__(
        'Hello %(name)s, welcome to %(brand)s.',
        name=user_db.name, brand=config.CONFIG_DB.brand_name,
      ), category='success')
    return response
  else:
    flask.flash(__('Sorry, but you could not sign in.'), category='danger')
    return flask.redirect(flask.url_for('signin'))
<|endoftext|>


<commit_before>buildings = {'shelter': 0,
             'shed': 0,
             'corral': 0,
             '': 0
             }<commit_msg>Locations<commit_after>buildings = {'shelter': 0,
             'shed': 0,
             'corral': 0,
             'workshop': 0,
             'outpost': 0
             }

locations = {'Volga banks': {'urban': False, 'difficulty': 1, 'wood': 50, 'food': 30, 'freshwater': 50,
                             'dirtywater': 0, 'polluted': False},
             'Pine forest': {'urban': False, 'difficulty': 1, 'wood': 75, 'food': 25, 'freshwater': 5,
                             'dirtywater': 0, 'polluted': False},
             'Belka E': {'urban': False, 'difficulty': 2, 'wood': 50, 'food': 20, 'freshwater': 5,
                         'dirtywater': 2, 'polluted': False},
             'Belka W': {'urban': False, 'difficulty': 3, 'wood': 40, 'food': 15, 'freshwater': 5,
                         'dirtywater': 5, 'polluted': False},
             'Blue hills E': {'urban': False, 'difficulty': 3, 'wood': 20, 'food': 10, 'freshwater': 0,
                              'dirtywater': 10, 'polluted': False},
             'Blue hills W': {'urban': False, 'difficulty': 4, 'wood': 20, 'food': 0, 'freshwater': 0,
                              'dirtywater': 20, 'polluted': False},
             'Lenin bridge': {'urban': True, 'difficulty': 5, 'wood': 0, 'food': 5, 'freshwater': 10,
                              'dirtywater': 30, 'polluted': True},
             'Park Svobodi': {'urban': True, 'difficulty': 4, 'wood': 0, 'food': 15, 'freshwater': 15,
                              'dirtywater': 60, 'polluted': True},
             'Zhukov industrial estate': {'urban': True, 'difficulty': 5, 'wood': 50, 'food': 0, 'freshwater': 0,
                                          'dirtywater': 90, 'polluted': True}

}
<|endoftext|>


<commit_before>import sys
if not ('lib.zip' in sys.path):
  sys.path.insert(0, 'lib.zip')

import flask
from flaskext import wtf
import config

app = flask.Flask(__name__)
app.config.from_object(config)

from google.appengine.api import mail

import auth
import util
import model
import admin

@app.route('/')
def welcome():
  return flask.render_template(
      'welcome.html',
      html_class='welcome',
      channel_name='welcome',
    )

################################################################################
# Profile stuff
################################################################################
class ProfileUpdateForm(wtf.Form):
  name = wtf.TextField('Name', [wtf.validators.required()])
  email = wtf.TextField('Email', [
      wtf.validators.optional(),
      wtf.validators.email("That doesn't look like an email"),
    ])

@app.route('/_s/profile/', endpoint='profile_service')
@app.route('/profile/', methods=['GET', 'POST'], endpoint='profile')
@auth.login_required
def profile():
  form = ProfileUpdateForm()
  user_db = auth.current_user_db()
  if form.validate_on_submit():
    user_db.name = form.name.data
    user_db.email = form.email.data.lower()
    user_db.put()
    return flask.redirect(flask.url_for('welcome'))
  if not form.errors:
    form.name.data = user_db.name
    form.email.data = user_db.email or ''

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_db(user_db)

  return flask.render_template(
      'profile.html',
      title='Profile',
      html_class='profile',
      form=form,
      user_db=user_db,
    )

################################################################################
# Feedback
################################################################################
class FeedbackForm(wtf.Form):
  subject = wtf.TextField('Subject', [wtf.validators.required()])
  message = wtf.TextAreaField('Message', [wtf.validators.required()])
  email = wtf.TextField('Email (optional)', [
      wtf.validators.optional(),
      wtf.validators.email("That doesn't look like an email"),
    ])

@app.route('/feedback/', methods=['GET', 'POST'])
def feedback():
  form = FeedbackForm()
  if form.validate_on_submit():
    mail.send_mail(
        sender=config.CONFIG_DB.feedback_email,
        to=config.CONFIG_DB.feedback_email,
        subject='[%s] %s' % (
            config.CONFIG_DB.brand_name,
            form.subject.data,
          ),
        reply_to=form.email.data or config.CONFIG_DB.feedback_email,
        body='%s\n\n%s' % (form.message.data, form.email.data)
      )
    flask.flash('Thank you for your feedback!', category='success')
    return flask.redirect(flask.url_for('welcome'))
  if not form.errors and auth.current_user_id() > 0:
    form.email.data = auth.current_user_db().email

  return flask.render_template(
      'feedback.html',
      title='Feedback',
      html_class='feedback',
      form=form,
    )

################################################################################
# User Stuff
################################################################################
@app.route('/_s/user/', endpoint='user_list_service')
@app.route('/user/', endpoint='user_list')
def user_list():
  user_dbs, more_cursor = util.retrieve_dbs(
      model.User,
      model.User.query(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order'),
      name=util.param('name'),
    )

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(user_dbs, more_cursor)

  return flask.render_template(
      'user_list.html',
      html_class='user',
      title='User List',
      user_dbs=user_dbs,
      more_url=util.generate_more_url(more_cursor),
    )

################################################################################
# Extras
################################################################################
@app.route('/_s/extras/', endpoint='extras_service')
@app.route('/extras/', endpoint='extras')
def extras():
  country = None
  region = None
  city = None
  city_lat_long = None
  if 'X-AppEngine-Country' in flask.request.headers:
    country = flask.request.headers['X-AppEngine-Country']
  if 'X-AppEngine-Region' in flask.request.headers:
    region = flask.request.headers['X-AppEngine-Region']
  if 'X-AppEngine-City' in flask.request.headers:
    city = flask.request.headers['X-AppEngine-City']
  if 'X-AppEngine-CityLatLong' in flask.request.headers:
    city_lat_long = flask.request.headers['X-AppEngine-CityLatLong']

  extra_info = {
    'country': country,
    'region': region,
    'city': city,
    'city_lat_long': city_lat_long,
    'user_agent': flask.request.headers['User-Agent'],
  }

  if flask.request.path.startswith('/_s/'):
    return flask.jsonify(extra_info)

  return flask.render_template(
      'extras.html',
      html_class='extras',
      title='Extras',
      extra_info=extra_info,
    )

@app.route('/chat/')
def chat():
  return flask.render_template(
      'chat.html',
      title='Chat',
      html_class='chat',
      channel_name='chat',
    )

################################################################################
# Error Handling
################################################################################
@app.errorhandler(400)
@app.errorhandler(401)
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(410)
@app.errorhandler(418)
@app.errorhandler(500)
def error_handler(e):
  try:
    e.code
  except:
    class e(object):
      code = 500
      name = 'Internal Server Error'

  if flask.request.path.startswith('/_s/'):
    return flask.jsonify({
        'status': 'error',
        'error_code': e.code,
        'error_name': e.name.lower().replace(' ', '_'),
        'error_message': e.name,
      })

  return flask.render_template(
      'error.html',
      title='%s!!1' % (e.name),
      html_class='error-page',
      error=e,
    ), e.code
<commit_msg>Removed the check for including the lib.zip<commit_after>import sys
sys.path.insert(0, 'lib.zip')

import flask
from flaskext import wtf
import config

app = flask.Flask(__name__)
app.config.from_object(config)

from google.appengine.api import mail

import auth
import util
import model
import admin

@app.route('/')
def welcome():
  return flask.render_template(
      'welcome.html',
      html_class='welcome',
      channel_name='welcome',
    )

################################################################################
# Profile stuff
################################################################################
class ProfileUpdateForm(wtf.Form):
  name = wtf.TextField('Name', [wtf.validators.required()])
  email = wtf.TextField('Email', [
      wtf.validators.optional(),
      wtf.validators.email("That doesn't look like an email"),
    ])

@app.route('/_s/profile/', endpoint='profile_service')
@app.route('/profile/', methods=['GET', 'POST'], endpoint='profile')
@auth.login_required
def profile():
  form = ProfileUpdateForm()
  user_db = auth.current_user_db()
  if form.validate_on_submit():
    user_db.name = form.name.data
    user_db.email = form.email.data.lower()
    user_db.put()
    return flask.redirect(flask.url_for('welcome'))
  if not form.errors:
    form.name.data = user_db.name
    form.email.data = user_db.email or ''

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_db(user_db)

  return flask.render_template(
      'profile.html',
      title='Profile',
      html_class='profile',
      form=form,
      user_db=user_db,
    )

################################################################################
# Feedback
################################################################################
class FeedbackForm(wtf.Form):
  subject = wtf.TextField('Subject', [wtf.validators.required()])
  message = wtf.TextAreaField('Message', [wtf.validators.required()])
  email = wtf.TextField('Email (optional)', [
      wtf.validators.optional(),
      wtf.validators.email("That doesn't look like an email"),
    ])

@app.route('/feedback/', methods=['GET', 'POST'])
def feedback():
  form = FeedbackForm()
  if form.validate_on_submit():
    mail.send_mail(
        sender=config.CONFIG_DB.feedback_email,
        to=config.CONFIG_DB.feedback_email,
        subject='[%s] %s' % (
            config.CONFIG_DB.brand_name,
            form.subject.data,
          ),
        reply_to=form.email.data or config.CONFIG_DB.feedback_email,
        body='%s\n\n%s' % (form.message.data, form.email.data)
      )
    flask.flash('Thank you for your feedback!', category='success')
    return flask.redirect(flask.url_for('welcome'))
  if not form.errors and auth.current_user_id() > 0:
    form.email.data = auth.current_user_db().email

  return flask.render_template(
      'feedback.html',
      title='Feedback',
      html_class='feedback',
      form=form,
    )

################################################################################
# User Stuff
################################################################################
@app.route('/_s/user/', endpoint='user_list_service')
@app.route('/user/', endpoint='user_list')
def user_list():
  user_dbs, more_cursor = util.retrieve_dbs(
      model.User,
      model.User.query(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order'),
      name=util.param('name'),
    )

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(user_dbs, more_cursor)

  return flask.render_template(
      'user_list.html',
      html_class='user',
      title='User List',
      user_dbs=user_dbs,
      more_url=util.generate_more_url(more_cursor),
    )

################################################################################
# Extras
################################################################################
@app.route('/_s/extras/', endpoint='extras_service')
@app.route('/extras/', endpoint='extras')
def extras():
  country = None
  region = None
  city = None
  city_lat_long = None
  if 'X-AppEngine-Country' in flask.request.headers:
    country = flask.request.headers['X-AppEngine-Country']
  if 'X-AppEngine-Region' in flask.request.headers:
    region = flask.request.headers['X-AppEngine-Region']
  if 'X-AppEngine-City' in flask.request.headers:
    city = flask.request.headers['X-AppEngine-City']
  if 'X-AppEngine-CityLatLong' in flask.request.headers:
    city_lat_long = flask.request.headers['X-AppEngine-CityLatLong']

  extra_info = {
    'country': country,
    'region': region,
    'city': city,
    'city_lat_long': city_lat_long,
    'user_agent': flask.request.headers['User-Agent'],
  }

  if flask.request.path.startswith('/_s/'):
    return flask.jsonify(extra_info)

  return flask.render_template(
      'extras.html',
      html_class='extras',
      title='Extras',
      extra_info=extra_info,
    )

@app.route('/chat/')
def chat():
  return flask.render_template(
      'chat.html',
      title='Chat',
      html_class='chat',
      channel_name='chat',
    )

################################################################################
# Error Handling
################################################################################
@app.errorhandler(400)
@app.errorhandler(401)
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(410)
@app.errorhandler(418)
@app.errorhandler(500)
def error_handler(e):
  try:
    e.code
  except:
    class e(object):
      code = 500
      name = 'Internal Server Error'

  if flask.request.path.startswith('/_s/'):
    return flask.jsonify({
        'status': 'error',
        'error_code': e.code,
        'error_name': e.name.lower().replace(' ', '_'),
        'error_message': e.name,
      })

  return flask.render_template(
      'error.html',
      title='%s!!1' % (e.name),
      html_class='error-page',
      error=e,
    ), e.code
<|endoftext|>


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

import logging

from flask.ext import wtf
from google.appengine.ext import ndb
import flask

import auth
import model
import util

from main import app

###############################################################################
# User List
###############################################################################
@app.route('/_s/user/', endpoint='user_list_service')
@app.route('/user/')
@auth.admin_required
def user_list():
  user_dbs, more_cursor = util.retrieve_dbs(
      model.User.query(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order') or '-created',
      name=util.param('name'),
      admin=util.param('admin', bool),
    )

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(user_dbs, more_cursor)

  return flask.render_template(
      'user/user_list.html',
      html_class='user',
      title='User List',
      user_dbs=user_dbs,
      more_url=util.generate_more_url(more_cursor),
      has_json=True,
    )

###############################################################################
# User Update
###############################################################################
class UserUpdateForm(wtf.Form):
  username = wtf.StringField(
      'Username',
      [wtf.validators.required(), wtf.validators.length(min=4)], filters=[util.email_filter],
    )
  name = wtf.StringField('Name',
      [wtf.validators.required()], filters=[util.strip_filter],
    )
  email = wtf.StringField('Email',
      [wtf.validators.optional(), wtf.validators.email()],
      filters=[util.email_filter],
    )

@app.route('/user/<int:user_id>/update/', methods=['GET', 'POST'])
@auth.admin_required
def user_update(user_id):
  user_db = model.User.get_by_id(user_id)
  if not user_db:
    flask.abort(404)

  form = UserUpdateForm(obj=user_db)
  if form.validate_on_submit():
    if not util.is_valid_username(form.username.data):
      form.username.errors.append('This username is invalid.')
    elif not is_username_available(form.username.data, user_db):
      form.username.errors.append('This username is taken.')
    else:
      form.populate_obj(user_db)
      user_db.put()
      return flask.redirect(flask.url_for('user_list', order='-modified'))

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_db(user_db)

  return flask.render_template(
      'user/user_update.html',
      title=user_db.name,
      html_class='user-update',
      form=form,
      user_db=user_db,
    )

###############################################################################
# User Delete
###############################################################################
@app.route('/_s/user/delete/', methods=['DELETE'])
@auth.admin_required
def user_delete_service():
  user_keys = util.param('user_keys', list)
  user_db_keys = [ndb.Key(urlsafe=k) for k in user_keys]
  delete_user_dbs(user_db_keys)
  return flask.jsonify({
      'result': user_keys,
      'status': 'success',
    })

@ndb.transactional(xg=True)
def delete_user_dbs(user_db_keys):
  ndb.delete_multi(user_db_keys)

###############################################################################
# Helpers
###############################################################################
def is_username_available(username, self_db=None):
  user_dbs, more_cursor = util.retrieve_dbs(
      model.User.query(),
      username=username,
      limit=2,
    )
  c = len(user_dbs)
  return not (c == 2 or (c == 1 and self_db and self_db.key != user_dbs[0].key))
<commit_msg>Changed the minimum username length to 3<commit_after># -*- coding: utf-8 -*-

import logging

from flask.ext import wtf
from google.appengine.ext import ndb
import flask

import auth
import model
import util

from main import app

###############################################################################
# User List
###############################################################################
@app.route('/_s/user/', endpoint='user_list_service')
@app.route('/user/')
@auth.admin_required
def user_list():
  user_dbs, more_cursor = util.retrieve_dbs(
      model.User.query(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order') or '-created',
      name=util.param('name'),
      admin=util.param('admin', bool),
    )

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(user_dbs, more_cursor)

  return flask.render_template(
      'user/user_list.html',
      html_class='user',
      title='User List',
      user_dbs=user_dbs,
      more_url=util.generate_more_url(more_cursor),
      has_json=True,
    )

###############################################################################
# User Update
###############################################################################
class UserUpdateForm(wtf.Form):
  username = wtf.StringField('Username',
      [wtf.validators.required(), wtf.validators.length(min=3)],
      filters=[util.email_filter],
    )
  name = wtf.StringField('Name',
      [wtf.validators.required()], filters=[util.strip_filter],
    )
  email = wtf.StringField('Email',
      [wtf.validators.optional(), wtf.validators.email()],
      filters=[util.email_filter],
    )

@app.route('/user/<int:user_id>/update/', methods=['GET', 'POST'])
@auth.admin_required
def user_update(user_id):
  user_db = model.User.get_by_id(user_id)
  if not user_db:
    flask.abort(404)

  form = UserUpdateForm(obj=user_db)
  if form.validate_on_submit():
    if not util.is_valid_username(form.username.data):
      form.username.errors.append('This username is invalid.')
    elif not is_username_available(form.username.data, user_db):
      form.username.errors.append('This username is taken.')
    else:
      form.populate_obj(user_db)
      user_db.put()
      return flask.redirect(flask.url_for('user_list', order='-modified'))

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_db(user_db)

  return flask.render_template(
      'user/user_update.html',
      title=user_db.name,
      html_class='user-update',
      form=form,
      user_db=user_db,
    )

###############################################################################
# User Delete
###############################################################################
@app.route('/_s/user/delete/', methods=['DELETE'])
@auth.admin_required
def user_delete_service():
  user_keys = util.param('user_keys', list)
  user_db_keys = [ndb.Key(urlsafe=k) for k in user_keys]
  delete_user_dbs(user_db_keys)
  return flask.jsonify({
      'result': user_keys,
      'status': 'success',
    })

@ndb.transactional(xg=True)
def delete_user_dbs(user_db_keys):
  ndb.delete_multi(user_db_keys)

###############################################################################
# Helpers
###############################################################################
def is_username_available(username, self_db=None):
  user_dbs, more_cursor = util.retrieve_dbs(
      model.User.query(),
      username=username,
      limit=2,
    )
  c = len(user_dbs)
  return not (c == 2 or (c == 1 and self_db and self_db.key != user_dbs[0].key))
<|endoftext|>


<commit_before>if __name__ == "__main__":

    # Initialize grammar
    from parglare.grammar import Grammar, NonTerminal, Terminal, Production, \
        ProductionRHS
    from parglare.parser import Parser

    g = Grammar()

    E, T, F = [NonTerminal(name) for name in ['E', 'T', 'F']]
    PLUS, MULT, ID, OPEN, CLOSE = [
        Terminal(value, value) for value in ['+', '*', 'id', '(', ')']]
    production = [
        (E, (E, PLUS, T)),
        (E, (T, )),
        (T, (T, MULT, F)),
        (T, (F, )),
        (F, (OPEN, E, CLOSE)),
        (F, (ID,))
    ]
    for p in production:
        g.productions.append(Production(p[0], ProductionRHS(p[1])))

    g.init_grammar()
    g.print_debug()

    # create parser
    parser = Parser(g, root_symbol=E)

    parser.parse("id+id*id")
<commit_msg>Removing old test file.<commit_after><|endoftext|>


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

from flask import Blueprint, jsonify, g, request, Response, url_for
from flask.views import MethodView
from .domain import EntityNotFoundError

def return_or_catch(f):
    def wrapper(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except EntityNotFoundError as e:
            return jsonify(status_code=404, message=e.msg), 404

    return wrapper

class MeasurementList(MethodView):
    def get(self):
        service = g.sl('measurement_service')
        return jsonify(items=service.find_all())

    def post(self):
        service = g.sl('measurement_service')
        id = service.create(**request.form.to_dict())
        location = url_for('api.measurement_item', id=id)
        response = Response(location, status=201, mimetype='text/plain')
        response.headers['Location'] = location
        return response

class MeasurementItem(MethodView):
    @return_or_catch
    def get(self, id):
        service = g.sl('measurement_service')
        return jsonify(service.find(id))

    @return_or_catch
    def put(self, id):
        service = g.sl('measurement_service')
        service.update(id, **request.form.to_dict())
        return '', 204

    @return_or_catch
    def delete(self, id):
        service = g.sl('measurement_service')
        service.delete(id)
        return '', 204

bp = Blueprint('api', __name__)

bp.add_url_rule(
    '/measurements/',
    view_func=MeasurementList.as_view('measurement_list'),
    methods=['GET', 'POST']
)

bp.add_url_rule(
    '/measurements/<id>',
    view_func=MeasurementItem.as_view('measurement_item'),
    methods=['GET', 'PUT', 'DELETE']
)
<commit_msg>Jsonify the response of put and delete methods and add a status code.<commit_after># -*- coding: utf-8 -*-

from flask import Blueprint, jsonify, g, request, Response, url_for
from flask.views import MethodView
from .domain import EntityNotFoundError

def return_or_catch(f):
    def wrapper(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except EntityNotFoundError as e:
            return jsonify(status_code=404, message=e.msg), 404

    return wrapper

class MeasurementList(MethodView):
    def get(self):
        service = g.sl('measurement_service')
        return jsonify(items=service.find_all())

    def post(self):
        service = g.sl('measurement_service')
        id = service.create(**request.form.to_dict())
        location = url_for('api.measurement_item', id=id)
        response = Response(location, status=201, mimetype='text/plain')
        response.headers['Location'] = location
        return response

class MeasurementItem(MethodView):
    @return_or_catch
    def get(self, id):
        service = g.sl('measurement_service')
        return jsonify(service.find(id))

    @return_or_catch
    def put(self, id):
        service = g.sl('measurement_service')
        service.update(id, **request.form.to_dict())
        return jsonify(status_code=204), 204

    @return_or_catch
    def delete(self, id):
        service = g.sl('measurement_service')
        service.delete(id)
        return jsonify(status_code=204), 204

bp = Blueprint('api', __name__)

bp.add_url_rule(
    '/measurements/',
    view_func=MeasurementList.as_view('measurement_list'),
    methods=['GET', 'POST']
)

bp.add_url_rule(
    '/measurements/<id>',
    view_func=MeasurementItem.as_view('measurement_item'),
    methods=['GET', 'PUT', 'DELETE']
)
<|endoftext|>


<commit_before>import argparse
import glob
import logging
import numpy as np
import os
import pprint
import subprocess
import tempfile

from project import convert_raw2nii
from tools.nii_info import read_nii

def main(test_data_folder, test_folder, rtol, atol):
    """
        Runs convert_raw2nii on a folder containing PARREC test data and
        compares the NIFTI output
    """
    logger = logging.getLogger('raw2nii_test')
    test_data_folder = os.path.abspath(test_data_folder)
    path_par = os.path.join(test_data_folder, test_folder, "PARREC")
    input_par = glob.glob("{0}/*.PAR".format(path_par))[0]
    out_folder = os.path.join("testOutput", test_folder, "NIFTI")
    #Set up convert_raw2nii logger
    raw2nii_logger = logging.getLogger('raw2nii')
    #raw2nii_logger.setLevel(logging.DEBUG)
    _formatter = logging.Formatter('test_data: %(levelname)s: %(message)s')
    _stream_handler = logging.StreamHandler()
    _stream_handler.setFormatter(_formatter)
    raw2nii_logger.addHandler(_stream_handler)
    #Run NIFTI conversion function and output to testOutput directory
    output = convert_raw2nii(filelist=[input_par], prefix="", suffix="",
        pathpar=path_par, outfolder=out_folder, outputformat=1, angulation=1,
        rescale=1, dti_revertb0=0)
    #Compare to the 4D data since we have the Vanderbilt override behavior
    test_nifti_folder = os.path.join(test_data_folder, test_folder, "NIFTI",
        "4D")
    #Produce listing of each file in NIFTI folders in lexographical order and
    #prepare each pair for comparison
    test_niftis = sorted(glob.glob("{0}/*.nii".format(test_nifti_folder)))
    output_niftis = sorted(glob.glob("{0}/*.nii".format(out_folder)))
    if len(test_niftis) != len(output_niftis):
        logger.error('Different number of output files! Test niftis: {0} '
            'Output niftis: {1}'.format(pprint.pformat(test_niftis),
            pprint.pformat(output_niftis)))
    output_pairs = zip(test_niftis, output_niftis)
    for test_nifti, output_nifti in output_pairs:
        logger.info("Output pair: {0} -> {1}".format(test_nifti, output_nifti))
        test_header, test_body = read_nii(test_nifti)
        output_header, output_body = read_nii(output_nifti)
        #First compare the headers of the two niftis
        for test_key, test_val in test_header.items():
            output_val = output_header[test_key]
            #logger.debug("Compare header.{0} : {1} -> {2}".format(
            #    test_key, test_val, output_val))
            if isinstance(test_val, np.ndarray):
                is_different = not np.allclose(test_val, output_val, rtol, atol)
            elif isinstance(test_val, float):
                is_different = abs(test_val - output_val) > (atol + rtol *
                    abs(output_val))
            else:
                is_different = (test_val != output_val)
            if is_different:
                logger.warning("header.{0} are different: {1} -> {2}".format(
                    test_key, test_val, output_val))
        #Then compare the bodies of the niftis
        if test_body['dtype'] != output_body['dtype']:
            logger.warning("Body dtypes are different: {0} -> {1}".format(
                test_body['dtype'], output_body['dtype']))
        elif test_body['data'].shape != output_body['data'].shape:
            logger.warning("Body shapes are different: {0} -> {1}".format(
                test_body['dtype'].shape, output_body['dtype'].shape))
        elif not np.allclose(test_body['data'], output_body['data'], rtol,
                atol):
            diff_output, err_output = body_diff_script(logger, test_body,
                output_body)
            logger.warning("Body data is different: <{0}> <{1}>".format(
                diff_output, err_output))
    return 0

def body_diff_script(logger, test_body, output_body):
    #Write out test binary data to a tempfile
    test_bin_fd, test_bin_name = tempfile.mkstemp()
    with os.fdopen(test_bin_fd, 'wb') as f:
        test_body['data'].astype(test_body['dtype']).T.tofile(f)
    #Write out canonical binary data to a tempfile
    output_bin_fd, output_bin_name = tempfile.mkstemp()
    with os.fdopen(output_bin_fd, 'wb') as f:
        output_body['data'].astype(output_body['dtype']).T.tofile(f)
    #Compare two tempfiles
    return subprocess.Popen(['./tools/nii_body_diff.sh', test_bin_name,
        output_bin_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE
        ).communicate()

if __name__ == "__main__":
    logger = logging.getLogger('raw2nii_test')
    logger.setLevel(logging.INFO)
    _formatter = logging.Formatter("%(levelname)s: %(message)s")
    _stream_handler = logging.StreamHandler()
    _stream_handler.setFormatter(_formatter)
    logger.addHandler(_stream_handler)
    parser = argparse.ArgumentParser()
    parser.add_argument("--debug", "-d", action="store_true")
    parser.add_argument("test_data_folder")
    parser.add_argument("test_folder")
    parser.add_argument("--rel-tolerance", "-r", type=float, default=1e-05)
    parser.add_argument("--abs-tolerance", "-a", type=float, default=1e-08)
    options = parser.parse_args()
    if options.debug:
        logger.setLevel(logging.DEBUG)
    main(options.test_data_folder, options.test_folder, options.rel_tolerance,
        options.abs_tolerance)
<commit_msg>Add unit test case to test_data.py<commit_after>import argparse
import glob
import logging
import numpy as np
import os
import pprint
import subprocess
import tempfile

from project import convert_raw2nii
from tools.nii_info import read_nii

DEFAULT_TEST_DATA = [
    ('canonicalData', 'philips/philips_3T_achieva/fMRI/')
]

def run_test(canon_data_folder, trial_folder, rtol, atol):
    """ Runs convert_raw2nii on a folder containing PARREC test data and
        compares the NIFTI output """
    logger = logging.getLogger('raw2nii_test')
    canon_data_folder = os.path.abspath(canon_data_folder)
    path_par = os.path.join(canon_data_folder, trial_folder, 'PARREC')
    input_par = glob.glob('{0}/*.PAR'.format(path_par))[0]
    test_folder = os.path.join('testOutput', trial_folder, 'NIFTI')
    #Set up convert_raw2nii logger
    raw2nii_logger = logging.getLogger('raw2nii')
    #raw2nii_logger.setLevel(logging.DEBUG)
    _formatter = logging.Formatter('test_data: %(levelname)s: %(message)s')
    _stream_handler = logging.StreamHandler()
    _stream_handler.setFormatter(_formatter)
    raw2nii_logger.addHandler(_stream_handler)
    #Run NIFTI conversion function and output to testOutput directory
    logger.info('Running test: parfile={0} path_par={1} test_folder={2}'.format(
        input_par, path_par, test_folder))
    output = convert_raw2nii(filelist=[input_par], prefix='', suffix='',
        pathpar=path_par, outfolder=test_folder, outputformat=1, angulation=1,
        rescale=1, dti_revertb0=0)
    #Compare to the 4D data since we have the Vanderbilt override behavior
    canon_nifti_folder = os.path.join(canon_data_folder, trial_folder, 'NIFTI',
        '4D')
    #Produce listing of each file in NIFTI folders in lexographical order and
    #prepare each pair for comparison
    canon_niftis = sorted(glob.glob('{0}/*.nii'.format(canon_nifti_folder)))
    test_niftis = sorted(glob.glob('{0}/*.nii'.format(test_folder)))
    if len(canon_niftis) != len(test_niftis):
        logger.error('Different number of output files! Test niftis: {0} '
            'Output niftis: {1}'.format(pprint.pformat(canon_niftis),
            pprint.pformat(test_niftis)))
    file_pairs = zip(canon_niftis, test_niftis)
    for canon_nifti, test_nifti in file_pairs:
        logger.info('File pair: {0} -> {1}'.format(canon_nifti, test_nifti))
        canon_header, canon_body = read_nii(canon_nifti)
        test_header, test_body = read_nii(test_nifti)
        #First compare the headers of the two niftis
        for canon_key, canon_val in canon_header.items():
            test_val = test_header[canon_key]
            #logger.debug('Compare header.{0} : {1} -> {2}'.format(canon_key,
            #   canon_val, test_val))
            if isinstance(canon_val, np.ndarray):
                is_different = not np.allclose(canon_val, test_val, rtol, atol)
            elif isinstance(canon_val, float):
                is_different = abs(canon_val - test_val) > (atol + rtol *
                    abs(test_val))
            else:
                is_different = (canon_val != test_val)
            if is_different:
                logger.warning('header.{0} are different: {1} -> {2}'.format(
                    canon_key, canon_val, test_val))
        #Then compare the bodies of the niftis
        if canon_body['dtype'] != test_body['dtype']:
            logger.warning('Body dtypes are different: {0} -> {1}'.format(
                canon_body['dtype'], test_body['dtype']))
        elif canon_body['data'].shape != test_body['data'].shape:
            logger.warning('Body shapes are different: {0} -> {1}'.format(
                canon_body['dtype'].shape, test_body['dtype'].shape))
        elif not np.allclose(canon_body['data'], test_body['data'], rtol, atol):
            diff_output, err_output = subprocess.Popen([
                './tools/nii_body_diff.sh', canon_nifti, test_nifti],
                stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
            logger.warning('Body data is different:\n{0}{1}'.format(
                diff_output, err_output))
    return 0

if __name__ == '__main__':
    logger = logging.getLogger('raw2nii_test')
    logger.setLevel(logging.INFO)
    _formatter = logging.Formatter('%(levelname)s: %(message)s')
    _stream_handler = logging.StreamHandler()
    _stream_handler.setFormatter(_formatter)
    logger.addHandler(_stream_handler)
    parser = argparse.ArgumentParser()
    parser.add_argument('--debug', '-d', action='store_true')
    parser.add_argument('canon_data_folder', nargs='?')
    parser.add_argument('trial_folder', nargs='?')
    parser.add_argument('--rel-tolerance', '-r', type=float, default=1e-05)
    parser.add_argument('--abs-tolerance', '-a', type=float, default=1e-08)
    options = parser.parse_args()
    if options.canon_data_folder and options.trial_folder:
        test_args = [(options.canon_data_folder, options.trial_folder)]
    else:
        test_args = DEFAULT_TEST_DATA
    if options.debug:
        logger.setLevel(logging.DEBUG)
    for canon_data_folder, trial_folder in test_args:
        run_test(canon_data_folder, trial_folder, options.rel_tolerance,
            options.abs_tolerance)
<|endoftext|>