naroo_reader/classes/utils/settings.py
2023-08-22 03:37:40 +02:00

147 lines
5.9 KiB
Python

class Settings:
"""
Settings manager
"""
def version( self ):
return '1.0.0'
def __init__( self , arguments ):
# Default configuration
self.cache = None
self.filename = None
self.inte_cal = None
self.no_cache = False
self.output = 'data.fits'
self.verbose = False
self.wave_cal = None
# configuration change
if len( arguments ) < 2:
raise Exception(
'type \'' + __file__ + ' -h\' for more information'
)
index = 0
while index < len( arguments ):
argument = arguments[ index ]
if argument[0] == '-':
if len( argument ) < 2:
raise Exception(
'unknown argument, type \' + __file__ + \' -h' +
' for more information'
)
if argument[1] != '-':
if argument == '-h':
argument = '--help'
elif argument == '-V':
argument = '--version'
elif argument == '-v':
argument = '--verbose'
elif argument == '-n':
argument = '--no-cache'
elif argument == '-c':
if index == len( arguments ) - 1:
raise Exception(
'cache have to take a value'
)
arguments[ index + 1 ] = '--cache=' + \
arguments[ index + 1 ]
index += 1
continue
elif argument == '-o':
if index == len( arguments ) - 1:
raise Exception(
'output have to take a value'
)
arguments[ index + 1 ] = '--output=' + \
arguments[ index + 1 ]
index += 1
continue
elif argument == '-w':
if index == len( arguments ) - 1:
raise Exception(
'wavelength calibration have to take a value'
)
arguments[ index + 1 ] = '--wavelength=' + \
arguments[ index + 1 ]
index += 1
continue
elif argument == '-i':
if index == len( arguments ) - 1:
raise Exception(
'intensity calibration have to take a value'
)
arguments[ index + 1 ] = '--intensity=' + \
arguments[ index + 1 ]
index += 1
continue
else:
raise Exception(
'unknown argument "' + argument + \
'", type \'' + __file__ + \
' -h\' for more information'
)
if argument[1] == '-': # not elif because argument
# can change in the last if
if argument == '--help':
print( self.help() )
exit()
elif argument == '--version':
print( self.version() )
exit()
elif argument == '--verbose':
self.verbose = True
elif argument == '--no-cache':
self.no_cache = True
elif (
len( argument ) > 8 and
argument[ : 8 ] == '--cache='
):
self.cache = argument[ 8 : ]
elif (
len( argument ) > 9 and
argument[ : 9 ] == '--output='
):
self.output = argument[ 9 : ]
elif (
len( argument ) > 13 and
argument[ : 13 ] == '--wavelength='
):
self.wave_cal = argument[ 13 : ]
elif (
len( argument ) > 12 and
argument[ : 12 ] == '--intensity='
):
self.inte_cal = argument[ 12 : ]
else:
raise Exception(
'unknown argument "' + argument + \
'", type \'' + __file__ + ' -h\' ' + \
'for more information'
)
else:
self.filename = argument
index += 1
if self.filename == None:
raise Exception( 'filename should be given' )
def help( self ):
return '\
naroo_reader [options...] filename\
\n -w wavelength wavelength calibration file, default to None.\
\n None means no wavelength interpolation\
\n -i intensity intensity calibration file, default to None.\
\n None means no intensity interpolation\
\n -c --cache use given cache file to store new temporary\
\n data or use old ones.\
\n -h --help show this help and quit\
\n -n --no-cache do not use already existing cache file.\
\n If cache is defined, the cache file will be\
\n overwrited.\
\n If cache is None, it does nothing\
\n -o --output Output file. Default to data.fits\
\n -V --version show version number and quit\
\n -v --verbose show more information to help debugging\
'