[openrtm-commit:01455] r163 - branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI

openrtm @ openrtm.org openrtm @ openrtm.org
2014年 5月 30日 (金) 14:24:03 JST


Author: kawauchi
Date: 2014-05-30 14:24:03 +0900 (Fri, 30 May 2014)
New Revision: 163

Added:
   branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/rtutil.py
   branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/setup.py
Modified:
   branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/CMakeLists.txt
   branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/TkCalibGUIComp.py
   branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/tkcalibgui.py
Log:
Added execution environment check and modified to use rtutil instead of rtshell.

Modified: branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/CMakeLists.txt
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/CMakeLists.txt	2014-05-16 06:49:37 UTC (rev 162)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/CMakeLists.txt	2014-05-30 05:24:03 UTC (rev 163)
@@ -28,7 +28,7 @@
 )
 
 set(EXEC_FILES ""
-               TkCalibGUIComp.py tkcalibgui.py
+               TkCalibGUIComp.py tkcalibgui.py rtutil.py
 )
 
 set(OTHER_SRCS CMakeLists.txt
@@ -206,6 +206,7 @@
   SET (calib_python_file
             ${PROJECT_SOURCE_DIR}/TkCalibGUIComp.py 
             ${PROJECT_SOURCE_DIR}/tkcalibgui.py 
+            ${PROJECT_SOURCE_DIR}/rtutil.py 
             ${PROJECT_BINARY_DIR}/CalibrationService_idl.py) 
                
   foreach(target ${calib_python_file})
@@ -217,7 +218,6 @@
   endforeach()
 
   SET (calib_python_dir
-            idl 
             ImageCalibService 
             ImageCalibService__POA)
 

Modified: branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/TkCalibGUIComp.py
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/TkCalibGUIComp.py	2014-05-16 06:49:37 UTC (rev 162)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/TkCalibGUIComp.py	2014-05-30 05:24:03 UTC (rev 163)
@@ -7,8 +7,85 @@
  @brief Image Calibration GUI
  @date $Date$
 
+"""
+def check_openrtmpy():
+    try:
+        import OpenRTM_aist
+        print "OpenRTM python is installed."
+    except:
+        import tkMessageBox
+        import Tkinter
+        msg = u'OpenRTM-aist Python is not installed. '
+        msg += u'Please download it from \n'
+        msg += u'http://openrtm.org/openrtm/ja/content/tkcalibgui'
+        Tkinter.Tk().withdraw()
+        tkMessageBox.showerror(title = "OpenRTM not found",
+                  message = msg)
 
-"""
+def check_ttk():
+    try:
+        import ttk
+        print "Ttk is installed."
+    except:
+        import tkMessageBox
+        import Tkinter
+        msg = u'Ttk is not installed. '
+        msg += u'Please download it from \n'
+        msg += u'http://openrtm.org/openrtm/ja/content/tkcalibgui'
+        Tkinter.Tk().withdraw()
+        tkMessageBox.showerror(title = "Ttk not found",
+                  message = msg)
+
+def check_pil():
+    try:
+        from PIL import Image, ImageTk
+        print "Python Imaging Library (PIL) is installed."
+    except:
+        import tkMessageBox
+        import Tkinter
+        msg = u'Python Imaging Library (PIL) is not installed. '
+        msg += u'Please download it from \n'
+        msg += u'http://openrtm.org/openrtm/ja/content/tkcalibgui'
+        Tkinter.Tk().withdraw()
+        tkMessageBox.showerror(title = "PIL not found",
+                  message = msg)
+
+def check_numpy():
+    try:
+        import numpy
+        print "Numpy is installed."
+    except:
+        import tkMessageBox
+        import Tkinter
+        msg = u'Numpy is not installed. '
+        msg += u'Please download it from \n'
+        msg += u'http://openrtm.org/openrtm/ja/content/tkcalibgui'
+        Tkinter.Tk().withdraw()
+        tkMessageBox.showerror(title = "Numpy not found",
+                  message = msg)
+                  
+def check_rtctree():
+    try:
+        import rtctree
+        print "rtctree is installed."
+    except:
+        import tkMessageBox
+        import Tkinter
+        msg = u'rtctree is not installed. '
+        msg += u'Please download it from \n'
+        msg += u'http://openrtm.org/openrtm/ja/content/tkcalibgui'
+        Tkinter.Tk().withdraw()
+        tkMessageBox.showerror(title = "rtctree not found",
+                  message = msg)
+        return
+		
+# check execution condition
+check_openrtmpy()
+check_pil()
+check_ttk()
+check_numpy()
+check_rtctree()
+
 import os
 import sys
 import socket
@@ -24,8 +101,10 @@
 
 import tkcalibgui
 import CalibrationService_idl
-from rtshell import rtdel
 
+#from rtshell import rtdel
+import rtutil
+
 # Import Service implementation class
 # <rtc-template block="service_impl">
 
@@ -52,6 +131,7 @@
 		 ""]
 # </rtc-template>
 
+                                    
 ##
 # @class TkCalibGUI
 # @brief Image Calibration GUI
@@ -83,8 +163,8 @@
 		# <rtc-template block="init_conf_param">
 		
 		# </rtc-template>
+		self.tree=None
 
-
 		 
 	##
 	#
@@ -358,7 +438,11 @@
 	
 	calibgui.set_options(orb, rt_dir, comp_option)
 	calibgui.mainloop()
-
+	
+	# GUIの×ボタンを押されるとmainloopを抜けてここへ来る
+	# コンポーネントの終了処理を確認する
+	calibgui.exit_comp()
+	
 	while True:
 		comp_list = mgr.getComponents()
 		# is finished?
@@ -368,7 +452,9 @@
 		
 	# Delete context
 	tree = comp.get_tree()
