<commit_before># Emanuele Ruffaldi 2016
import os,sys,re
from collections import defaultdict, OrderedDict
import subprocess, Queue
import argparse
import networkx as nx

titles = ["Flight from the Dark","Fire on the Water","The Caverns of Kalte","The Chasm of Doom","Shadow on the Sand","The Kingdoms of Terror","Castle Death"];

def count_dag_paths(ordered,incoming,outgoing,first,last):
    #reverse array from: vertex index to index
    invordered = dict([(ordered[i],i) for i in range(0,len(ordered))])
    lastidx = invordered[last]
    # paths to last
    counts = defaultdict(int)
    counts[last] = 1
    print ordered
    for i in range(lastidx-1,-1,-1): # index
        ni = ordered[i]
        q = 0
        for child in outgoing[ni]:
            j = invordered[child]
            if j > i and j <= lastidx:
                q += counts[child]
        counts[ni] = q
    return counts

def bfs(graph, start,visited=set()):
    queue = [(start,None)]
    distancefrompath = dict()
    while queue:
        vertex,parent = queue.pop(0)
        if vertex not in visited:
            yield (vertex,parent, True)
            visited.add(vertex)
            queue.extend([(x,vertex) for x in graph[vertex] - visited])
        else:
            yield (vertex,parent, False)

def makedag(o,i,b):
    ro = defaultdict(set)
    ri = defaultdict(set)
    for k,v in o.iteritems():
        ro[k] = v.copy()
    for k,v in i.iteritems():
        ri[k] = v.copy()
    for a,b in b:
        ro[a].remove(b)
        ri[b].remove(a)
    return ro, ri

def analyze(outgoing,incoming,roots,last,backward,booki):

    G = nx.from_dict_of_lists(outgoing,nx.DiGraph())
    try:
        shortest = nx.shortest_path(G,1,last)
    except:
        shortest = []
    r = dict()
    visited = set()
    minmax = dict()
    ancestors = defaultdict(set)
    distancefrompath = defaultdict(lambda: 0)
    for ro in roots:
        ancestors[ro] = set()
        for node,parent,isnew in bfs(outgoing,ro,visited):
            if parent is None:
                # except quiz on book 7
                distancefrompath[node] = 0
            else:
                distancefrompath[node] = distancefrompath[parent]+1
                ancestors[node].add(parent)
                ancestors[node] |= ancestors[parent]

    for parent,children in outgoing.iteritems():
        for c in children:
            if c in ancestors[parent]:
                backward.add((parent,c))
    # then estimate for last
    r["mindist"] = len(shortest)
    if False:
        print "----"
        print "last",last
        print "distancefrompath\n",distancefrompath
        print "shortestpath\n",shortestpath
        print r
        print "book",booki,"shortest",shortest
        print "----"
    return r,shortest

    # estimate number of paths
    # estimate

def main():

    parser = argparse.ArgumentParser(description='Graphs for Lone Wolf')
    parser.add_argument('--outputs',default="pdf png svg")
    parser.add_argument('--outputpath',default="/Users/eruffaldi/Dropbox/Public/lonewolf/")
    parser.add_argument('--inputpath',default="/Users/eruffaldi/Downloads/en/xhtml/lw/")
    parser.add_argument('--contentlink',default="https://www.projectaon.org/en/xhtml/lw/")
    parser.add_argument('--clusters',default=0,type=int)
    parser.add_argument('--book',default=-1,type=int)
    args = parser.parse_args()
    #
If you wish to use your Kai Discipline of Sixth Sense, turn to 141.

    # files: sect#.htm
    #/Users/eruffaldi/Downloads/en/xhtml/

    #for x in os.listdir(sys.argv[1]):
    #   if x.startswith("sect") and x.endswith(".htm"):

    outputs = args.outputs.split(" ")
    output = args.outputpath
    input = args.inputpath
    link=args.contentlink
    clusters = args.clusters != 0
    print "Cluster Mode",args.clusters

    oo = open("script.sh","w")
    booki = 0
    if clusters:
        outname = os.path.join(input,"all"+".dot")
        print "generating",outname
        outfile = open(outname,"wb")
        outfile.write("digraph G {\n")

    allstats = []
    for dirname in os.listdir(input):
        fx = os.path.join(input,dirname)
        if not os.path.isdir(fx):
            continue
        booki += 1
        if args.book != -1 and args.book != booki:
            continue
        if not clusters:
            outname = os.path.abspath(os.path.join(input,dirname+".dot"))
            print "generating",outname
            outfile = open(outname,"wb")
            outfile.write("digraph G {\n")
        else:
            outfile.write("subgraph cluster_%d {\n" % booki)
        outfile.write("\tlabel=\"%s\"\n" % titles[booki-1])

        allpairs = OrderedDict()
        incoming = defaultdict(set)
        outgoing = defaultdict(set)
        sommer = set()
        pagetype = defaultdict(set)
        pagelink = dict()
        ancestors = defaultdict(set)
        for i in range(1,500):
                #name = int(x[4:-4])
                name = "sect%d.htm" % i
                fp = os.path.join(fx,name)
                if not os.path.isfile(fp):
                    break
                pagelink[i] = name #os.path.abspath(fp)
                y = open(fp,"rb").read()
                if y.find("COMBAT SKILL") >= 0:
                    pagetype[i].add("combat")
                if y.find("Sommerswerd") >= 0:
                    sommer.add(i)
                for p in re.findall("",y):
                    p = int(p)
                    allpairs[(i,p)] = 1
                    incoming[p].add(i)
                    outgoing[i].add(p)
                    ancestors[p] |= ancestors[i]
        last = i-1

        if booki == 5:
            special = [(331,373)]
            for x,y in special:
                incoming[y].add(x)
                outgoing[x].add(y)
                allpairs[(x,y)] = 1

        # we want to estimate if an edge is backward, that is a given page (node) goes back to another
        # one that has been visited earlier
        backward = set()
        roots = [i for i in range(1,last+1) if len(incoming[i]) == 0]
        s,shortest = analyze(outgoing,incoming,roots,last,backward,booki)
        s["book"] = booki
        allstats.append(s)
        outgoing_dag,incoming_dag = makedag(outgoing,incoming,backward)

        print "apply shortest",len(shortest)
        for i in range(1,len(shortest)):
            allpairs[(shortest[i-1],shortest[i])] = 2
        # TBD print "book",booki,dirname,s
        dg = nx.from_dict_of_lists(outgoing_dag,nx.DiGraph())
        if nx.is_directed_acyclic_graph(dg):
            ordered = nx.topological_sort(dg);

            # topological sort
            cp = count_dag_paths(ordered,incoming_dag,outgoing_dag,1,last)
            totalpaths = cp[1]
            s["totalpaths"] = totalpaths
            if False:
                # Something for estimating the mandatory nodes, actually few of them
                necessarynodes = set([k for k,v in cp.iteritems() if v >= totalpaths])
                necessarynodes.remove(1)
                s["needednodes1"] = len(necessarynodes)
                necessarynodes = set()
                for c in outgoing[1]:
                    totalpathsc = cp[c]
                    necessarynodesc = set([k for k,v in cp.iteritems() if v >= totalpathsc])
                    necessarynodes |= necessarynodesc
                s["needednodes2"] = len(necessarynodes)
        else:
            if False:
                incoming = incoming_dag
                outgoing = outgoing_dag
                for a,b in backward:
                    del allpairs[(a,b)]
            s["totalpaths"] = 0
            s["needednodes1"] = 0
            s["needednodes2"] = 0

        pagedict = defaultdict(dict)
        for i in range(2,last):
            o = outgoing[i]
            ww = dict()
            if "combat" in pagetype[i]:
                ww["shape"] = "tripleoctagon"
                ww["fillcolor"] = "orange"
                ww["style"] = "filled"
                if i in sommer:
                    ww["fillcolor"] = "magenta"
                else:
                    ww["fillcolor"] = "orange"
            else:
                ww["shape"] = "circle"
            if len(o) == 0:
                ww["fillcolor"] = "red"
                ww["style"] = "filled"
                ww["shape"] = "Mcircle"
            elif len(incoming[i]) == 0:
                ww["fillcolor"] = "green"
                ww["style"] = "filled"
            pagedict[i].update(ww)

        for i in range(1,last+1):
            pagedict[i]["label"] = "\"%d\"" % i #/%d/%d\"" % (i,distancefromroot[i],maxincoming[i])
            pagedict[i]["URL"] = "\"%s/%s/%s\"" % (link,dirname,pagelink[i])
            pagedict[i]["target"] = "_blank"

        pagedict[1].update(dict(fillcolor="green",style="filled"))
        pagedict[last].update(dict(fillcolor="green",style="filled"))

        for i in range(1,last+1):
            ww = pagedict[i]
            if len(ww) > 0:
                outfile.write(" b%dp%d [%s];\n" % (booki,i,",".join(["%s=%s" % (x,y) for x,y in ww.iteritems()])))

        for k,v in allpairs.iteritems():
            pfrom,pto = k
            sw = {}
            if (pfrom,pto) in backward:
                sw["color"] = "red"
            if v == 2:
                sw["penwidth"] =3.0
            outfile.write("b%dp%d -> b%dp%d [%s];\n" % (booki,pfrom,booki,pto,",".join(["%s=%s" % (x,y) for x,y in sw.iteritems()])))

        outfile.write("}\n")

        if not clusters:
            for t in outputs:
                oo.write(" ".join(["dot", "-T" + t,outname,"-o"+os.path.join(output,dirname+"."+t)])+"\n")
            #subprocess.call(["dot", "-T" + t,outname,"-o"+os.path.join(output,dirname+"."+t)],shell=True)
    if clusters:
        outfile.write("}\n")
        for t in outputs:
            oo.write(" ".join(["dot", "-T" + t,outname,"-o"+os.path.join(output,dirname+"."+t)])+"\n")

    print "created script.sh"
    oo.close()
    if True:
        print "\t".join(["Book","Shortest","Total Paths"])
        for i,a in enumerate(allstats):
            print "%d\t%d\t%d" % (a["book"],a["mindist"],a["totalpaths"]) #,a["maxdist"])

if __name__ == '__main__':
    main()
<commit_msg>added random paths<commit_after># Emanuele Ruffaldi 2016
import os,sys,re
from collections import defaultdict, OrderedDict
import subprocess, Queue
import argparse
import networkx as nx

titles = ["Flight from the Dark","Fire on the Water","The Caverns of Kalte","The Chasm of Doom","Shadow on the Sand","The Kingdoms of Terror","Castle Death"];

def count_dag_paths(ordered,incoming,outgoing,first,last):
    #reverse array from: vertex index to index
    invordered = dict([(ordered[i],i) for i in range(0,len(ordered))])
    lastidx = invordered[last]
    # paths to last
    counts = defaultdict(int)
    counts[last] = 1
    #print ordered
    for i in range(lastidx-1,-1,-1): # index
        ni = ordered[i]
        q = 0
        for child in outgoing[ni]:
            j = invordered[child]
            if j > i and j <= lastidx:
                q += counts[child]
        counts[ni] = q
    return counts

