<commit_before>#! /usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2007 (ita)
# Gustavo Carneiro (gjc), 2007

"Python support"

import os, sys
import Object, Action, Utils, Runner, Params, Common
from pproc import *

class pyobj(Object.genobj):
	s_default_ext = ['.py']
	def __init__(self, env=None):
		Object.genobj.__init__(self, 'other')
		self.pyopts = ''

		self.inst_var = 'PYTHONDIR'
		self.inst_dir = ''
		self.prio = 50

		self.env = env
		if not self.env: self.env = Params.g_build.m_allenvs['default']
		self.pyc = self.env['PYC']
		self.pyo = self.env['PYO']

	def apply(self):
		find_source_lst = self.path.find_source_lst

		envpyo = self.env.copy()
		envpyo['PYCMD']

		# first create the nodes corresponding to the sources
		for filename in self.to_list(self.source):
			node = find_source_lst(Utils.split_path(filename))
			if node is None:
				Params.fatal("Python source '%s' not found" % filename)

			base, ext = os.path.splitext(filename)
			#node = self.path.find_build(filename)
			if not ext in self.s_default_ext:
				fatal("unknown file "+filename)

			if self.pyc:
				task = self.create_task('pyc', self.env, self.prio)
				task.set_inputs(node)
				task.set_outputs(node.change_ext('.pyc'))
			if self.pyo:
				task = self.create_task('pyo', self.env, self.prio)
				task.set_inputs(node)
				task.set_outputs(node.change_ext('.pyo'))

	def install(self):
		for i in self.m_tasks:
			current = Params.g_build.m_curdirnode
			lst=[a.relpath_gen(current) for a in i.m_outputs]
			Common.install_files(self.inst_var, self.inst_dir, lst)
			lst=[a.relpath_gen(current) for a in i.m_inputs]
			Common.install_files(self.inst_var, self.inst_dir, lst)
			#self.install_results(self.inst_var, self.inst_dir, i)

def setup(env):
	Object.register('py', pyobj)
	Action.simple_action('pyc', '${PYTHON} ${PYFLAGS} -c ${PYCMD} ${SRC} ${TGT}', color='BLUE')
	Action.simple_action('pyo', '${PYTHON} ${PYFLAGS_OPT} -c ${PYCMD} ${SRC} ${TGT}', color='BLUE')

def _get_python_variables(python_exe, variables, imports=['import sys']):
	"""Run a python interpreter and print some variables"""
	program = list(imports)
	program.append('')
	for v in variables:
		program.append("print repr(%s)" % v)
	output = Popen([python_exe, "-c", '\n'.join(program)], stdout=PIPE).communicate()[0].split("\n")
	return_values = []
	for s in output:
		# FIXME eval ????
		if s: return_values.append(eval(s.rstrip()))
		else: break
	return return_values

def check_python_headers(conf):
	"""Check for headers and libraries necessary to extend or embed python.

	If successful, xxx_PYEXT and xxx_PYEMBED variables are defined in the
    enviroment (for uselib).  PYEXT should be used for compiling
    python extensions, while PYEMBED should be used by programs that
    need to embed a python interpreter.

	Note: this test requires that check_python_version was previously
	executed and successful."""

	python = conf.env['PYTHON']
	assert python, ("python is %r !" % (python,))

	v = 'prefix CC SYSLIBS SHLIBS LIBDIR LIBPL INCLUDEPY Py_ENABLE_SHARED'.split()
	(python_prefix, python_CC, python_SYSLIBS, python_SHLIBS,
	 python_LIBDIR, python_LIBPL, INCLUDEPY, Py_ENABLE_SHARED) = \
		_get_python_variables(python, ["get_config_var('%s')" % x for x in v], ['from distutils.sysconfig import get_config_var'])
	python_includes = [INCLUDEPY]

	## Check for python libraries for embedding
	if python_SYSLIBS is not None:
		for lib in python_SYSLIBS.split():
			libname = lib[2:] # strip '-l'
			conf.env.append_value('LIB_PYEMBED', libname)
	if python_SHLIBS is not None:
		for lib in python_SHLIBS.split():
			libname = lib[2:] # strip '-l'
			conf.env.append_value('LIB_PYEMBED', libname)
	lib = conf.create_library_configurator()
	lib.name = 'python' + conf.env['PYTHON_VERSION']
	lib.uselib = 'PYTHON'
	lib.code = '''
#ifdef __cplusplus
extern "C" {
#endif
 void Py_Initialize(void);
 void Py_Finalize(void);
#ifdef __cplusplus
}
#endif
int main(int argc, char *argv[]) { Py_Initialize(); Py_Finalize(); return 0; }
'''
	if python_LIBDIR is not None:
		lib.path = [python_LIBDIR]
		result = lib.run()
	else:
		result = 0

	## try again with -L$python_LIBPL (some systems don't install the python library in $prefix/lib)
	if not result:
		if python_LIBPL is not None:
			lib.path = [python_LIBPL]
			result = lib.run()
		else:
			result = 0

	## try again with -L$prefix/libs, and pythonXY name rather than pythonX.Y (win32)
	if not result:
		lib.path = [os.path.join(python_prefix, "libs")]
		lib.name = 'python' + conf.env['PYTHON_VERSION'].replace('.', '')
		result = lib.run()

	if result:
		conf.env['LIBPATH_PYEMBED'] = lib.path
		conf.env.append_value('LIB_PYEMBED', lib.name)

	# according to
	# distutils.command.build_ext.build_ext.get_libraries.__doc__
	# this might want to be OS/2 aswell.
	if sys.platform == 'win32' or (Py_ENABLE_SHARED is not None
					and sys.platform != 'darwin'):
		conf.env['LIBPATH_PYEXT'] = conf.env['LIBPATH_PYEMBED']
		conf.env['LIB_PYEXT'] = conf.env['LIB_PYEMBED']

	## Check for Python headers
	header = conf.create_header_configurator()
	header.path = python_includes
	header.name = 'Python.h'
	header.define = 'HAVE_PYTHON_H'
	header.uselib = 'PYEXT'
	header.code = "#include <Python.h>\nint main(int argc, char *argv[]) { Py_Initialize(); Py_Finalize(); return 0; }"
	result = header.run()
	if not result: return result

	conf.env['CPPPATH_PYEXT'] = python_includes
	conf.env['CPPPATH_PYEMBED'] = python_includes

	return result

def check_python_version(conf, minver=None):
	"""
	Check if the python interpreter is found matching a given minimum version.
	minver should be a tuple, eg. to check for python >= 2.4.2 pass (2,4,2) as minver.

	If successful, PYTHON_VERSION is defined as 'MAJOR.MINOR'
	(eg. '2.4') of the actual python version found, and PYTHONDIR is
	defined, pointing to the site-packages directory appropriate for
	this python version, where modules/packages/extensions should be
	installed.
	"""

	python = conf.env['PYTHON']
	assert python, ("python is %r !" % (python,))

	## Get python version string
	## Note: only works for python >= 2.0, but we don't want to
	## support python 1.x in 2007, do we? :)
	proc = Popen([python, "-c", "import sys; print repr(sys.version_info)"], stdout=PIPE)
	pyver_tuple = eval(proc.communicate()[0].rstrip())

	## compare python version with the minimum required
	result = (minver is None) or (pyver_tuple >= minver)

	if result:
		## define useful environment variables
		pyver = '.'.join(map(str, pyver_tuple[:2]))
		conf.env['PYTHON_VERSION'] = pyver

		if 'PYTHONDIR' in os.environ:
			dir = os.environ['PYTHONDIR']
		else:
			if sys.platform == 'win32':
				(python_LIBDEST,) = \
						_get_python_variables(python, ["get_config_var('LIBDEST')"],
						['from distutils.sysconfig import get_config_var'])
			else:
				python_LIBDEST = None
			if python_LIBDEST is None:
				python_LIBDEST = os.path.join(conf.env['PREFIX'], "lib", "python" + pyver)
			dir = os.path.join(python_LIBDEST, "site-packages")

		conf.add_define('PYTHONDIR', dir)
		conf.env['PYTHONDIR'] = dir

	## Feedback
	pyver_full = '.'.join(map(str, pyver_tuple[:3]))
	if minver is None:
		conf.check_message_custom('Python version', '', pyver_full)
	else:
		minver_str = '.'.join(map(str, minver))
		conf.check_message('Python version', ">= %s" % (minver_str,), result, option=pyver_full)

	return result

def detect(conf):
	python = conf.find_program('python', var='PYTHON')
	if not python: return 0

	v = conf.env

	v['PYCMD'] = '"import sys, py_compile;py_compile.compile(sys.argv[1], sys.argv[2])"'
	v['PYFLAGS'] = ''
	v['PYFLAGS_OPT'] = '-O'

	v['PYC'] = getattr(Params.g_options, 'pyc', 1)
	v['PYO'] = getattr(Params.g_options, 'pyo', 1)

	v['pyext_INST_VAR'] = 'PYTHONDIR'
	v['pyext_INST_DIR'] = ''

	v['pyembed_INST_VAR'] = v['program_INST_VAR']
	v['pyembed_INST_DIR'] = v['program_INST_DIR']

	v['pyext_PREFIX'] = ''

	if sys.platform == 'win32':
		v['pyext_SUFFIX'] = '.pyd'

	# now a small difference
	v['pyext_USELIB'] = 'PYEXT'
	v['pyembed_USELIB'] = 'PYEMBED'

	conf.hook(check_python_version)
	conf.hook(check_python_headers)

	return 1

def set_options(opt):
	opt.add_option('--nopyc', action = 'store_false', default = 1, help = 'no pyc files (configuration)', dest = 'pyc')
	opt.add_option('--nopyo', action = 'store_false', default = 1, help = 'no pyo files (configuration)', dest = 'pyo')

<commit_msg>Make python extension modules use plugin type. Pick up suffix from distutils<commit_after>#! /usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2007 (ita)
# Gustavo Carneiro (gjc), 2007

"Python support"

import os, sys
import Object, Action, Utils, Runner, Params, Common
from pproc import *

class pyobj(Object.genobj):
	s_default_ext = ['.py']
	def __init__(self, env=None):
		Object.genobj.__init__(self, 'other')
		self.pyopts = ''

		self.inst_var = 'PYTHONDIR'
		self.inst_dir = ''
		self.prio = 50

		self.env = env
		if not self.env: self.env = Params.g_build.m_allenvs['default']
		self.pyc = self.env['PYC']
		self.pyo = self.env['PYO']

	def apply(self):
		find_source_lst = self.path.find_source_lst

		envpyo = self.env.copy()
		envpyo['PYCMD']

		# first create the nodes corresponding to the sources
		for filename in self.to_list(self.source):
			node = find_source_lst(Utils.split_path(filename))
			if node is None:
				Params.fatal("Python source '%s' not found" % filename)

			base, ext = os.path.splitext(filename)
			#node = self.path.find_build(filename)
			if not ext in self.s_default_ext:
				fatal("unknown file "+filename)

			if self.pyc:
				task = self.create_task('pyc', self.env, self.prio)
				task.set_inputs(node)
				task.set_outputs(node.change_ext('.pyc'))
			if self.pyo:
				task = self.create_task('pyo', self.env, self.prio)
				task.set_inputs(node)
				task.set_outputs(node.change_ext('.pyo'))

	def install(self):
		for i in self.m_tasks:
			current = Params.g_build.m_curdirnode
			lst=[a.relpath_gen(current) for a in i.m_outputs]
			Common.install_files(self.inst_var, self.inst_dir, lst)
			lst=[a.relpath_gen(current) for a in i.m_inputs]
			Common.install_files(self.inst_var, self.inst_dir, lst)
			#self.install_results(self.inst_var, self.inst_dir, i)

