<commit_before>"""
Kitchen is a main controller class of SOHOJUNG.
This class controls all the user events and drives business logic.
Model part of SOHOJUNG is called office.

author: Bak Hyeonjae
"""
import threading
import time
from threading import Timer, Lock
from copy import deepcopy
import Stack
import re
import sys
import const
import io

class Kitchen(threading.Thread):

    input_stream = None
    flagTerminate = False

    def __init__(self,mode,fileArg,cfgArg):
        threading.Thread.__init__(self)

        self.mode = mode
        self.argFile = fileArg
        self.cfgFile = cfgArg
        self.mutex = Lock()

        self.reset()

        const.STATE_INTERACTIVE_IDLE = 0
        const.STATE_INTERACTIVE_CAPTURING = 1
        const.STATE_INTERACTIVE_PROCESSING = 2
        const.STATE_INTERACTIVE_ACTIVE = 3
        const.STATE_INTERACTIVE_RESET = 4
        self.stateInteractive = const.STATE_INTERACTIVE_IDLE

        if const.mode_interactive == self.mode:
            self.input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='ignore')

    def reset(self):
        self.lifeLine = []
        self.callStack = [] #Stack.Stack()
        self.threads = []
        self.cnt = 0

    def connectView(self,view):
        self.view = view    # set restaurant(view) component

    def connectToolBox(self,toolBox):
        self.toolBox = toolBox

    def terminate(self):
        self.flagTerminate = True
        self.mutex.release()

    def run(self):
        while True:
            self.mutex.acquire()
            if self.flagTerminate:
                break
            print("***** KITCHEN THREAD *****   ",self.stateInteractive)
            self.loadData()
            self.mutex.release()
            self.setInteractiveModeState(const.STATE_INTERACTIVE_ACTIVE)

    def loadData(self):
        print("kitchen --> start loading")

        self.lifeLine.append("start")
        self.view.addLifeline("start")

        input_dat = []

        if const.mode_interactive == self.mode:
            for idx, line in enumerate(self.input_stream):
                if const.STATE_INTERACTIVE_RESET == self.stateInteractive:
                    self.setInteractiveModeState(const.STATE_INTERACTIVE_CAPTURING)
                if const.STATE_INTERACTIVE_IDLE == self.stateInteractive:
                    continue
                elif const.STATE_INTERACTIVE_PROCESSING == self.stateInteractive:
                    break
                input_dat.append(line)
        elif const.mode_batch == self.mode:
            input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='ignore')
            for idx, line in enumerate(input_stream):
                input_dat.append(line)
        elif const.mode_file == self.mode:
            file_in = open(self.argFile,"r", encoding='utf-8', errors='ignore')
            for idx, line in enumerate(file_in):
                input_dat.append(line)

        idx_for_remove = []
        for idx, line in list(reversed(list(enumerate(input_dat)))):
            match_flag = False
            if '[exit]' in line:
                for _i in reversed(range(0,idx)):
                    if '[entry]' in input_dat[_i]:
                        exit_str = line.split()
                        exit_method_name = ''
                        for _j in range(8,len(exit_str)):
                            exit_method_name += exit_str[_j]

                        entry_str = input_dat[_i].split()
                        entry_method_name = ''
                        for _j in range(8,len(entry_str)):
                            entry_method_name += entry_str[_j]

                        entry_method_name_arg_removed = entry_method_name.split("[args]")[0]
                        entry_method_name_arg_ret_removed = entry_method_name_arg_removed.split("[returns]")[0]
                        exit_method_name_arg_removed = exit_method_name.split("[args]")[0]
                        exit_method_name_arg_ret_removed = exit_method_name_arg_removed.split("[returns]")[0]

                        if entry_method_name_arg_ret_removed == exit_method_name_arg_ret_removed:
                            match_flag = True
                            break

                if False == match_flag:
                    print("REMOVE the line !!! -> %d" % idx)
                    print(line)
                    idx_for_remove.append(idx)

        for _i in idx_for_remove:
            del input_dat[_i]

        cfg_file_in = open(self.cfgFile,"r", encoding='utf-8', errors='ignore')

        cfg_time_index = 0
        cfg_thread_index = 0
        cfg_flowtype_index = 0
        cfg_methodname_index = 0
        cfg_ignore_str = None
        cfg_arg_seperator = None
        cfg_filter_tag = None

        for line in cfg_file_in:
            if '[time]' in line:
                str = line.split('[time]')
                cfg_time_index = int(str[1])
            elif '[thread]' in line:
                str = line.split('[thread]')
                cfg_thread_index = int(str[1])
            elif '[flowtype]' in line:
                str = line.split('[flowtype]')
                cfg_flowtype_index = int(str[1])
            elif '[methodname]' in line:
                str = line.split('[methodname]')
                cfg_methodname_index = int(str[1])
            elif '[ignore]' in line:
                str = line.split('[ignore]')
                cfg_ignore_str = str[1].replace(' ','').replace('\n','').split(',')
            elif '[argseperator]' in line:
                str = line.split('[argseperator]')
                cfg_arg_seperator = str[1].replace(' ','').replace('\n','')
            elif '[filtertag]' in line:
                str = line.split('[filtertag]')
                cfg_filter_tag = str[1].replace(' ','').replace('\n','')

        cfg_file_in.close()

        filter_str = "(.*)%s(.*)" % (cfg_filter_tag)
        index = 0
        for idx, line in enumerate(input_dat):
            if re.match(filter_str,line):
                str_list = line[:]
                for word in cfg_ignore_str:
                    str_list = str_list.replace(word,'')
                str_list = str_list.split()

                if len(str_list) < max(cfg_time_index,cfg_thread_index,cfg_flowtype_index,cfg_methodname_index):
                    print("input line has some errors : %s"%line)
                    continue

                if '[entry]' not in line and '[exit]' not in line:
                    print("input line has some errors : %s" % line)
                    continue

                event_time = str_list[cfg_time_index]
                thread_id = str_list[cfg_thread_index]
                flow_type = str_list[cfg_flowtype_index]
                method_name_index = cfg_methodname_index

                package_method_name = ''
                for _i in range(method_name_index,len(str_list)):
                    package_method_name += str_list[_i] + ' '

                org_line = line[:]
                arguments_str = org_line.split("[args]")[1] if "[args]" in org_line else None
                return_str = org_line.split("[returns]")[1] if "[returns]" in org_line else None
                arg_removed = package_method_name.split("[args]")[0]
                ret_removed = arg_removed.split("[returns]")[0]
                parameter_removed = package_method_name.split("(")[0]
                inside_parenthesis = package_method_name.split("(")[1].split(")")[0].replace(' ','')
                parameters = None
                if '' != inside_parenthesis:
                    parameters = inside_parenthesis.split(',')
                #method_name = list(reversed(parameter_removed .split(".")))[0]
                return_type_removed = list(reversed(parameter_removed.split(' ')))
                method_name = list(reversed(re.split('\\.|\\*|\\:\\:',parameter_removed)))[0]
                package_name = ".".join(list(reversed(list(reversed(parameter_removed.split(".")))[1:])))

                class_name = package_name
                if class_name == '':
                    class_name = 'Global'

                if not (thread_id in self.threads):
                    self.threads.append(thread_id)
                    stack = Stack.Stack()
                    stack.push({'class':"start", 'message':""})
                    self.callStack.append(stack)

                if not (class_name in self.lifeLine):
                    self.view.addLifeline(class_name)
                    self.lifeLine.append(class_name)

                thread_index = self.threads.index(thread_id)

                if flow_type == "[entry]":
                    self.view.sendSignal(self.callStack[thread_index].peek(),class_name,method_name,thread_index,event_time,index+1,self.callStack[thread_index],arguments_str.split(cfg_arg_seperator) if arguments_str else None,parameters)
                    self.callStack[thread_index].push({'class':class_name, 'message':method_name})

                if flow_type == "[exit]":
                    self.view.completeTask(class_name,method_name,1,event_time,index+1,self.callStack[thread_index].depth('compareClass'),return_str.split(cfg_arg_seperator) if return_str else None)
                    self.callStack[thread_index].pop()

                index += 1

            del line
            self.cnt = index

        print("kitchen --> refreshData")
        self.view.refreshData(self.cnt)
        self.view.setDrawingAvailable()
        self.toolBox.setAvailable(self.threads)
        print("kitchen start")

    def activateHide(self,flag):
        pass

    def resetAllLifelines(self):
        pass

    def activateCapture(self,flag):
        if const.STATE_INTERACTIVE_IDLE == self.stateInteractive or const.STATE_INTERACTIVE_RESET == self.stateInteractive:
            self.setInteractiveModeState(const.STATE_INTERACTIVE_CAPTURING)
        elif const.STATE_INTERACTIVE_CAPTURING == self.stateInteractive:
            self.setInteractiveModeState(const.STATE_INTERACTIVE_PROCESSING)
        elif const.STATE_INTERACTIVE_ACTIVE == self.stateInteractive:
            self.setInteractiveModeState(const.STATE_INTERACTIVE_RESET)

    def setInteractiveModeState(self,state):

        self.toolBox.notifyInteractiveStateChanged(state)

        if const.STATE_INTERACTIVE_RESET == state:
            print("setInteractiveModeState : STATE_INTERACTIVE_RESET")
            if const.STATE_INTERACTIVE_ACTIVE != self.stateInteractive:
                pass # throw exception
            self.view.reset()
            self.reset()
            self.stateInteractive = const.STATE_INTERACTIVE_RESET
            self.mutex.release()

        if const.STATE_INTERACTIVE_ACTIVE == state:
            print("setInteractiveModeState : STATE_INTERACTIVE_ACTIVE")
            if const.STATE_INTERACTIVE_PROCESSING != self.stateInteractive:
                pass # throw exception
            self.stateInteractive = const.STATE_INTERACTIVE_ACTIVE
            self.mutex.acquire()

        if const.STATE_INTERACTIVE_PROCESSING == state:
            print("setInteractiveModeState : STATE_INTERACTIVE_PROCESSING")
            if const.STATE_INTERACTIVE_CAPTURING != self.stateInteractive:
                pass # throw exception
            self.stateInteractive = const.STATE_INTERACTIVE_PROCESSING

        if const.STATE_INTERACTIVE_CAPTURING == state:
            print("setInteractiveModeState : STATE_INTERACTIVE_CAPTURING")
            if const.STATE_INTERACTIVE_IDLE != self.stateInteractive or const.STATE_INTERACTIVE_RESET != self.stateInteractive:
                pass # throw exception
            self.stateInteractive = const.STATE_INTERACTIVE_CAPTURING

    def searchMessage(self,str):
        pass

    def moveToPrev(self):
        pass

    def moveToNext(self):
        pass