-	rtdel.main([str(rt_dir)], tree)
+#	rtdel.main([str(rt_dir)], tree)
+	rtutil.delComponentRef(str(rt_dir), tree)
+
 	mgr.shutdown()
 
 if __name__ == "__main__":

Added: branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/rtutil.py
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/rtutil.py	                        (rev 0)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/rtutil.py	2014-05-30 05:24:03 UTC (rev 163)
@@ -0,0 +1,1074 @@
+#! python.exe
+
+import cgi
+import subprocess
+import sys
+import os
+import rtctree
+import rtctree.tree
+import json
+import time
+
+"""
+  Global variables
+"""
+__ORB__ = None
+demo_mode = False
+
+
+"""
+ Formart a component node for NSView
+"""
+def format_node(node, flag):
+  if flag==1 :
+    return """ "*%s" """ % node.name
+  elif flag==2 :
+    return """ "#%s" """ % node.name
+  else :
+    return """ "%s" """ % node.name
+
+"""
+ Fomat a context node forn NSView
+"""
+def format_context(node):
+  return  """ "folder": "%s" """ % node.name
+
+"""
+  Format NSView
+"""
+def format_nsview_json(tree, node):
+  result = ""
+  res = []
+  try:
+    chnode = node.children
+    count = 0
+    for x in chnode :
+      if count > 0:
+        result += ","
+      path, port = rtctree.path.parse_path(x.full_path_str)
+      res.append(x)
+      if tree.is_directory(path) :
+        val = format_context(x)
+        val2 = format_nsview_json(tree,x)
+        result += """{ %s, "components" : [ %s]}""" % (val, val2)
+      else:
+        flag = 0
+        if tree.is_zombie(path) : flag = 1
+        if tree.is_unknown(path) : flag = 2
+        val = format_node(x, flag)
+        result += val
+      count += 1
+  except:
+    pass
+
+  return result
+
+"""
+  Format NSView for demo.
+"""
+def format_dir_json(dirname):
+  res = []
+  filelist = os.listdir(dirname)
+  for f in filelist :
+    p = "%s\%s" % (dirname, f)
+    if os.path.isdir(p) :
+      val = format_dir_json(p)
+      res.append( """{ "folder" : "%s", "components": [%s] }""" % (f, val) )
+    else:
+      res.append( """ "%s" """ % f )
+  return ",".join(res)
+
+"""
+  Parse a data_type of a dataport
+"""
+def getDataType(tt):
+  val = tt.split(":")
+  if val[0] != 'IDL' or val[2] != '1.0':
+    return tt
+  return val[1].replace('/', '::')
+
+"""
+  Format port data of a component
+"""
+def format_port_info(port):
+  result = port.name
+
+  props = port.properties
+  porttype = props['port.port_type']
+
+  if porttype == 'DataOutPort' or porttype == 'DataInPort':
+    #datatype= getDataType(props['dataport.data_type'])
+    datatype= props['dataport.data_type']
+    result += """(%s;%s)""" % (porttype, datatype)
+  else:
+    info=[]
+    for x in port.interfaces :
+      info.append( "%s|%s|%d" % (x.instance_name , x.type_name,  x.polarity) )
+
+    info_str = "#".join(info)
+    result += """(%s;%s)""" % (porttype, info_str)
+
+  return result
+
+"""
+"""
+def find_composite_comp(tree, member, inst_name):
+  def get_fp(mgr, args):
+    for c in mgr.components:
+      if c.instance_name == inst_name:
+        return c.full_path_str
+    return None
+  def is_correct_mgr(node):
+    has_member = False
+    has_inst_name = False
+    for c in node.components:
+      if c.instance_name == inst_name:
+        has_inst_name = True
+      elif c.instance_name == member.instance_name:
+        has_member = True
+    return has_member and has_inst_name
+  return tree.iterate(get_fp, filter=['is_manager', is_correct_mgr])
+
+"""
+"""
+def get_composite_properties(tree, comp):
+  result=[]
+  for o in comp.organizations:
+    res={}
+    if not o.sdo_id:
+      sdo_id = "Unknown"
+    else:
+      sdo_id = o.sdo_id
+
+    res['Composition_ID'] = sdo_id 
+    res['ID'] = o.org_id 
+    for m in o.members:
+      c_path = find_composite_comp(tree, comp, m)
+      if c_path:
+        res['Member'] = c_path[0]
+      else:
+        res['Member'] = 'Unknown'
+    result.append(res)
+  return result
+
+"""
+"""
+def get_composite_member_properties(tree, comp):
+  result=[]
+  for po in comp.parent_orgs:
+    res={}
+    if not po.sdo_id:
+      sdo_id = "Unknown"
+    else:
+      sdo_id = po.sdo_id
+    res['Parent_ID'] = sdo_id 
+    res['ID'] = po.org_id 
+    composition_path = find_composite_comp(tree, comp, po.sdo_id)
+    if composition_path:
+      res['Path'] = composition_path[0]
+    else:
+      res['Path'] = 'Unknown'
+
+    result.append(res) 
+  return result
+
+"""
+"""
+def get_ecs_properties(ecs):
+  result=[]
+  for ec in ecs:
+    res={}
+    res['Handle'] = ec.handle
+    res['State'] = ec.running_as_string(add_colour=False)
+    res['Kind'] = ec.kind_as_string(add_colour=False)
+    res['Rate'] = ec.rate
+    if ec.owner_name:
+      res['Owner'] = ec.owner_name
+
+    if ec.participant_names:
+      res['Participants'] = ec.participant_names
+
+    if ec.properties:
+      res['ExtraProperties'] = ec.properties
+    result.append(res)
+  return result
+
+"""
+"""
+def get_ports_properties(comp, ports):
+  result=[]
+  for p in ports:
+    res = get_port_properties(comp, p)
+    result.append(res)
+
+  return result
+
+"""
+"""
+def get_port_properties(comp, p):
+  res = {}
+  res['Name'] = p.name
+  res['Type'] = p.porttype
+  res['Properties'] = p.properties
+
+  if p.porttype == 'CorbaPort' and p.interfaces:
+    res['Interfaces'] = []
+    for intf in p.interfaces:
+      res_intf = {}
+      res_intf['InstanceName'] = intf.instance_name
+      res_intf['TypeName'] = intf.type_name
+      res_intf['Polarity'] = intf.polarity_as_string(add_colour=False)
+    res['Interfaces'].append(res_intf)
+
+  res['Connections'] = []
+  for conn in p.connections:
+    res_conn = {}
+    dest_ports = []
+
+    for name,dp in conn.ports:
+      if not dp:
+        dest_ports.append(name)
+      elif not comp.get_port_by_ref(dp.object) :
+        dest_ports.append(name)
+
+    if dest_ports :
+      res_conn['DestPorts'] = dest_ports
+      res_conn['Name'] = conn.name
+      res_conn['ID'] = conn.id
+      keys = [k for k in conn.properties.keys() \
+              if not k.endswith('inport_ref') \
+              if not k.endswith('inport_ior')]
+      for key in keys:
+        if isinstance(conn.properties[key], str) :
+          res_conn[key] = conn.properties[key]
+
+      res['Connections'].append(res_conn)
+
+  return res
+
+"""
+"""
+def get_component_profile(tree, comp):
+  res = ""
+  profiles={}
+  profiles['Name'] = comp.name
+  profiles['State'] = comp.get_state_string(add_colour=False)
+  profiles['Category'] = comp.category
+  profiles['Description'] = comp.description
+  profiles['InstanceName'] = comp.instance_name
+  profiles['TypeName'] = comp.type_name
+  profiles['Vendor'] = comp.vendor
+
+
+  if comp.parent:
+    profiles['Parent'] = comp.parent_object
+
+  if comp.is_composite:
+    if comp.is_composite_member:
+      profiles['Type'] = 'Composite composite member'
+    else:
+      profiles['Type'] = 'Composite'
+    profiles['Composite'] = get_composite_properties(tree, comp)
+
+  elif comp.is_composite_member:
+    profiles['Type'] = 'Monolithic composit member'
+    profiles['CompositeMember'] = get_composite_member_properties(tree, comp)
+  else:
+    profiles['Type'] = 'Monolithic'
+
+  profiles['ExtraProperties'] = comp.properties
+  profiles['ExecutionContext'] = get_ecs_properties(comp.owned_ecs)
+  profiles['Ports'] = get_ports_properties(comp, comp.ports)
+
+
+  conf_sets = comp.conf_sets
+  if conf_sets :
+    profiles['ConfigSet'] = [k for k in conf_sets.keys() if not is_hidden(k) ]
+    try:
+      profiles['ConfigParams'] = conf_sets[comp.active_conf_set_name].data.keys()
+    except:
+      profiles['ConfigParams'] = []
+
+  profiles['ActiveConfig'] = comp.active_conf_set_name 
+
+  #print >> sys.stderr, profiles
+  return profiles
+
+"""
+"""
+def get_manager_profile(tree, comp):
+  res = {}
+  res['Name'] = comp.profile['name']
+  res['InstanceName'] = comp.profile['instance_name']
+  res['ProcessID'] = comp.profile['pid']
+  res['NamingFormat'] = comp.profile['naming_formats']
+  res['RefstringPath'] = comp.profile['refstring_path']
+  res['ComponentPrecreate'] = comp.profile['components.precreate']
+
+  res['LoadPath'] = comp.profile['modules.load_path']
+  res['ConfigPath'] = comp.profile['modules.config_path']
+  res['Preload'] = comp.profile['modules.preload']
+  res['InitFunctionPrefix'] = comp.profile['modules.init_func_prefix']
+  res['DownloadAllowed'] = comp.profile['modules.download_allowed']
+  res['AbsolutePathAllowed'] = comp.profile['modules.abs_path_allowed']
+
+  res['OsVersion'] = comp.profile['os.version']
+  res['OsArchitecture'] = comp.profile['os.arch']
+  res['OsRelease'] = comp.profile['os.release']
+  res['OsHostname'] = comp.profile['os.hostname']
+  res['OsName'] = comp.profile['os.name']
+
+  res['LoadedModules'] = []
+  for lm in comp.loaded_modules:
+    res_lm = {}
+    res_lm['Filepath'] = lm['file_path']
+    res['LoadedModules'].append(res_lm)
+  
+  res['LoadableModules'] = []
+  for lm in comp.loadable_modules:
+    res_lm = {}
+    res_lm['Filepath'] = lm['module_file_path']
+    res['LoadableModules'].append(res_lm)
+
+  return res
+
+"""
+"""
+def get_or(tree,comp):
+  res = ""
+  res = get_component_profile(tree, comp)
+  
+  return res
+
+"""
+"""
+def getFileContent(fname):
+  global demo_mode
+  res = ""
+  if demo_mode and os.path.exists(fname) and os.path.isfile(fname):
+    f = open(fname, 'r')
+    contents = ""
+    for line in f:
+      contents += line
+    f.close()
+    res = "".join(contents.split("\n"))
+
+  return res
+
+"""
+"""
+def getFileContents(fname):
+  res = ""
+  if os.path.exists(fname) and os.path.isfile(fname):
+    f = open(fname, 'r')
+    contents = ""
+    for line in f:
+      contents += line
+    f.close()
+  return contents
+
+"""
+"""
+def getPortInfoFromFile(filename):
+  global demo_mode
+  if demo_mode :
+    info = getFileContent(filename)
+    comp = eval(info)
+
+    res = []
+    for  port in comp['Ports'] :
+      if port['Type'] == 'CorbaPort' :
+        interfaces = port['Interfaces']
+        info =[]
+        for prop in interfaces:
+          info.append( """%s|%s|%s""" % (prop['InstanceName'],prop['TypeName'],prop['Polarity']))
+        propsinfo = "#".join(info)
+      else :
+        prop = port['Properties']
+        propsinfo = prop['dataport.data_type']
+
+      res.append("%s(%s;%s)" % (port['Name'] , port['Type'], propsinfo))
+
+    result = ",".join(res)
+  else:
+    result = "unknown"
+  return """ "ports": "%s" """ % result
+
+"""
+"""
+def getRtcInfoFromFile(cwd, name):
+  global demo_mode
+  if demo_mode :
+    ndir = cwd.split('/')
+    dirname = "\\".join(ndir[2:])
+    demoroot = os.getenv('DEMO_ROOT')
+    if not demoroot:
+      demoroot = """.\demo"""
+    filename = """%s\%s\%s""" % (demoroot, dirname, name)
+
+    result = getFileContent(filename)
+    return eval(result)
+  else:
+    return ""
+
+"""
+"""
+def getRtcPortFromFile(cwd, name):
+  global demo_mode
+  if demo_mode :
+    ndir = cwd.split('/')
+    dirname = "\\".join(ndir[2:])
+    demoroot = os.getenv('DEMO_ROOT')
+    if not demoroot:
+      demoroot = """.\demo"""
+    filename = """%s\%s\%s""" % (demoroot, dirname, name)
+  
+    if os.path.exists(filename) and os.path.isfile(filename):
+      portinfo = getPortInfoFromFile(filename)
+      result = """{"name": "%s", %s, "context" : "%s/"}""" % (name, portinfo, cwd)
+    else:
+      result = """{"name": "error", "ports": "", "msg" : "Invalid Component name!!", "context" : "%s/"}""" % cwd
+  else:
+    result = """{"name": "error", "ports": "", "msg" : "No such a component", "context" : "%s/"}""" % cwd
+
+  return result
+
+
+def getRtcTree(servers=None, paths=None, orb='global'):
+  global __ORB__
+
+  if paths : f= [paths]
+  else: f=[]
+
+  if orb=='global': orb=__ORB__
+  tree = rtctree.tree.RTCTree(servers=servers, paths=paths, filter=f, orb=orb)
+
+  if tree :
+    if not __ORB__ : __ORB__ = tree.orb
+
+  return tree
+
+"""
+"""
+def getComponent(cwd, name=None, tree=None, orb='global'):
+  try:
+    path = cwd.split("/")
+    if not path[-1] :
+      path.pop()
+      cwd = '/'.join(path)
+
+    rootpath, port = rtctree.path.parse_path(cwd)
+
+    if not tree:
+      tree = getRtcTree(paths=rootpath, orb=orb)
+
+    if name :
+      rootpath.append(name)
+
+    comp = tree.get_node(rootpath)
+  except:
+    tree = None
+    comp = None
+    port = None
+
+  return tree, comp, port
+
+"""
+"""
+def findConnectionId(port1, port2):
+  port1_all_conns = port1.connections
+  port2_all_conns = port2.connections
+
+  for c2 in port2_all_conns:
+    for c1 in port1_all_conns:
+      if c1.id == c2.id :
+        return c1.id
+
+  return None
+
+"""
+"""
+def getConnectedPorts(name, tree=None):
+  try:
+    tree, comp, port = getComponent(name, None, tree=tree)
+    port_objs = comp.connected_ports
+
+  except:
+    port_objs = None
+    pass
+
+  return tree,comp, port_objs
+
+
+"""
+"""
+def getComponentPort(name, tree=None):
+  try:
+    tree, comp, port = getComponent(name, None, tree=tree)
+    port_obj = comp.get_port_by_name(port)
+  except:
+    port_obj = None
+    pass
+
+  return tree,comp, port_obj
+
+"""
+"""
+def activateComponent(name, ec_index=0, tree=None):
+  code = 0
+  tree1, comp, port = getComponent(name, tree=tree)
+
+  if tree1 and comp :
+    comp.activate_in_ec(ec_index)
+    time.sleep(0.5)
+    state = comp.get_state_string(add_colour=False)
+    result ="""{"name" :  "%s", "result" : "%s" }""" % (name, state)
+    code = 1
+  else:
+    result ="""{"name" :  "%s", "result" : "Fail" }""" % name
+
+  return code, result
+
+"""
+"""
+def deactivateComponent(name, ec_index=0, tree=None):
+  code = 0
+  tree1, comp, port = getComponent(name, tree=tree)
+
+  if tree1 and comp :
+    comp.deactivate_in_ec(ec_index)
+    result ="""{"name" :  "%s", "result" : "Success" }""" % name
+    code = 1
+  else:
+    result ="""{"name" :  "%s", "result" : "Fail" }""" % name
+
+  return code, result
+
+"""
+"""
+def resetComponent(name, ec_index=0, tree=None):
+  code = 0
+  tree, comp, port = getComponent(name, tree=tree)
+
+  if tree and comp :
+    comp.reset_in_ec(ec_index)
+    result ="""{"name" :  "%s", "result" : "Success" }""" % name
+    code = 1
+  else:
+    result ="""{"name" :  "%s", "result" : "Fail" }""" % name
+
+  return code, result
+
+"""
+"""
+def getComponentState(name, ec_index=0, tree=None):
+  code = 0
+  tree, comp, port = getComponent(name, tree=tree)
+
+  if tree and comp :
+    state = comp.get_state_string(add_colour=False)
+    result ="""{"name" :  "%s", "result" : "%s" }""" % (name, state)
+    code = 1
+  else:
+    result ="""{"name" :  "%s", "result" : "Fail" }""" % name
+
+  return code, result
+
+"""
+"""
+def delComponentRef(fullpath, tree=None):
+  path, port = rtctree.path.parse_path(fullpath)
+  if port: return False
+  if not path[-1]: path = path[:-1]
+  if len(path) <= 2: return False
+
+  if not tree: tree = getRtcTree(paths=path)
+  parent=tree.get_node(path[:-1])
+
+  if parent.is_manager: return False
+  if not parent.is_directory: return False
+
+  name = path[-1]
+  if name.find('.') == -1 : name=name+'.'
+
+  parent.unbind(name)
+  return True
+
+
+"""
+"""
+def exitComponent(name, tree=None):
+  code = 0
+  tree, comp, port = getComponent(name, tree=tree)
+
+  if tree and comp :
+    try:
+      comp.exit()
+      result ="""{"name" :  "%s", "result" : "Success" }""" % name
+    except:
+      result ="""{"name" :  "%s", "result" : "Fail" }""" % name
+      code = -2
+      pass
+  else:
+    result ="""{"name" :  "%s", "result" : "No such a component" }""" % name
+    code = -1
+
+  return code, result
+
+"""
+"""
+def deleteZombieComponents(hosts):
+  res = delete_all_zombies(hosts)
+
+  result = ""
+
+  if res == False:
+    result = "Fail"
+  else:
+    for x in res:
+      result += "Delete %s\n" % x
+    result += "OK"
+
+  return result
+
+"""
+"""
+def getComponentList(host):
+  if not host : host = ""
+  roothost = "/"+host
+  tree, root, port = getComponent(roothost)
+
+  if tree and root :
+    val = format_nsview_json(tree, root)
+#    result = """{ "hostname": "%s", "components": [ %s ]} """ % (host, val)
+  else:
+    dirname = os.getenv('DEMO_ROOT')
+    if not dirname : dirname = "demo"
+    try:
+      val = format_dir_json(dirname)
+    except:
+      val = """ "No such component" """
+
+  result = """{ "hostname":"%s", "components": [ %s] }""" % (host, val)
+  return result
+
+"""
+"""
+def getConnectionInfo(name1, name2):
+  result = {}
+
+  try:
+    p1 = catComponentInfo(name1)
+    p2 = catComponentInfo(name2)
+
+    connections1 = p1['PortInfo']['Connections']
+    connections2 = p2['PortInfo']['Connections']
+
+  except:
+    print >>sys.stderr, "ERROR in getConnectionInfo"
+    return result
+
+  for con1 in  connections1 :
+    for con2 in  connections2 :
+      if con1['ID'] == con2['ID'] :
+         result = con1
+         if p1['PortInfo']['Type'] == 'CorbaPort':
+           result['ConnectionType']='ServicePort'
+         else:
+           result['ConnectionType']='DataPort'
+         break
+  return result;
+"""
+"""
+def catComponentInfo(name):
+  tree, comp, port = getComponent(name,orb=None)
+  result = {}
+
+  if tree and comp :
+    rootpath, port = rtctree.path.parse_path(name)
+
+    if port :
+      if comp.is_component :
+        p = comp.get_port_by_name(port)
+        if p :
+          result['PortInfo'] = get_port_properties(comp, p)
+    else:
+      if comp.is_component :
+        result = get_component_profile(tree,comp)
+        result['PathUrl'] = name
+      elif comp.is_manager :
+        result = get_manager_profile(tree,comp)
+        result['PathUrl'] = name
+      elif comp.is_zombie:
+        result = {'Name': comp.name, 'State': 'Zombie', 'Type': 'Zombie' }
+        result['PathUrl'] = name
+      else:
+        result = "" 
+  else:
+    result = """{"name": "%s", "ports": "", "msg" : "No name!!"}""" % name
+
+  return result
+
+
+"""
+"""
+def catComponent(name):
+  path = name.split("/")
+  try:
+    pos = path[-1].index(".rtc")
+  except:
+    return """{"name": "%s", "ports": "", "msg" : "No name!!"}""" % name
+  
+  rtcname = path.pop()
+  cwd = "/".join(path)
+
+  return catComponent2(cwd, rtcname)
+
+"""
+"""
+def catComponent2(cwd, name):
+  if not name :
+   result= """{"name": "error", "ports": "", "msg" : "No name!!"}"""
+
+  else:
+    tree, comp, port = getComponent(cwd, name)
+
+    if tree and comp :
+      rootpath, port = rtctree.path.parse_path(cwd)
+      if port :
+        if comp.is_component :
+          p = comp.get_port_by_name(port)
+          if p :
+            result['PortInfo'] = get_port_properties(comp, p)
+      else:
+        if comp.is_component :
+          result = get_component_profile(tree,comp)
+          result['PathUrl'] = cwd
+        elif comp.is_manager :
+          result = get_manager_profile(tree,comp)
+          result['PathUrl'] = cwd
+        elif comp.is_zombie:
+          result = {'Name': comp.name, 'State': 'Zombie', 'Type': 'Zombie' }
+          result['PathUrl'] = cwd
+        else:
+          result = "" 
+    else:
+      result= """{"name": "error", "ports": "", "msg" : "No name!!"}"""
+      #result = getRtcInfoFromFile(cwd, name)
+      #result['PathUrl'] = cwd
+
+    return result
+
+
+"""
+"""
+def checkComponent(name):
+  path = name.split("/")
+  try:
+    pos = path[-1].index(".rtc")
+  except:
+    return ""
+  
+  rtcname = path.pop()
+  cwd = "/".join(path)
+
+  return checkComponent2(cwd, rtcname)
+
+"""
+"""
+def checkComponent2(cwd, name):
+  if not name :
+   result= ""
+
+  else:
+    tree, comp, port = getComponent(cwd, name)
+
+    if tree and comp :
+      rootpath, port = rtctree.path.parse_path(cwd)
+      if port :
+        if comp.is_component :
+          result = "Component"
+      else:
+        if comp.is_component :
+          result = "Component"
+        elif comp.is_manager :
+          result = "Manager"
+        elif comp.is_zombie:
+          result = "Zombie"
+        else:
+          result = "" 
+    else:
+      result= ""
+
+    return result
+
+"""
+"""
+def getComponentInfo(cwd, name):
+  if not name :
+    result= """{"name": "error", "ports": "", "msg" : "No name....!!"}"""
+
+  else:
+    tree, comp, port = getComponent(cwd, name)
+
+    if tree and comp :
+      ports = comp.ports
+
+      portInfo =""
+      for x in ports:
+        if portInfo != "" : portInfo +=","
+        portInfo += format_port_info(x)
+
+      ecs = get_ecs_properties(comp.owned_ecs)
+      status = ecs[0]['State']
+      PortsInfo = get_ports_properties(comp, comp.ports)
+
+      result = """{"name": "%s", "ports" : "%s", "context" : "%s/"}""" % (name, portInfo, cwd)
+    else:
+      result = getRtcPortFromFile(cwd, name)
+
+  return result
+
+"""
+"""
+def connectPorts(name1, name2, tree=None):
+  if not name1 or  not name2:
+    result =  '{"name": "error", "ports": "", "msg" : "Invalid Component name!!"}'
+    code = -2
+
+  else:
+    try:
+      tree1, comp1, port_obj1 = getComponentPort(name1, tree)
+      tree2, comp2, port_obj2 = getComponentPort(name2, tree)
+
+      conn_id = findConnectionId(port_obj1, port_obj2)
+
+      if conn_id :
+        result = "Connection %s:%s and %s:%s already exists. " % (comp1.name, port1, comp2.name, port2)
+        code = 0
+      else:
+        profile =""
+        name = "%s.%s_%s.%s" % (comp1.name[0:-4], port_obj1.name, comp2.name[0:-4], port_obj2.name)
+        port_obj1.connect([port_obj2] , name)
+        #result = "Connected:  %s %s" % (name1, name2)
+        result = "Connected:  "
+        code = 1
+    except Exception, e:
+      result = "Error in connectPorts"
+      code=-1
+
+  return code,result
+
+
+"""
+"""
+def disconnectPorts(name1, name2):
+#  global __ORB__
+  if not name1 or  not name2:
+    result =  '{"name": "error", "ports": "", "msg" : "Invalid Component name!!"}'
+    code = -2
+
+  else:
+    try:
+      tree1, comp1, port_obj1 = getComponentPort(name1)
+      tree2, comp2, port_obj2 = getComponentPort(name2)
+      conn_id = findConnectionId(port_obj1, port_obj2)
+
+      if conn_id :
+        pobj = port_obj1.get_connection_by_id(conn_id)
+        pobj.disconnec()
+        result = "Disonnected:  %s and  %s" % (port_obj1.name, port_obj2.name)
+        code = 1
+      else: 
+        result = "No such a connection."
+        code = 0
+
+    except Exception, e:
+      result = "Error in disconnectPorts"
+      code = -1
+
+  return code,result
+
+"""
+"""
+def disconnectAll(name1, tree=None):
+  if not name1 :
+    result =  '{"name": "error", "ports": "", "msg" : "Invalid Component name!!"}'
+    code = -2
+
+  else:
+    try:
+      tree1, comp1, port_obj1 = getComponentPort(name1, tree)
+      if not comp1 : return False
+      if not comp1.is_component : return False
+
+      if port_obj1 : 
+        port_obj1.disconnect_all()
+        result = "Disonnected:  %s" % (port_obj1.name)
+        code = 0
+
+      else:
+        comp1.disconnect_all()
+        result = "Disonnected:  %s" % (comp1.name)
+        code = 0
+
+    except Exception, e:
+      result = "Error in disconnectPorts"
+      code = -1
+
+  return code,result
+
+"""
+"""
+def delete_all_zombies(hosts='localhost'):
+  hostnames = hosts.split(',')
+
+  tree = getRtcTree(servers=hostnames)
+
+  if not tree:
+    return False
+
+  def del_zombie(node, args):
+    try:
+      name = node.name
+      node.parent.unbind(node.name)
+      return name
+    except Exception, e:
+      traceback.print_exc()
+      print >>sys.stderr, '{0}: {1}'.format(sys.argv[0], e)
+
+  return tree.iterate(del_zombie, filter=['is_zombie'])
+
+
+"""
+"""
+def is_hidden(name):
+    hidden_prefix = '__'
+    hidden_suffix = '__'
+    return name.startswith(hidden_prefix) and name.endswith(hidden_suffix)
+
+def getRtcConfigurationList(name):
+  tree, comp, port = getComponent(name)
+
+#  result =  '{"name": "error", "config": "", "msg" : "Invalid Component name!!"}'
+
+  conf_sets = comp.conf_sets
+  print conf_sets.keys()
+
+  active_conf = comp.active_conf_set_name 
+
+  result = []
+  set_keys = [k for k in conf_sets.keys() if not is_hidden(k) or all]
+  
+  set_keys.sort()
+  for set_name in set_keys:
+    if set_name == active_conf :
+      result = conf_sets[set_name].data
+      params = conf_sets[set_name].data.keys()
+      for param in params:
+        print """ %s   %s """ % (param, conf_sets[set_name].data[param])
+
+  return result
+
+
+"""
+"""
+def getRtcConfiguration(name, conf_name):
+  tree, comp, port = getComponent(name)
+#  result =  '{"name": "error", "config": "", "msg" : "Invalid Component name!!"}'
+  conf_sets = comp.conf_sets
+  active_conf = comp.active_conf_set_name 
+  result = conf_sets[active_conf].data[conf_name]
+
+  return result
+
+
+"""
+"""
+def setRtcConfiguration(name, conf_name, val, setname=None):
+  tree, comp, port = getComponent(name)
+#  result =  '{"name": "error", "config": "", "msg" : "Invalid Component name!!"}'
+  if setname :
+    comp.activate_conf_set(setname)
+    active_conf = setname
+  else:
+    active_conf = comp.active_conf_set_name 
+
+  comp.set_conf_set_value(active_conf, conf_name, val)
+  comp.activate_conf_set(active_conf)
+
+  print >> sys.stderr, "Config: %s: %s = %s" % (active_conf, conf_name, val)
+  result = """ {"ConfigName" : "%s", "Value": "%s" }""" % (conf_name, val)
+  return result
+
+
+"""
+"""
+def activateRtcConfigurationSet(name, conf_name):
+  tree, comp, port = getComponent(name)
+  set_keys = [k for k in conf_sets.keys() if not is_hidden(k) ]
+
+  for set_name in set_keys:
+    if set_name == conf_name :
+      comp.activate_conf_set(conf_name)
+
+  result = """ { "Activate" : "%s" } """ % (conf_name)
+  return result
+
+"""
+"""
+def getRtcConfigurationSet(name):
+  tree, comp, port = getComponent(name)
+  conf_sets = comp.conf_sets
+  result = [k for k in conf_sets.keys() if not is_hidden(k) ]
+  return result
+
+"""
+"""
+def getActiveRtcConfigurationSet(name):
+  tree, comp, port = getComponent(name)
+  result = comp.active_conf_set_name 
+  return result
+
+"""
+"""
+def getRtcProfileList(dirname):
+  try:
+    val = format_dir_json(dirname)
+  except:
+    val = """ "No such component" """
+
+  result = """{ "folder":"%s", "components": [ %s] }""" % (dirname, val)
+  return result
+
+
+"""
+"""
+def getValue(form, val, n_arg):
+  try:
+    res = form[val].value
+  except:
+    if len(sys.argv) > n_arg+1:
+      res = sys.argv[n_arg+1]
+    else:
+      res = None
+  return res
+
+"""
+"""
+def printResult(result, isCGI=1):
+  if isCGI == 1 :
+    print "Content-type: text/html;charset=utf-8"
+    print ""
+    print result
+  else:
+    printJSON(result)
+
+"""
+"""
+def printJSON(result):
+  try:
+    res = json.loads(result)
+    print json.dumps(res, indent=2)
+  except:
+    print result

