<commit_before>#coding:utf-8

'''
    GPU run command:
        THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python cnn.py
        THEANO_FLAGS='cuda.root=/usr/local/cuda'=mode=FAST_RUN,device=gpu,floatX=float32 python cnn.py
    CPU run command:
        python cnn.py
'''

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import *
from keras.utils import np_utils, generic_utils
import random, cPickle, imp, numpy as np

dataset_getter = imp.load_source('dataset', '../../pyvec/pyvec/images/dataset.py')

np.random.seed(1337) # Reproducable results :)

custom_height = 64
custom_width = 64

num_epoch = 40
batch_size = 100
num_classes = 2

num_training_data = 14850
num_validation_data = 1648

# I have already preprocessed the data, if you haven't done this please look at the examples
# provided with pyvec: https://github.com/KeironO/Pyvec/blob/master/examples/loadingImages/loading_images.py
directory = "/home/osheak/datasets/fanfMid/imageFiles/numberedFoldersTest/"

X_train, Y_train, X_val, Y_val = dataset_getter.vectorise(directory,num_classes,custom_height,
                                                                custom_width,num_training_data,num_validation_data)
y_train = np_utils.to_categorical(Y_train, num_classes)
y_val = np_utils.to_categorical(Y_val, num_classes)

model = Sequential()

model.add(Convolution2D(64, 3, 3, 3, border_mode='full'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(65536, 128))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(128, num_classes))
model.add(Activation('softmax'))

rms = Adadelta()
model.compile(loss='categorical_crossentropy', optimizer=rms)

nb_train = len(Y_train)
nb_validation = len(Y_val)
print( 'train samples:',nb_train, 'validation samples:',nb_validation)

best_accuracy = 0.0

for e in range(num_epoch):
    print ('Epoch ', e)
    print ('Training')
    batch_num = len(y_train)/batch_size
    progbar = generic_utils.Progbar(X_train.shape[0])
    for i in range(batch_num):
        train_loss,train_accuracy = model.train(X_train[i*batch_size:(i+1)*batch_size], Y_train[i*batch_size:(i+1)*batch_size], accuracy=True)
        progbar.add(batch_size, values=[("train loss", train_loss), ("train accuracy:", train_accuracy)] )

    print('Valid')
    val_loss,val_accuracy = model.evaluate(X_val, y_val, batch_size=1,show_accuracy=True)

    if best_accuracy < val_accuracy:
        best_accuracy = val_accuracy
<commit_msg>Further implementation of pyvec and a few bugfixes<commit_after>'''
    GPU run command:
        THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python cnn.py
        THEANO_FLAGS='cuda.root=/usr/local/cuda'=mode=FAST_RUN,device=gpu,floatX=float32 python cnn.py
    CPU run command:
        python cnn.py
'''

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import *
from keras.utils import np_utils, generic_utils
from os.path import expanduser
import  imp, numpy as np

dataset_getter = imp.load_source('dataset', '../../pyvec/pyvec/images/dataset.py')

home_directory = expanduser("~")

# Data Parameters
custom_height = 64
custom_width = 64
directory = home_directory + "/datasets/fanf/preProcessed/"
num_classes = 2
num_training_data = 14850
num_validation_data = 1648

# Training Parameters
np.random.seed(1337) # Reproducable results :)
num_epoch = 40
batch_size = 100

# I have already preprocessed the data, if you haven't done this please look at the examples
# provided with pyvec: https://github.com/KeironO/Pyvec/blob/master/examples/loadingImages/loading_images.py
X_train, Y_train, X_val, Y_val = dataset_getter.vectorise(directory,num_classes,custom_height,
                                                                custom_width,num_training_data,num_validation_data)
y_train = np_utils.to_categorical(Y_train, num_classes)
y_val = np_utils.to_categorical(Y_val, num_classes)

model = Sequential()

model.add(Convolution2D(custom_height, 3, 3, 3, border_mode='full'))
model.add(Activation('relu'))
model.add(Convolution2D(custom_height, custom_width, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(65536, 128))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(128, num_classes))
model.add(Activation('softmax'))

rms = Adadelta()
model.compile(loss='categorical_crossentropy', optimizer=rms)

nb_train = len(Y_train)
nb_validation = len(Y_val)
print( 'train samples:',nb_train, 'validation samples:',nb_validation)

best_accuracy = 0.0

for e in range(num_epoch):
    print ('Epoch ', e)
    print ('Training')
    batch_num = len(y_train)/batch_size
    progbar = generic_utils.Progbar(X_train.shape[0])
    for i in range(batch_num):
        train_loss,train_accuracy = model.train(X_train[i*batch_size:(i+1)*batch_size], Y_train[i*batch_size:(i+1)*batch_size], accuracy=True)
        progbar.add(batch_size, values=[("train loss", train_loss), ("train accuracy:", train_accuracy)] )

    print('Valid')
    val_loss,val_accuracy = model.evaluate(X_val, y_val, batch_size=1,show_accuracy=True)

    if best_accuracy < val_accuracy:
        best_accuracy = val_accuracy
<|endoftext|>


<commit_before>def getStream(item):
    streams = []
    text = item.title.lower() + item.snip.lower()
    if 'b.tech' in text:
        streams.append('btech')
    if 'b.arch' in text:
        streams.append('barch')
    if 'm.tech' in text:
        streams.append('mtech')
    if 'dental' in text:
        streams.append('dental')
    return streams

def getBatch(item):
    batch = []
    text = item.title.lower() + item.snip.lower()
    if 'first year' in text or '1st year' in text or '1st semester' in text or '2nd semester' in text:
        batch.append('first year')
    if 'second year' in text or '2nd year' in text or '3rd semester' in text or '4th semester' in text:
        batch.append('second year')
    if 'third year' in text or '3rd year' in text or '5th semester' in text or '6th semester' in text:
        batch.append('third year')
    if 'fourth year' in text or '4th year' in text or '7th semester' in text or '8th semester' in text:
        batch.append('fourth year')
    return batch

# Get keywords from http://www.srmuniv.ac.in
def getKeyWords(item):
    ref = [
           'exam','change in schedule','dates', 'rescheduled','holiday',
           'holidays','timetable','urgent', 'time table', 'schedule',
           'practicals', 'postponed', 'tournaments', 'attendance'
           ] # ADD MORE #  #Sports categoory can also be added
    keyWords = []
    for e in ref:
        if e in (item.title.lower() + item.snip.lower()) and e not in keyWords:
            keyWords.append(e)
    return keyWords

def getPref(item):
    pref = []
    for e in getKeyWords(item):
        pref.append(e)
    for e in getBatch(item):
        pref.append(e)
    for e in getStream(item):
        pref.append(e)
    return pref

# TESTING #
#for e in news:
 #   print str(getBatch(e)) + str(getStream(e)) + str(getKeyWords(e))
<commit_msg>added two functions for match making<commit_after>def getStream(item):
    streams = []
    text = item.title.lower() + item.snip.lower()
    if 'b.tech' in text:
        streams.append('btech')
    if 'b.arch' in text:
        streams.append('barch')
    if 'm.tech' in text:
        streams.append('mtech')
    if 'dental' in text:
        streams.append('dental')
    return streams

def getBatch(item):
    batch = []
    text = item.title.lower() + item.snip.lower()
    if 'first year' in text or '1st year' in text or '1st semester' in text or '2nd semester' in text:
        batch.append('first year')
    if 'second year' in text or '2nd year' in text or '3rd semester' in text or '4th semester' in text:
        batch.append('second year')
    if 'third year' in text or '3rd year' in text or '5th semester' in text or '6th semester' in text:
        batch.append('third year')
    if 'fourth year' in text or '4th year' in text or '7th semester' in text or '8th semester' in text:
        batch.append('fourth year')
    return batch

# Get keywords from http://www.srmuniv.ac.in
def getKeyWords(item):
    ref = [
           'exam','change in schedule','dates', 'rescheduled','holiday',
           'holidays','timetable','urgent', 'time table', 'schedule',
           'practicals', 'postponed', 'tournaments', 'attendance'
           ] # ADD MORE #  #Sports categoory can also be added
    keyWords = []
    for e in ref:
        if e in (item.title.lower() + item.snip.lower()) and e not in keyWords:
            keyWords.append(e)
    return keyWords

def getPref(item):
    pref = []
    for e in getKeyWords(item):
        pref.append(e)
    for e in getBatch(item):
        pref.append(e)
    for e in getStream(item):
        pref.append(e)
    return pref

def newsToEmail(item,eList):
    """
      - item : One news item
      - eList : A list of all person objects
      - return : a list of validated email objects
    """
    IDs = []
    for e in eList:
        if countCommon(item.pref,e.pref) > 0:
            IDs.append(e)
    return IDs

def emailToNews(eObj,newsItems):
    """
      - eObj : an email object
      - newsItems : a list of news objects
      - return : a list of validated news objects
    """
    items = []
    for e in newsItems:
        if countCommon(eobj.pref,e.pref) > 0:
            items.append(e)
    return items

# TESTING #
#for e in news:
 #   print str(getBatch(e)) + str(getStream(e)) + str(getKeyWords(e))
<|endoftext|>


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

import sys
import os
import pickle
import shutil
import commands