<commit_msg>Class splitter for C++ is verified.<commit_after>"""
Kitchen is a main controller class of SOHOJUNG.
This class controls all the user events and drives business logic.
Model part of SOHOJUNG is called office.

author: Bak Hyeonjae
"""
import threading
import time
from threading import Timer, Lock
from copy import deepcopy
import Stack
import re
import sys
import const
import io

class Kitchen(threading.Thread):

    input_stream = None
    flagTerminate = False
    STR_CLASS_SPLITTER = '\\.|\\*|\\:\\:'

    def __init__(self,mode,fileArg,cfgArg):
        threading.Thread.__init__(self)

        self.mode = mode
        self.argFile = fileArg
        self.cfgFile = cfgArg
        self.mutex = Lock()

        self.reset()

        const.STATE_INTERACTIVE_IDLE = 0
        const.STATE_INTERACTIVE_CAPTURING = 1
        const.STATE_INTERACTIVE_PROCESSING = 2
        const.STATE_INTERACTIVE_ACTIVE = 3
        const.STATE_INTERACTIVE_RESET = 4
        self.stateInteractive = const.STATE_INTERACTIVE_IDLE

        if const.mode_interactive == self.mode:
            self.input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='ignore')

    def reset(self):
        self.lifeLine = []
        self.callStack = [] #Stack.Stack()
        self.threads = []
        self.cnt = 0

    def connectView(self,view):
        self.view = view    # set restaurant(view) component

    def connectToolBox(self,toolBox):
        self.toolBox = toolBox

    def terminate(self):
        self.flagTerminate = True
        self.mutex.release()

    def run(self):
        while True:
            self.mutex.acquire()
            if self.flagTerminate:
                break
            print("***** KITCHEN THREAD *****   ",self.stateInteractive)
            self.loadData()
            self.mutex.release()
            self.setInteractiveModeState(const.STATE_INTERACTIVE_ACTIVE)

    def loadData(self):
        print("kitchen --> start loading")

        self.lifeLine.append("start")
        self.view.addLifeline("start")

        input_dat = []

        if const.mode_interactive == self.mode:
            for idx, line in enumerate(self.input_stream):
                if const.STATE_INTERACTIVE_RESET == self.stateInteractive:
                    self.setInteractiveModeState(const.STATE_INTERACTIVE_CAPTURING)
                if const.STATE_INTERACTIVE_IDLE == self.stateInteractive:
                    continue
                elif const.STATE_INTERACTIVE_PROCESSING == self.stateInteractive:
                    break
                input_dat.append(line)
        elif const.mode_batch == self.mode:
            input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='ignore')
            for idx, line in enumerate(input_stream):
                input_dat.append(line)
        elif const.mode_file == self.mode:
            file_in = open(self.argFile,"r", encoding='utf-8', errors='ignore')
            for idx, line in enumerate(file_in):
                input_dat.append(line)

        idx_for_remove = []
        for idx, line in list(reversed(list(enumerate(input_dat)))):
            match_flag = False
            if '[exit]' in line:
                for _i in reversed(range(0,idx)):
                    if '[entry]' in input_dat[_i]:
                        exit_str = line.split()
                        exit_method_name = ''
                        for _j in range(8,len(exit_str)):
                            exit_method_name += exit_str[_j]

                        entry_str = input_dat[_i].split()
                        entry_method_name = ''
                        for _j in range(8,len(entry_str)):
                            entry_method_name += entry_str[_j]

                        entry_method_name_arg_removed = entry_method_name.split("[args]")[0]
                        entry_method_name_arg_ret_removed = entry_method_name_arg_removed.split("[returns]")[0]
                        exit_method_name_arg_removed = exit_method_name.split("[args]")[0]
                        exit_method_name_arg_ret_removed = exit_method_name_arg_removed.split("[returns]")[0]

                        if entry_method_name_arg_ret_removed == exit_method_name_arg_ret_removed:
                            match_flag = True
                            break

                if False == match_flag:
                    print("REMOVE the line !!! -> %d" % idx)
                    print(line)
                    idx_for_remove.append(idx)

        for _i in idx_for_remove:
            del input_dat[_i]

        cfg_file_in = open(self.cfgFile,"r", encoding='utf-8', errors='ignore')

        cfg_time_index = 0
        cfg_thread_index = 0
        cfg_flowtype_index = 0
        cfg_methodname_index = 0
        cfg_ignore_str = None
        cfg_arg_seperator = None
        cfg_filter_tag = None

        for line in cfg_file_in:
            if '[time]' in line:
                str = line.split('[time]')
                cfg_time_index = int(str[1])
            elif '[thread]' in line:
                str = line.split('[thread]')
                cfg_thread_index = int(str[1])
            elif '[flowtype]' in line:
                str = line.split('[flowtype]')
                cfg_flowtype_index = int(str[1])
            elif '[methodname]' in line:
                str = line.split('[methodname]')
                cfg_methodname_index = int(str[1])
            elif '[ignore]' in line:
                str = line.split('[ignore]')
                cfg_ignore_str = str[1].replace(' ','').replace('\n','').split(',')
            elif '[argseperator]' in line:
                str = line.split('[argseperator]')
                cfg_arg_seperator = str[1].replace(' ','').replace('\n','')
            elif '[filtertag]' in line:
                str = line.split('[filtertag]')
                cfg_filter_tag = str[1].replace(' ','').replace('\n','')

        cfg_file_in.close()

        filter_str = "(.*)%s(.*)" % (cfg_filter_tag)
        index = 0
        for idx, line in enumerate(input_dat):
            if re.match(filter_str,line):
                str_list = line[:]
                for word in cfg_ignore_str:
                    str_list = str_list.replace(word,'')
                str_list = str_list.split()

                if len(str_list) < max(cfg_time_index,cfg_thread_index,cfg_flowtype_index,cfg_methodname_index):
                    print("input line has some errors : %s"%line)
                    continue

                if '[entry]' not in line and '[exit]' not in line:
                    print("input line has some errors : %s" % line)
                    continue

                event_time = str_list[cfg_time_index]
                thread_id = str_list[cfg_thread_index]
                flow_type = str_list[cfg_flowtype_index]
                method_name_index = cfg_methodname_index

                package_method_name = ''
                for _i in range(method_name_index,len(str_list)):
                    package_method_name += str_list[_i] + ' '

                org_line = line[:]
                arguments_str = org_line.split("[args]")[1] if "[args]" in org_line else None
                return_str = org_line.split("[returns]")[1] if "[returns]" in org_line else None
                arg_removed = package_method_name.split("[args]")[0]
                ret_removed = arg_removed.split("[returns]")[0]
                parameter_removed = package_method_name.split("(")[0]
                inside_parenthesis = package_method_name.split("(")[1].split(")")[0].replace(' ','')
                parameters = None
                if '' != inside_parenthesis:
                    parameters = inside_parenthesis.split(',')
                #method_name = list(reversed(parameter_removed .split(".")))[0]
                return_type_removed = list(reversed(parameter_removed.split(' ')))
                method_name = list(reversed(re.split(STR_CLASS_SPLITTER,parameter_removed)))[0]
                package_name = ".".join(list(reversed(list(reversed(re.split(STR_CLASS_SPLITTER,parameter_removed)))[1:])))

                class_name = package_name
                if class_name == '':
                    class_name = 'Global'

                if not (thread_id in self.threads):
                    self.threads.append(thread_id)
                    stack = Stack.Stack()
                    stack.push({'class':"start", 'message':""})
                    self.callStack.append(stack)

                if not (class_name in self.lifeLine):
                    self.view.addLifeline(class_name)
                    self.lifeLine.append(class_name)

                thread_index = self.threads.index(thread_id)

                if flow_type == "[entry]":
                    self.view.sendSignal(self.callStack[thread_index].peek(),class_name,method_name,thread_index,event_time,index+1,self.callStack[thread_index],arguments_str.split(cfg_arg_seperator) if arguments_str else None,parameters)
                    self.callStack[thread_index].push({'class':class_name, 'message':method_name})

                if flow_type == "[exit]":
                    self.view.completeTask(class_name,method_name,1,event_time,index+1,self.callStack[thread_index].depth('compareClass'),return_str.split(cfg_arg_seperator) if return_str else None)
                    self.callStack[thread_index].pop()

                index += 1

            del line
            self.cnt = index

        print("kitchen --> refreshData")
        self.view.refreshData(self.cnt)
        self.view.setDrawingAvailable()
        self.toolBox.setAvailable(self.threads)
        print("kitchen start")

    def activateHide(self,flag):
        pass

    def resetAllLifelines(self):
        pass

    def activateCapture(self,flag):
        if const.STATE_INTERACTIVE_IDLE == self.stateInteractive or const.STATE_INTERACTIVE_RESET == self.stateInteractive:
            self.setInteractiveModeState(const.STATE_INTERACTIVE_CAPTURING)
        elif const.STATE_INTERACTIVE_CAPTURING == self.stateInteractive:
            self.setInteractiveModeState(const.STATE_INTERACTIVE_PROCESSING)
        elif const.STATE_INTERACTIVE_ACTIVE == self.stateInteractive:
            self.setInteractiveModeState(const.STATE_INTERACTIVE_RESET)

    def setInteractiveModeState(self,state):

        self.toolBox.notifyInteractiveStateChanged(state)

        if const.STATE_INTERACTIVE_RESET == state:
            print("setInteractiveModeState : STATE_INTERACTIVE_RESET")
            if const.STATE_INTERACTIVE_ACTIVE != self.stateInteractive:
                pass # throw exception
            self.view.reset()
            self.reset()
            self.stateInteractive = const.STATE_INTERACTIVE_RESET
            self.mutex.release()

        if const.STATE_INTERACTIVE_ACTIVE == state:
            print("setInteractiveModeState : STATE_INTERACTIVE_ACTIVE")
            if const.STATE_INTERACTIVE_PROCESSING != self.stateInteractive:
                pass # throw exception
            self.stateInteractive = const.STATE_INTERACTIVE_ACTIVE
            self.mutex.acquire()

        if const.STATE_INTERACTIVE_PROCESSING == state:
            print("setInteractiveModeState : STATE_INTERACTIVE_PROCESSING")
            if const.STATE_INTERACTIVE_CAPTURING != self.stateInteractive:
                pass # throw exception
            self.stateInteractive = const.STATE_INTERACTIVE_PROCESSING

        if const.STATE_INTERACTIVE_CAPTURING == state:
            print("setInteractiveModeState : STATE_INTERACTIVE_CAPTURING")
            if const.STATE_INTERACTIVE_IDLE != self.stateInteractive or const.STATE_INTERACTIVE_RESET != self.stateInteractive:
                pass # throw exception
            self.stateInteractive = const.STATE_INTERACTIVE_CAPTURING

    def searchMessage(self,str):
        pass

    def moveToPrev(self):
        pass

    def moveToNext(self):
        pass