def setup(env):
	Object.register('py', pyobj)
	Action.simple_action('pyc', '${PYTHON} ${PYFLAGS} -c ${PYCMD} ${SRC} ${TGT}', color='BLUE')
	Action.simple_action('pyo', '${PYTHON} ${PYFLAGS_OPT} -c ${PYCMD} ${SRC} ${TGT}', color='BLUE')

def _get_python_variables(python_exe, variables, imports=['import sys']):
	"""Run a python interpreter and print some variables"""
	program = list(imports)
	program.append('')
	for v in variables:
		program.append("print repr(%s)" % v)
	output = Popen([python_exe, "-c", '\n'.join(program)], stdout=PIPE).communicate()[0].split("\n")
	return_values = []
	for s in output:
		# FIXME eval ????
		if s: return_values.append(eval(s.rstrip()))
		else: break
	return return_values

def check_python_headers(conf):
	"""Check for headers and libraries necessary to extend or embed python.

	If successful, xxx_PYEXT and xxx_PYEMBED variables are defined in the
    enviroment (for uselib).  PYEXT should be used for compiling
    python extensions, while PYEMBED should be used by programs that
    need to embed a python interpreter.

	Note: this test requires that check_python_version was previously
	executed and successful."""

	python = conf.env['PYTHON']
	assert python, ("python is %r !" % (python,))

	v = 'prefix SO SYSLIBS SHLIBS LIBDIR LIBPL INCLUDEPY Py_ENABLE_SHARED'.split()
	(python_prefix, python_SO, python_SYSLIBS, python_SHLIBS,
	 python_LIBDIR, python_LIBPL, INCLUDEPY, Py_ENABLE_SHARED) = \
		_get_python_variables(python, ["get_config_var('%s')" % x for x in v], ['from distutils.sysconfig import get_config_var'])
	python_includes = [INCLUDEPY]

	conf.env['pyext_PREFIX'] = ''
	conf.env['pyext_SUFFIX'] = python_SO

	## Check for python libraries for embedding
	if python_SYSLIBS is not None:
		for lib in python_SYSLIBS.split():
			libname = lib[2:] # strip '-l'
			conf.env.append_value('LIB_PYEMBED', libname)
	if python_SHLIBS is not None:
		for lib in python_SHLIBS.split():
			libname = lib[2:] # strip '-l'
			conf.env.append_value('LIB_PYEMBED', libname)
	lib = conf.create_library_configurator()
	lib.name = 'python' + conf.env['PYTHON_VERSION']
	lib.uselib = 'PYTHON'
	lib.code = '''
#ifdef __cplusplus
extern "C" {
#endif
 void Py_Initialize(void);
 void Py_Finalize(void);
#ifdef __cplusplus
}
#endif
int main(int argc, char *argv[]) { Py_Initialize(); Py_Finalize(); return 0; }
'''
	if python_LIBDIR is not None:
		lib.path = [python_LIBDIR]
		result = lib.run()
	else:
		result = 0

	## try again with -L$python_LIBPL (some systems don't install the python library in $prefix/lib)
	if not result:
		if python_LIBPL is not None:
			lib.path = [python_LIBPL]
			result = lib.run()
		else:
			result = 0

	## try again with -L$prefix/libs, and pythonXY name rather than pythonX.Y (win32)
	if not result:
		lib.path = [os.path.join(python_prefix, "libs")]
		lib.name = 'python' + conf.env['PYTHON_VERSION'].replace('.', '')
		result = lib.run()

	if result:
		conf.env['LIBPATH_PYEMBED'] = lib.path
		conf.env.append_value('LIB_PYEMBED', lib.name)

	# according to
	# distutils.command.build_ext.build_ext.get_libraries.__doc__
	# this might want to be OS/2 aswell.
	if sys.platform == 'win32' or (Py_ENABLE_SHARED is not None
					and sys.platform != 'darwin'):
		conf.env['LIBPATH_PYEXT'] = conf.env['LIBPATH_PYEMBED']
		conf.env['LIB_PYEXT'] = conf.env['LIB_PYEMBED']

	## Check for Python headers
	header = conf.create_header_configurator()
	header.path = python_includes
	header.name = 'Python.h'
	header.define = 'HAVE_PYTHON_H'
	header.uselib = 'PYEXT'
	header.code = "#include <Python.h>\nint main(int argc, char *argv[]) { Py_Initialize(); Py_Finalize(); return 0; }"
	result = header.run()
	if not result: return result

	conf.env['CPPPATH_PYEXT'] = python_includes
	conf.env['CPPPATH_PYEMBED'] = python_includes

	return result

def check_python_version(conf, minver=None):
	"""
	Check if the python interpreter is found matching a given minimum version.
	minver should be a tuple, eg. to check for python >= 2.4.2 pass (2,4,2) as minver.

	If successful, PYTHON_VERSION is defined as 'MAJOR.MINOR'
	(eg. '2.4') of the actual python version found, and PYTHONDIR is
	defined, pointing to the site-packages directory appropriate for
	this python version, where modules/packages/extensions should be
	installed.
	"""

	python = conf.env['PYTHON']
	assert python, ("python is %r !" % (python,))

	## Get python version string
	## Note: only works for python >= 2.0, but we don't want to
	## support python 1.x in 2007, do we? :)
	proc = Popen([python, "-c", "import sys; print repr(sys.version_info)"], stdout=PIPE)
	pyver_tuple = eval(proc.communicate()[0].rstrip())

	## compare python version with the minimum required
	result = (minver is None) or (pyver_tuple >= minver)

	if result:
		## define useful environment variables
		pyver = '.'.join(map(str, pyver_tuple[:2]))
		conf.env['PYTHON_VERSION'] = pyver

		if 'PYTHONDIR' in os.environ:
			dir = os.environ['PYTHONDIR']
		else:
			if sys.platform == 'win32':
				(python_LIBDEST,) = \
						_get_python_variables(python, ["get_config_var('LIBDEST')"],
						['from distutils.sysconfig import get_config_var'])
			else:
				python_LIBDEST = None
			if python_LIBDEST is None:
				python_LIBDEST = os.path.join(conf.env['PREFIX'], "lib", "python" + pyver)
			dir = os.path.join(python_LIBDEST, "site-packages")

		conf.add_define('PYTHONDIR', dir)
		conf.env['PYTHONDIR'] = dir

	## Feedback
	pyver_full = '.'.join(map(str, pyver_tuple[:3]))
	if minver is None:
		conf.check_message_custom('Python version', '', pyver_full)
	else:
		minver_str = '.'.join(map(str, minver))
		conf.check_message('Python version', ">= %s" % (minver_str,), result, option=pyver_full)

	return result

def detect(conf):
	python = conf.find_program('python', var='PYTHON')
	if not python: return 0

	v = conf.env

	v['PYCMD'] = '"import sys, py_compile;py_compile.compile(sys.argv[1], sys.argv[2])"'
	v['PYFLAGS'] = ''
	v['PYFLAGS_OPT'] = '-O'

	v['PYC'] = getattr(Params.g_options, 'pyc', 1)
	v['PYO'] = getattr(Params.g_options, 'pyo', 1)

	v['pyext_INST_VAR'] = 'PYTHONDIR'
	v['pyext_INST_DIR'] = ''

	v['pyembed_INST_VAR'] = v['program_INST_VAR']
	v['pyembed_INST_DIR'] = v['program_INST_DIR']

	# now a small difference
	v['pyext_USELIB'] = 'PYEXT'
	v['pyembed_USELIB'] = 'PYEMBED'

	conf.hook(check_python_version)
	conf.hook(check_python_headers)

	return 1

def set_options(opt):
	opt.add_option('--nopyc', action = 'store_false', default = 1, help = 'no pyc files (configuration)', dest = 'pyc')
	opt.add_option('--nopyo', action = 'store_false', default = 1, help = 'no pyo files (configuration)', dest = 'pyo')

<|endoftext|>


<commit_before>#!/usr/bin/python
###################################################################################################################
### This code is developed by HighEnergyDataScientests Team.
### Do not copy or modify without written approval from one of the team members.
###################################################################################################################

import pandas as pd
import numpy as np

from sklearn import preprocessing
from sklearn.preprocessing import Imputer
#from sklearn.preprocessing import StandardScaler

from sklearn.cross_validation import train_test_split

import time
import os
import sklearn.metrics

# Support Vectro Machine Classifyer
from sklearn import svm

### Controlling Parameters
output_col_name = "target"
test_col_name = "PredictedProb"
id_col_name = "ID"
test_size = 0.05
C = 1.0

# load the datasets
print("## Loading Data")
models_predictions_file = "../predictions/models_predictions.csv"
train = pd.read_csv('../inputs/train.csv')
test = pd.read_csv('../inputs/test.csv')

# data processing
print("## Data Processing")
if os.path.isfile(models_predictions_file):
    models_predictions = pd.read_csv(models_predictions_file)
else:
    models_predictions = pd.DataFrame()

# encoding data
print("## Encoding Data")
train = train.drop(id_col_name, axis=1)
for f in train.columns:
    if train[f].dtype=='object':
        print(f)
        lbl = preprocessing.LabelEncoder()
        lbl.fit(list(train[f].values) + list(test[f].values))
        train[f] = lbl.transform(list(train[f].values))
        test[f] = lbl.transform(list(test[f].values))

features = [s for s in train.columns.ravel().tolist() if s != output_col_name]
print("Features: ", features)

imp = Imputer(missing_values='NaN', strategy='mean', axis=0)
imp.fit(train[features])
train[features] = imp.transform(train[features])
test[features] = imp.transform(test[features])

# splitting training data into training and cross validation sets
X_pos = train[train[output_col_name] == 1]
X_neg = train[train[output_col_name] == 0]

X_train_pos, X_valid_pos = train_test_split(X_pos, test_size=test_size)
X_train_neg, X_valid_neg = train_test_split(X_neg, test_size=test_size)

X_train = pd.concat([X_train_pos,X_train_neg])
X_valid = pd.concat([X_valid_pos,X_valid_neg])

X_train = X_train.iloc[np.random.permutation(len(X_train))]
X_valid = X_valid.iloc[np.random.permutation(len(X_valid))]

y_train = X_train[output_col_name]
y_valid = X_valid[output_col_name]

