path.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 Functions for parsing paths specifying name servers, directories, components,
17 etc.
18 
19 '''
20 
21 
22 from rtctree.exceptions import BadPathError
23 
24 
25 
27 
28 def parse_path(path):
29  '''Parses an address into directory and port parts.
30 
31  The last segment of the address will be checked to see if it matches a port
32  specification (i.e. contains a colon followed by text). This will be
33  returned separately from the directory parts.
34 
35  If a leading / is given, that will be returned as the first directory
36  component. All other / characters are removed.
37 
38  All leading / characters are condensed into a single leading /.
39 
40  Any path components that are . will be removed, as they just point to the
41  previous path component. For example, '/localhost/.' will become
42  '/localhost'. Any path components that are .. will be removed, along with
43  the previous path component. If this renders the path empty, it will be
44  replaced with '/'.
45 
46  Examples:
47 
48  'localhost:30000/manager/comp0.rtc' ->
49  (['localhost:30000', 'manager', 'comp0.rtc'], None)
50  'localhost/manager/comp0.rtc:in' ->
51  (['localhost', 'manager', 'comp0.rtc'], 'in')
52  '/localhost/manager/comp0.rtc' ->
53  (['/', 'localhost', 'manager', 'comp0.rtc'], None)
54  '/localhost/manager/comp0.rtc:in' ->
55  (['/', 'localhost', 'manager', 'comp0.rtc'], 'in')
56  'manager/comp0.rtc' ->
57  (['manager', 'comp0.rtc'], None)
58  'comp0.rtc' ->
59  (['comp0.rtc'], None)
60 
61  '''
62  bits = path.lstrip('/').split('/')
63  if not bits:
64  raise BadPathError(path)
65 
66  if bits[-1]:
67  bits[-1], port = get_port(bits[-1])
68  else:
69  port = None
70  if path[0] == '/':
71  bits = ['/'] + bits
72  condensed_bits = []
73  for bit in bits:
74  if bit == '.':
75  continue
76  if bit == '..':
77  condensed_bits = condensed_bits[:-1]
78  continue
79  condensed_bits.append(bit)
80  if not condensed_bits:
81  condensed_bits = ['/']
82  return condensed_bits, port
83 
84 
85 def get_port(path):
86  split_path = path.split(':')
87  if len(split_path) == 1:
88  return split_path[0], None
89  elif len(split_path) == 2:
90  return split_path[0], split_path[1]
91  else:
92  raise BadPathError(path)
93 
94 
95 def format_path(path):
96  '''Formats a path as a string, placing / between each component.
97 
98  @param path A path in rtctree format, as a tuple with the port name as the
99  second component.
100 
101  '''
102  if path[1]:
103  port = ':' + path[1]
104  else:
105  port = ''
106  if type(path[0]) is str:
107  # Don't add slashes if the path is singular
108  return path[0] + port
109  if path[0][0] == '/':
110  starter = '/'
111  path = path[0][1:]
112  else:
113  starter = ''
114  path = path[0]
115  return starter + '/'.join(path) + port
116 
117 
118 # vim: tw=79
119 
def get_port(path)
Definition: path.py:85
def parse_path(path)
API functions.
Definition: path.py:28
def format_path(path)
Definition: path.py:95


rtctree
Author(s): Geoffrey Biggs
autogenerated on Mon Feb 28 2022 23:39:40