<|endoftext|>


<commit_before># Copyright (c) 2012 Leif Johnson <leif@leifjohnson.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

'''This file contains optimization methods for neural networks.'''

import logging
import numpy
import theano
import theano.tensor as TT
import sys

class Trainer(object):
    '''This is an abstract base class for all trainers.'''

    def train(self, train_set, valid_set=None):
        raise NotImplementedError

    def evaluate(self, test_set):
        return [self.f_eval(*i) for i in test_set]

class SGD(Trainer):
    '''Stochastic gradient descent network trainer.'''

    def __init__(self, network, **kwargs):
        self.vf = kwargs.get('validate', 3)
        self.epochs = kwargs.get('epochs', sys.maxint)
        decay = kwargs.get('decay', 1)
        m = kwargs.get('momentum', 0)
        lr = kwargs.get('learning_rate', 0.1)

        J = network.J(**kwargs)
        t = theano.shared(numpy.cast['float32'](0), name='t')
        updates = {t: t + 1}
        for param in network.params:
            grad = TT.grad(J, param)
            heading = theano.shared(
                numpy.zeros_like(param.get_value(borrow=True)),
                name='g_%s' % param.name)
            updates[param] = param + heading
            updates[heading] = m * heading - lr * (decay ** t) * grad

        costs = [J] + network.monitors
        self.f_eval = theano.function(network.inputs, costs)
        self.f_rate = theano.function([], [lr * (decay ** t)])
        self.f_train = theano.function(network.inputs, costs, updates=updates)
        #theano.printing.pydotprint(
        #    theano.function(network.inputs, [J]), '/tmp/theano-network.png')

    def train(self, train_set, valid_set=None):
        for u in xrange(self.epochs):
            acc = [self.f_train(*i) for i in train_set]
            fmt = 'epoch %i[%.2g]: train %s'
            args = (u, self.f_rate()[0], numpy.mean(acc, axis=0))
            if valid_set is not None and self.vf > 0 and u % self.vf == 0:
                acc = [self.f_eval(*i) for i in valid_set]
                fmt += ' valid %s'
                args += (numpy.mean(acc, axis=0), )
            logging.info(fmt, *args)

class HF(Trainer):
    '''The hessian free trainer shells out to an external implementation.

    hf.py was implemented by Nicholas Boulanger-Lewandowski and developed by
    Martens and Sutskever. If you don't have a copy of the module handy, this
    class will attempt to download it from github.
    '''

    URL = 'https://raw.github.com/boulanni/theano-hf/master/hf.py'

    def __init__(self, network, **kwargs):
        try:
            import hf
        except:
            # if hf failed to import, try downloading it and saving it locally.
            import os, urllib
            path = os.path.join(os.getcwd(), 'hf.py')
            logging.error('hf import failed, attempting to download to %s', path)
            with open(path, 'w') as target:
                h = urllib.urlopen(HF.URL)
                target.write(h.read())
                h.close()
            del os
            del urllib
            import hf

        c = [network.J(**kwargs)] + network.monitors
        self.f_eval = theano.function(network.inputs, c)
        self.cg_set = kwargs.pop('cg_set')
        self.opt = hf.hf_optimizer(network.params, network.inputs, network.y, c)

        # fix mapping from kwargs into a dict to send to the hf optimizer
        kwargs['num_updates'] = kwargs.pop('epochs', sys.maxint)
        kwargs['validation_frequency'] = kwargs.pop('validate', sys.maxint)
        for k in set(kwargs) - set(self.opt.train.im_func.func_code.co_varnames[1:]):
            kwargs.pop(k)
        self.kwargs = kwargs

    def train(self, train_set, valid_set=None):
        self.opt.train(train_set, self.cg_set, validation=valid_set, **self.kwargs)

class FORCE(Trainer):
    '''FORCE is a training method for recurrent nets by Sussillo & Abbott.

    The code here still needs implementation and testing.
    '''

    def __init__(self, network, **kwargs):
        W_in, W_pool, W_out = network.weights

        n = len(W_pool.get_value(shared=True))
        alpha = kwargs.get('learning_rate', 1. / n)
        P = theano.shared(numpy.eye(n).astype(FLOAT) * alpha)

        k = T.dot(P, network.state)
        rPr = 1 + T.dot(network.state, k)
        J = network.J(**kwargs)

        updates = {}
        updates[P] = P - T.dot(k, k) / rPr
        updates[W_pool] = W_pool - J * k / rPr
        updates[W_out] = W_out - J * k / rPr
        updates[b_out] = b_out - alpha * T.grad(J, b_out)

        costs = [J] + network.monitors
        self.f_eval = theano.function(network.inputs, costs)
        self.f_train = theano.function(network.inputs, costs, updates=updates)

    def train(self, train_set, valid_set=None):
        # TODO !
        pass
<commit_msg>Replace urllib.urlopen.read with urllib.urlretrieve.<commit_after># Copyright (c) 2012 Leif Johnson <leif@leifjohnson.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

'''This file contains optimization methods for neural networks.'''

import logging
import numpy
import theano
import theano.tensor as TT
import sys

class Trainer(object):
    '''This is an abstract base class for all trainers.'''

    def train(self, train_set, valid_set=None):
        raise NotImplementedError

    def evaluate(self, test_set):
        return [self.f_eval(*i) for i in test_set]