# creating temp_test array to use to compute predictions with test data set
temp_test = test
temp_test = temp_test.drop(id_col_name, axis=1)

# deleting first column, which corresponds to output y, from both data sets
X_train = X_train.drop(output_col_name, axis=1)
X_valid = X_valid.drop(output_col_name, axis=1)
#print(X_train)

# scaling data
print("## Scaling Data")
X_train = preprocessing.scale(X_train)
X_valid = preprocessing.scale(X_valid)
temp_test = preprocessing.scale(temp_test)

#X_train = StandardScaler().fit_transform(X_train)
#X_valid = StandardScaler().fit_transform(X_valid)
#temp_test = StandardScaler().fit_transform(temp_test)
#print(X_train)
#print("## Mean of X_train = ", X_train.mean(axis=0))
#print("## STD of X_train = ", X_train.std(axis=0))

# fit SVC model to the data
print("## Training")
clf = svm.SVC(C=C, probability=True, max_iter=100)
clf.fit(X_train, y_train)
timestr = time.strftime("%Y%m%d-%H%M%S")

# make predictions
print("## Making Predictions")
p_train = clf.predict_proba(X_train)
p_valid = clf.predict_proba(X_valid)

score_train = sklearn.metrics.log_loss(y_train, p_train[:,1])
score_valid = sklearn.metrics.log_loss(y_valid, p_valid[:,1])
print("Score based on training data set = ", score_train)
print("Score based on validating data set = ", score_valid)

# make predictions on test data set
p_test = clf.predict_proba(temp_test)

# Producing output of predicted probabilities and writing it into a file
test[test_col_name] = p_test[:,1]
test[[id_col_name,test_col_name]].to_csv("../predictions/pred_SVC_" + timestr + ".csv", index=False)
<commit_msg>Deleting old files<commit_after><|endoftext|>


<commit_before>from __future__ import unicode_literals
from wagtailmenus.utils.version import get_version, get_stable_branch_name

# major.minor.patch.release.number
# release must be one of alpha, beta, rc, or final
VERSION = (2, 2, 3, 'final', 0)
__version__ = get_version(VERSION)
stable_branch_name = get_stable_branch_name(VERSION)

default_app_config = 'wagtailmenus.apps.WagtailMenusConfig'

def get_main_menu_model_string():
    """
    Get the dotted ``app.Model`` name for the main menu model as a string.
    Useful for developers extending wagtailmenus, that need to refer to the
    main menu model (such as in foreign keys), but the model itself is not
    required.
    """
    from wagtailmenus import app_settings
    return app_settings.MAIN_MENU_MODEL

def get_flat_menu_model_string():
    """
    Get the dotted ``app.Model`` name for the flat menu model as a string.
    Useful for developers extending wagtailmenus, that need to refer to the
    flat menu model (such as in foreign keys), but the model itself is not
    required.
    """
    from wagtailmenus import app_settings
    return app_settings.FLAT_MENU_MODEL

def get_main_menu_model():
    """
    Get the model from the ``WAGTAILMENUS_MAIN_MENU_MODEL`` setting.
    Useful for developers extending wagtailmenus, and need the actual model.
    Defaults to the standard :class:`~wagtailmenus.models.MainMenu` model
    if no custom model is defined.
    """
    from wagtailmenus import app_settings
    return app_settings.MAIN_MENU_MODEL_CLASS

def get_flat_menu_model():
    """
    Get the model from the ``WAGTAILMENUS_FLAT_MENU_MODEL`` setting.
    Useful for developers extending wagtailmenus, and need to the actual model.
    Defaults to the standard :class:`~wagtailmenus.models.FlatMenu` model
    if no custom model is defined.
    """
    from wagtailmenus import app_settings
    return app_settings.FLAT_MENU_MODEL_CLASS
<commit_msg>Update development version to 2.3.1a0<commit_after>from __future__ import unicode_literals
from wagtailmenus.utils.version import get_version, get_stable_branch_name

# major.minor.patch.release.number
# release must be one of alpha, beta, rc, or final
VERSION = (2, 3, 1, 'alpha', 0)
__version__ = get_version(VERSION)
stable_branch_name = get_stable_branch_name(VERSION)

default_app_config = 'wagtailmenus.apps.WagtailMenusConfig'

def get_main_menu_model_string():
    """
    Get the dotted ``app.Model`` name for the main menu model as a string.
    Useful for developers extending wagtailmenus, that need to refer to the
    main menu model (such as in foreign keys), but the model itself is not
    required.
    """
    from wagtailmenus import app_settings
    return app_settings.MAIN_MENU_MODEL

def get_flat_menu_model_string():
    """
    Get the dotted ``app.Model`` name for the flat menu model as a string.
    Useful for developers extending wagtailmenus, that need to refer to the
    flat menu model (such as in foreign keys), but the model itself is not
    required.
    """
    from wagtailmenus import app_settings
    return app_settings.FLAT_MENU_MODEL

def get_main_menu_model():
    """
    Get the model from the ``WAGTAILMENUS_MAIN_MENU_MODEL`` setting.
    Useful for developers extending wagtailmenus, and need the actual model.
    Defaults to the standard :class:`~wagtailmenus.models.MainMenu` model
    if no custom model is defined.
    """
    from wagtailmenus import app_settings
    return app_settings.MAIN_MENU_MODEL_CLASS

def get_flat_menu_model():
    """
    Get the model from the ``WAGTAILMENUS_FLAT_MENU_MODEL`` setting.
    Useful for developers extending wagtailmenus, and need to the actual model.
    Defaults to the standard :class:`~wagtailmenus.models.FlatMenu` model
    if no custom model is defined.
    """
    from wagtailmenus import app_settings
    return app_settings.FLAT_MENU_MODEL_CLASS
<|endoftext|>


<commit_before>#!/usr/bin/env python
import boto3
import logging
from jailscraper import app_config

logging.basicConfig()
logger = logging.getLogger(app_config.PROJECT_SLUG)
logger.setLevel(logging.DEBUG)

PUBLIC_READ_URI = 'http://acs.amazonaws.com/groups/global/AllUsers'
PUBLIC_READ_PERMISSION = 'READ'

def cleanup():
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(app_config.S3_BUCKET)
    prefix = '{0}/daily'.format(app_config.TARGET)
    keys = list(bucket.objects.filter(Prefix=prefix).all())
    for key in keys:
        acl = key.Acl()
        publicread_perms = [grant for grant in acl.grants
                            if grant['Grantee'].get('URI') == PUBLIC_READ_URI and
                            grant['Permission'] == PUBLIC_READ_PERMISSION]

        if not len(publicread_perms):
            logger.debug('{0}/{1} set to public-read'.format(key.bucket_name, key.key))
            key.Acl().put(ACL='public-read')
        else:
            logger.debug('{0}/{1} is already public read'.format(key.bucket_name, key.key))

if __name__ == '__main__':
    cleanup()
<commit_msg>write scraped urls list to file, closes #41<commit_after>#!/usr/bin/env python
import boto3
import csv
import logging
from io import StringIO
from jailscraper import app_config

logging.basicConfig()
logger = logging.getLogger(app_config.PROJECT_SLUG)
logger.setLevel(logging.DEBUG)

PUBLIC_READ_URI = 'http://acs.amazonaws.com/groups/global/AllUsers'
PUBLIC_READ_PERMISSION = 'READ'

def cleanup():
    """
    Set all urls to public read. Returns list of URLs found.
    """
    urls = []
    client = boto3.client('s3')
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(app_config.S3_BUCKET)
    prefix = '{0}/daily'.format(app_config.TARGET)
    keys = list(bucket.objects.filter(Prefix=prefix).all())
    for key in keys:
        acl = key.Acl()
        publicread_perms = [grant for grant in acl.grants
                            if grant['Grantee'].get('URI') == PUBLIC_READ_URI and
                            grant['Permission'] == PUBLIC_READ_PERMISSION]

        url = client.generate_presigned_url('get_object', Params= {'Bucket': app_config.S3_BUCKET, 'Key': key.key})
        urls.append((url.split('?')[0],))

        if not len(publicread_perms):
            logger.debug('{0}/{1} set to public-read'.format(key.bucket_name, key.key))
            key.Acl().put(ACL='public-read')
        else:
            logger.debug('{0}/{1} is already public read'.format(key.bucket_name, key.key))

    return urls

def write_urls(urls):
    """
    Write URLs to manifest file on S3.
    """
    urls = sorted(urls) # Sort in case Amazon order changes for some reason
    f = StringIO()
    writer = csv.writer(f)
    writer.writerows(urls)
    s3 = boto3.resource('s3')
    path = '{0}/manifest.csv'.format(app_config.TARGET)
    key = s3.Object(app_config.S3_BUCKET, path)
    key.put(Body=f.getvalue())
    key.Acl().put(ACL='public-read')

if __name__ == '__main__':
    urls = cleanup()
    write_urls(urls)
<|endoftext|>


<commit_before>from django.utils.timezone import now
from django.db import models

class MenuItemManager(models.Manager):
    ''' App-specific manager overrides '''

    def for_display(self):
        return self.filter(
            Q(link_page__isnull=True) |
            Q(link_page__live=True) & Q(link_page__expired=False) &
            Q(link_page__in_menus=True)
        ).select_related('link_page')
<commit_msg>Fix manager errors<commit_after>from django.utils.timezone import now
from django.db import models

class MenuItemManager(models.Manager):
    ''' App-specific manager overrides '''

    def for_display(self):
        return self.filter(
            models.Q(link_page__isnull=True) |
            models.Q(link_page__live=True) &
            models.Q(link_page__expired=False) &
            models.Q(link_page__show_in_menus=True)
        ).select_related('link_page')
<|endoftext|>


<commit_before>import os
import sys
import configobj
import threading
from warden_logging import log
import logging
from warden_utils import normalize_path

from diamond.server import Server

class DiamondManager:

    def __init__(self, settings):
        self.thread = None
        self.config = None

        configfile = normalize_path(settings.DIAMOND_CONFIG)

        if os.path.exists(configfile):
            self.config = self.build_configObj(configfile)

        else:
            print >> sys.stderr, "ERROR: Config file: %s does not exist." % configfile
            sys.exit(1)

        self.log_diamond = logging.getLogger('diamond')
        self.log_diamond.setLevel(settings.DIAMOND_STDOUT_LEVEL)
        self.log_diamond.propagate = False

