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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

# This file is part of ap_pipe. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

# 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 <https://www.gnu.org/licenses/>. 

 

__all__ = ["makeApdb"] 

 

import argparse 

 

from lsst.pipe.base import ConfigFileAction, ConfigValueAction 

from lsst.ap.association import make_dia_object_schema, make_dia_source_schema 

 

from .ap_pipe import ApPipeConfig 

 

 

class ConfigOnlyParser(argparse.ArgumentParser): 

"""Argument parser that knows standard config arguments. 

""" 

 

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

if description is None: 

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

description = """\ 

Create a Alert Production Database using config overrides for 

`lsst.ap.pipe.ApPipeConfig`. 

 

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

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

newly created database. 

 

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

""" 

 

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

 

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

help="config override(s), e.g. ``-c apdb.prefix=fancy apdb.db_url=\"sqlite://\"``", 

metavar="NAME=VALUE") 

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

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

 

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

"""Parse arguments for an `ApPipeConfig`. 

 

Parameters 

---------- 

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

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

namespace : `argparse.Namespace`, optional 

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

`~argparse.Namespace` object 

 

Returns 

------- 

namespace : `argparse.Namespace` 

A `~argparse.Namespace` instance containing fields: 

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

validated and frozen. 

""" 

if not namespace: 

namespace = argparse.Namespace() 

namespace.config = ApPipeConfig() 

 

# ConfigFileAction and ConfigValueAction require namespace.config to exist 

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

del namespace.configfile 

 

namespace.config.validate() 

namespace.config.freeze() 

 

return namespace 

 

 

def makeApdb(args=None): 

"""Create an APDB according to a config. 

 

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

for `ApPipeConfig`. 

 

Parameters 

---------- 

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

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

 

Returns 

------- 

apdb : `lsst.dax.apdb.Apdb` 

The newly configured APDB object. 

""" 

 

parser = ConfigOnlyParser() 

parsedCmd = parser.parse_args(args=args) 

 

apdb = parsedCmd.config.apdb.apply( 

afw_schemas=dict(DiaObject=make_dia_object_schema(), 

DiaSource=make_dia_source_schema())) 

apdb.makeSchema() 

return apdb