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

DM-32467: Allow a Path object to be used in Butler constructor #596

Merged
merged 3 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/DM-32467.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Butler constructor can now take a `os.PathLike` object when the ``butler.yaml`` is not included in the path.
3 changes: 2 additions & 1 deletion python/lsst/daf/butler/_butlerConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

__all__ = ("ButlerConfig",)

import os
import copy
from typing import (
Optional,
Expand Down Expand Up @@ -83,7 +84,7 @@ def __init__(self, other: Optional[Union[str, ButlerURI, Config]] = None,
self.configDir = copy.copy(other.configDir)
return

if isinstance(other, str):
if isinstance(other, (str, os.PathLike)):
# This will only allow supported schemes
uri = ButlerURI(other)

Expand Down
3 changes: 2 additions & 1 deletion python/lsst/daf/butler/core/_butlerUri/_butlerUri.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import re
import shutil
import tempfile
import os

from random import Random
from pathlib import Path, PurePath, PurePosixPath
Expand Down Expand Up @@ -145,7 +146,7 @@ def __new__(cls, uri: Union[str, urllib.parse.ParseResult, ButlerURI, Path],
dirLike: bool = False
subclass: Optional[Type[ButlerURI]] = None

if isinstance(uri, Path):
if isinstance(uri, os.PathLike):
uri = str(uri)

# Record if we need to post process the URI components
Expand Down
20 changes: 20 additions & 0 deletions tests/test_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import random
import time
import socket
import pathlib

try:
import boto3
Expand Down Expand Up @@ -1146,6 +1147,25 @@ class PosixDatastoreButlerTestCase(FileDatastoreButlerTests, unittest.TestCase):
datastoreName = [f"FileDatastore@{BUTLER_ROOT_TAG}"]
registryStr = "/gen3.sqlite3"

def testPathConstructor(self):
"""Independent test of constructor using PathLike.
"""
butler = Butler(self.tmpConfigFile, run="ingest")
self.assertIsInstance(butler, Butler)

# And again with a Path object with the butler yaml
path = pathlib.Path(self.tmpConfigFile)
butler = Butler(path, writeable=False)
self.assertIsInstance(butler, Butler)

# And again with a Path object without the butler yaml
# (making sure we skip it if the tmp config doesn't end
# in butler.yaml -- which is the case for a subclass)
if self.tmpConfigFile.endswith("butler.yaml"):
path = pathlib.Path(os.path.dirname(self.tmpConfigFile))
butler = Butler(path, writeable=False)
self.assertIsInstance(butler, Butler)

def testExportTransferCopy(self):
"""Test local export using all transfer modes"""
storageClass = self.storageClassFactory.getStorageClass("StructuredDataNoComponents")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import unittest
import urllib.parse
import responses
import pathlib

try:
import boto3
Expand Down Expand Up @@ -63,6 +64,10 @@ def testFile(self):
self.assertFalse(uri.exists(), f"{uri} should not exist")
self.assertEqual(uri.ospath, file)

path = pathlib.Path(file)
uri = ButlerURI(path)
self.assertEqual(uri.ospath, file)

content = "abcdefghijklmnopqrstuv\n"
uri.write(content.encode())
self.assertTrue(os.path.exists(file), "File should exist locally")
Expand Down