Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of ap_pipe. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22__all__ = ["makeApdb"] 

23 

24import argparse 

25 

26from lsst.pipe.base import ConfigFileAction, ConfigValueAction 

27from lsst.ap.association import make_dia_object_schema, make_dia_source_schema 

28 

29from .ap_pipe import ApPipeConfig 

30 

31 

32class ConfigOnlyParser(argparse.ArgumentParser): 

33 """Argument parser that knows standard config arguments. 

34 """ 

35 

36 def __init__(self, description=None, **kwargs): 

37 if description is None: 

38 # Description must be readable in both Sphinx and make_apdb.py -h 

39 description = """\ 

40Create a Alert Production Database using config overrides for 

41`lsst.ap.pipe.ApPipeConfig`. 

42 

43This script takes the same ``--config`` and ``--configfile`` arguments as 

44command-line tasks. Calling ``ap_pipe.py`` with the same arguments uses the 

45newly created database. 

46 

47The config overrides must define ``apdb.db_url`` to create a valid config. 

48""" 

49 

50 super().__init__(description=description, **kwargs) 

51 

52 self.add_argument("-c", "--config", nargs="*", action=ConfigValueAction, 

53 help="config override(s), e.g. " 

54 "``-c apdb.prefix=fancy diaPipe.apdb.db_url=\"sqlite://\"``", 

55 metavar="NAME=VALUE") 

56 self.add_argument("-C", "--configfile", dest="configfile", nargs="*", action=ConfigFileAction, 

57 help="config override file(s) for ApPipeConfig") 

58 

59 def parse_args(self, args=None, namespace=None): 

60 """Parse arguments for an `ApPipeConfig`. 

61 

62 Parameters 

63 ---------- 

64 args : `list` [`str`], optional 

65 Argument list; if `None` then ``sys.argv`` is used. 

66 namespace : `argparse.Namespace`, optional 

67 An object to take the attributes. The default is a new empty 

68 `~argparse.Namespace` object 

69 

70 Returns 

71 ------- 

72 namespace : `argparse.Namespace` 

73 A `~argparse.Namespace` instance containing fields: 

74 - ``config``: the supplied config with all overrides applied, 

75 validated and frozen. 

76 """ 

77 if not namespace: 

78 namespace = argparse.Namespace() 

79 namespace.config = ApPipeConfig() 

80 

81 # ConfigFileAction and ConfigValueAction require namespace.config to exist 

82 namespace = super().parse_args(args, namespace) 

83 del namespace.configfile 

84 

85 namespace.config.validate() 

86 # Do not freeze the config, as a workaround for DM-24435. 

87 

88 return namespace 

89 

90 

91def makeApdb(args=None): 

92 """Create an APDB according to a config. 

93 

94 The command-line arguments should provide config values or a config file 

95 for `ApPipeConfig`. 

96 

97 Parameters 

98 ---------- 

99 args : `list` [`str`], optional 

100 List of command-line arguments; if `None` use `sys.argv`. 

101 

102 Returns 

103 ------- 

104 apdb : `lsst.dax.apdb.Apdb` 

105 The newly configured APDB object. 

106 """ 

107 

108 parser = ConfigOnlyParser() 

109 parsedCmd = parser.parse_args(args=args) 

110 

111 apdb = parsedCmd.config.diaPipe.apdb.apply( 

112 afw_schemas=dict(DiaObject=make_dia_object_schema(), 

113 DiaSource=make_dia_source_schema())) 

114 apdb.makeSchema() 

115 return apdb