SourceXtractorPlusPlus 0.21
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
argv.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2
3# Copyright © 2019 Université de Genève, LMU Munich - Faculty of Physics, IAP-CNRS/Sorbonne Université
4#
5# This library is free software; you can redistribute it and/or modify it under
6# the terms of the GNU Lesser General Public License as published by the Free
7# Software Foundation; either version 3.0 of the License, or (at your option)
8# any later version.
9#
10# This library is distributed in the hope that it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13# details.
14#
15# You should have received a copy of the GNU Lesser General Public License
16# along with this library; if not, write to the Free Software Foundation, Inc.,
17# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18import sys
19from glob import glob
20
21
22class Arguments(object):
23 """
24 This helper class automatically parses the arguments received by the Python script via
25 sys.argv, using the set of key-value parameters received on its constructor to cast the types
26 and define the defaults.
27
28 Parameters
29 ----------
30 kwargs
31 A set of key-values with the defaults, or the types of the expected values. For instance, it is valid to do
32 both:
33
34 key1 = 32.
35 key2 = float
36
37 For the first case - a value -, if no value is passed via sys.argv, this will be the default. Otherwise,
38 its type (float) will be used to cast the received value.
39
40 For the second case - a type -, if no value is passed via sys.argv, the parameter will default to None.
41 Otherwise, the type will be used to cast the received value.
42
43 Callables are also accepted. They should accept to be called with no parameters, returning the default,
44 or with a single string, returning the parsed value.
45
46 Values can be accessed later as attributes:
47
48 instance.key1 and instance.key2
49
50 Raises
51 ------
52 ValueError
53 If the cast of the parameter failed (i.e. an invalid integer or float format)
54 KeyError
55 If additional unknown parameters are received
56 """
57
58 def __init__(self, **kwargs):
59 """
60 Constructor.
61 """
62 argv = dict([arg.split('=', 1) if '=' in arg else (arg, True) for arg in sys.argv[1:]])
63
64 self.__args = dict()
65 for arg_name, arg_spec in kwargs.items():
66 if isinstance(arg_spec, type):
67 arg_type = arg_spec
68 arg_default = None
69 elif callable(arg_spec):
70 arg_type = arg_spec
71 arg_default = arg_spec()
72 else:
73 arg_type = type(arg_spec)
74 arg_default = arg_spec
75
76 if arg_name in argv:
77 try:
78 self.__args[arg_name] = arg_type(argv[arg_name])
79 except ValueError:
80 raise ValueError('Invalid value for {}: {}'.format(arg_name, argv[arg_name]))
81 argv.pop(arg_name)
82 else:
83 self.__args[arg_name] = arg_default
84
85 if len(argv) != 0:
86 raise KeyError('Unknown parameters found: {}'.format(', '.join(argv.keys())))
87
88 def __getattr__(self, item):
89 """
90
91 Parameters
92 ----------
93 item : str
94 Name of the parameter to retrieve
95
96 Returns
97 -------
98 The parsed value of the parameter
99
100 Raises
101 ------
102 AttributeError
103 If item has not been defined on construction
104 """
105 if item not in self.__args:
106 raise AttributeError(item)
107 return self.__args[item]
108
109 def __str__(self):
110 """
111 Returns
112 -------
113 str
114 Human readable representation for the object
115 """
116 return str(self.__args)
117
118 def __repr__(self):
119 """
120 Returns
121 -------
122 str
123 String representation for the object
124 """
125 return repr(self.__args)
126
127
128class FileList(object):
129 """
130 Helper class for receiving a globing pattern as a parameter, defining a list of files
131 - i.e. measurement images or PSFs.
132 It is an iterable, and can be passed directly to load_fits_images.
133
134 Parameters
135 ----------
136 value : str
137 A file globing expression. i.e "*.psf", "band_[r|i|g]_*.fits" or similar.
138 The result is always stored and returned in alphabetical order, so the order between two
139 file lists - i.e frame image and PSF - is consistent and a matching can be done easily between them.
140
141 See Also
142 --------
143 glob.glob : Return a list of paths matching a pathname pattern.
144 """
145
146 def __init__(self, value):
147 """
148 Constructor.
149 """
150 self.__files = sorted(glob(value))
151
152 def __iter__(self):
153 """
154 Returns
155 -------
156 iterator
157 """
158 return iter(self.__files)
159
160 def __len__(self):
161 """
162 Returns
163 -------
164 int
165 Length of the list
166 """
167 return len(self.__files)
168
169 def __str__(self):
170 """
171 Returns
172 -------
173 str
174 Human readable representation for the object
175 """
176 return str(self.__files)
177
178 def __repr__(self):
179 """
180 Returns
181 -------
182 str
183 String representation for the object
184 """
185 return repr(self.__files)