class PgInstance(object):
	def __init__(self, path, clean_on_delete=False):
		self.config = {
		'path':os.path.abspath(path),
		'port':None
		}

		self.clean_on_delete = clean_on_delete

		try:
			self.load_conf()
		except IOError,e:
			pass

	def __del__(self):
		if self.clean_on_delete:
			try:
				self.stop()
				self.clean()
			except:
				pass
			print("Cleaning up %s..."%(self))

	def __unicode__(self):
		return u"<PgInstance path='%s' port='%s'>"%(self.config['path'], self.config['port'])

	def __str__(self):
		return "<PgInstance path='%s' port='%s'>"%(self.config['path'], self.config['port'])

	def run_command(self, cmd, expected_status=0):
		print ">>>> %s"%(cmd)
		status,output = commands.getstatusoutput(cmd)
		if status != expected_status:
			raise RuntimeError("Command '%s' failed!"%(cmd))
		return output

	def load_conf(self):
		# try to open the file
		config = pickle.load( open(os.path.join(self.config['path'],'config.pickle')) )
		pg_version = open( os.path.join(self.config['path'], 'PG_VERSION')).read().strip()
		self.config['version'] = pg_version

		self.config.update(config)

	def info(self):
		"""Returns a string of information about this instance"""
		self.is_initialized(True)

		ret = "\n".join(["%s = %s"%(k,v) for k,v in self.config.items()])
		ret += "\nrunning = %s"%(self.is_running())

		return ret

	def is_running(self):
		self.is_initialized(True)
		# Find the socket file
		sockfile = ".s.PGSQL.%s"%(self.config['port'])
		if not os.path.exists( os.path.join(self.config['path'], sockfile)):
			return False
		return True

	def is_initialized(self, throw_exception=False):
		try:
			self.load_conf()
		except IOError,e:
			print e
			if throw_exception:
				raise RuntimeError("ERROR: Directory '%s' is not initialized"%(self.config['path']))
			return False
		return True
		#return os.path.exists(os.path.join(self.config['path'],'config.pickle'))

	def initdb(self, version, port, encoding="UTF-8", locale="en_US.UTF-8", superuser="postgres"):
		# Write config file for ourselves
		config = {
			#'path':self.path,
			'version':version,
			'port':port,
			'encoding':encoding,
			'locale':locale,
			'superuser':superuser,
		}
		self.config.update(config)

		if self.is_initialized() or os.path.exists(self.config['path']):
			raise RuntimeError("ERROR: Cannot initdb, directory '%s' already exists"%(self.config['path']))

		cmd = '/usr/lib/postgresql/%s/bin/pg_ctl -D %s initdb -o "--encoding=%s --locale=%s -U %s"'%(self.config['version'], self.config['path'], self.config['encoding'], self.config['locale'], self.config['superuser'])
		self.run_command(cmd)

		conf_fp = open(os.path.join(self.config['path'],'config.pickle'),'w')
		conf_fp.write(pickle.dumps(self.config))

	def start(self, port=None):
		self.is_initialized(True)

		if port:
			self.config['port'] = port
		cmd = '/usr/lib/postgresql/%s/bin/pg_ctl -w -D %s -l %s/postgresql.log start -o "-k %s --port=%s"'%(self.config['version'], self.config['path'], self.config['path'], self.config['path'], self.config['port'])
		print self.run_command(cmd)

	def stop(self):
		self.is_initialized(True)
		cmd = '/usr/lib/postgresql/%s/bin/pg_ctl -D %s stop -m fast'%(self.config['version'], self.config['path'])
		print self.run_command(cmd)

	def clean(self, confirm=False):
		if self.is_running():
			raise RuntimeError("ERROR: The server is still running, not deleting anything.")
		print "Removing directory '%s'"%(self.config['path'])
		datadir = self.config['path']
		really_delete = False
		if not confirm:
			really_delete = True
		elif confirm and raw_input("Really delete the directory '%s'? (Y/N) "%(datadir)) == 'y':
			really_delete = True
		if really_delete:
			shutil.rmtree(datadir)

	def connect(self, *args, **kwargs):
		import psycopg2
		if not kwargs.has_key('user'):
			kwargs['user'] = self.config['user']
		kwargs['host'] = self.config['path']
		if not kwargs.has_key('port'):
			kwargs['port'] = self.config['port']
		return psycopg2.connect(*args, **kwargs)

	def test_connection(self, *args, **kwargs):
		conn = self.connect(*args, **kwargs)
		cur = conn.cursor()
		cur.execute('select version();')
		return cur.fetchall()

	def get_psql_args(self, database="postgres"):
		self.is_initialized(True)
		return [
			"psql",
			"-h %s"%(self.config['path']),
			"-p %s"%(self.config['port']),
			"-U %s"%(self.config['superuser']),
			"%s"%(database)
			]
<commit_msg>fix psql command line generator for properness<commit_after># -*- coding: utf-8

import sys
import os
import pickle
import shutil
import commands

class PgInstance(object):
	def __init__(self, path, clean_on_delete=False):
		self.config = {
		'path':os.path.abspath(path),
		'port':None
		}

		self.clean_on_delete = clean_on_delete

		try:
			self.load_conf()
		except IOError,e:
			pass

	def __del__(self):
		if self.clean_on_delete:
			try:
				self.stop()
				self.clean()
			except:
				pass
			print("Cleaning up %s..."%(self))

	def __unicode__(self):
		return u"<PgInstance path='%s' port='%s'>"%(self.config['path'], self.config['port'])

	def __str__(self):
		return "<PgInstance path='%s' port='%s'>"%(self.config['path'], self.config['port'])

	def run_command(self, cmd, expected_status=0):
		print ">>>> %s"%(cmd)
		status,output = commands.getstatusoutput(cmd)
		if status != expected_status:
			raise RuntimeError("Command '%s' failed!"%(cmd))
		return output

	def load_conf(self):
		# try to open the file
		config = pickle.load( open(os.path.join(self.config['path'],'config.pickle')) )
		pg_version = open( os.path.join(self.config['path'], 'PG_VERSION')).read().strip()
		self.config['version'] = pg_version

		self.config.update(config)

	def info(self):
		"""Returns a string of information about this instance"""
		self.is_initialized(True)

		ret = "\n".join(["%s = %s"%(k,v) for k,v in self.config.items()])
		ret += "\nrunning = %s"%(self.is_running())

		return ret

	def is_running(self):
		self.is_initialized(True)
		# Find the socket file
		sockfile = ".s.PGSQL.%s"%(self.config['port'])
		if not os.path.exists( os.path.join(self.config['path'], sockfile)):
			return False
		return True

	def is_initialized(self, throw_exception=False):
		try:
			self.load_conf()
		except IOError,e:
			print e
			if throw_exception:
				raise RuntimeError("ERROR: Directory '%s' is not initialized"%(self.config['path']))
			return False
		return True
		#return os.path.exists(os.path.join(self.config['path'],'config.pickle'))

	def initdb(self, version, port, encoding="UTF-8", locale="en_US.UTF-8", superuser="postgres"):
		# Write config file for ourselves
		config = {
			#'path':self.path,
			'version':version,
			'port':port,
			'encoding':encoding,
			'locale':locale,
			'superuser':superuser,
		}
		self.config.update(config)

		if self.is_initialized() or os.path.exists(self.config['path']):
			raise RuntimeError("ERROR: Cannot initdb, directory '%s' already exists"%(self.config['path']))

		cmd = '/usr/lib/postgresql/%s/bin/pg_ctl -D %s initdb -o "--encoding=%s --locale=%s -U %s"'%(self.config['version'], self.config['path'], self.config['encoding'], self.config['locale'], self.config['superuser'])
		self.run_command(cmd)

		conf_fp = open(os.path.join(self.config['path'],'config.pickle'),'w')
		conf_fp.write(pickle.dumps(self.config))

	def start(self, port=None):
		self.is_initialized(True)

		if port:
			self.config['port'] = port
		cmd = '/usr/lib/postgresql/%s/bin/pg_ctl -w -D %s -l %s/postgresql.log start -o "-k %s --port=%s"'%(self.config['version'], self.config['path'], self.config['path'], self.config['path'], self.config['port'])
		print self.run_command(cmd)

	def stop(self):
		self.is_initialized(True)
		cmd = '/usr/lib/postgresql/%s/bin/pg_ctl -D %s stop -m fast'%(self.config['version'], self.config['path'])
		print self.run_command(cmd)

	def clean(self, confirm=False):
		if self.is_running():
			raise RuntimeError("ERROR: The server is still running, not deleting anything.")
		print "Removing directory '%s'"%(self.config['path'])
		datadir = self.config['path']
		really_delete = False
		if not confirm:
			really_delete = True
		elif confirm and raw_input("Really delete the directory '%s'? (Y/N) "%(datadir)) == 'y':
			really_delete = True
		if really_delete:
			shutil.rmtree(datadir)

	def connect(self, *args, **kwargs):
		import psycopg2
		if not kwargs.has_key('user'):
			kwargs['user'] = self.config['user']
		kwargs['host'] = self.config['path']
		if not kwargs.has_key('port'):
			kwargs['port'] = self.config['port']
		return psycopg2.connect(*args, **kwargs)

	def test_connection(self, *args, **kwargs):
		conn = self.connect(*args, **kwargs)
		cur = conn.cursor()
		cur.execute('select version();')
		return cur.fetchall()

	def get_psql_args(self, database="postgres"):
		self.is_initialized(True)
		return [
			"psql",
			"-h", self.config['path'],
			"-p", self.config['port'],
			"-U", self.config['superuser'],
			database
			]
<|endoftext|>


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

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2019 sliptonic <shopinthewoods@gmail.com>               *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

import FreeCAD
import Path
import PathScripts.PathDressup as PathDressup
import PathScripts.PathGeom as PathGeom
import PathScripts.PathLog as PathLog
import PathScripts.PathStock as PathStock
import PathScripts.PathUtil as PathUtil
import PathScripts.PathUtils as PathUtils

from PySide import QtCore

PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule())
#PathLog.trackModule(PathLog.thisModule())

def _vstr(v):
    if v:
        return "(%.2f, %.2f, %.2f)" % (v.x, v.y, v.z)
    return '-'