#       LOG to STDOUT
        formatter = logging.Formatter('[%(asctime)s][%(levelname)s][%(message)s]')
        streamHandler = logging.StreamHandler(sys.stdout)
        streamHandler.setFormatter(formatter)
        streamHandler.setLevel(settings.DIAMOND_STDOUT_LEVEL)
        self.log_diamond.addHandler(streamHandler)
        self.log_diamond.disabled = False

    def build_configObj(self, config_path):
        config = configobj.ConfigObj(config_path)
        config['configfile'] = config_path

        dr = os.environ['DIAMOND_ROOT']

        self.ensure_var(config['server'],'pidfile', os.path.join(dr,'diamond.pid'))

        self.ensure_var(config['server'],'collectors_path', os.path.join(dr,'collectors'))

        self.ensure_var(config['server'],'collectors_config_path', os.path.join(dr,'collectors','config'))

        self.ensure_var(config['server'],'handlers_config_path', os.path.join(dr,'handlers'))

        return config

    def ensure_var(self, d, v, c):
        if d is None:
            return
        if d[v] is None:
            d[v] = c

    def start(self):
        log.debug("Starting Diamond..")
        self.thread = self.DiamondThread(self.config)
        self.thread.start()

        log.debug("Started Diamond.")

    def stop(self):
        if self.thread.isAlive():
            log.debug("Stopping Diamond..")
            self.thread.stop()
            self.thread.join()
            log.debug("Stopped Diamond.")
        else:
            log.error("Can't stop Diamond if it has not started.")

    def is_active(self):
        if not self.thread:
            return False

        return self.thread.server.running

    class DiamondThread(threading.Thread):

        def __init__(self, config):
            threading.Thread.__init__(self)
            self.server = Server(config)

        def run(self):
            self.server.run()

        def stop(self):
            self.server.stop()

<commit_msg>Diamond Root ENV, maintains missing diamond conf path entries<commit_after>import os
import sys
import configobj
import threading
from warden_logging import log
import logging
from warden_utils import normalize_path

from diamond.server import Server

class DiamondManager:

    def __init__(self, settings):
        self.thread = None
        self.config = None

        configfile = normalize_path(settings.DIAMOND_CONFIG)

        if os.path.exists(configfile):
            self.config = configobj.ConfigObj(configfile)
            self.config['configfile'] = configfile

            self.ensure_path(self.config['server'],'pid_file', 'diamond.pid')

            self.ensure_path(self.config['server'],'collectors_path', 'collectors','collectors')

            self.ensure_path(self.config['server'],'collectors_config_path', 'collectors','config')

            self.ensure_path(self.config['server'],'handlers_config_path', 'handlers')

        else:
            print >> sys.stderr, "ERROR: Config file: %s does not exist." % configfile
            sys.exit(1)

        self.log_diamond = logging.getLogger('diamond')
        self.log_diamond.setLevel(settings.DIAMOND_STDOUT_LEVEL)
        self.log_diamond.propagate = False

#       LOG to STDOUT
        formatter = logging.Formatter('[%(asctime)s][%(levelname)s][%(message)s]')
        streamHandler = logging.StreamHandler(sys.stdout)
        streamHandler.setFormatter(formatter)
        streamHandler.setLevel(settings.DIAMOND_STDOUT_LEVEL)
        self.log_diamond.addHandler(streamHandler)
        self.log_diamond.disabled = False

    def ensure_path(self, section, var, *args):
        if not var in section:
            try:
                dr = os.environ['DIAMOND_ROOT']
                section[var] = os.path.join(dr, args)
            except KeyError:
                print 'ERROR: Diamond missing path configuration %s[%s] AND $DIAMOND_ROOT has not been set!' % (section, var)
                exit(1)

    def start(self):
        log.debug("Starting Diamond..")
        self.thread = self.DiamondThread(self.config)
        self.thread.start()

        log.debug("Started Diamond.")

    def stop(self):
        if self.thread.isAlive():
            log.debug("Stopping Diamond..")
            self.thread.stop()
            self.thread.join()
            log.debug("Stopped Diamond.")
        else:
            log.error("Can't stop Diamond if it has not started.")

    def is_active(self):
        if not self.thread:
            return False

        return self.thread.server.running

    class DiamondThread(threading.Thread):

        def __init__(self, config):
            threading.Thread.__init__(self)
            self.server = Server(config)

        def run(self):
            self.server.run()

        def stop(self):
            self.server.stop()

<|endoftext|>


<commit_before>import os
import itertools
from waterbutler.core import exceptions

class WaterButlerPathPart:
    DECODE = lambda x: x
    ENCODE = lambda x: x

    def __init__(self, part, _id=None):
        self._id = _id
        self._count = 0
        self._orig_id = _id
        self._orig_part = part
        self._name, self._ext = os.path.splitext(self.original_value)

    @property
    def identifier(self):
        return self._id

    @property
    def value(self):
        if self._count:
            return'{} ({}){}'.format(self._name, self._count, self._ext)
        return'{}{}'.format(self._name, self._ext)

    @property
    def raw(self):
        return self.__class__.ENCODE(self.value)

    @property
    def original_value(self):
        return self.__class__.DECODE(self._orig_part)

    @property
    def original_raw(self):
        return self._orig_part

    @property
    def ext(self):
        return self._ext

    def increment_name(self, _id=None):
        self._id = _id
        self._count += 1
        return self

    def renamed(self, name):
        return self.__class__(self.__class__.ENCODE(name), _id=self._id)

    def __repr__(self):
        return '{}({!r}, count={})'.format(self.__class__.__name__, self._orig_part, self._count)

class WaterButlerPath:
    """
    A standardized and validated immutable WaterButler path.
    """

    PART_CLASS = WaterButlerPathPart

    @classmethod
    def generic_path_validation(cls, path):
        """Validates a WaterButler specific path, e.g. /folder/file.txt, /folder/
        :param str path: WaterButler path
        """
        if not path:
            raise exceptions.InvalidPathError('Must specify path')
        if not path.startswith('/'):
            raise exceptions.InvalidPathError('Invalid path \'{}\' specified'.format(path))
        if '//' in path:
            raise exceptions.InvalidPathError('Invalid path \'{}\' specified'.format(path))
        # Do not allow path manipulation via shortcuts, e.g. '..'
        absolute_path = os.path.abspath(path)
        if not path == '/' and path.endswith('/'):
            absolute_path += '/'
        if not path == absolute_path:
            raise exceptions.InvalidPathError('Invalid path \'{}\' specified'.format(absolute_path))

    @classmethod
    def validate_folder(cls, path):
        if not path.is_dir:
            raise exceptions.CreateFolderError('Path must be a directory', code=400)

        if path.is_root:
            raise exceptions.CreateFolderError('Path can not be root', code=400)

    @classmethod
    def from_parts(cls, parts, folder=False, **kwargs):
        _ids, _parts = [], []
        for part in parts:
            _ids.append(part.identifier)
            _parts.append(part.raw)

        path = '/'.join(_parts)
        if parts and not path:
            path = '/'

        return cls(path, _ids=_ids, folder=folder, **kwargs)

    def __init__(self, path, _ids=(), prepend=None, folder=None):
        self.__class__.generic_path_validation(path)

        self._orig_path = path

        self._prepend = prepend

        if prepend:
            self._prepend_parts = [self.PART_CLASS(part, None) for part in prepend.rstrip('/').split('/')]
        else:
            self._prepend_parts = []

        self._parts = [
            self.PART_CLASS(part, _id)
            for _id, part in
            itertools.zip_longest(_ids, path.rstrip('/').split('/'))
        ]

        if folder is not None:
            self._is_folder = bool(folder)
        else:
            self._is_folder = self._orig_path.endswith('/')

        if self.is_dir and not self._orig_path.endswith('/'):
            self._orig_path += '/'

    @property
    def is_root(self):
        return len(self._parts) == 1

    @property
    def is_dir(self):
        return self._is_folder

    @property
    def kind(self):
        return 'folder' if self._is_folder else 'file'

    @property
    def is_file(self):
        return not self._is_folder

    @property
    def parts(self):
        return self._parts

    @property
    def name(self):
        return self._parts[-1].value

    @property
    def identifier(self):
        return self._parts[-1].identifier

    @property
    def identifier_path(self):
        return '/' + self._parts[-1].identifier + ('/' if self.is_dir else '')

    @property
    def ext(self):
        return self._parts[-1].ext

    @property
    def path(self):
        if len(self.parts) == 1:
            return ''
        return '/'.join([x.value for x in self.parts[1:]]) + ('/' if self.is_dir else '')

    @property
    def raw_path(self):
        if len(self.parts) == 1:
            return ''
        return '/'.join([x.raw for x in self.parts[1:]]) + ('/' if self.is_dir else '')

    @property
    def full_path(self):
        return '/'.join([x.value for x in self._prepend_parts + self.parts[1:]]) + ('/' if self.is_dir else '')

    @property
    def parent(self):
        if len(self.parts) == 1:
            return None
        return self.__class__.from_parts(self.parts[:-1], folder=True, prepend=self._prepend)

    def child(self, name, _id=None, folder=False):
        return self.__class__.from_parts(self.parts + [self.PART_CLASS(name, _id=_id)], folder=folder, prepend=self._prepend)

    def increment_name(self):
        self._parts[-1].increment_name()
        return self

    def rename(self, name):
        self._parts[-1] = self._parts[-1].renamed(name)
        return self

    def __eq__(self, other):
        return isinstance(other, self.__class__) and str(self) == str(other)

    def __str__(self):
        return '/'.join([x.value for x in self.parts]) + ('/' if self.is_dir else '')

    def __repr__(self):
        return '{}({!r}, prepend={!r})'.format(self.__class__.__name__, self._orig_path, self._prepend)
<commit_msg>Add is_folder to WaterbutlerPath for consistancy<commit_after>import os
import itertools
from waterbutler.core import exceptions

class WaterButlerPathPart:
    DECODE = lambda x: x
    ENCODE = lambda x: x

    def __init__(self, part, _id=None):
        self._id = _id
        self._count = 0
        self._orig_id = _id
        self._orig_part = part
        self._name, self._ext = os.path.splitext(self.original_value)

    @property
    def identifier(self):
        return self._id

    @property
    def value(self):
        if self._count:
            return'{} ({}){}'.format(self._name, self._count, self._ext)
        return'{}{}'.format(self._name, self._ext)

    @property
    def raw(self):
        return self.__class__.ENCODE(self.value)

    @property
    def original_value(self):
        return self.__class__.DECODE(self._orig_part)

    @property
    def original_raw(self):
        return self._orig_part

    @property
    def ext(self):
        return self._ext

    def increment_name(self, _id=None):
        self._id = _id
        self._count += 1
        return self

    def renamed(self, name):
        return self.__class__(self.__class__.ENCODE(name), _id=self._id)

    def __repr__(self):
        return '{}({!r}, count={})'.format(self.__class__.__name__, self._orig_part, self._count)

