1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-06-26 06:53:14 +00:00
gnss-sdr/src/algorithms/libs/volk_gnsssdr_module/volk_gnsssdr/python/volk_gnsssdr_modtool/cfg.py

86 lines
3.0 KiB
Python
Raw Permalink Normal View History

2014-09-07 23:56:09 +00:00
#!/usr/bin/env python
#
# GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
# This file is part of GNSS-SDR.
2014-09-07 23:56:09 +00:00
#
# Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
# SPDX-License-Identifier: GPL-3.0-or-later
2014-09-07 23:56:09 +00:00
2016-12-25 14:44:45 +00:00
from __future__ import print_function
2014-09-07 23:56:09 +00:00
import sys
import os
2019-08-23 20:44:01 +00:00
import exceptions
2014-09-07 23:56:09 +00:00
import re
2018-07-21 09:57:30 +00:00
from six.moves import configparser, input
2014-09-07 23:56:09 +00:00
2018-07-21 09:57:30 +00:00
class volk_gnsssdr_modtool_config(object):
2014-09-07 23:56:09 +00:00
def key_val_sub(self, num, stuff, section):
2019-01-29 13:52:01 +00:00
return re.sub(r'\$' + 'k' + str(num), stuff[num][0], (re.sub(r'\$' + str(num), stuff[num][1], section[1][num])));
2014-09-07 23:56:09 +00:00
def verify(self):
for i in self.verification:
self.verify_section(i)
def remap(self):
for i in self.remapification:
self.verify_section(i)
def verify_section(self, section):
stuff = self.cfg.items(section[0])
for i in range(len(section[1])):
eval(self.key_val_sub(i, stuff, section))
try:
val = eval(self.key_val_sub(i, stuff, section))
if val == False:
2018-07-21 09:57:30 +00:00
raise ValueError
2014-09-07 23:56:09 +00:00
except ValueError:
2018-07-21 09:57:30 +00:00
raise ValueError('Verification function returns False... key:%s, val:%s'%(stuff[i][0], stuff[i][1]))
2014-09-07 23:56:09 +00:00
except:
2018-07-21 09:57:30 +00:00
raise IOError('bad configuration... key:%s, val:%s'%(stuff[i][0], stuff[i][1]))
2014-09-07 23:56:09 +00:00
def __init__(self, cfg=None):
self.config_name = 'config'
self.config_defaults = ['name', 'destination', 'base']
self.config_defaults_remap = ['1',
'self.cfg.set(self.config_name, \'$k1\', os.path.realpath(os.path.expanduser(\'$1\')))',
'self.cfg.set(self.config_name, \'$k2\', os.path.realpath(os.path.expanduser(\'$2\')))']
self.config_defaults_verify = ['re.match(\'[a-zA-Z0-9]+$\', \'$0\')',
'os.path.exists(\'$1\')',
'os.path.exists(\'$2\')']
self.remapification = [(self.config_name, self.config_defaults_remap)]
self.verification = [(self.config_name, self.config_defaults_verify)]
default = os.path.join(os.getcwd(), 'volk_gnsssdr_modtool.cfg')
2018-07-21 09:57:30 +00:00
icfg = configparser.RawConfigParser()
2014-09-07 23:56:09 +00:00
if cfg:
icfg.read(cfg)
elif os.path.exists(default):
icfg.read(default)
else:
2016-12-25 14:44:45 +00:00
print("Initializing config file...")
2014-09-07 23:56:09 +00:00
icfg.add_section(self.config_name)
for kn in self.config_defaults:
2018-07-21 09:57:30 +00:00
rv = input("%s: "%(kn))
2014-09-07 23:56:09 +00:00
icfg.set(self.config_name, kn, rv)
self.cfg = icfg
self.remap()
self.verify()
def read_map(self, name, inp):
if self.cfg.has_section(name):
self.cfg.remove_section(name)
self.cfg.add_section(name)
for i in inp:
self.cfg.set(name, i, inp[i])
def get_map(self, name):
retval = {}
stuff = self.cfg.items(name)
for i in stuff:
retval[i[0]] = i[1]
return retval