class DressupPathBoundary(object):

    def __init__(self, obj, base, job):
        obj.addProperty("App::PropertyLink", "Base", "Base", QtCore.QT_TRANSLATE_NOOP("Path_DressupPathBoundary", "The base path to modify"))
        obj.Base = base
        obj.addProperty("App::PropertyLink", "Stock", "Boundary", QtCore.QT_TRANSLATE_NOOP("Path_DressupPathBoundary", "Solid object to be used to limit the generated Path."))
        obj.Stock = PathStock.CreateFromBase(job)
        obj.addProperty("App::PropertyBool", "Inside", "Boundary", QtCore.QT_TRANSLATE_NOOP("Path_DressupPathBoundary", "Determines if Boundary describes an inclusion or exclusion mask."))
        obj.Inside = True

        self.obj = obj
        self.safeHeight = None
        self.clearanceHeight = None

    def __getstate__(self):
        return None
    def __setstate__(self, state):
        return None

    def onDcoumentRestored(self, obj):
        self.obj = obj

    def onDelete(self, obj, args):
        if obj.Base:
            job = PathUtils.findParentJob(obj)
            if job:
                job.Proxy.addOperation(obj.Base, obj)
            if obj.Base.ViewObject:
                obj.Base.ViewObject.Visibility = True
            obj.Base = None
        if obj.Stock:
            obj.Document.removeObject(obj.Stock.Name)
            obj.Stock = None
        return True

    def boundaryCommands(self, obj, begin, end, verticalFeed):
        PathLog.track(_vstr(begin), _vstr(end))
        if end and PathGeom.pointsCoincide(begin, end):
            return []
        cmds = []
        if begin.z < self.safeHeight:
            cmds.append(Path.Command('G1', {'Z': self.safeHeight, 'F': verticalFeed}))
        if begin.z < self.clearanceHeight:
            cmds.append(Path.Command('G0', {'Z': self.clearanceHeight}))
        if end:
            cmds.append(Path.Command('G0', {'X': end.x, 'Y': end.y}))
            if end.z < self.clearanceHeight:
                cmds.append(Path.Command('G0', {'Z': max(self.safeHeight, end.z)}))
            if end.z < self.safeHeight:
                cmds.append(Path.Command('G1', {'Z': end.z, 'F': verticalFeed}))
        return cmds

    def execute(self, obj):
        if not obj.Base or not obj.Base.isDerivedFrom('Path::Feature') or not obj.Base.Path:
            return

        tc = PathDressup.toolController(obj.Base)

        if len(obj.Base.Path.Commands) > 0:
            self.safeHeight = float(PathUtil.opProperty(obj.Base, 'SafeHeight'))
            self.clearanceHeight = float(PathUtil.opProperty(obj.Base, 'ClearanceHeight'))

            boundary = obj.Stock.Shape
            cmd = obj.Base.Path.Commands[0]
            pos = cmd.Placement.Base
            commands = [cmd]
            lastExit = None
            for cmd in obj.Base.Path.Commands[1:]:
                if cmd.Name in PathGeom.CmdMoveAll:
                    edge = PathGeom.edgeForCmd(cmd, pos)
                    inside  = edge.common(boundary).Edges
                    outside = edge.cut(boundary).Edges
                    if not obj.Inside:
                        t = inside
                        inside = outside
                        outside = t
                    # it's really a shame that one cannot trust the sequence and/or
                    # orientation of edges
                    if 1 == len(inside) and 0 == len(outside):
                        PathLog.track(_vstr(pos), _vstr(lastExit), ' + ', cmd)
                        # cmd fully included by boundary
                        if lastExit:
                            commands.extend(self.boundaryCommands(obj, lastExit, pos, tc.VertFeed.Value))
                            lastExit = None
                        commands.append(cmd)
                        pos = PathGeom.commandEndPoint(cmd, pos)
                    elif 0 == len(inside) and 1 == len(outside):
                        PathLog.track(_vstr(pos), _vstr(lastExit), ' - ', cmd)
                        # cmd fully excluded by boundary
                        if not lastExit:
                            lastExit = pos
                        pos = PathGeom.commandEndPoint(cmd, pos)
                    else:
                        PathLog.track(_vstr(pos), _vstr(lastExit), len(inside), len(outside), cmd)
                        # cmd pierces boundary
                        while inside or outside:
                            ie = [e for e in inside if PathGeom.edgeConnectsTo(e, pos)]
                            PathLog.track(ie)
                            if ie:
                                e = ie[0]
                                ptL = e.valueAt(e.LastParameter)
                                flip = PathGeom.pointsCoincide(pos, ptL)
                                newPos = e.valueAt(e.FirstParameter) if flip else ptL
                                # inside edges are taken at this point (see swap of inside/outside
                                # above - so we can just connect the dots ...
                                if lastExit:
                                    commands.extend(self.boundaryCommands(obj, lastExit, pos, tc.VertFeed.Value))
                                    lastExit = None
                                PathLog.track(e, flip)
                                commands.extend(PathGeom.cmdsForEdge(e, flip, False,50,tc.HorizFeed.Value,tc.VertFeed.Value)) # add missing HorizFeed to G2 paths
                                inside.remove(e)
                                pos = newPos
                                lastExit = newPos
                            else:
                                oe = [e for e in outside if PathGeom.edgeConnectsTo(e, pos)]
                                PathLog.track(oe)
                                if oe:
                                    e = oe[0]
                                    ptL = e.valueAt(e.LastParameter)
                                    flip = PathGeom.pointsCoincide(pos, ptL)
                                    newPos = e.valueAt(e.FirstParameter) if flip else ptL
                                    # outside edges are never taken at this point (see swap of
                                    # inside/outside above) - so just move along ...
                                    outside.remove(e)
                                    pos = newPos
                                else:
                                    PathLog.error('huh?')
                                    import Part
                                    Part.show(Part.Vertex(pos), 'pos')
                                    for e in inside:
                                        Part.show(e, 'ei')
                                    for e in outside:
                                        Part.show(e, 'eo')
                                    raise Exception('This is not supposed to happen')
                    #pos = PathGeom.commandEndPoint(cmd, pos)
                else:
                    PathLog.track('no-move', cmd)
                    commands.append(cmd)
            if lastExit:
                commands.extend(self.boundaryCommands(obj, lastExit, None, tc.VertFeed.Value))
                lastExit = None
        else:
            PathLog.warning("No Path Commands for %s" % obj.Base.Label)
            commands = []
        PathLog.track(commands)
        obj.Path = Path.Path(commands)

def Create(base, name='DressupPathBoundary'):
    '''Create(base, name='DressupPathBoundary') ... creates a dressup limiting base's Path to a boundary.'''

    if not base.isDerivedFrom('Path::Feature'):
        PathLog.error(translate('Path_DressupPathBoundary', 'The selected object is not a path')+'\n')
        return None

    obj = FreeCAD.ActiveDocument.addObject('Path::FeaturePython', name)
    job = PathUtils.findParentJob(base)
    obj.Proxy = DressupPathBoundary(obj, base, job)
    job.Proxy.addOperation(obj, base, True)
    return obj
<commit_msg>Path: Add `if edge:` test clause<commit_after># -*- coding: utf-8 -*-

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2019 sliptonic <shopinthewoods@gmail.com>               *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

import FreeCAD
import Path
import PathScripts.PathDressup as PathDressup
import PathScripts.PathGeom as PathGeom
import PathScripts.PathLog as PathLog
import PathScripts.PathStock as PathStock
import PathScripts.PathUtil as PathUtil
import PathScripts.PathUtils as PathUtils

from PySide import QtCore

PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule())
# PathLog.trackModule(PathLog.thisModule())

def _vstr(v):
    if v:
        return "(%.2f, %.2f, %.2f)" % (v.x, v.y, v.z)
    return '-'

