<commit_before>from __future__ import division
# copied from James's code...

def open_file_return_array(filename):
    import pycbf
    import numpy

    # open file
    cbf_handle = pycbf.cbf_handle_struct()
    cbf_handle.read_file(filename, pycbf.MSG_DIGEST)
    cbf_handle.rewind_datablock()

    # find right datablock
    cbf_handle.select_datablock(0)
    cbf_handle.select_category(0)
    cbf_handle.select_column(2)
    cbf_handle.select_row(0)

    type = cbf_handle.get_typeofvalue()
    assert (type.find('bnry') > -1)

    # read and reshape
    image = numpy.fromstring(cbf_handle.get_integerarray_as_string(),
                             numpy.int32)
    parameters = cbf_handle.get_integerarrayparameters_wdims()
    image.shape = (parameters[10], parameters[9])

    return image

def plot_image(image):
    from matplotlib import pyplot
    pyplot.imshow(image)
    pyplot.savefig('biostruct-25.png')

    return

def read_integrate_hkl_apply_corrections(int_hkl, x_crns_file, y_crns_file):
    '''Read X corrections and Y corrections to arrays; for record in
    int_hkl read x, y positions and compute r.m.s. deviations between
    observed and calculated centroids with and without the corrections.
    N.B. will only compute offsets for reflections with I/sigma > 10.'''

    from scitbx import matrix
    import math

    x_corrections = open_file_return_array(x_crns_file)
    y_corrections = open_file_return_array(y_crns_file)

    sumxx_orig = 0.0
    n_obs = 0
    sumxx_corr = 0.0

    for record in open(int_hkl):
        if record.startswith('!'):
            continue
        values = map(float, record.split())
        i, sigi = values[3], values[4]

        if sigi < 0:
            continue

        if i / sigi < 10:
            continue

        xc, yc, zc = values[5:8]
        xo, yo, zo = values[12:15]

        xc_orig = matrix.col((xc, yc))
        xo_orig = matrix.col((xo, yo))

        n_obs += 1
        sumxx_orig += (xo_orig - xc_orig).dot()

        # get the correcions for this pixel... N.B. 1 + ... lost due to C-style
        # rather than Fortran-style arrays

        ix4 = int(round((xc - 2) / 4))
        iy4 = int(round((yc - 2) / 4))

        # hmm.... Fortran multi-dimensional array ordering... identified by
        # bounds error other way around...

        dx = 0.1 * x_corrections[iy4, ix4]
        dy = 0.1 * y_corrections[iy4, ix4]

        xc_corr = matrix.col((xc + dx, yc + dy))

        sumxx_corr += (xo_orig - xc_corr).dot()

    return math.sqrt(sumxx_orig / n_obs), math.sqrt(sumxx_corr / n_obs)

if __name__ == '__main__':
    import sys

    orig, corr = read_integrate_hkl_apply_corrections(
        sys.argv[1], sys.argv[2], sys.argv[3])

    print orig, corr
<commit_msg>Work in progress; calculating positions from orientation matrix as well.<commit_after>from __future__ import division
# copied from James's code...

def open_file_return_array(filename):
    import pycbf
    import numpy

    # open file
    cbf_handle = pycbf.cbf_handle_struct()
    cbf_handle.read_file(filename, pycbf.MSG_DIGEST)
    cbf_handle.rewind_datablock()

    # find right datablock
    cbf_handle.select_datablock(0)
    cbf_handle.select_category(0)
    cbf_handle.select_column(2)
    cbf_handle.select_row(0)

    type = cbf_handle.get_typeofvalue()
    assert (type.find('bnry') > -1)

    # read and reshape
    image = numpy.fromstring(cbf_handle.get_integerarray_as_string(),
                             numpy.int32)
    parameters = cbf_handle.get_integerarrayparameters_wdims()
    image.shape = (parameters[10], parameters[9])

    return image

def plot_image(image):
    from matplotlib import pyplot
    pyplot.imshow(image)
    pyplot.savefig('biostruct-25.png')

    return

def parse_xds_xparm_scan_info(xparm_file):
    values = map(float, open(xparm_file).read().split())

    assert(len(values) == 42)

    img_start, osc_start, osc_range = values[:3]

    return img_start, osc_start, osc_range

def read_integrate_hkl_apply_corrections(
        int_hkl, x_crns_file, y_crns_file, xparm_file):
    '''Read X corrections and Y corrections to arrays; for record in
    int_hkl read x, y positions and compute r.m.s. deviations between
    observed and calculated centroids with and without the corrections.
    N.B. will only compute offsets for reflections with I/sigma > 10.'''

    from scitbx import matrix
    import math

    x_corrections = open_file_return_array(x_crns_file)
    y_corrections = open_file_return_array(y_crns_file)

    sumxx_orig = 0.0
    n_obs = 0
    sumxx_corr = 0.0

    # FIXME find code to run prediction of reflection lists...

    img_start, osc_start, osc_range = parse_xds_xparm_scan_info(xparm_file)

    from rstbx.cftbx.coordinate_frame_converter import \
        coordinate_frame_converter

    cfc = coordinate_frame_converter(xparm_file)

    fast = cfc.get_c('detector_fast')
    slow = cfc.get_c('detector_slow')
    normal = fast.cross(slow)

    origin = cfc.get_c('detector_origin')
    distance = origin.dot(normal)

    print distance

    A = cfc.get_c('real_space_a')
    B = cfc.get_c('real_space_b')
    C = cfc.get_c('real_space_c')

    cell = (A.length(), B.length(), C.length(), B.angle(C, deg = True),
            C.angle(A, deg = True), A.angle(B, deg = True))

    u, b = cfc.get_u_b()
    ub = u * b
    s0 = - cfc.get_c('sample_to_source')
    axis = cfc.get_c('rotation_axis')

    for record in open(int_hkl):
        if record.startswith('!'):
            continue
        values = map(float, record.split())
        i, sigi = values[3], values[4]

        if sigi < 0:
            continue

        if i / sigi < 10:
            continue

        # FIXME also read the GXPARM.XDS file and recompute the reflection
        # position from the Miller indices and model of experimental geometry
        # as xc, yc, zc... Ah, smarter: use the zc from the file to provide the
        # rotation, not a full prediction: only then need to worry about
        # computing the intersection.

        hkl = map(int, map(round, values[:3]))

        xc, yc, zc = values[5:8]

        p = (zc - img_start) * osc_range + osc_start

        rubh = (ub * hkl).rotate(axis, - p, deg = True)
        q = (s0 + rubh).normalize()

        r = q * distance / q.dot(normal) - origin

        _xc = r.dot(fast)
        _yc = r.dot(slow)

        xo, yo, zo = values[12:15]

        xc_orig = matrix.col((xc, yc))
        xo_orig = matrix.col((xo, yo))

        n_obs += 1
        sumxx_orig += (xo_orig - xc_orig).dot()

        # get the correcions for this pixel... N.B. 1 + ... lost due to C-style
        # rather than Fortran-style arrays

        ix4 = int(round((xc - 2) / 4))
        iy4 = int(round((yc - 2) / 4))

        # hmm.... Fortran multi-dimensional array ordering... identified by
        # bounds error other way around...

        dx = 0.1 * x_corrections[iy4, ix4]
        dy = 0.1 * y_corrections[iy4, ix4]

        xc_corr = matrix.col((xc + dx, yc + dy))

        sumxx_corr += (xo_orig - xc_corr).dot()

    return math.sqrt(sumxx_orig / n_obs), math.sqrt(sumxx_corr / n_obs)

if __name__ == '__main__':
    import sys

    orig, corr = read_integrate_hkl_apply_corrections(
        sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])

    print orig, corr
<|endoftext|>


<commit_before>#
# Project:
#   glideinWMS
#
# File Version:
#
# Description:
#   This module implements the functions needed
#   to aggregate the monitoring fo the frontend
#
# Author:
#   Igor Sfiligoi (Mar 19th 2009)
#

import time,string,os.path,copy
import timeConversion
import xmlParse,xmlFormat
import glideinFrontendMonitoring
import rrdSupport
import tempfile
import shutil
import time

############################################################
#
# Configuration
#
############################################################

class MonitorAggregatorConfig:
    def __init__(self):
        # The name of the attribute that identifies the glidein
        self.monitor_dir="monitor/"

        # list of entries
        self.entries=[]

        # name of the status files
        self.status_relname="frontend_status.xml"

    def config_frontend(self,monitor_dir,groups):
        self.monitor_dir=monitor_dir
        self.groups=groups
        glideinFrontendMonitoring.monitoringConfig.monitor_dir=monitor_dir

# global configuration of the module
monitorAggregatorConfig=MonitorAggregatorConfig()

###########################################################
#
# Functions
#
###########################################################

frontend_status_attributes={'Jobs':("Idle","OldIdle","Running","Total"),
                   'Glideins':("Idle","Running","Total"),
                   'MatchedJobs':("Idle","EffIdle","OldIdle","Running","RunningHere"),
                   'MatchedGlideins':("Total","Idle","Running"),
                   'Requested':("Idle","MaxRun")}

frontend_total_type_strings={'Jobs':'Jobs','Glideins':'Glidein','MatchedJobs':'MatchJob',
                      'MatchedGlideins':'MatchGlidein','Requested':'Req'}
frontend_job_type_strings={'MatchedJobs':'MatchJob',
                      'MatchedGlideins':'MatchGlidein','Requested':'Req'}