class SGD(Trainer):
    '''Stochastic gradient descent network trainer.'''

    def __init__(self, network, **kwargs):
        self.vf = kwargs.get('validate', 3)
        self.epochs = kwargs.get('epochs', sys.maxint)
        decay = kwargs.get('decay', 1)
        m = kwargs.get('momentum', 0)
        lr = kwargs.get('learning_rate', 0.1)

        J = network.J(**kwargs)
        t = theano.shared(numpy.cast['float32'](0), name='t')
        updates = {t: t + 1}
        for param in network.params:
            grad = TT.grad(J, param)
            heading = theano.shared(
                numpy.zeros_like(param.get_value(borrow=True)),
                name='g_%s' % param.name)
            updates[param] = param + heading
            updates[heading] = m * heading - lr * (decay ** t) * grad

        costs = [J] + network.monitors
        self.f_eval = theano.function(network.inputs, costs)
        self.f_rate = theano.function([], [lr * (decay ** t)])
        self.f_train = theano.function(network.inputs, costs, updates=updates)
        #theano.printing.pydotprint(
        #    theano.function(network.inputs, [J]), '/tmp/theano-network.png')

    def train(self, train_set, valid_set=None):
        for u in xrange(self.epochs):
            acc = [self.f_train(*i) for i in train_set]
            fmt = 'epoch %i[%.2g]: train %s'
            args = (u, self.f_rate()[0], numpy.mean(acc, axis=0))
            if valid_set is not None and self.vf > 0 and u % self.vf == 0:
                acc = [self.f_eval(*i) for i in valid_set]
                fmt += ' valid %s'
                args += (numpy.mean(acc, axis=0), )
            logging.info(fmt, *args)

class HF(Trainer):
    '''The hessian free trainer shells out to an external implementation.

    hf.py was implemented by Nicholas Boulanger-Lewandowski and developed by
    Martens and Sutskever. If you don't have a copy of the module handy, this
    class will attempt to download it from github.
    '''

    URL = 'https://raw.github.com/boulanni/theano-hf/master/hf.py'

    def __init__(self, network, **kwargs):
        try:
            import hf
        except:
            # if hf failed to import, try downloading it and saving it locally.
            import os, urllib
            logging.error('hf import failed, attempting to download %s', HF.URL)
            path = os.path.join(os.getcwd(), 'hf.py')
            urllib.urlretrieve(HF.URL, path)
            logging.error('downloaded hf code to %s', path)
            del os
            del urllib
            import hf

        c = [network.J(**kwargs)] + network.monitors
        self.f_eval = theano.function(network.inputs, c)
        self.cg_set = kwargs.pop('cg_set')
        self.opt = hf.hf_optimizer(network.params, network.inputs, network.y, c)

        # fix mapping from kwargs into a dict to send to the hf optimizer
        kwargs['num_updates'] = kwargs.pop('epochs', sys.maxint)
        kwargs['validation_frequency'] = kwargs.pop('validate', sys.maxint)
        for k in set(kwargs) - set(self.opt.train.im_func.func_code.co_varnames[1:]):
            kwargs.pop(k)
        self.kwargs = kwargs

    def train(self, train_set, valid_set=None):
        self.opt.train(train_set, self.cg_set, validation=valid_set, **self.kwargs)

class FORCE(Trainer):
    '''FORCE is a training method for recurrent nets by Sussillo & Abbott.

    The code here still needs implementation and testing.
    '''

    def __init__(self, network, **kwargs):
        W_in, W_pool, W_out = network.weights

        n = len(W_pool.get_value(shared=True))
        alpha = kwargs.get('learning_rate', 1. / n)
        P = theano.shared(numpy.eye(n).astype(FLOAT) * alpha)

        k = T.dot(P, network.state)
        rPr = 1 + T.dot(network.state, k)
        J = network.J(**kwargs)

        updates = {}
        updates[P] = P - T.dot(k, k) / rPr
        updates[W_pool] = W_pool - J * k / rPr
        updates[W_out] = W_out - J * k / rPr
        updates[b_out] = b_out - alpha * T.grad(J, b_out)

        costs = [J] + network.monitors
        self.f_eval = theano.function(network.inputs, costs)
        self.f_train = theano.function(network.inputs, costs, updates=updates)

    def train(self, train_set, valid_set=None):
        # TODO !
        pass
<|endoftext|>


<commit_before>from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from django.conf import settings
from django.db import models

from taggit.managers import TaggableManager
from taggit.utils import edit_string_for_tags

from markdown_deux import markdown
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter

from snipts.utils import slugify_uniquely

import md5

site = Site.objects.all()[0]

