LSSTApplications  20.0.0
LSSTDataManagementBasePackage
Classes | Functions | Variables
lsst.pipe.base.argumentParser Namespace Reference

Classes

class  ArgumentParser
 
class  ConfigDatasetType
 
class  ConfigFileAction
 
class  ConfigValueAction
 
class  DataIdArgument
 
class  DataIdContainer
 
class  DatasetArgument
 
class  DynamicDatasetType
 
class  IdValueAction
 
class  InputOnlyArgumentParser
 
class  LogLevelAction
 
class  ReuseAction
 

Functions

def getTaskDict (config, taskDict=None, baseName="")
 
def obeyShowArgument (showOpts, config=None, exit=False)
 
def showTaskHierarchy (config)
 
def setDottedAttr (item, name, value)
 
def getDottedAttr (item, name)
 

Variables

string DEFAULT_INPUT_NAME = "PIPE_INPUT_ROOT"
 
string DEFAULT_CALIB_NAME = "PIPE_CALIB_ROOT"
 
string DEFAULT_OUTPUT_NAME = "PIPE_OUTPUT_ROOT"
 

Function Documentation

◆ getDottedAttr()

def lsst.pipe.base.argumentParser.getDottedAttr (   item,
  name 
)
Get an attribute (like `getattr` but accepts hierarchical names
such as ``foo.bar.baz``).

Parameters
----------
item : obj
    Object whose attribute is to be returned.
name : `str`
    Name of the attribute to get.

Returns
-------
itemAttr : obj
    If name is ``foo.bar.baz then the return value is
    ``item.foo.bar.baz``.

Definition at line 1325 of file argumentParser.py.

1325 def getDottedAttr(item, name):
1326  """Get an attribute (like `getattr` but accepts hierarchical names
1327  such as ``foo.bar.baz``).
1328 
1329  Parameters
1330  ----------
1331  item : obj
1332  Object whose attribute is to be returned.
1333  name : `str`
1334  Name of the attribute to get.
1335 
1336  Returns
1337  -------
1338  itemAttr : obj
1339  If name is ``foo.bar.baz then the return value is
1340  ``item.foo.bar.baz``.
1341  """
1342  subitem = item
1343  for subname in name.split("."):
1344  subitem = getattr(subitem, subname)
1345  return subitem

◆ getTaskDict()

def lsst.pipe.base.argumentParser.getTaskDict (   config,
  taskDict = None,
  baseName = "" 
)
Get a dictionary of task info for all subtasks in a config

Parameters
----------
config : `lsst.pex.config.Config`
    Configuration to process.
taskDict : `dict`, optional
    Users should not specify this argument. Supports recursion.
    If provided, taskDict is updated in place, else a new `dict`
    is started.
baseName : `str`, optional
    Users should not specify this argument. It is only used for
    recursion: if a non-empty string then a period is appended
    and the result is used as a prefix for additional entries
    in taskDict; otherwise no prefix is used.

Returns
-------
taskDict : `dict`
    Keys are config field names, values are task names.

Notes
-----
This function is designed to be called recursively.
The user should call with only a config (leaving taskDict and baseName
at their default values).

Definition at line 921 of file argumentParser.py.

921 def getTaskDict(config, taskDict=None, baseName=""):
922  """Get a dictionary of task info for all subtasks in a config
923 
924  Parameters
925  ----------
926  config : `lsst.pex.config.Config`
927  Configuration to process.
928  taskDict : `dict`, optional
929  Users should not specify this argument. Supports recursion.
930  If provided, taskDict is updated in place, else a new `dict`
931  is started.
932  baseName : `str`, optional
933  Users should not specify this argument. It is only used for
934  recursion: if a non-empty string then a period is appended
935  and the result is used as a prefix for additional entries
936  in taskDict; otherwise no prefix is used.
937 
938  Returns
939  -------
940  taskDict : `dict`
941  Keys are config field names, values are task names.
942 
943  Notes
944  -----
945  This function is designed to be called recursively.
946  The user should call with only a config (leaving taskDict and baseName
947  at their default values).
948  """
949  if taskDict is None:
950  taskDict = dict()
951  for fieldName, field in config.items():
952  if hasattr(field, "value") and hasattr(field, "target"):
953  subConfig = field.value
954  if isinstance(subConfig, pexConfig.Config):
955  subBaseName = f"{baseName}.{fieldName}" if baseName else fieldName
956  try:
957  taskName = f"{field.target.__module__}.{field.target.__name__}"
958  except Exception:
959  taskName = repr(field.target)
960  taskDict[subBaseName] = taskName
961  getTaskDict(config=subConfig, taskDict=taskDict, baseName=subBaseName)
962  return taskDict
963 
964 

