Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7756

Re: os.path and Path

References <mailman.8.1308188800.1164.python-list@python.org> <4DF9AADE.6090609@gmail.com> <4df9b7be$0$29973$c3e8da3$5496439d@news.astraweb.com> <mailman.20.1308240105.1164.python-list@python.org> <4dfa324e$0$30002$c3e8da3$5496439d@news.astraweb.com>
Date 2011-06-16 11:21 -0600
Subject Re: os.path and Path
From Eric Snow <ericsnowcurrently@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.26.1308244867.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Jun 16, 2011 at 10:41 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>
> On a Linux or OS X box, you could have a file e inside a directory c:d
> inside b inside a. It can't be treated as platform independent, because
> c:d is not a legal path component under classic Mac or Windows.
>
> On a classic Mac (does anyone still use them?), you could have a file e
> inside a directory c/d inside b inside a. Likewise c/d isn't legal under
> POSIX or Windows.
>
> So there are paths that are legal under one file system, but not others,
> and hence there is no single normalization that can represent all legal
> paths under arbitrary file systems.
>

Perhaps one solution is to have the Path class accept registrations of
valid path formats:

    class PathFormat:
        @abstractmethod
        def map_path(self, pathstring):
            """Map the pathstring to the canonical path.

            This could take the form of some regex or an even a more
            explicit conversion.

            If there is no match, return None.

            """

        @abstractmethod
        def unmap_path(self, pathstring):
            """Map the pathstring from a canonical path to this format.

            If there is no match, return None.

            """

    class Path:
        ...
        _formats = []
        @classmethod
        def register_format(cls, format):
            cls._formats.append(format)

        def map_path(self, pathstring):
            for format in self._formats:
                result = format.map_path(pathstring)
                if result is None:
                    continue
                # remember which format matched?
                return result
            raise TypeError("No formatters could map the pathstring.")

        def unmap_path(self, pathstring):
            ...

With something like that, you have a PathFormat class for each
platform that matters.  Anyone would be able to add more, as they
like, through register_format.  This module could also include a few
lines to register a particular PathFormat depending on the platform
determined through sys.platform or whatever.

This way your path class doesn't have to try to worry about the
conversion to and from the canonical path format.

-eric

>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-15 19:00 -0700
  Re: os.path and Path Laurent Claessens <moky.math@gmail.com> - 2011-06-16 09:03 +0200
    Re: os.path and Path Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-16 07:58 +0000
      Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 09:16 -0700
        Re: os.path and Path Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-16 16:41 +0000
          Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 10:18 -0700
          Re: os.path and Path Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-16 11:21 -0600
      Re: os.path and Path Christian Heimes <lists@cheimes.de> - 2011-06-16 18:32 +0200
      Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 10:07 -0700
      Re: os.path and Path Chris Angelico <rosuav@gmail.com> - 2011-06-17 11:00 +1000
  Re: os.path and Path Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-16 07:14 +0000
    Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 09:05 -0700
      Re: os.path and Path Chris Torek <nospam@torek.net> - 2011-06-17 00:48 +0000
        Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 18:19 -0700
        Re: os.path and Path Ned Deily <nad@acm.org> - 2011-06-16 19:55 -0700
          Re: os.path and Path rusi <rustompmody@gmail.com> - 2011-06-16 21:24 -0700

csiph-web