class Snipt(models.Model):
    """An individual Snipt."""

    user     = models.ForeignKey(User, blank=True, null=True)

    title       = models.CharField(max_length=255)
    slug        = models.SlugField(max_length=255, blank=True)
    tags        = TaggableManager()

    lexer      = models.CharField(max_length=50)
    code       = models.TextField()
    stylized   = models.TextField(blank=True, null=True)
    embedded   = models.TextField(blank=True, null=True)
    line_count = models.IntegerField(blank=True, null=True, default=None)

    key      = models.CharField(max_length=100, blank=True, null=True)
    public   = models.BooleanField(default=False)

    created  = models.DateTimeField(auto_now_add=True, editable=False)
    modified = models.DateTimeField(auto_now=True, editable=False)

    def save(self, *args, **kwargs):

        if not self.slug:
            self.slug = slugify_uniquely(self.title, Snipt)

        if not self.key:
            self.key = md5.new(self.slug).hexdigest()

        if self.lexer == 'markdown':
            self.stylized = markdown(self.code, 'default')
        else:
            self.stylized = highlight(self.code,
                                      get_lexer_by_name(self.lexer, encoding='UTF-8'),
                                      HtmlFormatter())
        self.line_count = len(self.code.split('\n'))

        if self.lexer == 'markdown':
            lexer_for_embedded = 'text'
        else:
            lexer_for_embedded = self.lexer

        embedded = highlight(self.code,
                             get_lexer_by_name(lexer_for_embedded, encoding='UTF-8'),
                             HtmlFormatter(
                                 style='native',
                                 noclasses=True,
                                 prestyles="""
                                     background-color: #1C1C1C;
                                     border-radius: 5px;
                                     color: #D0D0D0;
                                     display: block;
                                     font: 11px Monaco, monospace !important;
                                     margin: 0;
                                     overflow: auto;
                                     padding: 15px;
                                     -webkit-border-radius: 5px;
                                     -moz-border-radius: 5px;
                                     """))
        embedded = (embedded.replace("\\\"","\\\\\"")
                            .replace('\'','\\\'')
                            .replace('\\n','\\\\n')
                            .replace("\\x", "\\\\x")
                            .replace('\\'', '\\\\'')
                            .replace('\\s', '\\\\s')
                            .replace('background: #202020', ''))
        self.embedded = embedded

        return super(Snipt, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        if self.public:
            return '/{}/{}/'.format(self.user.username, self.slug)
        else:
            return '/{}/{}/?key={}'.format(self.user.username, self.slug, self.key)

    def get_full_absolute_url(self):
        if settings.DEBUG:
            root = 'http://snipt.localhost'
        else:
            root = 'https://snipt.net'
        return '{}/{}/{}/'.format(root, self.user.username, self.slug)

    def get_embed_url(self):
        return 'https://{}/embed/{}/'.format(site.domain, self.key)

    @property
    def sorted_tags(self):
        return self.tags.all().order_by('name')

    @property
    def tags_list(self):
        return edit_string_for_tags(self.tags.all())

    @property
    def lexer_name(self):
        if self.lexer == 'markdown':
            return 'Markdown'
        else:
            return get_lexer_by_name(self.lexer).name

class Favorite(models.Model):
    snipt = models.ForeignKey(Snipt)
    user  = models.ForeignKey(User)

    created  = models.DateTimeField(auto_now_add=True, editable=False)
    modified = models.DateTimeField(auto_now=True, editable=False)

    def __unicode__(self):
        return u'{} favorited by {}'.format(self.snipt.title, self.user.username)
<commit_msg>Fix horrible security hole for generating keys<commit_after>from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from django.conf import settings
from django.db import models

from taggit.managers import TaggableManager
from taggit.utils import edit_string_for_tags

from markdown_deux import markdown
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter

from snipts.utils import slugify_uniquely

import datetime, md5

site = Site.objects.all()[0]

class Snipt(models.Model):
    """An individual Snipt."""

    user     = models.ForeignKey(User, blank=True, null=True)

    title       = models.CharField(max_length=255)
    slug        = models.SlugField(max_length=255, blank=True)
    tags        = TaggableManager()

    lexer      = models.CharField(max_length=50)
    code       = models.TextField()
    stylized   = models.TextField(blank=True, null=True)
    embedded   = models.TextField(blank=True, null=True)
    line_count = models.IntegerField(blank=True, null=True, default=None)

    key      = models.CharField(max_length=100, blank=True, null=True)
    public   = models.BooleanField(default=False)

    created  = models.DateTimeField(auto_now_add=True, editable=False)
    modified = models.DateTimeField(auto_now=True, editable=False)

    def save(self, *args, **kwargs):

        if not self.slug:
            self.slug = slugify_uniquely(self.title, Snipt)

        if not self.key:
            self.key = md5.new(self.slug + str(datetime.datetime.now())).hexdigest()

        if self.lexer == 'markdown':
            self.stylized = markdown(self.code, 'default')
        else:
            self.stylized = highlight(self.code,
                                      get_lexer_by_name(self.lexer, encoding='UTF-8'),
                                      HtmlFormatter())
        self.line_count = len(self.code.split('\n'))

        if self.lexer == 'markdown':
            lexer_for_embedded = 'text'
        else:
            lexer_for_embedded = self.lexer

        embedded = highlight(self.code,
                             get_lexer_by_name(lexer_for_embedded, encoding='UTF-8'),
                             HtmlFormatter(
                                 style='native',
                                 noclasses=True,
                                 prestyles="""
                                     background-color: #1C1C1C;
                                     border-radius: 5px;
                                     color: #D0D0D0;
                                     display: block;
                                     font: 11px Monaco, monospace !important;
                                     margin: 0;
                                     overflow: auto;
                                     padding: 15px;
                                     -webkit-border-radius: 5px;
                                     -moz-border-radius: 5px;
                                     """))
        embedded = (embedded.replace("\\\"","\\\\\"")
                            .replace('\'','\\\'')
                            .replace('\\n','\\\\n')
                            .replace("\\x", "\\\\x")
                            .replace('\\'', '\\\\'')
                            .replace('\\s', '\\\\s')
                            .replace('background: #202020', ''))
        self.embedded = embedded

        return super(Snipt, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        if self.public:
            return '/{}/{}/'.format(self.user.username, self.slug)
        else:
            return '/{}/{}/?key={}'.format(self.user.username, self.slug, self.key)

    def get_full_absolute_url(self):
        if settings.DEBUG:
            root = 'http://snipt.localhost'
        else:
            root = 'https://snipt.net'
        return '{}/{}/{}/'.format(root, self.user.username, self.slug)

    def get_embed_url(self):
        return 'https://{}/embed/{}/'.format(site.domain, self.key)

    @property
    def sorted_tags(self):
        return self.tags.all().order_by('name')

    @property
    def tags_list(self):
        return edit_string_for_tags(self.tags.all())

    @property
    def lexer_name(self):
        if self.lexer == 'markdown':
            return 'Markdown'
        else:
            return get_lexer_by_name(self.lexer).name

class Favorite(models.Model):
    snipt = models.ForeignKey(Snipt)
    user  = models.ForeignKey(User)

    created  = models.DateTimeField(auto_now_add=True, editable=False)
    modified = models.DateTimeField(auto_now=True, editable=False)

    def __unicode__(self):
        return u'{} favorited by {}'.format(self.snipt.title, self.user.username)
<|endoftext|>


<commit_before>from django.conf.urls.defaults import *
from core.views import projects, issues, comments

urlpatterns = patterns('',
    (r'^projects/$', projects, {}, 'projects' ),
    (r'^issues/(?P<project_id>\d+)/$', issues, {}, 'issues' ),
    (r'^comments/(?P<project_id>\d+)/(?P<issue_id>\d+)/$', comments, {}, 'comments' ),
)

<commit_msg>Added handler for static files in development.<commit_after>from django.conf.urls.defaults import *
from core.views import projects, issues, comments
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = patterns('',
    (r'^projects/$', projects, {}, 'projects' ),
    (r'^issues/(?P<project_id>\d+)/$', issues, {}, 'issues' ),
    (r'^comments/(?P<project_id>\d+)/(?P<issue_id>\d+)/$', comments, {}, 'comments' ),
)

urlpatterns += staticfiles_urlpatterns()
<|endoftext|>


<commit_before>from django.conf import settings

url = False
if hasattr(settings, 'KOBOFORM_URL') and settings.KOBOFORM_URL:
    url = settings.KOBOFORM_URL
else:
    url = False

active = bool(url)

autoredirect = active
if active and hasattr(settings, 'KOBOFORM_LOGIN_AUTOREDIRECT'):
    autoredirect = settings.KOBOFORM_LOGIN_AUTOREDIRECT

def redirect_url(url_param):
    if url:
        return url + url_param

def login_url(next_kobocat_url=False, next_url=False):
    # use kpi login if configuration exists
    if settings.KPI_URL:
        url = settings.KPI_URL

    if url:
        url_param = url + '/accounts/login/'
        if next_kobocat_url:
            next_url = '/kobocat%s' % next_kobocat_url
        if next_url:
            url_param += "?next=%s" % next_url
        return url_param
<commit_msg>Don't ref. before assignment<commit_after>from django.conf import settings

url = False
if hasattr(settings, 'KOBOFORM_URL') and settings.KOBOFORM_URL:
    url = settings.KOBOFORM_URL
else:
    url = False

active = bool(url)

autoredirect = active
if active and hasattr(settings, 'KOBOFORM_LOGIN_AUTOREDIRECT'):
    autoredirect = settings.KOBOFORM_LOGIN_AUTOREDIRECT

def redirect_url(url_param):
    if url:
        return url + url_param

def login_url(next_kobocat_url=False, next_url=False):
    # use kpi login if configuration exists
    if settings.KPI_URL:
        url = settings.KPI_URL

        if url:
            url_param = url + '/accounts/login/'
            if next_kobocat_url:
                next_url = '/kobocat%s' % next_kobocat_url
            if next_url:
                url_param += "?next=%s" % next_url
            return url_param
<|endoftext|>


<commit_before>"""
Created on Fri Sep 19 17:24:09 2014
@author: Natural Solutions (Thomas)
"""

from pyramid.view import view_config
from sqlalchemy import select, distinct, join, text,Table, and_, bindparam
from ecorelevesensor.models import * #(TProtocolBirdBiometry,
# 	TProtocolChiropteraCapture,TProtocolSimplifiedHabitat,
# 	TProtocolChiropteraDetection,TProtocolBuildingAndActivity,
# 	TProtocolVertebrateIndividualDeath, TProtocolStationDescription,
# 	Station, Individual,
# 	Base,
# 	DBSession,
# 	User)
import numpy as np
import datetime
from sqlalchemy.sql import func
import json
prefix = 'station'
dict_proto={
	'Bird Biometry': TProtocolBirdBiometry,
	'Chiroptera capture':TProtocolChiropteraCapture,
	'Simplified Habitat':TProtocolSimplifiedHabitat,
	'Chiroptera detection':TProtocolChiropteraDetection,
	'Building and Activities':TProtocolBuildingAndActivity,
	'station description':TProtocolStationDescription,
	'Vertebrate individual death':TProtocolVertebrateIndividualDeath,
	'Phytosociology habitat': TProtocolPhytosociologyHabitat,
	'Phytosociology releve': TProtocolPhytosociologyReleve,
	'Sighting conditions': TProtocolSightingCondition,
	'Simplified Habitat': TProtocolSimplifiedHabitat,
	'Station equipment': TProtocolStationEquipment,
	'Track clue': TProtocolTrackClue,
	'Capture Group': TProtocolCaptureGroup,
	'Capture Individual': TProtocolCaptureIndividual,
	'Nest Description': TProtocolNestDescription,
	'Clutch Description': TProtocolClutchDescription,
	'Entomo population': TProtocolEntomoPopulation,
	# 'Entomo Pop Census': TSubProtocolEntomoPopCensus,
	'Release Group': TProtocolReleaseGroup,
	'Release Individual': TProtocolReleaseIndividual,
	'Transects': TProtocolTransect,
	# 'SubProtocol Transect': TSubProtocolTransect,
	'Vertebrate group': TProtocolVertebrateGroup,
	'Vertebrate individual': TProtocolVertebrateIndividual
	}

@view_config(route_name=prefix, renderer='json', request_method='GET')
def monitoredSites(request):

	data = DBSession.query(Station).all()
	return data

@view_config(route_name=prefix+'/id', renderer='json', request_method='GET')
def monitoredSite(request):

	id_ = request.matchdict['id']
	print(id_)
	print(Station)
	data = DBSession.query(Station).filter(Station.id == id_).one()
	return data

@view_config(route_name=prefix+'/area', renderer='json', request_method='GET')
def monitoredSitesArea(request):
	print('passed')
	proto_view_Name=request.matchdict['name_vue'].replace('%20',' ')
	print ('______________')
	print (proto_view_Name)
	try :
		proto_view_Table=Base.metadata.tables[proto_view_Name]
		join_table=join(proto_view_Table, Station, proto_view_Table.c['TSta_PK_ID'] == Station.id )
	except :
		print('_______except______')
		proto_view_Table=dict_proto[proto_view_Name]()
		join_table=join(proto_view_Table, Station, proto_view_Table.FK_TSta_ID == Station.id )

	print (proto_view_Table)

	slct=select([Station.area]).distinct().select_from(join_table)
	data = DBSession.execute(slct).fetchall()

	return [row['Region' or 'Area'] for row in data]

@view_config(route_name=prefix+'/locality', renderer='json', request_method='GET')
def monitoredSitesLocality(request):
	print('passed')

	proto_view_Name=request.matchdict['name_vue'].replace('%20',' ')
	print ('______________')
	print (proto_view_Name)
	try :
		proto_view_Table=Base.metadata.tables[proto_view_Name]
		join_table=join(proto_view_Table, Station, proto_view_Table.c['TSta_PK_ID'] == Station.id )

	except :
		print('_______except______')

		proto_view_Table=dict_proto[proto_view_Name]()
		join_table=join(proto_view_Table, Station, proto_view_Table.FK_TSta_ID == Station.id )

	print (proto_view_Table)

	slct=select([Station.locality]).distinct().select_from(join_table)
	data = DBSession.execute(slct).fetchall()

	return [row['Place' or 'Locality'] for row in data]

@view_config(route_name=prefix+'/addStation', renderer='json', request_method='POST')
def insertNewStation(request):

	data=request.params
	check_duplicate_station = select([func.count(Station.id)]).where(and_(Station.date == bindparam('date'),
		Station.lat == bindparam('lat'),Station.lon == bindparam('lon')))

	if DBSession.execute(check_duplicate_station, {'date':data['Date_'], 'lat':data['LAT'], 'lon':data['LON']}).scalar() == 0:

		# get userID with fieldWorker_Name

		users_ID_query = select([User.id], User.fullname.in_((data['FieldWorker1'],data['FieldWorker2'],data['FieldWorker3'])))
		users_ID = DBSession.execute(users_ID_query).fetchall()
		users_ID=[row[0] for row in users_ID]
		if len(users_ID) <3 :
			users_ID.extend([None,None])

		station=Station(name=data['Name'],lat=data['LAT'], lon= data['LON'],
			date=data['Date_'], fieldActivityName = data['FieldActivity_Name'],
			creator=request.authenticated_userid, updateRegion=0,
			fieldWorker1=users_ID[0],fieldWorker2=users_ID[1],fieldWorker3=users_ID[2])

		DBSession.add(station)
		DBSession.flush()
		id_sta=station.id

		print(id_sta)
		return id_sta
	else :
		return 'a station exists at a same date and coordinates'

@view_config(route_name=prefix+'/searchStation', renderer='json', request_method='GET')
def check_newStation (request):
	print ('_____________________')

	data=request.params
	check_duplicate_station = select([func.count(Station.id)]).where(and_(Station.date == bindparam('date'),
		Station.lat == bindparam('lat'),Station.lon == bindparam('lon')))

	if DBSession.execute(check_duplicate_station, {'date':data['Date_'], 'lat':data['LAT'], 'lon':data['LON']}).scalar() == 0:
		return 0
	else :

		return 1

@view_config(route_name=prefix+'/addProtocol', renderer='json', request_method='POST')
def insert_protocol (request):
	print('----_____----')

	print(request.params)
	data=dict(request.params)
	print(data)
	protocolName=data['name']

	# insert new row in the protocol

	try :
		new_proto=dict_proto[protocolName]()
		# setattr(new_proto,'FK_TSta_ID',data.get('TSta_PK_ID'))
		new_proto.InitFromFields(data)
		DBSession.add(new_proto)
		DBSession.flush()
		id_proto= new_proto.PK
	except :
		DBSession.commit()

	return id_proto

@view_config(route_name=prefix+'/updateProtocol', renderer='json', request_method='POST')
def uptdate_protocol (request):

	date=request.params
	up_proto=dict_proto[data.get('protocolName')]

@view_config(route_name=prefix+'/updateProtocol', renderer='json', request_method='GET')
def get_protocol (request):

	data=request.params
	id_sta=data.get('id_sta')
	proto_onSta={}
	for protoName, Tproto in dict_proto :
		print(protoName+' : '+ Tproto)
		query=select(Tproto,Tproto.FK_TSta_ID==id_sta)
		res=DBSession.execute(query).scalar()
		print(res)
		if res!=None :
			proto_onSta[protoName]=res

	return proto_onSta

@view_config(route_name=prefix+'/station_byDate', renderer='json', request_method='GET')
def station_byDate (request) :

	data=reques.params

	query= select(Station).filter(Station.date>=data.get('begin_date')).filter(Station.date<=data.get('end_date'))
	result= DBSession.execute(query).fetchall()

	return result

<commit_msg>update protocol<commit_after>"""
Created on Fri Sep 19 17:24:09 2014
@author: Natural Solutions (Thomas)
"""

from pyramid.view import view_config
from sqlalchemy import select, distinct, join, text,Table, and_, bindparam
from ecorelevesensor.models import * #(TProtocolBirdBiometry,
# 	TProtocolChiropteraCapture,TProtocolSimplifiedHabitat,
# 	TProtocolChiropteraDetection,TProtocolBuildingAndActivity,
# 	TProtocolVertebrateIndividualDeath, TProtocolStationDescription,
# 	Station, Individual,
# 	Base,
# 	DBSession,
# 	User)
import numpy as np
import datetime
from sqlalchemy.sql import func
import json
prefix = 'station'
dict_proto={
	'Bird Biometry': TProtocolBirdBiometry,
	'Chiroptera capture':TProtocolChiropteraCapture,
	'Simplified Habitat':TProtocolSimplifiedHabitat,
	'Chiroptera detection':TProtocolChiropteraDetection,
	'Building and Activities':TProtocolBuildingAndActivity,
	'station description':TProtocolStationDescription,
	'Vertebrate individual death':TProtocolVertebrateIndividualDeath,
	'Phytosociology habitat': TProtocolPhytosociologyHabitat,
	'Phytosociology releve': TProtocolPhytosociologyReleve,
	'Sighting conditions': TProtocolSightingCondition,
	'Simplified Habitat': TProtocolSimplifiedHabitat,
	'Station equipment': TProtocolStationEquipment,
	'Track clue': TProtocolTrackClue,
	'Capture Group': TProtocolCaptureGroup,
	'Capture Individual': TProtocolCaptureIndividual,
	'Nest Description': TProtocolNestDescription,
	'Clutch Description': TProtocolClutchDescription,
	'Entomo population': TProtocolEntomoPopulation,
	# 'Entomo Pop Census': TSubProtocolEntomoPopCensus,
	'Release Group': TProtocolReleaseGroup,
	'Release Individual': TProtocolReleaseIndividual,
	'Transects': TProtocolTransect,
	# 'SubProtocol Transect': TSubProtocolTransect,
	'Vertebrate group': TProtocolVertebrateGroup,
	'Vertebrate individual': TProtocolVertebrateIndividual
	}

@view_config(route_name=prefix, renderer='json', request_method='GET')
def monitoredSites(request):

	data = DBSession.query(Station).all()
	return data

@view_config(route_name=prefix+'/id', renderer='json', request_method='GET')
def monitoredSite(request):

	id_ = request.matchdict['id']
	print(id_)
	print(Station)
	data = DBSession.query(Station).filter(Station.id == id_).one()
	return data

@view_config(route_name=prefix+'/area', renderer='json', request_method='GET')
def monitoredSitesArea(request):
	print('passed')
	proto_view_Name=request.matchdict['name_vue'].replace('%20',' ')
	print ('______________')
	print (proto_view_Name)
	try :
		proto_view_Table=Base.metadata.tables[proto_view_Name]
		join_table=join(proto_view_Table, Station, proto_view_Table.c['TSta_PK_ID'] == Station.id )
	except :
		print('_______except______')
		proto_view_Table=dict_proto[proto_view_Name]()
		join_table=join(proto_view_Table, Station, proto_view_Table.FK_TSta_ID == Station.id )

	print (proto_view_Table)

	slct=select([Station.area]).distinct().select_from(join_table)
	data = DBSession.execute(slct).fetchall()

	return [row['Region' or 'Area'] for row in data]

@view_config(route_name=prefix+'/locality', renderer='json', request_method='GET')
def monitoredSitesLocality(request):
	print('passed')

	proto_view_Name=request.matchdict['name_vue'].replace('%20',' ')
	print ('______________')
	print (proto_view_Name)
	try :
		proto_view_Table=Base.metadata.tables[proto_view_Name]
		join_table=join(proto_view_Table, Station, proto_view_Table.c['TSta_PK_ID'] == Station.id )

	except :
		print('_______except______')

		proto_view_Table=dict_proto[proto_view_Name]()
		join_table=join(proto_view_Table, Station, proto_view_Table.FK_TSta_ID == Station.id )

	print (proto_view_Table)

	slct=select([Station.locality]).distinct().select_from(join_table)
	data = DBSession.execute(slct).fetchall()

	return [row['Place' or 'Locality'] for row in data]

@view_config(route_name=prefix+'/addStation', renderer='json', request_method='POST')
def insertNewStation(request):

	data=request.params
	check_duplicate_station = select([func.count(Station.id)]).where(and_(Station.date == bindparam('date'),
		Station.lat == bindparam('lat'),Station.lon == bindparam('lon')))

	if DBSession.execute(check_duplicate_station, {'date':data['Date_'], 'lat':data['LAT'], 'lon':data['LON']}).scalar() == 0:

		# get userID with fieldWorker_Name

		users_ID_query = select([User.id], User.fullname.in_((data['FieldWorker1'],data['FieldWorker2'],data['FieldWorker3'])))
		users_ID = DBSession.execute(users_ID_query).fetchall()
		users_ID=[row[0] for row in users_ID]
		if len(users_ID) <3 :
			users_ID.extend([None,None])

		station=Station(name=data['Name'],lat=data['LAT'], lon= data['LON'],
			date=data['Date_'], fieldActivityName = data['FieldActivity_Name'],
			creator=request.authenticated_userid, updateRegion=0,
			fieldWorker1=users_ID[0],fieldWorker2=users_ID[1],fieldWorker3=users_ID[2])

		DBSession.add(station)
		DBSession.flush()
		id_sta=station.id

		print(id_sta)
		return id_sta
	else :
		return 'a station exists at a same date and coordinates'

@view_config(route_name=prefix+'/searchStation', renderer='json', request_method='GET')
def check_newStation (request):
	print ('_____________________')

	data=request.params
	check_duplicate_station = select([func.count(Station.id)]).where(and_(Station.date == bindparam('date'),
		Station.lat == bindparam('lat'),Station.lon == bindparam('lon')))

	if DBSession.execute(check_duplicate_station, {'date':data['Date_'], 'lat':data['LAT'], 'lon':data['LON']}).scalar() == 0:
		return 0
	else :

		return 1

@view_config(route_name=prefix+'/addProtocol', renderer='json', request_method='POST')
def insert_protocol (request):
	print('----_____----')

	print(request.params)
	data=dict(request.params)
	print(data)
	protocolName=data['name']

	# insert new row in the protocol
	new_proto=dict_proto[protocolName]()
	new_proto.InitFromFields(data)

	try :
		DBSession.add(new_proto)
		DBSession.flush()
		id_proto= new_proto.PK
	except :
		DBSession.commit()

	return id_proto

@view_config(route_name=prefix+'/updateProtocol', renderer='json', request_method='POST')
def uptdate_protocol (request):

	date=request.params
	up_proto=dict_proto[data.get('protocolName')]

@view_config(route_name=prefix+'/updateProtocol', renderer='json', request_method='GET')
def get_protocol (request):

	data=request.params
	id_sta=data.get('id_sta')
	proto_onSta={}
	for protoName, Tproto in dict_proto :
		print(protoName+' : '+ Tproto)
		query=select(Tproto,Tproto.FK_TSta_ID==id_sta)
		res=DBSession.execute(query).scalar()
		print(res)
		if res!=None :
			proto_onSta[protoName]=res

	return proto_onSta

@view_config(route_name=prefix+'/station_byDate', renderer='json', request_method='GET')
def station_byDate (request) :

	data=reques.params

	query= select(Station).filter(Station.date>=data.get('begin_date')).filter(Station.date<=data.get('end_date'))
	result= DBSession.execute(query).fetchall()

	return result

<|endoftext|>


<commit_before>#!/bin/false

# This file is part of Espruino, a JavaScript interpreter for Microcontrollers
#
# Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# ----------------------------------------------------------------------------------------
# Reads board information from boards/BOARDNAME.py - used by build_board_docs,
# build_pininfo, and build_platform_config
# ----------------------------------------------------------------------------------------

import subprocess;
import re;
import json;
import sys;
import os;

silent = os.getenv("SILENT");
if silent:
  class Discarder(object):
    def write(self, text):
        pass # do nothing
  # now discard everything coming out of stdout
  sys.stdout = Discarder()

# Scans files for comments of the form /*JSON......*/
#
# Comments look like:
#
#/*JSON{ "type":"staticmethod|staticproperty|constructor|method|property|function|variable|class|library",
#                      // class = built-in class that does not require instantiation
#                      // library = built-in class that needs require('classname')
#         "class" : "Double", "name" : "doubleToIntBits",
#         "needs_parentName":true,           // optional - if for a method, this makes the first 2 args parent+parentName (not just parent)
#         "generate_full|generate|wrap" : "*(JsVarInt*)&x",
#         "description" : " Convert the floating point value given into an integer representing the bits contained in it",
#         "params" : [ [ "x" , "float|int|JsVar|JsVarName|JsVarArray", "A floating point number"] ],
#                               // float - parses into a JsVarFloat which is passed to the function
#                               // int - parses into a JsVarInt which is passed to the function
#                               // JsVar - passes a JsVar* to the function (after skipping names)
#                               // JsVarName - passes a JsVar* to the function (WITHOUT skipping names)
#                               // JsVarArray - parses this AND ANY SUBSEQUENT ARGUMENTS into a JsVar of type JSV_ARRAY. THIS IS ALWAYS DEFINED, EVEN IF ZERO LENGTH. Currently it must be the only parameter
#         "return" : ["int|float", "The integer representation of x"],
#         "no_create_links":1                // optional - if this is set then hyperlinks are not created when this name is mentioned (good example = bit() )
#         "not_real_object" : "anything",    // optional - for classes, this means we shouldn't treat this as a built-in object, as internally it isn't stored in a JSV_OBJECT
#         "prototype" : "Object",    // optional - for classes, this is what their prototype is. It's particlarly helpful if not_real_object, because there is no prototype var in that case
#         "check" : "jsvIsFoo(var)", // for classes - this is code that returns true if 'var' is of the given type
#         "ifndef" : "SAVE_ON_FLASH", // if the given preprocessor macro is defined, don't implement this
#         "ifdef" : "USE_LCD_FOO", // if the given preprocessor macro isn't defined, don't implement this
#}*/
#
# description can be an array of strings as well as a simple string (in which case each element is separated by a newline),
# and adding ```sometext``` in the description surrounds it with HTML code tags
#

def get_jsondata(is_for_document):
        scriptdir = os.path.dirname	(os.path.realpath(__file__))
        print "Script location "+scriptdir
        os.chdir(scriptdir+"/..")

        jswraps = []
        defines = []

        if len(sys.argv)>1:
          print "Using files from command line"
          for i in range(1,len(sys.argv)):
            arg = sys.argv[i]
            if arg[0]=="-":
              if arg[1]=="D":
                defines.append(arg[2:])
              else:
                print "Unknown command-line option"
                exit(1)
            else:
              jswraps.append(arg)
        else:
          print "Scanning for jswrap.c files"
          jswraps = subprocess.check_output(["find", ".", "-name", "jswrap*.c"]).strip().split("\n")

        if len(defines)>1:
          print "Got #DEFINES:"
          for d in defines: print d

        jsondatas = []
        for jswrap in jswraps:
          # ignore anything from archives
          if jswrap.startswith("./archives/"): continue

          # now scan
          print "Scanning "+jswrap
          code = open(jswrap, "r").read()

          if is_for_document and "DO_NOT_INCLUDE_IN_DOCS" in code:
            print "FOUND 'DO_NOT_INCLUDE_IN_DOCS' IN FILE "+jswrap
            continue

          for comment in re.findall(r"/\*JSON.*?\*/", code, re.VERBOSE | re.MULTILINE | re.DOTALL):
            jsonstring = comment[6:-2]
#            print "Parsing "+jsonstring
            try:
              jsondata = json.loads(jsonstring)
              jsondata["filename"] = jswrap
              jsondata["include"] = jswrap[:-2]+".h"
              if ("ifndef" in jsondata) and (jsondata["ifndef"] in defines):
                print "Dropped because of #ifndef "+jsondata["ifndef"]
              if ("ifdef" in jsondata) and not (jsondata["ifdef"] in defines):
                print "Dropped because of #ifdef "+jsondata["ifdef"]
              else:
                jsondatas.append(jsondata)
            except ValueError as e:
              sys.stderr.write( "JSON PARSE FAILED -",  e , "\n")
              exit(1)
            except:
              sys.stderr.write( "JSON PARSE FAILED",  sys.exc_info()[0], "\n" )
              exit(1)
        print "Scanning finished."
        return jsondatas

def get_struct_from_jsondata(jsondata):
  context = dict()

  def checkClass(details):
    cl = details["class"]
    if not cl in context:
      context[cl] = {"methods": {}, "props": {}, "staticmethods": {}, "staticprops": {}, "desc": details.get("description", "")}
    return cl

  def addConstructor(details):
    cl = checkClass(details)
    context[cl]["constructor"] = {"params": details.get("params", []), "return": details.get("return", []), "desc": details.get("description", "")}

  def addMethod(details, type = ""):
    cl = checkClass(details)
    context[cl][type + "methods"][details["name"]] = {"params": details.get("params", []), "return": details.get("return", []), "desc": details.get("description", "")}

  def addProp(details, type = ""):
    cl = checkClass(details)
    context[cl][type + "props"][details["name"]] = {"return": details.get("return", []), "desc": details.get("description", "")}

  def addFunc(details):
    context[details["name"]] = {"return": details.get("return", []), "desc": details.get("description", "")}

  def addObj(details):
    context[details["name"]] = {"instanceof": details.get("instanceof", ""), "desc": details.get("description", "")}

  def addLib(details):
    context["require('" + details["class"] + "')"] = {"desc": details.get("description", "")}

  def addVar(details):
    return

  for data in jsondata:
    type = data["type"]
    if type=="class":
      checkClass(data)
    elif type=="constructor":
      addConstructor(data)
    elif type=="method":
      addMethod(data)
    elif type=="property":
      addProp(data)
    elif type=="staticmethod":
      addMethod(data, "static")
    elif type=="staticproperty":
      addProp(data, "static")
    elif type=="function":
      addFunc(data)
    elif type=="object":
      addObj(data)
    elif type=="library":
      addLib(data)
    elif type=="variable":
      addVar(data)
    else:
      print(json.dumps(data, sort_keys=True, indent=2))

  return context

def get_includes_from_jsondata(jsondatas):
        includes = []
        for jsondata in jsondatas:
          include = jsondata["include"]
          if not include in includes:
                includes.append(include)
        return includes

def is_property(jsondata):
  return jsondata["type"]=="property" or jsondata["type"]=="staticproperty"

def get_script_dir():
        return os.path.dirname(os.path.realpath(__file__))
def get_version():
        scriptdir = get_script_dir()
        jsutils = scriptdir+"/../src/jsutils.h"
        return subprocess.check_output(["sed", "-ne", "s/^.*JS_VERSION.*\"\(.*\)\"/\\1/p", jsutils]).strip()

def get_name_or_space(jsondata):
        if "name" in jsondata: return jsondata["name"]
        return ""

def get_bootloader_size():
        return 10*1024;

def get_board_binary_name(board):
        return board.info["binary_name"].replace("%v", get_version());
<commit_msg>Add description<commit_after>#!/bin/false

# This file is part of Espruino, a JavaScript interpreter for Microcontrollers
#
# Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# ----------------------------------------------------------------------------------------
# Reads board information from boards/BOARDNAME.py - used by build_board_docs,
# build_pininfo, and build_platform_config
# ----------------------------------------------------------------------------------------

import subprocess;
import re;
import json;
import sys;
import os;

silent = os.getenv("SILENT");
if silent:
  class Discarder(object):
    def write(self, text):
        pass # do nothing
  # now discard everything coming out of stdout
  sys.stdout = Discarder()

# Scans files for comments of the form /*JSON......*/
#
# Comments look like:
#
#/*JSON{ "type":"staticmethod|staticproperty|constructor|method|property|function|variable|class|library",
#                      // class = built-in class that does not require instantiation
#                      // library = built-in class that needs require('classname')
#         "class" : "Double", "name" : "doubleToIntBits",
#         "needs_parentName":true,           // optional - if for a method, this makes the first 2 args parent+parentName (not just parent)
#         "generate_full|generate|wrap" : "*(JsVarInt*)&x",
#         "description" : " Convert the floating point value given into an integer representing the bits contained in it",
#         "params" : [ [ "x" , "float|int|JsVar|JsVarName|JsVarArray", "A floating point number"] ],
#                               // float - parses into a JsVarFloat which is passed to the function
#                               // int - parses into a JsVarInt which is passed to the function
#                               // JsVar - passes a JsVar* to the function (after skipping names)
#                               // JsVarName - passes a JsVar* to the function (WITHOUT skipping names)
#                               // JsVarArray - parses this AND ANY SUBSEQUENT ARGUMENTS into a JsVar of type JSV_ARRAY. THIS IS ALWAYS DEFINED, EVEN IF ZERO LENGTH. Currently it must be the only parameter
#         "return" : ["int|float", "The integer representation of x"],
#         "no_create_links":1                // optional - if this is set then hyperlinks are not created when this name is mentioned (good example = bit() )
#         "not_real_object" : "anything",    // optional - for classes, this means we shouldn't treat this as a built-in object, as internally it isn't stored in a JSV_OBJECT
#         "prototype" : "Object",    // optional - for classes, this is what their prototype is. It's particlarly helpful if not_real_object, because there is no prototype var in that case
#         "check" : "jsvIsFoo(var)", // for classes - this is code that returns true if 'var' is of the given type
#         "ifndef" : "SAVE_ON_FLASH", // if the given preprocessor macro is defined, don't implement this
#         "ifdef" : "USE_LCD_FOO", // if the given preprocessor macro isn't defined, don't implement this
#}*/
#
# description can be an array of strings as well as a simple string (in which case each element is separated by a newline),
# and adding ```sometext``` in the description surrounds it with HTML code tags
#

def get_jsondata(is_for_document):
        scriptdir = os.path.dirname	(os.path.realpath(__file__))
        print "Script location "+scriptdir
        os.chdir(scriptdir+"/..")

        jswraps = []
        defines = []

        if len(sys.argv)>1:
          print "Using files from command line"
          for i in range(1,len(sys.argv)):
            arg = sys.argv[i]
            if arg[0]=="-":
              if arg[1]=="D":
                defines.append(arg[2:])
              else:
                print "Unknown command-line option"
                exit(1)
            else:
              jswraps.append(arg)
        else:
          print "Scanning for jswrap.c files"
          jswraps = subprocess.check_output(["find", ".", "-name", "jswrap*.c"]).strip().split("\n")

        if len(defines)>1:
          print "Got #DEFINES:"
          for d in defines: print d

        jsondatas = []
        for jswrap in jswraps:
          # ignore anything from archives
          if jswrap.startswith("./archives/"): continue

          # now scan
          print "Scanning "+jswrap
          code = open(jswrap, "r").read()

          if is_for_document and "DO_NOT_INCLUDE_IN_DOCS" in code:
            print "FOUND 'DO_NOT_INCLUDE_IN_DOCS' IN FILE "+jswrap
            continue

          for comment in re.findall(r"/\*JSON.*?\*/", code, re.VERBOSE | re.MULTILINE | re.DOTALL):
            jsonstring = comment[6:-2]
#            print "Parsing "+jsonstring
            try:
              jsondata = json.loads(jsonstring)
              jsondata["filename"] = jswrap
              jsondata["include"] = jswrap[:-2]+".h"
              if ("ifndef" in jsondata) and (jsondata["ifndef"] in defines):
                print "Dropped because of #ifndef "+jsondata["ifndef"]
              if ("ifdef" in jsondata) and not (jsondata["ifdef"] in defines):
                print "Dropped because of #ifdef "+jsondata["ifdef"]
              else:
                jsondatas.append(jsondata)
            except ValueError as e:
              sys.stderr.write( "JSON PARSE FAILED -",  e , "\n")
              exit(1)
            except:
              sys.stderr.write( "JSON PARSE FAILED",  sys.exc_info()[0], "\n" )
              exit(1)
        print "Scanning finished."
        return jsondatas

# Takes the data from get_jsondata and restructures it in prepartion for output as JS
#
# Results look like:,
#{
#  "Pin": {
#    "desc": [
#      "This is the built-in class for Pins, such as D0,D1,LED1, or BTN",
#      "You can call the methods on Pin, or you can use Wiring-style functions such as digitalWrite"
#    ],
#    "methods": {
#      "read": {
#        "desc": "Returns the input state of the pin as a boolean",
#        "params": [],
#        "return": [
#          "bool",
#          "Whether pin is a logical 1 or 0"
#        ]
#      },
#      "reset": {
#        "desc": "Sets the output state of the pin to a 0",
#        "params": [],
#        "return": []
#      },
#      ...
#    },
#    "props": {},
#    "staticmethods": {},
#    "staticprops": {}
#  },
#  "print": {
#    "desc": "Print the supplied string",
#    "return": []
#  },
#  ...
#}
#

def get_struct_from_jsondata(jsondata):
  context = dict()

  def checkClass(details):
    cl = details["class"]
    if not cl in context:
      context[cl] = {"methods": {}, "props": {}, "staticmethods": {}, "staticprops": {}, "desc": details.get("description", "")}
    return cl

  def addConstructor(details):
    cl = checkClass(details)
    context[cl]["constructor"] = {"params": details.get("params", []), "return": details.get("return", []), "desc": details.get("description", "")}

  def addMethod(details, type = ""):
    cl = checkClass(details)
    context[cl][type + "methods"][details["name"]] = {"params": details.get("params", []), "return": details.get("return", []), "desc": details.get("description", "")}

  def addProp(details, type = ""):
    cl = checkClass(details)
    context[cl][type + "props"][details["name"]] = {"return": details.get("return", []), "desc": details.get("description", "")}

  def addFunc(details):
    context[details["name"]] = {"return": details.get("return", []), "desc": details.get("description", "")}

  def addObj(details):
    context[details["name"]] = {"instanceof": details.get("instanceof", ""), "desc": details.get("description", "")}

  def addLib(details):
    context["require('" + details["class"] + "')"] = {"desc": details.get("description", "")}

  def addVar(details):
    return

  for data in jsondata:
    type = data["type"]
    if type=="class":
      checkClass(data)
    elif type=="constructor":
      addConstructor(data)
    elif type=="method":
      addMethod(data)
    elif type=="property":
      addProp(data)
    elif type=="staticmethod":
      addMethod(data, "static")
    elif type=="staticproperty":
      addProp(data, "static")
    elif type=="function":
      addFunc(data)
    elif type=="object":
      addObj(data)
    elif type=="library":
      addLib(data)
    elif type=="variable":
      addVar(data)
    else:
      print(json.dumps(data, sort_keys=True, indent=2))

  return context

def get_includes_from_jsondata(jsondatas):
        includes = []
        for jsondata in jsondatas:
          include = jsondata["include"]
          if not include in includes:
                includes.append(include)
        return includes

def is_property(jsondata):
  return jsondata["type"]=="property" or jsondata["type"]=="staticproperty"

def get_script_dir():
        return os.path.dirname(os.path.realpath(__file__))
def get_version():
        scriptdir = get_script_dir()
        jsutils = scriptdir+"/../src/jsutils.h"
        return subprocess.check_output(["sed", "-ne", "s/^.*JS_VERSION.*\"\(.*\)\"/\\1/p", jsutils]).strip()

def get_name_or_space(jsondata):
        if "name" in jsondata: return jsondata["name"]
        return ""

def get_bootloader_size():
        return 10*1024;

def get_board_binary_name(board):
        return board.info["binary_name"].replace("%v", get_version());
<|endoftext|>