Changeset - 51907305852b
[Not reviewed]
0 3 0
Lance Edgar (lance) - 4 months ago 2024-07-02 09:38:45
lance@edbob.org
fix: avoid `pkg_resources` for `files.resource_path()`
3 files changed with 30 insertions and 16 deletions:
0 comments (0 inline, 0 general)
rattail/files.py
Show inline comments
 
@@ -2,7 +2,7 @@
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2023 Lance Edgar
 
#  Copyright © 2010-2024 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -26,6 +26,7 @@ File Utilities
 
This module contains various utility functions for use with the filesystem.
 
"""
 

	
 
import datetime
 
import io
 
import os
 
import shutil
 
@@ -33,9 +34,11 @@ import lockfile
 
import tempfile
 
import errno
 
import warnings
 
from datetime import datetime
 

	
 
import pkg_resources
 
try:
 
    import importlib.resources as importlib_resources
 
except ImportError:
 
    import importlib_resources
 

	
 
from rattail.exceptions import PathNotFound
 

	
 
@@ -72,7 +75,7 @@ def creation_time(path):
 
    """
 

	
 
    time = os.path.getctime(path)
 
    return datetime.fromtimestamp(time)
 
    return datetime.datetime.fromtimestamp(time)
 

	
 

	
 
def locking_copy(src, destdir, timeout=None):
 
@@ -204,23 +207,26 @@ def move_lpt(src, dst):
 

	
 
def resource_path(path):
 
    """
 
    Obtain a resource file path, extracting the resource and/or coercing the
 
    path as necessary.
 
    Obtain a resource file path, extracting the resource and/or
 
    coercing the path as necessary.
 

	
 
    :param path: May be either a package resource specifier, or a regular file
 
       path.
 
    If ``path`` is a package resource specifier, and the package
 
    containing it is a zipped egg, then the resource will be extracted
 
    and the resultant filename will be returned.
 

	
 
    :param path: May be either a package resource specifier, or a
 
       regular file path.
 
    :type path: string
 

	
 
    :returns: Absolute file path to the resource.
 
    :rtype: string
 

	
 
    If ``path`` is a package resource specifier, and the package containing it
 
    is a zipped egg, then the resource will be extracted and the resultant
 
    filename will be returned.
 
    """
 

	
 
    if not os.path.isabs(path) and ':' in path:
 
        return pkg_resources.resource_filename(*path.split(':'))
 
        package, filename = path.split(':')
 
        ref = importlib_resources.files(package) / filename
 
        with importlib_resources.as_file(ref) as path:
 
            return str(path)
 

	
 
    return path
 

	
 

	
setup.cfg
Show inline comments
 
@@ -39,6 +39,7 @@ install_requires =
 
        colander
 
        humanize
 
        importlib_metadata ; python_version < '3.8'
 
        importlib_resources ; python_version < '3.8'
 
        lockfile
 
        makefun
 
        Mako
tests/test_files.py
Show inline comments
 
# -*- coding: utf-8; -*-
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
import os
 
import shutil
 
import tempfile
 
@@ -36,3 +34,12 @@ class TestLockingCopy(unittest.TestCase):
 
        self.assertTrue(os.path.exists(dst_file))
 
        self.assertTrue(os.path.isfile(dst_file))
 
        self.assertFalse(os.path.exists(os.path.join(self.dstdir, 'somefile.lock')))
 

	
 

	
 
class TestResourcePath(unittest.TestCase):
 

	
 
    def test_basic(self):
 
        path = files.resource_path('rattail:files.py')
 
        self.assertTrue(path.endswith('rattail/files.py'))
 

	
 
        self.assertEqual(files.resource_path('/tmp/foo.txt'), '/tmp/foo.txt')
0 comments (0 inline, 0 general)