def load_config(self, filename): """ Loads configuration settings from the file whose name is given. It will read the section called "oscar.estop". @param filename: the name of the config file to be read """ cfg = RawConfigParser(); cfg.read(filename) section = "oscar.estop" self.button = cfg.getint(section, "button") self.ignore_duration = cfg.getfloat(section, "ignore_duration") self.count_duration = cfg.getfloat(section, "count_duration")
def load_config(self, filename): """ Loads configuration settings from the file whose name is given. It will read the section called "oscar.ranger". @param filename: the name of the config file to be read """ cfg = RawConfigParser(); cfg.read(filename) section = "oscar.ranger" self.kp_steering = cfg.getfloat(section, "kp_steering") self.kp_steering_rev = cfg.getfloat(section, "kp_steering_rev") self.panic_threshold = cfg.getfloat(section, "panic_threshold") self.escape_speed = cfg.getfloat(section, "escape_speed") self.smoothing_factor = cfg.getfloat(section, "smoothing_factor")
def read_config(filename): ''' Read in the user configuation file.''' user_config = { 'user_call': 'HORUS_RX', 'freedv_udp_port': 55690, 'ozi_udp_port': 55683, 'summary_port': 55672, 'station_lat': 0.0, 'station_lon': 0.0, 'radio_comment': "", 'antenna_comment': "" } try: config = RawConfigParser() config.read(filename) user_config['user_call'] = config.get('user', 'callsign') user_config['station_lat'] = config.getfloat('user', 'station_lat') user_config['station_lon'] = config.getfloat('user', 'station_lon') user_config['radio_comment'] = config.get('user', 'radio_comment') user_config['antenna_comment'] = config.get('user', 'antenna_comment') user_config['freedv_udp_port'] = config.getint('freedv', 'udp_port') user_config['ozi_udp_port'] = config.getint('ozimux', 'ozimux_port') user_config['summary_port'] = config.getint('ozimux', 'summary_port') return user_config except: traceback.print_exc() logging.error( "Could not parse config file, exiting. Have you copied user.cfg.example to user.cfg?" ) return None
def loadMeta(self): "Loads the 'meta' - variables that change with the server (worlds, admins, etc.)" config = ConfigParser() config.read("config/data/ranks.meta") specs = ConfigParser() specs.read("config/data/spectators.meta") lastseen = ConfigParser() lastseen.read("config/data/lastseen.meta") bans = ConfigParser() bans.read("config/data/bans.meta") worlds = ConfigParser() worlds.read("config/data/worlds.meta") # Read in the admins if config.has_section("admins"): for name in config.options("admins"): self.admins.add(name) # Read in the mods if config.has_section("mods"): for name in config.options("mods"): self.mods.add(name) if config.has_section("globalbuilders"): for name in config.options("globalbuilders"): self.globalbuilders.add(name) if config.has_section("members"): for name in config.options("members"): self.members.add(name) # Read in the directors if config.has_section("directors"): for name in config.options("directors"): self.directors.add(name) if config.has_section("silenced"): for name in config.options("silenced"): self.silenced.add(name) # Read in the spectators (experimental) if specs.has_section("spectators"): for name in specs.options("spectators"): self.spectators.add(name) bans = ConfigParser() bans.read("config/data/bans.meta") # Read in the bans if bans.has_section("banned"): for name in bans.options("banned"): self.banned[name] = bans.get("banned", name) # Read in the ipbans if bans.has_section("ipbanned"): for ip in bans.options("ipbanned"): self.ipbanned[ip] = bans.get("ipbanned", ip) # Read in the lastseen if lastseen.has_section("lastseen"): for username in lastseen.options("lastseen"): self.lastseen[username] = lastseen.getfloat("lastseen", username) # Read in the worlds if worlds.has_section("worlds"): for name in worlds.options("worlds"): if name is self.default_name: self.default_loaded = True else: self.worlds[self.default_name] = None if not self.default_loaded: self.worlds[self.default_name] = None
def __init__(self, configuration_file, rewrite=False): parser = RawConfigParser() parser.read(configuration_file) dist_filename = parser.get('files', 'distances-filename') entropy_filename = parser.get('files', 'entropy-filename') self.mRMR_filename = parser.get('files', 'mRMR-filename') self.num_bins = parser.getint('parameters', 'num-bins') self.bin_width = parser.getfloat('parameters', 'bin-width') self.aa = parser.getboolean('parameters', 'aa') self.chains = parser.getint('parameters', 'chains') self.dist_range = [float(i) for i in parser.get('parameters', 'distance-range').split()] self.weights = [float(i) for i in parser.get('parameters', 'weights').split()] self.resi_select = parser.get('parameters', 'excluded-residues').split() if not self.resi_select: self.resi_select = None print "Loading distance file %s" % dist_filename init = time.time() self.dists = cPickle.load(open(dist_filename)) print "Loaded %s in %f seconds" % (dist_filename, time.time() - init) # If no entropy has been provided, entropy must be calculated for permitted residues. print colors.HEADER + "\tENTROPY CALCULATION" + colors.ENDC if os.path.exists(entropy_filename) and not rewrite: warn_file_exists(entropy_filename) self.entropy = cPickle.load(open(entropy_filename)) else: print "No entropy file was found or you have chosen to rewrite. Calculating entropies..." self.entropy = h_fast(self.dists) print "Dumping entopies to", entropy_filename cPickle.dump(self.entropy, open(entropy_filename, 'w'))
def getfloat(self, section, option, default=None): val = default try: val = RawConfigParser.getfloat(self, section, option) except NoOptionError, e: # NOTE: None cannot be the default. if default is None: raise
def load_config(self, filename): """ Loads configuration settings from the file whose name is given. It will read the section called "oscar.commander". @param filename: the name of the config file to be read """ cfg = RawConfigParser(); cfg.read(filename) section = "oscar.commander" self.obey_ranger = cfg.getfloat(section, "obey_ranger") self.min_throttle = cfg.getfloat(section, "min_throttle") self.max_throttle = cfg.getfloat(section, "max_throttle") # load configuration of sub-components self.vision.load_config(filename) self.ranger.load_config(filename) self.stopsign.load_config(filename) self.estop.load_config(filename)
def MainProcess(): #--------------------------------- Initialization ------------------------------------ #Prepare Environment Variables: #CNF='/Users/xiaoming/Documents/workspace/myzabbixagent/src/vmagent.conf' Path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) CNF = Path + "/cnf/vmagent.conf" config = RawConfigParser() config.read(CNF) INTERVAL = config.getint('vmagent', 'interval') CACHE = Path + "/.cache/vmagent.cache" if not config.has_option( 'vmagent', 'cache') else config.get('vmagent', 'cache') LOGPATH = Path + "/log" if not config.has_option( 'vmagent', 'logpath') else config.get('vmagent', 'logpath') ZA = ZabbixAPI(url=config.get('zabbix', 'uri'), user=config.get('zabbix', 'user'), password=config.get('zabbix', 'password')) @TimeLimitbyProcess(config.getfloat('vmagent', 'timeout')) def GetInstances(config=config): return GetInstancesInfo(config) #Mark the start to log StartStr = "VM Daemon starts with PID %d." LogWrite(LOGPATH, status="Started", content=StartStr % os.getpid()) #One-off execution at start VmStatus = 1 while VmStatus: os.system('rm -f ' + CACHE) Last_Time = int(time.time()) try: DataList = GetInstances() print "First get info:", DataList #Mimic diagnostics API output #DataList=eval(getoutput('cat /Users/xiaoming/Documents/workspace/myzabbixagent/src/tmp/new1.txt')) except SyntaxError, e: LogWrite(LOGPATH, status="SyntaxError on input file", content=repr(e)) else: VmStatus = DataFormatCheck(DataList) print "Check result: ", VmStatus if VmStatus: print "Datacheck Error:", VmStatus continue if not VmStatus: Res_Initialized = Initialize(ZA, config) Data_to_Cache = EthReplace(ZA, config, DataExtract(DataList), Old=Res_Initialized["Mac_Eth"], Initialization=True) with open(CACHE, 'w') as f: f.write(json.dumps(Data_to_Cache)) CreateZabbixHost(ZA, config, DL=Data_to_Cache, Entire=True) finally:
def load_config(self, filename): """ Loads configuration settings from the file whose name is given. It will read the section called "oscar.stopsign". @param filename: the name of the config file to be read """ cfg = RawConfigParser(); cfg.read(filename) section = "oscar.stopsign" self.quantity = cfg.getfloat(section, "quantity")
def updateProcess(self): config = RawConfigParser() config.read(self.fp) process = config.getfloat('Task-State', 'Process') total = self.getTotalTileNum() process = process + 1/float(total)*100 config.set('Task-State', 'Process', str(process)) with open(self.fp, 'wb') as configfile: config.write(configfile) return process
class preferences: config = [] parse = None lcdconnection = None def __init__(self): if (DEBUG == True): self.writetolog("Init Prefs") self.loadprefs(None, None, None) if (DEBUG == True): self.writetolog("Done") def loadprefs(self, word, word_eol, userdata): if (self.lcdconnection != None): self.lcdconnection.offlights(None, None, None) prefs = open(module_dir + "config.txt") self.parse = RawConfigParser() self.parse.readfp(prefs) prefs.close() print "Prefrences Loaded" def value(self,option): if (DEBUG == True): self.writetolog("Prefs -> Value") return self.parse.get("config",option) if (DEBUG == True): self.writetolog("Done") def floatvalue(self,option): if (DEBUG == True): self.writetolog("Prefs -> FloatValue") return self.parse.getfloat("config",option) if (DEBUG == True): self.writetolog("Done") def intvalue(self,option): if (DEBUG == True): self.writetolog("Prefs -> IntValue") return self.parse.getint("config",option) if (DEBUG == True): self.writetolog("Done") def boolvalue(self,option): if (DEBUG == True): self.writetolog("Prefs -> IntValue") return self.parse.getboolean("config",option) if (DEBUG == True): self.writetolog("Done") def writetolog(self, message): debuglog = open(module_dir + "log.txt","a") debuglog.write(message + "\n") debuglog.close
def create_from_config(cls, path): """Return a RatioListLID class initialized from a config file.""" # Parse configuration from the file config = RawConfigParser() result = config.read(path) if not result: raise IOError("Couldn't parse config file: %s" % path) # Read in configuration values lang1 = config.get(cls._CONFIG_MODEL, "lang1") lang2 = config.get(cls._CONFIG_MODEL, "lang2") high_ratio = config.getfloat(cls._CONFIG_MODEL, "high_ratio") low_ratio = config.getfloat(cls._CONFIG_MODEL, "low_ratio") wordlist1_path = config.get(cls._CONFIG_MODEL, "wordlist1") wordlist2_path = config.get(cls._CONFIG_MODEL, "wordlist2") smoothing = config.getfloat(cls._CONFIG_MODEL, "smoothing") min_freq = config.getint(cls._CONFIG_MODEL, "min_freq") lang1_min = config.getint(cls._CONFIG_MODEL, "lang1_min") lang2_min = config.getint(cls._CONFIG_MODEL, "lang2_min") lang1_max_unk_rate = config.getfloat(cls._CONFIG_MODEL, "lang1_max_unk_rate") lang2_max_unk_rate = config.getfloat(cls._CONFIG_MODEL, "lang2_max_unk_rate") cs_max_unk_rate = config.getfloat(cls._CONFIG_MODEL, "cs_max_unk_rate") try: ignorelist = config.get(cls._CONFIG_MODEL, "ignorelist") except NoOptionError: ignorelist = None # Create the structures needed for the LIDder and return it wordlist1 = codecs.open(wordlist1_path, 'Ur', 'utf_8') wordlist2 = codecs.open(wordlist2_path, 'Ur', 'utf_8') ratios, unused1, unused2 = wordlist_ratio(wordlist1, wordlist2, smoothing, min_freq) # Remove words from the ignorelist if ignorelist: bad_words = set(line.strip() for line in codecs.open(ignorelist, 'Ur', 'utf_8')) prune_ratios(ratios, bad_words) return cls(ratios, lang1, lang2, low_ratio, high_ratio, lang1_min, lang2_min, lang1_max_unk_rate, lang2_max_unk_rate, cs_max_unk_rate)
class Settings(): _changed = True def __init__(self): self._settings = RawConfigParser() self._funcs = (self._getGeneric, self._getInt, self._getFloat, self._getStruct, self._getBool) def getValue(self, section, option, select): """Returns value from settings file - 0: string/raw value, 1: integer, 2: float, 3: data structure, 4: boolean""" if Settings._changed: self._fileOpen() Settings._changed = False return self._funcs[select](section,option) def changeValue(self, section, option): self._settings.set(section, str(option)) with open('cellular.ini', 'wb') as fp: self._settings.write(fp) Settings._changed = True def _throwError(self, message, title = 'Fatal Error'): Tkinter.Tk().withdraw() tkMessageBox.showerror(str(title),str(message)) exit def _fileOpen(self): try: fp = open(configFile) except IOError: self._throwError('config file not found.\nThe program will now terminate.') self._settings.readfp(fp) def _getGeneric(self, section, option): value = self._settings.get(section, option) return value def _getInt(self, section, option): try: value = self._settings.getint(section,option) except ValueError: self._throwError('Expected integer, recieved something else at ['+section+'],'+option+'.\nThe program will now terminate.') return value def _getFloat(self, section, option): try: value = self._settings.getfloat(section,option) except ValueError: self._throwError('Expected float, recieved something else at ['+section+'],'+option+'.\nThe program will now terminate.') return value def _getStruct(self, section, option): try: value = decode(self._settings.get(section,option)) except ValueError or SyntaxError: self._throwError('Expected data structure, recieved something else at ['+section+'], '+option+'.\nTheProgram will now terminate.') return value def _getBool(self, section, option): try: value = self._settings.getboolean(section,option) except ValueError: self._throwError('Expected boolean, recieved something else at ['+section+'], '+option+'.\nThe program will now terminate.') return value
def read_ini(self, filename): if not filename or not os.path.isfile(filename): return parser = RawConfigParser() parser.read(filename) for section in parser.sections(): for option in parser.options(section): value = None if section in self.defaults and option in self.defaults[section]: t = type(self.defaults[section][option]) if t is str: value = parser.get(section, option) elif t is int: value = parser.getint(section, option) elif t is float: value = parser.getfloat(section, option) elif t is bool: value = parser.getboolean(section, option) else: value = parser.get(section, option) self.set(section, option, value)
def load_state(self): fn = QtGui.QFileDialog.getOpenFileName(self, 'Load state') if fn == '': return cp = RawConfigParser() cp.read([fn]) sid.volume = cp.getint('main', 'volume') for v in sid.voices: sn = 'voice%d' % v.voicenum v.waveform = cp.getint(sn, 'waveform') v.pulse_width = cp.getfloat(sn, 'pulse_width') v.attack = cp.getint(sn, 'attack') v.decay = cp.getint(sn, 'decay') v.sustain = cp.getint(sn, 'sustain') v.release = cp.getint(sn, 'release') try: sn = cp.get(cp.get(sn, 'sources'), 'sources') self.load_sink_state(VoiceSink.POOL[v], cp, sn) except: pass
def read_config(): # config file should be placed in same directory as the script config_file_path = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "locopod.config") parser = RawConfigParser() try: parser.readfp(open(config_file_path)) except IOError: return for name, value in parser.items("config"): name = name.upper() if name in GLOBAL_VARS: default = globals()[name] if type(default) == type(1): value = parser.getint("config", name) elif type(default) == type(1.0): value = parser.getfloat("config", name) elif type(default) == type(True): value = parser.getboolean("config", name) globals()[name] = value
def read_mri_cfg(subject, subjects_dir=None): """Read information from the cfg file of a scaled MRI brain Parameters ---------- subject : str Name of the scaled MRI subject. subjects_dir : None | str Override the SUBJECTS_DIR environment variable. Returns ------- cfg : dict Dictionary with entries from the MRI's cfg file. """ subjects_dir = get_subjects_dir(subjects_dir, raise_error=True) fname = os.path.join(subjects_dir, subject, 'MRI scaling parameters.cfg') if not os.path.exists(fname): err = ("%r does not seem to be a scaled mri subject: %r does not " "exist." % (subject, fname)) raise IOError(err) config = RawConfigParser() config.read(fname) n_params = config.getint("MRI Scaling", 'n_params') if n_params == 1: scale = config.getfloat("MRI Scaling", 'scale') elif n_params == 3: scale_str = config.get("MRI Scaling", 'scale') scale = np.array(map(float, scale_str.split())) else: raise ValueError("Invalid n_params value in MRI cfg: %i" % n_params) out = { 'subject_from': config.get("MRI Scaling", 'subject_from'), 'n_params': n_params, 'scale': scale } return out
def read_mri_cfg(subject, subjects_dir=None): """Read information from the cfg file of a scaled MRI brain Parameters ---------- subject : str Name of the scaled MRI subject. subjects_dir : None | str Override the SUBJECTS_DIR environment variable. Returns ------- cfg : dict Dictionary with entries from the MRI's cfg file. """ subjects_dir = get_subjects_dir(subjects_dir, raise_error=True) fname = os.path.join(subjects_dir, subject, 'MRI scaling parameters.cfg') if not os.path.exists(fname): err = ("%r does not seem to be a scaled mri subject: %r does not " "exist." % (subject, fname)) raise IOError(err) config = RawConfigParser() config.read(fname) n_params = config.getint("MRI Scaling", 'n_params') if n_params == 1: scale = config.getfloat("MRI Scaling", 'scale') elif n_params == 3: scale_str = config.get("MRI Scaling", 'scale') scale = np.array(map(float, scale_str.split())) else: raise ValueError("Invalid n_params value in MRI cfg: %i" % n_params) out = {'subject_from': config.get("MRI Scaling", 'subject_from'), 'n_params': n_params, 'scale': scale} return out
def read_auto_rx_config(filename, no_sdr_test=False): """ Read an Auto-RX v2 Station Configuration File. This function will attempt to parse a configuration file. It will also confirm the accessibility of any SDRs specified in the config file. Args: filename (str): Filename of the configuration file to read. no_sdr_test (bool): Skip testing the SDRs (used for some unit tests) Returns: auto_rx_config (dict): The configuration dictionary. sdr_config (dict): A dictionary with SDR parameters. """ global global_config # Configuration Defaults: auto_rx_config = { # Log Settings 'per_sonde_log' : True, # Email Settings 'email_enabled': False, 'email_smtp_server': 'localhost', 'email_smtp_port': 25, 'email_smtp_authentication': 'None', 'email_smtp_login': '******', 'email_smtp_password': '******', 'email_from': 'sonde@localhost', 'email_to': None, 'email_subject': "<type> Sonde launch detected on <freq>: <id>", # SDR Settings 'sdr_fm': 'rtl_fm', 'sdr_power': 'rtl_power', 'sdr_quantity': 1, # Search Parameters 'min_freq' : 400.4, 'max_freq' : 404.0, 'rx_timeout' : 120, 'whitelist' : [], 'blacklist' : [], 'greylist' : [], # Location Settings 'station_lat' : 0.0, 'station_lon' : 0.0, 'station_alt' : 0.0, 'station_code' : 'SONDE', # NOTE: This will not be read from the config file, but will be left in place for now # as a default setting. 'gpsd_enabled' : False, 'gpsd_host' : 'localhost', 'gpsd_port' : 2947, # Position Filter Settings 'max_altitude' : 50000, 'max_radius_km' : 1000, # Habitat Settings 'habitat_enabled': False, 'habitat_upload_rate': 30, 'habitat_uploader_callsign': 'SONDE_AUTO_RX', 'habitat_uploader_antenna': '1/4-wave', 'habitat_upload_listener_position': False, 'habitat_payload_callsign': '<id>', # APRS Settings 'aprs_enabled' : False, 'aprs_upload_rate': 30, 'aprs_user' : 'N0CALL', 'aprs_pass' : '00000', 'aprs_server' : 'rotate.aprs2.net', 'aprs_object_id': '<id>', 'aprs_custom_comment': 'Radiosonde Auto-RX <freq>', 'aprs_position_report': False, 'station_beacon_enabled': False, 'station_beacon_rate': 30, 'station_beacon_comment': "radiosonde_auto_rx SondeGate v<version>", 'station_beacon_icon': '/r', # Web Settings, 'web_host' : '0.0.0.0', 'web_port' : 5000, 'web_archive_age': 120, 'web_control': True, # Advanced Parameters 'search_step' : 800, 'snr_threshold' : 10, 'min_distance' : 1000, 'dwell_time' : 10, 'max_peaks' : 10, 'quantization' : 10000, 'decoder_spacing_limit': 15000, 'synchronous_upload' : False, 'scan_dwell_time' : 20, 'detect_dwell_time' : 5, 'scan_delay' : 10, 'payload_id_valid' : 5, 'temporary_block_time' : 60, 'rs41_drift_tweak': False, 'decoder_stats': False, 'ngp_tweak': False, # Rotator Settings 'enable_rotator': False, 'rotator_update_rate': 30, 'rotator_hostname': '127.0.0.1', 'rotator_port' : 4533, 'rotation_threshold': 5.0, 'rotator_homing_enabled': False, 'rotator_homing_delay': 10, 'rotator_home_azimuth': 0, 'rotator_home_elevation': 0, # OziExplorer Settings 'ozi_enabled' : False, 'ozi_update_rate': 5, 'ozi_port' : 55681, 'payload_summary_enabled': False, 'payload_summary_port' : 55672, # Debugging settings 'save_detection_audio' : False, 'save_decode_audio' : False, 'save_decode_iq' : False, # URL for the Habitat DB Server. # As of July 2018 we send via sondehub.org, which will allow us to eventually transition away # from using the habhub.org tracker, and leave it for use by High-Altitude Balloon Hobbyists. # For now, sondehub.org just acts as a proxy to habhub.org. # This setting is not exposed to users as it's only used for unit/int testing 'habitat_url': "https://habitat.sondehub.org/" } try: # Check the file exists. if not os.path.isfile(filename): logging.critical("Config file %s does not exist!" % filename) return None config = RawConfigParser(auto_rx_config) config.read(filename) # Log Settings auto_rx_config['per_sonde_log'] = config.getboolean('logging', 'per_sonde_log') # Email Settings if config.has_option('email', 'email_enabled'): try: auto_rx_config['email_enabled'] = config.getboolean('email', 'email_enabled') auto_rx_config['email_smtp_server'] = config.get('email', 'smtp_server') auto_rx_config['email_smtp_port'] = config.get('email', 'smtp_port') auto_rx_config['email_smtp_authentication'] = config.get('email', 'smtp_authentication') auto_rx_config['email_smtp_login'] = config.get('email', 'smtp_login') auto_rx_config['email_smtp_password'] = config.get('email', 'smtp_password') auto_rx_config['email_from'] = config.get('email', 'from') auto_rx_config['email_to'] = config.get('email', 'to') auto_rx_config['email_subject'] = config.get('email', 'subject') if auto_rx_config['email_smtp_authentication'] not in ['None', 'TLS', 'SSL']: logging.error("Config - Invalid email authentication setting. Must be None, TLS or SSL.") return None except: logging.error("Config - Invalid or missing email settings. Disabling.") auto_rx_config['email_enabled'] = False # SDR Settings auto_rx_config['sdr_fm'] = config.get('advanced', 'sdr_fm_path') auto_rx_config['sdr_power'] = config.get('advanced', 'sdr_power_path') auto_rx_config['sdr_quantity'] = config.getint('sdr', 'sdr_quantity') # Search Parameters auto_rx_config['min_freq'] = config.getfloat('search_params', 'min_freq') auto_rx_config['max_freq'] = config.getfloat('search_params', 'max_freq') auto_rx_config['rx_timeout'] = config.getint('search_params', 'rx_timeout') auto_rx_config['whitelist'] = json.loads(config.get('search_params', 'whitelist')) auto_rx_config['blacklist'] = json.loads(config.get('search_params', 'blacklist')) auto_rx_config['greylist'] = json.loads(config.get('search_params', 'greylist')) # Location Settings auto_rx_config['station_lat'] = config.getfloat('location', 'station_lat') auto_rx_config['station_lon'] = config.getfloat('location', 'station_lon') auto_rx_config['station_alt'] = config.getfloat('location', 'station_alt') # Position Filtering auto_rx_config['max_altitude'] = config.getint('filtering', 'max_altitude') auto_rx_config['max_radius_km'] = config.getint('filtering', 'max_radius_km') # Habitat Settings auto_rx_config['habitat_enabled'] = config.getboolean('habitat', 'habitat_enabled') auto_rx_config['habitat_upload_rate'] = config.getint('habitat', 'upload_rate') auto_rx_config['habitat_payload_callsign'] = config.get('habitat', 'payload_callsign') auto_rx_config['habitat_uploader_callsign'] = config.get('habitat', 'uploader_callsign') auto_rx_config['habitat_upload_listener_position'] = config.getboolean('habitat','upload_listener_position') auto_rx_config['habitat_uploader_antenna'] = config.get('habitat', 'uploader_antenna').strip() try: # Use the default configuration if not found auto_rx_config['habitat_url'] = config.get('habitat','url') except: pass # APRS Settings auto_rx_config['aprs_enabled'] = config.getboolean('aprs', 'aprs_enabled') auto_rx_config['aprs_upload_rate'] = config.getint('aprs', 'upload_rate') auto_rx_config['aprs_user'] = config.get('aprs', 'aprs_user') auto_rx_config['aprs_pass'] = config.get('aprs', 'aprs_pass') auto_rx_config['aprs_server'] = config.get('aprs', 'aprs_server') auto_rx_config['aprs_object_id'] = config.get('aprs', 'aprs_object_id') auto_rx_config['aprs_custom_comment'] = config.get('aprs', 'aprs_custom_comment') auto_rx_config['aprs_position_report'] = config.getboolean('aprs','aprs_position_report') auto_rx_config['station_beacon_enabled'] = config.getboolean('aprs','station_beacon_enabled') auto_rx_config['station_beacon_rate'] = config.getint('aprs', 'station_beacon_rate') auto_rx_config['station_beacon_comment'] = config.get('aprs', 'station_beacon_comment') auto_rx_config['station_beacon_icon'] = config.get('aprs', 'station_beacon_icon') # OziPlotter Settings auto_rx_config['ozi_enabled'] = config.getboolean('oziplotter', 'ozi_enabled') auto_rx_config['ozi_update_rate'] = config.getint('oziplotter', 'ozi_update_rate') auto_rx_config['ozi_port'] = config.getint('oziplotter', 'ozi_port') auto_rx_config['payload_summary_enabled'] = config.getboolean('oziplotter', 'payload_summary_enabled') auto_rx_config['payload_summary_port'] = config.getint('oziplotter', 'payload_summary_port') # Advanced Settings auto_rx_config['search_step'] = config.getfloat('advanced', 'search_step') auto_rx_config['snr_threshold'] = config.getfloat('advanced', 'snr_threshold') auto_rx_config['min_distance'] = config.getfloat('advanced', 'min_distance') auto_rx_config['dwell_time'] = config.getint('advanced', 'dwell_time') auto_rx_config['quantization'] = config.getint('advanced', 'quantization') auto_rx_config['max_peaks'] = config.getint('advanced', 'max_peaks') auto_rx_config['scan_dwell_time'] = config.getint('advanced', 'scan_dwell_time') auto_rx_config['detect_dwell_time'] = config.getint('advanced', 'detect_dwell_time') auto_rx_config['scan_delay'] = config.getint('advanced', 'scan_delay') auto_rx_config['payload_id_valid'] = config.getint('advanced', 'payload_id_valid') auto_rx_config['synchronous_upload'] = config.getboolean('advanced', 'synchronous_upload') # Rotator Settings auto_rx_config['rotator_enabled'] = config.getboolean('rotator','rotator_enabled') auto_rx_config['rotator_update_rate'] = config.getint('rotator', 'update_rate') auto_rx_config['rotator_hostname'] = config.get('rotator', 'rotator_hostname') auto_rx_config['rotator_port'] = config.getint('rotator', 'rotator_port') auto_rx_config['rotator_homing_enabled'] = config.getboolean('rotator', 'rotator_homing_enabled') auto_rx_config['rotator_home_azimuth'] = config.getfloat('rotator', 'rotator_home_azimuth') auto_rx_config['rotator_home_elevation'] = config.getfloat('rotator', 'rotator_home_elevation') auto_rx_config['rotator_homing_delay'] = config.getint('rotator', 'rotator_homing_delay') auto_rx_config['rotation_threshold'] = config.getfloat('rotator', 'rotation_threshold') # Web interface settings. auto_rx_config['web_host'] = config.get('web', 'web_host') auto_rx_config['web_port'] = config.getint('web', 'web_port') auto_rx_config['web_archive_age'] = config.getint('web', 'archive_age') auto_rx_config['save_detection_audio'] = config.getboolean('debugging', 'save_detection_audio') auto_rx_config['save_decode_audio'] = config.getboolean('debugging', 'save_decode_audio') auto_rx_config['save_decode_iq'] = config.getboolean('debugging', 'save_decode_iq') # NOTE 2019-09-21: The station code will now be fixed at the default to avoid multiple iMet callsign issues. # auto_rx_config['station_code'] = config.get('location', 'station_code') # if len(auto_rx_config['station_code']) > 5: # auto_rx_config['station_code'] = auto_rx_config['station_code'][:5] # logging.warning("Config - Clipped station code to 5 digits: %s" % auto_rx_config['station_code']) auto_rx_config['temporary_block_time'] = config.getint('advanced', 'temporary_block_time') # New demod tweaks - Added 2019-04-23 # Default to all experimental decoders off. auto_rx_config['experimental_decoders'] = { 'RS41': False, 'RS92': False, 'DFM': False, 'M10': False, 'iMet': False, 'LMS6': True, 'MK2LMS': False, 'MEISEI': False, 'UDP': False} auto_rx_config['rs41_drift_tweak'] = config.getboolean('advanced', 'drift_tweak') auto_rx_config['decoder_spacing_limit'] = config.getint('advanced', 'decoder_spacing_limit') auto_rx_config['experimental_decoders']['RS41'] = config.getboolean('advanced', 'rs41_experimental') auto_rx_config['experimental_decoders']['RS92'] = config.getboolean('advanced', 'rs92_experimental') auto_rx_config['experimental_decoders']['M10'] = config.getboolean('advanced', 'm10_experimental') auto_rx_config['experimental_decoders']['DFM'] = config.getboolean('advanced', 'dfm_experimental') auto_rx_config['experimental_decoders']['LMS6'] = config.getboolean('advanced', 'lms6-400_experimental') try: auto_rx_config['web_control'] = config.getboolean('web', 'web_control') auto_rx_config['ngp_tweak'] = config.getboolean('advanced', 'ngp_tweak') auto_rx_config['gpsd_enabled'] = config.getboolean('location', 'gpsd_enabled') auto_rx_config['gpsd_host'] = config.get('location', 'gpsd_host') auto_rx_config['gpsd_port'] = config.getint('location', 'gpsd_port') except: logging.warning("Config - Did not find web control / ngp_tweak / gpsd options, using defaults (disabled)") auto_rx_config['web_control'] = False auto_rx_config['ngp_tweak'] = False auto_rx_config['gpsd_enabled'] = False # If we are being called as part of a unit test, just return the config now. if no_sdr_test: return auto_rx_config # Now we attempt to read in the individual SDR parameters. auto_rx_config['sdr_settings'] = {} for _n in range(1,auto_rx_config['sdr_quantity']+1): _section = "sdr_%d" % _n try: _device_idx = config.get(_section,'device_idx') _ppm = config.getint(_section, 'ppm') _gain = config.getfloat(_section, 'gain') _bias = config.getboolean(_section, 'bias') if (auto_rx_config['sdr_quantity'] > 1) and (_device_idx == '0'): logging.critical("Config - SDR Device ID of 0 used with a multi-SDR configuration. Go read the warning in the config file!") return None # See if the SDR exists. _sdr_valid = rtlsdr_test(_device_idx) if _sdr_valid: auto_rx_config['sdr_settings'][_device_idx] = {'ppm':_ppm, 'gain':_gain, 'bias':_bias, 'in_use': False, 'task': None} logging.info('Config - Tested SDR #%s OK' % _device_idx) else: logging.warning("Config - SDR #%s invalid." % _device_idx) except Exception as e: logging.error("Config - Error parsing SDR %d config - %s" % (_n,str(e))) continue # Sanity checks when using more than one SDR if (len(auto_rx_config['sdr_settings'].keys()) > 1) and (auto_rx_config['habitat_payload_callsign'] != "<id>"): logging.critical("Fixed Habitat Payload callsign used in a multi-SDR configuration. Go read the warnings in the config file!") return None if (len(auto_rx_config['sdr_settings'].keys()) > 1) and (auto_rx_config['aprs_object_id'] != "<id>"): logging.critical("Fixed APRS object ID used in a multi-SDR configuration. Go read the warnings in the config file!") return None if (len(auto_rx_config['sdr_settings'].keys()) > 1) and (auto_rx_config['rotator_enabled']): logging.critical("Rotator enabled in a multi-SDR configuration. Go read the warnings in the config file!") return None # TODO: Revisit this limitation once the OziPlotter output sub-module is complete. if (len(auto_rx_config['sdr_settings'].keys()) > 1) and auto_rx_config['ozi_enabled']: logging.critical("Oziplotter output enabled in a multi-SDR configuration.") return None if len(auto_rx_config['sdr_settings'].keys()) == 0: # We have no SDRs to use!! logging.error("Config - No working SDRs! Cannot run...") return None else: # Create a global copy of the configuration file at this point global_config = copy.deepcopy(auto_rx_config) # Excise some sensitive parameters from the global config. global_config.pop('email_smtp_login') global_config.pop('email_smtp_password') global_config.pop('email_smtp_server') return auto_rx_config except: traceback.print_exc() logging.error("Could not parse config file.") return None
def read_auto_rx_config(filename): """ Read an Auto-RX v2 Station Configuration File. This function will attempt to parse a configuration file. It will also confirm the accessibility of any SDRs specified in the config file. Args: filename (str): Filename of the configuration file to read. Returns: auto_rx_config (dict): The configuration dictionary. sdr_config (dict): A dictionary with SDR parameters. """ global global_config # Configuration Defaults: auto_rx_config = { # Log Settings 'per_sonde_log': True, # SDR Settings 'sdr_fm': 'rtl_fm', 'sdr_power': 'rtl_power', 'sdr_quantity': 1, # Search Parameters 'min_freq': 400.4, 'max_freq': 404.0, 'rx_timeout': 120, 'whitelist': [], 'blacklist': [], 'greylist': [], # Location Settings 'station_lat': 0.0, 'station_lon': 0.0, 'station_alt': 0.0, # Position Filter Settings 'max_altitude': 50000, 'max_radius_km': 1000, # Habitat Settings 'habitat_enabled': False, 'habitat_upload_rate': 30, 'habitat_uploader_callsign': 'SONDE_AUTO_RX', 'habitat_uploader_antenna': '1/4-wave', 'habitat_upload_listener_position': False, 'habitat_payload_callsign': '<id>', # APRS Settings 'aprs_enabled': False, 'aprs_upload_rate': 30, 'aprs_user': '******', 'aprs_pass': '******', 'aprs_server': 'rotate.aprs2.net', 'aprs_object_id': '<id>', 'aprs_custom_comment': 'Radiosonde Auto-RX <freq>', # Web Settings, 'web_port': 5000, 'web_archive_age': 120, # Advanced Parameters 'search_step': 800, 'snr_threshold': 10, 'min_distance': 1000, 'dwell_time': 10, 'max_peaks': 10, 'quantization': 10000, 'synchronous_upload': False, 'scan_dwell_time': 20, 'detect_dwell_time': 5, 'scan_delay': 10, 'payload_id_valid': 5, # Rotator Settings 'enable_rotator': False, 'rotator_hostname': '127.0.0.1', 'rotator_port': 4533, 'rotator_homing_enabled': False, 'rotator_home_azimuth': 0, 'rotator_home_elevation': 0, # OziExplorer Settings 'ozi_enabled': False, 'ozi_update_rate': 5, 'ozi_port': 55681, 'payload_summary_enabled': False, 'payload_summary_port': 55672 } sdr_settings = {} #'0':{'ppm':0, 'gain':-1, 'bias': False}} try: config = RawConfigParser(auto_rx_config) config.read(filename) # Log Settings auto_rx_config['per_sonde_log'] = config.getboolean( 'logging', 'per_sonde_log') # SDR Settings auto_rx_config['sdr_fm'] = config.get('advanced', 'sdr_fm_path') auto_rx_config['sdr_power'] = config.get('advanced', 'sdr_power_path') auto_rx_config['sdr_quantity'] = config.getint('sdr', 'sdr_quantity') # Search Parameters auto_rx_config['min_freq'] = config.getfloat('search_params', 'min_freq') auto_rx_config['max_freq'] = config.getfloat('search_params', 'max_freq') auto_rx_config['rx_timeout'] = config.getint('search_params', 'rx_timeout') auto_rx_config['whitelist'] = json.loads( config.get('search_params', 'whitelist')) auto_rx_config['blacklist'] = json.loads( config.get('search_params', 'blacklist')) auto_rx_config['greylist'] = json.loads( config.get('search_params', 'greylist')) # Location Settings auto_rx_config['station_lat'] = config.getfloat( 'location', 'station_lat') auto_rx_config['station_lon'] = config.getfloat( 'location', 'station_lon') auto_rx_config['station_alt'] = config.getfloat( 'location', 'station_alt') # Position Filtering auto_rx_config['max_altitude'] = config.getint('filtering', 'max_altitude') auto_rx_config['max_radius_km'] = config.getint( 'filtering', 'max_radius_km') # Habitat Settings auto_rx_config['habitat_enabled'] = config.getboolean( 'habitat', 'habitat_enabled') auto_rx_config['habitat_upload_rate'] = config.getint( 'habitat', 'upload_rate') auto_rx_config['habitat_payload_callsign'] = config.get( 'habitat', 'payload_callsign') auto_rx_config['habitat_uploader_callsign'] = config.get( 'habitat', 'uploader_callsign') auto_rx_config['habitat_upload_listener_position'] = config.getboolean( 'habitat', 'upload_listener_position') # APRS Settings auto_rx_config['aprs_enabled'] = config.getboolean( 'aprs', 'aprs_enabled') auto_rx_config['aprs_upload_rate'] = config.getint( 'aprs', 'upload_rate') auto_rx_config['aprs_user'] = config.get('aprs', 'aprs_user') auto_rx_config['aprs_pass'] = config.get('aprs', 'aprs_pass') auto_rx_config['aprs_server'] = config.get('aprs', 'aprs_server') auto_rx_config['aprs_object_id'] = config.get('aprs', 'aprs_object_id') auto_rx_config['aprs_custom_comment'] = config.get( 'aprs', 'aprs_custom_comment') # OziPlotter Settings auto_rx_config['ozi_enabled'] = config.getboolean( 'oziplotter', 'ozi_enabled') auto_rx_config['ozi_update_rate'] = config.getint( 'oziplotter', 'ozi_update_rate') auto_rx_config['ozi_port'] = config.getint('oziplotter', 'ozi_port') auto_rx_config['payload_summary_enabled'] = config.getboolean( 'oziplotter', 'payload_summary_enabled') auto_rx_config['payload_summary_port'] = config.getint( 'oziplotter', 'payload_summary_port') # Advanced Settings auto_rx_config['search_step'] = config.getfloat( 'advanced', 'search_step') auto_rx_config['snr_threshold'] = config.getfloat( 'advanced', 'snr_threshold') auto_rx_config['min_distance'] = config.getfloat( 'advanced', 'min_distance') auto_rx_config['dwell_time'] = config.getint('advanced', 'dwell_time') auto_rx_config['quantization'] = config.getint('advanced', 'quantization') auto_rx_config['max_peaks'] = config.getint('advanced', 'max_peaks') auto_rx_config['scan_dwell_time'] = config.getint( 'advanced', 'scan_dwell_time') auto_rx_config['detect_dwell_time'] = config.getint( 'advanced', 'detect_dwell_time') auto_rx_config['scan_delay'] = config.getint('advanced', 'scan_delay') auto_rx_config['payload_id_valid'] = config.getint( 'advanced', 'payload_id_valid') auto_rx_config['synchronous_upload'] = config.getboolean( 'advanced', 'synchronous_upload') # Rotator Settings (TBC) auto_rx_config['rotator_enabled'] = config.getboolean( 'rotator', 'rotator_enabled') auto_rx_config['rotator_update_rate'] = config.getint( 'rotator', 'update_rate') auto_rx_config['rotator_hostname'] = config.get( 'rotator', 'rotator_hostname') auto_rx_config['rotator_port'] = config.getint('rotator', 'rotator_port') auto_rx_config['rotator_homing_enabled'] = config.getboolean( 'rotator', 'rotator_homing_enabled') auto_rx_config['rotator_home_azimuth'] = config.getfloat( 'rotator', 'rotator_home_azimuth') auto_rx_config['rotator_home_elevation'] = config.getfloat( 'rotator', 'rotator_home_elevation') # New setting in this version (20180616). Keep it in a try-catch to avoid bombing out if the new setting isn't present. try: auto_rx_config['habitat_uploader_antenna'] = config.get( 'habitat', 'uploader_antenna').strip() except: logging.error( "Config - Missing uploader_antenna setting. Using default.") auto_rx_config['habitat_uploader_antenna'] = '1/4-wave' # New settings added in 20180624. try: auto_rx_config['web_port'] = config.getint('web', 'web_port') auto_rx_config['web_archive_age'] = config.getint( 'web', 'archive_age') except: logging.error( "Config - Missing Web Server settings. Using defaults.") auto_rx_config['web_port'] = 5000 auto_rx_config['web_archive_age'] = 120 # Now we attempt to read in the individual SDR parameters. auto_rx_config['sdr_settings'] = {} for _n in range(1, auto_rx_config['sdr_quantity'] + 1): _section = "sdr_%d" % _n try: _device_idx = config.get(_section, 'device_idx') _ppm = config.getint(_section, 'ppm') _gain = config.getfloat(_section, 'gain') _bias = config.getboolean(_section, 'bias') if (auto_rx_config['sdr_quantity'] > 1) and (_device_idx == '0'): logging.critical( "Config - SDR Device ID of 0 used with a multi-SDR configuration. Go read the warning in the config file!" ) return None # See if the SDR exists. _sdr_valid = rtlsdr_test(_device_idx) if _sdr_valid: auto_rx_config['sdr_settings'][_device_idx] = { 'ppm': _ppm, 'gain': _gain, 'bias': _bias, 'in_use': False, 'task': None } logging.info('Config - Tested SDR #%s OK' % _device_idx) else: logging.warning("Config - SDR #%s invalid." % _device_idx) except Exception as e: logging.error("Config - Error parsing SDR %d config - %s" % (_n, str(e))) continue # Sanity checks when using more than one SDR if (len(auto_rx_config['sdr_settings'].keys()) > 1) and (auto_rx_config['habitat_payload_callsign'] != "<id>"): logging.critical( "Fixed Habitat Payload callsign used in a multi-SDR configuration. Go read the warnings in the config file!" ) return None if (len(auto_rx_config['sdr_settings'].keys()) > 1) and (auto_rx_config['aprs_object_id'] != "<id>"): logging.critical( "Fixed APRS object ID used in a multi-SDR configuration. Go read the warnings in the config file!" ) return None if (len(auto_rx_config['sdr_settings'].keys()) > 1) and (auto_rx_config['rotator_enabled']): logging.critical( "Rotator enabled in a multi-SDR configuration. Go read the warnings in the config file!" ) return None # TODO: Revisit this limitation once the OziPlotter output sub-module is complete. if (len(auto_rx_config['sdr_settings'].keys()) > 1) and (auto_rx_config['ozi_enabled'] or auto_rx_config['payload_summary_enabled']): logging.critical( "Chase car outputs (OziPlotter/Payload Summary) enabled in a multi-SDR configuration." ) return None if len(auto_rx_config['sdr_settings'].keys()) == 0: # We have no SDRs to use!! logging.error("Config - No working SDRs! Cannot run...") return None else: # Create a global copy of the configuration file at this point global_config = copy.deepcopy(auto_rx_config) return auto_rx_config except: traceback.print_exc() logging.error("Could not parse config file.") return None
def getfloat(self, section, option, default=None): try: return RawConfigParser.getfloat(self, section, option) except Exception: return default
def sync(self): changesLoaded = False self.lock.acquire() try: config = RawConfigParser() config.read(self.configFile) except Exception as err: archas.logError("Config: Can not open %s to sync config: %s" % (self.configFile,str(err))) err = WH10_CONFIG_ERR_READ_CONFIG else: if not config.has_section(self.configSection): config.add_section(self.configSection) err = WH10_CONFIG_ERR_NO_ERR writeRequired = False for key,c in self.config.iteritems(): if c.changed: writeRequired = True try: if isinstance(c.value, int): config.set(self.configSection, key, "%i" % c.value) elif isinstance(c.value, float): config.set(self.configSection, key, "%f" % c.value) elif isinstance(c.value, unicode): config.set(self.configSection, key, c.value.encode("utf-8")) else: archas.logError("Config: Invalid value type for %s"%key) c.changed = False except Exception as err: archas.logError("Config: Failed to set config value %s (%s)." % (key, str(err))) err = WH10_CONFIG_ERR_SET_CONFIG try: fileValue = None if isinstance(c.default, int): fileValue = config.getint(self.configSection, key) elif isinstance(c.default, float): fileValue = config.getfloat(self.configSection, key) elif isinstance(c.default, unicode): fileValue = config.get(self.configSection, key).decode("utf-8") else: archas.logError("Config: Invalid default value type for %s."%key) if fileValue != None and fileValue != c.value: valid = True validator = self.validators.get(key, None) if validator != None: valid = validator(key, fileValue) if not valid: # We read the value as string for log message again v = config.get(self.configSection, key).decode("utf-8") archas.logWarning('Invalid value "%s" in configuration file for key "%s".'%(v, key)) else: c.value = fileValue c.changed = False changesLoaded = True except Exception as e: # wh10LogWarning("fail to get value for %s: %s. Setting default."%(key,str(e))) c.value = c.default c.changed = False if writeRequired: try: with open(self.configFile, 'wb') as cfg_file: config.write(cfg_file) except: archas.logError("Config: Failed to write config values to %s." % self.configFile) err = WH10_CONFIG_ERR_WRITE_CONFIG self.lock.release() # Config change handler is called on local and file changes if (changesLoaded or writeRequired): if callable(self.configChangeHandler): self.configChangeHandler() return err
def getfloat(self, section, option, default=None): if self.has_option(section, option) or not isinstance(default, float): return RawConfigParser.getfloat(self, section, option) else: return default
def read_auto_rx_config(filename, no_sdr_test=False): """Read an Auto-RX v2 Station Configuration File. This function will attempt to parse a configuration file. It will also confirm the accessibility of any SDRs specified in the config file. Args: filename (str): Filename of the configuration file to read. no_sdr_test (bool): Skip testing the SDRs (used for some unit tests) Returns: auto_rx_config (dict): The configuration dictionary. sdr_config (dict): A dictionary with SDR parameters. """ global global_config, web_password # Configuration Defaults: auto_rx_config = { # Log Settings "per_sonde_log": True, # Email Settings "email_enabled": False, #'email_error_notifications': False, "email_smtp_server": "localhost", "email_smtp_port": 25, "email_smtp_authentication": "None", "email_smtp_login": "******", "email_smtp_password": "******", "email_from": "sonde@localhost", "email_to": None, "email_subject": "<type> Sonde launch detected on <freq>: <id>", # SDR Settings "sdr_fm": "rtl_fm", "sdr_power": "rtl_power", "sdr_quantity": 1, # Search Parameters "min_freq": 400.4, "max_freq": 404.0, "rx_timeout": 120, "only_scan": [], "never_scan": [], "always_scan": [], # Location Settings "station_lat": 0.0, "station_lon": 0.0, "station_alt": 0.0, "station_code": "SONDE", # NOTE: This will not be read from the config file, but will be left in place for now # as a default setting. "gpsd_enabled": False, "gpsd_host": "localhost", "gpsd_port": 2947, # Position Filter Settings "max_altitude": 50000, "max_radius_km": 1000, "min_radius_km": 0, "radius_temporary_block": False, # "sonde_time_threshold": 3, # Commented out to ensure warning message is shown. # Habitat Settings "habitat_enabled": False, "habitat_upload_rate": 30, "habitat_uploader_callsign": "SONDE_AUTO_RX", "habitat_uploader_antenna": "1/4-wave", "habitat_upload_listener_position": False, "habitat_payload_callsign": "<id>", # APRS Settings "aprs_enabled": False, "aprs_upload_rate": 30, "aprs_user": "******", "aprs_pass": "******", "aprs_server": "rotate.aprs2.net", "aprs_object_id": "<id>", #'aprs_use_custom_object_id': False, "aprs_custom_comment": "Radiosonde Auto-RX <freq>", "aprs_position_report": False, "station_beacon_enabled": False, "station_beacon_rate": 30, "station_beacon_comment": "radiosonde_auto_rx SondeGate v<version>", "station_beacon_icon": "/r", # Web Settings, "web_host": "0.0.0.0", "web_port": 5000, "web_archive_age": 120, "web_control": False, # "web_password": "******", # Commented out to ensure warning message is shown #'kml_refresh_rate': 10, # Advanced Parameters "search_step": 800, "snr_threshold": 10, "min_distance": 1000, "dwell_time": 10, "max_peaks": 10, "quantization": 10000, "decoder_spacing_limit": 15000, "synchronous_upload": False, "scan_dwell_time": 20, "detect_dwell_time": 5, "scan_delay": 10, "payload_id_valid": 5, "temporary_block_time": 60, "rs41_drift_tweak": False, "decoder_stats": False, "ngp_tweak": False, # Rotator Settings "enable_rotator": False, "rotator_update_rate": 30, "rotator_hostname": "127.0.0.1", "rotator_port": 4533, "rotation_threshold": 5.0, "rotator_homing_enabled": False, "rotator_homing_delay": 10, "rotator_home_azimuth": 0, "rotator_home_elevation": 0, # OziExplorer Settings "ozi_enabled": False, "ozi_update_rate": 5, "ozi_port": 55681, "payload_summary_enabled": False, "payload_summary_port": 55672, # Debugging settings "save_detection_audio": False, "save_decode_audio": False, "save_decode_iq": False, # URL for the Habitat DB Server. # As of July 2018 we send via sondehub.org, which will allow us to eventually transition away # from using the habhub.org tracker, and leave it for use by High-Altitude Balloon Hobbyists. # For now, sondehub.org just acts as a proxy to habhub.org. # This setting is not exposed to users as it's only used for unit/int testing "habitat_url": "https://habitat.sondehub.org/", # New Sondehub DB Settings "sondehub_enabled": True, "sondehub_upload_rate": 30, # "sondehub_contact_email": "*****@*****.**" # Commented out to ensure a warning message is shown on startup } try: # Check the file exists. if not os.path.isfile(filename): logging.critical("Config file %s does not exist!" % filename) return None config = RawConfigParser(auto_rx_config) config.read(filename) # Log Settings auto_rx_config["per_sonde_log"] = config.getboolean( "logging", "per_sonde_log") # Email Settings if config.has_option("email", "email_enabled"): try: auto_rx_config["email_enabled"] = config.getboolean( "email", "email_enabled") auto_rx_config["email_smtp_server"] = config.get( "email", "smtp_server") auto_rx_config["email_smtp_port"] = config.get( "email", "smtp_port") auto_rx_config["email_smtp_authentication"] = config.get( "email", "smtp_authentication") auto_rx_config["email_smtp_login"] = config.get( "email", "smtp_login") auto_rx_config["email_smtp_password"] = config.get( "email", "smtp_password") auto_rx_config["email_from"] = config.get("email", "from") auto_rx_config["email_to"] = config.get("email", "to") auto_rx_config["email_subject"] = config.get( "email", "subject") if auto_rx_config["email_smtp_authentication"] not in [ "None", "TLS", "SSL", ]: logging.error( "Config - Invalid email authentication setting. Must be None, TLS or SSL." ) return None except: logging.error( "Config - Invalid or missing email settings. Disabling.") auto_rx_config["email_enabled"] = False # SDR Settings auto_rx_config["sdr_fm"] = config.get("advanced", "sdr_fm_path") auto_rx_config["sdr_power"] = config.get("advanced", "sdr_power_path") auto_rx_config["sdr_quantity"] = config.getint("sdr", "sdr_quantity") # Search Parameters auto_rx_config["min_freq"] = config.getfloat("search_params", "min_freq") auto_rx_config["max_freq"] = config.getfloat("search_params", "max_freq") auto_rx_config["rx_timeout"] = config.getint("search_params", "rx_timeout") if (config.has_option("search_params", "only_scan") and config.get("search_params", "only_scan") != ""): # check if user has new name for scan lists auto_rx_config["only_scan"] = json.loads( config.get("search_params", "only_scan")) else: logging.warning( "Config - whitelist configuration has been deprecated and replaced with only_scan list" ) auto_rx_config["only_scan"] = json.loads( config.get("search_params", "whitelist")) if (config.has_option("search_params", "never_scan") and config.get("search_params", "never_scan") != ""): # check if user has new name for scan lists auto_rx_config["never_scan"] = json.loads( config.get("search_params", "never_scan")) else: logging.warning( "Config - blacklist configuration has been deprecated and replaced with never_scan list" ) auto_rx_config["never_scan"] = json.loads( config.get("search_params", "blacklist")) if (config.has_option("search_params", "always_scan") and config.get("search_params", "always_scan") != ""): # check if user has new name for scan lists auto_rx_config["always_scan"] = json.loads( config.get("search_params", "always_scan")) else: logging.warning( "Config - greylist configuration has been deprecated and replaced with always_scan list" ) auto_rx_config["always_scan"] = json.loads( config.get("search_params", "greylist")) # Location Settings auto_rx_config["station_lat"] = config.getfloat( "location", "station_lat") auto_rx_config["station_lon"] = config.getfloat( "location", "station_lon") auto_rx_config["station_alt"] = config.getfloat( "location", "station_alt") # Position Filtering auto_rx_config["max_altitude"] = config.getint("filtering", "max_altitude") auto_rx_config["max_radius_km"] = config.getint( "filtering", "max_radius_km") # Habitat Settings # Deprecated from v1.5.0 # auto_rx_config["habitat_enabled"] = config.getboolean( # "habitat", "habitat_enabled" # ) # auto_rx_config["habitat_upload_rate"] = config.getint("habitat", "upload_rate") auto_rx_config["habitat_uploader_callsign"] = config.get( "habitat", "uploader_callsign") auto_rx_config["habitat_upload_listener_position"] = config.getboolean( "habitat", "upload_listener_position") auto_rx_config["habitat_uploader_antenna"] = config.get( "habitat", "uploader_antenna").strip() # try: # Use the default configuration if not found # auto_rx_config["habitat_url"] = config.get("habitat", "url") # except: # pass # Deprecated from v1.5.0 # if auto_rx_config["habitat_upload_rate"] < MINIMUM_HABITAT_UPDATE_RATE: # logging.warning( # "Config - Habitat Update Rate clipped to minimum of %d seconds. Please be respectful of other users of Habitat." # % MINIMUM_HABITAT_UPDATE_RATE # ) # auto_rx_config["habitat_upload_rate"] = MINIMUM_HABITAT_UPDATE_RATE # APRS Settings auto_rx_config["aprs_enabled"] = config.getboolean( "aprs", "aprs_enabled") auto_rx_config["aprs_upload_rate"] = config.getint( "aprs", "upload_rate") auto_rx_config["aprs_user"] = config.get("aprs", "aprs_user") auto_rx_config["aprs_pass"] = config.get("aprs", "aprs_pass") auto_rx_config["aprs_server"] = config.get("aprs", "aprs_server") auto_rx_config["aprs_object_id"] = config.get("aprs", "aprs_object_id") auto_rx_config["aprs_custom_comment"] = config.get( "aprs", "aprs_custom_comment") auto_rx_config["aprs_position_report"] = config.getboolean( "aprs", "aprs_position_report") auto_rx_config["station_beacon_enabled"] = config.getboolean( "aprs", "station_beacon_enabled") auto_rx_config["station_beacon_rate"] = config.getint( "aprs", "station_beacon_rate") auto_rx_config["station_beacon_comment"] = config.get( "aprs", "station_beacon_comment") auto_rx_config["station_beacon_icon"] = config.get( "aprs", "station_beacon_icon") if auto_rx_config["aprs_upload_rate"] < MINIMUM_APRS_UPDATE_RATE: logging.warning( "Config - APRS Update Rate clipped to minimum of %d seconds. Please be respectful of other users of APRS-IS." % MINIMUM_APRS_UPDATE_RATE) auto_rx_config["aprs_upload_rate"] = MINIMUM_APRS_UPDATE_RATE # OziPlotter Settings auto_rx_config["ozi_enabled"] = config.getboolean( "oziplotter", "ozi_enabled") auto_rx_config["ozi_update_rate"] = config.getint( "oziplotter", "ozi_update_rate") auto_rx_config["ozi_port"] = config.getint("oziplotter", "ozi_port") auto_rx_config["payload_summary_enabled"] = config.getboolean( "oziplotter", "payload_summary_enabled") auto_rx_config["payload_summary_port"] = config.getint( "oziplotter", "payload_summary_port") # Advanced Settings auto_rx_config["search_step"] = config.getfloat( "advanced", "search_step") auto_rx_config["snr_threshold"] = config.getfloat( "advanced", "snr_threshold") auto_rx_config["min_distance"] = config.getfloat( "advanced", "min_distance") auto_rx_config["dwell_time"] = config.getint("advanced", "dwell_time") auto_rx_config["quantization"] = config.getint("advanced", "quantization") auto_rx_config["max_peaks"] = config.getint("advanced", "max_peaks") auto_rx_config["scan_dwell_time"] = config.getint( "advanced", "scan_dwell_time") auto_rx_config["detect_dwell_time"] = config.getint( "advanced", "detect_dwell_time") auto_rx_config["scan_delay"] = config.getint("advanced", "scan_delay") auto_rx_config["payload_id_valid"] = config.getint( "advanced", "payload_id_valid") auto_rx_config["synchronous_upload"] = config.getboolean( "advanced", "synchronous_upload") # Rotator Settings auto_rx_config["rotator_enabled"] = config.getboolean( "rotator", "rotator_enabled") auto_rx_config["rotator_update_rate"] = config.getint( "rotator", "update_rate") auto_rx_config["rotator_hostname"] = config.get( "rotator", "rotator_hostname") auto_rx_config["rotator_port"] = config.getint("rotator", "rotator_port") auto_rx_config["rotator_homing_enabled"] = config.getboolean( "rotator", "rotator_homing_enabled") auto_rx_config["rotator_home_azimuth"] = config.getfloat( "rotator", "rotator_home_azimuth") auto_rx_config["rotator_home_elevation"] = config.getfloat( "rotator", "rotator_home_elevation") auto_rx_config["rotator_homing_delay"] = config.getint( "rotator", "rotator_homing_delay") auto_rx_config["rotation_threshold"] = config.getfloat( "rotator", "rotation_threshold") # Web interface settings. auto_rx_config["web_host"] = config.get("web", "web_host") auto_rx_config["web_port"] = config.getint("web", "web_port") auto_rx_config["web_archive_age"] = config.getint("web", "archive_age") auto_rx_config["save_detection_audio"] = config.getboolean( "debugging", "save_detection_audio") auto_rx_config["save_decode_audio"] = config.getboolean( "debugging", "save_decode_audio") auto_rx_config["save_decode_iq"] = config.getboolean( "debugging", "save_decode_iq") # NOTE 2019-09-21: The station code will now be fixed at the default to avoid multiple iMet callsign issues. # auto_rx_config['station_code'] = config.get('location', 'station_code') # if len(auto_rx_config['station_code']) > 5: # auto_rx_config['station_code'] = auto_rx_config['station_code'][:5] # logging.warning("Config - Clipped station code to 5 digits: %s" % auto_rx_config['station_code']) auto_rx_config["temporary_block_time"] = config.getint( "advanced", "temporary_block_time") # New demod tweaks - Added 2019-04-23 # Default to experimental decoders on for FSK/GFSK sondes... auto_rx_config["experimental_decoders"] = { "RS41": True, "RS92": True, "DFM": True, "M10": True, "M20": True, "IMET": False, "IMET5": True, "LMS6": True, "MK2LMS": False, "MEISEI": False, "MRZ": False, # .... except for the MRZ, until we know it works. "UDP": False, } auto_rx_config["decoder_spacing_limit"] = config.getint( "advanced", "decoder_spacing_limit") auto_rx_config["experimental_decoders"]["RS41"] = config.getboolean( "advanced", "rs41_experimental") auto_rx_config["experimental_decoders"]["RS92"] = config.getboolean( "advanced", "rs92_experimental") auto_rx_config["experimental_decoders"]["M10"] = config.getboolean( "advanced", "m10_experimental") auto_rx_config["experimental_decoders"]["DFM"] = config.getboolean( "advanced", "dfm_experimental") auto_rx_config["experimental_decoders"]["LMS6"] = config.getboolean( "advanced", "lms6-400_experimental") try: auto_rx_config["web_control"] = config.getboolean( "web", "web_control") auto_rx_config["ngp_tweak"] = config.getboolean( "advanced", "ngp_tweak") auto_rx_config["gpsd_enabled"] = config.getboolean( "location", "gpsd_enabled") auto_rx_config["gpsd_host"] = config.get("location", "gpsd_host") auto_rx_config["gpsd_port"] = config.getint( "location", "gpsd_port") except: logging.warning( "Config - Did not find web control / ngp_tweak / gpsd options, using defaults (disabled)" ) auto_rx_config["web_control"] = False auto_rx_config["ngp_tweak"] = False auto_rx_config["gpsd_enabled"] = False try: auto_rx_config["min_radius_km"] = config.getint( "filtering", "min_radius_km") auto_rx_config["radius_temporary_block"] = config.getboolean( "filtering", "radius_temporary_block") except: logging.warning( "Config - Did not find minimum radius filter setting, using default (0km)." ) auto_rx_config["min_radius_km"] = 0 auto_rx_config["radius_temporary_block"] = False try: auto_rx_config["aprs_use_custom_object_id"] = config.getboolean( "aprs", "aprs_use_custom_object_id") except: logging.warning( "Config - Did not find aprs_use_custom_object_id setting, using default (False)" ) auto_rx_config["aprs_use_custom_object_id"] = False try: auto_rx_config["aprs_port"] = config.getint("aprs", "aprs_port") except: logging.warning( "Config - Did not find aprs_port setting - using default of 14590. APRS packets might not be forwarded out to the wider APRS-IS network!" ) auto_rx_config["aprs_port"] = 14590 try: auto_rx_config["email_error_notifications"] = config.getboolean( "email", "error_notifications") auto_rx_config["email_launch_notifications"] = config.getboolean( "email", "launch_notifications") auto_rx_config["email_landing_notifications"] = config.getboolean( "email", "landing_notifications") auto_rx_config["email_landing_range_threshold"] = config.getfloat( "email", "landing_range_threshold") auto_rx_config[ "email_landing_altitude_threshold"] = config.getfloat( "email", "landing_altitude_threshold") except: logging.warning( "Config - Did not find new email settings (v1.3.3), using defaults" ) auto_rx_config["email_error_notifications"] = False auto_rx_config["email_launch_notifications"] = True auto_rx_config["email_landing_notifications"] = True auto_rx_config["email_landing_range_threshold"] = 30 auto_rx_config["email_landing_altitude_threshold"] = 1000 try: auto_rx_config["kml_refresh_rate"] = config.getint( "web", "kml_refresh_rate") except: logging.warning( "Config - Did not find kml_refresh_rate setting, using default (10 seconds)." ) auto_rx_config["kml_refresh_rate"] = 11 # New Sondehub db Settings try: auto_rx_config["sondehub_enabled"] = config.getboolean( "sondehub", "sondehub_enabled") auto_rx_config["sondehub_upload_rate"] = config.getint( "sondehub", "sondehub_upload_rate") except: logging.warning( "Config - Did not find sondehub_enabled setting, using default (enabled / 15 seconds)." ) auto_rx_config["sondehub_enabled"] = True auto_rx_config["sondehub_upload_rate"] = 15 try: auto_rx_config["experimental_decoders"]["MRZ"] = config.getboolean( "advanced", "mrz_experimental") except: logging.warning( "Config - Did not find MRZ decoder experimental decoder setting, using default (disabled)." ) auto_rx_config["experimental_decoders"]["MRZ"] = False try: auto_rx_config["experimental_decoders"][ "IMET5"] = config.getboolean("advanced", "imet54_experimental") except: logging.warning( "Config - Did not find iMet-54 decoder experimental decoder setting, using default (enabled)." ) auto_rx_config["experimental_decoders"]["IMET5"] = True # Sondehub Contact email (1.5.1) try: auto_rx_config["sondehub_contact_email"] = config.get( "sondehub", "sondehub_contact_email") except: logging.warning( "Config - Did not find Sondehub contact e-mail setting, using default (none)." ) auto_rx_config["sondehub_contact_email"] = "*****@*****.**" # Sonde time threshold (1.5.1) try: auto_rx_config["sonde_time_threshold"] = config.getfloat( "filtering", "sonde_time_threshold") except: logging.warning( "Config - Did not find Sonde Time Threshold, using default (3 hrs)." ) auto_rx_config["sonde_time_threshold"] = 3 # Web control password try: auto_rx_config["web_password"] = config.get("web", "web_password") if auto_rx_config["web_password"] == "none": logging.warning( "Config - Web Password not set, disabling web control") auto_rx_config["web_control"] = True except: logging.warning( "Config - Did not find Web Password setting, using default (web control disabled)" ) auto_rx_config["web_control"] = False auto_rx_config["web_password"] = "******" # If we are being called as part of a unit test, just return the config now. if no_sdr_test: return auto_rx_config # Now we attempt to read in the individual SDR parameters. auto_rx_config["sdr_settings"] = {} for _n in range(1, auto_rx_config["sdr_quantity"] + 1): _section = "sdr_%d" % _n try: _device_idx = config.get(_section, "device_idx") _ppm = round(config.getfloat(_section, "ppm")) _gain = config.getfloat(_section, "gain") _bias = config.getboolean(_section, "bias") if (auto_rx_config["sdr_quantity"] > 1) and (_device_idx == "0"): logging.critical( "Config - SDR Device ID of 0 used with a multi-SDR configuration. Go read the warning in the config file!" ) return None # See if the SDR exists. _sdr_valid = rtlsdr_test(_device_idx) if _sdr_valid: auto_rx_config["sdr_settings"][_device_idx] = { "ppm": _ppm, "gain": _gain, "bias": _bias, "in_use": False, "task": None, } logging.info("Config - Tested SDR #%s OK" % _device_idx) else: logging.warning("Config - SDR #%s invalid." % _device_idx) except Exception as e: logging.error("Config - Error parsing SDR %d config - %s" % (_n, str(e))) continue # Sanity checks when using more than one SDR if (len(auto_rx_config["sdr_settings"].keys()) > 1) and (auto_rx_config["aprs_object_id"] != "<id>"): logging.critical( "Fixed APRS object ID used in a multi-SDR configuration. Go read the warnings in the config file!" ) return None if (len(auto_rx_config["sdr_settings"].keys()) > 1) and (auto_rx_config["rotator_enabled"]): logging.critical( "Rotator enabled in a multi-SDR configuration. Go read the warnings in the config file!" ) return None # TODO: Revisit this limitation once the OziPlotter output sub-module is complete. if (len(auto_rx_config["sdr_settings"].keys()) > 1) and auto_rx_config["ozi_enabled"]: logging.critical( "Oziplotter output enabled in a multi-SDR configuration.") return None if len(auto_rx_config["sdr_settings"].keys()) == 0: # We have no SDRs to use!! logging.error("Config - No working SDRs! Cannot run...") return None else: # Create a global copy of the configuration file at this point global_config = copy.deepcopy(auto_rx_config) # Excise some sensitive parameters from the global config. global_config.pop("email_smtp_login") global_config.pop("email_smtp_password") global_config.pop("email_smtp_server") global_config.pop("email_smtp_port") global_config.pop("email_from") global_config.pop("email_to") global_config.pop("email_smtp_authentication") global_config.pop("sondehub_contact_email") global_config.pop("web_password") web_password = auto_rx_config["web_password"] return auto_rx_config except: traceback.print_exc() logging.error("Could not parse config file.") return None
from ConfigParser import RawConfigParser logging.basicConfig(level=logging.DEBUG) config = RawConfigParser() config.read('config.ini') db = MySQLdb.connect( host=config.get('database', 'host'), user=config.get('database', 'user'), passwd=config.get('database', 'password'), db=config.get('database', 'database')) cur = db.cursor() READING_INTERVAL = config.getfloat('main', 'reading_interval') ser = serial.Serial(config.get('main', 'serial_port'), 9600, timeout=1) time.sleep(1) logging.debug(ser.readline()) time.sleep(10) def parse_response(line): h, c = line.split(' ') return float(h), float(c)
import sqlite3 import subprocess import smtplib import email.mime.text # Read settings from config file from ConfigParser import RawConfigParser settings = RawConfigParser() settings.read("cups_quota.conf") cups_pagelog_location = settings.get("general", "cups_pagelog_location") default_page_quota = settings.getint("general", "default_page_quota") initial_page_number = settings.getint("general", "initial_page_number") monthly_pagenumber_decrease = settings.getint("general", "monthly_pagenumber_decrease") color_factor = settings.getfloat("general", "color_factor") sleep_duration = settings.getfloat("general", "sleep_duration") ldap_server = settings.get("ldap", "server") ldap_base = settings.get("ldap", "base") ldap_user = settings.get("ldap", "user") ldap_password = settings.get("ldap", "password") noprinting_group = settings.get("ldap", "noprinting_group") ldap_uid_attribute = settings.get("ldap", "uid_attribute") smtp_server = settings.get("mail", "smtp_server") mail_from = settings.get("mail", "from") error_recipient = settings.get("mail", "error_recipient") def error_msg(msg): print "Send email to %s" % error_recipient
class MyConfig(MyLog): #---------------------MyConfig::__init__------------------------------------ def __init__(self, filename=None, section=None, log=None): super(MyLog, self).__init__() self.log = log self.FileName = filename self.Section = section self.CriticalLock = threading.Lock( ) # Critical Lock (writing conf file) self.InitComplete = False self.LogLocation = "/var/log/" self.Latitude = 51.4769 self.Longitude = 0 self.SendRepeat = 1 self.UseHttps = False self.HTTPPort = 80 self.HTTPSPort = 443 self.RTS_Address = "0x279620" self.Shutters = {} self.ShuttersByName = {} self.Schedule = {} self.Password = "" try: self.config = RawConfigParser() self.config.read(self.FileName) if self.Section == None: SectionList = self.GetSections() if len(SectionList): self.Section = SectionList[0] except Exception as e1: self.LogErrorLine("Error in MyConfig:init: " + str(e1)) return self.InitComplete = True # -------------------- MyConfig::LoadConfig----------------------------------- def LoadConfig(self): parameters = { 'LogLocation': str, 'Latitude': float, 'Longitude': float, 'SendRepeat': int, 'UseHttps': bool, 'HTTPPort': int, 'HTTPSPort': int, 'TXGPIO': int, 'RTS_Address': str, "Password": str } self.SetSection("General") for key, type in parameters.items(): try: if self.HasOption(key): setattr(self, key, self.ReadValue(key, return_type=type)) except Exception as e1: self.LogErrorLine( "Missing config file or config file entries in Section General for key " + key + ": " + str(e1)) return False parameters = { 'MQTT_Server': str, 'MQTT_Port': int, 'MQTT_User': str, 'MQTT_Password': str, 'EnableDiscovery': bool } self.SetSection("MQTT") for key, type in parameters.items(): try: if self.HasOption(key): setattr(self, key, self.ReadValue(key, return_type=type)) except Exception as e1: self.LogErrorLine( "Missing config file or config file entries in Section General for key " + key + ": " + str(e1)) return False self.SetSection("Shutters") shutters = self.GetList() for key, value in shutters: try: param1 = value.split(",") if param1[1].strip().lower() == 'true': if (len(param1) < 3): param1.append("10") elif (param1[2].strip() == "") or (int( param1[2]) <= 0) or (int(param1[2]) >= 100): param1[2] = "10" param2 = int( self.ReadValue(key, section="ShutterRollingCodes", return_type=int)) self.Shutters[key] = { 'name': param1[0], 'code': param2, 'duration': int(param1[2]) } self.ShuttersByName[param1[0]] = key except Exception as e1: self.LogErrorLine( "Missing config file or config file entries in Section Shutters for key " + key + ": " + str(e1)) return False self.SetSection("Scheduler") schedules = self.GetList() for key, value in schedules: try: param = value.split(",") if param[0].strip().lower() in ('active', 'paused'): self.Schedule[key] = { 'active': param[0], 'repeatType': param[1], 'repeatValue': param[2], 'timeType': param[3], 'timeValue': param[4], 'shutterAction': param[5], 'shutterIds': param[6] } except Exception as e1: self.LogErrorLine( "Missing config file or config file entries in Section Scheduler for key " + key + ": " + str(e1)) return False return True #---------------------MyConfig::setLocation--------------------------------- def setLocation(self, lat, lng): self.WriteValue("Latitude", lat, section="General") self.WriteValue("Longitude", lng, section="General") self.Latitude = lat self.Longitude = lng #---------------------MyConfig::setCode--------------------------------- def setCode(self, shutterId, code): self.WriteValue(shutterId, str(code), section="ShutterRollingCodes") self.Shutters[shutterId]['code'] = code #---------------------MyConfig::HasOption----------------------------------- def HasOption(self, Entry): return self.config.has_option(self.Section, Entry) #---------------------MyConfig::GetList------------------------------------- def GetList(self): return self.config.items(self.Section) #---------------------MyConfig::GetSections--------------------------------- def GetSections(self): return self.config.sections() #---------------------MyConfig::SetSection---------------------------------- def SetSection(self, section): # if not (isinstance(section, str) or isinstance(section, unicode)) or not len(section): if not len(section): self.LogError("Error in MyConfig:ReadValue: invalid section: " + str(section)) return False self.Section = section return True #---------------------MyConfig::ReadValue----------------------------------- def ReadValue(self, Entry, return_type=str, default=None, section=None, NoLog=False): try: if section != None: self.SetSection(section) if self.config.has_option(self.Section, Entry): if return_type == str: return self.config.get(self.Section, Entry) elif return_type == bool: return self.config.getboolean(self.Section, Entry) elif return_type == float: return self.config.getfloat(self.Section, Entry) elif return_type == int: return self.config.getint(self.Section, Entry) else: self.LogErrorLine( "Error in MyConfig:ReadValue: invalid type:" + str(return_type)) return default else: return default except Exception as e1: if not NoLog: self.LogErrorLine("Error in MyConfig:ReadValue: " + Entry + ": " + str(e1)) return default #---------------------MyConfig::WriteSection-------------------------------- def WriteSection(self, SectionName): SectionList = self.GetSections() if SectionName in SectionList: self.LogError("Error in WriteSection: Section already exist.") return True try: with self.CriticalLock: with open(self.FileName, "a") as ConfigFile: ConfigFile.write("[" + SectionName + "]") ConfigFile.flush() ConfigFile.close() # update the read data that is cached self.config.read(self.FileName) return True except Exception as e1: self.LogErrorLine("Error in WriteSection: " + str(e1)) return False #---------------------MyConfig::WriteValue---------------------------------- def WriteValue(self, Entry, Value, remove=False, section=None): if section != None: self.SetSection(section) SectionFound = False try: with self.CriticalLock: Found = False ConfigFile = open(self.FileName, 'r') FileList = ConfigFile.read().splitlines() ConfigFile.close() mySectionStart = -1 mySectionEnd = -1 myLine = -1 currentLastDataLine = -1 for i, line in enumerate(FileList): if self.LineIsSection(line) and self.Section.lower( ) == self.GetSectionName(line).lower(): mySectionStart = i elif mySectionStart >= 0 and mySectionEnd == -1 and len( line.strip().split('=')) >= 2 and ( line.strip().split('='))[0].strip() == Entry: myLine = i elif mySectionStart >= 0 and mySectionEnd == -1 and self.LineIsSection( line): mySectionEnd = currentLastDataLine if not line.isspace() and not len( line.strip()) == 0 and not line.strip()[0] == "#": currentLastDataLine = i if mySectionStart >= 0 and mySectionEnd == -1: mySectionEnd = currentLastDataLine self.LogDebug("CONFIG FILE WRITE ->> mySectionStart = " + str(mySectionStart) + ", mySectionEnd = " + str(mySectionEnd) + ", myLine = " + str(myLine)) if mySectionStart == -1: raise Exception("NOT ABLE TO FIND SECTION:" + self.Section) ConfigFile = open(self.FileName, 'w') for i, line in enumerate(FileList): if myLine >= 0 and myLine == i and not remove: # I found my line, now write new value ConfigFile.write(Entry + " = " + Value + "\n") elif myLine == -1 and mySectionEnd == i: # Here we have to insert the new record... ConfigFile.write(line + "\n") ConfigFile.write(Entry + " = " + Value + "\n") else: # Nothing special, just copy the previous line.... ConfigFile.write(line + "\n") ConfigFile.flush() ConfigFile.close() # update the read data that is cached self.config.read(self.FileName) return True except Exception as e1: self.LogError("Error in WriteValue: " + str(e1)) return False #---------------------MyConfig::GetSectionName------------------------------ def GetSectionName(self, Line): Line = Line.strip() if Line.startswith("[") and Line.endswith("]") and len(Line) >= 3: Line = Line.replace("[", "") Line = Line.replace("]", "") return Line return "" #---------------------MyConfig::LineIsSection------------------------------- def LineIsSection(self, Line): Line = Line.strip() if Line.startswith("[") and Line.endswith("]") and len(Line) >= 3: return True return False
if sys.hexversion < 0x03000000: from ConfigParser import RawConfigParser else: from configparser import RawConfigParser import logging from math import expm1 from component import Component DEBUG = False logger = logging.getLogger('fancontrol') config = RawConfigParser() config.read('fancontrol.cfg') ventilation_period = config.getfloat('fan', 'ventilation_period') class Fan(Component): def __init__(self): Component.__init__(self, 'fan') self.mode = None self.fanState = None self.lastOff = None self.stayOnUntil = 0 self.stayOffUntil = 0 def __enter__(self): with self.lock: self.messageboard.subscribe('Mode', self, Fan.onMode) self.messageboard.subscribe('Time', self, Fan.onTime) return Component.__enter__(self)
pub = publisher.NoisyPublisher("gatherer") # TODO: get this from the product config files regions = [ get_area_def(region) for region in config.get("default", "regions").split() ] for section in config.sections(): if section == "default": continue timeliness = timedelta(minutes=config.getint(section, "timeliness")) try: duration = timedelta(seconds=config.getfloat(section, "duration")) except NoOptionError: duration = None collectors = [ region_collector.RegionCollector(region, timeliness, duration) for region in regions ] try: pattern = config.get(section, "pattern") try: observer_class = config.get(section, "watcher") except NoOptionError: observer_class = None logger.debug("Using watchdog for %s", section) parser = Parser(pattern)
def parse_config_file(filename): """ Parse a Configuration File """ chase_config = default_config.copy() config = RawConfigParser() config.read(filename) # Map Defaults chase_config['flask_host'] = config.get('map', 'flask_host') chase_config['flask_port'] = config.getint('map', 'flask_port') chase_config['default_lat'] = config.get('map', 'default_lat') chase_config['default_lon'] = config.get('map', 'default_lon') chase_config['payload_max_age'] = config.getint('map', 'payload_max_age') chase_config['thunderforest_api_key'] = config.get( 'map', 'thunderforest_api_key') # GPSD Settings chase_config['car_gpsd_host'] = config.get('gpsd', 'gpsd_host') chase_config['car_gpsd_port'] = config.getint('gpsd', 'gpsd_port') # Serial GPS Settings chase_config['car_serial_port'] = config.get('gps_serial', 'gps_port') chase_config['car_serial_baud'] = config.getint('gps_serial', 'gps_baud') # Habitat Settings chase_config['habitat_upload_enabled'] = config.getboolean( 'habitat', 'habitat_upload_enabled') chase_config['habitat_call'] = config.get('habitat', 'habitat_call') chase_config['habitat_update_rate'] = config.getint( 'habitat', 'habitat_update_rate') # Predictor chase_config['pred_enabled'] = config.getboolean('predictor', 'predictor_enabled') chase_config['pred_burst'] = config.getfloat('predictor', 'default_burst') chase_config['pred_desc_rate'] = config.getfloat('predictor', 'default_descent_rate') chase_config['pred_binary'] = config.get('predictor', 'pred_binary') chase_config['pred_gfs_directory'] = config.get('predictor', 'gfs_directory') chase_config['pred_model_download'] = config.get('predictor', 'model_download') # Range Ring Settings chase_config['range_rings_enabled'] = config.getboolean( 'range_rings', 'range_rings_enabled') chase_config['range_ring_quantity'] = config.getint( 'range_rings', 'range_ring_quantity') chase_config['range_ring_spacing'] = config.getint('range_rings', 'range_ring_spacing') chase_config['range_ring_weight'] = config.getfloat( 'range_rings', 'range_ring_weight') chase_config['range_ring_color'] = config.get('range_rings', 'range_ring_color') chase_config['range_ring_custom_color'] = config.get( 'range_rings', 'range_ring_custom_color') # Bearing Processing chase_config['max_bearings'] = config.getint('bearings', 'max_bearings') chase_config['max_bearing_age'] = config.getint( 'bearings', 'max_bearing_age') * 60 # Convert to seconds if chase_config['max_bearing_age'] < 60: chase_config[ 'max_bearing_age'] = 60 # Make sure this number is something sane, otherwise things will break chase_config['car_speed_gate'] = config.getfloat( 'bearings', 'car_speed_gate') / 3.6 # Convert to m/s chase_config['bearing_length'] = config.getfloat('bearings', 'bearing_length') chase_config['bearing_weight'] = config.getfloat('bearings', 'bearing_weight') chase_config['bearing_color'] = config.get('bearings', 'bearing_color') chase_config['bearing_custom_color'] = config.get('bearings', 'bearing_custom_color') # Offline Map Settings chase_config['tile_server_enabled'] = config.getboolean( 'offline_maps', 'tile_server_enabled') chase_config['tile_server_path'] = config.get('offline_maps', 'tile_server_path') # Determine valid offline map layers. chase_config['offline_tile_layers'] = [] if chase_config['tile_server_enabled']: for _dir in os.listdir(chase_config['tile_server_path']): if os.path.isdir( os.path.join(chase_config['tile_server_path'], _dir)): chase_config['offline_tile_layers'].append(_dir) logging.info("Found Map Layers: %s" % str(chase_config['offline_tile_layers'])) # Telemetry Source Profiles _profile_count = config.getint('profile_selection', 'profile_count') _default_profile = config.getint('profile_selection', 'default_profile') chase_config['selected_profile'] = "" chase_config['profiles'] = {} for i in range(1, _profile_count + 1): _profile_section = "profile_%d" % i try: _profile_name = config.get(_profile_section, 'profile_name') _profile_telem_source_type = config.get(_profile_section, 'telemetry_source_type') _profile_telem_source_port = config.getint( _profile_section, 'telemetry_source_port') _profile_car_source_type = config.get(_profile_section, 'car_source_type') _profile_car_source_port = config.getint(_profile_section, 'car_source_port') chase_config['profiles'][_profile_name] = { 'name': _profile_name, 'telemetry_source_type': _profile_telem_source_type, 'telemetry_source_port': _profile_telem_source_port, 'car_source_type': _profile_car_source_type, 'car_source_port': _profile_car_source_port } if _default_profile == i: chase_config['selected_profile'] = _profile_name except Exception as e: logging.error("Error reading profile section %d - %s" % (i, str(e))) if len(chase_config['profiles'].keys()) == 0: logging.critical("Could not read any profile data!") return None if chase_config['selected_profile'] not in chase_config['profiles']: logging.critical("Default profile selection does not exist.") return None return chase_config
def getfloat(self, section, option, default=None): try: return RawConfigParser.getfloat(self, section, option) except: return default
class Config(object): """A wrapper around RawConfigParser""" def __init__(self, version=None): """Use read() to read in an existing config file. version should be an int starting with 0 that gets incremented if you want to register a new upgrade function. If None, upgrade is disabled. """ self._config = ConfigParser(dict_type=_sorted_dict) self._version = version self._loaded_version = None self._upgrade_funcs = [] self._initial = {} def _do_upgrade(self, func): assert self._loaded_version is not None assert self._version is not None old_version = self._loaded_version new_version = self._version if old_version != new_version: print_d("Config upgrade: %d->%d (%r)" % ( old_version, new_version, func)) func(self, old_version, new_version) def get_version(self): """Get the version of the loaded config file (for testing only) Raises Error if no file was loaded or versioning is disabled. """ if self._version is None: raise Error("Versioning disabled") if self._loaded_version is None: raise Error("No file loaded") return self._loaded_version def register_upgrade_function(self, function): """Register an upgrade function that gets called at each read() if the current config version and the loaded version don't match. Can also be registered after read was called. function(config, old_version: int, new_version: int) -> None """ if self._version is None: raise Error("Versioning disabled") self._upgrade_funcs.append(function) # after read(), so upgrade now if self._loaded_version is not None: self._do_upgrade(function) return function def set_inital(self, section, option, value): """Set an initial value for an option. The section must be added with add_section() first. Adds the value to the config and calling reset() will reset the value to it. """ self.set(section, option, value) self._initial.setdefault(section, {}) self._initial[section].setdefault(option, {}) self._initial[section][option] = value def reset(self, section, option): """Reset the value to the initial state""" value = self._initial[section][option] self.set(section, option, value) def options(self, section): """Returns a list of options available in the specified section.""" return self._config.options(section) def get(self, section, option, default=_DEFAULT): """get(section, option[, default]) -> str If default is not given, raises Error in case of an error """ try: return self._config.get(section, option) except Error: if default is _DEFAULT: raise return default def getboolean(self, section, option, default=_DEFAULT): """getboolean(section, option[, default]) -> bool If default is not given, raises Error in case of an error """ try: return self._config.getboolean(section, option) except Error: if default is _DEFAULT: raise if not isinstance(default, bool): raise ValueError return default def getint(self, section, option, default=_DEFAULT): """getint(section, option[, default]) -> int If default is not give, raises Error in case of an error """ try: return self._config.getint(section, option) except Error: if default is _DEFAULT: raise if not isinstance(default, int): raise ValueError return default def getfloat(self, section, option, default=_DEFAULT): """getfloat(section, option[, default]) -> float If default is not give, raises Error in case of an error """ try: return self._config.getfloat(section, option) except Error: if default is _DEFAULT: raise if not isinstance(default, float): raise ValueError return default def getstringlist(self, section, option, default=_DEFAULT): """getstringlist(section, option[, default]) -> list If default is not given, raises Error in case of an error. Gets a list of strings, using CSV to parse and delimit. """ try: value = self._config.get(section, option) except Error: if default is _DEFAULT: raise if not isinstance(default, list): raise ValueError return default parser = csv.reader( [value], lineterminator='\n', quoting=csv.QUOTE_MINIMAL) try: vals = [v.decode('utf-8') for v in parser.next()] except (csv.Error, ValueError) as e: raise Error(e) return vals def setstringlist(self, section, option, values): """Saves a list of unicode strings using the csv module""" sw = StringIO() values = [unicode(v).encode('utf-8') for v in values] writer = csv.writer(sw, lineterminator='\n', quoting=csv.QUOTE_MINIMAL) writer.writerow(values) self._config.set(section, option, sw.getvalue()) def setlist(self, section, option, values, sep=","): """Saves a list of str using ',' as a separator and \\ for escaping""" values = map(str, values) joined = join_escape(values, sep) self._config.set(section, option, joined) def getlist(self, section, option, default=_DEFAULT, sep=","): """Returns a str list saved with setlist()""" try: value = self._config.get(section, option) except Error: if default is _DEFAULT: raise if not isinstance(default, list): raise ValueError return default return split_escape(value, sep) def set(self, section, option, value): """Saves the string representation for the passed value Don't pass unicode, encode first. """ # RawConfigParser only allows string values but doesn't # scream if they are not (and it only fails before the # first config save..) if not isinstance(value, str): value = str(value) self._config.set(section, option, value) def setdefault(self, section, option, default): """Like set but only sets the new value if the option isn't set before. """ if not self._config.has_option(section, option): self._config.set(section, option, default) def write(self, filename): """Write config to filename. Can raise EnvironmentError """ assert is_fsnative(filename) mkdir(os.path.dirname(filename)) # temporary set the new version for saving if self._version is not None: self.add_section("__config__") self.set("__config__", "version", self._version) try: with atomic_save(filename, ".tmp", "wb") as fileobj: self._config.write(fileobj) finally: if self._loaded_version is not None: self.set("__config__", "version", self._loaded_version) def clear(self): """Remove all sections and initial values""" for section in self._config.sections(): self._config.remove_section(section) self._initial.clear() def is_empty(self): """Whether the config has any sections""" return not self._config.sections() def read(self, filename): """Reads the config from `filename` if the file exists, otherwise does nothing Can raise EnvironmentError, Error. """ parsed_filenames = self._config.read(filename) # don't upgrade if we just created a new config if parsed_filenames and self._version is not None: self._loaded_version = self.getint("__config__", "version", -1) for func in self._upgrade_funcs: self._do_upgrade(func) def sections(self): """Return a list of the sections available""" return self._config.sections() def has_option(self, section, option): """If the given section exists, and contains the given option""" return self._config.has_option(section, option) def remove_option(self, section, option): """Remove the specified option from the specified section Can raise Error. """ return self._config.remove_option(section, option) def add_section(self, section): """Add a section named section to the instance if it not already exists.""" if not self._config.has_section(section): self._config.add_section(section)
granule_triggers = [] pub = publisher.NoisyPublisher("gatherer") # TODO: get this from the product config files regions = [get_area_def(region) for region in config.get("default", "regions").split()] for section in config.sections(): if section == "default": continue timeliness = timedelta(minutes=config.getint(section, "timeliness")) try: duration = timedelta(seconds=config.getfloat(section, "duration")) except NoOptionError: duration = None collectors = [region_collector.RegionCollector( region, timeliness, duration) for region in regions] try: pattern = config.get(section, "pattern") try: observer_class = config.get(section, "watcher") except NoOptionError: observer_class = None logger.debug("Using watchdog for %s", section) parser = Parser(pattern) granule_trigger = trigger.WatchDogTrigger(collectors, terminator,
class Configuration(object): def __init__(self, bus, target_name, file_name): self.bus = bus self.target_name = target_name self.file_name = file_name self.config = RawConfigParser() self.config.read(file_name) if target_name is not None: self.service = bus.create_service({"type": "autoconfigure", "target": target_name}, active=False, use_py_object=_AutoconfigureService(self)) self.sections_object = self.service.create_object("sections", self._compute_sections(), doc=sections_object_doc) self.options_object = self.service.create_object("options", self._compute_options(), doc=options_object_doc) @synchronized(_lock) def _save(self): with open(self.file_name, "w") as file: self.config.write(file) self.sections_object.set_value(self._compute_sections()) self.options_object.set_value(self._compute_options()) @synchronized(_lock) def _compute_sections(self): return self.config.sections() @synchronized(_lock) def _compute_options(self): return dict((section, dict(self.config.items(section))) for section in self.config.sections()) @synchronized(_lock) def sections(self): return self.config.sections() @synchronized(_lock) def add_section(self, name): self.config.add_section(name) self._save() @synchronized(_lock) def has_section(self, name): return self.config.has_section(name) @synchronized(_lock) def options(self, section): return self.config.options(section) @synchronized(_lock) def has_option(self, section, option): return self.config.has_option(section, option) @synchronized(_lock) def get(self, section, option): return self.config.get(section, option) @synchronized(_lock) def getint(self, section, option): return self.config.getint(section, option) @synchronized(_lock) def getfloat(self, section, option): return self.config.getfloat(section, option) @synchronized(_lock) def getboolean(self, section, option): return self.config.getboolean(section, option) @synchronized(_lock) def items(self, section): return self.config.items(section) @synchronized(_lock) def set(self, section, option, value): self.config.set(section, option, value) self._save() @synchronized(_lock) def remove_option(self, section, option): self.config.remove_option(section, option) self._save() @synchronized(_lock) def remove_section(self, section): self.config.remove_section(section) self._save()
class Config(object): """A wrapper around RawConfigParser""" def __init__(self, version=None): """Use read() to read in an existing config file. version should be an int starting with 0 that gets incremented if you want to register a new upgrade function. If None, upgrade is disabled. """ self._config = ConfigParser(dict_type=_sorted_dict) self._version = version self._loaded_version = None self._upgrade_funcs = [] self._initial = {} def _do_upgrade(self, func): assert self._loaded_version is not None assert self._version is not None old_version = self._loaded_version new_version = self._version if old_version != new_version: print_d("Config upgrade: %d->%d (%r)" % (old_version, new_version, func)) func(self, old_version, new_version) def get_version(self): """Get the version of the loaded config file (for testing only) Raises Error if no file was loaded or versioning is disabled. """ if self._version is None: raise Error("Versioning disabled") if self._loaded_version is None: raise Error("No file loaded") return self._loaded_version def register_upgrade_function(self, function): """Register an upgrade function that gets called at each read() if the current config version and the loaded version don't match. Can also be registered after read was called. function(config, old_version: int, new_version: int) -> None """ if self._version is None: raise Error("Versioning disabled") self._upgrade_funcs.append(function) # after read(), so upgrade now if self._loaded_version is not None: self._do_upgrade(function) return function def set_inital(self, section, option, value): """Set an initial value for an option. The section must be added with add_section() first. Adds the value to the config and calling reset() will reset the value to it. """ self.set(section, option, value) self._initial.setdefault(section, {}) self._initial[section].setdefault(option, {}) self._initial[section][option] = value def reset(self, section, option): """Reset the value to the initial state""" value = self._initial[section][option] self.set(section, option, value) def options(self, section): """Returns a list of options available in the specified section.""" return self._config.options(section) def get(self, *args): """get(section, option[, default]) -> str If default is not given, raises Error in case of an error """ if len(args) == 3: try: return self._config.get(*args[:2]) except Error: return args[-1] return self._config.get(*args) def getboolean(self, *args): """getboolean(section, option[, default]) -> bool If default is not given, raises Error in case of an error """ if len(args) == 3: if not isinstance(args[-1], bool): raise ValueError try: return self._config.getboolean(*args[:2]) # ValueError if the value found in the config file # does not match any string representation -> so catch it too except (ValueError, Error): return args[-1] return self._config.getboolean(*args) def getint(self, *args): """getint(section, option[, default]) -> int If default is not give, raises Error in case of an error """ if len(args) == 3: if not isinstance(args[-1], int): raise ValueError try: return self._config.getint(*args[:2]) except Error: return args[-1] return self._config.getint(*args) def getfloat(self, *args): """getfloat(section, option[, default]) -> float If default is not give, raises Error in case of an error """ if len(args) == 3: if not isinstance(args[-1], float): raise ValueError try: return self._config.getfloat(*args[:2]) except Error: return args[-1] return self._config.getfloat(*args) def getstringlist(self, *args): """getstringlist(section, option[, default]) -> list If default is not given, raises Error in case of an error. Gets a list of strings, using CSV to parse and delimit. """ if len(args) == 3: if not isinstance(args[-1], list): raise ValueError try: value = self._config.get(*args[:2]) except Error: return args[-1] else: value = self._config.get(*args) parser = csv.reader([value], lineterminator='\n', quoting=csv.QUOTE_MINIMAL) try: vals = [v.decode('utf-8') for v in parser.next()] except (csv.Error, ValueError) as e: raise Error(e) return vals def setstringlist(self, section, option, values): """Saves a list of unicode strings using the csv module""" sw = StringIO() values = [unicode(v).encode('utf-8') for v in values] writer = csv.writer(sw, lineterminator='\n', quoting=csv.QUOTE_MINIMAL) writer.writerow(values) self._config.set(section, option, sw.getvalue()) def set(self, section, option, value): """Saves the string representation for the passed value Don't pass unicode, encode first. """ # RawConfigParser only allows string values but doesn't # scream if they are not (and it only fails before the # first config save..) if not isinstance(value, str): value = str(value) self._config.set(section, option, value) def setdefault(self, section, option, default): """Like set but only sets the new value if the option isn't set before. """ if not self._config.has_option(section, option): self._config.set(section, option, default) def write(self, filename): """Write config to filename. Can raise EnvironmentError """ assert is_fsnative(filename) mkdir(os.path.dirname(filename)) # temporary set the new version for saving if self._version is not None: self.add_section("__config__") self.set("__config__", "version", self._version) try: with atomic_save(filename, ".tmp", "wb") as fileobj: self._config.write(fileobj) finally: if self._loaded_version is not None: self.set("__config__", "version", self._loaded_version) def clear(self): """Remove all sections and initial values""" for section in self._config.sections(): self._config.remove_section(section) self._initial.clear() def is_empty(self): """Whether the config has any sections""" return not self._config.sections() def read(self, filename): """Reads the config from `filename` if the file exists, otherwise does nothing Can raise EnvironmentError, Error. """ parsed_filenames = self._config.read(filename) # don't upgrade if we just created a new config if parsed_filenames and self._version is not None: self._loaded_version = self.getint("__config__", "version", -1) for func in self._upgrade_funcs: self._do_upgrade(func) def sections(self): """Return a list of the sections available""" return self._config.sections() def has_option(self, section, option): """If the given section exists, and contains the given option""" return self._config.has_option(section, option) def remove_option(self, section, option): """Remove the specified option from the specified section Can raise Error. """ return self._config.remove_option(section, option) def add_section(self, section): """Add a section named section to the instance if it not already exists.""" if not self._config.has_section(section): self._config.add_section(section)
def getFloat(self, section, option, default=0.0): if self.has_option(section, option): return RawConfigParser.getfloat(self, section, option) else: return default
class MyConfig(MyCommon): #---------------------MyConfig::__init__------------------------------------ def __init__(self, filename=None, section=None, simulation=False, log=None): super(MyConfig, self).__init__() self.log = log self.FileName = filename self.Section = section self.Simulation = simulation self.CriticalLock = threading.Lock( ) # Critical Lock (writing conf file) self.InitComplete = False try: self.config = RawConfigParser() self.config.read(self.FileName) if self.Section == None: SectionList = self.GetSections() if len(SectionList): self.Section = SectionList[0] except Exception as e1: self.LogErrorLine("Error in MyConfig:init: " + str(e1)) return self.InitComplete = True #---------------------MyConfig::HasOption----------------------------------- def HasOption(self, Entry): return self.config.has_option(self.Section, Entry) #---------------------MyConfig::GetList------------------------------------- def GetList(self): return self.config.items(self.Section) #---------------------MyConfig::GetSections--------------------------------- def GetSections(self): return self.config.sections() #---------------------MyConfig::SetSection---------------------------------- def SetSection(self, section): if self.Simulation: return True if not (isinstance(section, str) or isinstance(section, unicode)) or not len(section): self.LogError("Error in MyConfig:ReadValue: invalid section: " + str(section)) return False self.Section = section return True #---------------------MyConfig::ReadValue----------------------------------- def ReadValue(self, Entry, return_type=str, default=None, section=None, NoLog=False): try: if section != None: self.SetSection(section) if self.config.has_option(self.Section, Entry): if return_type == str: return self.config.get(self.Section, Entry) elif return_type == bool: return self.config.getboolean(self.Section, Entry) elif return_type == float: return self.config.getfloat(self.Section, Entry) elif return_type == int: return self.config.getint(self.Section, Entry) else: self.LogErrorLine( "Error in MyConfig:ReadValue: invalid type:" + str(return_type)) return default else: return default except Exception as e1: if not NoLog: self.LogErrorLine("Error in MyConfig:ReadValue: " + Entry + ": " + str(e1)) return default #---------------------MyConfig::WriteSection-------------------------------- def WriteSection(self, SectionName): if self.Simulation: return True SectionList = self.GetSections() if SectionName in SectionList: self.LogError("Error in WriteSection: Section already exist.") return True try: with self.CriticalLock: with open(self.FileName, "a") as ConfigFile: ConfigFile.write("[" + SectionName + "]") ConfigFile.flush() ConfigFile.close() # update the read data that is cached self.config.read(self.FileName) return True except Exception as e1: self.LogErrorLine("Error in WriteSection: " + str(e1)) return False #---------------------MyConfig::WriteValue---------------------------------- def WriteValue(self, Entry, Value, remove=False, section=None): if self.Simulation: return if section != None: self.SetSection(section) SectionFound = False try: with self.CriticalLock: Found = False ConfigFile = open(self.FileName, 'r') FileString = ConfigFile.read() ConfigFile.close() ConfigFile = open(self.FileName, 'w') for line in FileString.splitlines(): if not line.isspace(): # blank lines newLine = line.strip() # strip leading spaces if len(newLine): if not newLine[0] == "#": # not a comment if not SectionFound and not self.LineIsSection( newLine): ConfigFile.write(line + "\n") continue if self.LineIsSection( newLine ) and self.Section.lower( ) != self.GetSectionName(newLine).lower(): if SectionFound and not Found and not remove: # we reached the end of the section ConfigFile.write(Entry + " = " + Value + "\n") Found = True SectionFound = False ConfigFile.write(line + "\n") continue if self.LineIsSection( newLine) and self.Section.lower( ) == self.GetSectionName( newLine).lower(): SectionFound = True ConfigFile.write(line + "\n") continue if not SectionFound: ConfigFile.write(line + "\n") continue items = newLine.split( '=') # split items in line by spaces if len(items) >= 2: items[0] = items[0].strip() if items[0] == Entry: if not remove: ConfigFile.write(Entry + " = " + Value + "\n") Found = True continue ConfigFile.write(line + "\n") # if this is a new entry, then write it to the file, unless we are removing it # this check is if there is not section below the one we are working in, # it will be added to the end of the file if not Found and not remove: ConfigFile.write(Entry + " = " + Value + "\n") ConfigFile.flush() ConfigFile.close() # update the read data that is cached self.config.read(self.FileName) return True except Exception as e1: self.LogError("Error in WriteValue: " + str(e1)) return False #---------------------MyConfig::GetSectionName------------------------------ def GetSectionName(self, Line): if self.Simulation: return "" Line = Line.strip() if Line.startswith("[") and Line.endswith("]") and len(Line) >= 3: Line = Line.replace("[", "") Line = Line.replace("]", "") return Line return "" #---------------------MyConfig::LineIsSection------------------------------- def LineIsSection(self, Line): if self.Simulation: return False Line = Line.strip() if Line.startswith("[") and Line.endswith("]") and len(Line) >= 3: return True return False
ini.read(iniFileName) iniPotenThinDisk = dict(ini.items("Potential thin disk")) iniPotenThickDisk= dict(ini.items("Potential thick disk")) iniPotenGasDisk = dict(ini.items("Potential gas disk")) iniPotenBulge = dict(ini.items("Potential bulge")) iniPotenDarkHalo = dict(ini.items("Potential dark halo")) iniDFThinDisk = dict(ini.items("DF thin disk")) iniDFThickDisk = dict(ini.items("DF thick disk")) iniDFStellarHalo = dict(ini.items("DF stellar halo")) iniDFDarkHalo = dict(ini.items("DF dark halo")) iniDFBulge = dict(ini.items("DF bulge")) iniSCMHalo = dict(ini.items("SelfConsistentModel halo")) iniSCMBulge = dict(ini.items("SelfConsistentModel bulge")) iniSCMDisk = dict(ini.items("SelfConsistentModel disk")) iniSCM = dict(ini.items("SelfConsistentModel")) solarRadius = ini.getfloat("Data", "SolarRadius") # define external unit system describing the data (including the parameters in INI file) agama.setUnits(length=1, velocity=1, mass=1) # in Kpc, km/s, Msun # initialize the SelfConsistentModel object (only the potential expansion parameters) model = agama.SelfConsistentModel(**iniSCM) # create initial ('guessed') density profiles of all components densityBulge = agama.Density(**iniPotenBulge) densityDarkHalo = agama.Density(**iniPotenDarkHalo) densityThinDisk = agama.Density(**iniPotenThinDisk) densityThickDisk = agama.Density(**iniPotenThickDisk) densityGasDisk = agama.Density(**iniPotenGasDisk) densityStellarDisk = agama.Density(densityThinDisk, densityThickDisk) # composite
class Config(object): """A wrapper around RawConfigParser""" def __init__(self): """Use read() to read in an existing config file""" self._config = ConfigParser(dict_type=_sorted_dict) self._initial = {} def set_inital(self, section, option, value): """Set an initial value for an option. The section must be added with add_section() first. Adds the value to the config and calling reset() will reset the value to it. """ self.set(section, option, value) self._initial.setdefault(section, {}) self._initial[section].setdefault(option, {}) self._initial[section][option] = value def reset(self, section, option): """Reset the value to the initial state""" value = self._initial[section][option] self.set(section, option, value) def options(self, section): """Returns a list of options available in the specified section.""" return self._config.options(section) def get(self, *args): """get(section, option[, default]) -> str If default is not given, raises Error in case of an error """ if len(args) == 3: try: return self._config.get(*args[:2]) except Error: return args[-1] return self._config.get(*args) def getboolean(self, *args): """getboolean(section, option[, default]) -> bool If default is not given, raises Error in case of an error """ if len(args) == 3: if not isinstance(args[-1], bool): raise ValueError try: return self._config.getboolean(*args[:2]) # ValueError if the value found in the config file # does not match any string representation -> so catch it too except (ValueError, Error): return args[-1] return self._config.getboolean(*args) def getint(self, *args): """getint(section, option[, default]) -> int If default is not give, raises Error in case of an error """ if len(args) == 3: if not isinstance(args[-1], int): raise ValueError try: return self._config.getint(*args[:2]) except Error: return args[-1] return self._config.getint(*args) def getfloat(self, *args): """getfloat(section, option[, default]) -> float If default is not give, raises Error in case of an error """ if len(args) == 3: if not isinstance(args[-1], float): raise ValueError try: return self._config.getfloat(*args[:2]) except Error: return args[-1] return self._config.getfloat(*args) def getstringlist(self, *args): """getstringlist(section, option[, default]) -> list If default is not given, raises Error in case of an error. Gets a list of strings, using CSV to parse and delimit. """ if len(args) == 3: if not isinstance(args[-1], list): raise ValueError try: value = self._config.get(*args[:2]) except Error: return args[-1] else: value = self._config.get(*args) parser = csv.reader([value], lineterminator='\n', quoting=csv.QUOTE_MINIMAL) try: vals = [v.decode('utf-8') for v in parser.next()] except (csv.Error, ValueError) as e: raise Error(e) return vals def setstringlist(self, section, option, values): """Saves a list of unicode strings using the csv module""" sw = StringIO() values = [unicode(v).encode('utf-8') for v in values] writer = csv.writer(sw, lineterminator='\n', quoting=csv.QUOTE_MINIMAL) writer.writerow(values) self._config.set(section, option, sw.getvalue()) def set(self, section, option, value): """Saves the string representation for the passed value Don't pass unicode, encode first. """ # RawConfigParser only allows string values but doesn't # scream if they are not (and it only fails before the # first config save..) if not isinstance(value, str): value = str(value) self._config.set(section, option, value) def setdefault(self, section, option, default): """Like set but only sets the new value if the option isn't set before. """ if not self._config.has_option(section, option): self._config.set(section, option, default) def write(self, filename): """Write config to filename. Can raise EnvironmentError """ assert is_fsnative(filename) mkdir(os.path.dirname(filename)) with atomic_save(filename, ".tmp", "wb") as fileobj: self._config.write(fileobj) def clear(self): """Remove all sections and initial values""" for section in self._config.sections(): self._config.remove_section(section) self._initial.clear() def is_empty(self): """Whether the config has any sections""" return not self._config.sections() def read(self, filename): """Reads the config from `filename` if the file exists, otherwise does nothing Can raise EnvironmentError, Error. """ self._config.read(filename) def sections(self): """Return a list of the sections available""" return self._config.sections() def has_option(self, section, option): """If the given section exists, and contains the given option""" return self._config.has_option(section, option) def remove_option(self, section, option): """Remove the specified option from the specified section Can raise Error. """ return self._config.remove_option(section, option) def add_section(self, section): """Add a section named section to the instance if it not already exists.""" if not self._config.has_section(section): self._config.add_section(section)
def getfloat(self, section, option): if not self.has_option(section, option): return self.DEF_FLOAT return RawConfigParser.getfloat(self, section, option)
DisSpan=cf.get('auto_pick','DisSpan').split(',') Excepts=cf.get('auto_pick','Excepts').split(',') Keywords=cf.get('auto_pick','Keywords').split(',') #config Debug=cf.getboolean('config','Debug') TestMode=cf.getboolean('config','TestMode') for acc in cf.get('config','Account').split('\n'): Account.append((acc.split(':')[0],acc.split(':')[1])) for key in cf.get('config','Keys').split(' '): Keys.append(unicode(key)) for brand in cf.get('config','Brands').split(' '): Brands.append(unicode(brand)) #timeout cf1=RawConfigParser() cf1.read(W_CONFIG_FILE) BuyTimeout=cf1.getfloat('timeout','buytimeout') print '原BuyTimeout为:',BuyTimeout if Debug: print LoginUrl pOpt() def set_timeout(): '修改配置文件的超时时间BuyTimeout' global cf1 cf1.set('timeout','buytimeout',str(BuyTimeout)) with open(W_CONFIG_FILE,'wb') as configFile: cf1.write(configFile) print '新BuyTimeout已设置为:'+str(BuyTimeout)
def main(): print "bioImaging loaded" configFileName = "settings.txt" config = RawConfigParser() config.read(configFileName) logLevel = config.get("General", "logLevel") if logLevel=="INFO": level=logging.INFO elif logLevel=="WARNING": level=logging.WARNING elif logLevel=="DEBUG": level=logging.DEBUG elif logLevel=="ERROR": level=logging.ERROR else: level=logging.INFO logging.basicConfig(level=level, filename="log.txt", filemode="w") logger = logging.getLogger("main()") logger.info("bioImaging program started.") inFiles = config.get("Input", "inputFilePattern") maxFiles = config.getint("Input", "maximumFiles") excelOut = True#config.getboolean("Output", "writeExcelFile") excelOutFileName = config.get("Output", "excelFileName") tempWorkDir = "temporary" logger.info("Removing temporary dir {0}".format(tempWorkDir)) shutil.rmtree(tempWorkDir, True) os.mkdir(tempWorkDir) files = glob.glob(inFiles) files = map(convPath, files) maxROIs = config.getint("Parameters", "maxROIs") subtractBias = config.getboolean("Parameters", "subtractDarkChargeBias") nSigma = config.getfloat("Parameters", "NumberOfSigmaAboveMean") nDilations = config.getint("Parameters", "NumberOfDilations") if excelOut: wb = xlwt.Workbook() sheet = wb.add_sheet("data") dataBeginCol = 4 i = 0 j = 0 for nRoi in range(maxROIs): for m in Measurement.outFormat: sheet.write(0, dataBeginCol+j, m) j += 1 for fn in files[0:maxFiles]: sys.stdout.write(".") sys.stdout.flush() d = fn[:fn.rindex("/")] i += 1 logger.info("Processing directory: {0}".format(d)) ofdir = tempWorkDir + "/" + str(i) if excelOut: wb.get_sheet(0).write(i, 0, fn) imageName = "Image%d" % i try: if not ofdir is None: os.mkdir(ofdir) p = processLuminescent(d, ofdir, subtractBias, nSigma, nDilations, maxROIs) if excelOut: wb.get_sheet(0).write(i, 1, imageName) except IOError as e: logger.error("Could not process {0}: {1}".format(d, str(e))) #os.rmdir(ofdir) if excelOut: wb.get_sheet(0).write(i, 1, "FAILED: {0}".format(str(e))) continue j = dataBeginCol if excelOut: wb.get_sheet(0).write(i, 2, p.stats.mean) wb.get_sheet(0).write(i, 3, p.stats.total) for m in p.measurements[0:maxROIs]: if excelOut: l = m.writeToSheet(wb.get_sheet(0), i, j) j += l if excelOut and (not ofdir is None): sheet = wb.add_sheet(imageName) tempImage = Image.open(ofdir + "/out.png").convert("RGB") tempImage.save("temp.bmp") sheet.insert_bitmap("temp.bmp", 5, 1) os.remove("temp.bmp") if excelOut: wb.save(excelOutFileName) print "Done analyzing %d files" % i
def parse_config_file(filename): """ Parse a Configuration File """ chase_config = default_config.copy() config = RawConfigParser() config.read(filename) # Map Defaults chase_config["flask_host"] = config.get("map", "flask_host") chase_config["flask_port"] = config.getint("map", "flask_port") chase_config["default_lat"] = config.getfloat("map", "default_lat") chase_config["default_lon"] = config.getfloat("map", "default_lon") chase_config["payload_max_age"] = config.getint("map", "payload_max_age") chase_config["thunderforest_api_key"] = config.get( "map", "thunderforest_api_key") # GPSD Settings chase_config["car_gpsd_host"] = config.get("gpsd", "gpsd_host") chase_config["car_gpsd_port"] = config.getint("gpsd", "gpsd_port") # Serial GPS Settings chase_config["car_serial_port"] = config.get("gps_serial", "gps_port") chase_config["car_serial_baud"] = config.getint("gps_serial", "gps_baud") # Habitat Settings chase_config["habitat_upload_enabled"] = config.getboolean( "habitat", "habitat_upload_enabled") chase_config["habitat_call"] = config.get("habitat", "habitat_call") chase_config["habitat_update_rate"] = config.getint( "habitat", "habitat_update_rate") # Predictor chase_config["pred_enabled"] = config.getboolean("predictor", "predictor_enabled") chase_config["offline_predictions"] = config.getboolean( "predictor", "offline_predictions") chase_config["pred_burst"] = config.getfloat("predictor", "default_burst") chase_config["pred_desc_rate"] = config.getfloat("predictor", "default_descent_rate") chase_config["pred_binary"] = config.get("predictor", "pred_binary") chase_config["pred_gfs_directory"] = config.get("predictor", "gfs_directory") chase_config["pred_model_download"] = config.get("predictor", "model_download") # Range Ring Settings chase_config["range_rings_enabled"] = config.getboolean( "range_rings", "range_rings_enabled") chase_config["range_ring_quantity"] = config.getint( "range_rings", "range_ring_quantity") chase_config["range_ring_spacing"] = config.getint("range_rings", "range_ring_spacing") chase_config["range_ring_weight"] = config.getfloat( "range_rings", "range_ring_weight") chase_config["range_ring_color"] = config.get("range_rings", "range_ring_color") chase_config["range_ring_custom_color"] = config.get( "range_rings", "range_ring_custom_color") # Bearing Processing chase_config["max_bearings"] = config.getint("bearings", "max_bearings") chase_config["max_bearing_age"] = ( config.getint("bearings", "max_bearing_age") * 60 ) # Convert to seconds if chase_config["max_bearing_age"] < 60: chase_config[ "max_bearing_age"] = 60 # Make sure this number is something sane, otherwise things will break chase_config["car_speed_gate"] = ( config.getfloat("bearings", "car_speed_gate") / 3.6) # Convert to m/s chase_config["bearing_length"] = config.getfloat("bearings", "bearing_length") chase_config["bearing_weight"] = config.getfloat("bearings", "bearing_weight") chase_config["bearing_color"] = config.get("bearings", "bearing_color") chase_config["bearing_custom_color"] = config.get("bearings", "bearing_custom_color") # Offline Map Settings chase_config["tile_server_enabled"] = config.getboolean( "offline_maps", "tile_server_enabled") chase_config["tile_server_path"] = config.get("offline_maps", "tile_server_path") # Determine valid offline map layers. chase_config["offline_tile_layers"] = [] if chase_config["tile_server_enabled"]: for _dir in os.listdir(chase_config["tile_server_path"]): if os.path.isdir( os.path.join(chase_config["tile_server_path"], _dir)): chase_config["offline_tile_layers"].append(_dir) logging.info("Found Map Layers: %s" % str(chase_config["offline_tile_layers"])) try: chase_config["chase_car_speed"] = config.getboolean( "speedo", "chase_car_speed") except: logging.info( "Missing Chase Car Speedo Setting, using default (disabled)") chase_config["chase_car_speed"] = False try: chase_config["default_alt"] = config.getfloat("map", "default_alt") except: logging.info("Missing default_alt setting, using default (0m)") chase_config["default_alt"] = 0 try: chase_config["stadia_api_key"] = config.get("map", "stadia_api_key") except: logging.info("Missing Stadia API Key setting, using default (none)") chase_config["stadia_api_key"] = "none" try: chase_config["turn_rate_threshold"] = config.getfloat( "bearings", "turn_rate_threshold") except: logging.info("Missing turn rate gate setting, using default (4m/s)") chase_config["turn_rate_threshold"] = 4.0 # Telemetry Source Profiles _profile_count = config.getint("profile_selection", "profile_count") _default_profile = config.getint("profile_selection", "default_profile") chase_config["selected_profile"] = "" chase_config["profiles"] = {} # Unit Selection chase_config["unitselection"] = config.get("units", "unitselection", fallback="metric") if chase_config["unitselection"] != "imperial": chase_config[ "unitselection"] = "metric" # unless imperial is explicitly requested do metric chase_config["switch_miles_feet"] = config.get("units", "switch_miles_feet", fallback="400") for i in range(1, _profile_count + 1): _profile_section = "profile_%d" % i try: _profile_name = config.get(_profile_section, "profile_name") _profile_telem_source_type = config.get(_profile_section, "telemetry_source_type") _profile_telem_source_port = config.getint( _profile_section, "telemetry_source_port") _profile_car_source_type = config.get(_profile_section, "car_source_type") _profile_car_source_port = config.getint(_profile_section, "car_source_port") _profile_online_tracker = config.get(_profile_section, "online_tracker") chase_config["profiles"][_profile_name] = { "name": _profile_name, "telemetry_source_type": _profile_telem_source_type, "telemetry_source_port": _profile_telem_source_port, "car_source_type": _profile_car_source_type, "car_source_port": _profile_car_source_port, "online_tracker": _profile_online_tracker, } if _default_profile == i: chase_config["selected_profile"] = _profile_name except Exception as e: logging.error("Error reading profile section %d - %s" % (i, str(e))) if len(chase_config["profiles"].keys()) == 0: logging.critical("Could not read any profile data!") return None if chase_config["selected_profile"] not in chase_config["profiles"]: logging.critical("Default profile selection does not exist.") return None # History chase_config["reload_last_position"] = config.getboolean( "history", "reload_last_position", fallback=False) return chase_config
class Config(object): """A wrapper around RawConfigParser. Provides a ``defaults`` attribute of the same type which can be used to set default values. """ def __init__(self, version=None, _defaults=True): """Use read() to read in an existing config file. version should be an int starting with 0 that gets incremented if you want to register a new upgrade function. If None, upgrade is disabled. """ self._config = ConfigParser(dict_type=_sorted_dict) self.defaults = None if _defaults: self.defaults = Config(_defaults=False) self._version = version self._loaded_version = None self._upgrade_funcs = [] def _do_upgrade(self, func): assert self._loaded_version is not None assert self._version is not None old_version = self._loaded_version new_version = self._version if old_version != new_version: print_d("Config upgrade: %d->%d (%r)" % ( old_version, new_version, func)) func(self, old_version, new_version) def get_version(self): """Get the version of the loaded config file (for testing only) Raises Error if no file was loaded or versioning is disabled. """ if self._version is None: raise Error("Versioning disabled") if self._loaded_version is None: raise Error("No file loaded") return self._loaded_version def register_upgrade_function(self, function): """Register an upgrade function that gets called at each read() if the current config version and the loaded version don't match. Can also be registered after read was called. function(config, old_version: int, new_version: int) -> None """ if self._version is None: raise Error("Versioning disabled") self._upgrade_funcs.append(function) # after read(), so upgrade now if self._loaded_version is not None: self._do_upgrade(function) return function def reset(self, section, option): """Reset the value to the default state""" assert self.defaults is not None self.set(section, option, self.defaults.get(section, option)) def options(self, section): """Returns a list of options available in the specified section.""" try: options = self._config.options(section) except NoSectionError: if self.defaults: return self.defaults.options(section) raise else: if self.defaults: try: options.extend(self.defaults.options(section)) options = list_unique(options) except NoSectionError: pass return options def get(self, section, option, default=_DEFAULT): """get(section, option[, default]) -> str If default is not given or set, raises Error in case of an error """ try: return self._config.get(section, option) except Error: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.get(section, option) except Error: pass raise return default def gettext(self, *args, **kwargs): value = self.get(*args, **kwargs) if PY2: value = value.decode("utf-8") else: # make sure there are no surrogates value.encode("utf-8") return value def getbytes(self, section, option, default=_DEFAULT): try: value = self._config.get(section, option) if PY3: value = value.encode("utf-8", "surrogateescape") return value except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getbytes(section, option) except Error: pass raise Error(e) return default def getboolean(self, section, option, default=_DEFAULT): """getboolean(section, option[, default]) -> bool If default is not given or set, raises Error in case of an error """ try: return self._config.getboolean(section, option) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getboolean(section, option) except Error: pass raise Error(e) return default def getint(self, section, option, default=_DEFAULT): """getint(section, option[, default]) -> int If default is not give or set, raises Error in case of an error """ try: return int(self._config.getfloat(section, option)) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getint(section, option) except Error: pass raise Error(e) return default def getfloat(self, section, option, default=_DEFAULT): """getfloat(section, option[, default]) -> float If default is not give or set, raises Error in case of an error """ try: return self._config.getfloat(section, option) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getfloat(section, option) except Error: pass raise Error(e) return default def getstringlist(self, section, option, default=_DEFAULT): """getstringlist(section, option[, default]) -> list If default is not given or set, raises Error in case of an error. Gets a list of strings, using CSV to parse and delimit. """ try: value = self._config.get(section, option) parser = csv.reader( [value], lineterminator='\n', quoting=csv.QUOTE_MINIMAL) try: if PY2: vals = [v.decode('utf-8') for v in next(parser)] else: vals = next(parser) except (csv.Error, ValueError) as e: raise Error(e) return vals except Error as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getstringlist(section, option) except Error: pass raise Error(e) return default def setstringlist(self, section, option, values): """Saves a list of unicode strings using the csv module""" if PY2: sw = cBytesIO() values = [text_type(v).encode('utf-8') for v in values] else: sw = StringIO() values = [text_type(v) for v in values] writer = csv.writer(sw, lineterminator='\n', quoting=csv.QUOTE_MINIMAL) writer.writerow(values) self.set(section, option, sw.getvalue()) def setlist(self, section, option, values, sep=","): """Saves a list of str using ',' as a separator and \\ for escaping""" values = [str(v) for v in values] joined = join_escape(values, sep) self.set(section, option, joined) def getlist(self, section, option, default=_DEFAULT, sep=","): """Returns a str list saved with setlist()""" try: value = self._config.get(section, option) return split_escape(value, sep) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getlist(section, option, sep=sep) except Error: pass raise Error(e) return default def set(self, section, option, value): """Saves the string representation for the passed value Don't pass unicode, encode first. """ if PY3 and isinstance(value, bytes): raise TypeError("use setbytes") # RawConfigParser only allows string values but doesn't # scream if they are not (and it only fails before the # first config save..) if not isinstance(value, str): value = str(value) try: self._config.set(section, option, value) except NoSectionError: if self.defaults and self.defaults.has_section(section): self._config.add_section(section) self._config.set(section, option, value) else: raise def settext(self, section, option, value): value = text_type(value) if PY2: value = value.encode("utf-8") else: # make sure there are no surrogates value.encode("utf-8") self.set(section, option, value) def setbytes(self, section, option, value): assert isinstance(value, bytes) if PY3: value = value.decode("utf-8", "surrogateescape") self.set(section, option, value) def write(self, filename): """Write config to filename. Can raise EnvironmentError """ assert isinstance(filename, fsnative) mkdir(os.path.dirname(filename)) # temporary set the new version for saving if self._version is not None: self.add_section("__config__") self.set("__config__", "version", self._version) try: with atomic_save(filename, "wb") as fileobj: if PY2: self._config.write(fileobj) else: temp = StringIO() self._config.write(temp) data = temp.getvalue().encode("utf-8", "surrogateescape") fileobj.write(data) finally: if self._loaded_version is not None: self.set("__config__", "version", self._loaded_version) def clear(self): """Remove all sections.""" for section in self._config.sections(): self._config.remove_section(section) def is_empty(self): """Whether the config has any sections""" return not self._config.sections() def read(self, filename): """Reads the config from `filename` if the file exists, otherwise does nothing Can raise EnvironmentError, Error. """ try: with open(filename, "rb") as fileobj: if PY3: fileobj = StringIO( fileobj.read().decode("utf-8", "surrogateescape")) self._config.readfp(fileobj, filename) except (IOError, OSError): return # don't upgrade if we just created a new config if self._version is not None: self._loaded_version = self.getint("__config__", "version", -1) for func in self._upgrade_funcs: self._do_upgrade(func) def has_option(self, section, option): """If the given section exists, and contains the given option""" return self._config.has_option(section, option) or ( self.defaults and self.defaults.has_option(section, option)) def has_section(self, section): """If the given section exists""" return self._config.has_section(section) or ( self.defaults and self.defaults.has_section(section)) def remove_option(self, section, option): """Remove the specified option from the specified section Can raise Error. """ return self._config.remove_option(section, option) def add_section(self, section): """Add a section named section to the instance if it not already exists.""" if not self._config.has_section(section): self._config.add_section(section)
from ConfigParser import RawConfigParser from basepaths import * import os HOME = os.path.expanduser("~") config_path = config_folder + config_filename config = RawConfigParser() configfile = open(config_path) config.readfp(configfile) configfile.close #[misc] level_on_startup = config.get('misc', 'level_on_startup') max_level_on_startup = config.get('misc', 'max_level_on_startup') gmax = config.getfloat('misc', 'gmax') tone_defeat_on_startup = config.getboolean('misc', 'tone_defeat_on_startup') command_delay = config.getfloat('misc', 'command_delay') beep_options = config.get('misc', 'beep_options') dummy_ports = config.get('misc', 'dummy_ports') pause_players = config.getboolean('misc', 'pause_players') resume_players = config.getboolean('misc', 'resume_players') jack_internal_monitors = config.get('misc', 'jack_internal_monitors') jack_external_monitors = config.get('misc', 'jack_external_monitors') #[presets] default_preset = config.get('presets', 'default_preset')
def read_config(self): '''Return (properties, variables_list)''' filename = self._get_filename('config') parser = RawConfigParser() parser.read(filename) properties = {} if parser.has_option('properties', 'hittypeid'): properties['hittypeid'] = parser.get('properties', 'hittypeid') else: # Create a new HIT Type ID if not present for key in ('title', 'description', 'keywords'): properties[key] = parser.get('properties', key) properties['reward'] = Price(parser.getfloat('properties', 'reward')) for key in ('assignmentduration', 'autoapprovaldelay'): properties[key] = self._parse_time(parser.get('timing', key)) # Qualifications requirements = [] if parser.has_option('qualifications', 'locale'): requirements.append(LocaleRequirement( 'EqualTo', parser.get('qualifications', 'locale'), True)) for key in RecordWrapper.QUALIFICATIONS: if parser.has_option('qualifications', key): value = parser.get('qualifications', key) comparator = ''.join(x for x in value if not x.isdigit()) value = int(value[len(comparator):]) requirements.append(RecordWrapper.QUALIFICATIONS[key]( RecordWrapper.COMPARATORS[comparator], value, True)) properties['qualifications'] = Qualifications(requirements) # Other properties properties['annotation'] = parser.get('properties', 'annotation') properties['assignments'] = parser.get('properties', 'assignments') try: properties['assignments'] = int(properties['assignments']) except ValueError: properties['assignments'] = self.read_assignment_amounts(properties['assignments']) properties['hitlifetime'] = self._parse_time(parser.get('timing', 'hitlifetime')) # Question properties['url'] = parser.get('question', 'url') properties['frameheight'] = parser.get('question', 'frameheight') # Input n = parser.getint('input', 'numhits') if isinstance(properties['assignments'], list): assert len(properties['assignments']) == n, (len(properties['assignments']), n) variables_list = [dict() for i in xrange(n)] for key in parser.options('input'): if key != 'numhits': value = parser.get('input', key) if value[0] == '[': value = json.loads(value) assert len(value) == n for i in xrange(n): variables_list[i][key] = value[i] elif '-' in value: start, end = [int(x) for x in value.split('-')] assert end - start + 1 == n for i in xrange(n): variables_list[i][key] = start + i else: for i in xrange(n): variables_list[i][key] = value return properties, variables_list
from ConfigParser import RawConfigParser from basepaths import * status_path = config_folder + status_filename status = RawConfigParser() statusfile = open(status_path) status.readfp(statusfile) statusfile.close #[room EQ] system_eq = status.get('room EQ', 'system_eq') drc_eq = status.get('room EQ', 'drc_eq') peq = status.get('room EQ', 'peq') peqdefeat = status.get('room EQ', 'peqdefeat') #[recording EQ] treble = status.getfloat('recording EQ', 'treble') bass = status.getfloat('recording EQ', 'bass') loudness_ref = status.getfloat('recording EQ', 'loudness_ref') loudness_track = status.getboolean('recording EQ', 'loudness_track') #[level] replaygain_track = status.getboolean('level', 'replaygain_track') level = status.getfloat('level', 'level') headroom = status.getfloat('level', 'headroom') balance = status.getfloat('level', 'balance') #[general] fs = status.get('general', 'fs') filter_type = status.get('general', 'filter_type') preset = status.get('general', 'preset') muted = status.getboolean('general', 'muted') polarity = status.get('general', 'polarity') clock = status.get('general', 'clock')
class MyConfig (MyLog): #---------------------MyConfig::__init__------------------------------------ def __init__(self, filename = None, section = None, log = None): super(MyLog, self).__init__() self.log = log self.FileName = filename self.Section = section self.CriticalLock = threading.Lock() # Critical Lock (writing conf file) self.InitComplete = False self.LogLocation = "/var/log/" self.Latitude = 51.4769 self.Longitude = 0 self.SendRepeat = 1 self.UseHttps = False self.HTTPPort = 80 self.HTTPSPort = 443 self.RTS_Address = "0x279620" self.Shutters = {} self.ShuttersByName = {} self.Schedule = {} try: self.config = RawConfigParser() self.config.read(self.FileName) if self.Section == None: SectionList = self.GetSections() if len(SectionList): self.Section = SectionList[0] except Exception as e1: self.LogErrorLine("Error in MyConfig:init: " + str(e1)) return self.InitComplete = True # -------------------- MyConfig::LoadConfig----------------------------------- def LoadConfig(self): parameters = {'LogLocation': str, 'Latitude': float, 'Longitude': float, 'SendRepeat': int, 'UseHttps': bool, 'HTTPPort': int, 'HTTPSPort': int, 'TXGPIO': int, 'RTS_Address': str} self.SetSection("General"); for key, type in parameters.items(): try: if self.HasOption(key): setattr(self, key, self.ReadValue(key, return_type=type)) except Exception as e1: self.LogErrorLine("Missing config file or config file entries in Section General for key "+key+": " + str(e1)) return False self.SetSection("Shutters"); shutters = self.GetList(); for key, value in shutters: try: param1 = value.split(",") if param1[1].strip().lower() == 'true': param2 = int(self.ReadValue(key, section="ShutterRollingCodes", return_type=int)) self.Shutters[key] = {'name': param1[0], 'code': param2} self.ShuttersByName[param1[0]] = key except Exception as e1: self.LogErrorLine("Missing config file or config file entries in Section Shutters for key "+key+": " + str(e1)) return False self.SetSection("Scheduler") schedules = self.GetList() for key, value in schedules: try: param = value.split(",") if param[0].strip().lower() in ('active', 'paused'): self.Schedule[key] = {'active': param[0], 'repeatType': param[1], 'repeatValue': param[2], 'timeType': param[3], 'timeValue': param[4], 'shutterAction': param[5], 'shutterIds': param[6]} except Exception as e1: self.LogErrorLine("Missing config file or config file entries in Section Scheduler for key "+key+": " + str(e1)) return False return True #---------------------MyConfig::setLocation--------------------------------- def setLocation(self, lat, lng): self.WriteValue("Latitude", lat, section="General"); self.WriteValue("Longitude", lng, section="General"); self.Latitude = lat self.Longitude = lng #---------------------MyConfig::setCode--------------------------------- def setCode(self, shutterId, code): self.WriteValue(shutterId, str(code), section="ShutterRollingCodes"); self.Shutters[shutterId]['code'] = code #---------------------MyConfig::HasOption----------------------------------- def HasOption(self, Entry): return self.config.has_option(self.Section, Entry) #---------------------MyConfig::GetList------------------------------------- def GetList(self): return self.config.items(self.Section) #---------------------MyConfig::GetSections--------------------------------- def GetSections(self): return self.config.sections() #---------------------MyConfig::SetSection---------------------------------- def SetSection(self, section): # if not (isinstance(section, str) or isinstance(section, unicode)) or not len(section): if not len(section): self.LogError("Error in MyConfig:ReadValue: invalid section: " + str(section)) return False self.Section = section return True #---------------------MyConfig::ReadValue----------------------------------- def ReadValue(self, Entry, return_type = str, default = None, section = None, NoLog = False): try: if section != None: self.SetSection(section) if self.config.has_option(self.Section, Entry): if return_type == str: return self.config.get(self.Section, Entry) elif return_type == bool: return self.config.getboolean(self.Section, Entry) elif return_type == float: return self.config.getfloat(self.Section, Entry) elif return_type == int: return self.config.getint(self.Section, Entry) else: self.LogErrorLine("Error in MyConfig:ReadValue: invalid type:" + str(return_type)) return default else: return default except Exception as e1: if not NoLog: self.LogErrorLine("Error in MyConfig:ReadValue: " + Entry + ": " + str(e1)) return default #---------------------MyConfig::WriteSection-------------------------------- def WriteSection(self, SectionName): SectionList = self.GetSections() if SectionName in SectionList: self.LogError("Error in WriteSection: Section already exist.") return True try: with self.CriticalLock: with open(self.FileName, "a") as ConfigFile: ConfigFile.write("[" + SectionName + "]") ConfigFile.flush() ConfigFile.close() # update the read data that is cached self.config.read(self.FileName) return True except Exception as e1: self.LogErrorLine("Error in WriteSection: " + str(e1)) return False #---------------------MyConfig::WriteValue---------------------------------- def WriteValue(self, Entry, Value, remove = False, section = None): if section != None: self.SetSection(section) SectionFound = False try: with self.CriticalLock: Found = False ConfigFile = open(self.FileName,'r') FileList = ConfigFile.read().splitlines() ConfigFile.close() mySectionStart = -1; mySectionEnd = -1; myLine = -1; currentLastDataLine = -1; for i, line in enumerate(FileList): if self.LineIsSection(line) and self.Section.lower() == self.GetSectionName(line).lower(): mySectionStart = i elif mySectionStart >=0 and mySectionEnd == -1 and len(line.strip().split('=')) >= 2 and (line.strip().split('='))[0].strip() == Entry: myLine = i elif mySectionStart >=0 and mySectionEnd == -1 and self.LineIsSection(line): mySectionEnd = currentLastDataLine if not line.isspace() and not len(line.strip()) == 0 and not line.strip()[0] == "#": currentLastDataLine = i if mySectionStart >=0 and mySectionEnd == -1: mySectionEnd = currentLastDataLine self.LogDebug("CONFIG FILE WRITE ->> mySectionStart = "+str(mySectionStart)+", mySectionEnd = "+str(mySectionEnd)+", myLine = "+str(myLine)) if mySectionStart == -1: raise Exception("NOT ABLE TO FIND SECTION:"+self.Section) ConfigFile = open(self.FileName,'w') for i, line in enumerate(FileList): if myLine >= 0 and myLine == i and not remove: # I found my line, now write new value ConfigFile.write(Entry + " = " + Value + "\n") elif myLine == -1 and mySectionEnd == i: # Here we have to insert the new record... ConfigFile.write(line+"\n") ConfigFile.write(Entry + " = " + Value + "\n") else: # Nothing special, just copy the previous line.... ConfigFile.write(line+"\n") ConfigFile.flush() ConfigFile.close() # update the read data that is cached self.config.read(self.FileName) return True except Exception as e1: self.LogError("Error in WriteValue: " + str(e1)) return False #---------------------MyConfig::GetSectionName------------------------------ def GetSectionName(self, Line): Line = Line.strip() if Line.startswith("[") and Line.endswith("]") and len(Line) >=3 : Line = Line.replace("[", "") Line = Line.replace("]", "") return Line return "" #---------------------MyConfig::LineIsSection------------------------------- def LineIsSection(self, Line): Line = Line.strip() if Line.startswith("[") and Line.endswith("]") and len(Line) >=3 : return True return False