Go to the documentation of this file.00001
00002
00003
00004 '''rtctree
00005
00006 Copyright (C) 2009-2014
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 Functions for parsing paths specifying name servers, directories, components,
00017 etc.
00018
00019 '''
00020
00021
00022 from rtctree.exceptions import BadPathError
00023
00024
00025
00026
00027
00028 def parse_path(path):
00029 '''Parses an address into directory and port parts.
00030
00031 The last segment of the address will be checked to see if it matches a port
00032 specification (i.e. contains a colon followed by text). This will be
00033 returned separately from the directory parts.
00034
00035 If a leading / is given, that will be returned as the first directory
00036 component. All other / characters are removed.
00037
00038 All leading / characters are condensed into a single leading /.
00039
00040 Any path components that are . will be removed, as they just point to the
00041 previous path component. For example, '/localhost/.' will become
00042 '/localhost'. Any path components that are .. will be removed, along with
00043 the previous path component. If this renders the path empty, it will be
00044 replaced with '/'.
00045
00046 Examples:
00047
00048 'localhost:30000/manager/comp0.rtc' ->
00049 (['localhost:30000', 'manager', 'comp0.rtc'], None)
00050 'localhost/manager/comp0.rtc:in' ->
00051 (['localhost', 'manager', 'comp0.rtc'], 'in')
00052 '/localhost/manager/comp0.rtc' ->
00053 (['/', 'localhost', 'manager', 'comp0.rtc'], None)
00054 '/localhost/manager/comp0.rtc:in' ->
00055 (['/', 'localhost', 'manager', 'comp0.rtc'], 'in')
00056 'manager/comp0.rtc' ->
00057 (['manager', 'comp0.rtc'], None)
00058 'comp0.rtc' ->
00059 (['comp0.rtc'], None)
00060
00061 '''
00062 bits = path.lstrip('/').split('/')
00063 if not bits:
00064 raise BadPathError(path)
00065
00066 if bits[-1]:
00067 bits[-1], port = get_port(bits[-1])
00068 else:
00069 port = None
00070 if path[0] == '/':
00071 bits = ['/'] + bits
00072 condensed_bits = []
00073 for bit in bits:
00074 if bit == '.':
00075 continue
00076 if bit == '..':
00077 condensed_bits = condensed_bits[:-1]
00078 continue
00079 condensed_bits.append(bit)
00080 if not condensed_bits:
00081 condensed_bits = ['/']
00082 return condensed_bits, port
00083
00084
00085 def get_port(path):
00086 split_path = path.split(':')
00087 if len(split_path) == 1:
00088 return split_path[0], None
00089 elif len(split_path) == 2:
00090 return split_path[0], split_path[1]
00091 else:
00092 raise BadPathError(path)
00093
00094
00095 def format_path(path):
00096 '''Formats a path as a string, placing / between each component.
00097
00098 @param path A path in rtctree format, as a tuple with the port name as the
00099 second component.
00100
00101 '''
00102 if path[1]:
00103 port = ':' + path[1]
00104 else:
00105 port = ''
00106 if type(path[0]) is str:
00107
00108 return path[0] + port
00109 if path[0][0] == '/':
00110 starter = '/'
00111 path = path[0][1:]
00112 else:
00113 starter = ''
00114 path = path[0]
00115 return starter + '/'.join(path) + port
00116
00117
00118
00119