Added: branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/setup.py
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/setup.py	                        (rev 0)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/setup.py	2014-05-30 05:24:03 UTC (rev 163)
@@ -0,0 +1,18 @@
+
+from distutils.core import setup
+import py2exe
+
+option = {
+  "packages"	 : ['PIL'],
+  "compressed"	 : 1,
+  "optimize"	 : 2,
+  }
+
+setup(
+  options = { "py2exe"	:	option },
+  console = [
+   {"script"	:	"TkCalibGUIComp.py" }
+  ],
+  zipfile = 'lib\libs.zip'
+)
+			 

Modified: branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/tkcalibgui.py
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/tkcalibgui.py	2014-05-16 06:49:37 UTC (rev 162)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/TkCalibGUI/tkcalibgui.py	2014-05-30 05:24:03 UTC (rev 163)
@@ -15,16 +15,19 @@
 import time
 import sys
 import os.path
-from cv2.cv import *
+#from cv2.cv import *
+import numpy
 from io import BytesIO
 from StringIO import StringIO
-from rtshell import rtcat, rtcon
 from threading import Thread
 
+import rtutil
+
 WIN_DEFAULT_CAMERA = {
 				"DirectShowCamComp" : ":m_FIN_OUTPORT",
 				"OpenCVCameraComp"  : ":out",
-				"PGRCameraComp"     : ":outputImage"
+				"PGRCameraComp"     : ":outputImage",
+				"MFCameraComp"      : ":out"
 				}
 LINUX_DEFAULT_CAMERA = {
 				"OpenCVCameraComp"  : ":out",
@@ -49,6 +52,9 @@
 		self.save_btn_state = [] # 保存ボタンの状態保存用
 		self.view_flg = True	# カメラ画像を表示する
 		self.comp_all_found = False
+
+		self.camera_select_flg = False
+		self.exit_btn_click = False
 			
 		self.result_mes = StringVar()
 		self.result_mes.set("")
@@ -198,12 +204,15 @@
 			# automatically connect the components
 			ret = self.connect_components_auto()
 			if not ret:
-				self.connect_components_manually()
+				self.register_me()
 				self.comp_all_found = False
 		else:
 			# manually connect the components
-			self.connect_components_manually()		
+			self.register_me()		
 
+		# camera selected
+		self.camera_select_flg = True
+		
 		# right pane (2)
 		fr = Frame(self.frameR_base, width=self.frame_w)
 		fr.pack(ipadx=3, ipady=3, fill=X)		
@@ -363,7 +372,11 @@
 		else:
 			# camera not found
 			self.set_warning_msg()
-		self.comp_btn.config(state = NORMAL)
+			
+		if self.camera_select_flg:
+			self.comp_btn.config(state = DISABLED)
+		else:
+			self.comp_btn.config(state = NORMAL)
 		
 	def set_warning_msg(self):
 		Label(self.fix1, text=self.msg11, fg="red").grid(column = 0, row = 1, columnspan = 3)	
@@ -436,10 +449,11 @@
 		self.comp_startup_check(camera_rtc)
 		tree = self.comp_startup_check(calib_rtc)
 		
-		ret1 = rtcon.main([str(camera_rtc)+str(camera_outport), str(calib_rtc)+":original_image"], tree)
-		ret2 = rtcon.main([str(calib_rtc)+":checker_image", str(tk_rtc)+":checker_image"], tree)
-		ret3 = rtcon.main([str(calib_rtc)+":CameraCalibrationService", str(tk_rtc)+":CameraCalibrationService"], tree)
-		if ret1==1 or ret2==1 or ret3==1:
+		ret1, res = rtutil.connectPorts(str(camera_rtc)+str(camera_outport), str(calib_rtc)+":original_image", tree)
+		ret2, res = rtutil.connectPorts(str(calib_rtc)+":checker_image", str(tk_rtc)+":checker_image", tree)
+		ret3, res = rtutil.connectPorts(str(calib_rtc)+":CameraCalibrationService", str(tk_rtc)+":CameraCalibrationService", tree)
+
+		if ret1 < 0 or ret2 < 0 or ret3 < 0:
 			Label(self.fix1, text=self.err_msg3, fg="red").grid(column = 0, row = 1, columnspan = 3)
 			Label(self.fix1, text=self.msg11, fg="red").grid(column = 0, row = 2, columnspan = 3)
 			return False
@@ -447,7 +461,7 @@
 		self.set_comp_name(camera_rtc, calib_rtc, tk_rtc, tree)
 		return True
 
-	def connect_components_manually(self):
+	def register_me(self):
 		tk_rtc = self.rt_dir + GUI_COMP + "0.rtc"
 		self.tree = self.comp_startup_check(tk_rtc)
 		self.rtc_list = [tk_rtc]
@@ -462,8 +476,8 @@
 		filter = [path]
 		for cnt in range(0, 10):
 			tree = rtctree.tree.RTCTree(paths=path, orb=self.orb, filter=filter)
-			ret, result = rtcat.main([str(name)], tree)
-			if ret == 0:
+			tree1,comp1, port1 = rtutil.getComponent(str(name), tree=tree)
+			if tree1 and comp1:
 				judge = " found."
 				break
 			time.sleep(1)
@@ -514,15 +528,17 @@
 	
 	# [callback] exit button
 	def exit_comp(self):
-		self.comp_button['activate'].config(state = DISABLED)
-		self.comp_button['deactivate'].config(state = DISABLED)
-		self.comp_button['exit'].config(state = DISABLED)
-		
+		if self.exit_btn_click:
+			# exit_comp already completed.
+			return
+			
 		# Start a thread to observe the transition state of the component.
 		self.comp_transition_thread = CompTransitionThread('Exit', self.rtc_list, self.tree)
 		self.comp_transition_thread.start()
 		self.exit_state = True
 		self.check_exit()
+		
+		self.exit_btn_click = True
 
 	def check_exit(self):
 		if not self.comp_transition_thread.running:
@@ -628,8 +644,8 @@
 			self.flip_button['tb'].config(state = DISABLED)
 			self.flip_button['lr'].config(state = DISABLED)
 			return
-		import ImageOps
-		import ImageDraw
+
+		from PIL import ImageOps,ImageDraw
 		# Getting PIL image from buffer, converting RGB->BGR
 		self.pilImage = Image.frombuffer("RGB", (width, height),	image, "raw", "BGR")
 		# Resize if size is different
@@ -670,13 +686,13 @@
 		self.orb = orb
 		self.rt_dir = rt_dir
 		self.comp_option = comp_option
+		self.register_me()
 
 	def set_on_update(self, func):
 		self.on_update = func
 
 #============================================================
 # Thread class
-from rtshell import rtact, rtdeact, rtdis, rtexit, state_control_base
 		
 class CompTransitionThread(Thread):
 	def __init__(self, msg, rtc_list, tree):
@@ -688,38 +704,47 @@
 		
 		if self.msg.find("Activate") != -1:
 			# Activate
-			self.action = rtact.activate_action
 			self.cmd = "rtact"
 		else:
 			# Deactivate or Exit
-			self.action = rtdeact.deactivate_action
 			self.cmd = "rtdeact"
 		
 	def run(self):
 		comp_cnt = len(self.rtc_list)
 		self.running = True
 		for i in range(comp_cnt):
-			self.check_comp(self.cmd, self.rtc_list[i], self.msg, self.action)
+			self.check_comp(self.cmd, self.rtc_list[i])
 			
 		if self.msg == "Exit":
 			for i in range(comp_cnt):
 				# rtdis
-				self.check_comp('rtdis', self.rtc_list[i], None, None)
+				self.check_comp('rtdis', self.rtc_list[i])
 			
 			for i in range(comp_cnt):
 				# rtexit
-				self.check_comp('rtexit', self.rtc_list[i], None, None)
+				self.check_comp('rtexit', self.rtc_list[i])
 		self.running = False
 
-	def check_comp(self, cmd, rtc, msg, action):
+	def check_comp(self, cmd, rtc):
 		for cnt in range(0, 10):
 			if cmd == "rtdis":
-				ret = rtdis.main([str(rtc)], self.tree)
+				ret,res = rtutil.disconnectAll(str(rtc), self.tree)
+
 			elif cmd == "rtexit":
-				ret = rtexit.main([str(rtc)], self.tree)
+				ret,res = rtutil.exitComponent(str(rtc), self.tree)
+
+			elif cmd == "rtact":
+				ret,res = rtutil.activateComponent(str(rtc), tree=self.tree)
+				if ret == 1: ret=0
+				else: ret=0
+
+			elif cmd == "rtdeact":
+				ret,res = rtutil.deactivateComponent(str(rtc), tree=self.tree)
+				if ret == 1: ret=0
+				else: ret=0
 			else:
-				# activate, deactivate
-				ret = state_control_base.base_main(msg, action, [str(rtc)], self.tree)
+				pass
+
 			if ret == 0:
 				print "{0}:{1} OK!".format(cmd, str(rtc))
 				break



More information about the openrtm-commit mailing list