utils.py
Go to the documentation of this file.
1 # -*- Python -*-
2 # -*- coding: utf-8 -*-
3 
4 '''rtsprofile
5 
6 Copyright (C) 2009-2010
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 File: utils.py
17 
18 Utility functions.
19 
20 '''
21 
22 __version__ = '$Revision: $'
23 # $Source$
24 
25 
26 import new
27 import re
28 import time
29 
30 from rtsprofile import RTS_EXT_NS, RTS_EXT_NS_S
31 from rtsprofile.exceptions import InvalidTypeError, RequiredAttributeError
32 
33 
34 ###############################################################################
35 ## Public API functions
36 
37 def date_to_dict(date):
38  date = date.split('T')
39  t = time.strptime(date[0], '%Y-%m-%d')
40  year = t.tm_year
41  month = t.tm_mon
42  day = t.tm_mday
43  t = time.strptime(date[1], '%H:%M:%S')
44  hour = t.tm_hour
45  min = t.tm_min
46  sec = t.tm_sec
47  return {'year': year, 'month': month, 'day': day, 'hour': hour,
48  'minute': min, 'second': sec}
49 
50 def get_direct_child_elements_xml(node, prefix=None, local_name=None):
51  for c in node.childNodes:
52  matches = False
53  if prefix:
54  if c.prefix == prefix:
55  matches = True
56  if local_name:
57  if c.localName == local_name:
58  matches = True
59  else:
60  matches = False
61  if matches:
62  yield c
63 
64 
65 def indent_string(string, num_spaces=2):
66  '''Add indentation to a string.
67 
68  Replaces all new lines in the string with a new line followed by the
69  specified number of spaces, and adds the specified number of spaces to the
70  start of the string.
71 
72  '''
73  indent = ' '.ljust(num_spaces)
74  return indent + re.sub('\n', '\n' + indent, string)
75 
76 
78  name = node.getAttributeNS(RTS_EXT_NS, 'name')
79  value = node.getAttributeNS(RTS_EXT_NS, 'value')
80  if not value:
81  return name, None
82  else:
83  return name, value
84 
85 
86 def properties_to_xml(element, name, value=None):
87  element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'name', name)
88  if value:
89  element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'value', value)
90 
91 
92 def validate_attribute(attr, name, expected_type=None, required=False):
93  '''Validates that an attribute meets expectations.
94 
95  This function will check if the given attribute value matches a necessary
96  type and/or is not None, an empty string, an empty list, etc. It will raise
97  suitable exceptions on validation failure.
98 
99  @param attr The value to validate.
100  @param name The attribute name to use in exceptions.
101  @param expected_type The type the value must be. If None, no check is
102  performed. If a list, attr must match one type in the list.
103  @param required If the value must not be empty, e.g. not an empty string.
104  @raises InvalidTypeError
105  @raises RequiredAttributeError
106 
107  '''
108  if expected_type:
109  if type(expected_type) == list:
110  if not _check_type(attr, expected_type):
111  raise InvalidTypeError(name, type(attr), expected_type)
112  else:
113  if not _check_type(attr, [expected_type]):
114  raise InvalidTypeError(name, type(attr), expected_type)
115  if required and not attr:
116  raise RequiredAttributeError(name)
117 
118 
119 ##############################################################################
120 ## Private functions
121 
122 def _check_type(value, expected_types):
123  # Check if the type of value is one of those listed in expected_types
124  for et in expected_types:
125  if type(et) == type and type(value) == et:
126  return True
127  elif type(et) == new.classobj and \
128  value.__class__ == et:
129  return True
130  return False
131 
132 
133 # vim: tw=79
134 
def date_to_dict(date)
Public API functions.
Definition: utils.py:37
def properties_to_xml(element, name, value=None)
Definition: utils.py:86
def parse_properties_xml(node)
Definition: utils.py:77
def validate_attribute(attr, name, expected_type=None, required=False)
Definition: utils.py:92
def get_direct_child_elements_xml(node, prefix=None, local_name=None)
Definition: utils.py:50
def indent_string(string, num_spaces=2)
Definition: utils.py:65
def _check_type(value, expected_types)
Private functions.
Definition: utils.py:122


rtsprofile
Author(s): Geoffrey Biggs
autogenerated on Fri Jun 7 2019 21:52:35