class WaterButlerPath:
    """
    A standardized and validated immutable WaterButler path.
    """

    PART_CLASS = WaterButlerPathPart

    @classmethod
    def generic_path_validation(cls, path):
        """Validates a WaterButler specific path, e.g. /folder/file.txt, /folder/
        :param str path: WaterButler path
        """
        if not path:
            raise exceptions.InvalidPathError('Must specify path')
        if not path.startswith('/'):
            raise exceptions.InvalidPathError('Invalid path \'{}\' specified'.format(path))
        if '//' in path:
            raise exceptions.InvalidPathError('Invalid path \'{}\' specified'.format(path))
        # Do not allow path manipulation via shortcuts, e.g. '..'
        absolute_path = os.path.abspath(path)
        if not path == '/' and path.endswith('/'):
            absolute_path += '/'
        if not path == absolute_path:
            raise exceptions.InvalidPathError('Invalid path \'{}\' specified'.format(absolute_path))

    @classmethod
    def validate_folder(cls, path):
        if not path.is_dir:
            raise exceptions.CreateFolderError('Path must be a directory', code=400)

        if path.is_root:
            raise exceptions.CreateFolderError('Path can not be root', code=400)

    @classmethod
    def from_parts(cls, parts, folder=False, **kwargs):
        _ids, _parts = [], []
        for part in parts:
            _ids.append(part.identifier)
            _parts.append(part.raw)

        path = '/'.join(_parts)
        if parts and not path:
            path = '/'

        return cls(path, _ids=_ids, folder=folder, **kwargs)

    def __init__(self, path, _ids=(), prepend=None, folder=None):
        self.__class__.generic_path_validation(path)

        self._orig_path = path

        self._prepend = prepend

        if prepend:
            self._prepend_parts = [self.PART_CLASS(part, None) for part in prepend.rstrip('/').split('/')]
        else:
            self._prepend_parts = []

        self._parts = [
            self.PART_CLASS(part, _id)
            for _id, part in
            itertools.zip_longest(_ids, path.rstrip('/').split('/'))
        ]

        if folder is not None:
            self._is_folder = bool(folder)
        else:
            self._is_folder = self._orig_path.endswith('/')

        if self.is_dir and not self._orig_path.endswith('/'):
            self._orig_path += '/'

    @property
    def is_root(self):
        return len(self._parts) == 1

    @property
    def is_dir(self):
        return self._is_folder

    @property
    def is_folder(self):
        return self._is_folder

    @property
    def is_file(self):
        return not self._is_folder

    @property
    def kind(self):
        return 'folder' if self._is_folder else 'file'

    @property
    def parts(self):
        return self._parts

    @property
    def name(self):
        return self._parts[-1].value

    @property
    def identifier(self):
        return self._parts[-1].identifier

    @property
    def identifier_path(self):
        return '/' + self._parts[-1].identifier + ('/' if self.is_dir else '')

    @property
    def ext(self):
        return self._parts[-1].ext

    @property
    def path(self):
        if len(self.parts) == 1:
            return ''
        return '/'.join([x.value for x in self.parts[1:]]) + ('/' if self.is_dir else '')

    @property
    def raw_path(self):
        if len(self.parts) == 1:
            return ''
        return '/'.join([x.raw for x in self.parts[1:]]) + ('/' if self.is_dir else '')

    @property
    def full_path(self):
        return '/'.join([x.value for x in self._prepend_parts + self.parts[1:]]) + ('/' if self.is_dir else '')

    @property
    def parent(self):
        if len(self.parts) == 1:
            return None
        return self.__class__.from_parts(self.parts[:-1], folder=True, prepend=self._prepend)

    def child(self, name, _id=None, folder=False):
        return self.__class__.from_parts(self.parts + [self.PART_CLASS(name, _id=_id)], folder=folder, prepend=self._prepend)

    def increment_name(self):
        self._parts[-1].increment_name()
        return self

    def rename(self, name):
        self._parts[-1] = self._parts[-1].renamed(name)
        return self

    def __eq__(self, other):
        return isinstance(other, self.__class__) and str(self) == str(other)

    def __str__(self):
        return '/'.join([x.value for x in self.parts]) + ('/' if self.is_dir else '')

    def __repr__(self):
        return '{}({!r}, prepend={!r})'.format(self.__class__.__name__, self._orig_path, self._prepend)
<|endoftext|>


<commit_before>"""
this is the stuff specific to the plant studies that are loaded. It can include comments and all that. The studytreelist will be read from the load_synth_extract file. Other variables in the conf could be overridden here.
"""

import load_synth_extract

studytreelist=["1022_1967",
               "194_2284",#early and nymphaeles
               "562_817",
               "424_532",
               "1916_3902",
               "588_878",
               "2610_6117", #malpighiales
               "826_1584",#rosaceae
               "1133_5647",
               "2539_5465",
               "1118_2225", #Mentheae,lamiaceae
               "650_1147",#Meliaceae, Sapindales
               "2085_4317",#Araceae
               "2044_4212",#Orobanchaceae
               "2626_6142",#Amaranthaceae
               "2624_6139",#Veronica
               "2598_6020",#Boraginaceae
               "2564_5699",#Polystichum
               "2042_4202",#Bartramiaceae
               "2034_4191",#Ruellieae
               #"2608_6116", #saxifrigales
               #"231_5505", #caryoph SOME SORT OF LOADING PROBLEM
               "14_12",
               "180_794",
               #"574_840", upload problems
               "576_849",
               "581_859",
               "582_862",
               "598_926",
               "599_927",
               "603_940",
               "704_1266",
               "721_1298",
               "723_1300",
               #"724_3212", upload problems
               "921_4103",
               "1300_2613",
               #"1302_2616", make for weird euphyllophyta
               "915_1802",
               "915_1803",
               "9_1",
               "142_38",
               "21_37",
               "57_777",
               "58_775",
               "72_801",
               "75_1743",
               "535_768",
               "61_816",
               "284_185",
               "2546_5493",
               "1086_2111",
               "283_184",
               "713_1284",
               "54_949",
               "1116_2217",
               "225_5991",#deep plants
               "1867_3766", #cycads
               "412_2166",#conifers
               "2046_5928" #Trebouxiophyceae, Chlorophyta
               ]

if __name__ == "__main__":
	from stephen_desktop_conf import *

	synthottolid="10218"

	print "loading synthottolid:",synthottolid
	print "loading studytreelist:",studytreelist

	load_synth_extract.run(dott,dload,studyloc,studytreelist,javapre,
				 treemloc,generallogfileloc,dsynth,synthottolid,treefn)
<commit_msg>more plants<commit_after>"""
this is the stuff specific to the plant studies that are loaded. It can include comments and all that. The studytreelist will be read from the load_synth_extract file. Other variables in the conf could be overridden here.
"""

import load_synth_extract

studytreelist=["1022_1967",
               "194_2284",#early and nymphaeles
               "562_817",
               "424_532",
               "1916_3902",
               "588_878",
               "2610_6117", #malpighiales
               "826_1584",#rosaceae
               "1133_5647",
               "2539_5465",
               "713_1284", #Lamiales
               "2645_6165",#Menispermaceae
               "2644_6164",#Ranunculales
               "2642_6161",#Cayophyllales; not sure if you have a better study here)
               "2140_4483",#Annonaceae
               "2648_6171",#Marchantiales
               "1118_2226", #Mentheae,lamiaceae
               "650_1147",#Meliaceae, Sapindales
               "2085_4317",#Araceae
               "2044_4212",#Orobanchaceae
               "2626_6142",#Amaranthaceae
               "2624_6139",#Veronica
               "2598_6020",#Boraginaceae
               "2564_5699",#Polystichum
               "2042_4202",#Bartramiaceae
               "2034_4191",#Ruellieae
               "1101_2172",#Rubieae
               "2565_5708",#Ericoideae
               "2641_6160",#Rubiaceae
               "99_5885",#Barnadesioideae
               "93_1411",#Symplocos
               "30_2281",#Illicium
               "36_36",#Dendropanax
               "37_5871",#Rhus
               "41_1396",#Feddea
               "50_1397",#Anagallis
               "59_5731",#Aristolochiaceae
               "73_5787",#Passiflora
               "80_5881",#Rhododendron
               "81_5863",#Pinus
               "82_5792",#Campanula
               "88_5848",#Erodium
               #"2608_6116", #saxifrigales
               #"231_5505", #caryoph SOME SORT OF LOADING PROBLEM
               "14_12",
               "180_794",
               #"574_840", upload problems
               "576_849",
               "581_859",
               "582_862",
               "598_926",
               "599_927",
               "603_940",
               "704_1266",
               "721_1298",
               "723_1300",
               #"724_3212", upload problems
               "921_4103",
               "1300_2613",
               #"1302_2616", make for weird euphyllophyta
               "915_1802",
               "915_1803",
               "9_1",
               "142_38",
               "21_37",
               "57_777",
               "58_775",
               "72_801",
               "75_1743",
               "535_768",
               "61_816",
               "284_185",
               "2546_5493",
               "1086_2111",
               "283_184",
               "54_949",
               "1116_2217",
               "225_5991",#deep plants
               "1867_3766", #cycads
               "412_2166",#conifers
               "2046_5928" #Trebouxiophyceae, Chlorophyta
               ]

if __name__ == "__main__":
	from stephen_desktop_conf import *

	synthottolid="10218"

	print "loading synthottolid:",synthottolid
	print "loading studytreelist:",studytreelist

	load_synth_extract.run(dott,dload,studyloc,studytreelist,javapre,
				 treemloc,generallogfileloc,dsynth,synthottolid,treefn)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Fri Dec 28 13:12:58 2012