def bfs(graph, start,visited=set()):
    queue = [(start,None)]
    distancefrompath = dict()
    while queue:
        vertex,parent = queue.pop(0)
        if vertex not in visited:
            yield (vertex,parent, True)
            visited.add(vertex)
            queue.extend([(x,vertex) for x in graph[vertex] - visited])
        else:
            yield (vertex,parent, False)

def makedag(o,i,b):
    ro = defaultdict(set)
    ri = defaultdict(set)
    for k,v in o.iteritems():
        ro[k] = v.copy()
    for k,v in i.iteritems():
        ri[k] = v.copy()
    for a,b in b:
        ro[a].remove(b)
        ri[b].remove(a)
    return ro, ri

def analyzeshortest(outgoing,last):
    G = nx.from_dict_of_lists(outgoing,nx.DiGraph())
    try:
        shortest = nx.shortest_path(G,1,last)
    except:
        shortest = []
    return shortest

def analyze(outgoing,incoming,roots,last,backward,booki):

    r = dict()
    visited = set()
    minmax = dict()
    ancestors = defaultdict(set)
    distancefrompath = defaultdict(lambda: 0)
    for ro in roots:
        ancestors[ro] = set()
        for node,parent,isnew in bfs(outgoing,ro,visited):
            if parent is None:
                # except quiz on book 7
                distancefrompath[node] = 0
            else:
                distancefrompath[node] = distancefrompath[parent]+1
                ancestors[node].add(parent)
                ancestors[node] |= ancestors[parent]

    for parent,children in outgoing.iteritems():
        for c in children:
            if c in ancestors[parent]:
                backward.add((parent,c))
    return r

    # estimate number of paths
    # estimate

def main():

    parser = argparse.ArgumentParser(description='Graphs for Lone Wolf')
    parser.add_argument('--outputs',default="pdf png svg")
    parser.add_argument('--outputpath',default="/Users/eruffaldi/Dropbox/Public/lonewolf/")
    parser.add_argument('--inputpath',default="/Users/eruffaldi/Documents/personalfun/lonewolf/xhtml/lw/")
    parser.add_argument('--contentlink',default="https://www.projectaon.org/en/xhtml/lw/")
    parser.add_argument('--clusters',default=0,type=int)
    parser.add_argument('--book',default=-1,type=int)
    args = parser.parse_args()
    #
If you wish to use your Kai Discipline of Sixth Sense, turn to 141.

    # files: sect#.htm
    #/Users/eruffaldi/Downloads/en/xhtml/

    #for x in os.listdir(sys.argv[1]):
    #   if x.startswith("sect") and x.endswith(".htm"):

    outputs = args.outputs.split(" ")
    output = args.outputpath
    input = args.inputpath
    link=args.contentlink
    clusters = args.clusters != 0
    print "Cluster Mode",args.clusters

    oo = open("script.sh","w")
    booki = 0
    if clusters:
        outname = os.path.join(input,"all"+".dot")
        print "generating",outname
        outfile = open(outname,"wb")
        outfile.write("digraph G {\n")

    allstats = []
    for dirname in os.listdir(input):
        fx = os.path.join(input,dirname)
        if not os.path.isdir(fx):
            continue
        booki += 1
        if args.book != -1 and args.book != booki:
            continue
        if not clusters:
            outname = os.path.abspath(os.path.join(input,dirname+".dot"))
            print "generating",outname
            outfile = open(outname,"wb")
            outfile.write("digraph G {\n")
        else:
            outfile.write("subgraph cluster_%d {\n" % booki)
        outfile.write("\tlabel=\"%s\"\n" % titles[booki-1])

        allpairs = OrderedDict()
        incoming = defaultdict(set)
        outgoing = defaultdict(set)
        sommer = set()
        pagetype = defaultdict(set)
        pagelink = dict()
        ancestors = defaultdict(set)
        randomnodes = set()
        for i in range(1,500):
                #name = int(x[4:-4])
                name = "sect%d.htm" % i
                fp = os.path.join(fx,name)
                if not os.path.isfile(fp):
                    break
                pagelink[i] = name #os.path.abspath(fp)
                y = open(fp,"rb").read()
                if y.find("COMBAT SKILL") >= 0 and y.find("ENDURANCE") >= 0:
                    pagetype[i].add("combat")
                if y.find("Sommerswerd") >= 0:
                    sommer.add(i)
                israndom = y.find("If the number you have picked is") >= 0 and y.find("Random") >= 0
                if israndom:
                    randomnodes.add(i)

                for p in re.findall("",y):
                    p = int(p)
                    allpairs[(i,p)] = 1
                    incoming[p].add(i)
                    outgoing[i].add(p)
                    ancestors[p] |= ancestors[i]
        last = i-1

        if booki == 5:
            special = [(331,373)]
            for x,y in special:
                incoming[y].add(x)
                outgoing[x].add(y)
                allpairs[(x,y)] = 1

        # we want to estimate if an edge is backward, that is a given page (node) goes back to another
        # one that has been visited earlier
        backward = set()
        roots = [i for i in range(1,last+1) if len(incoming[i]) == 0]

        s = analyze(outgoing,incoming,roots,last,backward,booki)
        s["book"] = booki

        outgoing_norandom = dict([(k,v) for k,v in outgoing.iteritems() if k not in randomnodes])
        shortest = analyzeshortest(outgoing_norandom,last)
        if len(shortest) == 0:
            shortest = analyzeshortest(outgoing,last)
            s["mindist"] = -len(shortest)
        else:
            s["mindist"] = len(shortest)
        allstats.append(s)
        outgoing_dag,incoming_dag = makedag(outgoing,incoming,backward)
        print "shortest:"

        print "apply shortest",len(shortest)
        for i in range(1,len(shortest)):
            allpairs[(shortest[i-1],shortest[i])] = 2
        # TBD print "book",booki,dirname,s
        dg = nx.from_dict_of_lists(outgoing_dag,nx.DiGraph())
        if nx.is_directed_acyclic_graph(dg):
            ordered = nx.topological_sort(dg);

            # topological sort
            cp = count_dag_paths(ordered,incoming_dag,outgoing_dag,1,last)
            totalpaths = cp[1]
            s["totalpaths"] = totalpaths
            if False:
                # Something for estimating the mandatory nodes, actually few of them
                necessarynodes = set([k for k,v in cp.iteritems() if v >= totalpaths])
                necessarynodes.remove(1)
                s["needednodes1"] = len(necessarynodes)
                necessarynodes = set()
                for c in outgoing[1]:
                    totalpathsc = cp[c]
                    necessarynodesc = set([k for k,v in cp.iteritems() if v >= totalpathsc])
                    necessarynodes |= necessarynodesc
                s["needednodes2"] = len(necessarynodes)
        else:
            if False:
                incoming = incoming_dag
                outgoing = outgoing_dag
                for a,b in backward:
                    del allpairs[(a,b)]
            s["totalpaths"] = 0
            s["needednodes1"] = 0
            s["needednodes2"] = 0

        pagedict = defaultdict(dict)
        for i in range(2,last):
            o = outgoing[i]
            ww = dict()
            if "combat" in pagetype[i]:
                ww["shape"] = "tripleoctagon"
                ww["fillcolor"] = "orange"
                ww["style"] = "filled"
                if i in sommer:
                    ww["fillcolor"] = "magenta"
                else:
                    ww["fillcolor"] = "orange"
            else:
                ww["shape"] = "circle"
            if len(o) == 0:
                ww["fillcolor"] = "red"
                ww["style"] = "filled"
                ww["shape"] = "Mcircle"
            elif len(incoming[i]) == 0:
                ww["fillcolor"] = "green"
                ww["style"] = "filled"
            pagedict[i].update(ww)

        for i in range(1,last+1):
            pagedict[i]["label"] = "\"%d\"" % i #/%d/%d\"" % (i,distancefromroot[i],maxincoming[i])
            pagedict[i]["URL"] = "\"%s/%s/%s\"" % (link,dirname,pagelink[i])
            pagedict[i]["target"] = "_blank"

        pagedict[1].update(dict(fillcolor="green",style="filled"))
        pagedict[last].update(dict(fillcolor="green",style="filled"))

        for i in range(1,last+1):
            ww = pagedict[i]
            if len(ww) > 0:
                outfile.write(" b%dp%d [%s];\n" % (booki,i,",".join(["%s=%s" % (x,y) for x,y in ww.iteritems()])))

        for k,v in allpairs.iteritems():
            pfrom,pto = k
            sw = {}
            if (pfrom,pto) in backward:
                sw["color"] = "red"
            if v == 2:
                sw["penwidth"] =3.0
            if pfrom in randomnodes:
                sw["style"] = "dashed"
            outfile.write("b%dp%d -> b%dp%d [%s];\n" % (booki,pfrom,booki,pto,",".join(["%s=%s" % (x,y) for x,y in sw.iteritems()])))

        outfile.write("}\n")

        if not clusters:
            for t in outputs:
                oo.write(" ".join(["dot", "-T" + t,outname,"-o"+os.path.join(output,dirname+"."+t)])+"\n")
            #subprocess.call(["dot", "-T" + t,outname,"-o"+os.path.join(output,dirname+"."+t)],shell=True)
    if clusters:
        outfile.write("}\n")
        for t in outputs:
            oo.write(" ".join(["dot", "-T" + t,outname,"-o"+os.path.join(output,dirname+"."+t)])+"\n")

    print "created script.sh"
    oo.close()
    if True:
        print "\t".join(["Book","Shortest","Total Paths"])
        for i,a in enumerate(allstats):
            print "%d\t%d\t%d" % (a["book"],a["mindist"],a["totalpaths"]) #,a["maxdist"])

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


<commit_before>from flask import current_app

from zeus import auth
from zeus.artifacts import manager as default_manager
from zeus.config import celery, db
from zeus.constants import Result
from zeus.models import Artifact, Job, Status

from .aggregate_job_stats import aggregate_build_stats_for_job

@celery.task(max_retries=None)
def process_artifact(artifact_id, manager=None, **kwargs):
    artifact = Artifact.query.unrestricted_unsafe().select_for_update().get(artifact_id)
    if artifact is None:
        return

    artifact.status = Status.in_progress
    db.session.add(artifact)
    db.session.flush()

    auth.set_current_tenant(auth.Tenant(
        repository_ids=[artifact.repository_id]))

    job = Job.query.get(artifact.job_id)

    if job.result == Result.aborted:
        return

    if artifact.file:
        if manager is None:
            manager = default_manager

        with db.session.begin_nested():
            try:
                manager.process(artifact)
            except Exception:
                current_app.logger.exception(
                    'Unrecoverable exception processing artifact %s: %s', artifact.job_id, artifact
                )

    artifact.status = Status.finished
    db.session.add(artifact)
    db.session.commit()

    # we always aggregate results to avoid locking here
    aggregate_build_stats_for_job.delay(job_id=job.id)
<commit_msg>Bad transaction wrapping<commit_after>from flask import current_app

from zeus import auth
from zeus.artifacts import manager as default_manager
from zeus.config import celery, db
from zeus.constants import Result
from zeus.models import Artifact, Job, Status

from .aggregate_job_stats import aggregate_build_stats_for_job

