Hiroaki Itoh os.path alternative (force using forward slash rather than backslash, even if on windows)

Updated by Hiroaki Itoh

File fwspath.py Modified

  • Ignore whitespace
  • Hide word diff
 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
            "basename","dirname","commonprefix","getsize","getmtime",
            "getatime","getctime", "islink","exists","lexists","isdir","isfile",
-           "ismount","walk","expanduser","expandvars","normpath","abspath",
+           "ismount","expanduser","expandvars","normpath","abspath",
            "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
            "extsep","devnull","realpath","supports_unicode_filenames","relpath"]
 
 isdir = os.path.isdir
 isfile = os.path.isfile
 ismount = os.path.ismount
-walk = os.path.walk
+#walk = os.path.walk  # Python 3 doesn't have "os.path.walk"
 sep = os.path.sep
 pathsep = os.path.pathsep
 defpath = os.path.defpath
 extsep = os.path.extsep
 devnull = os.path.devnull
 supports_unicode_filenames = os.path.supports_unicode_filenames
-
-
-#def normdir(path):
-#    path = normpath(path)
-#    if not path:
-#        return "./"
-#    if not path.endswith("/"):
-#        path += "/"
-#    return path
Updated by Hiroaki Itoh

File fwspath.py Modified

  • Ignore whitespace
  • Hide word diff
 supports_unicode_filenames = os.path.supports_unicode_filenames
 
 
-def normdir(path):
-    path = normpath(path)
-    if not path:
-        return "./"
-    if not path.endswith("/"):
-        path += "/"
-    return path
+#def normdir(path):
+#    path = normpath(path)
+#    if not path:
+#        return "./"
+#    if not path.endswith("/"):
+#        path += "/"
+#    return path
Updated by Hiroaki Itoh

File fwspath.py Modified

  • Ignore whitespace
  • Hide word diff
 
 import os
 from functools import wraps
+import sys
 
 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
            "basename","dirname","commonprefix","getsize","getmtime",
 #
 #
 #
-def _decorator1(func):
-    @wraps(func)
-    def wrapper(*args, **kwargs):
-        return func(*args, **kwargs).replace(os.sep, "/")
-    return wrapper
+if sys.platform == 'win32':
+    def _deco4str(func):
+        @wraps(func)
+        def wrapper(*args, **kwargs):
+            return func(*args, **kwargs).replace(os.sep, "/")
+        return wrapper
 
-def _decorator2(func):
-    @wraps(func)
-    def wrapper(*args, **kwargs):
-        return tuple((s.replace(os.sep, "/")
-                 for s in func(*args, **kwargs)))
-    return wrapper
+    def _deco4tpl(func):
+        @wraps(func)
+        def wrapper(*args, **kwargs):
+            return tuple((s.replace(os.sep, "/")
+                     for s in func(*args, **kwargs)))
+        return wrapper
 
-normcase = _decorator1(os.path.normcase)
-join = _decorator1(os.path.join)
-splitdrive = _decorator2(os.path.splitdrive)
-split = _decorator2(os.path.split)
-splitext = _decorator2(os.path.splitext)
-dirname = _decorator1(os.path.dirname)
-commonprefix = _decorator1(os.path.commonprefix)
-expanduser = _decorator1(os.path.expanduser)
-expandvars = _decorator1(os.path.expandvars)
-normpath = _decorator1(os.path.normpath)
-abspath = _decorator1(os.path.abspath)
-splitunc = _decorator2(os.path.splitunc)
-realpath = _decorator1(os.path.realpath)
-relpath = _decorator1(os.path.relpath)
+    normcase = _deco4str(os.path.normcase)
+    join = _deco4str(os.path.join)
+    splitdrive = _deco4tpl(os.path.splitdrive)
+    split = _deco4tpl(os.path.split)
+    splitext = _deco4tpl(os.path.splitext)
+    dirname = _deco4str(os.path.dirname)
+    commonprefix = _deco4str(os.path.commonprefix)
+    expanduser = _deco4str(os.path.expanduser)
+    expandvars = _deco4str(os.path.expandvars)
+    normpath = _deco4str(os.path.normpath)
+    abspath = _deco4str(os.path.abspath)
+    splitunc = _deco4tpl(os.path.splitunc)
+    realpath = _deco4str(os.path.realpath)
+    relpath = _deco4str(os.path.relpath)
+else:
+    normcase = os.path.normcase
+    join = os.path.join
+    splitdrive = os.path.splitdrive
+    split = os.path.split
+    splitext = os.path.splitext
+    dirname = os.path.dirname
+    commonprefix = os.path.commonprefix
+    expanduser = os.path.expanduser
+    expandvars = os.path.expandvars
+    normpath = os.path.normpath
+    abspath = os.path.abspath
+    splitunc = os.path.splitunc
+    realpath = os.path.realpath
+    relpath = os.path.relpath
 
 curdir = os.path.curdir
 pardir = os.path.pardir