◆ obeyShowArgument()

def lsst.pipe.base.argumentParser.obeyShowArgument (   showOpts,
  config = None,
  exit = False 
)
Process arguments specified with ``--show`` (but ignores
``"data"``).

Parameters
----------
showOpts : `list` of `str`
    List of options passed to ``--show``.
config : optional
    The provided config.
exit : bool, optional
    Exit if ``"run"`` isn't included in ``showOpts``.

Parameters
----------
Supports the following options in showOpts:

- ``config[=PAT]``. Dump all the config entries, or just the ones that
    match the glob pattern.
- ``history=PAT``. Show where the config entries that match the glob
    pattern were set.
- ``tasks``. Show task hierarchy.
- ``data``. Ignored; to be processed by caller.
- ``run``. Keep going (the default behaviour is to exit if
    ``--show`` is specified).

Calls ``sys.exit(1)`` if any other option found.

Definition at line 965 of file argumentParser.py.

965 def obeyShowArgument(showOpts, config=None, exit=False):
966  """Process arguments specified with ``--show`` (but ignores
967  ``"data"``).
968 
969  Parameters
970  ----------
971  showOpts : `list` of `str`
972  List of options passed to ``--show``.
973  config : optional
974  The provided config.
975  exit : bool, optional
976  Exit if ``"run"`` isn't included in ``showOpts``.
977 
978  Parameters
979  ----------
980  Supports the following options in showOpts:
981 
982  - ``config[=PAT]``. Dump all the config entries, or just the ones that
983  match the glob pattern.
984  - ``history=PAT``. Show where the config entries that match the glob
985  pattern were set.
986  - ``tasks``. Show task hierarchy.
987  - ``data``. Ignored; to be processed by caller.
988  - ``run``. Keep going (the default behaviour is to exit if
989  ``--show`` is specified).
990 
991  Calls ``sys.exit(1)`` if any other option found.
992  """
993  if not showOpts:
994  return
995 
996  for what in showOpts:
997  showCommand, showArgs = what.split("=", 1) if "=" in what else (what, "")
998 
999  if showCommand == "config":
1000  matConfig = re.search(r"^(?:config.)?(.+)?", showArgs)
1001  pattern = matConfig.group(1)
1002  if pattern:
1003  class FilteredStream:
1004  """A file object that only prints lines
1005  that match the glob "pattern".
1006 
1007  N.b. Newlines are silently discarded and reinserted;
1008  crude but effective.
1009  """
1010 
1011  def __init__(self, pattern):
1012  # obey case if pattern isn't lowecase or requests NOIGNORECASE
1013  mat = re.search(r"(.*):NOIGNORECASE$", pattern)
1014 
1015  if mat:
1016  pattern = mat.group(1)
1017  self._pattern = re.compile(fnmatch.translate(pattern))
1018  else:
1019  if pattern != pattern.lower():
1020  print(f"Matching {pattern!r} without regard to case "
1021  "(append :NOIGNORECASE to prevent this)", file=sys.stdout)
1022  self._pattern = re.compile(fnmatch.translate(pattern), re.IGNORECASE)
1023 
1024  def write(self, showStr):
1025  showStr = showStr.rstrip()
1026  # Strip off doc string line(s) and cut off
1027  # at "=" for string matching
1028  matchStr = showStr.split("\n")[-1].split("=")[0]
1029  if self._pattern.search(matchStr):
1030  print("\n" + showStr)
1031 
1032  fd = FilteredStream(pattern)
1033  else:
1034  fd = sys.stdout
1035 
1036  config.saveToStream(fd, "config")
1037  elif showCommand == "history":
1038  matHistory = re.search(r"^(?:config.)?(.+)?", showArgs)
1039  globPattern = matHistory.group(1)
1040  if not globPattern:
1041  print("Please provide a value with --show history (e.g. history=*.doXXX)", file=sys.stderr)
1042  sys.exit(1)
1043 
1044  error = False
1045  for i, pattern in enumerate(fnmatch.filter(config.names(), globPattern)):
1046  if i > 0:
1047  print("")
1048 
1049  pattern = pattern.split(".")
1050  cpath, cname = pattern[:-1], pattern[-1]
1051  hconfig = config # the config that we're interested in
1052  for i, cpt in enumerate(cpath):
1053  try:
1054  hconfig = getattr(hconfig, cpt)
1055  except AttributeError:
1056  config_path = ".".join(["config"] + cpath[:i])
1057  print(f"Error: configuration {config_path} has no subconfig {cpt}", file=sys.stderr)
1058  error = True
1059 
1060  try:
1061  print(pexConfig.history.format(hconfig, cname))
1062  except KeyError:
1063  config_path = ".".join(["config"] + cpath)
1064  print(f"Error: {config_path} has no field {cname}", file=sys.stderr)
1065  error = True
1066 
1067  if error:
1068  sys.exit(1)
1069 
1070  elif showCommand == "data":
1071  pass
1072  elif showCommand == "run":
1073  pass
1074  elif showCommand == "tasks":
1075  showTaskHierarchy(config)
1076  else:
1077  choices = "', '".join("config[=XXX] data history=XXX tasks run".split())
1078  print(f"Unknown value for show: {what} (choose from {choices!r})", file=sys.stderr)
1079  sys.exit(1)
1080 
1081  if exit and "run" not in showOpts:
1082  sys.exit(0)
1083 
1084 