@celery.task(max_retries=None)
def process_artifact(artifact_id, manager=None, **kwargs):
    artifact = Artifact.query.unrestricted_unsafe().select_for_update().get(artifact_id)
    if artifact is None:
        return

    artifact.status = Status.in_progress
    db.session.add(artifact)
    db.session.flush()

    auth.set_current_tenant(auth.Tenant(
        repository_ids=[artifact.repository_id]))

    job = Job.query.get(artifact.job_id)

    if job.result == Result.aborted:
        return

    if artifact.file:
        if manager is None:
            manager = default_manager

            try:
                with db.session.begin_nested():
                    manager.process(artifact)
            except Exception:
                current_app.logger.exception(
                    'Unrecoverable exception processing artifact %s: %s', artifact.job_id, artifact
                )

    artifact.status = Status.finished
    db.session.add(artifact)
    db.session.commit()

    # we always aggregate results to avoid locking here
    aggregate_build_stats_for_job.delay(job_id=job.id)
<|endoftext|>


<commit_before># ===============================================================================
# Copyright 2015 Jake Ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================

# ============= enthought library imports =======================
from pyface.tasks.action.schema import SToolBar
from pyface.tasks.task_layout import TaskLayout, PaneItem
from pyface.tasks.traits_dock_pane import TraitsDockPane
from traits.api import Instance, Bool
# ============= standard library imports ========================
from datetime import datetime, timedelta
# ============= local library imports  ==========================
from pychron.envisage.browser.browser_task import BaseBrowserTask
from pychron.envisage.browser.view import PaneBrowserView
from pychron.envisage.tasks.actions import ToggleFullWindowAction
from pychron.globals import globalv
from pychron.pipeline.tasks.actions import ConfigureRecallAction, ConfigureAnalysesTableAction, \
    LoadReviewStatusAction, EditAnalysisAction, DiffViewAction
from pychron.pipeline.tasks.analysis_range_selector import AnalysisRangeSelector

class BrowserPane(TraitsDockPane, PaneBrowserView):
    id = 'pychron.browser.pane'
    name = 'Analysis Selection'

    # def trait_context(self):
    #     return {'object':self.model, 'pane':self}
    #
    # def traits_view(self):
    #     bv = BrowserView(model=self.model)
    #     return bv.traits_view()

# class ToPipelineAction(TaskAction):
#     name = 'To Pipeline'
#     method = 'switch_to_pipeline'
#     image = icon('play')

class BrowserTask(BaseBrowserTask):
    name = 'Analysis Browser'

    model = Instance('pychron.envisage.browser.browser_model.BrowserModel')
    tool_bars = [SToolBar(ConfigureRecallAction(),
                          ConfigureAnalysesTableAction(),
                          name='Configure'),
                 SToolBar(ToggleFullWindowAction(),
                          LoadReviewStatusAction(),
                          DiffViewAction(),
                          name='View'),
                 SToolBar(EditAnalysisAction(),
                          name='Edit')]

    diff_enabled = Bool

    def activated(self):
        if self.application.get_plugin('pychron.mass_spec.plugin'):
            self.diff_enabled = True
        super(BrowserTask, self).activated()

    def _opened_hook(self):
        super(BrowserTask, self)._opened_hook()

        self.browser_model.activated()
        self._activate_sample_browser()

        if globalv.browser_debug:
            if self.browser_model.analysis_table.analyses:
                r = self.browser_model.analysis_table.analyses[0]
                self.recall(r)

    # menu actions
    def open_time_view_browser(self):
        self.debug('open time view')

        v = AnalysisRangeSelector()
        v.load()

        db = self.dvc.db
        spec_names = db.get_mass_spectrometer_names()
        v.set_mass_spectrometers(spec_names)

        # open a time view selector
        info = v.edit_traits(kind='livemodal')
        if info.result:
            v.dump()

        sms = v.selected_mass_spectrometers
        ants = v.selected_analysis_types
        # with db.session_ctx():
        #     if v.use_date_range:
        #         h, l = v.high_post, v.low_post
        #         ans = db.get_analyses_date_range(l, h,
        #                                          analysis_type=ants,
        #                                          mass_spectrometers=sms)
        #     else:
        #         # get analyses
        #         ans, h, l = db.get_last_nhours_analyses(v.nhours,
        #                                                 return_limits=True,
        #                                                 analysis_types=ants,
        #                                                 mass_spectrometers=sms)
        #
        #     def func(x, prog, i, n):
        #         return x.record_views
        #
        #     records = progress_loader(ans, func)
        #
        # # set analysis_table.analyses
        # bm.analysis_table.set_analyses(records)
        # bm.analysis_table.scroll_to_row = len(records)
        if v.use_date_range:
            h, l = v.high_post, v.low_post
        else:
            now = datetime.now()
            h, l = now, now - timedelta(hours=v.nhours)
        bm = self.browser_model
        if sms:
            bm.mass_spectrometers_enabled = True
            bm.mass_spectrometer_includes = v.selected_mass_spectrometers
        if ants:
            bm.use_analysis_type_filtering = True
            bm._analysis_include_types = ants

        bm._low_post = l.date()
        bm.use_low_post = True

        bm._high_post = h.date()
        bm.use_high_post = True

        bm.do_filter()
        bm.select_all()

        at = bm.analysis_table
        end = len(at.analyses)
        at.scroll_to_row = end

    # toolbar actions
    def diff_analysis(self):
        self.debug('diff analysis')
        if not self.has_active_editor():
            return

        recaller = self.application.get_service('pychron.mass_spec.mass_spec_recaller.MassSpecRecaller')
        if recaller is None:
            self.warning_dialog('Could not access MassSpec database')
            return

        if not recaller.connect():
            self.warning_dialog('Could not connect to MassSpec database. {}'.format(recaller.db.datasource_url))
            return

        active_editor = self.active_editor
        left = active_editor.analysis

        from pychron.pipeline.editors.diff_editor import DiffEditor
        editor = DiffEditor(recaller=recaller)
        left.set_stored_value_states(True, save=True)
        left.load_raw_data()
        if editor.setup(left):
            editor.set_diff(left)
            self._open_editor(editor)
        else:
            self.warning_dialog('Failed to locate analysis {} in MassSpec database'.format(left.record_id))
        left.revert_use_stored_values()

    def create_dock_panes(self):
        return [BrowserPane(model=self.browser_model)]

    def _default_layout_default(self):
        return TaskLayout(left=PaneItem('pychron.browser.pane'))

# ============= EOF =============================================
<commit_msg>updated diff<commit_after># ===============================================================================
# Copyright 2015 Jake Ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================

# ============= enthought library imports =======================
from pyface.tasks.action.schema import SToolBar
from pyface.tasks.task_layout import TaskLayout, PaneItem
from pyface.tasks.traits_dock_pane import TraitsDockPane
from traits.api import Instance, Bool
# ============= standard library imports ========================
from datetime import datetime, timedelta
# ============= local library imports  ==========================
from pychron.envisage.browser.browser_task import BaseBrowserTask
from pychron.envisage.browser.view import PaneBrowserView
from pychron.envisage.tasks.actions import ToggleFullWindowAction
from pychron.globals import globalv
from pychron.pipeline.tasks.actions import ConfigureRecallAction, ConfigureAnalysesTableAction, \
    LoadReviewStatusAction, EditAnalysisAction, DiffViewAction
from pychron.pipeline.tasks.analysis_range_selector import AnalysisRangeSelector

class BrowserPane(TraitsDockPane, PaneBrowserView):
    id = 'pychron.browser.pane'
    name = 'Analysis Selection'

    # def trait_context(self):
    #     return {'object':self.model, 'pane':self}
    #
    # def traits_view(self):
    #     bv = BrowserView(model=self.model)
    #     return bv.traits_view()

# class ToPipelineAction(TaskAction):
#     name = 'To Pipeline'
#     method = 'switch_to_pipeline'
#     image = icon('play')

class BrowserTask(BaseBrowserTask):
    name = 'Analysis Browser'

    model = Instance('pychron.envisage.browser.browser_model.BrowserModel')
    tool_bars = [SToolBar(ConfigureRecallAction(),
                          ConfigureAnalysesTableAction(),
                          name='Configure'),
                 SToolBar(ToggleFullWindowAction(),
                          LoadReviewStatusAction(),
                          DiffViewAction(),
                          name='View'),
                 SToolBar(EditAnalysisAction(),
                          name='Edit')]

    diff_enabled = Bool

    def activated(self):
        if self.application.get_plugin('pychron.mass_spec.plugin'):
            self.diff_enabled = True
        super(BrowserTask, self).activated()

    def _opened_hook(self):
        super(BrowserTask, self)._opened_hook()

        self.browser_model.activated()
        self._activate_sample_browser()

        if globalv.browser_debug:
            if self.browser_model.analysis_table.analyses:
                r = self.browser_model.analysis_table.analyses[0]
                self.recall(r)

    # menu actions
    def open_time_view_browser(self):
        self.debug('open time view')

        v = AnalysisRangeSelector()
        v.load()

        db = self.dvc.db
        spec_names = db.get_mass_spectrometer_names()
        v.set_mass_spectrometers(spec_names)

        # open a time view selector
        info = v.edit_traits(kind='livemodal')
        if info.result:
            v.dump()

        sms = v.selected_mass_spectrometers
        ants = v.selected_analysis_types
        # with db.session_ctx():
        #     if v.use_date_range:
        #         h, l = v.high_post, v.low_post
        #         ans = db.get_analyses_date_range(l, h,
        #                                          analysis_type=ants,
        #                                          mass_spectrometers=sms)
        #     else:
        #         # get analyses
        #         ans, h, l = db.get_last_nhours_analyses(v.nhours,
        #                                                 return_limits=True,
        #                                                 analysis_types=ants,
        #                                                 mass_spectrometers=sms)
        #
        #     def func(x, prog, i, n):
        #         return x.record_views
        #
        #     records = progress_loader(ans, func)
        #
        # # set analysis_table.analyses
        # bm.analysis_table.set_analyses(records)
        # bm.analysis_table.scroll_to_row = len(records)
        if v.use_date_range:
            h, l = v.high_post, v.low_post
        else:
            now = datetime.now()
            h, l = now, now - timedelta(hours=v.nhours)
        bm = self.browser_model
        if sms:
            bm.mass_spectrometers_enabled = True
            bm.mass_spectrometer_includes = v.selected_mass_spectrometers
        if ants:
            bm.use_analysis_type_filtering = True
            bm._analysis_include_types = ants

        bm._low_post = l.date()
        bm.use_low_post = True

        bm._high_post = h.date()
        bm.use_high_post = True

        bm.do_filter()
        bm.select_all()

        at = bm.analysis_table
        end = len(at.analyses)
        at.scroll_to_row = end

    # toolbar actions
    def diff_analysis(self):
        self.debug('diff analysis')
        if not self.has_active_editor():
            return

        recaller = self.application.get_service('pychron.mass_spec.mass_spec_recaller.MassSpecRecaller')
        if recaller is None:
            self.warning_dialog('Could not access MassSpec database')
            return

        if not recaller.connect():
            self.warning_dialog('Could not connect to MassSpec database. {}'.format(recaller.db.datasource_url))
            return

        active_editor = self.active_editor
        left = active_editor.analysis

        from pychron.pipeline.editors.diff_editor import DiffEditor
        editor = DiffEditor(recaller=recaller)
        left.set_stored_value_states(True, save=True)
        left.load_raw_data(n_only=True)
        if editor.setup(left):
            editor.set_diff(left)
            self._open_editor(editor)
        else:
            self.warning_dialog('Failed to locate analysis {} in MassSpec database'.format(left.record_id))
        left.revert_use_stored_values()

    def create_dock_panes(self):
        return [BrowserPane(model=self.browser_model)]

    def _default_layout_default(self):
        return TaskLayout(left=PaneItem('pychron.browser.pane'))

