Properties.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
5 
17 
18 
19 import sys
20 import string
21 
22 import OpenRTM_aist
23 
24 
25 
83 class Properties:
84  """
85  """
86 
87 
164  def __init__(self, key=None, value=None, defaults_map=None, defaults_str=None, num=None, prop=None):
165  self.default_value = ""
166  self.root = None
167  self.empty = ""
168  self.leaf = []
169 
170  # Properties::Properties(const Properties& prop)
171  if prop:
172  self.name = prop.name
173  self.value = prop.value
174  self.default_value = prop.default_value
175 
176  keys = prop.propertyNames()
177  for _key in keys:
178  node = None
179  node = prop.getNode(_key)
180  if node:
181  self.setDefault(_key, node.default_value)
182  self.setProperty(_key, node.value)
183 
184  return
185 
186  # Properties::Properties(const char* key, const char* value)
187  if key:
188  self.name = key
189  if value is None:
190  self.value = ""
191  else:
192  self.value = value
193  return
194 
195  self.name = ""
196  self.value = ""
197 
198  # Properties::Properties(std::map<std::string, std::string>& defaults)
199  if defaults_map:
200  #for i in range(len(defaults_map.items())):
201  # self.setDefault(defaults_map.keys()[i], defaults_map.values()[i])
202  for key, value in defaults_map.items():
203  self.setDefault(key, value)
204  return
205 
206  if defaults_str:
207  if num is None:
208  _num = sys.maxint
209  else:
210  _num = num
211  self.setDefaults(defaults_str, _num)
212  return
213 
214 
215 
231  def assigmentOperator(self, prop):
232  self.clear()
233  self.name = prop.name
234  self.value = prop.value
235  self.default_value = prop.default_value
236 
237  keys = prop.propertyNames()
238 
239  for key in keys:
240  node = None
241  node = prop.getNode(key)
242  if node:
243  self.setDefault(key, node.default_value)
244  self.setProperty(key, node.value)
245 
246  return self
247 
248 
249 
261  def __del__(self):
262  self.clear()
263  if self.root:
264  self.root.removeNode(self.name)
265  return
266 
267  #============================================================
268  # public functions
269  #============================================================
270 
271 
272 
285  def getName(self):
286  return self.name
287 
288 
289 
302  def getValue(self):
303  return self.value
304 
305 
306 
319  def getDefaultValue(self):
320  return self.default_value
321 
322 
323 
336  def getLeaf(self):
337  return self.leaf
338 
339 
340 
353  def getRoot(self):
354  return self.root
355 
356 
357 
385  def getProperty(self, key, default=None):
386  if default is None:
387  keys = []
388  #keys = string.split(key, ".")
389  self.split(key, ".", keys)
390 
391  node = None
392  node = self._getNode(keys, 0, self)
393  if node:
394  if node.value:
395  return node.value
396  else:
397  return node.default_value
398  return self.empty
399 
400  else:
401  value = self.getProperty(key)
402  if value:
403  return value
404  else:
405  return default
406 
407 
408 
423  def getDefault(self, key):
424  keys = []
425  #keys = string.split(key, ".")
426  self.split(key, ".", keys)
427  node = None
428  node = self._getNode(keys, 0, self)
429  if node:
430  return node.default_value
431 
432  return self.empty
433 
434 
435 
463  def setProperty(self, key, value=None):
464  if value is not None:
465  keys = []
466  #keys = string.split(key, ".")
467  self.split(key, ".", keys)
468  curr = self
469  for _key in keys:
470  next = curr.hasKey(_key)
471  if next is None:
472  next = OpenRTM_aist.Properties(key=_key)
473  next.root = curr
474  curr.leaf.append(next)
475  curr = next
476  retval = curr.value
477  curr.value = value
478  return retval
479 
480  else:
481  self.setProperty(key, self.getProperty(key))
482  prop = self.getNode(key)
483  return prop.value
484 
485 
486 
501  def setDefault(self, key, value):
502  keys = []
503  self.split(key, ".", keys)
504  #keys = string.split(key, ".")
505 
506  curr = self
507  for _key in keys:
508  next = curr.hasKey(_key)
509  if next is None:
510  next = OpenRTM_aist.Properties(key=_key)
511  next.root = curr
512  curr.leaf.append(next)
513  curr = next
514  if value != "" and value[-1] == "\n":
515  value = value[0:len(value)-1]
516  curr.default_value = value
517  return value
518 
519 
520 
536  def setDefaults(self, defaults, num = None):
537  if num is None:
538  num = sys.maxint
539 
540  i = 0
541  len_ = len(defaults)
542  while 1:
543  if i > num or i > (len_ - 1) or defaults[i] == "":
544  break
545 
546  key = [defaults[i]]
547  value = [defaults[i+1]]
548 
549  OpenRTM_aist.eraseHeadBlank(key)
550  OpenRTM_aist.eraseTailBlank(key)
551 
552  OpenRTM_aist.eraseHeadBlank(value)
553  OpenRTM_aist.eraseTailBlank(value)
554 
555  self.setDefault(key[0], value[0])
556 
557  i +=2
558 
559 
560 
561  #============================================================
562  # load and save functions
563  #============================================================
564 
565 
586  def list(self, out):
587  self._store(out, "", self)
588  return
589 
590 
591 
753  def load(self, inStream):
754  pline = ""
755  for readStr in inStream:
756  if not readStr:
757  continue
758 
759  tmp = [readStr]
760  OpenRTM_aist.eraseHeadBlank(tmp)
761  _str = tmp[0]
762 
763  if _str[0] == "#" or _str[0] == "!" or _str[0] == "\n":
764  continue
765 
766  _str = _str.rstrip('\r\n')
767 
768  if _str[len(_str)-1] == "\\" and not OpenRTM_aist.isEscaped(_str, len(_str)-1):
769  #_str = _str[0:len(_str)-1]
770  tmp = [_str[0:len(_str)-1]]
771  OpenRTM_aist.eraseTailBlank(tmp)
772  #pline += _str
773  pline += tmp[0]
774  continue
775  pline += _str
776  if pline == "":
777  continue
778 
779  key = []
780  value = []
781  self.splitKeyValue(pline, key, value)
782  key[0] = OpenRTM_aist.unescape(key)
783  OpenRTM_aist.eraseHeadBlank(key)
784  OpenRTM_aist.eraseTailBlank(key)
785 
786  value[0] = OpenRTM_aist.unescape(value)
787  OpenRTM_aist.eraseHeadBlank(value)
788  OpenRTM_aist.eraseTailBlank(value)
789 
790  self.setProperty(key[0], value[0])
791  pline = ""
792 
793 
794 
817  def save(self, out, header):
818  self.store(out, header)
819  return
820 
821 
822 
905  def store(self, out, header):
906  out.write("#"+header+"\n")
907  self._store(out, "", self)
908 
909 
910  #============================================================
911  # other util functions
912  #============================================================
913 
914 
940  def propertyNames(self):
941  names = []
942  for leaf in self.leaf:
943  self._propertyNames(names, leaf.name, leaf)
944  return names
945 
946 
947 
960  def size(self):
961  return len(self.propertyNames())
962 
963 
964 
971  def findNode(self, key):
972  if not key:
973  return None
974 
975  keys = []
976  self.split(key, '.', keys)
977  return self._getNode(keys, 0, self)
978 
979 
980 
994  def getNode(self, key):
995  if not key:
996  return self
997 
998  leaf = self.findNode(key)
999  if leaf:
1000  return leaf
1001 
1002  self.createNode(key)
1003  return self.findNode(key)
1004 
1005 
1006 
1022  def createNode(self, key):
1023  if not key:
1024  return False
1025 
1026  if self.findNode(key):
1027  return False
1028 
1029  self.setProperty(key,"")
1030  return True
1031 
1032 
1033 
1048  def removeNode(self, leaf_name):
1049  len_ = len(self.leaf)
1050  for i in range(len_):
1051  idx = (len_ - 1) - i
1052  if self.leaf[idx].name == leaf_name:
1053  prop = self.leaf[idx]
1054  del self.leaf[idx]
1055  return prop
1056  return None
1057 
1058 
1059 
1074  def hasKey(self, key):
1075  for leaf in self.leaf:
1076  if leaf.name == key:
1077  return leaf
1078 
1079  return None
1080 
1081 
1082 
1091  def clear(self):
1092  len_ = len(self.leaf)
1093  for i in range(len_):
1094  if self.leaf[-1]:
1095  del self.leaf[-1]
1096 
1097  return
1098 
1099 
1100 
1114  def mergeProperties(self, prop):
1115  keys = prop.propertyNames()
1116 
1117  for i in range(prop.size()):
1118  self.setProperty(keys[i], prop.getProperty(keys[i]))
1119 
1120  return self
1121 
1122 
1123 
1144  def splitKeyValue(self, _str, key, value):
1145  i = 0
1146  length = len(_str)
1147 
1148  while i < length:
1149  if (_str[i] == ":" or _str[i] == "=") and not OpenRTM_aist.isEscaped(_str, i):
1150  key.append(_str[0:i])
1151  value.append(_str[i+1:])
1152  return
1153  i += 1
1154 
1155  # If no ':' or '=' exist, ' ' would be delimiter.
1156  i = 0
1157  while i < length:
1158  if (_str[i] == " ") and not OpenRTM_aist.isEscaped(_str, i):
1159  key.append(_str[0:i])
1160  value.append(_str[i+1:])
1161  return
1162  i += 1
1163 
1164  key.append(_str)
1165  value.append("")
1166  return
1167 
1168 
1169 
1188  def split(self, _str, delim, value):
1189  if _str == "":
1190  return False
1191 
1192  begin_it = end_it = 0
1193 
1194  length = len(_str)
1195 
1196  while end_it < length:
1197  if _str[end_it] == delim and not OpenRTM_aist.isEscaped(_str, end_it):
1198  value.append(_str[begin_it:end_it])
1199  begin_it = end_it + 1
1200  end_it += 1
1201 
1202  value.append(_str[begin_it:end_it])
1203  return True
1204 
1205 
1206 
1225  def _getNode(self, keys, index, curr):
1226  next = curr.hasKey(keys[index])
1227  if next is None:
1228  return None
1229 
1230  if index < (len(keys) - 1):
1231  index+=1
1232  return next._getNode(keys, index, next)
1233  else:
1234  return next
1235 
1236  return None
1237 
1238 
1239 
1253  def _propertyNames(self, names, curr_name, curr):
1254  if len(curr.leaf) > 0:
1255  for i in range(len(curr.leaf)):
1256  next_name = curr_name+"."+curr.leaf[i].name
1257  self._propertyNames(names, next_name, curr.leaf[i])
1258  else:
1259  names.append(curr_name)
1260 
1261  return
1262 
1263 
1264 
1278  def _store(self, out, curr_name, curr):
1279  if len(curr.leaf) > 0:
1280  for i in range(len(curr.leaf)):
1281  if curr_name == "":
1282  next_name = curr.leaf[i].name
1283  else:
1284  next_name = curr_name+"."+curr.leaf[i].name
1285  self._store(out, next_name, curr.leaf[i])
1286 
1287  else:
1288  val = curr.value
1289  if val == "":
1290  val = curr.default_value
1291  out.write(curr_name+": "+val+"\n")
1292 
1293  return
1294 
1295 
1296 
1311  def indent(self, index):
1312  space = ""
1313 
1314  for i in range(index-1):
1315  space += " "
1316 
1317  return space
1318 
1319 
1320 
1336  def _dump(self, out, curr, index):
1337  if index != 0:
1338  #ut.write(self.indent(index)+"- "+curr.name)
1339  out[0]+=self.indent(index)+"- "+curr.name
1340 
1341  if curr.leaf == []:
1342  if curr.value == "":
1343  #out.write(": "+curr.default_value+"\n")
1344  out[0]+=": "+curr.default_value+"\n"
1345  else:
1346  #out.write(": "+curr.value+"\n")
1347  out[0]+=": "+str(curr.value)+"\n"
1348  return out[0]
1349 
1350  if index != 0:
1351  #out.write("\n")
1352  out[0]+="\n"
1353 
1354  for i in range(len(curr.leaf)):
1355  self._dump(out, curr.leaf[i], index + 1)
1356 
1357  return out[0]
1358 
1359 
1360 
1375  def __str__(self):
1376  string=[""]
1377  return self._dump(string, self, 0)
1378 
1379 
def removeNode(self, leaf_name)
Get node of Properties.
Definition: Properties.py:1048
def splitKeyValue(self, _str, key, value)
Definition: Properties.py:1144
def clear(self)
If key exists in the children.
Definition: Properties.py:1091
def store(self, out, header)
Stores property list to the output stream.
Definition: Properties.py:905
def assigmentOperator(self, prop)
Assignment operator.
Definition: Properties.py:231
def size(self)
Get number of Properties.
Definition: Properties.py:960
def split(self, _str, delim, value)
Definition: Properties.py:1188
def hasKey(self, key)
If key exists in the children.
Definition: Properties.py:1074
def list(self, out)
Prints this property list out to the specified output stream.
Definition: Properties.py:586
def getDefault(self, key)
Set value as the default value to specified key&#39;s property.
Definition: Properties.py:423
The Properties class represents a persistent set of properties.
Definition: Properties.py:83
def getProperty(self, key, default=None)
Searches for the property with the specified key in this property.
Definition: Properties.py:385
def save(self, out, header)
Save the properties list to the stream.
Definition: Properties.py:817
def load(self, inStream)
Loads property list consists of key:value from input stream.
Definition: Properties.py:753
def __init__(self, key=None, value=None, defaults_map=None, defaults_str=None, num=None, prop=None)
Constructor.
Definition: Properties.py:164
def mergeProperties(self, prop)
Merge properties.
Definition: Properties.py:1114
def _getNode(self, keys, index, curr)
Definition: Properties.py:1225
def _dump(self, out, curr, index)
Definition: Properties.py:1336
def setDefault(self, key, value)
Sets a default value associated with key in the property list.
Definition: Properties.py:501
def __del__(self)
Destructor.
Definition: Properties.py:261
def findNode(self, key)
Find node of properties Properties* const Properties::findNode(const std::string& key) const...
Definition: Properties.py:971
def propertyNames(self)
Returns an vector of all the keys in this property.
Definition: Properties.py:940
def _store(self, out, curr_name, curr)
Definition: Properties.py:1278
def setProperty(self, key, value=None)
Sets a value associated with key in the property list.
Definition: Properties.py:463
def getNode(self, key)
Get node of Properties.
Definition: Properties.py:994
def _propertyNames(self, names, curr_name, curr)
Definition: Properties.py:1253
def setDefaults(self, defaults, num=None)
Sets a default value associated with key in the property list.
Definition: Properties.py:536


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:06