Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PurePathBase syntax customisation #7

Closed
barneygale opened this issue Dec 16, 2023 · 1 comment
Closed

PurePathBase syntax customisation #7

barneygale opened this issue Dec 16, 2023 · 1 comment

Comments

@barneygale
Copy link
Owner

It should be possible to make path syntax and normalization more customizable. Very rough plan:

  • Move PurePathBase.pathmod to PurePath.pathmod
  • Ditto for drive and root?
  • Make PurePathbase.is_absolute() and is_reserved() abstract (or return False?), move existing implementations to PurePath
  • Add is_case_sensitive() abstract method (or class attribute, like sep below?)
  • Add PurePathBase.sep and altsep attributes (defaults "/" and None?)
@barneygale
Copy link
Owner Author

I've spent some time playing around with the above implementation sketch (i.e. making PurePathBase methods abstract, and moving pathmod into PurePath), and it feels pretty gross. The problem comes with mixing two mostly-independent ideas (syntax and I/O impl) in the same class hierarchy.

On reflection it seems obvious we should favour composition over inheritance; the bit I was missing is that we need to provide an abstract version of PurePathBase.pathmod, roughly:

class PathModuleBase:
    @classmethod
    def _unsupported(cls, attr):
        raise UnsupportedOperation(f"{cls.__name__}.{attr} is unsupported")

    @property
    def sep(self):
        return self._unsupported('sep')

    @property
    def altsep(self):
        return self._unsupported('altsep')

    def normcase(self, path):
        return self._unsupported('normcase()')

    def isabs(self, path):
        return self._unsupported('isabs()')

    def join(self, path, *paths):
        return self._unsupported('join()')

    def split(self, path):
        return self._unsupported('split()')

    def splitroot(self, path):
        return self._unsupported('splitroot()')

    def splitdrive(self, path):
        drive, root, rel = self.splitroot(path)
        return drive, root + rel

    def dirname(self, path):
        return self.split(path)[0]

    def basename(self, path):
        return self.split(path)[1]

Thus user subclasses of PurePathBase and PathBase would be able to set the pathmod class attribute to posixpath, ntpath, os.path, or their own subclass of PathModuleBase if they need fine-grained control over syntax.

This should be doable once #10 and #13 are resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant