utils.py
Go to the documentation of this file.
00001 # -*- Python -*-
00002 # -*- coding: utf-8 -*-
00003 
00004 '''rtsprofile
00005 
00006 Copyright (C) 2009-2010
00007     Geoffrey Biggs
00008     RT-Synthesis Research Group
00009     Intelligent Systems Research Institute,
00010     National Institute of Advanced Industrial Science and Technology (AIST),
00011     Japan
00012     All rights reserved.
00013 Licensed under the Eclipse Public License -v 1.0 (EPL)
00014 http://www.opensource.org/licenses/eclipse-1.0.txt
00015 
00016 File: utils.py
00017 
00018 Utility functions.
00019 
00020 '''
00021 
00022 __version__ = '$Revision: $'
00023 # $Source$
00024 
00025 
00026 import new
00027 import re
00028 import time
00029 
00030 from rtsprofile import RTS_EXT_NS, RTS_EXT_NS_S
00031 from rtsprofile.exceptions import InvalidTypeError, RequiredAttributeError
00032 
00033 
00034 ###############################################################################
00035 ## Public API functions
00036 
00037 def date_to_dict(date):
00038     date = date.split('T')
00039     t = time.strptime(date[0], '%Y-%m-%d')
00040     year = t.tm_year
00041     month = t.tm_mon
00042     day = t.tm_mday
00043     t = time.strptime(date[1], '%H:%M:%S')
00044     hour = t.tm_hour
00045     min = t.tm_min
00046     sec = t.tm_sec
00047     return {'year': year, 'month': month, 'day': day, 'hour': hour,
00048             'minute': min, 'second': sec}
00049 
00050 def get_direct_child_elements_xml(node, prefix=None, local_name=None):
00051     for c in node.childNodes:
00052         matches = False
00053         if prefix:
00054             if c.prefix == prefix:
00055                 matches = True
00056         if local_name:
00057             if c.localName == local_name:
00058                 matches = True
00059             else:
00060                 matches = False
00061         if matches:
00062             yield c
00063 
00064 
00065 def indent_string(string, num_spaces=2):
00066     '''Add indentation to a string.
00067 
00068     Replaces all new lines in the string with a new line followed by the
00069     specified number of spaces, and adds the specified number of spaces to the
00070     start of the string.
00071 
00072     '''
00073     indent = ' '.ljust(num_spaces)
00074     return indent + re.sub('\n', '\n' + indent, string)
00075 
00076 
00077 def parse_properties_xml(node):
00078     name = node.getAttributeNS(RTS_EXT_NS, 'name')
00079     value = node.getAttributeNS(RTS_EXT_NS, 'value')
00080     if not value:
00081         return name, None
00082     else:
00083         return name, value
00084 
00085 
00086 def properties_to_xml(element, name, value=None):
00087     element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'name', name)
00088     if value:
00089         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'value', value)
00090 
00091 
00092 def validate_attribute(attr, name, expected_type=None, required=False):
00093     '''Validates that an attribute meets expectations.
00094 
00095     This function will check if the given attribute value matches a necessary
00096     type and/or is not None, an empty string, an empty list, etc. It will raise
00097     suitable exceptions on validation failure.
00098 
00099     @param attr The value to validate.
00100     @param name The attribute name to use in exceptions.
00101     @param expected_type The type the value must be. If None, no check is
00102     performed. If a list, attr must match one type in the list.
00103     @param required If the value must not be empty, e.g. not an empty string.
00104     @raises InvalidTypeError
00105     @raises RequiredAttributeError
00106 
00107     '''
00108     if expected_type:
00109         if type(expected_type) == list:
00110             if not _check_type(attr, expected_type):
00111                 raise InvalidTypeError(name, type(attr), expected_type)
00112         else:
00113             if not _check_type(attr, [expected_type]):
00114                 raise InvalidTypeError(name, type(attr), expected_type)
00115     if required and not attr:
00116         raise RequiredAttributeError(name)
00117 
00118 
00119 ##############################################################################
00120 ## Private functions
00121 
00122 def _check_type(value, expected_types):
00123     # Check if the type of value is one of those listed in expected_types
00124     for et in expected_types:
00125         if type(et) == type and type(value) == et:
00126             return True
00127         elif type(et) == new.classobj and \
00128                 value.__class__ == et:
00129             return True
00130     return False
00131 
00132 
00133 # vim: tw=79
00134 


rtsprofile
Author(s): Geoffrey Biggs
autogenerated on Thu Aug 27 2015 14:59:19