github.com/google/grumpy@v0.0.0-20171122020858-3ec87959189c/lib/os/path.py (about)

     1  # Copyright 2016 Google Inc. All Rights Reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  """"Utilities for manipulating and inspecting OS paths."""
    16  
    17  from '__go__/os' import Stat
    18  from '__go__/path/filepath' import Abs, Base, Clean, Dir as dirname, IsAbs as isabs, Join, Split  # pylint: disable=g-multiple-import,unused-import
    19  
    20  
    21  def abspath(path):
    22    result, err = Abs(path)
    23    if err:
    24      raise OSError(err.Error())
    25    if isinstance(path, unicode):
    26      # Grumpy compiler encoded the string into utf-8, so the result can be
    27      # decoded using utf-8.
    28      return unicode(result, 'utf-8')
    29    return result
    30  
    31  
    32  def basename(path):
    33    return '' if path.endswith('/') else Base(path)
    34  
    35  
    36  def exists(path):
    37    _, err = Stat(path)
    38    return err is None
    39  
    40  
    41  def isdir(path):
    42    info, err = Stat(path)
    43    if info and err is None:
    44      return info.Mode().IsDir()
    45    return False
    46  
    47  
    48  def isfile(path):
    49    info, err = Stat(path)
    50    if info and err is None:
    51      return info.Mode().IsRegular()
    52    return False
    53  
    54  
    55  # NOTE(compatibility): This method uses Go's filepath.Join() method which
    56  # implicitly normalizes the resulting path (pruning extra /, .., etc.) The usual
    57  # CPython behavior is to leave all the cruft. This deviation is reasonable
    58  # because a) result paths will point to the same files and b) one cannot assume
    59  # much about the results of join anyway since it's platform dependent.
    60  def join(*paths):
    61    if not paths:
    62      raise TypeError('join() takes at least 1 argument (0 given)')
    63    parts = []
    64    for p in paths:
    65      if isabs(p):
    66        parts = [p]
    67      else:
    68        parts.append(p)
    69    result = Join(*parts)
    70    if result and not paths[-1]:
    71      result += '/'
    72    return result
    73  
    74  
    75  def normpath(path):
    76    result = Clean(path)
    77    if isinstance(path, unicode):
    78      return unicode(result, 'utf-8')
    79    return result
    80  
    81  
    82  def split(path):
    83    head, tail = Split(path)
    84    if len(head) > 1 and head[-1] == '/':
    85      head = head[:-1]
    86    return (head, tail)