class DressupPathBoundary(object):

    def __init__(self, obj, base, job):
        obj.addProperty("App::PropertyLink", "Base", "Base", QtCore.QT_TRANSLATE_NOOP("Path_DressupPathBoundary", "The base path to modify"))
        obj.Base = base
        obj.addProperty("App::PropertyLink", "Stock", "Boundary", QtCore.QT_TRANSLATE_NOOP("Path_DressupPathBoundary", "Solid object to be used to limit the generated Path."))
        obj.Stock = PathStock.CreateFromBase(job)
        obj.addProperty("App::PropertyBool", "Inside", "Boundary", QtCore.QT_TRANSLATE_NOOP("Path_DressupPathBoundary", "Determines if Boundary describes an inclusion or exclusion mask."))
        obj.Inside = True

        self.obj = obj
        self.safeHeight = None
        self.clearanceHeight = None

    def __getstate__(self):
        return None

    def __setstate__(self, state):
        return None

    def onDcoumentRestored(self, obj):
        self.obj = obj

    def onDelete(self, obj, args):
        if obj.Base:
            job = PathUtils.findParentJob(obj)
            if job:
                job.Proxy.addOperation(obj.Base, obj)
            if obj.Base.ViewObject:
                obj.Base.ViewObject.Visibility = True
            obj.Base = None
        if obj.Stock:
            obj.Document.removeObject(obj.Stock.Name)
            obj.Stock = None
        return True

    def boundaryCommands(self, obj, begin, end, verticalFeed):
        PathLog.track(_vstr(begin), _vstr(end))
        if end and PathGeom.pointsCoincide(begin, end):
            return []
        cmds = []
        if begin.z < self.safeHeight:
            cmds.append(Path.Command('G1', {'Z': self.safeHeight, 'F': verticalFeed}))
        if begin.z < self.clearanceHeight:
            cmds.append(Path.Command('G0', {'Z': self.clearanceHeight}))
        if end:
            cmds.append(Path.Command('G0', {'X': end.x, 'Y': end.y}))
            if end.z < self.clearanceHeight:
                cmds.append(Path.Command('G0', {'Z': max(self.safeHeight, end.z)}))
            if end.z < self.safeHeight:
                cmds.append(Path.Command('G1', {'Z': end.z, 'F': verticalFeed}))
        return cmds

    def execute(self, obj):
        if not obj.Base or not obj.Base.isDerivedFrom('Path::Feature') or not obj.Base.Path:
            return

        tc = PathDressup.toolController(obj.Base)

        if len(obj.Base.Path.Commands) > 0:
            self.safeHeight = float(PathUtil.opProperty(obj.Base, 'SafeHeight'))
            self.clearanceHeight = float(PathUtil.opProperty(obj.Base, 'ClearanceHeight'))

            boundary = obj.Stock.Shape
            cmd = obj.Base.Path.Commands[0]
            pos = cmd.Placement.Base
            commands = [cmd]
            lastExit = None
            for cmd in obj.Base.Path.Commands[1:]:
                if cmd.Name in PathGeom.CmdMoveAll:
                    edge = PathGeom.edgeForCmd(cmd, pos)
                    if edge:
                        inside = edge.common(boundary).Edges
                        outside = edge.cut(boundary).Edges
                        if not obj.Inside:
                            t = inside
                            inside = outside
                            outside = t
                        # it's really a shame that one cannot trust the sequence and/or
                        # orientation of edges
                        if 1 == len(inside) and 0 == len(outside):
                            PathLog.track(_vstr(pos), _vstr(lastExit), ' + ', cmd)
                            # cmd fully included by boundary
                            if lastExit:
                                commands.extend(self.boundaryCommands(obj, lastExit, pos, tc.VertFeed.Value))
                                lastExit = None
                            commands.append(cmd)
                            pos = PathGeom.commandEndPoint(cmd, pos)
                        elif 0 == len(inside) and 1 == len(outside):
                            PathLog.track(_vstr(pos), _vstr(lastExit), ' - ', cmd)
                            # cmd fully excluded by boundary
                            if not lastExit:
                                lastExit = pos
                            pos = PathGeom.commandEndPoint(cmd, pos)
                        else:
                            PathLog.track(_vstr(pos), _vstr(lastExit), len(inside), len(outside), cmd)
                            # cmd pierces boundary
                            while inside or outside:
                                ie = [e for e in inside if PathGeom.edgeConnectsTo(e, pos)]
                                PathLog.track(ie)
                                if ie:
                                    e = ie[0]
                                    ptL = e.valueAt(e.LastParameter)
                                    flip = PathGeom.pointsCoincide(pos, ptL)
                                    newPos = e.valueAt(e.FirstParameter) if flip else ptL
                                    # inside edges are taken at this point (see swap of inside/outside
                                    # above - so we can just connect the dots ...
                                    if lastExit:
                                        commands.extend(self.boundaryCommands(obj, lastExit, pos, tc.VertFeed.Value))
                                        lastExit = None
                                    PathLog.track(e, flip)
                                    commands.extend(PathGeom.cmdsForEdge(e, flip, False, 50, tc.HorizFeed.Value, tc.VertFeed.Value))  # add missing HorizFeed to G2 paths
                                    inside.remove(e)
                                    pos = newPos
                                    lastExit = newPos
                                else:
                                    oe = [e for e in outside if PathGeom.edgeConnectsTo(e, pos)]
                                    PathLog.track(oe)
                                    if oe:
                                        e = oe[0]
                                        ptL = e.valueAt(e.LastParameter)
                                        flip = PathGeom.pointsCoincide(pos, ptL)
                                        newPos = e.valueAt(e.FirstParameter) if flip else ptL
                                        # outside edges are never taken at this point (see swap of
                                        # inside/outside above) - so just move along ...
                                        outside.remove(e)
                                        pos = newPos
                                    else:
                                        PathLog.error('huh?')
                                        import Part
                                        Part.show(Part.Vertex(pos), 'pos')
                                        for e in inside:
                                            Part.show(e, 'ei')
                                        for e in outside:
                                            Part.show(e, 'eo')
                                        raise Exception('This is not supposed to happen')
                                    # Eif
                                # Eif
                            # Ewhile
                        # Eif
                        # pos = PathGeom.commandEndPoint(cmd, pos)
                    # Eif
                else:
                    PathLog.track('no-move', cmd)
                    commands.append(cmd)
            if lastExit:
                commands.extend(self.boundaryCommands(obj, lastExit, None, tc.VertFeed.Value))
                lastExit = None
        else:
            PathLog.warning("No Path Commands for %s" % obj.Base.Label)
            commands = []
        PathLog.track(commands)
        obj.Path = Path.Path(commands)

def Create(base, name='DressupPathBoundary'):
    '''Create(base, name='DressupPathBoundary') ... creates a dressup limiting base's Path to a boundary.'''

    if not base.isDerivedFrom('Path::Feature'):
        PathLog.error(translate('Path_DressupPathBoundary', 'The selected object is not a path')+'\n')
        return None

    obj = FreeCAD.ActiveDocument.addObject('Path::FeaturePython', name)
    job = PathUtils.findParentJob(base)
    obj.Proxy = DressupPathBoundary(obj, base, job)
    job.Proxy.addOperation(obj, base, True)
    return obj
<|endoftext|>


<commit_before># -*- python -*-
#
#  This file is part of cellnopt software
#
#  Copyright (c) 2014 - EBI-EMBL
#
#  File author(s): Thomas Cokelaer <cokelaer@ebi.ac.uk>
#
#  Distributed under the GPLv3 License.
#  See accompanying file LICENSE.txt or copy at
#      http://www.gnu.org/licenses/gpl-3.0.html
#
#  website: http://github.com/cellnopt
#
##############################################################################
""""""

import os
from . import __path__

name = 'ToyMMB'

__all__ = ['description', 'model_filename', 'data_filename', 'name']

model_filename = __path__[0] + os.sep + "PKN-{0}.sif".format(name)
data_filename = __path__[0] + os.sep + "MD-{0}.csv".format(name)
description = open(__path__[0] + os.sep + "README.rst").read()

__doc__ += description

def plot():
    from cellnopt.core import CNOGraph
    CNOGraph(model_filename, data_filename).plot()
<commit_msg>update cellnopt.core -> cno.io<commit_after># -*- python -*-
#
#  This file is part of cellnopt software
#
#  Copyright (c) 2014 - EBI-EMBL
#
#  File author(s): Thomas Cokelaer <cokelaer@ebi.ac.uk>
#
#  Distributed under the GPLv3 License.
#  See accompanying file LICENSE.txt or copy at
#      http://www.gnu.org/licenses/gpl-3.0.html
#
#  website: http://github.com/cellnopt
#
##############################################################################
""""""

import os
from . import __path__

name = 'ToyMMB'

__all__ = ['description', 'model_filename', 'data_filename', 'name']

model_filename = __path__[0] + os.sep + "PKN-{0}.sif".format(name)
data_filename = __path__[0] + os.sep + "MD-{0}.csv".format(name)
description = open(__path__[0] + os.sep + "README.rst").read()

__doc__ += description

def plot():
    from cno.io import CNOGraph
    CNOGraph(model_filename, data_filename).plot()
<|endoftext|>


<commit_before>from django.db import models

# Create your models here.
<commit_msg>Add  models<commit_after>from django.db import models
from django.contrib.postgres.fields import JSONField

# See
# https://developers.google.com/maps/documentation/javascript/3.exp/reference?hl=en#LatLngBoundsLiteral
from django.conf import settings

class LatLngBounds(models.Model):
    east = models.FloatField()
    north = models.FloatField()
    south = models.FloatField()
    west = models.FloatField()

class MapArea(models.Model):
    display_area = models.ForeignKey(LatLngBounds, related_name='+')
    # This data should be obviously moved to a storage suitable
    # for blobs. Keeping in here to ease deployment. Migration is
    # an exercise for the happy future developer.
    # The data stored is image in PNG or JPEG format.
    contour_map_image = models.BinaryField()
    # Which part of the Earth the rectangular stored
    # in contour_map_image represents.
    contour_map_reference = models.ForeignKey(LatLngBounds, related_name='+')

class Question(models.Model):
    map_area = models.ForeignKey(MapArea)
    max_duration = models.DurationField()
    creator = models.ForeignKey(settings.AUTH_USER_MODEL)
    # See “JSON Objects per Question Type” for more details.
    type = models.TextField()
    statement_data = JSONField()
    reference_data = JSONField()

class QuestionSet(models.Model):
    title = models.CharField(max_length=100)
    creator = models.ForeignKey(settings.AUTH_USER_MODEL)
    max_duration = models.DurationField()

class QuestionSetMember(models.Model):
    question_set = models.ForeignKey(QuestionSet, db_index=True)
    question = models.ForeignKey(Question)
    index = models.IntegerField()

    class Meta:
        ordering = ['index']

class AnswerSet(models.Model):
    student = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True)
    question_set = models.ForeignKey(QuestionSet)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()

class Answer(models.Model):
    answer_set = models.ForeignKey(AnswerSet, db_index=True)
    question_set_member = models.ForeignKey(QuestionSetMember)
    answer_data = JSONField()
    scoring_data = JSONField() # May be recalculated
    duration = models.DurationField()
    submission_time = models.DateTimeField()
<|endoftext|>


<commit_before>import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
from creds import *
from requests import Request
import requests
import json
import re
import tempfile
import redis
import uuid
import random
import string
import ast
from pydub import AudioSegment

def gettoken(uid):
	red = redis.from_url(redis_url)
	token = red.get(uid+"-access_token")
	refresh = red.get(uid+"-refresh_token")
	if token:
		return token
	elif refresh:
		payload = {"client_id" : Client_ID, "client_secret" : Client_Secret, "refresh_token" : refresh, "grant_type" : "refresh_token", }
		url = "https://api.amazon.com/auth/o2/token"
		r = requests.post(url, data = payload)
		resp = json.loads(r.text)
		red.set(uid+"-access_token", resp['access_token'])
		red.expire(uid+"-access_token", 3600)
		return resp['access_token']
	else:
		return False

def settrigger(user):
	red = redis.from_url(redis_url)
	self.set(user + "-trigger", True)

class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        return self.get_cookie("user")

class MainHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		f = open('static/index.html', 'r')
		resp = f.read()
		f.close()
		self.write(resp)
		self.finish()

class QuestionHandler(BaseHandler):
	# @tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		red = redis.from_url(redis_url)
		uid = self.get_argument("access_token")
		print uid
		res = red.get(uid + "-questions")
		res = res if res != None else ""
		self.write(res)
		self.finish()

	@tornado.web.asynchronous
	def post(self):
		red = redis.from_url(redis_url)
		uid = self.get_argument("access_token")
		print uid
		red.set(uid + "-questions", self.get_argument("questions"))
		print self.get_argument("questions")
		self.write({"success": True})
		self.finish()

class TriggerHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		red = redis.from_url(redis_url)
		# print self.get_current_user(), "=>", red.get(self.get_current_user() + "-trigger")
		status = red.get(self.get_current_user() + "-trigger")
		status = True if status == "True" else False
		self.write({"trigger": status})
		red.set(self.get_current_user() + "-trigger", False)
		self.finish()

class StartAuthHandler(tornado.web.RequestHandler):
	@tornado.web.asynchronous
	def get(self):
		sd = json.dumps({
		    "alexa:all": {
		        "productID": Product_ID,
		        "productInstanceAttributes": {
		            "deviceSerialNumber": "1"
		        }
		    }
		})
		url = "https://www.amazon.com/ap/oa"
		path = self.request.protocol + "://" + self.request.host
		callback = path + "/code"
		payload = {"client_id" : Client_ID, "scope" : "alexa profile", "scope_data" : sd, "response_type" : "code", "redirect_uri" : callback }
		req = Request('GET', url, params=payload)
		p = req.prepare()
		self.redirect(p.url)

class CodeAuthHandler(tornado.web.RequestHandler):
	@tornado.web.asynchronous
	def get(self):
		code=self.get_argument("code")
		path = self.request.protocol + "://" + self.request.host
		callback = path+"/code"
		payload = {"client_id" : Client_ID, "client_secret" : Client_Secret, "code" : code, "grant_type" : "authorization_code", "redirect_uri" : callback }
		url = "https://api.amazon.com/auth/o2/token"
		r = requests.post(url, data = payload)
		uid = str(uuid.uuid4())
		red = redis.from_url(redis_url)
		resp = json.loads(r.text)
		print resp
		red.set(uid+"-access_token", resp['access_token'])
		red.expire(uid+"-access_token", 3600)
		red.set(uid+"-refresh_token", resp['refresh_token'])
		self.set_cookie("user", uid)
		self.redirect("/")

class LogoutHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		uid = tornado.escape.xhtml_escape(self.current_user)
		red = redis.from_url(redis_url)
		red.delete(uid+"-access_token")
		red.delete(uid+"-refresh_token")
		self.clear_cookie("user")
		self.set_header('Content-Type', 'text/plain')
		self.write("Logged Out, Goodbye")
		self.finish()

class AudioHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def post(self):
		uid = tornado.escape.xhtml_escape(self.current_user)
		token = gettoken(uid)
		if (token == False):
			self.set_status(403)
		else:
			rxfile = self.request.files['data'][0]['body']
			tf = tempfile.NamedTemporaryFile(suffix=".wav")
			tf.write(rxfile)
			_input = AudioSegment.from_wav(tf.name)
			tf.close()
			tf = tempfile.NamedTemporaryFile(suffix=".wav")
			output = _input.set_channels(1).set_frame_rate(16000)
			f = output.export(tf.name, format="wav")
			url = 'https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize'
			headers = {'Authorization' : 'Bearer %s' % token}
			d = {
		    	"messageHeader": {
		        	"deviceContext": [
		            	{
		                	"name": "playbackState",
		                	"namespace": "AudioPlayer",
		                	"payload": {
		                    	"streamId": "",
		         			   	"offsetInMilliseconds": "0",
		                    	"playerActivity": "IDLE"
		                	}
		            	}
		        	]
		    	},
		    	"messageBody": {
		        	"profile": "alexa-close-talk",
		        	"locale": "en-us",
		        	"format": "audio/L16; rate=16000; channels=1"
		    	}
			}
			files = [
				('file', ('request', json.dumps(d), 'application/json; charset=UTF-8')),
				('file', ('audio', tf, 'audio/L16; rate=16000; channels=1'))
			]
			r = requests.post(url, headers=headers, files=files)
			tf.close()
			for v in r.headers['content-type'].split(";"):
				if re.match('.*boundary.*', v):
					boundary =  v.split("=")[1]
			data = r.content.split(boundary)
			for d in data:
				if (len(d) >= 1024):
			 	   audio = d.split('\r\n\r\n')[1].rstrip('--')
			self.set_header('Content-Type', 'audio/mpeg')
			self.write(audio)
		self.finish()

class TextHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		text = self.get_argument("text", None, True)

		uid = tornado.escape.xhtml_escape(self.current_user)
		token = gettoken(uid)
		if (token == False):
			self.set_status(403)
		else:
			# rxfile = self.request.files['data'][0]['body']
			# tf = tempfile.NamedTemporaryFile(suffix=".wav")
			# tf.write(rxfile)
			# _input = AudioSegment.from_wav(tf.name)
			# tf.close()
			# tf = tempfile.NamedTemporaryFile(suffix=".wav")
			# output = _input.set_channels(1).set_frame_rate(16000)
			# f = output.export(tf.name, format="wav")

			random_str = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(20))
			file_name = "/tmp/{}.wav".format(random_str)
			cmd = 'espeak "{}" --stdout > {}'.format(text, file_name)

			print text
			print file_name
			print cmd

			os.system(cmd)

			_input = AudioSegment.from_wav(file_name)
                        tf = tempfile.NamedTemporaryFile(suffix=".wav")
                        output = _input.set_channels(1).set_frame_rate(16000)
                        f = output.export(tf.name, format="wav")

			#tf = open(file_name)

			url = 'https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize'
			headers = {'Authorization' : 'Bearer %s' % token}
			d = {
		    	"messageHeader": {
		        	"deviceContext": [
		            	{
		                	"name": "playbackState",
		                	"namespace": "AudioPlayer",
		                	"payload": {
		                    	"streamId": "",
		         			   	"offsetInMilliseconds": "0",
		                    	"playerActivity": "IDLE"
		                	}
		            	}
		        	]
		    	},
		    	"messageBody": {
		        	"profile": "alexa-close-talk",
		        	"locale": "en-us",
		        	"format": "audio/L16; rate=16000; channels=1"
		    	}
			}
			files = [
				('file', ('request', json.dumps(d), 'application/json; charset=UTF-8')),
				('file', ('audio', tf, 'audio/L16; rate=16000; channels=1'))
			]
			r = requests.post(url, headers=headers, files=files)
			tf.close()
			print r
			for v in r.headers['content-type'].split(";"):
				if re.match('.*boundary.*', v):
					boundary =  v.split("=")[1]
			data = r.content.split(boundary)
			for d in data:
				if (len(d) >= 1024):
			 	   audio = d.split('\r\n\r\n')[1].rstrip('--')
			self.set_header('Content-Type', 'audio/mpeg')
			self.write(audio)
		self.finish()