@author: Preston
"""
import Player, Pile, socket, threading, pickle
import time, random, Queue
from OptionsParser import config_parser
from Card import Card

class Server(object):
    def __init__(self, rule_handler, player_handler, port = None,
                 ip_address = None):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.bind((ip_address or socket.getfqdn(), int(port)))
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server.listen(5)
        self.clients = {}
        self._server_running = threading.Event()
        threading.Thread(name = 'Connection Thread',
                         target = (self.get_connections),
                         args = (self.server,)).start()
        self.player_handler = player_handler
        self.rule_handler = rule_handler
        self.deck = Pile.Pile()
        self.pile = Pile.Pile()
        self.update_deck()
        self._main_event = threading.Event()

    def is_running(self):
        "Returns True if the server is running, or False if it is not"
        return not self._server_running.is_set()

    def get_connections(self, server):
        "Constantly get connections and adds them to clients"
        while self.is_running():
            try:
                server.settimeout(None)
                client, address = server.accept()
                if address in self.clients:
                    client.send('Same ip as other user! Connection aborted')
                    client.close()
                    continue
                client.send('Connection Established')
                print 'Connection Established with {}'.format(
                ':'.join((address[0], str(address[1]))))
                if client not in self.clients and (
                config_parser.getint('Players', 'max_players') > len(
                self.clients)):
                    player = self.authorise_player(client)
                    self.clients.update({client: player})
                    self.player_handler.add_player(player)
                    Server.send(player, client)
                    self.receive(client) #Pile request
                    Server.send(self.pile.cards, client)
                    if not self.player_handler.current_player:
                        self.player_handler.next_player()
                    threading.Thread(name = 'Listening to {0} at {1}'.format(
                        player.name, address), target = self.listen,
                        args = (client, player)).start()
                else:
                    Server.send('', client)
                    client.close()
            except (socket.timeout, socket.error):
                try:
                    client.close()
                    if client in self.clients:
                        self.clients.pop(client)
                except (socket.error, UnboundLocalError):
                    pass

    def authorise_player(self, client):
        """Generates and then authorises the player with the information
        provided by the client (It has to send the name). It then returns
        the player if it succeeds, or raises socket.error"""
        player_name = self.receive(client)[0]
        if any(player.name == player_name for player in (
        self.player_handler.players)):
            print '{} is already taken!'.format(player_name)
            raise socket.error
        print player_name + ' Joined!'
        self.send_all('[{} Joined!]'.format(player_name))
        return Player.Player(player_name, hand = self.deck.remove(0,
                               config_parser.getint('Cards', 'hand_size')))

    @staticmethod
    def send(data, client):
        "Send some data (pickled) to the client"
        pickled = pickle.dumps(data) +  ';'
        client.send(str(len(pickled)) + ':')

        total = 0
        while total < len(pickled):
            sent = client.send(pickled[total:])
            if sent == 0:
                raise socket.error("Connection Broken")
            else:
                total += sent

    def send_all(self, data):
        for client in self.clients:
            Server.send(data, client)

    def receive(self, client, timeout = 10):
        """Get a response from the server and de-pickles it. If the actual
        message takes too long (longer than 10 seconds or timeout if given)
        then socket.error is raised"""
        def get_message(client, timeout, message = None):
            if not message:
                received = client.recv(2048).split(':', 1)
            else:
                received = message.split(':', 1)
            length = received[0]
            if not length:
                raise socket.error
            data = received[1]
            client.settimeout(timeout)
            while len(data) < int(length):
                message = client.recv(2048)
                if not message:
                    raise socket.error
                data += message
            client.settimeout(None)
            first, second = data.rsplit(';', 1)
            first = pickle.loads(first)
            if first == '':
                raise socket.error('The Client closed the connection!')
            data = []
            data.append(first)
            if second:
                print "Message was too long!"
                data.extend(get_message(client, timeout = 10,
                                         message = second))
            return data
        return get_message(client, timeout)

    def constantly_receive(self, client, queue):
        """Constantly receives messages from the client and passes it into
        the given queue to be handled by the server"""
        while self.is_running() and client in self.clients:
            try:
                data = self.receive(client, timeout = 1)
                for item in data:
                    queue.put(item)
            except socket.timeout:
                continue
            except socket.error:
                if client in self.clients:
                    self.disconnect(client)
                break

    def disconnect(self, client):
        player = self.clients.pop(client)
        try:
            Server.send('', client)
        except (socket.error, socket.timeout):
            pass
        finally:
            client.close()
            self.deck.add(player.hand)
            self.deck.shuffle()
            if player is self.player_handler.current_player:
                self._main_event.set() #Stop the thread
            self.player_handler.remove_player(player)
            self.send_all('{} disconnected'.format(player.name))
            self.send_all(self.player_handler.players)
            #Just added this, hope it works aight
            print '{} disconnected'.format(player.name)

    def shutdown(self):
        "Turns off the server, closing all the connections in clients"
        self._server_running.set()
        clients = []
        clients.extend(self.clients.iterkeys())
        for client in clients:
            self.disconnect(client)
        self.server.shutdown(socket.SHUT_RDWR)
        self.server.close()

    def get_client(self, player):
        """Returns the client connected to the player instance"""
        reversed_map = {player: client for client, player in (
        self.clients.iteritems())}
        return reversed_map.get(player)

    def punish(self, player, penalty_num = config_parser.getint(
               'Punishment','penalty_num'), reason = None, cards = None):
        """The player is punished by being given penalty_num of cards.
        Raises a KeyError if the player is not in the list of players.
        If cards is given, then it appends that to the cards so that they are
        sent as one batch"""
        if cards is None:
            cards = ()
        if reason is None:
            reason = random.choice(config_parser.get(
                    'Punishment', 'default_phrases').split(';')).format(
                    name = player.name, random_card = (
                    random.choice(player.hand).rank + ' of ' + (
                    random.choice(player.hand).suit)))
        self.send_all('Penalty card for {} '.format(player.name) + reason)
        if player in self.player_handler.players:
            self.update_deck()
            cards = self.deck.remove(0, penalty_num) + cards
            Server.send(cards, self.get_client(player))
            for card in cards:
                player.add_card(card)
        else:
            raise KeyError, player.name + ' is not in players!'

    def constantly_punish(self, player, penalty_num, punish_timer, event):
        """Constantly punishes the player the number of cards in penalty_num
        until the event is set. If no event is given, uses the main_event
        (the one that is set when the current player finally plays a card)"""
        while (not event.wait(punish_timer) and
        player in self.player_handler.players):
            self.punish(player, penalty_num)
        event.clear()

    def update_deck(self):
        "Checks if the deck is getting low (<= 26), and adds card if it is"
        if len(self.deck.cards) <= 26:
            suits = []
            for suit in (
            config_parser.get('Cards', suit_type).split() for suit_type in (
            option for option in config_parser.options('Cards') \
            if option.endswith('suit'))):
                suits.extend(suit)
            ranks = config_parser.get('Cards', 'ranks').split()
            new_deck = (Pile.Pile([Card(suit, rank) for rank in \
            ranks for suit in suits]))
            self.deck.add(new_deck.cards)
            self.deck.shuffle()
            self.deck.update_top_card()

    def main_loop(self, timer = config_parser.getint('Punishment', 'timer'),
                  penalty_num = config_parser.getint('Punishment',
                  'penalty_num')):
        """Main loop that should be called after initalisation. timer is the
        amount of time a player has to play before he is punished for taking
        too long and penalty num is the amount he is punished by"""
        while self.is_running():
            if self.player_handler.current_player:
                print 'Current Player: {}'.format(
                    self.player_handler.get_current_player())
                if self.constantly_punish(self.player_handler.current_player,
                                    penalty_num, timer,
                                    self._main_event):
                #Wait for a player to play
                    self.player_handler.next_player()
                    self.player_handler.update_order()
                    self.update_deck()
                    self.deck.update_top_card()
            else:
                time.sleep(1)
        self._main_event.set()

    def listen(self, client, player):
        """Constantly listens for messages from the respective client.
        Depending on the input, the player is either sent a card or nothing
        """
        message_queue = Queue.Queue()
        get_message_thread = threading.Thread(name = (
            'Getting messages from {}'.format(player.name)), target = (
            self.constantly_receive), args = (client, message_queue))
        get_message_thread.start()
        self.send(self.player_handler.players, client)#Added this too
        try:
            while self.is_running():
                try:
                    data = message_queue.get(timeout = 1)
                except Queue.Empty:
                    continue
                if not self.is_running():
                    #else it'd sent to a broken socket
                    break
                for code in (
                self.rule_handler.check_rules(data, config_parser.get(
                'Rules','allow_no_trigger'))):
                    threading.Thread(name = 'A Rule thread',
                                     target = lambda: code).start()
                self.handle_data(data, player)
        except (socket.error, socket.timeout):
            if self.is_running() and client in self.clients:
                self.disconnect(client)
            return

    def handle_data(self, data, player):
        """Using data as in the player input and the current game conditions,
        this function responds to the player's action, usually by sending
        either a card or a message"""
        if type(data) == str:
            self.send_all('{player}: {message}'.format(
            player = player.name, message = data))
            print '{player}: {message}'.format(
            player = player.name, message = data)
            self.rule_handler.said(data, player,
                         PH.get_player_distance(player))
        elif player == self.player_handler.current_player and (
        type(data) == Card):
            card_index = player.get_card_index(data)
            if card_index is not None:#Card index could be 0
                print '{player} attempted to play {card}'.format(
                        player = player.name,
                        card = data.rank + ' of ' + data.suit)
                card = player.get_card(card_index)
                if any((card.suit == self.pile.top_card.suit,
                card.rank == self.pile.top_card.rank,
                not self.pile.top_card.suit,
                not self.pile.top_card.rank)):
                    print ('{card} is now the top card'.format(
                    card = card.rank + ' of ' + card.suit))
                    self._main_event.set()
                    self.pile.add((card,))
                    self.send_all(card)
                    self.rule_handler.played(card, player,
                                   PH.get_player_distance(
                                   player))
                else:
                    print (
                    '{card} was given back to {player}'.format(
                    player = player.name, card = card.rank + (
                    ' of ' + card.suit)))
                    assert type(data) == Card
                    if not data.rank and not data.suit:
                        data = None
                    else:
                        data = (data,)
                    self.punish(player, cards = data, reason = (
                    "for not playing a valid card"))
            elif not data.rank and not data.suit:
                self._main_event.set()
                self.punish(player)#They draw a card
            else:
                self._main_event.set()
                self.send_all(
                '{} was caught with a fake card!'.format(
                player.name))
                print '{} was caught with a fake card!'.format(
                player.name)
                raise socket.error

        else:
            assert type(data) == Card
            if not data.rank and not data.suit:
                data = None
            else:
                data = (data,)
            self.punish(player, cards = data, reason = (
            'for playing out of turn!'))

if __name__ == '__main__':
    from PlayerHandler import PlayerHandler
    from RuleHandler import RuleHandler, RuleGenerator
    from Rule import Rule
    rules = [Rule('Test Rule', None, 'Set a to exit()')]
    RH = RuleHandler(rules)
    PH = PlayerHandler([])
    ip = config_parser.get('Misc.', 'ip')
    port = config_parser.getint('Misc.', 'port')
    players = PH.players
    server = Server(RH, PH, port, ip)

    for variable in ('RH', 'PH', 'server.deck', 'server.pile'):
        RuleGenerator.WHITE_LIST.add(variable)
    print 'Server at {}:{}'.format(ip or 'localhost', port)
    MAIN_THREAD = threading.Thread(target = server.main_loop)
    print "Type 'MAIN_THREAD.start()' to start the game!"

    def shutdown():
        server.shutdown()
        exit()

    print "Type 'shutdown()' to shutdown the server; DON'T exit the prompt"
    print
    print [var for var in vars(server).keys() if not var.startswith('_')]
    print "I don't think we need clients in here....:P"
<commit_msg>Added the clients variable for the server runtime<commit_after># -*- coding: utf-8 -*-
"""
Created on Fri Dec 28 13:12:58 2012