####################################
rrd_problems_found=False
def verifyHelper(filename,dict, fix_rrd=False):
    """
    Helper function for verifyRRD.  Checks one file,
    prints out errors.  if fix_rrd, will attempt to
    dump out rrd to xml, add the missing attributes,
    then restore.  Original file is obliterated.

    @param filename: filename of rrd to check
    @param dict: expected dictionary
    @param fix_rrd: if true, will attempt to add missing attrs
    """
    global rrd_problems_found
    if not os.path.exists(filename):
        print "WARNING: %s missing, will be created on restart" % (filename)
        return
    rrd_obj=rrdSupport.rrdSupport()
    (missing,extra)=rrd_obj.verify_rrd(filename,dict)
    for attr in extra:
        print "ERROR: %s has extra attribute %s" % (filename,attr)
        if fix_rrd:
            print "ERROR: fix_rrd cannot fix extra attributes"
    if not fix_rrd:
        for attr in missing:
            print "ERROR: %s missing attribute %s" % (filename,attr)
        if len(missing) > 0:
            rrd_problems_found=True
    if fix_rrd and (len(missing) > 0):
        (f,tempfilename)=tempfile.mkstemp()
        (out,tempfilename2)=tempfile.mkstemp()
        (restored,restoredfilename)=tempfile.mkstemp()
        backup_str=str(int(time.time()))+".backup"
        print "Fixing %s... (backed up to %s)" % (filename,filename+backup_str)
        os.close(out)
        os.close(restored)
        os.unlink(restoredfilename)
        #Use exe version since dump, restore not available in rrdtool
        dump_obj=rrdSupport.rrdtool_exe()
        outstr=dump_obj.dump(filename)
        for line in outstr:
            os.write(f,line)
        os.close(f)
        rrdSupport.addDataStore(tempfilename,tempfilename2,missing)
        os.unlink(filename)
        outstr=dump_obj.restore(tempfilename2,restoredfilename)
        os.unlink(tempfilename)
        os.unlink(tempfilename2)
        shutil.move(restoredfilename,filename)
    if len(extra) > 0:
        rrd_problems_found=True

def verifyRRD(fix_rrd=False):
    """
    Go through all known monitoring rrds and verify that they
    match existing schema (could be different if an upgrade happened)
    If fix_rrd is true, then also attempt to add any missing attributes.
    """
    global rrd_problems_found
    global monitorAggregatorConfig
    dir=monitorAggregatorConfig.monitor_dir
    total_dir=os.path.join(dir,"total")

    status_dict={}
    status_total_dict={}
    for tp in frontend_status_attributes.keys():
        if tp in frontend_total_type_strings.keys():
            tp_str=frontend_total_type_strings[tp]
            attributes_tp=frontend_status_attributes[tp]
            for a in attributes_tp:
                status_total_dict["%s%s"%(tp_str,a)]=None
        if tp in frontend_job_type_strings.keys():
            tp_str=frontend_job_type_strings[tp]
            attributes_tp=frontend_status_attributes[tp]
            for a in attributes_tp:
                status_dict["%s%s"%(tp_str,a)]=None

    if not os.path.isdir(dir):
        print "WARNING: monitor/ directory does not exist, skipping rrd verification."
        return True
    for filename in os.listdir(dir):
        if (filename[:6]=="group_") or (filename=="total"):
            current_dir=os.path.join(dir,filename)
            if filename=="total":
                verifyHelper(os.path.join(current_dir,
                    "Status_Attributes.rrd"),status_total_dict, fix_rrd)
            for dirname in os.listdir(current_dir):
                current_subdir=os.path.join(current_dir,dirname)
                if dirname[:6]=="state_":
                    verifyHelper(os.path.join(current_subdir,
                        "Status_Attributes.rrd"),status_dict, fix_rrd)
                if dirname[:8]=="factory_":
                    verifyHelper(os.path.join(current_subdir,
                        "Status_Attributes.rrd"),status_dict, fix_rrd)
                if dirname=="total":
                    verifyHelper(os.path.join(current_subdir,
                        "Status_Attributes.rrd"),status_total_dict, fix_rrd)
    return not rrd_problems_found

####################################
# PRIVATE - Used by aggregateStatus
# Write one RRD
def write_one_rrd(name,updated,data,fact=0):
    if fact==0:
        type_strings=frontend_total_type_strings
    else:
        type_strings=frontend_job_type_strings

    # initialize the RRD dictionary, so it gets created properly
    val_dict={}
    for tp in frontend_status_attributes.keys():
        if tp in type_strings.keys():
            tp_str=type_strings[tp]
            attributes_tp=frontend_status_attributes[tp]
            for a in attributes_tp:
                val_dict["%s%s"%(tp_str,a)]=None

    for tp in data.keys():
        # type - status or requested
        if not (tp in frontend_status_attributes.keys()):
            continue
        if not (tp in type_strings.keys()):
            continue

        tp_str=type_strings[tp]
        attributes_tp=frontend_status_attributes[tp]

        tp_el=data[tp]

        for a in tp_el.keys():
            if a in attributes_tp:
                a_el=int(tp_el[a])
                if type(a_el)!=type({}): # ignore subdictionaries
                    val_dict["%s%s"%(tp_str,a)]=a_el

    glideinFrontendMonitoring.monitoringConfig.establish_dir("%s"%name)
    glideinFrontendMonitoring.monitoringConfig.write_rrd_multi("%s"%name,
                                                               "GAUGE",updated,val_dict)