Created by Hiroaki Itoh

File fwspath.py Added

  • Ignore whitespace
  • Hide word diff
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017, Hiroaki Itoh <https://bitbucket.org/hhsprings/>
+# All rights reserved.
+# 
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+#
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in
+#   the documentation and/or other materials provided with the
+#   distribution.
+#
+# - Neither the name of the xwhhsprings nor the names of its contributors
+#   may be used to endorse or promote products derived from this software
+#   without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+from __future__ import unicode_literals
+
+import os
+from functools import wraps
+
+__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
+           "basename","dirname","commonprefix","getsize","getmtime",
+           "getatime","getctime", "islink","exists","lexists","isdir","isfile",
+           "ismount","walk","expanduser","expandvars","normpath","abspath",
+           "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
+           "extsep","devnull","realpath","supports_unicode_filenames","relpath"]
+
+# ----------------------------------------
+#
+#
+#
+def _decorator1(func):
+    @wraps(func)
+    def wrapper(*args, **kwargs):
+        return func(*args, **kwargs).replace(os.sep, "/")
+    return wrapper
+
+def _decorator2(func):
+    @wraps(func)
+    def wrapper(*args, **kwargs):
+        return tuple((s.replace(os.sep, "/")
+                 for s in func(*args, **kwargs)))
+    return wrapper
+
+normcase = _decorator1(os.path.normcase)
+join = _decorator1(os.path.join)
+splitdrive = _decorator2(os.path.splitdrive)
+split = _decorator2(os.path.split)
+splitext = _decorator2(os.path.splitext)
+dirname = _decorator1(os.path.dirname)
+commonprefix = _decorator1(os.path.commonprefix)
+expanduser = _decorator1(os.path.expanduser)
+expandvars = _decorator1(os.path.expandvars)
+normpath = _decorator1(os.path.normpath)
+abspath = _decorator1(os.path.abspath)
+splitunc = _decorator2(os.path.splitunc)
+realpath = _decorator1(os.path.realpath)
+relpath = _decorator1(os.path.relpath)
+
+curdir = os.path.curdir
+pardir = os.path.pardir
+isabs = os.path.isabs
+basename = os.path.basename
+getsize = os.path.getsize
+getmtime = os.path.getmtime
+getatime = os.path.getatime
+getctime = os.path.getctime
+islink = os.path.islink
+exists = os.path.exists
+lexists = os.path.lexists
+isdir = os.path.isdir
+isfile = os.path.isfile
+ismount = os.path.ismount
+walk = os.path.walk
+sep = os.path.sep
+pathsep = os.path.pathsep
+defpath = os.path.defpath
+altsep = os.path.altsep
+extsep = os.path.extsep
+devnull = os.path.devnull
+supports_unicode_filenames = os.path.supports_unicode_filenames
+
+
+def normdir(path):
+    path = normpath(path)
+    if not path:
+        return "./"
+    if not path.endswith("/"):
+        path += "/"
+    return path