# ============= EOF =============================================
<|endoftext|>


<commit_before>import logging
from pyhomematic.devicetypes.generic import HMDevice
from pyhomematic.devicetypes.sensors import AreaThermostat
from pyhomematic.devicetypes.helper import HelperValveState, HelperBatteryState, HelperLowBat, HelperLowBatIP

LOG = logging.getLogger(__name__)

class HMThermostat(HMDevice):
    """
    HM-CC-RT-DN, HM-CC-RT-DN-BoM
    ClimateControl-RadiatorThermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # constante
        self.AUTO_MODE = 0
        self.MANU_MODE = 1
        self.PARTY_MODE = 2
        self.BOOST_MODE = 3
        self.COMFORT_MODE = 4
        self.LOWERING_MODE = 5
        self.OFF_VALUE = 4.5

        self.mode = None

    def actual_temperature(self):
        """ Returns the current temperature. """
        return self.getSensorData("ACTUAL_TEMPERATURE")

    def get_set_temperature(self):
        """ Returns the current target temperature. """
        return self.getWriteData("SET_TEMPERATURE")

    def set_temperature(self, target_temperature):
        """ Set the target temperature. """
        try:
            target_temperature = float(target_temperature)
        except Exception as err:
            LOG.debug("Thermostat.set_temperature: Exception %s" % (err,))
            return False
        self.writeNodeData("SET_TEMPERATURE", target_temperature)

    def turnoff(self):
        """ Turn off Thermostat. """
        self.writeNodeData("SET_TEMPERATURE", self.OFF_VALUE)

    @property
    def MODE(self):
        """ Return mode. """
        return self.getAttributeData("CONTROL_MODE")

    @MODE.setter
    def MODE(self, setmode):
        """ Set mode. """
        set_data = True
        mode = None
        if setmode == self.AUTO_MODE:
            mode = 'AUTO_MODE'
        elif setmode == self.MANU_MODE:
            mode = 'MANU_MODE'
            set_data = self.get_set_temperature()
        elif setmode == self.BOOST_MODE:
            mode = 'BOOST_MODE'
        elif setmode == self.COMFORT_MODE:
            mode = 'COMFORT_MODE'
        elif setmode == self.LOWERING_MODE:
            mode = 'LOWERING_MODE'
        else:
            LOG.warning("Thermostat.MODE.setter: Invalid mode: %s" % str(setmode))
        if mode:
            self.actionNodeData(mode, set_data)

    @property
    def AUTOMODE(self):
        """ Return auto mode state. """
        return self.mode == self.AUTO_MODE

    @property
    def MANUMODE(self):
        """ Return manual mode state. """
        return self.mode == self.MANU_MODE

    @property
    def PARTYMODE(self):
        """ Return party mode state. """
        return self.mode == self.PARTY_MODE

    @property
    def BOOSTMODE(self):
        """ Return boost state. """
        return self.mode == self.BOOST_MODE

    @property
    def COMFORTMODE(self):
        """ Return comfort state. """
        return self.mode == self.COMFORT_MODE

    @property
    def LOWERINGMODE(self):
        """ Return lowering state. """
        return self.mode == self.LOWERING_MODE

class ThermostatGroup(HMThermostat):
    """
    HM-CC-VG-1
    Thermostatgroups made up out of multiple supported thermostats
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1]})
        self.WRITENODE.update({"SET_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"AUTO_MODE": [1],
                                "MANU_MODE": [1],
                                "BOOST_MODE": [1],
                                "COMFORT_MODE": [1],
                                "LOWERING_MODE": [1]})
        self.ATTRIBUTENODE.update({"VALVE_STATE": [1],
                                   "CONTROL_MODE": [1]})

class Thermostat(HMThermostat, HelperBatteryState, HelperValveState):
    """
    HM-CC-RT-DN, HM-CC-RT-DN-BoM
    ClimateControl-Radiator Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [4]})
        self.WRITENODE.update({"SET_TEMPERATURE": [4]})
        self.ACTIONNODE.update({"AUTO_MODE": [4],
                                "MANU_MODE": [4],
                                "BOOST_MODE": [4],
                                "COMFORT_MODE": [4],
                                "LOWERING_MODE": [4]})
        self.ATTRIBUTENODE.update({"VALVE_STATE": [4],
                                   "BATTERY_STATE": [4],
                                   "CONTROL_MODE": [4]})

class ThermostatWall(HMThermostat, AreaThermostat, HelperBatteryState):
    """
    HM-TC-IT-WM-W-EU
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [2],
                                "ACTUAL_HUMIDITY": [2]})
        self.WRITENODE.update({"SET_TEMPERATURE": [2]})
        self.ACTIONNODE.update({"AUTO_MODE": [2],
                                "MANU_MODE": [2],
                                "BOOST_MODE": [2],
                                "COMFORT_MODE": [2],
                                "LOWERING_MODE": [2]})
        self.ATTRIBUTENODE.update({"CONTROL_MODE": [2], "BATTERY_STATE": [2]})

class ThermostatWall2(HMThermostat, AreaThermostat, HelperLowBat):
    """
    HM-CC-TC
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"TEMPERATURE": [1],
                                "HUMIDITY": [1]})
        self.WRITENODE.update({"SETPOINT": [2]})

class MAXThermostat(HMThermostat, HelperLowBat, HelperValveState):
    """
    BC-RT-TRX-CyG, BC-RT-TRX-CyG-2, BC-RT-TRX-CyG-3, BC-RT-TRX-CyG-4
    ClimateControl-Radiator Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1]})
        self.WRITENODE.update({"SET_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"AUTO_MODE": [1],
                                "MANU_MODE": [1],
                                "BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"LOWBAT": [0],
                                   "CONTROL_MODE": [1],
                                   "VALVE_STATE": [1]})

class MAXWallThermostat(HMThermostat, HelperLowBat):
    """
    BC-TC-C-WM-4
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1]})
        self.WRITENODE.update({"SET_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"AUTO_MODE": [1],
                                "MANU_MODE": [1],
                                "BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"LOWBAT": [0], "CONTROL_MODE": [1]})

class IPThermostat(HMThermostat, HelperLowBatIP, HelperValveState):
    """
    HPIM-eTRV
    ClimateControl-Radiator Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1]})
        self.WRITENODE.update({"SET_POINT_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"AUTO_MODE": [1],
                                "MANU_MODE": [1],
                                "CONTROL_MODE": [1],
                                "BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"LOW_BAT": [0],
                                   "OPERATING_VOLTAGE": [0],
                                   "SET_POINT_MODE": [1],
                                   "BOOST_MODE": [1],
                                   "VALVE_STATE": [1]})

    def get_set_temperature(self):
        """ Returns the current target temperature. """
        return self.getWriteData("SET_POINT_TEMPERATURE")

    def set_temperature(self, target_temperature):
        """ Set the target temperature. """
        try:
            target_temperature = float(target_temperature)
        except Exception as err:
            LOG.debug("Thermostat.set_temperature: Exception %s" % (err,))
            return False
        self.writeNodeData("SET_POINT_TEMPERATURE", target_temperature)

    @property
    def MODE(self):
        """ Return mode. """
        if self.getAttributeData("BOOST_MODE"):
            return self.BOOST_MODE
        else:
            return self.getAttributeData("SET_POINT_MODE")

    @MODE.setter
    def MODE(self, setmode):
        """ Set mode. """
        if setmode == self.BOOST_MODE:
            self.actionNodeData('BOOST_MODE', True)
        elif setmode in [self.AUTO_MODE, self.MANU_MODE]:
            if self.getAttributeData("BOOST_MODE"):
                self.actionNodeData('BOOST_MODE', False)
            self.actionNodeData('CONTROL_MODE', setmode)

    def turnoff(self):
        """ Turn off Thermostat. """
        self.writeNodeData("SET_POINT_TEMPERATURE", self.OFF_VALUE)

