app/soc/logic/path_link_name.py
author Todd Larsen <tlarsen@google.com>
Thu, 20 Nov 2008 18:50:30 +0000
changeset 512 aae25d2b4464
parent 462 1f164cd0529b
child 513 3c1e16637ad7
permissions -rw-r--r--
Rename link_name to link_id everywhere, regardless of case (so LINK_NAME becomes LINK_ID, "link name" becomes "link id", etc.). Also, rename LINKNAME to LINK_ID. Patch by: Todd Larsen
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
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    17
"""Path and link ID manipulation functions.
298
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>',
462
1f164cd0529b Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 409
diff changeset
    22
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
298
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
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
import re
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
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
# start with ASCII digit or lowercase
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
#   (additional ASCII digit or lowercase
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
#     -OR-
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
#   underscore and ASCII digit or lowercase)
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
#     zero or more of OR group
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    34
LINK_ID_PATTERN_CORE = r'[0-9a-z](?:[0-9a-z]|_[0-9a-z])*'
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    35
LINK_ID_ARG_PATTERN = r'(?P<link_id>%s)' % LINK_ID_PATTERN_CORE
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    36
LINK_ID_PATTERN = r'^%s$' % LINK_ID_PATTERN_CORE
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    37
LINK_ID_REGEX = re.compile(LINK_ID_PATTERN)
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    39
# partial path is multiple link_id chunks,
462
1f164cd0529b Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 409
diff changeset
    40
# each separated by a trailing /
1f164cd0529b Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 409
diff changeset
    41
# (at least 1)
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    42
PARTIAL_PATH_ARG_PATTERN = (r'(?P<partial_path>%(link_id)s'
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    43
                             '(?:/%(link_id)s)*)' % {
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    44
                               'link_id': LINK_ID_PATTERN_CORE})
462
1f164cd0529b Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 409
diff changeset
    45
PARTIAL_PATH_PATTERN = r'^%s$' % PARTIAL_PATH_ARG_PATTERN
1f164cd0529b Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 409
diff changeset
    46
PARTIAL_PATH_REGEX = re.compile(PARTIAL_PATH_PATTERN)
1f164cd0529b Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 409
diff changeset
    47
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    48
# path is multiple link_id chunks,
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
#   each separated by a trailing /
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
#     (at least 1)
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    51
# followed by a single link_id with no trailing /
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    52
PATH_LINK_ID_ARGS_PATTERN = (
462
1f164cd0529b Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 409
diff changeset
    53
    r'%(partial_path)s/'
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    54
     '(?P<link_id>%(link_id)s)' % {
462
1f164cd0529b Adds a check to logic/valididate.py that checks the partial path format using a regexp. This regexp has been added to logic/path_link_name.py, therefore changing PATH_LINKNAME_REGEX to use the PARTIAL_PATH in building this regexp. Plus views/models/docs.py now uses the validation functions in the clean_ methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 409
diff changeset
    55
       'partial_path' : PARTIAL_PATH_ARG_PATTERN,
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    56
       'link_id': LINK_ID_PATTERN_CORE})
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    57
PATH_LINK_ID_PATTERN = r'^%s$' % PATH_LINK_ID_ARGS_PATTERN
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    58
PATH_LINK_ID_REGEX = re.compile(PATH_LINK_ID_PATTERN)
298
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
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
def getPartsFromPath(path):
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    62
  """Splits path string into partial_path and link_id.
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
  
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
  Returns:
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
    {'partial_path': 'everything/but',
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    66
     'link_id': 'link_id'}
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    67
    or {} (empty dict) if string did not match PATH_LINK_ID_PATTERN.
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
  """
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    69
  path_link_name_match = PATH_LINK_ID_REGEX.match(path)
298
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
  if not path_link_name_match:
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
    return {}
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
  return path_link_name_match.groupdict()
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
def combinePath(path_parts):
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
  """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
    79
  
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
  Args:
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
    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
    82
      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
    83
      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
    84
        'a/complete/path/in/one/string'
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
        ['some', 'path', 'parts']
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 462
diff changeset
    86
        [['path', 'parts', 'and', 'a'], 'link ID']
298
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
  Returns:
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
    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
    90
    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
    91
    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
    92
  """
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
  if not path_parts:
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
    # completely empty input, so return early
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
    return None
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
  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
    98
    # 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
    99
    return path_parts
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
  
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
  flattened_parts = []
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
  
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
  for part in path_parts:
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
    if not part:
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
      # 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
   106
      return None    
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
  
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   108
    if isinstance(part, (list, tuple)):
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
      flattened_parts.extend(part)
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
    else:
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
      flattened_parts.append(part)
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
  return '/'.join(flattened_parts)