Skip to content

Commit

Permalink
move replacement for pkg_resources.resource_filename() to wc_utils an…
Browse files Browse the repository at this point in the history
…d use it
  • Loading branch information
artgoldberg committed Jul 22, 2019
1 parent 028d818 commit 14e7212
Showing 1 changed file with 8 additions and 43 deletions.
51 changes: 8 additions & 43 deletions wc_lang/config/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,9 @@

import configobj
import os
from pathlib import Path
import wc_utils.config
import wc_utils.debug_logs.config


def get_package_root(file_in_package):
""" Get root directory of a package
Args:
file_in_package (:obj:`str`): pathname of a file in a package
Returns:
:obj:`str`: pathname of root of package
"""
path = Path(file_in_package)
# go up directory hierarchy from path and get first directory that does not contain '__init__.py'
dir = path.parent
found_package = False
while True:
if not dir.joinpath('__init__.py').is_file():
break
# exit at / root
if dir == dir.parent:
break
found_package = True
dir = dir.parent
if found_package:
return str(dir)


def get_resource_filename(*args):
""" Get pathname of resource file; replaces `pkg_resources.resource_filename`
Args:
args (:obj:`list`): pathname components of resource file
Returns:
:obj:`str`: pathname of resource file
"""
package_root = get_package_root(__file__)
return os.path.join(package_root, *args)
from wc_utils.config.core import AltResourceName


def get_config(extra=None):
Expand All @@ -59,11 +21,13 @@ def get_config(extra=None):
extra (:obj:`dict`, optional): additional configuration to override
Returns:
:obj:`configobj.ConfigObj`: nested dictionary with the configuration settings loaded from the configuration source(s).
:obj:`configobj.ConfigObj`: nested dictionary with the configuration settings loaded from the
configuration source(s).
"""
alt_resource_name = AltResourceName(__file__)
paths = wc_utils.config.ConfigPaths(
default=get_resource_filename('wc_lang', 'config/core.default.cfg'),
schema=get_resource_filename('wc_lang', 'config/core.schema.cfg'),
default=alt_resource_name.resource_filename('config/core.default.cfg'),
schema=alt_resource_name.resource_filename('config/core.schema.cfg'),
user=(
'wc_lang.cfg',
os.path.expanduser('~/.wc/wc_lang.cfg'),
Expand Down Expand Up @@ -118,7 +82,8 @@ def get_debug_logs_config(extra=None):
:obj:`configobj.ConfigObj`: nested dictionary with the configuration settings loaded from the configuration source(s).
"""
paths = wc_utils.debug_logs.config.paths.deepcopy()
paths.default = get_resource_filename('wc_lang', 'config/debug.default.cfg')
alt_resource_name = AltResourceName(__file__)
paths.default = alt_resource_name.resource_filename('config/debug.default.cfg')
paths.user = (
'wc_lang.debug.cfg',
os.path.expanduser('~/.wc/wc_lang.debug.cfg'),
Expand Down

0 comments on commit 14e7212

Please sign in to comment.