class IPThermostatWall(HMThermostat, HelperLowBatIP):
    """
    HmIP-STHD
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1],
                                "HUMIDITY": [1]})
        self.WRITENODE.update({"SET_POINT_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"LOW_BAT": [0],
                                   "SET_POINT_MODE": [1]})

    def get_set_temperature(self):
        """ Returns the current target temperature. """
        return self.getWriteData("SET_POINT_TEMPERATURE")

    def set_temperature(self, target_temperature):
        """ Set the target temperature. """
        try:
            target_temperature = float(target_temperature)
        except Exception as err:
            LOG.debug("Thermostat.set_temperature: Exception %s" % (err,))
            return False
        self.writeNodeData("SET_POINT_TEMPERATURE", target_temperature)

    def turnoff(self):
        """ Turn off Thermostat. """
        self.writeNodeData("SET_POINT_TEMPERATURE", self.OFF_VALUE)

class IPThermostatWall230V(HMThermostat):
    """
    HmIP-BWTH
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1],
                                "HUMIDITY": [1]})
        self.WRITENODE.update({"SET_POINT_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"SET_POINT_MODE": [1]})

    def get_set_temperature(self):
        """ Returns the current target temperature. """
        return self.getWriteData("SET_POINT_TEMPERATURE")

    def set_temperature(self, target_temperature):
        """ Set the target temperature. """
        try:
            target_temperature = float(target_temperature)
        except Exception as err:
            LOG.debug("Thermostat.set_temperature: Exception %s" % (err,))
            return False
        self.writeNodeData("SET_POINT_TEMPERATURE", target_temperature)

    def turnoff(self):
        """ Turn off Thermostat. """
        self.writeNodeData("SET_POINT_TEMPERATURE", self.OFF_VALUE)

DEVICETYPES = {
    "HM-CC-VG-1": ThermostatGroup,
    "HM-CC-RT-DN": Thermostat,
    "HM-CC-RT-DN-BoM": Thermostat,
    "HM-TC-IT-WM-W-EU": ThermostatWall,
    "HM-CC-TC": ThermostatWall2,
    "ZEL STG RM FWT": ThermostatWall2,
    "BC-RT-TRX-CyG": MAXThermostat,
    "BC-RT-TRX-CyG-2": MAXThermostat,
    "BC-RT-TRX-CyG-3": MAXThermostat,
    "BC-RT-TRX-CyG-4": MAXThermostat,
    "BC-RT-TRX-CyN": MAXThermostat,
    "BC-TC-C-WM-2": MAXWallThermostat,
    "BC-TC-C-WM-4": MAXWallThermostat,
    "HMIP-eTRV": IPThermostat,
    "HmIP-eTRV": IPThermostat,
    "HmIP-eTRV-2": IPThermostat,
    "HmIP-STHD": IPThermostatWall,
    "HmIP-STH": IPThermostatWall,
    "HmIP-WTH-2": IPThermostatWall,
    "HMIP-WTH": IPThermostatWall,
    "HmIP-WTH": IPThermostatWall,
    "HmIP-BWTH": IPThermostatWall,
    "HmIP-HEATING": IPThermostat,
}
<commit_msg>Assign to proper class<commit_after>import logging
from pyhomematic.devicetypes.generic import HMDevice
from pyhomematic.devicetypes.sensors import AreaThermostat
from pyhomematic.devicetypes.helper import HelperValveState, HelperBatteryState, HelperLowBat, HelperLowBatIP

LOG = logging.getLogger(__name__)

class HMThermostat(HMDevice):
    """
    HM-CC-RT-DN, HM-CC-RT-DN-BoM
    ClimateControl-RadiatorThermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # constante
        self.AUTO_MODE = 0
        self.MANU_MODE = 1
        self.PARTY_MODE = 2
        self.BOOST_MODE = 3
        self.COMFORT_MODE = 4
        self.LOWERING_MODE = 5
        self.OFF_VALUE = 4.5

        self.mode = None

    def actual_temperature(self):
        """ Returns the current temperature. """
        return self.getSensorData("ACTUAL_TEMPERATURE")

    def get_set_temperature(self):
        """ Returns the current target temperature. """
        return self.getWriteData("SET_TEMPERATURE")

    def set_temperature(self, target_temperature):
        """ Set the target temperature. """
        try:
            target_temperature = float(target_temperature)
        except Exception as err:
            LOG.debug("Thermostat.set_temperature: Exception %s" % (err,))
            return False
        self.writeNodeData("SET_TEMPERATURE", target_temperature)

    def turnoff(self):
        """ Turn off Thermostat. """
        self.writeNodeData("SET_TEMPERATURE", self.OFF_VALUE)

    @property
    def MODE(self):
        """ Return mode. """
        return self.getAttributeData("CONTROL_MODE")

    @MODE.setter
    def MODE(self, setmode):
        """ Set mode. """
        set_data = True
        mode = None
        if setmode == self.AUTO_MODE:
            mode = 'AUTO_MODE'
        elif setmode == self.MANU_MODE:
            mode = 'MANU_MODE'
            set_data = self.get_set_temperature()
        elif setmode == self.BOOST_MODE:
            mode = 'BOOST_MODE'
        elif setmode == self.COMFORT_MODE:
            mode = 'COMFORT_MODE'
        elif setmode == self.LOWERING_MODE:
            mode = 'LOWERING_MODE'
        else:
            LOG.warning("Thermostat.MODE.setter: Invalid mode: %s" % str(setmode))
        if mode:
            self.actionNodeData(mode, set_data)

    @property
    def AUTOMODE(self):
        """ Return auto mode state. """
        return self.mode == self.AUTO_MODE

    @property
    def MANUMODE(self):
        """ Return manual mode state. """
        return self.mode == self.MANU_MODE

    @property
    def PARTYMODE(self):
        """ Return party mode state. """
        return self.mode == self.PARTY_MODE

    @property
    def BOOSTMODE(self):
        """ Return boost state. """
        return self.mode == self.BOOST_MODE

    @property
    def COMFORTMODE(self):
        """ Return comfort state. """
        return self.mode == self.COMFORT_MODE

    @property
    def LOWERINGMODE(self):
        """ Return lowering state. """
        return self.mode == self.LOWERING_MODE

class ThermostatGroup(HMThermostat):
    """
    HM-CC-VG-1
    Thermostatgroups made up out of multiple supported thermostats
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1]})
        self.WRITENODE.update({"SET_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"AUTO_MODE": [1],
                                "MANU_MODE": [1],
                                "BOOST_MODE": [1],
                                "COMFORT_MODE": [1],
                                "LOWERING_MODE": [1]})
        self.ATTRIBUTENODE.update({"VALVE_STATE": [1],
                                   "CONTROL_MODE": [1]})

class Thermostat(HMThermostat, HelperBatteryState, HelperValveState):
    """
    HM-CC-RT-DN, HM-CC-RT-DN-BoM
    ClimateControl-Radiator Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [4]})
        self.WRITENODE.update({"SET_TEMPERATURE": [4]})
        self.ACTIONNODE.update({"AUTO_MODE": [4],
                                "MANU_MODE": [4],
                                "BOOST_MODE": [4],
                                "COMFORT_MODE": [4],
                                "LOWERING_MODE": [4]})
        self.ATTRIBUTENODE.update({"VALVE_STATE": [4],
                                   "BATTERY_STATE": [4],
                                   "CONTROL_MODE": [4]})

class ThermostatWall(HMThermostat, AreaThermostat, HelperBatteryState):
    """
    HM-TC-IT-WM-W-EU
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [2],
                                "ACTUAL_HUMIDITY": [2]})
        self.WRITENODE.update({"SET_TEMPERATURE": [2]})
        self.ACTIONNODE.update({"AUTO_MODE": [2],
                                "MANU_MODE": [2],
                                "BOOST_MODE": [2],
                                "COMFORT_MODE": [2],
                                "LOWERING_MODE": [2]})
        self.ATTRIBUTENODE.update({"CONTROL_MODE": [2], "BATTERY_STATE": [2]})

class ThermostatWall2(HMThermostat, AreaThermostat, HelperLowBat):
    """
    HM-CC-TC
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"TEMPERATURE": [1],
                                "HUMIDITY": [1]})
        self.WRITENODE.update({"SETPOINT": [2]})

class MAXThermostat(HMThermostat, HelperLowBat, HelperValveState):
    """
    BC-RT-TRX-CyG, BC-RT-TRX-CyG-2, BC-RT-TRX-CyG-3, BC-RT-TRX-CyG-4
    ClimateControl-Radiator Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1]})
        self.WRITENODE.update({"SET_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"AUTO_MODE": [1],
                                "MANU_MODE": [1],
                                "BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"LOWBAT": [0],
                                   "CONTROL_MODE": [1],
                                   "VALVE_STATE": [1]})

class MAXWallThermostat(HMThermostat, HelperLowBat):
    """
    BC-TC-C-WM-4
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1]})
        self.WRITENODE.update({"SET_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"AUTO_MODE": [1],
                                "MANU_MODE": [1],
                                "BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"LOWBAT": [0], "CONTROL_MODE": [1]})

class IPThermostat(HMThermostat, HelperLowBatIP, HelperValveState):
    """
    HPIM-eTRV
    ClimateControl-Radiator Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1]})
        self.WRITENODE.update({"SET_POINT_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"AUTO_MODE": [1],
                                "MANU_MODE": [1],
                                "CONTROL_MODE": [1],
                                "BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"LOW_BAT": [0],
                                   "OPERATING_VOLTAGE": [0],
                                   "SET_POINT_MODE": [1],
                                   "BOOST_MODE": [1],
                                   "VALVE_STATE": [1]})

    def get_set_temperature(self):
        """ Returns the current target temperature. """
        return self.getWriteData("SET_POINT_TEMPERATURE")

    def set_temperature(self, target_temperature):
        """ Set the target temperature. """
        try:
            target_temperature = float(target_temperature)
        except Exception as err:
            LOG.debug("Thermostat.set_temperature: Exception %s" % (err,))
            return False
        self.writeNodeData("SET_POINT_TEMPERATURE", target_temperature)

    @property
    def MODE(self):
        """ Return mode. """
        if self.getAttributeData("BOOST_MODE"):
            return self.BOOST_MODE
        else:
            return self.getAttributeData("SET_POINT_MODE")

    @MODE.setter
    def MODE(self, setmode):
        """ Set mode. """
        if setmode == self.BOOST_MODE:
            self.actionNodeData('BOOST_MODE', True)
        elif setmode in [self.AUTO_MODE, self.MANU_MODE]:
            if self.getAttributeData("BOOST_MODE"):
                self.actionNodeData('BOOST_MODE', False)
            self.actionNodeData('CONTROL_MODE', setmode)

    def turnoff(self):
        """ Turn off Thermostat. """
        self.writeNodeData("SET_POINT_TEMPERATURE", self.OFF_VALUE)