@author: Preston
"""
import Player, Pile, socket, threading, pickle
import time, random, Queue
from OptionsParser import config_parser
from Card import Card

class Server(object):
    def __init__(self, rule_handler, player_handler, port = None,
                 ip_address = None):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.bind((ip_address or socket.getfqdn(), int(port)))
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server.listen(5)
        self.clients = {}
        self._server_running = threading.Event()
        threading.Thread(name = 'Connection Thread',
                         target = (self.get_connections),
                         args = (self.server,)).start()
        self.player_handler = player_handler
        self.rule_handler = rule_handler
        self.deck = Pile.Pile()
        self.pile = Pile.Pile()
        self.update_deck()
        self._main_event = threading.Event()

    def is_running(self):
        "Returns True if the server is running, or False if it is not"
        return not self._server_running.is_set()

    def get_connections(self, server):
        "Constantly get connections and adds them to clients"
        while self.is_running():
            try:
                server.settimeout(None)
                client, address = server.accept()
                if address in self.clients:
                    client.send('Same ip as other user! Connection aborted')
                    client.close()
                    continue
                client.send('Connection Established')
                print 'Connection Established with {}'.format(
                ':'.join((address[0], str(address[1]))))
                if client not in self.clients and (
                config_parser.getint('Players', 'max_players') > len(
                self.clients)):
                    player = self.authorise_player(client)
                    self.clients.update({client: player})
                    self.player_handler.add_player(player)
                    Server.send(player, client)
                    self.receive(client) #Pile request
                    Server.send(self.pile.cards, client)
                    if not self.player_handler.current_player:
                        self.player_handler.next_player()
                    threading.Thread(name = 'Listening to {0} at {1}'.format(
                        player.name, address), target = self.listen,
                        args = (client, player)).start()
                else:
                    Server.send('', client)
                    client.close()
            except (socket.timeout, socket.error):
                try:
                    client.close()
                    if client in self.clients:
                        self.clients.pop(client)
                except (socket.error, UnboundLocalError):
                    pass

    def authorise_player(self, client):
        """Generates and then authorises the player with the information
        provided by the client (It has to send the name). It then returns
        the player if it succeeds, or raises socket.error"""
        player_name = self.receive(client)[0]
        if any(player.name == player_name for player in (
        self.player_handler.players)):
            print '{} is already taken!'.format(player_name)
            raise socket.error
        print player_name + ' Joined!'
        self.send_all('[{} Joined!]'.format(player_name))
        return Player.Player(player_name, hand = self.deck.remove(0,
                               config_parser.getint('Cards', 'hand_size')))

    @staticmethod
    def send(data, client):
        "Send some data (pickled) to the client"
        pickled = pickle.dumps(data) +  ';'
        client.send(str(len(pickled)) + ':')

        total = 0
        while total < len(pickled):
            sent = client.send(pickled[total:])
            if sent == 0:
                raise socket.error("Connection Broken")
            else:
                total += sent

    def send_all(self, data):
        for client in self.clients:
            Server.send(data, client)

    def receive(self, client, timeout = 10):
        """Get a response from the server and de-pickles it. If the actual
        message takes too long (longer than 10 seconds or timeout if given)
        then socket.error is raised"""
        def get_message(client, timeout, message = None):
            if not message:
                received = client.recv(2048).split(':', 1)
            else:
                received = message.split(':', 1)
            length = received[0]
            if not length:
                raise socket.error
            data = received[1]
            client.settimeout(timeout)
            while len(data) < int(length):
                message = client.recv(2048)
                if not message:
                    raise socket.error
                data += message
            client.settimeout(None)
            first, second = data.rsplit(';', 1)
            first = pickle.loads(first)
            if first == '':
                raise socket.error('The Client closed the connection!')
            data = []
            data.append(first)
            if second:
                print "Message was too long!"
                data.extend(get_message(client, timeout = 10,
                                         message = second))
            return data
        return get_message(client, timeout)

    def constantly_receive(self, client, queue):
        """Constantly receives messages from the client and passes it into
        the given queue to be handled by the server"""
        while self.is_running() and client in self.clients:
            try:
                data = self.receive(client, timeout = 1)
                for item in data:
                    queue.put(item)
            except socket.timeout:
                continue
            except socket.error:
                if client in self.clients:
                    self.disconnect(client)
                break

    def disconnect(self, client):
        player = self.clients.pop(client)
        try:
            Server.send('', client)
        except (socket.error, socket.timeout):
            pass
        finally:
            client.close()
            self.deck.add(player.hand)
            self.deck.shuffle()
            if player is self.player_handler.current_player:
                self._main_event.set() #Stop the thread
            self.player_handler.remove_player(player)
            self.send_all('{} disconnected'.format(player.name))
            self.send_all(self.player_handler.players)
            #Just added this, hope it works aight
            print '{} disconnected'.format(player.name)

    def shutdown(self):
        "Turns off the server, closing all the connections in clients"
        self._server_running.set()
        clients = []
        clients.extend(self.clients.iterkeys())
        for client in clients:
            self.disconnect(client)
        self.server.shutdown(socket.SHUT_RDWR)
        self.server.close()

    def get_client(self, player):
        """Returns the client connected to the player instance"""
        reversed_map = {player: client for client, player in (
        self.clients.iteritems())}
        return reversed_map.get(player)

    def punish(self, player, penalty_num = config_parser.getint(
               'Punishment','penalty_num'), reason = None, cards = None):
        """The player is punished by being given penalty_num of cards.
        Raises a KeyError if the player is not in the list of players.
        If cards is given, then it appends that to the cards so that they are
        sent as one batch"""
        if cards is None:
            cards = ()
        if reason is None:
            reason = random.choice(config_parser.get(
                    'Punishment', 'default_phrases').split(';')).format(
                    name = player.name, random_card = (
                    random.choice(player.hand).rank + ' of ' + (
                    random.choice(player.hand).suit)))
        self.send_all('Penalty card for {} '.format(player.name) + reason)
        if player in self.player_handler.players:
            self.update_deck()
            cards = self.deck.remove(0, penalty_num) + cards
            Server.send(cards, self.get_client(player))
            for card in cards:
                player.add_card(card)
        else:
            raise KeyError, player.name + ' is not in players!'

    def constantly_punish(self, player, penalty_num, punish_timer, event):
        """Constantly punishes the player the number of cards in penalty_num
        until the event is set. If no event is given, uses the main_event
        (the one that is set when the current player finally plays a card)"""
        while (not event.wait(punish_timer) and
        player in self.player_handler.players):
            self.punish(player, penalty_num)
        event.clear()

    def update_deck(self):
        "Checks if the deck is getting low (<= 26), and adds card if it is"
        if len(self.deck.cards) <= 26:
            suits = []
            for suit in (
            config_parser.get('Cards', suit_type).split() for suit_type in (
            option for option in config_parser.options('Cards') \
            if option.endswith('suit'))):
                suits.extend(suit)
            ranks = config_parser.get('Cards', 'ranks').split()
            new_deck = (Pile.Pile([Card(suit, rank) for rank in \
            ranks for suit in suits]))
            self.deck.add(new_deck.cards)
            self.deck.shuffle()
            self.deck.update_top_card()

    def main_loop(self, timer = config_parser.getint('Punishment', 'timer'),
                  penalty_num = config_parser.getint('Punishment',
                  'penalty_num')):
        """Main loop that should be called after initalisation. timer is the
        amount of time a player has to play before he is punished for taking
        too long and penalty num is the amount he is punished by"""
        while self.is_running():
            if self.player_handler.current_player:
                print 'Current Player: {}'.format(
                    self.player_handler.get_current_player())
                if self.constantly_punish(self.player_handler.current_player,
                                    penalty_num, timer,
                                    self._main_event):
                #Wait for a player to play
                    self.player_handler.next_player()
                    self.player_handler.update_order()
                    self.update_deck()
                    self.deck.update_top_card()
            else:
                time.sleep(1)
        self._main_event.set()

    def listen(self, client, player):
        """Constantly listens for messages from the respective client.
        Depending on the input, the player is either sent a card or nothing
        """
        message_queue = Queue.Queue()
        get_message_thread = threading.Thread(name = (
            'Getting messages from {}'.format(player.name)), target = (
            self.constantly_receive), args = (client, message_queue))
        get_message_thread.start()
        self.send(self.player_handler.players, client)#Added this too
        try:
            while self.is_running():
                try:
                    data = message_queue.get(timeout = 1)
                except Queue.Empty:
                    continue
                if not self.is_running():
                    #else it'd sent to a broken socket
                    break
                for code in (
                self.rule_handler.check_rules(data, config_parser.get(
                'Rules','allow_no_trigger'))):
                    threading.Thread(name = 'A Rule thread',
                                     target = lambda: code).start()
                self.handle_data(data, player)
        except (socket.error, socket.timeout):
            if self.is_running() and client in self.clients:
                self.disconnect(client)
            return

    def handle_data(self, data, player):
        """Using data as in the player input and the current game conditions,
        this function responds to the player's action, usually by sending
        either a card or a message"""
        if type(data) == str:
            self.send_all('{player}: {message}'.format(
            player = player.name, message = data))
            print '{player}: {message}'.format(
            player = player.name, message = data)
            self.rule_handler.said(data, player,
                         PH.get_player_distance(player))
        elif player == self.player_handler.current_player and (
        type(data) == Card):
            card_index = player.get_card_index(data)
            if card_index is not None:#Card index could be 0
                print '{player} attempted to play {card}'.format(
                        player = player.name,
                        card = data.rank + ' of ' + data.suit)
                card = player.get_card(card_index)
                if any((card.suit == self.pile.top_card.suit,
                card.rank == self.pile.top_card.rank,
                not self.pile.top_card.suit,
                not self.pile.top_card.rank)):
                    print ('{card} is now the top card'.format(
                    card = card.rank + ' of ' + card.suit))
                    self._main_event.set()
                    self.pile.add((card,))
                    self.send_all(card)
                    self.rule_handler.played(card, player,
                                   PH.get_player_distance(
                                   player))
                else:
                    print (
                    '{card} was given back to {player}'.format(
                    player = player.name, card = card.rank + (
                    ' of ' + card.suit)))
                    assert type(data) == Card
                    if not data.rank and not data.suit:
                        data = None
                    else:
                        data = (data,)
                    self.punish(player, cards = data, reason = (
                    "for not playing a valid card"))
            elif not data.rank and not data.suit:
                self._main_event.set()
                self.punish(player)#They draw a card
            else:
                self._main_event.set()
                self.send_all(
                '{} was caught with a fake card!'.format(
                player.name))
                print '{} was caught with a fake card!'.format(
                player.name)
                raise socket.error

        else:
            assert type(data) == Card
            if not data.rank and not data.suit:
                data = None
            else:
                data = (data,)
            self.punish(player, cards = data, reason = (
            'for playing out of turn!'))

if __name__ == '__main__':
    from PlayerHandler import PlayerHandler
    from RuleHandler import RuleHandler, RuleGenerator
    from Rule import Rule
    rules = [Rule('Test Rule', None, 'Set a to exit()')]
    RH = RuleHandler(rules)
    PH = PlayerHandler([])
    ip = config_parser.get('Misc.', 'ip')
    port = config_parser.getint('Misc.', 'port')
    players = PH.players
    server = Server(RH, PH, port, ip)
    clients = server.clients

    for variable in ('RH', 'PH', 'server.deck', 'server.pile'):
        RuleGenerator.WHITE_LIST.add(variable)
    print 'Server at {}:{}'.format(ip or 'localhost', port)
    MAIN_THREAD = threading.Thread(target = server.main_loop)
    print "Type 'MAIN_THREAD.start()' to start the game!"

    def shutdown():
        server.shutdown()
        exit()

    print "Type 'shutdown()' to shutdown the server; DON'T exit the prompt"
    print
    print [var for var in vars(server).keys() if not var.startswith('_')]
    print "I don't think we need clients in here....:P"
<|endoftext|>


<commit_before>r"""
Definition
----------