def main():
	settings = {
	    "cookie_secret": "parisPOLANDbroadFENCEcornWOULD",
	    "login_url": "/static/welcome.html",
	}
	static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
	application = tornado.web.Application([(r"/", MainHandler),
											(r"/start", StartAuthHandler),
											(r"/code", CodeAuthHandler),
											(r"/logout", LogoutHandler),
											(r"/audio", AudioHandler),
											(r"/trigger", TriggerHandler),
											(r"/questions", QuestionHandler),
											(r"/text", TextHandler),
											(r'/(favicon.ico)', tornado.web.StaticFileHandler,{'path': static_path}),
											(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': static_path}),
											], **settings)
	http_server = tornado.httpserver.HTTPServer(application)
	port = int(os.environ.get("PORT", 8080))
	http_server.listen(port)
	tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
	main()

<commit_msg>add QuestionHandler<commit_after>import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
from creds import *
from requests import Request
import requests
import json
import re
import tempfile
import redis
import uuid
import random
import string
import ast
from pydub import AudioSegment

def gettoken(uid):
	red = redis.from_url(redis_url)
	token = red.get(uid+"-access_token")
	refresh = red.get(uid+"-refresh_token")
	if token:
		return token
	elif refresh:
		payload = {"client_id" : Client_ID, "client_secret" : Client_Secret, "refresh_token" : refresh, "grant_type" : "refresh_token", }
		url = "https://api.amazon.com/auth/o2/token"
		r = requests.post(url, data = payload)
		resp = json.loads(r.text)
		red.set(uid+"-access_token", resp['access_token'])
		red.expire(uid+"-access_token", 3600)
		return resp['access_token']
	else:
		return False

def settrigger(user):
	red = redis.from_url(redis_url)
	self.set(user + "-trigger", True)

class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        return self.get_cookie("user")

class MainHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		f = open('static/index.html', 'r')
		resp = f.read()
		f.close()
		self.write(resp)
		self.finish()

class QuestionHandler(BaseHandler):
	# @tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		red = redis.from_url(redis_url)
		uid = self.get_argument("access_token")
		print uid
		res = red.get(uid + "-questions")
		res = res if res != None else ""
		self.write(res)
		self.finish()

	@tornado.web.asynchronous
	def post(self):
		red = redis.from_url(redis_url)
		uid = self.get_argument("access_token")
		print uid
		red.set(uid + "-questions", self.get_argument("questions"))
		print self.get_argument("questions")
		self.write({"success": True})
		self.finish()

class TriggerHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		red = redis.from_url(redis_url)
		# print self.get_current_user(), "=>", red.get(self.get_current_user() + "-trigger")
		status = red.get(self.get_current_user() + "-trigger")
		status = True if status == "True" else False
		self.write({"trigger": status})
		red.set(self.get_current_user() + "-trigger", False)
		self.finish()

class StartAuthHandler(tornado.web.RequestHandler):
	@tornado.web.asynchronous
	def get(self):
		sd = json.dumps({
		    "alexa:all": {
		        "productID": Product_ID,
		        "productInstanceAttributes": {
		            "deviceSerialNumber": "1"
		        }
		    }
		})
		url = "https://www.amazon.com/ap/oa"
		path = self.request.protocol + "://" + self.request.host
		callback = path + "/code"
		payload = {"client_id" : Client_ID, "scope" : "alexa:all profile:all", "scope_data" : sd, "response_type" : "code", "redirect_uri" : callback }
		req = Request('GET', url, params=payload)
		p = req.prepare()
		self.redirect(p.url)

class CodeAuthHandler(tornado.web.RequestHandler):
	@tornado.web.asynchronous
	def get(self):
		code=self.get_argument("code")
		path = self.request.protocol + "://" + self.request.host
		callback = path+"/code"
		payload = {"client_id" : Client_ID, "client_secret" : Client_Secret, "code" : code, "grant_type" : "authorization_code", "redirect_uri" : callback }
		url = "https://api.amazon.com/auth/o2/token"
		r = requests.post(url, data = payload)
		uid = str(uuid.uuid4())
		red = redis.from_url(redis_url)
		resp = json.loads(r.text)
		print resp
		red.set(uid+"-access_token", resp['access_token'])
		red.expire(uid+"-access_token", 3600)
		red.set(uid+"-refresh_token", resp['refresh_token'])
		self.set_cookie("user", uid)
		self.redirect("/")

class LogoutHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		uid = tornado.escape.xhtml_escape(self.current_user)
		red = redis.from_url(redis_url)
		red.delete(uid+"-access_token")
		red.delete(uid+"-refresh_token")
		self.clear_cookie("user")
		self.set_header('Content-Type', 'text/plain')
		self.write("Logged Out, Goodbye")
		self.finish()

class AudioHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def post(self):
		uid = tornado.escape.xhtml_escape(self.current_user)
		token = gettoken(uid)
		if (token == False):
			self.set_status(403)
		else:
			rxfile = self.request.files['data'][0]['body']
			tf = tempfile.NamedTemporaryFile(suffix=".wav")
			tf.write(rxfile)
			_input = AudioSegment.from_wav(tf.name)
			tf.close()
			tf = tempfile.NamedTemporaryFile(suffix=".wav")
			output = _input.set_channels(1).set_frame_rate(16000)
			f = output.export(tf.name, format="wav")
			url = 'https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize'
			headers = {'Authorization' : 'Bearer %s' % token}
			d = {
		    	"messageHeader": {
		        	"deviceContext": [
		            	{
		                	"name": "playbackState",
		                	"namespace": "AudioPlayer",
		                	"payload": {
		                    	"streamId": "",
		         			   	"offsetInMilliseconds": "0",
		                    	"playerActivity": "IDLE"
		                	}
		            	}
		        	]
		    	},
		    	"messageBody": {
		        	"profile": "alexa-close-talk",
		        	"locale": "en-us",
		        	"format": "audio/L16; rate=16000; channels=1"
		    	}
			}
			files = [
				('file', ('request', json.dumps(d), 'application/json; charset=UTF-8')),
				('file', ('audio', tf, 'audio/L16; rate=16000; channels=1'))
			]
			r = requests.post(url, headers=headers, files=files)
			tf.close()
			for v in r.headers['content-type'].split(";"):
				if re.match('.*boundary.*', v):
					boundary =  v.split("=")[1]
			data = r.content.split(boundary)
			for d in data:
				if (len(d) >= 1024):
			 	   audio = d.split('\r\n\r\n')[1].rstrip('--')
			self.set_header('Content-Type', 'audio/mpeg')
			self.write(audio)
		self.finish()

class TextHandler(BaseHandler):
	@tornado.web.authenticated
	@tornado.web.asynchronous
	def get(self):
		text = self.get_argument("text", None, True)

		uid = tornado.escape.xhtml_escape(self.current_user)
		token = gettoken(uid)
		if (token == False):
			self.set_status(403)
		else:
			# rxfile = self.request.files['data'][0]['body']
			# tf = tempfile.NamedTemporaryFile(suffix=".wav")
			# tf.write(rxfile)
			# _input = AudioSegment.from_wav(tf.name)
			# tf.close()
			# tf = tempfile.NamedTemporaryFile(suffix=".wav")
			# output = _input.set_channels(1).set_frame_rate(16000)
			# f = output.export(tf.name, format="wav")

			random_str = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(20))
			file_name = "/tmp/{}.wav".format(random_str)
			cmd = 'espeak "{}" --stdout > {}'.format(text, file_name)

			print text
			print file_name
			print cmd

			os.system(cmd)

			_input = AudioSegment.from_wav(file_name)
                        tf = tempfile.NamedTemporaryFile(suffix=".wav")
                        output = _input.set_channels(1).set_frame_rate(16000)
                        f = output.export(tf.name, format="wav")

			#tf = open(file_name)

			url = 'https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize'
			headers = {'Authorization' : 'Bearer %s' % token}
			d = {
		    	"messageHeader": {
		        	"deviceContext": [
		            	{
		                	"name": "playbackState",
		                	"namespace": "AudioPlayer",
		                	"payload": {
		                    	"streamId": "",
		         			   	"offsetInMilliseconds": "0",
		                    	"playerActivity": "IDLE"
		                	}
		            	}
		        	]
		    	},
		    	"messageBody": {
		        	"profile": "alexa-close-talk",
		        	"locale": "en-us",
		        	"format": "audio/L16; rate=16000; channels=1"
		    	}
			}
			files = [
				('file', ('request', json.dumps(d), 'application/json; charset=UTF-8')),
				('file', ('audio', tf, 'audio/L16; rate=16000; channels=1'))
			]
			r = requests.post(url, headers=headers, files=files)
			tf.close()
			print r
			for v in r.headers['content-type'].split(";"):
				if re.match('.*boundary.*', v):
					boundary =  v.split("=")[1]
			data = r.content.split(boundary)
			for d in data:
				if (len(d) >= 1024):
			 	   audio = d.split('\r\n\r\n')[1].rstrip('--')
			self.set_header('Content-Type', 'audio/mpeg')
			self.write(audio)
		self.finish()