◆ setDottedAttr()

def lsst.pipe.base.argumentParser.setDottedAttr (   item,
  name,
  value 
)
Set an instance attribute (like `setattr` but accepting
hierarchical names such as ``foo.bar.baz``).

Parameters
----------
item : obj
    Object whose attribute is to be set.
name : `str`
    Name of attribute to set.
value : obj
    New value for the attribute.

Notes
-----
For example if name is ``foo.bar.baz`` then ``item.foo.bar.baz``
is set to the specified value.

Definition at line 1300 of file argumentParser.py.

1300 def setDottedAttr(item, name, value):
1301  """Set an instance attribute (like `setattr` but accepting
1302  hierarchical names such as ``foo.bar.baz``).
1303 
1304  Parameters
1305  ----------
1306  item : obj
1307  Object whose attribute is to be set.
1308  name : `str`
1309  Name of attribute to set.
1310  value : obj
1311  New value for the attribute.
1312 
1313  Notes
1314  -----
1315  For example if name is ``foo.bar.baz`` then ``item.foo.bar.baz``
1316  is set to the specified value.
1317  """
1318  subitem = item
1319  subnameList = name.split(".")
1320  for subname in subnameList[:-1]:
1321  subitem = getattr(subitem, subname)
1322  setattr(subitem, subnameList[-1], value)
1323 
1324 

◆ showTaskHierarchy()

def lsst.pipe.base.argumentParser.showTaskHierarchy (   config)
Print task hierarchy to stdout.

Parameters
----------
config : `lsst.pex.config.Config`
    Configuration to process.

Definition at line 1085 of file argumentParser.py.

1085 def showTaskHierarchy(config):
1086  """Print task hierarchy to stdout.
1087 
1088  Parameters
1089  ----------
1090  config : `lsst.pex.config.Config`
1091  Configuration to process.
1092  """
1093  print("Subtasks:")
1094  taskDict = getTaskDict(config=config)
1095 
1096  fieldNameList = sorted(taskDict.keys())
1097  for fieldName in fieldNameList:
1098  taskName = taskDict[fieldName]
1099  print(f"{fieldName}: {taskName}")
1100 
1101 

Variable Documentation

◆ DEFAULT_CALIB_NAME

string lsst.pipe.base.argumentParser.DEFAULT_CALIB_NAME = "PIPE_CALIB_ROOT"

Definition at line 45 of file argumentParser.py.

◆ DEFAULT_INPUT_NAME

string lsst.pipe.base.argumentParser.DEFAULT_INPUT_NAME = "PIPE_INPUT_ROOT"

Definition at line 44 of file argumentParser.py.

◆ DEFAULT_OUTPUT_NAME

string lsst.pipe.base.argumentParser.DEFAULT_OUTPUT_NAME = "PIPE_OUTPUT_ROOT"

Definition at line 46 of file argumentParser.py.

lsst.pipe.base.argumentParser.showTaskHierarchy
def showTaskHierarchy(config)
Definition: argumentParser.py:1085
lsst.pipe.base.argumentParser.setDottedAttr
def setDottedAttr(item, name, value)
Definition: argumentParser.py:1300
lsst.pipe.base.argumentParser.obeyShowArgument
def obeyShowArgument(showOpts, config=None, exit=False)
Definition: argumentParser.py:965
lsst.pipe.tasks.mergeDetections.write
def write(self, patchRef, catalog)
Write the output.
Definition: mergeDetections.py:388
lsst.pipe.base.argumentParser.getTaskDict
def getTaskDict(config, taskDict=None, baseName="")
Definition: argumentParser.py:921
lsst.pipe.base.argumentParser.getDottedAttr
def getDottedAttr(item, name)
Definition: argumentParser.py:1325