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-23359: Add support for __file__ in config files #51

Merged
merged 2 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions python/lsst/pex/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ def load(self, filename, root="config"):
"""
with open(filename, "r") as f:
code = compile(f.read(), filename=filename, mode="exec")
self.loadFromStream(stream=code, root=root)
self.loadFromStream(stream=code, root=root, filename=filename)

def loadFromStream(self, stream, root="config", filename=None):
"""Modify this Config in place by executing the Python code in the
Expand Down Expand Up @@ -1017,9 +1017,10 @@ def loadFromStream(self, stream, root="config", filename=None):
lsst.pex.config.Config.saveFromStream
"""
with RecordingImporter() as importer:
globals = {"__file__": filename}
try:
local = {root: self}
exec(stream, {}, local)
exec(stream, globals, local)
except NameError as e:
if root == "config" and "root" in e.args[0]:
if filename is None:
Expand All @@ -1032,7 +1033,7 @@ def loadFromStream(self, stream, root="config", filename=None):
print(f"Config override file {filename!r}"
" appears to use 'root' instead of 'config'; trying with 'root'", file=sys.stderr)
local = {"root": self}
exec(stream, {}, local)
exec(stream, globals, local)
else:
raise

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exclude =
tests/.tests,
testLib.py,
ticket1914.py
tests/config/filename.py

[tool:pytest]
addopts = --flake8
Expand Down
3 changes: 3 additions & 0 deletions tests/config/filename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

config.filename = __file__
config.number = 5
52 changes: 52 additions & 0 deletions tests/test__file__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This file is part of pex_config.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This software is dual licensed under the GNU General Public License and also
# under a 3-clause BSD license. Recipients may choose which of these licenses
# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
# respectively. If you choose the GPL option then the following text applies
# (but note that there is still no warranty even if you opt for BSD instead):
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import unittest
import lsst.pex.config as pexConf

TESTDIR = os.path.dirname(os.path.abspath(__file__))


class FileConfig(pexConf.Config):
number = pexConf.Field("FileConfig.number", int, default=0)
filename = pexConf.Field("FileConfig.filename", str, default=None)


class FilenameTestCase(unittest.TestCase):
"""Check that __file__ can be used in a config file."""

def test__file(self):
c = FileConfig()
confFile = os.path.join(TESTDIR, "config", "filename.py")
c.load(confFile)
self.assertEqual(c.filename, confFile)
self.assertEqual(c.number, 5)


if __name__ == "__main__":
unittest.main()