def main():
	settings = {
	    "cookie_secret": "parisPOLANDbroadFENCEcornWOULD",
	    "login_url": "/static/welcome.html",
	}
	static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
	application = tornado.web.Application([(r"/", MainHandler),
											(r"/start", StartAuthHandler),
											(r"/code", CodeAuthHandler),
											(r"/logout", LogoutHandler),
											(r"/audio", AudioHandler),
											(r"/trigger", TriggerHandler),
											(r"/questions", QuestionHandler),
											(r"/text", TextHandler),
											(r'/(favicon.ico)', tornado.web.StaticFileHandler,{'path': static_path}),
											(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': static_path}),
											], **settings)
	http_server = tornado.httpserver.HTTPServer(application)
	port = int(os.environ.get("PORT", 8080))
	http_server.listen(port)
	tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
	main()

<|endoftext|>


<commit_before>
from experiment_config_lib import ControllerConfig
from sts.topology import *
from sts.control_flow import Replayer

controllers = [ControllerConfig(cmdline='./pox.py --verbose --no-cli openflow.of_01 --address=__address__ --port=__port__ sts.syncproto.pox_syncer samples.topo forwarding.l2_multi messenger.messenger samples.nommessenger', address='127.0.0.1', port=8888, cwd='pox', sync='tcp:localhost:18899')]
topology_class = MeshTopology
topology_params = "num_switches=2"
patch_panel_class = BufferedPatchPanel
control_flow = Replayer("input_traces/pox_list_mcs_final.trace",
                        wait_time=2.0,
                        switch_init_sleep_seconds=0.0)
# MCS trace path: input_traces/pox_list_mcs_final_mcs_final.trace
dataplane_trace = "dataplane_traces/ping_pong_same_subnet.trace"
<commit_msg>New pox_list_mcs<commit_after><|endoftext|>


<commit_before>###############################################################################
# Caleydo - Visualization for Molecular Biology - http://caleydo.org
# Copyright (c) The Caleydo Team. All rights reserved.
# Licensed under the new BSD license, available at http://caleydo.org/license
###############################################################################

from builtins import range
import json

class JSONExtensibleEncoder(json.JSONEncoder):
  """
  json encoder with extension point extensions
  """

  def __init__(self, *args, **kwargs):
    super(JSONExtensibleEncoder, self).__init__(*args, **kwargs)

    from .plugin import list as list_plugins
    self.encoders = [p.load().factory() for p in list_plugins('json-encoder')]

  def default(self, o):
    for encoder in self.encoders:
      if o in encoder:
        return encoder(o, self)
    return super(JSONExtensibleEncoder, self).default(o)

def to_json(obj, *args, **kwargs):
  """
  convert the given object ot json using the extensible encoder
  :param obj:
  :param args:
  :param kwargs:
  :return:
  """
  if 'allow_nan' in kwargs:
    del kwargs['allow_nan']
  if 'indent' in kwargs:
    del kwargs['indent']
  kwargs['ensure_ascii'] = False

  # Pandas JSON module has been deprecated and removed. UJson cannot convert numpy arrays, so it cannot be used here. The JSON used here does not support the `double_precision` keyword.
  if isinstance(obj, dict) or isinstance(obj, list):
    obj = _handle_nan_values(obj)
  return json.dumps(obj, cls=JSONExtensibleEncoder, *args, **kwargs)

def _handle_nan_values(obj_to_convert):
     """
    Convert any NaN values in the given object to None. Previously, Pandas was used to encode NaN to null. This feature has been deprecated and removed, therefore
    the standard JSON encoder is used which parses NaN instead of null. A custom JSON encoder does not work for converting these values to None because python's
    JSON encoder already knows how to serialize NaN values.
    :param obj_to_convert:
    :return:
    """
    import math
    converted_dict = {}
    converted_list = []
    if isinstance(obj_to_convert, dict):
      for k, v in obj_to_convert.items():
          # value is dictionary or list
          if isinstance(v, dict) or isinstance(v, list):
              converted_dict[k] = _handle_nan_values(v)
          else:
              if (isinstance(v, float) and math.isnan(v)):
                  converted_dict[k] = None
              else:
                  converted_dict[k] = v
      return converted_dict
    elif isinstance(obj_to_convert, list):
      for elem in obj_to_convert:
        if isinstance(elem, dict):
          converted_list.append(_handle_nan_values(elem))
        elif (isinstance(elem, float) and math.isnan(elem)):
          converted_list.append(None)
        else:
          converted_list.append(elem)
      return converted_list

def jsonify(obj, *args, **kwargs):
  """
  similar to flask.jsonify but uses the extended json encoder and an arbitrary object
  :param obj:
  :param args:
  :param kwargs:
  :return:
  """
  from .ns import Response
  return Response(to_json(obj, *args, **kwargs), mimetype='application/json; charset=utf-8')

def glob_recursivly(path, match):
  import os
  import fnmatch

  for dirpath, dirnames, files in os.walk(path):
    if match is None:
      return None
    for f in fnmatch.filter(files, match):
      yield os.path.join(dirpath, f)

def fix_id(id):
  """
  fixes the id such that is it a resource identifier
  :param id:
  :return:
  """
  import re
  # convert strange characters to space
  r = re.sub(r"""[!#$%&'\(\)\*\+,\./:;<=>\?@\[\\\]\^`\{\|}~_]+""", ' ', id)
  # title case all words
  r = r.title()
  r = r[0].lower() + r[1:]
  # remove white spaces
  r = re.sub(r'\s+', '', r, flags=re.UNICODE)
  return r

def random_id(length):
  import string
  import random
  s = string.ascii_lowercase + string.digits
  id = ''
  for i in range(0, length):
    id += random.choice(s)
  return id
<commit_msg>add comments and handling for primitive case<commit_after>###############################################################################
# Caleydo - Visualization for Molecular Biology - http://caleydo.org
# Copyright (c) The Caleydo Team. All rights reserved.
# Licensed under the new BSD license, available at http://caleydo.org/license
###############################################################################

from builtins import range
import json

class JSONExtensibleEncoder(json.JSONEncoder):
  """
  json encoder with extension point extensions
  """

  def __init__(self, *args, **kwargs):
    super(JSONExtensibleEncoder, self).__init__(*args, **kwargs)

    from .plugin import list as list_plugins
    self.encoders = [p.load().factory() for p in list_plugins('json-encoder')]

  def default(self, o):
    for encoder in self.encoders:
      if o in encoder:
        return encoder(o, self)
    return super(JSONExtensibleEncoder, self).default(o)

def to_json(obj, *args, **kwargs):
  """
  convert the given object ot json using the extensible encoder
  :param obj:
  :param args:
  :param kwargs:
  :return:
  """
  if 'allow_nan' in kwargs:
    del kwargs['allow_nan']
  if 'indent' in kwargs:
    del kwargs['indent']
  kwargs['ensure_ascii'] = False

  # Pandas JSON module has been deprecated and removed. UJson cannot convert numpy arrays, so it cannot be used here. The JSON used here does not support the `double_precision` keyword.
  if isinstance(obj, dict) or isinstance(obj, list):
    obj = _handle_nan_values(obj)
  return json.dumps(obj, cls=JSONExtensibleEncoder, *args, **kwargs)

def _handle_nan_values(obj_to_convert):
     """
    Convert any NaN values in the given object to None. Previously, Pandas was used to encode NaN to null. This feature has been deprecated and removed, therefore
    the standard JSON encoder is used which parses NaN instead of null. A custom JSON encoder does not work for converting these values to None because python's
    JSON encoder already knows how to serialize NaN values.
    :param obj_to_convert:
    :return:
    """
    import math
    converted_dict = {}
    converted_list = []
    # primitive value
    if (isinstance(v, float) and math.isnan(elem)):
      return None
    # convert dictionaries
    if isinstance(obj_to_convert, dict):
      for k, v in obj_to_convert.items():
          # value is dictionary or list
          if isinstance(v, dict) or isinstance(v, list):
              converted_dict[k] = _handle_nan_values(v)
          else:
              # value is NaN
              if (isinstance(v, float) and math.isnan(v)):
                  converted_dict[k] = None
              else:
                  converted_dict[k] = v
      return converted_dict
    # convert lists
    elif isinstance(obj_to_convert, list):
      for elem in obj_to_convert:
        # list element is dictionary
        if isinstance(elem, dict):
          converted_list.append(_handle_nan_values(elem))
        # list element is NaN value
        elif (isinstance(elem, float) and math.isnan(elem)):
          converted_list.append(None)
        else:
          converted_list.append(elem)
      return converted_list

def jsonify(obj, *args, **kwargs):
  """
  similar to flask.jsonify but uses the extended json encoder and an arbitrary object
  :param obj:
  :param args:
  :param kwargs:
  :return:
  """
  from .ns import Response
  return Response(to_json(obj, *args, **kwargs), mimetype='application/json; charset=utf-8')

def glob_recursivly(path, match):
  import os
  import fnmatch

  for dirpath, dirnames, files in os.walk(path):
    if match is None:
      return None
    for f in fnmatch.filter(files, match):
      yield os.path.join(dirpath, f)

def fix_id(id):
  """
  fixes the id such that is it a resource identifier
  :param id:
  :return:
  """
  import re
  # convert strange characters to space
  r = re.sub(r"""[!#$%&'\(\)\*\+,\./:;<=>\?@\[\\\]\^`\{\|}~_]+""", ' ', id)
  # title case all words
  r = r.title()
  r = r[0].lower() + r[1:]
  # remove white spaces
  r = re.sub(r'\s+', '', r, flags=re.UNICODE)
  return r

def random_id(length):
  import string
  import random
  s = string.ascii_lowercase + string.digits
  id = ''
  for i in range(0, length):
    id += random.choice(s)
  return id
<|endoftext|>


<commit_before>#
#                       _oo0oo_
#                      o8888888o
#                      88" . "88
#                      (| -_- |)
#                      0\  =  /0
#                    ___/`---'\___
#                  .' \\|     |// '.
#                 / \\|||  :  |||// \
#                / _||||| -:- |||||- \
#               |   | \\\  -  /// |   |
#               | \_|  ''\---/''  |_/ |
#               \  .-\__  '-'  ___/-. /
#             ___'. .'  /--.--\  `. .'___
#          ."" '<  `.___\_<|>_/___.' >' "".
#         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
#         \  \ `_.   \_ __\ /__ _/   .-` /  /
#     =====`-.____`.___ \_____/___.-`___.-'=====
#                       `=---='
#
#
#     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Ask the user for input
input_for_sorting = input("Paste in (or type, I won't judge) a list of things you'd like to be sorted!")
split_input = input_for_sorting.split()
length_of_list = len(split_input)

#The sorter
#Sort all the words.
for index in range(length_of_list):
  #Needed for sorting just one word
  for index in range(len(split_input)):
    #If current one is earlier in the alphabet than the previous one:
    if split_input[index] < split_input[index - 1] and split_input[index-1] != split_input[-1]:
      #Swap their positions
      swap_down = split_input[index - 1]
      swap_up = split_input[index]
      split_input[index] = swap_down
      split_input[index-1] = swap_up

#Print the new list, one by one.
for index in range(length_of_list):
  print(split_input[index] + " ")

#https://www.youtube.com/watch?v=WaNLJf8xzC4
<commit_msg>Comment and input<commit_after>#Ask the user for input
input_for_sorting = input("Paste in (or type, I won't judge) a list of things you'd like to be sorted!")
split = input("What would you like this list to be split by? If you leave it blank, it will be space")
split_input = input_for_sorting.split(split) #Split by whatever
length_of_list = len(split_input) #the length of the list

#The sorter
#Sort all the words.
for index in range(length_of_list):
  #Needed for sorting just one word
  for index in range(len(split_input)):
    #If current one is earlier in the alphabet than the previous one:
    if split_input[index] < split_input[index - 1] and split_input[index-1] != split_input[-1]:
      #Swap their positions
      swap_down = split_input[index - 1]
      swap_up = split_input[index]
      split_input[index] = swap_down
      split_input[index-1] = swap_up

#Print the new list, one by one.
for index in range(length_of_list):
  print(split_input[index] + " ")
<|endoftext|>


<commit_before>from __future__ import print_function

import os
import tarfile
import re
import platform
import time
import textwrap
import stat
import shutil

from SCons.Script import *

col = 0

class ResourceContext:
    def __init__(self): pass

def start_file(ctx, f):
    note = ('WARNING: This file was auto generated.  Please do NOT '
            'edit directly or check in to source control.')

    f.write(
        '/' + ('*' * 75) + '\\\n   ' +
        '\n   '.join(textwrap.wrap(note)) + '\n' +
        '\\' + ('*' * 75) + '/\n'
        '\n'
        '#include <cbang/util/Resource.h>\n\n'
        'using namespace cb;\n\n'
        )

    if ctx.ns:
        for namespace in ctx.ns.split('::'):
            f.write('namespace %s {\n' % namespace)

def end_file(ctx, f):
    if ctx.ns:
        for namespace in ctx.ns.split('::'):
            f.write('} // namespace %s\n' % namespace)

def write_string(ctx, output, s, newline = 0):
    l = len(s)

    if newline or ctx.col + l > 80:
        output.write('\n')
        ctx.col = 0

    i = s.rfind('\n')
    if i != -1: ctx.col = l - (i + 1)
    else: ctx.col += l

    output.write(s)

def is_excluded(exclude, path):
    return exclude != None and exclude.search(path) != None

def write_resource(ctx, output, data_dir, path, children = None,
                   exclude = None):
    name = os.path.basename(path)
    if is_excluded(ctx.exclude, path): return

    is_dir = os.path.isdir(path)
    id = ctx.next_id
    ctx.next_id += 1
    length = 0

    if is_dir:
        typeStr = 'Directory'
        child_resources = []

        for filename in os.listdir(path):
            write_resource(ctx, output, data_dir, os.path.join(path, filename),
                           child_resources, exclude)

        write_string(ctx, output, 'const Resource *children%d[] = {' % id)

        for res in child_resources:
            write_string(ctx, output, '&resource%d,' % res)

        write_string(ctx, output, '0};\n')

    else:
        out_path = '%s/data%d.cpp' % (data_dir, id)
        print('Writing resource: %s to %s' % (path, out_path))

        typeStr = 'File'
        with open(path, 'rb') as f:
            prototype = 'extern const unsigned char data%d[]' % id

            write_string(ctx, output, '%s;\n' % prototype)

            with open(out_path, 'w') as out:
                start_file(ctx, out)

                write_string(ctx, out, prototype + ' = {')

                while True:
                    count = 0
                    for c in f.read(102400):
                        if not isinstance(c, int): c = ord(c)
                        write_string(ctx, out, '%d,' % c)
                        count += 1

                    if count == 0: break
                    length += count

                write_string(ctx, out, '0};\n')

                end_file(ctx, out)

    if children != None: children.append(id)

    output.write('extern const %sResource resource%d("%s", ' %
                 (typeStr, id, name))

    if is_dir: output.write('children%d' % id)
    else: output.write('(const char *)data%d, %d' % (id, length))

    output.write(');\n')

def update_time(ctx, path):
    if not os.path.exists(path): return 0
    exclude = ctx.exclude
    if exclude is not None and exclude.search(path) is not None: return 0

    if os.path.isdir(path):
        updated = 0

        for name in os.listdir(path):
            t = os.stat(path)[stat.ST_MTIME]
            if updated < t: updated = t
            t = update_time(os.path.join(path, name), exclude)
            if updated < t: updated = t

        return updated

    else: return os.stat(path)[stat.ST_MTIME]

def get_exclude(env):
    pattern = None
    for ex in env.get('RESOURCES_EXCLUDES'):
        if pattern == None: pattern = ''
        else: pattern += '|'
        pattern += '(%s)' % ex

    return re.compile(pattern)

def resources_build(target, source, env):
    ctx = ResourceContext()
    ctx.env = env
    ctx.ns = env.get('RESOURCES_NS')
    ctx.exclude = get_exclude(env)
    ctx.next_id = 0
    ctx.col = 0

    target = str(target[0])

    # Check update times
    if not env.get('RESOURCES_ALWAYS_BUILD'):
        if os.path.exists(target):
            updated = False
            lastUpdate = os.stat(target)[stat.ST_MTIME]

            for src in source:
                if lastUpdate < update_time(ctx, str(src)):
                    updated = True
                    break

            if not updated: return

    data_dir = os.path.splitext(target)[0] + ".data"
    if os.path.exists(data_dir): shutil.rmtree(data_dir)
    os.mkdir(data_dir)

    # Write resources
    with open(target, 'w') as f:
        start_file(ctx, f)

        for src in source:
            write_resource(ctx, f, data_dir, str(src))

        end_file(ctx, f)

def get_targets(exclude, path, data_dir, count = [0]):
    if is_excluded(exclude, path): return []

    id = count[0]
    count[0] += 1

    if os.path.isdir(path):
        targets = []

        for name in os.listdir(path):
            targets += \
                get_targets(exclude, os.path.join(path, name), data_dir, count)

        return targets

    else:
        target = '%s/data%d.cpp' % (data_dir, id)
        return [File(target)]

def modify_targets(target, source, env):
    exclude = get_exclude(env)
    name = str(target[0])
    data_dir = os.path.splitext(name)[0] + ".data"
    target += get_targets(exclude, str(source[0]), data_dir, [0])
    print(tuple(map(str, target)))
    Depends(target, FindFile('cbang/util/Resource.h', env['CPPPATH']))
    return target, source

def resources_message(target, source, env):
    return 'building resource file "%s"' % str(target[0])

def generate(env):
    env.SetDefault(RESOURCES_NS = '')
    env.SetDefault(RESOURCES_EXCLUDES = [r'\.svn', r'.*~'])
    env.SetDefault(RESOURCES_ALWAYS_BUILD = True)

    bld = env.Builder(action = resources_build,
                      source_factory = SCons.Node.FS.Entry,
                      source_scanner = SCons.Defaults.DirScanner,
                      emitter = modify_targets)
    env.Append(BUILDERS = {'Resources' : bld})

def exists():
    return True
<commit_msg>Complain if resources do not exist<commit_after>from __future__ import print_function

import os
import tarfile
import re
import platform
import time
import textwrap
import stat
import shutil

from SCons.Script import *

col = 0

class ResourceContext:
    def __init__(self): pass

def start_file(ctx, f):
    note = ('WARNING: This file was auto generated.  Please do NOT '
            'edit directly or check in to source control.')

    f.write(
        '/' + ('*' * 75) + '\\\n   ' +
        '\n   '.join(textwrap.wrap(note)) + '\n' +
        '\\' + ('*' * 75) + '/\n'
        '\n'
        '#include <cbang/util/Resource.h>\n\n'
        'using namespace cb;\n\n'
        )

    if ctx.ns:
        for namespace in ctx.ns.split('::'):
            f.write('namespace %s {\n' % namespace)

def end_file(ctx, f):
    if ctx.ns:
        for namespace in ctx.ns.split('::'):
            f.write('} // namespace %s\n' % namespace)

def write_string(ctx, output, s, newline = 0):
    l = len(s)

    if newline or ctx.col + l > 80:
        output.write('\n')
        ctx.col = 0

    i = s.rfind('\n')
    if i != -1: ctx.col = l - (i + 1)
    else: ctx.col += l

    output.write(s)

def is_excluded(exclude, path):
    return exclude != None and exclude.search(path) != None

def write_resource(ctx, output, data_dir, path, children = None,
                   exclude = None):
    name = os.path.basename(path)
    if is_excluded(ctx.exclude, path): return

    is_dir = os.path.isdir(path)
    id = ctx.next_id
    ctx.next_id += 1
    length = 0

    if is_dir:
        typeStr = 'Directory'
        child_resources = []

        for filename in os.listdir(path):
            write_resource(ctx, output, data_dir, os.path.join(path, filename),
                           child_resources, exclude)

        write_string(ctx, output, 'const Resource *children%d[] = {' % id)

        for res in child_resources:
            write_string(ctx, output, '&resource%d,' % res)

        write_string(ctx, output, '0};\n')

    else:
        out_path = '%s/data%d.cpp' % (data_dir, id)
        print('Writing resource: %s to %s' % (path, out_path))

        typeStr = 'File'
        with open(path, 'rb') as f:
            prototype = 'extern const unsigned char data%d[]' % id

            write_string(ctx, output, '%s;\n' % prototype)

            with open(out_path, 'w') as out:
                start_file(ctx, out)

                write_string(ctx, out, prototype + ' = {')

                while True:
                    count = 0
                    for c in f.read(102400):
                        if not isinstance(c, int): c = ord(c)
                        write_string(ctx, out, '%d,' % c)
                        count += 1

                    if count == 0: break
                    length += count

                write_string(ctx, out, '0};\n')

                end_file(ctx, out)

    if children != None: children.append(id)

    output.write('extern const %sResource resource%d("%s", ' %
                 (typeStr, id, name))

    if is_dir: output.write('children%d' % id)
    else: output.write('(const char *)data%d, %d' % (id, length))

    output.write(');\n')

def update_time(ctx, path):
    if not os.path.exists(path): return 0
    exclude = ctx.exclude
    if exclude is not None and exclude.search(path) is not None: return 0

    if os.path.isdir(path):
        updated = 0

        for name in os.listdir(path):
            t = os.stat(path)[stat.ST_MTIME]
            if updated < t: updated = t
            t = update_time(os.path.join(path, name), exclude)
            if updated < t: updated = t

        return updated

    else: return os.stat(path)[stat.ST_MTIME]

def get_exclude(env):
    pattern = None
    for ex in env.get('RESOURCES_EXCLUDES'):
        if pattern == None: pattern = ''
        else: pattern += '|'
        pattern += '(%s)' % ex

    return re.compile(pattern)

def resources_build(target, source, env):
    ctx = ResourceContext()
    ctx.env = env
    ctx.ns = env.get('RESOURCES_NS')
    ctx.exclude = get_exclude(env)
    ctx.next_id = 0
    ctx.col = 0

    target = str(target[0])

    # Check update times
    if not env.get('RESOURCES_ALWAYS_BUILD'):
        if os.path.exists(target):
            updated = False
            lastUpdate = os.stat(target)[stat.ST_MTIME]

            for src in source:
                if lastUpdate < update_time(ctx, str(src)):
                    updated = True
                    break

            if not updated: return

    data_dir = os.path.splitext(target)[0] + ".data"
    if os.path.exists(data_dir): shutil.rmtree(data_dir)
    os.mkdir(data_dir)

    # Write resources
    with open(target, 'w') as f:
        start_file(ctx, f)

        for src in source:
            write_resource(ctx, f, data_dir, str(src))

        end_file(ctx, f)

def get_targets(exclude, path, data_dir, count = [0]):
    if is_excluded(exclude, path): return []

    if not os.path.exists(path):
        raise Exception('Resource "%s" does not exist' % path)

    id = count[0]
    count[0] += 1

    if os.path.isdir(path):
        targets = []

        for name in os.listdir(path):
            child = os.path.join(path, name)
            targets += get_targets(exclude, child, data_dir, count)

        return targets

    return [File('%s/data%d.cpp' % (data_dir, id))]

def modify_targets(target, source, env):
    exclude = get_exclude(env)
    name = str(target[0])
    data_dir = os.path.splitext(name)[0] + ".data"
    count = [0]
    for s in source: target += get_targets(exclude, str(s), data_dir, count)
    print(tuple(map(str, target)))
    Depends(target, FindFile('cbang/util/Resource.h', env['CPPPATH']))
    return target, source

def resources_message(target, source, env):
    return 'building resource file "%s"' % str(target[0])

def generate(env):
    env.SetDefault(RESOURCES_NS = '')
    env.SetDefault(RESOURCES_EXCLUDES = [r'\.svn', r'.*~'])
    env.SetDefault(RESOURCES_ALWAYS_BUILD = True)

    bld = env.Builder(action = resources_build,
                      source_factory = SCons.Node.FS.Entry,
                      source_scanner = SCons.Defaults.DirScanner,
                      emitter = modify_targets)
    env.Append(BUILDERS = {'Resources' : bld})

def exists():
    return True
<|endoftext|>