class IPThermostatWall(HMThermostat, HelperLowBatIP):
    """
    HmIP-STHD
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1],
                                "HUMIDITY": [1]})
        self.WRITENODE.update({"SET_POINT_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"LOW_BAT": [0],
                                   "SET_POINT_MODE": [1]})

    def get_set_temperature(self):
        """ Returns the current target temperature. """
        return self.getWriteData("SET_POINT_TEMPERATURE")

    def set_temperature(self, target_temperature):
        """ Set the target temperature. """
        try:
            target_temperature = float(target_temperature)
        except Exception as err:
            LOG.debug("Thermostat.set_temperature: Exception %s" % (err,))
            return False
        self.writeNodeData("SET_POINT_TEMPERATURE", target_temperature)

    def turnoff(self):
        """ Turn off Thermostat. """
        self.writeNodeData("SET_POINT_TEMPERATURE", self.OFF_VALUE)

class IPThermostatWall230V(HMThermostat):
    """
    HmIP-BWTH
    ClimateControl-Wall Thermostat that measures temperature and allows to set a target temperature or use some automatic mode.
    """
    def __init__(self, device_description, proxy, resolveparamsets=False):
        super().__init__(device_description, proxy, resolveparamsets)

        # init metadata
        self.SENSORNODE.update({"ACTUAL_TEMPERATURE": [1],
                                "HUMIDITY": [1]})
        self.WRITENODE.update({"SET_POINT_TEMPERATURE": [1]})
        self.ACTIONNODE.update({"BOOST_MODE": [1]})
        self.ATTRIBUTENODE.update({"SET_POINT_MODE": [1]})

    def get_set_temperature(self):
        """ Returns the current target temperature. """
        return self.getWriteData("SET_POINT_TEMPERATURE")

    def set_temperature(self, target_temperature):
        """ Set the target temperature. """
        try:
            target_temperature = float(target_temperature)
        except Exception as err:
            LOG.debug("Thermostat.set_temperature: Exception %s" % (err,))
            return False
        self.writeNodeData("SET_POINT_TEMPERATURE", target_temperature)

    def turnoff(self):
        """ Turn off Thermostat. """
        self.writeNodeData("SET_POINT_TEMPERATURE", self.OFF_VALUE)

DEVICETYPES = {
    "HM-CC-VG-1": ThermostatGroup,
    "HM-CC-RT-DN": Thermostat,
    "HM-CC-RT-DN-BoM": Thermostat,
    "HM-TC-IT-WM-W-EU": ThermostatWall,
    "HM-CC-TC": ThermostatWall2,
    "ZEL STG RM FWT": ThermostatWall2,
    "BC-RT-TRX-CyG": MAXThermostat,
    "BC-RT-TRX-CyG-2": MAXThermostat,
    "BC-RT-TRX-CyG-3": MAXThermostat,
    "BC-RT-TRX-CyG-4": MAXThermostat,
    "BC-RT-TRX-CyN": MAXThermostat,
    "BC-TC-C-WM-2": MAXWallThermostat,
    "BC-TC-C-WM-4": MAXWallThermostat,
    "HMIP-eTRV": IPThermostat,
    "HmIP-eTRV": IPThermostat,
    "HmIP-eTRV-2": IPThermostat,
    "HmIP-STHD": IPThermostatWall,
    "HmIP-STH": IPThermostatWall,
    "HmIP-WTH-2": IPThermostatWall,
    "HMIP-WTH": IPThermostatWall,
    "HmIP-WTH": IPThermostatWall,
    "HmIP-BWTH": IPThermostatWall230V,
    "HmIP-HEATING": IPThermostat,
}
<|endoftext|>


<commit_before>import os
import pylearn2
from pylearn2.devtools.list_files import list_files

whitelist = ["rbm_tools.py",
             "training_algorithms/tests/test_bgd.py",
             "training_algorithms/tests/test_sgd.py",
             "training_algorithms/tests/test_learning_rule.py",
             "training_algorithms/bgd.py",
             "distributions/mnd.py",
             "models/sparse_autoencoder.py",
             "models/tests/test_dbm.py",
             "models/tests/test_autoencoder.py",
             "models/tests/test_s3c_inference.py",
             "models/tests/test_maxout.py",
             "models/tests/test_mnd.py",
             "models/tests/test_s3c_misc.py",
             "models/gsn.py",
             "models/model.py",
             "models/dbm/layer.py",
             "models/dbm/__init__.py",
             "models/dbm/dbm.py",
             "models/dbm/ising.py",
             "models/dbm/inference_procedure.py",
             "models/differentiable_sparse_coding.py",
             "models/local_coordinate_coding.py",
             "models/maxout.py",
             "models/s3c.py",
             "models/mnd.py",
             "models/dense_binary_dbm.py",
             "models/svm.py",
             "models/rbm.py",
             "models/autoencoder.py",
             "tests/test_monitor.py",
             "tests/rbm/test_ais.py",
             "kmeans.py",
             "packaged_dependencies/theano_linear/conv2d.py",
             "packaged_dependencies/theano_linear/imaging.py",
             "packaged_dependencies/theano_linear/pyramid.py",
             "packaged_dependencies/theano_linear/unshared_conv/"
             "test_gpu_unshared_conv.py",
             "packaged_dependencies/theano_linear/unshared_conv/"
             "test_localdot.py",
             "packaged_dependencies/theano_linear/unshared_conv/localdot.py",
             "packaged_dependencies/theano_linear/unshared_conv/"
             "unshared_conv.py",
             "packaged_dependencies/theano_linear/linear.py",
             "packaged_dependencies/theano_linear/test_spconv.py",
             "packaged_dependencies/theano_linear/test_matrixmul.py",
             "packaged_dependencies/theano_linear/spconv.py",
             "expr/tests/test_coding.py",
             "expr/tests/test_normalize.py",
             "expr/tests/test_stochastic_pool.py",
             "expr/nnet.py",
             "expr/stochastic_pool.py",
             "expr/sampling.py",
             "expr/normalize.py",
             "expr/information_theory.py",
             "expr/basic.py",
             "testing/datasets.py",
             "testing/cost.py",
             "gui/graph_2D.py",
             "gui/patch_viewer.py",
             "sandbox/cuda_convnet/weight_acts.py",
             "sandbox/cuda_convnet/filter_acts.py",
             "sandbox/cuda_convnet/tests/test_filter_acts_strided.py",
             "sandbox/cuda_convnet/tests/test_probabilistic_max_pooling.py",
             "sandbox/cuda_convnet/tests/test_filter_acts.py",
             "sandbox/cuda_convnet/tests/test_weight_acts_strided.py",
             "sandbox/cuda_convnet/tests/test_image_acts_strided.py",
             "sandbox/cuda_convnet/tests/test_img_acts.py",
             "sandbox/cuda_convnet/testsprofile_probabilistic_max_pooling.py",
             "sandbox/cuda_convnet/tests/test_weight_acts.py",
             "sandbox/cuda_convnet/tests/test_stochastic_pool.py",
             "sandbox/cuda_convnet/specialized_bench.py",
             "sandbox/cuda_convnet/response_norm.py",
             "sandbox/cuda_convnet/__init__.py",
             "sandbox/cuda_convnet/img_acts.py",
             "sandbox/cuda_convnet/convnet_compile.py",
             "sandbox/cuda_convnet/base_acts.py",
             "sandbox/cuda_convnet/pthreads.py",
             "sandbox/cuda_convnet/pool.py",
             "sandbox/cuda_convnet/bench.py",
             "sandbox/cuda_convnet/stochastic_pool.py",
             "sandbox/cuda_convnet/probabilistic_max_pooling.py",
             "sandbox/tuple_var.py",
             "sandbox/lisa_rl/bandit/average_agent.py",
             "sandbox/lisa_rl/bandit/classifier_bandit.py",
             "sandbox/lisa_rl/bandit/classifier_agent.py",
             "sandbox/lisa_rl/bandit/plot_reward.py",
             "config/old_config.py",
             "config/yaml_parse.py",
             "space/tests/test_space.py",
             "datasets/utlc.py",
             "datasets/mnistplus.py",
             "datasets/cos_dataset.py",
             "datasets/cifar10.py",
             "datasets/svhn.py",
             "datasets/tests/test_csv_dataset.py",
             "datasets/tests/test_icml07.py",
             "datasets/tests/test_utlc.py",
             "datasets/preprocessing.py",
             "datasets/config.py",
             "datasets/dense_design_matrix.py",
             "datasets/adult.py",
             "datasets/tfd.py",
             "datasets/icml07.py",
             "datasets/filetensor.py",
             "datasets/hepatitis.py",
             "datasets/wiskott.py",
             "datasets/mnist.py",
             "datasets/sparse_dataset.py",
             "datasets/csv_dataset.py",
             "datasets/cifar100.py",
             "datasets/tl_challenge.py",
             "datasets/norb_small.py",
             "datasets/retina.py",
             "datasets/ocr.py",
             "datasets/stl10.py",
             "datasets/vector_spaces_dataset.py",
             "datasets/debug.py",
             "datasets/binarizer.py",
             "utils/utlc.py",
             "utils/tests/test_serial.py",
             "utils/common_strings.py",
             "utils/serial.py",
             "utils/mem.py",
             "train.py",
             "format/tests/test_target_format.py",
             "format/target_format.py",
             "dataset_get/dataset-get.py",
             "dataset_get/helper-scripts/make-archive.py",
             "dataset_get/dataset_resolver.py",
             "pca.py",
             "monitor.py",
             "optimization/batch_gradient_descent.py",
             "optimization/test_batch_gradient_descent.py",
             "optimization/minres.py",
             "costs/ebm_estimation.py",
             "costs/gsn.py",
             "costs/mlp/__init__.py",
             "costs/mlp/dropout.py",
             "costs/mlp/missing_target_cost.py",
             "costs/cost.py",
             "costs/dbm.py",
             "costs/autoencoder.py",
             "linear/conv2d.py",
             "linear/local_c01b.py",
             "linear/matrixmul.py",
             "linear/linear_transform.py",
             "linear/conv2d_c01b.py",
             "energy_functions/rbm_energy.py",
             "scripts/plot_monitor.py",
             "scripts/tests/test_autoencoder.py",
             "scripts/show_examples.py",
             "scripts/summarize_model.py",
             "scripts/lcc_tangents/make_dataset.py",
             "scripts/pkl_inspector.py",
             "scripts/print_monitor.py",
             "scripts/show_binocular_greyscale_examples.py",
             "scripts/jobman/tester.py",
             "scripts/dbm/show_samples.py",
             "scripts/dbm/show_reconstructions.py",
             "scripts/dbm/dbm_metrics.py",
             "scripts/dbm/top_filters.py",
             "scripts/papers/maxout/svhn_preprocessing.py",
             "scripts/papers/jia_huang_wkshp_11/fit_final_model.py",
             "scripts/papers/jia_huang_wkshp_11/evaluate.py",
             "scripts/papers/jia_huang_wkshp_11/extract_features.py",
             "scripts/papers/jia_huang_wkshp_11/assemble.py",
             "scripts/gpu_pkl_to_cpu_pkl.py",
             "scripts/datasets/make_cifar10_whitened.py",
             "scripts/datasets/make_cifar100_patches_8x8.py",
             "scripts/datasets/make_cifar100_patches.py",
             "scripts/datasets/make_cifar10_gcn_whitened.py",
             "scripts/datasets/make_cifar100_whitened.py",
             "scripts/datasets/make_stl10_patches_8x8.py",
             "scripts/datasets/make_cifar100_gcn_whitened.py",
             "scripts/datasets/make_stl10_whitened.py",
             "scripts/datasets/make_stl10_patches.py",
             "scripts/gsn_example.py",
             "scripts/tutorials/tests/test_convnet.py",
             "scripts/tutorials/deep_trainer/run_deep_trainer.py",
             "scripts/tutorials/grbm_smd/make_dataset.py",
             "scripts/tutorials/grbm_smd/test_grbm_smd.py",
             "scripts/icml_2013_wrepl/multimodal/"
             "extract_layer_2_kmeans_features.py",
             "scripts/icml_2013_wrepl/multimodal/make_submission.py",
             "scripts/icml_2013_wrepl/multimodal/lcn.py",
             "scripts/icml_2013_wrepl/multimodal/extract_kmeans_features.py",
             "scripts/icml_2013_wrepl/emotions/emotions_dataset.py",
             "scripts/icml_2013_wrepl/emotions/make_submission.py",
             "scripts/icml_2013_wrepl/black_box/black_box_dataset.py",
             "scripts/icml_2013_wrepl/black_box/make_submission.py",
             "scripts/diff_monitor.py",
             "train_extensions/window_flip.py",
             "corruption.py",
             "sandbox/cuda_convnet/tests/profile_probabilistic_max_pooling.py",
             "training_algorithms/sgd.py",
             "devtools/nan_guard.py",
             "models/mlp.py",
             "sandbox/lisa_rl/bandit/gaussian_bandit.py",
             "config/tests/test_yaml_parse.py",
             "utils/iteration.py",
             "utils/track_version.py",
             "scripts/get_version.py",
             "training_algorithms/default.py",
             "training_algorithms/training_algorithm.py",
             "distributions/parzen.py",
             "models/independent_multiclass_logistic.py",
             "models/normalized_ebm.py",
             "models/tests/test_dropout.py",
             "models/tests/test_mlp.py",
             "models/dbm/sampling_procedure.py",
             "tests/test_dbm_metrics.py",
             "packaged_dependencies/theano_linear/unshared_conv/"
             "gpu_unshared_conv.py",
             "expr/probabilistic_max_pooling.py",
             "sandbox/lisa_rl/bandit/simulator.py",
             "space/__init__.py",
             "datasets/norb.py",
             "datasets/iris.py",
             "datasets/zca_dataset.py",
             "termination_criteria/__init__.py",
             "utils/logger.py",
             "utils/tests/test_one_hot.py",
             "utils/tests/test_pooling.py",
             "utils/image.py",
             "utils/video.py",
             "utils/string_utils.py",
             "utils/pooling.py",
             "utils/__init__.py",
             "utils/datasets.py",
             "utils/data_specs.py",
             "utils/shell.py",
             "utils/rng.py",
             "utils/insert_along_axis.py",
             "utils/call_check.py",
             "deprecated/classifier.py",
             "dataset_get/helper-scripts/make-sources.py",
             "optimization/test_linesearch.py",
             "optimization/linear_cg.py",
             "optimization/test_feature_sign.py",
             "optimization/linesearch.py",
             "scripts/jobman/experiment.py",
             "scripts/datasets/step_through_small_norb.py",
             "scripts/datasets/browse_small_norb.py",
             "scripts/datasets/make_mnistplus.py",
             "scripts/tutorials/jobman_demo/utils.py",
             "scripts/train.py",
             "base.py",
             "devtools/tests/test_record.py",
             "devtools/tests/test_format.py",
             "train_extensions/__init__.py"]

def test_format():
    format_infractions = []
    for path in list_files(".py"):
        rel_path = os.path.relpath(path, pylearn2.__path__[0])
        if rel_path in whitelist:
            continue
        with open(path) as file:
            for i, line in enumerate(file):
                if len(line) > 79:
                    format_infractions.append((path, i + 1))
    if len(format_infractions) > 0:
        msg = "\n".join('File "%s" line %d has more than 79 characters'
              % (fn, line) for fn, line in format_infractions)
        raise AssertionError("Format not respected:\n%s" % msg)
<commit_msg>fix bug where endlines were counted against the line length limit<commit_after>import os
import pylearn2
from pylearn2.devtools.list_files import list_files

whitelist = ["rbm_tools.py",
             "training_algorithms/tests/test_bgd.py",
             "training_algorithms/tests/test_learning_rule.py",
             "training_algorithms/bgd.py",
             "distributions/mnd.py",
             "models/sparse_autoencoder.py",
             "models/tests/test_dbm.py",
             "models/tests/test_autoencoder.py",
             "models/tests/test_s3c_inference.py",
             "models/tests/test_maxout.py",
             "models/tests/test_mnd.py",
             "models/tests/test_s3c_misc.py",
             "models/gsn.py",
             "models/dbm/layer.py",
             "models/dbm/__init__.py",
             "models/dbm/dbm.py",
             "models/dbm/ising.py",
             "models/dbm/inference_procedure.py",
             "models/differentiable_sparse_coding.py",
             "models/local_coordinate_coding.py",
             "models/mnd.py",
             "models/s3c.py",
             "models/dense_binary_dbm.py",
             "models/rbm.py",
             "models/autoencoder.py",
             "tests/test_monitor.py",
             "tests/rbm/test_ais.py",
             "kmeans.py",
             "packaged_dependencies/theano_linear/conv2d.py",
             "packaged_dependencies/theano_linear/imaging.py",
             "packaged_dependencies/theano_linear/pyramid.py",
             "packaged_dependencies/theano_linear/unshared_conv/"
             "test_gpu_unshared_conv.py",
             "packaged_dependencies/theano_linear/unshared_conv/"
             "test_localdot.py",
             "packaged_dependencies/theano_linear/unshared_conv/localdot.py",
             "packaged_dependencies/theano_linear/unshared_conv/"
             "unshared_conv.py",
             "packaged_dependencies/theano_linear/linear.py",
             "packaged_dependencies/theano_linear/test_spconv.py",
             "packaged_dependencies/theano_linear/test_matrixmul.py",
             "packaged_dependencies/theano_linear/spconv.py",
             "expr/tests/test_coding.py",
             "expr/tests/test_normalize.py",
             "expr/tests/test_stochastic_pool.py",
             "expr/nnet.py",
             "expr/stochastic_pool.py",
             "expr/sampling.py",
             "expr/normalize.py",
             "expr/information_theory.py",
             "expr/basic.py",
             "testing/datasets.py",
             "testing/cost.py",
             "gui/graph_2D.py",
             "gui/patch_viewer.py",
             "sandbox/cuda_convnet/weight_acts.py",
             "sandbox/cuda_convnet/filter_acts.py",
             "sandbox/cuda_convnet/tests/test_filter_acts_strided.py",
             "sandbox/cuda_convnet/tests/test_probabilistic_max_pooling.py",
             "sandbox/cuda_convnet/tests/test_filter_acts.py",
             "sandbox/cuda_convnet/tests/test_weight_acts_strided.py",
             "sandbox/cuda_convnet/tests/test_image_acts_strided.py",
             "sandbox/cuda_convnet/tests/test_img_acts.py",
             "sandbox/cuda_convnet/tests/test_weight_acts.py",
             "sandbox/cuda_convnet/tests/test_stochastic_pool.py",
             "sandbox/cuda_convnet/specialized_bench.py",
             "sandbox/cuda_convnet/response_norm.py",
             "sandbox/cuda_convnet/__init__.py",
             "sandbox/cuda_convnet/img_acts.py",
             "sandbox/cuda_convnet/convnet_compile.py",
             "sandbox/cuda_convnet/base_acts.py",
             "sandbox/cuda_convnet/pthreads.py",
             "sandbox/cuda_convnet/pool.py",
             "sandbox/cuda_convnet/bench.py",
             "sandbox/cuda_convnet/stochastic_pool.py",
             "sandbox/cuda_convnet/probabilistic_max_pooling.py",
             "sandbox/tuple_var.py",
             "sandbox/lisa_rl/bandit/average_agent.py",
             "sandbox/lisa_rl/bandit/classifier_bandit.py",
             "sandbox/lisa_rl/bandit/classifier_agent.py",
             "sandbox/lisa_rl/bandit/plot_reward.py",
             "config/old_config.py",
             "config/yaml_parse.py",
             "datasets/utlc.py",
             "datasets/mnistplus.py",
             "datasets/cos_dataset.py",
             "datasets/cifar10.py",
             "datasets/svhn.py",
             "datasets/tests/test_csv_dataset.py",
             "datasets/tests/test_icml07.py",
             "datasets/tests/test_utlc.py",
             "datasets/preprocessing.py",
             "datasets/config.py",
             "datasets/dense_design_matrix.py",
             "datasets/adult.py",
             "datasets/tfd.py",
             "datasets/icml07.py",
             "datasets/filetensor.py",
             "datasets/hepatitis.py",
             "datasets/wiskott.py",
             "datasets/mnist.py",
             "datasets/sparse_dataset.py",
             "datasets/csv_dataset.py",
             "datasets/cifar100.py",
             "datasets/tl_challenge.py",
             "datasets/norb_small.py",
             "datasets/retina.py",
             "datasets/ocr.py",
             "datasets/stl10.py",
             "datasets/vector_spaces_dataset.py",
             "datasets/debug.py",
             "datasets/binarizer.py",
             "utils/utlc.py",
             "utils/tests/test_serial.py",
             "utils/common_strings.py",
             "utils/serial.py",
             "utils/mem.py",
             "format/tests/test_target_format.py",
             "format/target_format.py",
             "dataset_get/dataset-get.py",
             "dataset_get/helper-scripts/make-archive.py",
             "dataset_get/dataset_resolver.py",
             "pca.py",
             "monitor.py",
             "optimization/batch_gradient_descent.py",
             "optimization/minres.py",
             "costs/ebm_estimation.py",
             "costs/gsn.py",
             "costs/mlp/__init__.py",
             "costs/mlp/dropout.py",
             "costs/mlp/missing_target_cost.py",
             "costs/cost.py",
             "costs/dbm.py",
             "costs/autoencoder.py",
             "linear/conv2d.py",
             "linear/local_c01b.py",
             "linear/matrixmul.py",
             "linear/linear_transform.py",
             "linear/conv2d_c01b.py",
             "energy_functions/rbm_energy.py",
             "scripts/plot_monitor.py",
             "scripts/tests/test_autoencoder.py",
             "scripts/show_examples.py",
             "scripts/summarize_model.py",
             "scripts/lcc_tangents/make_dataset.py",
             "scripts/pkl_inspector.py",
             "scripts/print_monitor.py",
             "scripts/show_binocular_greyscale_examples.py",
             "scripts/jobman/tester.py",
             "scripts/dbm/show_samples.py",
             "scripts/dbm/show_reconstructions.py",
             "scripts/dbm/dbm_metrics.py",
             "scripts/dbm/top_filters.py",
             "scripts/papers/maxout/svhn_preprocessing.py",
             "scripts/papers/jia_huang_wkshp_11/fit_final_model.py",
             "scripts/papers/jia_huang_wkshp_11/evaluate.py",
             "scripts/papers/jia_huang_wkshp_11/extract_features.py",
             "scripts/papers/jia_huang_wkshp_11/assemble.py",
             "scripts/gpu_pkl_to_cpu_pkl.py",
             "scripts/datasets/make_cifar10_whitened.py",
             "scripts/datasets/make_cifar100_patches_8x8.py",
             "scripts/datasets/make_cifar100_patches.py",
             "scripts/datasets/make_cifar10_gcn_whitened.py",
             "scripts/datasets/make_cifar100_whitened.py",
             "scripts/datasets/make_stl10_patches_8x8.py",
             "scripts/datasets/make_cifar100_gcn_whitened.py",
             "scripts/datasets/make_stl10_whitened.py",
             "scripts/datasets/make_stl10_patches.py",
             "scripts/gsn_example.py",
             "scripts/tutorials/tests/test_convnet.py",
             "scripts/tutorials/deep_trainer/run_deep_trainer.py",
             "scripts/tutorials/grbm_smd/make_dataset.py",
             "scripts/tutorials/grbm_smd/test_grbm_smd.py",
             "scripts/icml_2013_wrepl/multimodal/"
             "extract_layer_2_kmeans_features.py",
             "scripts/icml_2013_wrepl/multimodal/make_submission.py",
             "scripts/icml_2013_wrepl/multimodal/lcn.py",
             "scripts/icml_2013_wrepl/multimodal/extract_kmeans_features.py",
             "scripts/icml_2013_wrepl/emotions/emotions_dataset.py",
             "scripts/icml_2013_wrepl/emotions/make_submission.py",
             "scripts/icml_2013_wrepl/black_box/black_box_dataset.py",
             "scripts/icml_2013_wrepl/black_box/make_submission.py",
             "scripts/diff_monitor.py",
             "corruption.py",
             "training_algorithms/sgd.py",
             "devtools/nan_guard.py",
             "models/mlp.py",
             "sandbox/lisa_rl/bandit/gaussian_bandit.py",
             "config/tests/test_yaml_parse.py",
             "utils/iteration.py",
             "utils/track_version.py",
             "scripts/get_version.py",
             "training_algorithms/training_algorithm.py",
             ]

def test_format():
    format_infractions = []
    for path in list_files(".py"):
        rel_path = os.path.relpath(path, pylearn2.__path__[0])
        if rel_path in whitelist:
            continue
        with open(path) as file:
            for i, line in enumerate(file):
                if len(line.rstrip()) > 79:
                    format_infractions.append((path, i + 1))
    if len(format_infractions) > 0:
        msg = "\n".join('File "%s" line %d has more than 79 characters'
              % (fn, line) for fn, line in format_infractions)
        raise AssertionError("Format not respected:\n%s" % msg)

if __name__ == "__main__":
    print "Files that may be removed from whitelist: "
    for path in whitelist:
        with open(path) as file:
            white = False
            for i, line in enumerate(file):
                if len(line.rstrip()) > 79:
                    white = True
            if not white:
                print path
<|endoftext|>


<commit_before># Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.

import os
import unittest

from monty.serialization import loadfn

from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure
from pymatgen.symmetry.bandstructure import HighSymmKpath
from pymatgen.util.testing import PymatgenTest

try:
    from seekpath import get_path  # type: ignore
except ImportError:
    get_path = None

class HighSymmKpathTest(PymatgenTest):
    @unittest.skipIf(get_path is None, "No seek path present.")
    def test_kpath_generation(self):
        triclinic = [1, 2]
        monoclinic = range(3, 16)
        orthorhombic = range(16, 75)
        tetragonal = range(75, 143)
        rhombohedral = range(143, 168)
        hexagonal = range(168, 195)
        cubic = range(195, 231)

        species = ["K", "La", "Ti"]
        coords = [[0.345, 5, 0.77298], [0.1345, 5.1, 0.77298], [0.7, 0.8, 0.9]]
        for i in range(230):
            sg_num = i + 1
            if sg_num in triclinic:
                lattice = Lattice(
                    [
                        [3.0233057319441246, 1, 0],
                        [0, 7.9850357844548681, 1],
                        [0, 1.2, 8.1136762279561818],
                    ]
                )
            elif sg_num in monoclinic:
                lattice = Lattice.monoclinic(2, 9, 1, 99)
            elif sg_num in orthorhombic:
                lattice = Lattice.orthorhombic(2, 9, 1)
            elif sg_num in tetragonal:
                lattice = Lattice.tetragonal(2, 9)
            elif sg_num in rhombohedral:
                lattice = Lattice.hexagonal(2, 95)
            elif sg_num in hexagonal:
                lattice = Lattice.hexagonal(2, 9)
            elif sg_num in cubic:
                lattice = Lattice.cubic(2)

            struct = Structure.from_spacegroup(sg_num, lattice, species, coords)

            # Throws error if something doesn't work, causing test to fail.
            kpath = HighSymmKpath(struct, path_type="all")
            _ = kpath.get_kpoints()

    def test_continuous_kpath(self):
        bs = loadfn(os.path.join(PymatgenTest.TEST_FILES_DIR, "Cu2O_361_bandstructure.json"))
        cont_bs = loadfn(os.path.join(PymatgenTest.TEST_FILES_DIR, "Cu2O_361_bandstructure_continuous.json"))
        alt_bs = HighSymmKpath(bs.structure).get_continuous_path(bs)

        self.assertEqual(cont_bs.as_dict(), alt_bs.as_dict())

if __name__ == "__main__":
    unittest.main()
<commit_msg>Alter continuous BS test<commit_after># Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.

import os
import unittest

from monty.serialization import loadfn

from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure
from pymatgen.symmetry.bandstructure import HighSymmKpath
from pymatgen.util.testing import PymatgenTest

try:
    from seekpath import get_path  # type: ignore
except ImportError:
    get_path = None

class HighSymmKpathTest(PymatgenTest):
    @unittest.skipIf(get_path is None, "No seek path present.")
    def test_kpath_generation(self):
        triclinic = [1, 2]
        monoclinic = range(3, 16)
        orthorhombic = range(16, 75)
        tetragonal = range(75, 143)
        rhombohedral = range(143, 168)
        hexagonal = range(168, 195)
        cubic = range(195, 231)

        species = ["K", "La", "Ti"]
        coords = [[0.345, 5, 0.77298], [0.1345, 5.1, 0.77298], [0.7, 0.8, 0.9]]
        for i in range(230):
            sg_num = i + 1
            if sg_num in triclinic:
                lattice = Lattice(
                    [
                        [3.0233057319441246, 1, 0],
                        [0, 7.9850357844548681, 1],
                        [0, 1.2, 8.1136762279561818],
                    ]
                )
            elif sg_num in monoclinic:
                lattice = Lattice.monoclinic(2, 9, 1, 99)
            elif sg_num in orthorhombic:
                lattice = Lattice.orthorhombic(2, 9, 1)
            elif sg_num in tetragonal:
                lattice = Lattice.tetragonal(2, 9)
            elif sg_num in rhombohedral:
                lattice = Lattice.hexagonal(2, 95)
            elif sg_num in hexagonal:
                lattice = Lattice.hexagonal(2, 9)
            elif sg_num in cubic:
                lattice = Lattice.cubic(2)

            struct = Structure.from_spacegroup(sg_num, lattice, species, coords)

            # Throws error if something doesn't work, causing test to fail.
            kpath = HighSymmKpath(struct, path_type="all")
            _ = kpath.get_kpoints()

    def test_continuous_kpath(self):
        bs = loadfn(os.path.join(PymatgenTest.TEST_FILES_DIR, "Cu2O_361_bandstructure.json"))
        cont_bs = loadfn(os.path.join(PymatgenTest.TEST_FILES_DIR, "Cu2O_361_bandstructure_continuous.json"))
        alt_bs = HighSymmKpath(bs.structure).get_continuous_path(bs)

        self.assertEqual(cont_bs.as_dict(), alt_bs.as_dict())
        self.assertEqual(alt_bs.kpoints[0].label, alt_bs.kpoints[-1].label)

if __name__ == "__main__":
    unittest.main()
<|endoftext|>


<commit_before>__author__ = 'psuresh'
from pyramid.authorization import ACLAuthorizationPolicy

# Use the default ACL policy and define permissions as ACL's`

class OktaAuthorizationPolicy(ACLAuthorizationPolicy):
    pass<commit_msg>add custom authz policy<commit_after>__author__ = 'psuresh'

# Use the default ACL policy and define permissions as ACL's`

class OktaAuthorizationPolicy(object):
    def permits(self, context, principals, permission):
        """ Return ``True`` if any of the ``principals`` is allowed the
        ``permission`` in the current ``context``, else return ``False``
        """
        if permission in principals:
            return True
        else:
            return False

    def principals_allowed_by_permission(self, context, permission):
        """ Return a set of principal identifiers allowed by the
        ``permission`` in ``context``.  This behavior is optional; if you
        choose to not implement it you should define this method as
        something which raises a ``NotImplementedError``.  This method
        will only be called when the
        ``pyramid.security.principals_allowed_by_permission`` API is
        used."""
        raise NotImplementedError<|endoftext|>


<commit_before>import time, requests, json
from pyspider.database.base.projectdb import ProjectDB as BaseProjectDB

class ProjectDB(BaseProjectDB):
    __collection_name__ = 'projectdb'

    def __init__(self, url, database='projectdb'):
        self.url = url + database + "/"
        self.database = database
        self.insert('', {})

    def _default_fields(self, each):
        if each is None:
            return each
        each.setdefault('group', None)
        each.setdefault('status', 'TODO')
        each.setdefault('script', '')
        each.setdefault('comments', None)
        each.setdefault('rate', 0)
        each.setdefault('burst', 0)
        each.setdefault('updatetime', 0)
        return each

    def insert(self, name, obj={}):
        url = self.url + name
        obj = dict(obj)
        obj['name'] = name
        obj['updatetime'] = time.time()
        res = requests.put(url, data = json.dumps(obj), headers = {"Content-Type": "application/json"}).json()
        print('[couchdb projectdb insert] - url: {} res: {}'.format(url,res))
        return res

    def update(self, name, obj={}, **kwargs):
        obj = dict(obj)
        obj.update(kwargs)
        self.insert(name, obj)

    def get_all(self, fields=None):
        payload = {
            "selector": {},
            "fields": fields
        }
        url = self.url + "_find"
        res = requests.post(url, data=json.dumps(payload), headers={"Content-Type": "application/json"}).json()
        print('[couchdb projectdb get_all] - url: {} res: {}'.format(url, res))
        return res

    def get(self, name, fields=None):
        payload = {
            "selector": {"name": name},
            "fields": fields,
            "limit": 1
        }
        url = self.url + "_find"
        res = requests.post(url, data=json.dumps(payload), headers={"Content-Type": "application/json"}).json()
        print('[couchdb projectdb get] - url: {} res: {}'.format(url, res))
        return res

    def check_update(self, timestamp, fields=None):
        for project in self.get_all(fields=('updatetime', 'name')):
            if project['updatetime'] > timestamp:
                project = self.get(project['name'], fields)
                yield self._default_fields(project)

    def drop(self, name):
        doc = self.get(name)
        url = self.url + name + "/" + doc["_rev"]
        res = requests.delete(url, headers={"Content-Type": "application/json"}).json()
        print('[couchdb projectdb drop] - url: {} res: {}'.format(url, res))
        return res

<commit_msg>fixed "Fields must be an array of strings, not: null" eroor<commit_after>import time, requests, json
from pyspider.database.base.projectdb import ProjectDB as BaseProjectDB

class ProjectDB(BaseProjectDB):
    __collection_name__ = 'projectdb'

    def __init__(self, url, database='projectdb'):
        self.url = url + database + "/"
        self.database = database
        self.insert('', {})

    def _default_fields(self, each):
        if each is None:
            return each
        each.setdefault('group', None)
        each.setdefault('status', 'TODO')
        each.setdefault('script', '')
        each.setdefault('comments', None)
        each.setdefault('rate', 0)
        each.setdefault('burst', 0)
        each.setdefault('updatetime', 0)
        return each

    def insert(self, name, obj={}):
        url = self.url + name
        obj = dict(obj)
        obj['name'] = name
        obj['updatetime'] = time.time()
        res = requests.put(url, data = json.dumps(obj), headers = {"Content-Type": "application/json"}).json()
        print('[couchdb projectdb insert] - url: {} res: {}'.format(url,res))
        return res

    def update(self, name, obj={}, **kwargs):
        obj = dict(obj)
        obj.update(kwargs)
        self.insert(name, obj)

    def get_all(self, fields=None):
        if fields is None:
            fields = []
        payload = {
            "selector": {},
            "fields": fields
        }
        url = self.url + "_find"
        res = requests.post(url, data=json.dumps(payload), headers={"Content-Type": "application/json"}).json()
        print('[couchdb projectdb get_all] - url: {} res: {}'.format(url, res))
        return res

    def get(self, name, fields=None):
        if fields is None:
            fields = []
        payload = {
            "selector": {"name": name},
            "fields": fields,
            "limit": 1
        }
        url = self.url + "_find"
        res = requests.post(url, data=json.dumps(payload), headers={"Content-Type": "application/json"}).json()
        print('[couchdb projectdb get] - url: {} res: {}'.format(url, res))
        return res

    def check_update(self, timestamp, fields=None):
        if fields is None:
            fields = []
        for project in self.get_all(fields=('updatetime', 'name')):
            if project['updatetime'] > timestamp:
                project = self.get(project['name'], fields)
                yield self._default_fields(project)

    def drop(self, name):
        doc = self.get(name)
        url = self.url + name + "/" + doc["_rev"]
        res = requests.delete(url, headers={"Content-Type": "application/json"}).json()
        print('[couchdb projectdb drop] - url: {} res: {}'.format(url, res))
        return res

<|endoftext|>