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