This model calculates the scattered intensity of a two-component system
using the Teubner-Strey model. Unlike :ref:`dab` this function generates
a peak.

.. math::

    I(q) = \frac{1}{a_2 + c_1 q^2 + c_2 q^4} + \text{background}

The parameters $a_2$, $c_1$ and $c_2$ can be used to determine the
characteristic domain size $d$,

.. math::

    d = 2\pi\left[\frac12\left(\frac{a_2}{c_2}\right)^{1/2}
                  + \frac14\frac{c_1}{c_2}\right]^{-1/2}

and the correlation length $\xi$,

.. math::

    \xi = \left[\frac12\left(\frac{a_2}{c_2}\right)^{1/2}
                  - \frac14\frac{c_1}{c_2}\right]^{-1/2}

For 2D data, scattering intensity is calculated in the same way as 1D,
where the $q$ vector is defined as

.. math::

    q = \sqrt{q_x^2 + q_y^2}

.. image:: img/image185.jpg

    1D plot using the default values (w/200 data point).

Reference
---------

M Teubner, R Strey, *J. Chem. Phys.*, 87 (1987) 3195

K V Schubert, R Strey, S R Kline and E W Kaler,
*J. Chem. Phys.*, 101 (1994) 5343

"""

import numpy as np
from numpy import inf, sqrt

name = "teubner_strey"
title = "Teubner-Strey model of microemulsions"
description = """\
   Scattering model class for the Teubner-Strey model given by
    Provide F(x) = 1/( a2 + c1 q^2+  c2 q^4 ) + background
    a2>0, c1<0, c2>0, 4 a2 c2 - c1^2 > 0
"""
category = "shape-independent"

#             ["name", "units", default, [lower, upper], "type","description"],
parameters = [["a2", "", 0.1, [0, inf], "",
               "a2"],
              ["c1", "1e-6/Ang^2", -30., [-inf, 0], "",
               "c1"],
              ["c2", "Ang", 5000., [0, inf], "volume",
               "c2"],
             ]

def form_volume(radius):
    return 1.0

def Iq(q, a2, c1, c2):
    return 1. / np.polyval([c2, c1, a2], q**2)
Iq.vectorized = True  # Iq accepts an array of Q values

def Iqxy(qx, qy, a2, c1, c2):
    return Iq(sqrt(qx**2 + qy**2), a2, c1, c2)
Iqxy.vectorized = True  # Iqxy accepts arrays of Qx, Qy values

# ER defaults to 0.0

# VR defaults to 1.0

demo = dict(scale=1, background=0, a2=0.1, c1=-30.0, c2=5000.0)
oldname = "TeubnerStreyModel"
oldpars = dict(a2='scale')
<commit_msg>add test case for Teubner-Strey model<commit_after>r"""
Definition
----------

This model calculates the scattered intensity of a two-component system
using the Teubner-Strey model. Unlike :ref:`dab` this function generates
a peak.

.. math::

    I(q) = \frac{1}{a_2 + c_1 q^2 + c_2 q^4} + \text{background}

The parameters $a_2$, $c_1$ and $c_2$ can be used to determine the
characteristic domain size $d$,

.. math::

    d = 2\pi\left[\frac12\left(\frac{a_2}{c_2}\right)^{1/2}
                  + \frac14\frac{c_1}{c_2}\right]^{-1/2}

and the correlation length $\xi$,

.. math::

    \xi = \left[\frac12\left(\frac{a_2}{c_2}\right)^{1/2}
                  - \frac14\frac{c_1}{c_2}\right]^{-1/2}

For 2D data, scattering intensity is calculated in the same way as 1D,
where the $q$ vector is defined as

.. math::

    q = \sqrt{q_x^2 + q_y^2}

.. image:: img/image185.jpg

    1D plot using the default values (w/200 data point).

Reference
---------

M Teubner, R Strey, *J. Chem. Phys.*, 87 (1987) 3195

K V Schubert, R Strey, S R Kline and E W Kaler,
*J. Chem. Phys.*, 101 (1994) 5343

"""

import numpy as np
from numpy import inf, sqrt

name = "teubner_strey"
title = "Teubner-Strey model of microemulsions"
description = """\
   Scattering model class for the Teubner-Strey model given by
    Provide F(x) = 1/( a2 + c1 q^2+  c2 q^4 ) + background
    a2>0, c1<0, c2>0, 4 a2 c2 - c1^2 > 0
"""
category = "shape-independent"

#             ["name", "units", default, [lower, upper], "type","description"],
parameters = [["a2", "", 0.1, [0, inf], "",
               "a2"],
              ["c1", "1e-6/Ang^2", -30., [-inf, 0], "",
               "c1"],
              ["c2", "Ang", 5000., [0, inf], "volume",
               "c2"],
             ]

def form_volume(radius):
    return 1.0

def Iq(q, a2, c1, c2):
    return 1. / np.polyval([c2, c1, a2], q**2)
Iq.vectorized = True  # Iq accepts an array of Q values

def Iqxy(qx, qy, a2, c1, c2):
    return Iq(sqrt(qx**2 + qy**2), a2, c1, c2)
Iqxy.vectorized = True  # Iqxy accepts arrays of Qx, Qy values

# ER defaults to 0.0

# VR defaults to 1.0

demo = dict(scale=1, background=0, a2=0.1, c1=-30.0, c2=5000.0)
oldname = "TeubnerStreyModel"
oldpars = dict(a2='scale')

tests = [[{}, 0.2, 0.144927536232]]
<|endoftext|>


<commit_before># encoding: utf-8

"""MongoDB database connection extension."""

import re

from webob import request
from bson import json_util
from pymongo import MongoClient
from pymongo.errors import ConfigurationError
from gridfs.grid_file import GridOut

from .model import Model
from .resource import MongoDBResource
from .collection import MongoDBCollection
from .grid import render_grid_file

__all__ = ['Model', 'MongoDBResource', 'MongoDBCollection', 'MongoDBConnection']

log = __import__('logging').getLogger(__name__)

_safe_uri_replace = re.compile(r'(\w+)://(\w+):(?P<password>[^@]+)@')

class MongoDBConnection(object):
	"""WebCore database extension connector for MongoDB databases.

	This tiny class performs the work needed to populate the WebCore context with a MonogoDB database (or connection
	if no default database is provided) on startup, using `pymongo`. In addition to performing initial configuration,
	this extension adapts
	"""

	__slots__ = ('__name__', 'uri', 'config', 'client', 'db', 'alias', 'minimum')

	provides = {'mongodb'}

	def __init__(self, uri, alias=None, minimum_version=None, **config):
		"""Prepare MongoDB client configuration.

		The only required configuration option (passed positionally or by keyword) is `uri`, specifying the host to
		connect to and optionally client credentials (username, password), default database, optional minimum server
		version defined as a version tuple, and additional options.  The minimum version will be checked immeiately
		upon startup.

		Extraneous keyword arguments will be stored and passed through to the `MongoClient` class instantiated on
		startup.
		"""
		self.uri = uri
		self.client = None
		self.db = None
		self.minimum = minimum_version
		self.alias = alias

		# Configure a few of our own defaults here, usually because we compare the value somewhere.
		config.setdefault('event_listeners', [])  # For logging purposes, we add some of our own handlers.

		self.config = config

	def start(self, context):
		name = self.alias or self.__name__  # Either we were configured with an explicit name, or the DB ext infers.

		request.json = json_util

		log.info("Connecting context.db.{name} to MongoDB database.".format(name=name), extra=dict(
				uri = _safe_uri_replace.sub(r'\1://\2@', self.uri),
				config = self.config,
			))

		client = self.client = MongoClient(self.uri, **self.config)

		if self.minimum:
			version = tuple(client.server_info()['version'].split('.'))
			if version < self.minimum:
				raise RuntimeError("Unsupported MongoDB server version: " + ".".join(version))

		try:
			db = self.db = client.get_default_database()
		except ConfigurationError:
			db = self.db = None

		if self.config.get('connect', True):
			pass  # Log extra details about the connection here.

		context.db[name] = db if db is not None else client

		if GridOut not in context.view:  # Register the GridFS handler if not already present.
			context.view.register(GridOut, render_grid_file)

	def stop(self, context):
		self.client.close()
		del context.db[self.alias or self.__name__]

<commit_msg>Removed extraneous imports.<commit_after># encoding: utf-8

"""MongoDB database connection extension."""

import re

from webob import request
from bson import json_util
from pymongo import MongoClient
from pymongo.errors import ConfigurationError
from gridfs.grid_file import GridOut

from .grid import render_grid_file

log = __import__('logging').getLogger(__name__)

_safe_uri_replace = re.compile(r'(\w+)://(\w+):(?P<password>[^@]+)@')

class MongoDBConnection(object):
	"""WebCore database extension connector for MongoDB databases.

	This tiny class performs the work needed to populate the WebCore context with a MonogoDB database (or connection
	if no default database is provided) on startup, using `pymongo`. In addition to performing initial configuration,
	this extension adapts
	"""

	__slots__ = ('__name__', 'uri', 'config', 'client', 'db', 'alias', 'minimum')

	provides = {'mongodb'}

	def __init__(self, uri, alias=None, minimum_version=None, **config):
		"""Prepare MongoDB client configuration.

		The only required configuration option (passed positionally or by keyword) is `uri`, specifying the host to
		connect to and optionally client credentials (username, password), default database, optional minimum server
		version defined as a version tuple, and additional options.  The minimum version will be checked immeiately
		upon startup.

		Extraneous keyword arguments will be stored and passed through to the `MongoClient` class instantiated on
		startup.
		"""
		self.uri = uri
		self.client = None
		self.db = None
		self.minimum = minimum_version
		self.alias = alias

		# Configure a few of our own defaults here, usually because we compare the value somewhere.
		config.setdefault('event_listeners', [])  # For logging purposes, we add some of our own handlers.

		self.config = config

	def start(self, context):
		name = self.alias or self.__name__  # Either we were configured with an explicit name, or the DB ext infers.

		request.json = json_util

		log.info("Connecting context.db.{name} to MongoDB database.".format(name=name), extra=dict(
				uri = _safe_uri_replace.sub(r'\1://\2@', self.uri),
				config = self.config,
			))

		client = self.client = MongoClient(self.uri, **self.config)

		if self.minimum:
			version = tuple(client.server_info()['version'].split('.'))
			if version < self.minimum:
				raise RuntimeError("Unsupported MongoDB server version: " + ".".join(version))

		try:
			db = self.db = client.get_default_database()
		except ConfigurationError:
			db = self.db = None

		if self.config.get('connect', True):
			pass  # Log extra details about the connection here.

		context.db[name] = db if db is not None else client

		if GridOut not in context.view:  # Register the GridFS handler if not already present.
			context.view.register(GridOut, render_grid_file)

	def stop(self, context):
		self.client.close()
		del context.db[self.alias or self.__name__]

<|endoftext|>