##############################################################################
# create an aggregate of status files, write it in an aggregate status file
# end return the values
def aggregateStatus():
    global monitorAggregatorConfig

    type_strings={'Jobs':'Jobs','Glideins':'Glidein','MatchedJobs':'MatchJob',
                  'MatchedGlideins':'MatchGlidein','Requested':'Req'}
    global_total={'Jobs':None,'Glideins':None,'MatchedJobs':None,'Requested':None,'MatchedGlideins':None}
    status={'groups':{},'total':global_total}
    global_fact_totals={}

    for fos in ('factories','states'):
        global_fact_totals[fos]={}

    nr_groups=0
    for group in monitorAggregatorConfig.groups:
        # load group status file
        status_fname=os.path.join(os.path.join(monitorAggregatorConfig.monitor_dir,'group_'+group),
                                  monitorAggregatorConfig.status_relname)
        try:
            group_data=xmlParse.xmlfile2dict(status_fname)
        except IOError:
            continue # file not found, ignore

        # update group
        status['groups'][group]={}
        for fos in ('factories','states'):
            try:
                status['groups'][group][fos]=group_data[fos]
            except KeyError, e:
                # first time after upgrade factories may not be defined
                status['groups'][group][fos]={}

        this_group=status['groups'][group]
        for fos in ('factories','states'):
          for fact in this_group[fos].keys():
            this_fact=this_group[fos][fact]
            if not fact in global_fact_totals[fos].keys():
                # first iteration through, set fact totals equal to the first group's fact totals
                global_fact_totals[fos][fact]={}
                for attribute in type_strings.keys():
                    global_fact_totals[fos][fact][attribute]={}
                    if attribute in this_fact.keys():
                        for type_attribute in this_fact[attribute].keys():
                            this_type_attribute=this_fact[attribute][type_attribute]
                            try:
                                global_fact_totals[fos][fact][attribute][type_attribute]=int(this_type_attribute)
                            except:
                                pass
            else:
                # next iterations, factory already present in global fact totals, add the new factory values to the previous ones
                for attribute in type_strings.keys():
                    if attribute in this_fact.keys():
                        for type_attribute in this_fact[attribute].keys():
                            this_type_attribute=this_fact[attribute][type_attribute]
                            if type(this_type_attribute)==type(global_fact_totals[fos]):
                                # dict, do nothing
                                pass
                            else:
                                if type_attribute in global_fact_totals[fos][fact][attribute].keys() and attribute in global_fact_totals[fos][fact].keys():
                                   global_fact_totals[fos][fact][attribute][type_attribute]+=int(this_type_attribute)
                                else:
                                   global_fact_totals[fos][fact][attribute][type_attribute]=int(this_type_attribute)
        #nr_groups+=1
        #status['groups'][group]={}

        if group_data.has_key('total'):
            nr_groups += 1
            status['groups'][group]['total'] = group_data['total']

            for w in global_total.keys():
                tel = global_total[w]
                if not group_data['total'].has_key(w):
                    continue
                #status['groups'][group][w]=group_data[w]
                el = group_data['total'][w]
                if tel == None:
                    # new one, just copy over
                    global_total[w] = {}
                    tel = global_total[w]
                    for a in el.keys():
                        tel[a] = int(el[a]) #coming from XML, everything is a string
                else:
                    # successive, sum
                    for a in el.keys():
                        if tel.has_key(a):
                            tel[a] += int(el[a])

                    # if any attribute from prev. factories are not in the current one, remove from total
                    for a in tel.keys():
                        if not el.has_key(a):
                            del tel[a]

    for w in global_total.keys():
        if global_total[w]==None:
            del global_total[w] # remove group if not defined

    # Write xml files

    updated=time.time()
    xml_str=('<?xml version="1.0" encoding="ISO-8859-1"?>\n\n'+
             '<VOFrontendStats>\n'+
             xmlFormat.time2xml(updated, "updated", indent_tab=xmlFormat.DEFAULT_TAB,leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+
             xmlFormat.dict2string(status["groups"],dict_name="groups",el_name="group",
                                   subtypes_params={"class":{"dicts_params":{"factories":{"el_name":"factory",
                                                                                "subtypes_params":{"class":{"subclass_params":{"Requested":{"dicts_params":{"Parameters":{"el_name":"Parameter",
                                                                                "subtypes_params":{"class":{}}}}}}}}},
                                                                             "states":{"el_name":"state",
                                                                                "subtypes_params":{"class":{"subclass_params":{"Requested":{"dicts_params":{"Parameters":{"el_name":"Parameter",
                                                                                "subtypes_params":{"class":{}}}}}}}}}
                                                                             }}},
                                   leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+
             xmlFormat.class2string(status["total"],inst_name="total",leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+

             xmlFormat.dict2string(global_fact_totals['factories'],dict_name="factories",el_name="factory",
                                   subtypes_params={"class":{"subclass_params":{"Requested":{"dicts_params":{"Parameters":{"el_name":"Parameter",

       "subtypes_params":{"class":{}}}}}}}},
                                   leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+
             xmlFormat.dict2string(global_fact_totals['states'],dict_name="states",el_name="state",
                                   subtypes_params={"class":{"subclass_params":{"Requested":{"dicts_params":{"Parameters":{"el_name":"Parameter",

       "subtypes_params":{"class":{}}}}}}}},
                                   leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+
             "</VOFrontendStats>\n")

    glideinFrontendMonitoring.monitoringConfig.write_file(monitorAggregatorConfig.status_relname,xml_str)

    # Write rrds

    glideinFrontendMonitoring.monitoringConfig.establish_dir("total")
    write_one_rrd("total/Status_Attributes",updated,global_total,0)

    for fact in global_fact_totals['factories'].keys():
        fe_dir="total/factory_%s"%glideinFrontendMonitoring.sanitize(fact)
        glideinFrontendMonitoring.monitoringConfig.establish_dir(fe_dir)
        write_one_rrd("%s/Status_Attributes"%fe_dir,updated,global_fact_totals['factories'][fact],1)
    for fact in global_fact_totals['states'].keys():
        fe_dir="total/state_%s"%glideinFrontendMonitoring.sanitize(fact)
        glideinFrontendMonitoring.monitoringConfig.establish_dir(fe_dir)
        write_one_rrd("%s/Status_Attributes"%fe_dir,updated,global_fact_totals['states'][fact],1)

    return status

<commit_msg>Bug 2963: Reversed the order of the conditions to avoid key error<commit_after>#
# Project:
#   glideinWMS
#
# File Version:
#
# Description:
#   This module implements the functions needed
#   to aggregate the monitoring fo the frontend
#
# Author:
#   Igor Sfiligoi (Mar 19th 2009)
#

import time,string,os.path,copy
import timeConversion
import xmlParse,xmlFormat
import glideinFrontendMonitoring
import rrdSupport
import tempfile
import shutil
import time

############################################################
#
# Configuration
#
############################################################

class MonitorAggregatorConfig:
    def __init__(self):
        # The name of the attribute that identifies the glidein
        self.monitor_dir="monitor/"

        # list of entries
        self.entries=[]

        # name of the status files
        self.status_relname="frontend_status.xml"

    def config_frontend(self,monitor_dir,groups):
        self.monitor_dir=monitor_dir
        self.groups=groups
        glideinFrontendMonitoring.monitoringConfig.monitor_dir=monitor_dir

# global configuration of the module
monitorAggregatorConfig=MonitorAggregatorConfig()

###########################################################
#
# Functions
#
###########################################################

frontend_status_attributes={'Jobs':("Idle","OldIdle","Running","Total"),
                   'Glideins':("Idle","Running","Total"),
                   'MatchedJobs':("Idle","EffIdle","OldIdle","Running","RunningHere"),
                   'MatchedGlideins':("Total","Idle","Running"),
                   'Requested':("Idle","MaxRun")}

frontend_total_type_strings={'Jobs':'Jobs','Glideins':'Glidein','MatchedJobs':'MatchJob',
                      'MatchedGlideins':'MatchGlidein','Requested':'Req'}
frontend_job_type_strings={'MatchedJobs':'MatchJob',
                      'MatchedGlideins':'MatchGlidein','Requested':'Req'}

####################################
rrd_problems_found=False
def verifyHelper(filename,dict, fix_rrd=False):
    """
    Helper function for verifyRRD.  Checks one file,
    prints out errors.  if fix_rrd, will attempt to
    dump out rrd to xml, add the missing attributes,
    then restore.  Original file is obliterated.

    @param filename: filename of rrd to check
    @param dict: expected dictionary
    @param fix_rrd: if true, will attempt to add missing attrs
    """
    global rrd_problems_found
    if not os.path.exists(filename):
        print "WARNING: %s missing, will be created on restart" % (filename)
        return
    rrd_obj=rrdSupport.rrdSupport()
    (missing,extra)=rrd_obj.verify_rrd(filename,dict)
    for attr in extra:
        print "ERROR: %s has extra attribute %s" % (filename,attr)
        if fix_rrd:
            print "ERROR: fix_rrd cannot fix extra attributes"
    if not fix_rrd:
        for attr in missing:
            print "ERROR: %s missing attribute %s" % (filename,attr)
        if len(missing) > 0:
            rrd_problems_found=True
    if fix_rrd and (len(missing) > 0):
        (f,tempfilename)=tempfile.mkstemp()
        (out,tempfilename2)=tempfile.mkstemp()
        (restored,restoredfilename)=tempfile.mkstemp()
        backup_str=str(int(time.time()))+".backup"
        print "Fixing %s... (backed up to %s)" % (filename,filename+backup_str)
        os.close(out)
        os.close(restored)
        os.unlink(restoredfilename)
        #Use exe version since dump, restore not available in rrdtool
        dump_obj=rrdSupport.rrdtool_exe()
        outstr=dump_obj.dump(filename)
        for line in outstr:
            os.write(f,line)
        os.close(f)
        rrdSupport.addDataStore(tempfilename,tempfilename2,missing)
        os.unlink(filename)
        outstr=dump_obj.restore(tempfilename2,restoredfilename)
        os.unlink(tempfilename)
        os.unlink(tempfilename2)
        shutil.move(restoredfilename,filename)
    if len(extra) > 0:
        rrd_problems_found=True

def verifyRRD(fix_rrd=False):
    """
    Go through all known monitoring rrds and verify that they
    match existing schema (could be different if an upgrade happened)
    If fix_rrd is true, then also attempt to add any missing attributes.
    """
    global rrd_problems_found
    global monitorAggregatorConfig
    dir=monitorAggregatorConfig.monitor_dir
    total_dir=os.path.join(dir,"total")

    status_dict={}
    status_total_dict={}
    for tp in frontend_status_attributes.keys():
        if tp in frontend_total_type_strings.keys():
            tp_str=frontend_total_type_strings[tp]
            attributes_tp=frontend_status_attributes[tp]
            for a in attributes_tp:
                status_total_dict["%s%s"%(tp_str,a)]=None
        if tp in frontend_job_type_strings.keys():
            tp_str=frontend_job_type_strings[tp]
            attributes_tp=frontend_status_attributes[tp]
            for a in attributes_tp:
                status_dict["%s%s"%(tp_str,a)]=None

    if not os.path.isdir(dir):
        print "WARNING: monitor/ directory does not exist, skipping rrd verification."
        return True
    for filename in os.listdir(dir):
        if (filename[:6]=="group_") or (filename=="total"):
            current_dir=os.path.join(dir,filename)
            if filename=="total":
                verifyHelper(os.path.join(current_dir,
                    "Status_Attributes.rrd"),status_total_dict, fix_rrd)
            for dirname in os.listdir(current_dir):
                current_subdir=os.path.join(current_dir,dirname)
                if dirname[:6]=="state_":
                    verifyHelper(os.path.join(current_subdir,
                        "Status_Attributes.rrd"),status_dict, fix_rrd)
                if dirname[:8]=="factory_":
                    verifyHelper(os.path.join(current_subdir,
                        "Status_Attributes.rrd"),status_dict, fix_rrd)
                if dirname=="total":
                    verifyHelper(os.path.join(current_subdir,
                        "Status_Attributes.rrd"),status_total_dict, fix_rrd)
    return not rrd_problems_found

####################################
# PRIVATE - Used by aggregateStatus
# Write one RRD
def write_one_rrd(name,updated,data,fact=0):
    if fact==0:
        type_strings=frontend_total_type_strings
    else:
        type_strings=frontend_job_type_strings

    # initialize the RRD dictionary, so it gets created properly
    val_dict={}
    for tp in frontend_status_attributes.keys():
        if tp in type_strings.keys():
            tp_str=type_strings[tp]
            attributes_tp=frontend_status_attributes[tp]
            for a in attributes_tp:
                val_dict["%s%s"%(tp_str,a)]=None

    for tp in data.keys():
        # type - status or requested
        if not (tp in frontend_status_attributes.keys()):
            continue
        if not (tp in type_strings.keys()):
            continue

        tp_str=type_strings[tp]
        attributes_tp=frontend_status_attributes[tp]

        tp_el=data[tp]

        for a in tp_el.keys():
            if a in attributes_tp:
                a_el=int(tp_el[a])
                if type(a_el)!=type({}): # ignore subdictionaries
                    val_dict["%s%s"%(tp_str,a)]=a_el

    glideinFrontendMonitoring.monitoringConfig.establish_dir("%s"%name)
    glideinFrontendMonitoring.monitoringConfig.write_rrd_multi("%s"%name,
                                                               "GAUGE",updated,val_dict)

##############################################################################
# create an aggregate of status files, write it in an aggregate status file
# end return the values
def aggregateStatus():
    global monitorAggregatorConfig

    type_strings={'Jobs':'Jobs','Glideins':'Glidein','MatchedJobs':'MatchJob',
                  'MatchedGlideins':'MatchGlidein','Requested':'Req'}
    global_total={'Jobs':None,'Glideins':None,'MatchedJobs':None,'Requested':None,'MatchedGlideins':None}
    status={'groups':{},'total':global_total}
    global_fact_totals={}

    for fos in ('factories','states'):
        global_fact_totals[fos]={}

    nr_groups=0
    for group in monitorAggregatorConfig.groups:
        # load group status file
        status_fname=os.path.join(os.path.join(monitorAggregatorConfig.monitor_dir,'group_'+group),
                                  monitorAggregatorConfig.status_relname)
        try:
            group_data=xmlParse.xmlfile2dict(status_fname)
        except IOError:
            continue # file not found, ignore

        # update group
        status['groups'][group]={}
        for fos in ('factories','states'):
            try:
                status['groups'][group][fos]=group_data[fos]
            except KeyError, e:
                # first time after upgrade factories may not be defined
                status['groups'][group][fos]={}

        this_group=status['groups'][group]
        for fos in ('factories','states'):
          for fact in this_group[fos].keys():
            this_fact=this_group[fos][fact]
            if not fact in global_fact_totals[fos].keys():
                # first iteration through, set fact totals equal to the first group's fact totals
                global_fact_totals[fos][fact]={}
                for attribute in type_strings.keys():
                    global_fact_totals[fos][fact][attribute]={}
                    if attribute in this_fact.keys():
                        for type_attribute in this_fact[attribute].keys():
                            this_type_attribute=this_fact[attribute][type_attribute]
                            try:
                                global_fact_totals[fos][fact][attribute][type_attribute]=int(this_type_attribute)
                            except:
                                pass
            else:
                # next iterations, factory already present in global fact totals, add the new factory values to the previous ones
                for attribute in type_strings.keys():
                    if attribute in this_fact.keys():
                        for type_attribute in this_fact[attribute].keys():
                            this_type_attribute=this_fact[attribute][type_attribute]
                            if type(this_type_attribute)==type(global_fact_totals[fos]):
                                # dict, do nothing
                                pass
                            else:
                                if attribute in global_fact_totals[fos][fact].keys() and type_attribute in global_fact_totals[fos][fact][attribute].keys():
                                   global_fact_totals[fos][fact][attribute][type_attribute]+=int(this_type_attribute)
                                else:
                                   global_fact_totals[fos][fact][attribute][type_attribute]=int(this_type_attribute)
        #nr_groups+=1
        #status['groups'][group]={}

        if group_data.has_key('total'):
            nr_groups += 1
            status['groups'][group]['total'] = group_data['total']

            for w in global_total.keys():
                tel = global_total[w]
                if not group_data['total'].has_key(w):
                    continue
                #status['groups'][group][w]=group_data[w]
                el = group_data['total'][w]
                if tel == None:
                    # new one, just copy over
                    global_total[w] = {}
                    tel = global_total[w]
                    for a in el.keys():
                        tel[a] = int(el[a]) #coming from XML, everything is a string
                else:
                    # successive, sum
                    for a in el.keys():
                        if tel.has_key(a):
                            tel[a] += int(el[a])

                    # if any attribute from prev. factories are not in the current one, remove from total
                    for a in tel.keys():
                        if not el.has_key(a):
                            del tel[a]

    for w in global_total.keys():
        if global_total[w]==None:
            del global_total[w] # remove group if not defined

    # Write xml files

    updated=time.time()
    xml_str=('<?xml version="1.0" encoding="ISO-8859-1"?>\n\n'+
             '<VOFrontendStats>\n'+
             xmlFormat.time2xml(updated, "updated", indent_tab=xmlFormat.DEFAULT_TAB,leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+
             xmlFormat.dict2string(status["groups"],dict_name="groups",el_name="group",
                                   subtypes_params={"class":{"dicts_params":{"factories":{"el_name":"factory",
                                                                                "subtypes_params":{"class":{"subclass_params":{"Requested":{"dicts_params":{"Parameters":{"el_name":"Parameter",
                                                                                "subtypes_params":{"class":{}}}}}}}}},
                                                                             "states":{"el_name":"state",
                                                                                "subtypes_params":{"class":{"subclass_params":{"Requested":{"dicts_params":{"Parameters":{"el_name":"Parameter",
                                                                                "subtypes_params":{"class":{}}}}}}}}}
                                                                             }}},
                                   leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+
             xmlFormat.class2string(status["total"],inst_name="total",leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+

             xmlFormat.dict2string(global_fact_totals['factories'],dict_name="factories",el_name="factory",
                                   subtypes_params={"class":{"subclass_params":{"Requested":{"dicts_params":{"Parameters":{"el_name":"Parameter",

       "subtypes_params":{"class":{}}}}}}}},
                                   leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+
             xmlFormat.dict2string(global_fact_totals['states'],dict_name="states",el_name="state",
                                   subtypes_params={"class":{"subclass_params":{"Requested":{"dicts_params":{"Parameters":{"el_name":"Parameter",

       "subtypes_params":{"class":{}}}}}}}},
                                   leading_tab=xmlFormat.DEFAULT_TAB)+"\n"+
             "</VOFrontendStats>\n")

    glideinFrontendMonitoring.monitoringConfig.write_file(monitorAggregatorConfig.status_relname,xml_str)

    # Write rrds

    glideinFrontendMonitoring.monitoringConfig.establish_dir("total")
    write_one_rrd("total/Status_Attributes",updated,global_total,0)

    for fact in global_fact_totals['factories'].keys():
        fe_dir="total/factory_%s"%glideinFrontendMonitoring.sanitize(fact)
        glideinFrontendMonitoring.monitoringConfig.establish_dir(fe_dir)
        write_one_rrd("%s/Status_Attributes"%fe_dir,updated,global_fact_totals['factories'][fact],1)
    for fact in global_fact_totals['states'].keys():
        fe_dir="total/state_%s"%glideinFrontendMonitoring.sanitize(fact)
        glideinFrontendMonitoring.monitoringConfig.establish_dir(fe_dir)
        write_one_rrd("%s/Status_Attributes"%fe_dir,updated,global_fact_totals['states'][fact],1)

    return status

<|endoftext|>


<commit_before>'''
Created on Jul 11, 2016

@author: slewis
'''
from logging import getLogger as getLibLogger
from threading import RLock

from py4j.java_gateway import JavaGateway, CallbackServerParameters
from py4j.java_collections import ListConverter, MapConverter
from servicebridge import merge_dicts, ENDPOINT_ID
import servicebridge
from argparse import ArgumentError

PY4J_EXPORTED_CONFIG = 'ecf.py4j.host.python'
PY4J_EXPORTED_CONFIGS = [PY4J_EXPORTED_CONFIG]
PY4J_PROTOCOL = 'py4j'
PY4J_DEFAULT_GATEWAY_PORT = 25333
PY4J_DEFAULT_CB_PORT = 25334
PY4J_DEFAULT_HOSTNAME = 'localhost'
PY4J_PYTHON_PATH = "/python"
PY4J_JAVA_PATH = "/java"

PY4J_NAMESPACE = 'ecf.namespace.py4j'
JAVA_DIRECT_ENDPOINT_CLASS = 'org.eclipse.ecf.provider.direct.DirectRemoteServiceProvider'
PY4J_SERVICE_INTENTS = ['passByReference', 'exactlyOnce', 'ordered']

# Version
__version_info__ = (0, 1, 0)
__version__ = ".".join(str(x) for x in __version_info__)

# Documentation strings format
__docformat__ = "restructuredtext en"

# ------------------------------------------------------------------------------

_logger = getLibLogger(__name__)

# ------------------------------------------------------------------------------

def make_edef_props(*args,**kwargs):
    dicts = [x for x in args if isinstance(x,type({}))]
    itemdict = {k: v for k, v in kwargs.items() if k not in dicts}
    dicts.append(itemdict)
    tpl = tuple(dicts)
    return merge_dicts(*tpl)

def createLocalPy4jId(hostname=PY4J_DEFAULT_HOSTNAME,port=PY4J_DEFAULT_CB_PORT):
    return PY4J_PROTOCOL + "://" + hostname + ":" + str(port) + PY4J_PYTHON_PATH

def createJavaPy4jId(hostname=PY4J_DEFAULT_HOSTNAME,port=PY4J_DEFAULT_GATEWAY_PORT):
    return PY4J_PROTOCOL + "://" + hostname + ":" + str(port) + PY4J_JAVA_PATH

class Py4jServiceBridgeEventListener(object):
    '''
    Listener adapter class.   Subclasses can set themselves up
    as a listener for Py4jBridge events (service_imported, service_modified,
    service_unimported see below), buy extending this class and overriding
    these methods, and then providing the listener to the Py4jBridge instance
    creation'''
    def service_imported(self,endpointid,endpoint):
        _logger.info('_service_imported endpointid='+endpointid)

    def service_modified(self,endpointid,endpoint):
        _logger.info('_service_modified endpointid='+endpointid)

    def service_unimported(self,endpointid,endpoint):
        _logger.info('_service_unimported endpointid='+endpointid)

class Py4jServiceBridge(object):
    '''Py4jServiceBridge class
    This class provides and API for consumers to use the Py4jServiceBridge.  This
    allows a bridge between Python and the OSGi service registry.
    '''
    def __init__(self,listener=None):
        self._gateway = None
        self._lock = RLock()
        self._consumer = None
        self._endpoints_lock = RLock()
        self._endpoints = {}
        self._exported_endpoints_lock = RLock()
        self._exported_endpoints = {}
        self._map_converter = MapConverter()
        self._list_converter = ListConverter()
        self._listener = listener

    def export(self,svc,export_props):
        with self._lock:
            self._raise_not_connected()
        try:
            endpointid = export_props[ENDPOINT_ID]
        except KeyError:
            raise ArgumentError('Cannot export service since no ENDPOINT_ID present in export_props')
        with self._exported_endpoints_lock:
            self._exported_endpoints[endpointid] = (svc,export_props)
        self.__export(svc,export_props)
        return endpointid

    def unexport(self,export_props):
        with self._lock:
            self._raise_not_connected()
        endpointid = None
        try:
            endpointid = export_props[ENDPOINT_ID]
        except KeyError:
            raise ArgumentError('Cannot export service since no ENDPOINT_ID present in export_props')
        if endpointid:
            return self.unexport_raw(endpointid)

    def export_raw(self,svc,object_class, rsvc_id, fw_id=None, pkg_ver=None):
        return self.export(svc, self.make_rsa_props(object_class, rsvc_id, fw_id, pkg_ver))

    def unexport_raw(self,endpointid):
        with self._lock:
            self._raise_not_connected()
        endpoint = self._remove_export_endpoint(endpointid)
        if endpoint:
            self.__unexport(endpoint[1])
            return endpoint
        else:
            return None

    def get_jvm(self):
        with self._lock:
            self._raise_not_connected();
        return self._gateway.jvm

    def get_import_endpoint(self,endpointid):
        with self._endpoints_lock:
            try:
                return self._endpoints[endpointid]
            except KeyError:
                return None

    def get_export_endpoint(self,endpointid):
        with self._exported_endpoints_lock:
            try:
                return self._exported_endpoints[endpointid]
            except KeyError:
                return None

    def isconnected(self):
        with self._lock:
            return True if self._gateway is not None else False

    def connect(self,callback_server_parameters=None):
        if not callback_server_parameters:
            callback_server_parameters = CallbackServerParameters()
        with self._lock:
            if not self._gateway is None:
                raise ConnectionError('already connected to java gateway')
            self._gateway = JavaGateway(callback_server_parameters=callback_server_parameters)
            self._consumer = self._gateway.entry_point.getJavaConsumer()
            class JavaRemoteServiceExporter(object):
                def __init__(self, bridge):
                    self._bridge = bridge

                def exportService(self,proxy,props):
                    self._bridge._import_service_from_java(proxy,props)

                def modifyService(self,props):
                    self._bridge._modify_service_from_java(props)

                def unexportService(self,props):
                    self._bridge._unimport_service_from_java(props)
                class Java:
                    implements = [JAVA_DIRECT_ENDPOINT_CLASS]

            self._bridge = JavaRemoteServiceExporter(self)
            self._gateway.entry_point.setPythonConsumer(self._bridge)

    def make_rsa_props(self,object_class, rsvc_id, fw_id, pkg_ver):
        osgiprops = servicebridge.get_rsa_props(object_class, PY4J_EXPORTED_CONFIGS, PY4J_SERVICE_INTENTS, rsvc_id, fw_id, pkg_ver)
        cbserver = self._gateway.get_callback_server()
        if cbserver:
            hostname = str(cbserver.get_listening_address())
            port = cbserver.get_listening_port()
            myid = createLocalPy4jId(hostname, port)
        else:
            myid = createLocalPy4jId()
        ecfprops = servicebridge.get_ecf_props(myid, PY4J_NAMESPACE, rsvc_id)
        return servicebridge.merge_dicts(osgiprops,ecfprops)

# Methods called by listener
    def _import_service_from_java(self,proxy,props):
        endpointid = None
        try:
            endpointid = props[ENDPOINT_ID]
        except KeyError:
            pass
        if endpointid:
            endpoint = (proxy,props)
            with self._endpoints_lock:
                self._endpoints[endpointid] = endpoint
        if self._listener and endpointid:
            try:
                self._listener.service_imported(endpointid,endpoint)
            except Exception as e:
                _logger.error('_import_service_from_java listener threw exception endpointid='+endpointid, e)

    def _modify_service_from_java(self,props):
        newendpoint = None
        try:
            endpointid = props[ENDPOINT_ID]
            with self._endpoints_lock:
                endpoint = self._endpoints[endpointid]
                props = endpoint[1]
                props.update(props)
                newendpoint = (endpoint[0],props)
                self._endpoints[endpointid] = newendpoint
        except KeyError:
            pass
        if self._listener and newendpoint:
            try:
                self._listener.service_modified(endpointid,newendpoint)
            except Exception as e:
                _logger.error('_modify_service_from_java listener threw exception endpointid='+endpointid, e)

    def _unimport_service_from_java(self,props):
        endpoint = None
        endpointid = None
        try:
            endpointid = props[ENDPOINT_ID]
            with self._endpoints_lock:
                endpoint = self._endpoints.pop(endpointid, None)
        except KeyError:
            pass
        if self._listener and endpoint:
            try:
                self._listener.service_unimported(endpointid,endpoint)
            except Exception as e:
                _logger.error('_unimport_service_from_java listener threw exception endpointid='+endpointid, e)

    def _remove_export_endpoint(self,endpointid):
        with self._exported_endpoints_lock:
            return self._exported_endpoints.pop(endpointid, None)

    def _convert_string_list(self,l):
        llength = len(l)
        r = self._gateway.new_array(self._gateway.jvm.java.lang.String,llength)
        for i in range(0,llength):
            r[i] = l[i]
        return r

    def _convert_props_for_java(self,props):
        with self._lock:
            self._raise_not_connected()
        result = {}
        for item in props.items():
            val = item[1]
            if isinstance(val, type([])):
                val = self._convert_string_list(val)
            result[item[0]] = val
        return self._map_converter.convert(result,self._gateway._gateway_client)

    def _raise_not_connected(self):
        if not self.isconnected():
            raise ConnectionError('Not connected to java gateway')

    def get_access(self):
        return createLocalPy4jId(port=self._gateway.get_callback_server().get_listening_port())

    def __export(self,svc,props):
        try:
            self._consumer.exportService(svc,self._convert_props_for_java(props))
        except Exception as e:
            _logger.error(e)
            raise e

    def __unexport(self,props):
        try:
            self._consumer.unexportService(self._convert_props_for_java(props))
        except Exception as e:
            _logger.error(e)
            raise e

    def disconnect(self):
        with self._lock:
            if self.isconnected():
                self._gateway.shutdown()
                self._gateway = None
                self._consumer = None
                self._bridge = None
        with self._endpoints_lock:
            self._endpoints.clear()
        with self._exported_endpoints_lock:
            self._exported_endpoints.clear()

<commit_msg>Improvements in layout for Py4jServiceBridge<commit_after>'''
Created on Jul 11, 2016

@author: slewis
'''
from logging import getLogger as getLibLogger
from threading import RLock

from py4j.java_gateway import JavaGateway, CallbackServerParameters
from py4j.java_collections import ListConverter, MapConverter
from servicebridge import merge_dicts, ENDPOINT_ID
import servicebridge
from argparse import ArgumentError

PY4J_EXPORTED_CONFIG = 'ecf.py4j.host.python'
PY4J_EXPORTED_CONFIGS = [PY4J_EXPORTED_CONFIG]
PY4J_PROTOCOL = 'py4j'
PY4J_DEFAULT_GATEWAY_PORT = 25333
PY4J_DEFAULT_CB_PORT = 25334
PY4J_DEFAULT_HOSTNAME = 'localhost'
PY4J_PYTHON_PATH = "/python"
PY4J_JAVA_PATH = "/java"

PY4J_NAMESPACE = 'ecf.namespace.py4j'
JAVA_DIRECT_ENDPOINT_CLASS = 'org.eclipse.ecf.provider.direct.DirectRemoteServiceProvider'
PY4J_SERVICE_INTENTS = ['passByReference', 'exactlyOnce', 'ordered']

# Version
__version_info__ = (0, 1, 0)
__version__ = ".".join(str(x) for x in __version_info__)

# Documentation strings format
__docformat__ = "restructuredtext en"

# ------------------------------------------------------------------------------

_logger = getLibLogger(__name__)

# ------------------------------------------------------------------------------

def make_edef_props(*args,**kwargs):
    dicts = [x for x in args if isinstance(x,type({}))]
    itemdict = {k: v for k, v in kwargs.items() if k not in dicts}
    dicts.append(itemdict)
    tpl = tuple(dicts)
    return merge_dicts(*tpl)

def createLocalPy4jId(hostname=PY4J_DEFAULT_HOSTNAME,port=PY4J_DEFAULT_CB_PORT):
    return PY4J_PROTOCOL + "://" + hostname + ":" + str(port) + PY4J_PYTHON_PATH

def createJavaPy4jId(hostname=PY4J_DEFAULT_HOSTNAME,port=PY4J_DEFAULT_GATEWAY_PORT):
    return PY4J_PROTOCOL + "://" + hostname + ":" + str(port) + PY4J_JAVA_PATH

class Py4jServiceBridgeEventListener(object):
    '''
    Listener adapter class.   Subclasses can set themselves up
    as a listener for Py4jBridge events (service_imported, service_modified,
    service_unimported see below), buy extending this class and overriding
    these methods, and then providing the listener to the Py4jBridge instance
    creation'''
    def service_imported(self,endpointid,endpoint):
        _logger.info('_service_imported endpointid='+endpointid)

    def service_modified(self,endpointid,endpoint):
        _logger.info('_service_modified endpointid='+endpointid)

    def service_unimported(self,endpointid,endpoint):
        _logger.info('_service_unimported endpointid='+endpointid)

class Py4jServiceBridge(object):
    '''Py4jServiceBridge class
    This class provides and API for consumers to use the Py4jServiceBridge.  This
    allows a bridge between Python and the OSGi service registry.
    '''
    def __init__(self,listener=None):
        self._gateway = None
        self._lock = RLock()
        self._consumer = None
        self._endpoints_lock = RLock()
        self._endpoints = {}
        self._exported_endpoints_lock = RLock()
        self._exported_endpoints = {}
        self._map_converter = MapConverter()
        self._list_converter = ListConverter()
        self._listener = listener

    def export(self,svc,export_props):
        with self._lock:
            self._raise_not_connected()
        try:
            endpointid = export_props[ENDPOINT_ID]
        except KeyError:
            raise ArgumentError('Cannot export service since no ENDPOINT_ID present in export_props')
        with self._exported_endpoints_lock:
            self._exported_endpoints[endpointid] = (svc,export_props)
        self.__export(svc,export_props)
        return endpointid

    def unexport(self,export_props):
        with self._lock:
            self._raise_not_connected()
        endpointid = None
        try:
            endpointid = export_props[ENDPOINT_ID]
        except KeyError:
            raise ArgumentError('Cannot export service since no ENDPOINT_ID present in export_props')
        if endpointid:
            return self.unexport_raw(endpointid)

    def export_raw(self,svc,object_class, rsvc_id, fw_id=None, pkg_ver=None):
        return self.export(svc, self.make_rsa_props(object_class, rsvc_id, fw_id, pkg_ver))

    def unexport_raw(self,endpointid):
        with self._lock:
            self._raise_not_connected()
        endpoint = self._remove_export_endpoint(endpointid)
        if endpoint:
            self.__unexport(endpoint[1])
            return endpoint
        else:
            return None

    def get_jvm(self):
        with self._lock:
            self._raise_not_connected();
        return self._gateway.jvm

    def get_import_endpoint(self,endpointid):
        with self._endpoints_lock:
            try:
                return self._endpoints[endpointid]
            except KeyError:
                return None

    def get_export_endpoint(self,endpointid):
        with self._exported_endpoints_lock:
            try:
                return self._exported_endpoints[endpointid]
            except KeyError:
                return None

    def isconnected(self):
        with self._lock:
            return True if self._gateway is not None else False

    def connect(self,callback_server_parameters=None):
        if not callback_server_parameters:
            callback_server_parameters = CallbackServerParameters()
        with self._lock:
            if not self._gateway is None:
                raise ConnectionError('already connected to java gateway')
            self._gateway = JavaGateway(callback_server_parameters=callback_server_parameters)
            self._consumer = self._gateway.entry_point.getJavaConsumer()
            class JavaRemoteServiceExporter(object):
                def __init__(self, bridge):
                    self._bridge = bridge

                def exportService(self,proxy,props):
                    self._bridge._import_service_from_java(proxy,props)

                def modifyService(self,props):
                    self._bridge._modify_service_from_java(props)

                def unexportService(self,props):
                    self._bridge._unimport_service_from_java(props)
                class Java:
                    implements = [JAVA_DIRECT_ENDPOINT_CLASS]

            self._bridge = JavaRemoteServiceExporter(self)
            self._gateway.entry_point.setPythonConsumer(self._bridge)

    def disconnect(self):
        with self._lock:
            if self.isconnected():
                self._gateway.shutdown()
                self._gateway = None
                self._consumer = None
                self._bridge = None
        with self._endpoints_lock:
            self._endpoints.clear()
        with self._exported_endpoints_lock:
            self._exported_endpoints.clear()

    def make_rsa_props(self,object_class, rsvc_id, fw_id, pkg_ver):
        osgiprops = servicebridge.get_rsa_props(object_class, PY4J_EXPORTED_CONFIGS, PY4J_SERVICE_INTENTS, rsvc_id, fw_id, pkg_ver)
        cbserver = self._gateway.get_callback_server()
        if cbserver:
            hostname = str(cbserver.get_listening_address())
            port = cbserver.get_listening_port()
            myid = createLocalPy4jId(hostname, port)
        else:
            myid = createLocalPy4jId()
        ecfprops = servicebridge.get_ecf_props(myid, PY4J_NAMESPACE, rsvc_id)
        return servicebridge.merge_dicts(osgiprops,ecfprops)

# Methods called by java
    def _import_service_from_java(self,proxy,props):
        endpointid = None
        try:
            endpointid = props[ENDPOINT_ID]
        except KeyError:
            pass
        if endpointid:
            endpoint = (proxy,props)
            with self._endpoints_lock:
                self._endpoints[endpointid] = endpoint
        if self._listener and endpointid:
            try:
                self._listener.service_imported(endpointid,endpoint)
            except Exception as e:
                _logger.error('_import_service_from_java listener threw exception endpointid='+endpointid, e)

    def _modify_service_from_java(self,props):
        newendpoint = None
        try:
            endpointid = props[ENDPOINT_ID]
            with self._endpoints_lock:
                endpoint = self._endpoints[endpointid]
                props = endpoint[1]
                props.update(props)
                newendpoint = (endpoint[0],props)
                self._endpoints[endpointid] = newendpoint
        except KeyError:
            pass
        if self._listener and newendpoint:
            try:
                self._listener.service_modified(endpointid,newendpoint)
            except Exception as e:
                _logger.error('_modify_service_from_java listener threw exception endpointid='+endpointid, e)

    def _unimport_service_from_java(self,props):
        endpoint = None
        endpointid = None
        try:
            endpointid = props[ENDPOINT_ID]
            with self._endpoints_lock:
                endpoint = self._endpoints.pop(endpointid, None)
        except KeyError:
            pass
        if self._listener and endpoint:
            try:
                self._listener.service_unimported(endpointid,endpoint)
            except Exception as e:
                _logger.error('_unimport_service_from_java listener threw exception endpointid='+endpointid, e)

    def _remove_export_endpoint(self,endpointid):
        with self._exported_endpoints_lock:
            return self._exported_endpoints.pop(endpointid, None)

    def _convert_string_list(self,l):
        llength = len(l)
        r = self._gateway.new_array(self._gateway.jvm.java.lang.String,llength)
        for i in range(0,llength):
            r[i] = l[i]
        return r

    def _convert_props_for_java(self,props):
        with self._lock:
            self._raise_not_connected()
        result = {}
        for item in props.items():
            val = item[1]
            if isinstance(val, type([])):
                val = self._convert_string_list(val)
            result[item[0]] = val
        return self._map_converter.convert(result,self._gateway._gateway_client)

    def _raise_not_connected(self):
        if not self.isconnected():
            raise ConnectionError('Not connected to java gateway')

    def get_access(self):
        return createLocalPy4jId(port=self._gateway.get_callback_server().get_listening_port())

    def __export(self,svc,props):
        try:
            self._consumer.exportService(svc,self._convert_props_for_java(props))
        except Exception as e:
            _logger.error(e)
            raise e

    def __unexport(self,props):
        try:
            self._consumer.unexportService(self._convert_props_for_java(props))
        except Exception as e:
            _logger.error(e)
            raise e

<|endoftext|>


<commit_before>import pickle
import pytest
import subprocess
import time

requests = pytest.importorskip("requests")
from fsspec.implementations.webhdfs import WebHDFS
import fsspec

@pytest.fixture(scope='module')
def hdfs_cluster():
    cmd0 = "htcluster shutdown".split()
    try:
        subprocess.check_output(cmd0)
    except FileNotFoundError:
        pytest.skip("htcluster not found")
    cmd1 = "htcluster startup --image base".split()
    subprocess.check_output(cmd1)
    try:
        while True:
            t = 90
            try:
                requests.get('http://localhost:50070/webhdfs/v1/?op=LISTSTATUS')
            except:
                t -= 1
                assert t > 0, "Timeout waiting for HDFS"
                time.sleep(1)
                continue
            break
        time.sleep(7)
        yield "localhost"
    finally:
        subprocess.check_output(cmd0)

def test_pickle(hdfs_cluster):
    w = WebHDFS(hdfs_cluster, user='testuser')
    w2 = pickle.loads(pickle.dumps(w))
    assert w == w2

def test_simple(hdfs_cluster):
    w = WebHDFS(hdfs_cluster, user='testuser')
    home = w.home_directory()
    assert home == '/user/testuser'
    with pytest.raises(PermissionError):
        w.mkdir('/root')

def test_url(hdfs_cluster):
    url = 'webhdfs://testuser@localhost:50070/user/testuser/myfile'
    fo = fsspec.open(url, 'wb', data_proxy={'worker.example.com': 'localhost'})
    with fo as f:
        f.write(b'hello')
    fo = fsspec.open(url, 'rb', data_proxy={'worker.example.com': 'localhost'})
    with fo as f:
        assert f.read() == b'hello'

def test_workflow(hdfs_cluster):
    w = WebHDFS(hdfs_cluster, user='testuser',
                data_proxy={'worker.example.com': 'localhost'})
    fn = '/user/testuser/testrun/afile'
    w.mkdir('/user/testuser/testrun')
    with w.open(fn, 'wb') as f:
        f.write(b'hello')
    assert w.exists(fn)
    info = w.info(fn)
    assert info['size'] == 5
    assert w.isfile(fn)
    assert w.cat(fn) == b'hello'
    w.rm('/user/testuser/testrun', recursive=True)
    assert not w.exists(fn)

def test_workflow_transaction(hdfs_cluster):
    w = WebHDFS(hdfs_cluster, user='testuser',
                data_proxy={'worker.example.com': 'localhost'})
    fn = '/user/testuser/testrun/afile'
    w.mkdirs('/user/testuser/testrun')
    with w.transaction:
        with w.open(fn, 'wb') as f:
            f.write(b'hello')
        assert not w.exists(fn)
    assert w.exists(fn)
    assert w.ukey(fn)
    files = w.ls('/user/testuser/testrun', True)
    summ = w.content_summary('/user/testuser/testrun')
    assert summ['length'] == files[0]['size']
    assert summ['fileCount'] == 1

    w.rm('/user/testuser/testrun', recursive=True)
    assert not w.exists(fn)

<commit_msg>Add gzip test to webhdfs<commit_after>import pickle
import pytest
import subprocess
import time

requests = pytest.importorskip("requests")
from fsspec.implementations.webhdfs import WebHDFS
import fsspec

@pytest.fixture(scope='module')
def hdfs_cluster():
    cmd0 = "htcluster shutdown".split()
    try:
        subprocess.check_output(cmd0)
    except FileNotFoundError:
        pytest.skip("htcluster not found")
    cmd1 = "htcluster startup --image base".split()
    subprocess.check_output(cmd1)
    try:
        while True:
            t = 90
            try:
                requests.get('http://localhost:50070/webhdfs/v1/?op=LISTSTATUS')
            except:
                t -= 1
                assert t > 0, "Timeout waiting for HDFS"
                time.sleep(1)
                continue
            break
        time.sleep(7)
        yield "localhost"
    finally:
        subprocess.check_output(cmd0)

def test_pickle(hdfs_cluster):
    w = WebHDFS(hdfs_cluster, user='testuser')
    w2 = pickle.loads(pickle.dumps(w))
    assert w == w2

def test_simple(hdfs_cluster):
    w = WebHDFS(hdfs_cluster, user='testuser')
    home = w.home_directory()
    assert home == '/user/testuser'
    with pytest.raises(PermissionError):
        w.mkdir('/root')

def test_url(hdfs_cluster):
    url = 'webhdfs://testuser@localhost:50070/user/testuser/myfile'
    fo = fsspec.open(url, 'wb', data_proxy={'worker.example.com': 'localhost'})
    with fo as f:
        f.write(b'hello')
    fo = fsspec.open(url, 'rb', data_proxy={'worker.example.com': 'localhost'})
    with fo as f:
        assert f.read() == b'hello'

def test_workflow(hdfs_cluster):
    w = WebHDFS(hdfs_cluster, user='testuser',
                data_proxy={'worker.example.com': 'localhost'})
    fn = '/user/testuser/testrun/afile'
    w.mkdir('/user/testuser/testrun')
    with w.open(fn, 'wb') as f:
        f.write(b'hello')
    assert w.exists(fn)
    info = w.info(fn)
    assert info['size'] == 5
    assert w.isfile(fn)
    assert w.cat(fn) == b'hello'
    w.rm('/user/testuser/testrun', recursive=True)
    assert not w.exists(fn)

def test_with_gzip(hdfs_cluster):
    from gzip import GzipFile
    w = WebHDFS(hdfs_cluster, user='testuser',
                data_proxy={'worker.example.com': 'localhost'})
    fn = '/user/testuser/gzfile'
    with w.open(fn, 'wb') as f:
        gf = GzipFile(fileobj=f, mode='w')
        gf.write(b'hello')
        gf.close()
    with w.open(fn, 'rb') as f:
        gf = GzipFile(fileobj=f, mode='r')
        assert gf.read() == b'hello'

def test_workflow_transaction(hdfs_cluster):
    w = WebHDFS(hdfs_cluster, user='testuser',
                data_proxy={'worker.example.com': 'localhost'})
    fn = '/user/testuser/testrun/afile'
    w.mkdirs('/user/testuser/testrun')
    with w.transaction:
        with w.open(fn, 'wb') as f:
            f.write(b'hello')
        assert not w.exists(fn)
    assert w.exists(fn)
    assert w.ukey(fn)
    files = w.ls('/user/testuser/testrun', True)
    summ = w.content_summary('/user/testuser/testrun')
    assert summ['length'] == files[0]['size']
    assert summ['fileCount'] == 1

    w.rm('/user/testuser/testrun', recursive=True)
    assert not w.exists(fn)

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Simple VTK Viewer.

Example:

$ viewer.py -f head.vtk
"""
from optparse import OptionParser
import sys
import vessel_cut

from PyQt4 import QtCore, QtGui
from PyQt4 import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *

from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtk import *
from Tkinter import *
plane = vtk.vtkPlane()

class QVTKViewer(QDialog):
    """
    Simple VTK Viewer.
    """

    planew = None

    def __init__(self, vtk_filename):

        def callback(button):
            print button

        def Cutter(obj, event):
            global plane, selectActor
            obj.GetPlane(plane)

        def Plane():
            planeWidget = vtk.vtkImplicitPlaneWidget()
            planeWidget.SetInteractor(iren)
            planeWidget.SetPlaceFactor(1.5)
            planeWidget.SetInput(surface.GetOutput())
            planeWidget.PlaceWidget()
            planeWidget.TubingOff()
            planeWidget.OutsideBoundsOff()
            planeWidget.ScaleEnabledOff()
            planeWidget.OutlineTranslationOff()
            planeWidget.AddObserver("InteractionEvent", Cutter)
            print(planeWidget.GetNormal())
            print(planeWidget.GetOrigin())
            planeWidget.On()
            window.setLayout(grid)

            self.planew = planeWidget

            window.show()
            iren.Initialize()
            renWin.Render()
            iren.Start()

        def liver_view():
            print('Zobrazuji liver')
            vessel_cut.prog('liver')

        def vein_view():
            print('Zobrazuji vein')
            vessel_cut.prog('porta')

        def liver_cut():
            print(self.planew.GetNormal())
            print(self.planew.GetOrigin())

        """
        Initiate Viwer

        Parameters
        ----------
        vtk_filename : str
            Input VTK filename
        """

        # Renderer and InteractionStyle
        ren = vtk.vtkRenderer()
        renWin = vtk.vtkRenderWindow()
        renWin.AddRenderer(ren)

        iren = vtk.vtkRenderWindowInteractor()
        iren.SetRenderWindow(renWin)
        iren.SetInteractorStyle(MyInteractorStyle())

        # VTK file
        reader = vtk.vtkUnstructuredGridReader()
        reader.SetFileName(vtk_filename)
        reader.Update()

        # VTK surface
        surface=vtk.vtkDataSetSurfaceFilter()
        surface.SetInput(reader.GetOutput())
        surface.Update()

        # Cutter
        clipper = vtk.vtkClipPolyData()
        clipper.SetInput(surface.GetOutput())
        clipper.SetClipFunction(plane)
        #clipper.GenerateClipScalarsOn()
        clipper.GenerateClippedOutputOn()

        clipMapper = vtk.vtkPolyDataMapper()
        clipMapper.SetInput(clipper.GetOutput())

        backProp = vtk.vtkProperty()

        clipActor = vtk.vtkActor()
        clipActor.SetMapper(clipMapper)
        clipActor.GetProperty().SetColor(1,0,0)
        clipActor.SetBackfaceProperty(backProp)

        cutEdges = vtk.vtkCutter()
        cutEdges.SetInput(surface.GetOutput())
        cutEdges.SetCutFunction(plane)
        cutEdges.GenerateCutScalarsOn()

        cutStrips = vtk.vtkStripper()
        cutStrips.SetInput(cutEdges.GetOutput())
        cutStrips.Update()

        cutPoly = vtk.vtkPolyData()
        cutPoly.SetPoints(cutStrips.GetOutput().GetPoints())
        cutPoly.SetPolys(cutStrips.GetOutput().GetLines())

        cutTriangles = vtk.vtkTriangleFilter()
        cutTriangles.SetInput(cutPoly)

        cutMapper = vtk.vtkPolyDataMapper()
        cutMapper.SetInput(cutTriangles.GetOutput())

        cutActor = vtk.vtkActor()
        cutActor.SetMapper(cutMapper)
        cutActor.GetProperty().SetColor((1,0,0))

        mapper = vtk.vtkDataSetMapper()
        mapper.SetInput(clipper.GetClippedOutput())
        mapper.ScalarVisibilityOff()

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().EdgeVisibilityOn()
        #sirka linek u objektu
        actor.GetProperty().SetLineWidth(0.1)
        actor.GetProperty().SetRepresentationToWireframe()
        ren.AddActor(clipActor)
        ren.AddActor(cutActor)
        ren.AddActor(actor)

        window = QtGui.QWidget()
        grid = QtGui.QGridLayout()

        # Button liver
        button_liver = QtGui.QPushButton()
        button_liver.setText(unicode('liver'))
        grid.addWidget(button_liver, 1, 0)
        window.connect(button_liver, QtCore.SIGNAL("clicked()"),(lambda y:lambda: callback(y) )('Stisknuto : liver'))
        button_liver.clicked.connect(liver_view)
        button_liver.show()

        # Button vein
        button_vein = QtGui.QPushButton()
        button_vein.setText(unicode('vein'))
        grid.addWidget(button_vein, 2, 0)
        window.connect(button_vein, QtCore.SIGNAL("clicked()"),(lambda y:lambda: callback(y) )('Stisknuto : vein'))
        button_liver.clicked.connect(vein_view)
        button_vein.show()

        # Button plane
        button_plane = QtGui.QPushButton()
        button_plane.setText(unicode('plane'))
        grid.addWidget(button_plane, 3, 0)
        window.connect(button_plane, QtCore.SIGNAL("clicked()"),(lambda y:lambda: callback(y) )('Stisknuto : plane'))
        button_plane.clicked.connect(Plane)
        button_plane.show()

        # Button cut
        button_cut = QtGui.QPushButton()
        button_cut.setText(unicode('cut'))
        grid.addWidget(button_cut, 4, 0)
        window.connect(button_cut, QtCore.SIGNAL("clicked()"),(lambda y:lambda: callback(y) )('Stisknuto : liver'))
        button_cut.clicked.connect(liver_cut)
        button_cut.show()

        window.setLayout(grid)
        window.show()

        # set interaction and Interaction style
        iren.Initialize()
        renWin.Render()
        iren.Start()

class MyInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
        def __init__(self,parent=None):
            self.AddObserver("LeftButtonPressEvent",self.LeftButtonPressEvent)
            self.AddObserver("RightButtonPressEvent",self.RightButtonPressEvent)

        def LeftButtonPressEvent(self,obj,event):
            print "Left Button pressed"
            self.Rotate()
            self.OnLeftButtonDown()

        def RightButtonPressEvent(self,obj,event):
            print "Right Button pressed"
            self.Pan()
            self.OnRightButtonDown()

        e = '%prog [options]\n' + __doc__.rstrip()
help = {
    'in_file': 'input VTK file with unstructured mesh',
}

def main():
    parser = OptionParser(description='Simple VTK Viewer')
    parser.add_option('-f','--filename', action='store',
                      dest='in_filename', default=None,
                      help=help['in_file'])
    (options, args) = parser.parse_args()

    if options.in_filename is None:
        raise IOError('No VTK data!')

    app = QApplication(sys.argv)
    viewer = QVTKViewer(options.in_filename)
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

<commit_msg>return normal and coordinates of plane widget<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Simple VTK Viewer.

Example:

$ viewer.py -f head.vtk
"""
from optparse import OptionParser
import sys
import vessel_cut

from PyQt4 import QtCore, QtGui
from PyQt4 import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *

from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtk import *
from Tkinter import *
plane = vtk.vtkPlane()
normal = None
coordinates = None
planew = None

class normal_and_coordinates():

    def set_normal(self):
        return normal

    def set_coordinates(self):
        return coordinates

class QVTKViewer(QDialog):
    """
    Simple VTK Viewer.
    """

    def __init__(self, vtk_filename):

        def callback(button):
            print button

        def Cutter(obj, event):
            global plane, selectActor
            obj.GetPlane(plane)

        def Plane():
            planeWidget = vtk.vtkImplicitPlaneWidget()
            planeWidget.SetInteractor(iren)
            planeWidget.SetPlaceFactor(1.5)
            planeWidget.SetInput(surface.GetOutput())
            planeWidget.PlaceWidget()
            planeWidget.TubingOff()
            planeWidget.OutsideBoundsOff()
            planeWidget.ScaleEnabledOff()
            planeWidget.OutlineTranslationOff()
            planeWidget.AddObserver("InteractionEvent", Cutter)

            planeWidget.On()
            window.setLayout(grid)

            self.planew = planeWidget

            window.show()
            iren.Initialize()
            renWin.Render()
            iren.Start()

        def liver_view():
            print('Zobrazuji liver')
            vessel_cut.View('liver')

        def vein_view():
            print('Zobrazuji vein')
            vessel_cut.View('porta')

        def liver_cut():
            global normal
            global coordinates
            normal = self.planew.GetNormal()
            coordinates = self.planew.GetOrigin()
            print(normal)
            print(coordinates)

        """
        Initiate Viwer

        Parameters
        ----------
        vtk_filename : str
            Input VTK filename
        """

        # Renderer and InteractionStyle
        ren = vtk.vtkRenderer()
        renWin = vtk.vtkRenderWindow()
        renWin.AddRenderer(ren)

        iren = vtk.vtkRenderWindowInteractor()
        iren.SetRenderWindow(renWin)
        iren.SetInteractorStyle(MyInteractorStyle())

        # VTK file
        reader = vtk.vtkUnstructuredGridReader()
        reader.SetFileName(vtk_filename)
        reader.Update()

        # VTK surface
        surface=vtk.vtkDataSetSurfaceFilter()
        surface.SetInput(reader.GetOutput())
        surface.Update()

        # Cutter
        clipper = vtk.vtkClipPolyData()
        clipper.SetInput(surface.GetOutput())
        clipper.SetClipFunction(plane)
        #clipper.GenerateClipScalarsOn()
        clipper.GenerateClippedOutputOn()

        clipMapper = vtk.vtkPolyDataMapper()
        clipMapper.SetInput(clipper.GetOutput())

        backProp = vtk.vtkProperty()

        clipActor = vtk.vtkActor()
        clipActor.SetMapper(clipMapper)
        clipActor.GetProperty().SetColor(1,0,0)
        clipActor.SetBackfaceProperty(backProp)

        cutEdges = vtk.vtkCutter()
        cutEdges.SetInput(surface.GetOutput())
        cutEdges.SetCutFunction(plane)
        cutEdges.GenerateCutScalarsOn()

        cutStrips = vtk.vtkStripper()
        cutStrips.SetInput(cutEdges.GetOutput())
        cutStrips.Update()

        cutPoly = vtk.vtkPolyData()
        cutPoly.SetPoints(cutStrips.GetOutput().GetPoints())
        cutPoly.SetPolys(cutStrips.GetOutput().GetLines())

        cutTriangles = vtk.vtkTriangleFilter()
        cutTriangles.SetInput(cutPoly)

        cutMapper = vtk.vtkPolyDataMapper()
        cutMapper.SetInput(cutTriangles.GetOutput())

        cutActor = vtk.vtkActor()
        cutActor.SetMapper(cutMapper)
        cutActor.GetProperty().SetColor((1,0,0))

        mapper = vtk.vtkDataSetMapper()
        mapper.SetInput(clipper.GetClippedOutput())
        mapper.ScalarVisibilityOff()

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().EdgeVisibilityOn()
        #sirka linek u objektu
        actor.GetProperty().SetLineWidth(0.1)
        actor.GetProperty().SetRepresentationToWireframe()
        ren.AddActor(clipActor)
        ren.AddActor(cutActor)
        ren.AddActor(actor)

        window = QtGui.QWidget()
        grid = QtGui.QGridLayout()

        # Button liver
        button_liver = QtGui.QPushButton()
        button_liver.setText(unicode('liver'))
        grid.addWidget(button_liver, 1, 0)
        window.connect(button_liver, QtCore.SIGNAL("clicked()"),(lambda y:lambda: callback(y) )('Stisknuto : liver'))
        button_liver.clicked.connect(liver_view)
        button_liver.show()

        # Button vein
        button_vein = QtGui.QPushButton()
        button_vein.setText(unicode('vein'))
        grid.addWidget(button_vein, 2, 0)
        window.connect(button_vein, QtCore.SIGNAL("clicked()"),(lambda y:lambda: callback(y) )('Stisknuto : vein'))
        button_liver.clicked.connect(vein_view)
        button_vein.show()

        # Button plane
        button_plane = QtGui.QPushButton()
        button_plane.setText(unicode('plane'))
        grid.addWidget(button_plane, 3, 0)
        window.connect(button_plane, QtCore.SIGNAL("clicked()"),(lambda y:lambda: callback(y) )('Stisknuto : plane'))
        button_plane.clicked.connect(Plane)
        button_plane.show()

        # Button cut
        button_cut = QtGui.QPushButton()
        button_cut.setText(unicode('cut'))
        grid.addWidget(button_cut, 4, 0)
        window.connect(button_cut, QtCore.SIGNAL("clicked()"),(lambda y:lambda: callback(y) )('Stisknuto : cut'))
        button_cut.clicked.connect(liver_cut)
        button_cut.show()

        window.setLayout(grid)
        window.show()

        # set interaction and Interaction style
        iren.Initialize()
        renWin.Render()
        iren.Start()

class MyInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
        def __init__(self,parent=None):
            self.AddObserver("LeftButtonPressEvent",self.LeftButtonPressEvent)
            self.AddObserver("RightButtonPressEvent",self.RightButtonPressEvent)

        def LeftButtonPressEvent(self,obj,event):
            print "Left Button pressed"
            self.Rotate()
            self.OnLeftButtonDown()

        def RightButtonPressEvent(self,obj,event):
            print "Right Button pressed"
            self.Pan()
            self.OnRightButtonDown()

        e = '%prog [options]\n' + __doc__.rstrip()
help = {
    'in_file': 'input VTK file with unstructured mesh',
}

def main():
    parser = OptionParser(description='Simple VTK Viewer')
    parser.add_option('-f','--filename', action='store',
                      dest='in_filename', default=None,
                      help=help['in_file'])
    (options, args) = parser.parse_args()

    if options.in_filename is None:
        raise IOError('No VTK data!')

    app = QApplication(sys.argv)
    viewer = QVTKViewer(options.in_filename)
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

<|endoftext|>