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