directory.py
Go to the documentation of this file.
1 # -*- Python -*-
2 # -*- coding: utf-8 -*-
3 
4 '''rtctree
5 
6 Copyright (C) 2009-2014
7  Geoffrey Biggs
8  RT-Synthesis Research Group
9  Intelligent Systems Research Institute,
10  National Institute of Advanced Industrial Science and Technology (AIST),
11  Japan
12  All rights reserved.
13 Licensed under the Eclipse Public License -v 1.0 (EPL)
14 http://www.opensource.org/licenses/eclipse-1.0.txt
15 
16 Object representing a directory node in the tree.
17 
18 '''
19 
20 
21 from copy import deepcopy
22 import CosNaming
23 from omniORB import URI, CORBA, TRANSIENT_ConnectFailed
24 import sys
25 
26 from rtctree.component import Component
27 from rtctree.exceptions import BadPathError
28 from rtctree.manager import Manager
29 from rtctree.node import TreeNode
30 from rtctree.options import Options
31 from rtctree.unknown import Unknown
32 from rtctree.zombie import Zombie
33 from rtctree.utils import filtered, trim_filter
34 import RTC
35 import RTM
36 
37 
38 ##############################################################################
39 ## Directory node object
40 
42  '''Node representing a naming context on a name server.
43 
44  Name servers contain contexts (including the root context) and objects. For
45  us, contexts are directories and objects are managers and components. A
46  directory context may specialise as a name server context, in which case
47  it represents the root context of a name server.
48 
49  '''
50  def __init__(self, name=None, parent=None, children=None, filter=[], *args,
51  **kwargs):
52  '''Constructor. Calls the TreeNode constructor.'''
53  super(Directory, self).__init__(name=name, parent=parent,
54  children=children, filter=filter, *args, **kwargs)
55 
56  def reparse(self):
57  '''Reparse all children of this directory.
58 
59  This effectively rebuilds the tree below this node.
60 
61  This operation takes an unbounded time to complete; if there are a lot
62  of objects registered below this directory's context, they will all
63  need to be parsed.
64 
65  '''
67  self._parse_context(self._context, self.orb)
68 
69  def unbind(self, name):
70  '''Unbind an object from the context represented by this directory.
71 
72  Warning: this is a dangerous operation. You may unlink an entire
73  section of the tree and be unable to recover it. Be careful what you
74  unbind.
75 
76  The name should be in the format used in paths. For example,
77  'manager.mgr' or 'ConsoleIn0.rtc'.
78 
79  '''
80  with self._mutex:
81  id, sep, kind = name.rpartition('.')
82  if not id:
83  id = kind
84  kind = ''
85  name = CosNaming.NameComponent(id=str(id), kind=str(kind))
86  try:
87  self.context.unbind([name])
88  except CosNaming.NamingContext.NotFound:
89  raise BadPathError(name)
90 
91  @property
92  def context(self):
93  '''The object representing this naming context.'''
94  with self._mutex:
95  return self._context
96 
97  @property
98  def is_directory(self):
99  '''Is this node a directory?'''
100  return True
101 
102  def _parse_context(self, context, orb, filter=[]):
103  with self._mutex:
104  # Parse a naming context to fill in the children.
105  self._context = context
106  # Get the list of bindings from the context
107  bindings, bindings_it = context.list(Options().\
108  get_option('max_bindings'))
109  for binding in bindings:
110  # Process the bindings that are within max_bindings
111  self._process_binding(binding, orb, filter)
112  if bindings_it:
113  # Handle the iterator containing the remaining bindings
114  remaining, bindings = bindings_it.next_n(Options().\
115  get_option('max_bindings'))
116  while remaining:
117  for binding in bindings:
118  self._process_binding(binding, orb, filter)
119  remaining, binding = bindings_it.next_n(Options().\
120  get_option('max_bindings'))
121  bindings_it.destroy()
122 
123  def _process_binding(self, binding, orb, filter):
124  if filtered([corba_name_to_string(binding.binding_name)], filter):
125  # Do not pass anything which does not pass the filter
126  return
127  trimmed_filter = trim_filter(deepcopy(filter))
128  with self._mutex:
129  # Process a binding, creating the correct child type for it and
130  # adding that child to this node's children.
131  if binding.binding_type == CosNaming.nobject:
132  # This is a leaf node; either a component or a manager. The
133  # specific type can be determined from the binding name kind.
134  if binding.binding_name[0].kind == 'mgr':
135  name = corba_name_to_string(binding.binding_name)
136  obj = self._context.resolve(binding.binding_name)
137  if not obj:
138  leaf = Zombie(name, self)
139  return
140  obj = obj._narrow(RTM.Manager)
141  try:
142  leaf = Manager(name, self, obj, dynamic=self.dynamic)
143  except CORBA.OBJECT_NOT_EXIST:
144  # Manager zombie
145  leaf = Zombie(name, self)
146  except CORBA.TRANSIENT:
147  # Manager zombie
148  leaf = Zombie(name, self)
149  self._add_child(leaf)
150  elif binding.binding_name[0].kind == 'rtc':
151  name = corba_name_to_string(binding.binding_name)
152  obj = self._context.resolve(binding.binding_name)
153  try:
154  obj = obj._narrow(RTC.RTObject)
155  except CORBA.TRANSIENT, e:
156  if e.args[0] == TRANSIENT_ConnectFailed:
157  self._add_child(Zombie(name, self))
158  return
159  else:
160  raise
161  except CORBA.OBJECT_NOT_EXIST:
162  self._add_child(Zombie(name, self))
163  return
164  try:
165  leaf = Component(name, self, obj, dynamic=self.dynamic)
166  except CORBA.OBJECT_NOT_EXIST:
167  # Component zombie
168  leaf = Zombie(name, self, dynamic=self.dynamic)
169  except CORBA.TRANSIENT, e:
170  if e.args[0] == TRANSIENT_ConnectFailed:
171  self._add_child(Zombie(name, self))
172  return
173  else:
174  raise
175  self._add_child(leaf)
176  else:
177  # Unknown type - add a plain node
178  name = corba_name_to_string(binding.binding_name)
179  obj = self._context.resolve(binding.binding_name)
180  leaf = Unknown(name, self, obj)
181  self._add_child(leaf)
182  else:
183  # This is a context, and therefore a subdirectory.
184  subdir_name = corba_name_to_string(binding.binding_name)
185  subdir = Directory(subdir_name, self, filter=trimmed_filter,
186  dynamic=self.dynamic)
187  subdir_context = self._context.resolve(binding.binding_name)
188  subdir_context = subdir_context._narrow(CosNaming.NamingContext)
189  subdir._parse_context(subdir_context, orb,
190  filter=trimmed_filter)
191  self._add_child(subdir)
192 
193 
195  '''Convert a CORBA CosNaming.Name to a string.'''
196  parts = []
197  if type(name) is not list and type(name) is not tuple:
198  raise NotCORBANameError(name)
199  if len(name) == 0:
200  raise NotCORBANameError(name)
201 
202  for nc in name:
203  if not nc.kind:
204  parts.append(nc.id)
205  else:
206  parts.append('{0}.{1}'.format(nc.id, nc.kind))
207  return '/'.join(parts)
208 
209 
210 # vim: tw=79
211 
Component node object.
Definition: component.py:38
Zombie node object.
Definition: zombie.py:28
Unknown node object.
Definition: unknown.py:28
Directory node object.
Definition: directory.py:41
def __init__(self, name=None, parent=None, children=None, filter=[], args, kwargs)
Definition: directory.py:51
def dynamic(self)
Definition: node.py:221
def corba_name_to_string(name)
Definition: directory.py:194
def trim_filter(filter, levels=1)
Definition: utils.py:201
def unbind(self, name)
Definition: directory.py:69
def orb(self)
Definition: node.py:309
def _remove_all_children(self)
Definition: node.py:376
def filtered(path, filter)
Definition: utils.py:177
def _add_child(self, new_child)
Definition: node.py:360
Options object.
Definition: options.py:29
Manager node object.
Definition: manager.py:42
Base node object.
Definition: node.py:29
def _parse_context(self, context, orb, filter=[])
Definition: directory.py:102
def _process_binding(self, binding, orb, filter)
Definition: directory.py:123


rtctree
Author(s): Geoffrey Biggs
autogenerated on Fri Jun 7 2019 21:56:24