|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Path and link name manipulation functions. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Todd Larsen" <tlarsen@google.com>', |
|
22 ] |
|
23 |
|
24 |
|
25 import re |
|
26 |
|
27 |
|
28 # start with ASCII digit or lowercase |
|
29 # (additional ASCII digit or lowercase |
|
30 # -OR- |
|
31 # underscore and ASCII digit or lowercase) |
|
32 # zero or more of OR group |
|
33 LINKNAME_PATTERN_CORE = r'[0-9a-z](?:[0-9a-z]|_[0-9a-z])*' |
|
34 LINKNAME_ARG_PATTERN = r'(?P<link_name>%s)' % LINKNAME_PATTERN_CORE |
|
35 LINKNAME_PATTERN = r'^%s$' % LINKNAME_PATTERN_CORE |
|
36 LINKNAME_REGEX = re.compile(LINKNAME_PATTERN) |
|
37 |
|
38 # partial path is multiple link_name chunks, |
|
39 # each separated by a trailing / |
|
40 # (at least 1) |
|
41 # followed by a single link_name with no trailing / |
|
42 PATH_LINKNAME_ARGS_PATTERN = ( |
|
43 r'(?P<partial_path>%(link_name)s(?:/%(link_name)s)*)/' |
|
44 '(?P<link_name>%(link_name)s)' % { |
|
45 'link_name': LINKNAME_PATTERN_CORE}) |
|
46 |
|
47 PATH_LINKNAME_PATTERN = r'^%s$' % PATH_LINKNAME_ARGS_PATTERN |
|
48 PATH_LINKNAME_REGEX = re.compile(PATH_LINKNAME_PATTERN) |
|
49 |
|
50 |
|
51 def getPartsFromPath(path): |
|
52 """Splits path string into partial_path and link_name. |
|
53 |
|
54 Returns: |
|
55 {'partial_path': 'everything/but', |
|
56 'link_name': 'link_name'} |
|
57 or {} (empty dict) if string did not match PATH_LINKNAME_PATTERN. |
|
58 """ |
|
59 path_link_name_match = PATH_LINKNAME_REGEX.match(path) |
|
60 |
|
61 if not path_link_name_match: |
|
62 return {} |
|
63 |
|
64 return path_link_name_match.groupdict() |
|
65 |
|
66 |
|
67 def combinePath(path_parts): |
|
68 """Returns path components combined into a single string. |
|
69 |
|
70 Args: |
|
71 path_parts: a single path string, or a list of path part strings, |
|
72 or a nested list of path part strings (where the zeroeth element in |
|
73 the list is itself a list); for example: |
|
74 'a/complete/path/in/one/string' |
|
75 ['some', 'path', 'parts'] |
|
76 [['path', 'parts', 'and', 'a'], 'link name'] |
|
77 |
|
78 Returns: |
|
79 None if path_parts is False (None, empty string, etc.) or if |
|
80 any list elements are False (an empty list, empty string, etc.); |
|
81 otherwise, the combined string with the necessary separators. |
|
82 """ |
|
83 if not path_parts: |
|
84 # completely empty input, so return early |
|
85 return None |
|
86 |
|
87 if not isinstance(path_parts, (list, tuple)): |
|
88 # a single path string, so just return it as-is (nothing to do) |
|
89 return path_parts |
|
90 |
|
91 flattened_parts = [] |
|
92 |
|
93 for part in path_parts: |
|
94 if not part: |
|
95 # encountered a "False" element, which invalidates everything else |
|
96 return None |
|
97 |
|
98 if isinstance(part, (list, tuple)): |
|
99 flattened_parts.extend(part) |
|
100 else: |
|
101 flattened_parts.append(part) |
|
102 |
|
103 return '/'.join(flattened_parts) |