def _init_translation_config(): global _translation_config local_dir = sys.path[0] if local_dir == '': local_dir = '.' local_configs = [ local_dir + "/.vapp.conf" ] cwd = os.getcwd() if cwd != local_dir: local_configs.append(cwd + "/.vapp.conf") if sys.prefix == '/usr': sys_etc_dir = '' else: sys_etc_dir = sys.prefix _translation_config = ConfigParser() _translation_config.read([sys_etc_dir + "/etc/vapp.conf"] + local_configs) if not _translation_config.has_section('default_config'): _translation_config.add_section('default_config') if not _translation_config.has_option('default_config', 'text_domain'): _translation_config.set('default_config', 'text_domain', 'vapp') if not _translation_config.has_option('default_config', 'po_dir'): _translation_config.set('default_config', 'po_dir', sys.prefix + '/share/vapp/po') if not _translation_config.has_option('default_config', 'msg_catalog_dir'): _translation_config.set('default_config', 'msg_catalog_dir', sys.prefix + '/share/locale') if not _translation_config.has_option('default_config', 'prompt_catalog_dir'): _translation_config.set('default_config', 'prompt_catalog_dir', sys.prefix + '/share/vapp/prompts')
def _parse_info(self, info_file): cp = ConfigParser() cp.readfp(info_file) section = 'Activity' if cp.has_option(section, 'bundle_id'): self._bundle_id = cp.get(section, 'bundle_id') # FIXME deprecated elif cp.has_option(section, 'service_name'): warnings.warn('use bundle_id instead of service_name ' \ 'in your activity.info', DeprecationWarning) self._bundle_id = cp.get(section, 'service_name') else: raise MalformedBundleException( 'Activity bundle %s does not specify a bundle id' % self._path) if cp.has_option(section, 'name'): self._name = cp.get(section, 'name') else: raise MalformedBundleException( 'Activity bundle %s does not specify a name' % self._path) # FIXME class is deprecated if cp.has_option(section, 'class'): warnings.warn('use exec instead of class ' \ 'in your activity.info', DeprecationWarning) self.activity_class = cp.get(section, 'class') elif cp.has_option(section, 'exec'): self.bundle_exec = cp.get(section, 'exec') else: raise MalformedBundleException( 'Activity bundle %s must specify either class or exec' % self._path) if cp.has_option(section, 'mime_types'): mime_list = cp.get(section, 'mime_types').strip(';') self._mime_types = [mime.strip() for mime in mime_list.split(';')] if cp.has_option(section, 'show_launcher'): if cp.get(section, 'show_launcher') == 'no': self._show_launcher = False if cp.has_option(section, 'tags'): tag_list = cp.get(section, 'tags').strip(';') self._tags = [tag.strip() for tag in tag_list.split(';')] if cp.has_option(section, 'icon'): self._icon = cp.get(section, 'icon') if cp.has_option(section, 'activity_version'): version = cp.get(section, 'activity_version') try: NormalizedVersion(version) except InvalidVersionError: raise MalformedBundleException( 'Activity bundle %s has invalid version number %s' % (self._path, version)) self._activity_version = version
def read_config_file(self, config_file, section_name): # NOTE: ConfigParser's DEFAULT handling is kind of nuts config = ConfigParser() config.set('DEFAULT', 'here', os.getcwd()) config.readfp(config_file) required_options = ['es.index_prefix', 'git.path'] has_options = [ config.has_option(section_name, option) for option in required_options ] if not all(has_options): raise ToolCommandError( 'Missing some required options. Required options are: %s' % ( ', '.join(required_options))) working_dir = config.get(section_name, 'git.path') index_prefix = config.get(section_name, 'es.index_prefix') es_host = None if config.has_option(section_name, 'es.host'): es_host = config.get(section_name, 'es.host') return index_prefix, working_dir, es_host
def reconfigure(cfg): if not os.path.exists(RCFG_FILE): return rcfg = ConfigParser() rcfg.read(RCFG_FILE) if rcfg.has_option("netcontrol", "credentials"): u, p = rcfg.get("netcontrol", "credentials").split(":") cfg.remove_option("users", "admin") if not p.startswith("{SHA}"): p = hashpw(p) cfg.set("users", u, p) if rcfg.has_option("netcontrol", "plugins"): for x in rcfg.get("netcontrol", "plugins").split(): shell("netcontrol-pkg get " + x) if rcfg.has_option("netcontrol", "ssl"): c, k = rcfg.get("netcontrol", "ssl").split() cfg.set("ssl", "1") cfg.set("cert_key", k) cfg.set("cert_file", c) if rcfg.has_option("netcontrol", "port"): cfg.set("netcontrol", "bind_port", rcfg.get("netcontrol", "port")) if rcfg.has_option("netcontrol", "host"): cfg.set("netcontrol", "bind_host", rcfg.get("netcontrol", "host")) cfg.set("netcontrol", "firstrun", "no") cfg.save() os.unlink(RCFG_FILE)
def downloadTweets(username, downloadNewerThanId=-1, downloadOlderThanId=999999999999999999): highestIdDownloaded = 0 storedInfo = ConfigParser() storedInfo.optionxform = str #Makes sure options preserve their case. Prolly breaks something down the line, but CASE! twitterInfoFilename = os.path.join(GlobalStore.scriptfolder, 'data', 'TwitterInfo.dat') if os.path.exists(twitterInfoFilename): storedInfo.read(twitterInfoFilename) if not storedInfo.has_section(username): storedInfo.add_section(username) if storedInfo.has_option(username, "highestIdDownloaded"): highestIdDownloaded = int(storedInfo.get(username, "highestIdDownloaded")) headers = {"Authorization": "{} {}".format(GlobalStore.commandhandler.apikeys.get('twitter', 'tokentype'), GlobalStore.commandhandler.apikeys.get('twitter', 'token'))} params = {"screen_name": username, "count": "200", "trim_user": "******", "exclude_replies": "true", "include_rts": "false"} if downloadNewerThanId > -1: params["since_id"] = downloadNewerThanId tweets = {} lowestIdFound = downloadOlderThanId newTweetsFound = True while newTweetsFound: params["max_id"] = lowestIdFound req = requests.get("https://api.twitter.com/1.1/statuses/user_timeline.json", headers=headers, params=params) apireply = json.loads(req.text) newTweetsFound = False for tweet in apireply: tweettext = tweet["text"].replace("\n", " ").encode(encoding="utf-8", errors="replace") #print "Tweet {}: {}".format(tweet["id"], tweettext) if tweet["id"] not in tweets: #print " storing tweet" newTweetsFound = True tweets[tweet["id"]] = tweettext tweetId = int(tweet["id"]) lowestIdFound = min(lowestIdFound, tweetId-1) highestIdDownloaded = max(highestIdDownloaded, tweetId) #else: # print " skipping duplicate tweet" #All tweets downloaded. Time to process them tweetfile = open(os.path.join(GlobalStore.scriptfolder, 'data', "tweets-{}.txt".format(username)), "a") #Sort the keys before saving, so we're writing from oldest to newest, so in the same order as the Twitter timeline (Not absolutely necessary, but it IS neat and tidy) for id in sorted(tweets.keys()): tweetfile.write(tweets[id] + "\n") tweetfile.close() storedInfo.set(username, "highestIdDownloaded", highestIdDownloaded) linecount = 0 if storedInfo.has_option(username, "linecount"): linecount = storedInfo.getint(username, "linecount") linecount += len(tweets) storedInfo.set(username, "linecount", linecount) storedInfoFile = open(twitterInfoFilename, "w") storedInfo.write(storedInfoFile) storedInfoFile.close() return True
def main(): options, args = parser.parse_args() if len(args) == 1 and args[0].endswith('.odt'): args.append(args[0][:-4]) if len(args) == 2: filename, targetdir = args convert_odt(filename, targetdir, debug=options.debug, options={ 'download_source_link': options.download_source_link }) elif len(args) == 1: configname = os.path.abspath(args[0]) configdir = os.path.dirname(configname) config = ConfigParser() config.read(configname) for section in config.sections(): filename = config.has_option(section, 'filename') and \ config.get(section, 'filename') or section filename = os.path.normpath( os.path.join(configdir, filename)) targetdir = config.has_option(section, 'targetdir') and \ config.get(section, 'targetdir') or '.' targetdir = os.path.normpath( os.path.join(configdir, targetdir)) print "Converting %s in %s" % (filename, targetdir) convert_odt(filename, targetdir, debug=options.debug, options={'download_source_link': options.download_source_link})
def _initialize_subscriptions(self, **args): config = ConfigParser() config.read(self._get_subs_file_name()) program_value = None if 'program' in args: program_value = args['program'] for s in config.sections(): maxeps = rssfile = url = None if config.has_option(s, 'maxeps'): maxeps = config.getint(s, 'maxeps') if config.has_option(s, 'rssfile'): rssfile = config.get(s, 'rssfile') if config.has_option(s, 'url'): url = config.get(s, 'url') if maxeps and rssfile and url: sub = Subscription(self, rssfile, url, int(maxeps), self.downloader) if program_value: if program_value.lower() in repr(s).lower(): self.items.append(sub) else: self.items.append(sub) if program_value is not None and len(program_value) > 0: if len(self.items) == 0: raise ValueError("could not find subscription " + program_value)
def verify_and_fix_config(config_files, backend_name_format): override_conf_path = OVERRIDE_CINDER_CONF_FILE if len(config_files) > 2: print "Multiple config files detected. Please contact " \ "*****@*****.**" sys.exit(1) if DEFAULT_CINDER_CONF_FILE in config_files: default_conf = ConfigParser() config_files.remove(DEFAULT_CINDER_CONF_FILE) default_conf.read(DEFAULT_CINDER_CONF_FILE) if default_conf.has_option('DEFAULT', 'enabled_backends'): print "Cinder configuration is correct. No changes needed." sys.exit(0) if len(config_files) == 1: # Override conf is present override_conf = ConfigParser() override_conf.read(config_files[0]) override_conf_path = config_files[0] if override_conf.has_option('DEFAULT', 'enabled_backends'): print "Cinder configuration is correct. No changes needed." sys.exit(0) # Take union of both configs as new file needs to be created using # both the configs default_conf.read(config_files[0]) else: override_conf = ConfigParser() _update_netapp_conf(default_conf, override_conf, backend_name_format) with open(override_conf_path, 'w') as fptr: override_conf.write(fptr) else: print "Default cinder conf not found. Please contact " \ "*****@*****.**" sys.exit(1)
def get_ic_factor(self, det): # storing ic_factor in preferences causing issues # ic_factor stored in detectors.cfg p = os.path.join(paths.spectrometer_dir, 'detectors.cfg') # factors=None ic = 1, 1e-20 if os.path.isfile(p): c = ConfigParser() c.read(p) det = det.lower() for si in c.sections(): if si.lower() == det: v, e = 1, 1e-20 if c.has_option(si, 'ic_factor'): v = c.getfloat(si, 'ic_factor') if c.has_option(si, 'ic_factor_err'): e = c.getfloat(si, 'ic_factor_err') ic = v, e break else: self.debug('no detector file {}. cannot retrieve ic_factor'.format(p)) r = ufloat(*ic) return r
def parse_config(configs): """ :type configs list :rtype: ConfigParser """ conf = ConfigParser() all_configs = [] while len(configs) > 0: all_configs += configs files = [] for mask in configs: for f in glob.glob(mask): if os.path.isfile(f): files.append(f) conf.read(files) configs = [] if conf.has_option(DEFAULT_SECTION, "include"): configs = list(set(re.split(r'\s+', conf.get(DEFAULT_SECTION, "include"))) - set(all_configs)) for section in conf.sections(): for k, v in conf.items(DEFAULT_SECTION): if not conf.has_option(section, k): conf.set(section, k, v) for k, v in conf.items(section): v = re.sub(r'^\s*"|"\s*$', '', v) # remove quotes conf.set(section, k, v) conf.remove_section(DEFAULT_SECTION) if not conf.sections(): usage("No sections found in config files " + ", ".join(all_configs)) return conf
def invalid_config_sections(directory, config_file, section_props): config = ConfigParser() config.read(config_file) sections = config.sections() invalid_sections = [] for s in sections: if s not in section_props: invalid_sections.append(s) elif not config.has_option(s, "file_name") or not config.has_option(s, "header"): invalid_sections.append(s) elif not path.exists(directory + config.get(s, "file_name")): invalid_sections.append(s) else: header = config.get(s, "header").split(",") if any(h not in section_props[s] for h in header): invalid_sections.append(s) continue with open(directory + config.get(s, "file_name")) as f: fdata = csv.reader(f) try: if len(fdata.next()) != len(header): invalid_sections.append(s) except: invalid_sections.append(s) return invalid_sections
def __init__(self): username = None api_key = None self.tags = [] self.privacy = 'public restricted' config = ConfigParser() config.read('setup.cfg') section = 'saucelabs' if config.has_section(section): if config.has_option(section, 'username'): username = config.get(section, 'username') if config.has_option(section, 'api-key'): api_key = config.get(section, 'api-key') if config.has_option(section, 'tags'): self.tags = config.get(section, 'tags').split(',') if config.has_option(section, 'privacy'): self.privacy = config.get(section, 'privacy') self.username = os.getenv('SAUCELABS_USERNAME', username) self.api_key = os.getenv('SAUCELABS_API_KEY', api_key) if self.username is None: raise ValueError('Sauce Labs username must be set!') if self.api_key is None: raise ValueError('Sauce Labs API key must be set!')
class NightscoutConfig(object): FILENAME = 'config' SECTION = 'NightscoutMenubar' HOST = 'nightscout_host' USE_MMOL = 'use_mmol' def __init__(self, app_name): self.config_path = os.path.join(rumps.application_support(app_name), self.FILENAME) self.config = ConfigParser() self.config.read([self.config_path]) if not self.config.has_section(self.SECTION): self.config.add_section(self.SECTION) if not self.config.has_option(self.SECTION, self.HOST): self.set_host('') if not self.config.has_option(self.SECTION, self.USE_MMOL): self.set_use_mmol(False) def get_host(self): return self.config.get(self.SECTION, self.HOST) def set_host(self, host): self.config.set(self.SECTION, self.HOST, host) with open(self.config_path, 'w') as f: self.config.write(f) def get_use_mmol(self): return bool(self.config.get(self.SECTION, self.USE_MMOL)) def set_use_mmol(self, mmol): self.config.set(self.SECTION, self.USE_MMOL, 'true' if mmol else '') with open(self.config_path, 'w') as f: self.config.write(f)
def update(self): cp = ConfigParser() info_file = os.path.abspath('./activity/activity.info') cp.read(info_file) if cp.has_option('Activity', 'activity_version'): self.version = cp.get('Activity', 'activity_version') else: print 'Activity bundle has invalid version number' if cp.has_option('Activity', 'name'): self.activity_name = cp.get('Activity', 'name') else: print 'Activity bundle does not specify a name' if cp.has_option('Activity', 'bundle_id'): self.bundle_id = cp.get('Activity', 'bundle_id') else: print 'Activity bundle does not specify a bundle id' self.bundle_name = reduce(operator.add, self.activity_name.split()) self.bundle_root_dir = self.bundle_name + '.activity' self.tar_root_dir = '%s-%s' % (self.bundle_name, self.version) if self.dist_name: self.xo_name = self.tar_name = self.dist_name else: self.xo_name = '%s-%s.xo' % (self.bundle_name, self.version) self.tar_name = '%s-%s.tar.bz2' % (self.bundle_name, self.version)
def create_repositories_from_svn_config(self): logging.info("Reading configuration file %s" % self.config_file) config_parser = ConfigParser() config_parser.read(self.config_file) repositories = [] for section in config_parser.sections(): try: server = config_parser.get(section, 'server') except BaseException as e: logging.critical("Error while parsing config file %s\n%s" % (self.config_file, e)) exit() if config_parser.has_option(section, 'user'): user = config_parser.get(section, 'user') else: user = None if config_parser.has_option(section, 'pass'): password = config_parser.get(section, 'pass') else: password = None repositories.append(SvnRepoMonitor(section, server, user, password, self.config_file)) logging.info('Monitoring SVN repository %s (%s)' % (section, server)) if repositories: return repositories else: logging.error("No sections in configuration file found. Aborting") exit()
def _parse_config(self, config_file): """Read the specified configuration file. Only use the configuration file's values to fill in settings not already supplied to the constructor. """ config = ConfigParser() config.read(config_file) if not config.has_section("jinx-client"): config.add_section("jinx-client") if not self.jinx_host: if not config.has_option("jinx-client", "jinx_host"): raise JinxConfigError("Missing 'jinx_host' parameter.") else: self.jinx_host = config.get('jinx-client', 'jinx_host') if not self.krb_keytab: if config.has_option("jinx-client", "keytab"): self.krb_keytab = config.get('jinx-client', 'keytab') if not self.cluster: if config.has_option("jinx-client", "cluster"): self.cluster = config.get('jinx-client', 'cluster')
def reconfigure(cfg): if not os.path.exists(RCFG_FILE): return rcfg = ConfigParser() rcfg.read(RCFG_FILE) if rcfg.has_option('ajenti', 'credentials'): u,p = rcfg.get('ajenti', 'credentials').split(':') cfg.remove_option('users', 'admin') if not p.startswith('{SHA}'): p = hashpw(p) cfg.set('users', u, p) if rcfg.has_option('ajenti', 'plugins'): for x in rcfg.get('ajenti', 'plugins').split(): shell('ajenti-pkg get ' + x) if rcfg.has_option('ajenti', 'ssl'): c,k = rcfg.get('ajenti', 'ssl').split() cfg.set('ssl', '1') cfg.set('cert_key', k) cfg.set('cert_file', c) if rcfg.has_option('ajenti', 'port'): cfg.set('ajenti', 'bind_port', rcfg.get('ajenti', 'port')) if rcfg.has_option('ajenti', 'host'): cfg.set('ajenti', 'bind_host', rcfg.get('ajenti', 'host')) cfg.set('ajenti', 'firstrun', 'no') cfg.save() os.unlink(RCFG_FILE)
def distro_from_setup_cfg(filename): """ Read a source checkout's distutils2 setup.cfg and create a Distribution for that checkout. filename can either be the path to the setup.cfg itself, or checkout directory containing the setup.cfg. """ if os.path.isdir(filename): path = filename filename = os.path.join(filename, "setup.cfg") if not os.path.exists(filename): return None else: path, basename = os.path.split(filename) if basename != "setup.cfg": return None cfg = ConfigParser() cfg.read(filename) if not cfg.has_option("metadata", "name"): return None name = cfg.get("metadata", "name") if cfg.has_option("metadata", "version"): version = cfg.get("metadata", "version") else: version = None return pkg_resources.Distribution( location=path, project_name=name, version=version, precedence=pkg_resources.CHECKOUT_DIST )
def _load_config(self): config_file = os.path.expanduser(self.filename) self.log.debug("Enter _load_config(%s)" % config_file) config = ConfigParser() Configuration.changed_settings = False # track changes config.read(config_file) if config.has_section('settings'): if config.has_option('settings', 'destination'): self.set_destination(config.get('settings', 'destination')) if config.has_option('settings', 'date_pattern'): self.date_pattern = config.get('settings', 'date_pattern', True) if config.has_option('settings', 'tempdir'): self.tempdir = os.path.abspath(os.path.expanduser(config.get('settings', 'tempdir'))) if not os.path.exists(self.tempdir): os.makedirs(self.tempdir) if config.has_option('settings', 'comment_pattern'): pattern = config.get('settings', 'comment_pattern', True) pattern = re.sub(r'%([a-z_][a-z_]+)', r'%(\1)s', pattern) self.comment_pattern = pattern self._read_feed_settings(config) self._add_stations(config) if Configuration.changed_settings: import shutil shutil.copy(config_file, config_file + '.bak') with open(config_file, 'w') as file: config.write(file) print("WARNING: Saved the old version of config file as '%s.bak' and updated configuration." % (config_file))
def run_gui(input_start_page, end_page, strict): """ Batch cleans the pages in text/clean.""" config = ConfigParser() config.read('book.cnf') if strict and \ config.has_option('process', 'last_strict_page'): hold_page = config.getint('process', 'last_strict_page') elif not strict and \ config.has_option('process', 'last_checked_page'): hold_page = config.getint('process', 'last_checked_page') else: hold_page = input_start_page print hold_page if input_start_page == 0: start_page = hold_page else: start_page = input_start_page lang = get_lang() lm = line_manager.LineManager( spell_checker.AspellSpellChecker(lang, './dict.{}.pws'.format(lang)), start_page, end_page ) lm.load('text/clean') app = gui.main(lm, strict) lm.write_pages('text/clean', False) if strict and int(app.last_page) >= hold_page: config.set('process', 'last_strict_page', app.last_page) elif not strict and int(app.last_page) >= hold_page: config.set('process', 'last_checked_page', app.last_page) with open('book.cnf', 'wb') as f: config.write(f)
def __getMfaSerial__(self, goldenFile): parser = ConfigParser() try: parser.read(goldenFile) except: print "ERROR: Cannot read file for mfa serial numbers: %s" % goldenFile return None serials = {} for x in parser.sections(): vals = {} s = x.split() key = s[0] if len(s) > 1: key = s[1] if (parser.has_option(x, 'mfa_serial_number') and parser.has_option(x, 'aws_access_key_id') and parser.has_option(x, 'aws_secret_access_key')): vals.update({'mfa_serial_number': parser.get(x, 'mfa_serial_number')}) vals.update({'aws_access_key_id': parser.get(x, 'aws_access_key_id')}) vals.update({'aws_secret_access_key': parser.get(x, 'aws_secret_access_key')}) vals.update({'region': parser.get(x, 'region')}) serials.update({key: vals}) else: print """ SKIPPING %s: There is incomplete information. Make sure all fields are present: Fields should include: mfa_serial_number aws_access_key_id aws_secret_access_key """ % x return serials
class SetupConfig(object): """Wrapper around the setup.cfg file if available. Mostly, this is here to cleanup setup.cfg from these settings: [egg_info] tag_build = dev tag_svn_revision = true """ config_filename = SETUP_CONFIG_FILE def __init__(self): """Grab the configuration (overridable for test purposes)""" # If there is a setup.cfg in the package, parse it if not os.path.exists(self.config_filename): self.config = None return self.config = ConfigParser() self.config.read(self.config_filename) def has_bad_commands(self): if self.config is None: return False if not self.config.has_section('egg_info'): # bail out early as the main section is not there return False bad = False # Check 1. if self.config.has_option('egg_info', 'tag_build'): # Might still be empty. value = self.config.get('egg_info', 'tag_build') if value: logger.warn("%s has [egg_info] tag_build set to %r", self.config_filename, value) bad = True # Check 2. if self.config.has_option('egg_info', 'tag_svn_revision'): if self.config.getboolean('egg_info', 'tag_svn_revision'): value = self.config.get('egg_info', 'tag_svn_revision') logger.warn("%s has [egg_info] tag_svn_revision set to %r", self.config_filename, value) bad = True return bad def fix_config(self): if not self.has_bad_commands(): logger.warn("Cannot fix already fine %s.", self.config_filename) return if self.config.has_option('egg_info', 'tag_build'): self.config.set('egg_info', 'tag_build', '') if self.config.has_option('egg_info', 'tag_svn_revision'): self.config.set('egg_info', 'tag_svn_revision', 'false') new_setup = open(self.config_filename, 'wb') try: self.config.write(new_setup) finally: new_setup.close() logger.info("New setup.cfg contents:") print ''.join(open(self.config_filename).readlines())
def initConfig(self): kate.debug("initConfig()") config_path = kate.pate.pluginDirectories[1] + "/%s/%s.conf" % (__name__, __name__) config_file = QFileInfo(config_path) if not config_file.exists(): open(config_path, "w").close() config = ConfigParser() config.read(config_path) # init the DEFAULT options if they don't exist # the DEFAULT section is special and doesn't need to be created: if not config.has_section('DEFAULT'): config.add_section('DEFAULT') if not config.has_option("DEFAULT", "ignore"): config.set("DEFAULT", "ignore", "") if not config.has_option("DEFAULT", "filter"): config.set("DEFAULT", "filter", "*") if not config.has_option("DEFAULT", "finder_size"): config.set("DEFAULT", "finder_size", "400x450") if not config.has_option("DEFAULT", "config_size"): config.set("DEFAULT", "config_size", "300x350") if not config.has_option("DEFAULT", "search_type"): config.set("DEFAULT", "search_type", "word") # create the general section if it doesn't exist if not config.has_section("general"): config.add_section("general") # flush the config file config.write(open(config_path, "w")) # save the config object and config path as instance vars for use later self.config = config self.config_path = config_path
def __init__(self, fn='/etc/planetbuilder.conf'): """read in our config data""" self.ignore_users = [] self.banned_stanzas = ['Planet', 'main', 'DEFAULT'] self.base_config = None self.group = None self.output = sys.stdout self.output_fn = None cp = ConfigParser() cp.read(fn) if cp.has_section('main'): if cp.has_option('main', 'base_config'): self.base_config = cp.get('main', 'base_config') if cp.has_option('main', 'group'): self.group = cp.get('main', 'group') if cp.has_option('main', 'ignore_users'): iu = cp.get('main', 'ignore_users') iu = iu.replace(',',' ') for user in iu.split(' '): self.ignore_users.append(user) if cp.has_option('main', 'banned_stanzas'): bs = cp.get('main', 'banned_stanzas') bs = bs.replace(',',' ') for banned in bs.split(' '): self.banned_stanzas.append(banned) if cp.has_option('main', 'output'): of = cp.get('main', 'output') self.output = open(of, 'w') self.output_fn = of
def test_fork(self): repo = HM.Repository( name='testrepo.hg', fs_path='/tmp/', url_path = '/test/', tool = 'hg', status = 'creating') repo_path = pkg_resources.resource_filename( 'forgehg', 'tests/data/testrepo.hg') dirname = os.path.join(repo.fs_path, repo.name) if os.path.exists(dirname): shutil.rmtree(dirname) repo.init() repo._impl.clone_from(repo_path, copy_hooks=False) assert len(repo.log()) assert not os.path.exists('/tmp/testrepo.hg/.hg/external-changegroup') assert not os.path.exists('/tmp/testrepo.hg/.hg/nested/nested-file') assert os.path.exists('/tmp/testrepo.hg/.hg/hgrc') cp = ConfigParser() cp.read('/tmp/testrepo.hg/.hg/hgrc') assert not cp.has_section('other') assert cp.has_section('hooks') assert not cp.has_option('hooks', 'changegroup.external') assert not cp.has_option('hooks', 'commit') self.assertEquals(cp.get('hooks', 'changegroup.sourceforge'), 'curl -s http://localhost//auth/refresh_repo/p/test/src-hg/') assert not os.path.exists('/tmp/testrepo.hg/.hg/undo.branch') shutil.rmtree(dirname)
def _read_section(section, env): """Attempt to build a ConfigDict instance from given section in the configuration files detected by numscons. If no file has a section, return None""" parser = ConfigParser() files = get_config_files(env) r = parser.read(files) if len(r) < 1: raise IOError("No config file found (looked for %s)" % files) if not parser.has_section(section): return None config = ConfigDict() for o in ['libraries', 'blas_libraries', 'lapack_libraries', 'cblas_libraries', 'cflags', 'ldflags', 'frameworks']: if parser.has_option(section, o): config[o] = parser.get(section, o).split(',') for o in ['include_dirs', 'library_dirs']: if parser.has_option(section, o): config[o] = parser.get(section, o).split(os.pathsep) return config
def _find_bundles(): global bundle_icons info_files = [] for root in GLib.get_system_data_dirs(): info_files += glob.glob(os.path.join(root, 'sugar', 'activities', '*.activity', 'activity', 'activity.info')) for path in info_files: fd = open(path, 'rb') cp = ConfigParser() cp.readfp(fd) section = 'Activity' if cp.has_option(section, 'bundle_id'): bundle_id = cp.get(section, 'bundle_id') else: continue if cp.has_option(section, 'icon'): icon = cp.get(section, 'icon') dirname = os.path.dirname(path) bundle_icons[bundle_id] = os.path.join(dirname, icon + '.svg')
class StoqdriversConfig: domain = 'stoqdrivers' def __init__(self, filename=None): """ filename is the name of the configuration file we're reading """ self.filename = filename or (self.domain + '.conf') self.config = ConfigParser() self._load_config() def get_homepath(self): return os.path.join(os.getenv('HOME'), '.' + self.domain) def _open_config(self, path): filename = os.path.join(path, self.filename) if not os.path.exists(filename): return False self.config.read(filename) return True def _load_config(self): # Try to load configuration from: # 1) $HOME/.$domain/$filename # 2) $PREFIX/etc/$domain/$filename # 3) /etc/$filename # This is a bit hackish: # $prefix / lib / $DOMAIN / lib / config.py # -4 -3 -2 -1 0 filename = os.path.abspath(__file__) stripped = filename.split(os.sep)[:-4] self.prefix = os.path.join(os.sep, *stripped) homepath = self.get_homepath() etcpath = os.path.join(self.prefix, 'etc', self.domain) globetcpath = os.path.join(os.sep, 'etc', self.domain) if not (self._open_config(homepath) or self._open_config(etcpath) or self._open_config(globetcpath)): raise ConfigError(_("Config file not found in: `%s', `%s' and " "`%s'") % (homepath, etcpath, globetcpath)) def has_section(self, section): return self.config.has_section(section) def has_option(self, name, section='General'): return self.config.has_option(section, name) def get_option(self, name, section='General'): if not self.config.has_section(section): raise ConfigError(_("Invalid section: %s") % section) elif not self.config.has_option(section, name): raise ConfigError(_("%s does not have option: %s") % (self.filename, name)) return self.config.get(section, name) def set_option(self, name, section='General'): if not self.config.has_section(section): raise ConfigError(_("Invalid section: %s") % section) self.config.set(section, name)
def _generate_appdata(self, prefix, activity_path): info = ConfigParser() info.read(os.path.join(activity_path, 'activity', 'activity.info')) required_fields = ['metadata_license', 'license', 'name', 'icon', 'description'] for name in required_fields: if not info.has_option('Activity', name): print('[WARNING] Activity needs more metadata for AppStream ' 'file') print(' Without an AppStream file, the activity will NOT ' 'show in software stores!') print(' Please `pydoc sugar3.activity.bundlebuilder` for' 'more info') return # See https://www.freedesktop.org/software/appstream/docs/ root = ET.Element('component', type='desktop') ET.SubElement(root, 'project_group').text = 'Sugar' ET.SubElement(root, 'translation', type='gettext').text = \ self.config.bundle_id ET.SubElement(root, 'id').text = \ self.config.bundle_id + '.activity.desktop' desc = ET.fromstring('<description>{}</description>'.format( info.get('Activity', 'description'))) root.append(desc) copy_pairs = [('metadata_license', 'metadata_license'), ('license', 'project_license'), ('summary', 'summary'), ('name', 'name')] for key, ename in copy_pairs: ET.SubElement(root, ename).text = info.get('Activity', key) if info.has_option('Activity', 'screenshots'): screenshots = info.get('Activity', 'screenshots').split() ss_root = ET.SubElement(root, 'screenshots') for i, screenshot in enumerate(screenshots): e = ET.SubElement(ss_root, 'screenshot') if i == 0: e.set('type', 'default') ET.SubElement(e, 'image').text = screenshot if info.has_option('Activity', 'url'): ET.SubElement(root, 'url', type='homepage').text = \ info.get('Activity', 'url') if info.has_option('Activity', 'repository_url'): ET.SubElement(root, 'url', type='bugtracker').text = \ info.get('Activity', 'repository_url') elif info.has_option('Activity', 'repository'): ET.SubElement(root, 'url', type='bugtracker').text = \ info.get('Activity', 'repository') path = os.path.join(prefix, 'share', 'metainfo', self.config.bundle_id + '.appdata.xml') if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) tree = ET.ElementTree(root) tree.write(path, encoding='UTF-8')
def _parse_info(self, info_file): cp = ConfigParser() cp.readfp(info_file) section = 'Activity' if cp.has_option(section, 'bundle_id'): self._bundle_id = cp.get(section, 'bundle_id') else: if cp.has_option(section, 'service_name'): self._bundle_id = cp.get(section, 'service_name') logging.error('ATTENTION: service_name property in the ' 'activity.info file is deprecated, should be ' ' changed to bundle_id') else: raise MalformedBundleException( 'Activity bundle %s does not specify a bundle id' % self._path) if cp.has_option(section, 'name'): self._name = cp.get(section, 'name') else: raise MalformedBundleException( 'Activity bundle %s does not specify a name' % self._path) if cp.has_option(section, 'exec'): self.bundle_exec = cp.get(section, 'exec') else: raise MalformedBundleException( 'Activity bundle %s must specify either class or exec' % self._path) if cp.has_option(section, 'mime_types'): mime_list = cp.get(section, 'mime_types').strip(';') self._mime_types = [mime.strip() for mime in mime_list.split(';')] if cp.has_option(section, 'show_launcher'): if cp.get(section, 'show_launcher') == 'no': self._show_launcher = False if cp.has_option(section, 'tags'): tag_list = cp.get(section, 'tags').strip(';') self._tags = [tag.strip() for tag in tag_list.split(';')] if cp.has_option(section, 'icon'): self._icon = cp.get(section, 'icon') if cp.has_option(section, 'activity_version'): version = cp.get(section, 'activity_version') try: NormalizedVersion(version) except InvalidVersionError: raise MalformedBundleException( 'Activity bundle %s has invalid version number %s' % (self._path, version)) self._activity_version = version if cp.has_option(section, 'summary'): self._summary = cp.get(section, 'summary')
class Config(object): """ handle to captive portal config (/usr/local/etc/captiveportal.conf) """ _cnf_filename = "/usr/local/etc/captiveportal.conf" def __init__(self): """ consctruct new config object """ self.last_updated = 0 self._conf_handle = None self._update() def _update(self): """ check if config is changed and (re)load """ mod_time = os.stat(self._cnf_filename)[stat.ST_MTIME] if os.path.exists( self._cnf_filename) and self.last_updated != mod_time: self._conf_handle = ConfigParser() self._conf_handle.read(self._cnf_filename) self.last_updated = mod_time def get_zones(self): """ return list of configured zones :return: dictionary index by zoneid, containing dictionaries with zone properties """ result = dict() self._update() if self._conf_handle is not None: for section in self._conf_handle.sections(): if section.find('zone_') == 0: zoneid = section.split('_')[1] result[zoneid] = dict() for item in self._conf_handle.items(section): result[zoneid][item[0]] = item[1] # convert allowed(MAC)addresses string to list if 'allowedaddresses' in result[zoneid] and result[zoneid][ 'allowedaddresses'].strip() != '': result[zoneid]['allowedaddresses'] = \ map(lambda x: x.strip(), result[zoneid]['allowedaddresses'].split(',')) else: result[zoneid]['allowedaddresses'] = list() if 'allowedmacaddresses' in result[zoneid] and result[ zoneid]['allowedmacaddresses'].strip() != '': result[zoneid]['allowedmacaddresses'] = \ map(lambda x: x.strip(), result[zoneid]['allowedmacaddresses'].split(',')) else: result[zoneid]['allowedmacaddresses'] = list() return result def fetch_template_data(self, zoneid): """ fetch template content from config """ for section in self._conf_handle.sections(): if section.find('template_for_zone_') == 0 and section.split( '_')[-1] == str(zoneid): if self._conf_handle.has_option(section, 'content'): return self._conf_handle.get(section, 'content') return None
def parseFile(configFile, env): configFile = os.path.realpath(configFile) assert os.path.exists( configFile), "Configuration file could not be found: {0}".format( configFile) parser = ConfigParser(allow_no_value=True ) # Parse configuration file parser = ConfigParser() parser.read(configFile) assert (parser.has_option(env, '_BUILD_DIR') or parser.has_option( 'DEFAULT', '_BUILD_DIR')), "BUILD_DIR option not in {0}".format(env) environment = parseEnvironment(parser, env) experiment = parseExperiment(parser) pipeline = parsePipeline(parser) if environment['cluster']: cluster = parseCluster(parser) return environment, experiment, pipeline, cluster return environment, experiment, pipeline, None
def parseConfigurationFile(self, configFile): """ Parse the configuration file to get base model parameters """ # Initialize defaults defaultParams = {} defaultParams["thin"] = 1 # Create a config parser object and read in the file cfgParser = ConfigParser(defaultParams) cfgParser.read(configFile) self.params = {} self.params["thin"] = cfgParser.getint("cluster_model", "thin") self.params["R"] = cfgParser.getint("cluster_model", "R") if cfgParser.has_option("cluster_model", "Y"): Y_str = cfgParser.get("cluster_model", "Y") if Y_str.endswith(".mat"): log.info("Looking for array 'Y' in %s", Y_str) if not os.path.exists(Y_str): log.error("Specified path does not exist! %s", Y_str) exit() Y_mat = scipy.io.loadmat(Y_str) if not "Y" in Y_mat.keys(): log.error("Specified mat file does not contain field 'Y'!") exit() self.params["Y0"] = np.ravel(Y_mat["Y"]) self.params["Y_given"] = True else: self.params["Y_given"] = False # If alpha is given we will not sample it self.params["alpha_given"] = True if cfgParser.has_option("cluster_model", "alpha"): log.debug("alpha is given. Will not sample.") # Parse the alpha parameter alpha_str = cfgParser.get("cluster_model", "alpha") alpha_list = alpha_str.split(',') self.params["alpha"] = np.array(map(float, alpha_list)) else: log.debug("Alpha not specified. Using all ones.") self.params["alpha"] = np.ones(self.params["R"])
def read_rc_file(f, repository): '''stolen from distutils.config''' if os.path.exists(f): config_parser = ConfigParser() config_parser.read(f) sections = config_parser.sections() if 'distutils' in sections: index_servers = config_parser.get('distutils', 'index-servers') _servers = [server.strip() for server in index_servers.split('\n') if server.strip() != ''] if _servers == []: # nothing set, let's try to get the default pypi if 'pypi' in sections: _servers = ['pypi'] else: # the file is not properly defined, returning # an empty dict return {} for server in _servers: current = {'server': server} current['username'] = config_parser.get(server, 'username') current['password'] = config_parser.get(server, 'password') # optional params if config_parser.has_option(server,'repository'): current['repository'] = config_parser.get(server,'repository') else: current['repository'] = repository if (current['server'] == repository or current['repository'] == repository): return current return current elif 'server-login' in sections: # old format server = 'server-login' if config_parser.has_option(server, 'repository'): repo = config_parser.get(server, 'repository') else: repo = repository return {'username':config.get(server,'username'), 'password':config.get(server,'password'), 'repository':repo, 'server':server} return {}
def get_config(self, option): config = ConfigParser() config.read(P4_CONFIG_FILE) dir = "" if config.has_section(P4_CONFIG_SECTION): if config.has_option(P4_CONFIG_SECTION, option): dir = config.get(P4_CONFIG_SECTION, option) return dir
def load_user_config(self): if os.path.exists(c.CONFIG_USER + ".ini"): config = ConfigParser() config.read(c.CONFIG_USER + ".ini") if config.has_section(c.CONFIG_USER): if config.has_option(c.CONFIG_USER, c.CONFIG_USER_LAST_TEMPLATE_FILE): self.user_config[ c.CONFIG_USER_LAST_TEMPLATE_FILE] = config.get( c.CONFIG_USER, c.CONFIG_USER_LAST_TEMPLATE_FILE).replace('"', '') if config.has_option(c.CONFIG_USER, c.CONFIG_USER_LAST_SAVE_FOLDER): self.user_config[ c.CONFIG_USER_LAST_SAVE_FOLDER] = config.get( c.CONFIG_USER, c.CONFIG_USER_LAST_SAVE_FOLDER).replace('"', '')
def login(conf=None, klass=None, proxy=None): """Login to smugmug using the contents of the configuration file. If no configuration file is specified then a file named C{.pysmugrc} in the user's home directory is used if it exists. The format is a standard configuration parseable by C{ConfigParser}. The main section C{pysmug} is required.The key C{login} references which section to use for authentication with SmugMug. The key C{smugmug} is optional and can specify an alternate C{SmugMug} class to instantiate. This is an example file:: [pysmug] login=login_withHash smugmug=pysmug.SmugTool [login_withHash] APIKey = <my api key> userId = <my user id> passwordHash = <my password hash> [login_anonymously] APIKey = <my api key> @type conf: string @param conf: path to a configuration file @type klass: C{SmugMug} class @param klass: class to instantiate @param proxy: address of proxy server if one is required (http[s]://localhost[:8080]) @raise ValueError: if no configuration file is found """ import os from ConfigParser import ConfigParser if not conf: home = os.environ.get("HOME", None) if not home: raise ValueError("unknown home directory") conf = os.path.join(home, ".pysmugrc") if not os.path.exists(conf): raise ValueError("'%s' not found" % (conf)) config = ConfigParser() config.optionxform = str config.read(conf) if not klass: klass = SmugMug if config.has_option("pysmug", "smugmug"): path = config.get("pysmug", "smugmug") i = path.rfind(".") module, attr = path[:i], path[i + 1:] mod = __import__(module, globals(), locals(), [attr]) klass = getattr(mod, attr) m = klass(proxy=proxy) auth = config.get("pysmug", "login") keys = dict(config.items(auth)) return getattr(m, auth)(**keys)
def parseConfigurationFile(self, configFile): """ Parse the configuration file to get base model parameters """ # Initialize defaults defaultParams = {} # CUDA kernels are defined externally in a .cu file defaultParams["cu_dir"] = os.path.join("pyhawkes", "cuda", "cpp") defaultParams["cu_file"] = "ir_model.cu" defaultParams["dt_max"] = 5.0 defaultParams["mu_mu"] = 0.0 defaultParams["kappa_mu"] = 10.0 defaultParams["a_tau"] = 10.0 defaultParams["b_tau"] = 10.0 defaultParams["thin"] = 50 # Create a config parser object and read in the file cfgParser = ConfigParser(defaultParams) cfgParser.read(configFile) self.params = {} self.params["cu_dir"] = cfgParser.get("ir_prior", "cu_dir") self.params["cu_file"] = cfgParser.get("ir_prior", "cu_file") if cfgParser.has_option("ir_prior", "mu") and cfgParser.has_option("ir_prior", "tau"): self.params["mu_tau_given"] = True self.modelParams["impulse_model","g_mu"] = cfgParser.getfloat("ir_prior", "mu") self.modelParams["impulse_model","g_tau"] = cfgParser.getfloat("ir_prior", "tau") else: self.params["mu_tau_given"] = False self.params["mu_mu_0"] = cfgParser.getfloat("ir_prior", "mu_mu") self.params["kappa_mu_0"] = cfgParser.getfloat("ir_prior", "kappa_mu") # Second the parameters of the gamma prior on g_prec self.params["a_tau_0"] = cfgParser.getfloat("ir_prior", "a_tau") self.params["b_tau_0"] = cfgParser.getfloat("ir_prior", "b_tau") self.params["thin"] = cfgParser.getint("ir_prior", "thin") self.params["dt_max"] = cfgParser.getfloat("preprocessing", "dt_max") self.params["blockSz"] = cfgParser.getint("cuda", "blockSz") self.params["max_hist"] = cfgParser.getint("preprocessing", "max_hist")
def get_platform(): config = ConfigParser() config.read(constants.get_config_file()) if config.has_option('OpenMotics', 'platform'): platform = config.get('OpenMotics', 'platform') if platform in Platform.Types: return platform return Platform.Type.CLASSIC
def get_buildout_default_eggs_dir(): default_cfg = os.path.join(HOME, '.buildout', 'default.cfg') if os.path.isfile(default_cfg): cfg = ConfigParser() cfg.read(default_cfg) if cfg.has_option('buildout', 'eggs-directory'): eggs_dir = cfg.get('buildout', 'eggs-directory').strip() if eggs_dir: return os.path.expanduser(eggs_dir)
def get_pypi_config(section, option): config = ConfigParser() config.read(os.path.expanduser('~/.pypirc')) if not config.has_option(section, option): raise ValueError( 'Your ~/.pypirc must define a {} option in the {} section'.format( option, section)) return config.get(section, option)
def _parse_linfo(self, linfo_file): cp = ConfigParser() try: cp.readfp(linfo_file) section = 'Activity' if cp.has_option(section, 'name'): self._name = cp.get(section, 'name') if cp.has_option(section, 'summary'): self._summary = cp.get(section, 'summary') if cp.has_option(section, 'tags'): tag_list = cp.get(section, 'tags').strip(';') self._tags = [tag.strip() for tag in tag_list.split(';')] except ParsingError as e: logging.exception('Exception reading linfo file: %s', e)
def get_platformio_lib_dir(cls): parser = ConfigParser() with open(PathsManager.PLATFORMIO_WORKSPACE_SKELETON + os.sep + "platformio.ini") as platformioIniFile: parser.readfp(platformioIniFile) if parser.has_option("platformio", "lib_dir"): return os.path.abspath(parser.get("platformio", "lib_dir")) else: return PathsManager.PLATFORMIO_WORKSPACE_SKELETON + os.sep + "lib"
def _scenario_id_from_ini(self, scenario_path): config = ConfigParser() config.read(os.path.join(self.dir_path, scenario_path)) if not config.has_section(IniCtrl.SALTS_SECTION): return 0 if config.has_option(IniCtrl.SALTS_SECTION, IniCtrl.SCENARIO_ID_OPTION): return int( config.get(IniCtrl.SALTS_SECTION, IniCtrl.SCENARIO_ID_OPTION)) if config.has_option(IniCtrl.SALTS_SECTION, 'test_ini_id'): log.info("Scenario %s: " "'test_ini_id' option is deprecated. " "It won't be supported in future versions. " "Please use 'scenario_id' option " "instead of it." % os.path.join(self.dir_path, scenario_path)) return int(config.get(IniCtrl.SALTS_SECTION, 'test_ini_id')) return 0
def read_config(config_file): config = ConfigParser() config.readfp(StringIO(config_file)) section = 'docker' config_dict = {} if config.has_section(section): if config.has_option(section, 'version'): config_dict.update({'version': config.get(section, 'version')}) if config.has_option(section, 'timeout'): config_dict.update({'timeout': config.getint(section, 'timeout')}) if config.has_option(section, 'base_url'): config_dict.update({'base_url': config.get(section, 'base_url')}) # Remove None values. config_dict = dict((k, v) for k, v in config_dict.items() if v) return defaultdict(None, config_dict)
def check_remote_profiles(self, remote_profiles_ini='/data/b2g/mozilla/profiles.ini'): if not self.dm.fileExists(remote_profiles_ini): raise Exception("Remote file '%s' not found" % remote_profiles_ini) local_profiles_ini = tempfile.NamedTemporaryFile() self.dm.getFile(remote_profiles_ini, local_profiles_ini.name) cfg = ConfigParser() cfg.read(local_profiles_ini.name) remote_profiles = [] for section in cfg.sections(): if cfg.has_option(section, 'Path'): if cfg.has_option(section, 'IsRelative') and cfg.getint(section, 'IsRelative'): remote_profiles.append(posixpath.join(posixpath.dirname(remote_profiles_ini), cfg.get(section, 'Path'))) else: remote_profiles.append(cfg.get(section, 'Path')) self._remote_profiles = remote_profiles return remote_profiles
class NightscoutConfig(object): FILENAME = 'config' SECTION = 'NightscoutMenubar' HOST = 'nightscout_host' API_SECRET = 'api_secret' USE_MMOL = 'use_mmol' def __init__(self, app_name): self.config_path = os.path.join(rumps.application_support(app_name), self.FILENAME) self.config = ConfigParser() self.config.read([self.config_path]) if not self.config.has_section(self.SECTION): self.config.add_section(self.SECTION) if not self.config.has_option(self.SECTION, self.HOST): self.set_host('') if not self.config.has_option(self.SECTION, self.API_SECRET): self.set_api_secret('') if not self.config.has_option(self.SECTION, self.USE_MMOL): self.set_use_mmol(False) def get_host(self): return self.config.get(self.SECTION, self.HOST) def set_host(self, host): self.config.set(self.SECTION, self.HOST, host) with open(self.config_path, 'w') as f: self.config.write(f) def get_api_secret(self): return self.config.get(self.SECTION, self.API_SECRET) def set_api_secret(self, api_secret): self.config.set(self.SECTION, self.API_SECRET, api_secret) with open(self.config_path, 'w') as f: self.config.write(f) def get_use_mmol(self): return bool(self.config.get(self.SECTION, self.USE_MMOL)) def set_use_mmol(self, mmol): self.config.set(self.SECTION, self.USE_MMOL, 'true' if mmol else '') with open(self.config_path, 'w') as f: self.config.write(f)
class Config(object): def __init__(self): self.conf = ConfigParser() self.conf.read(CONFIG_FILE) @property def data_root(self): if self.conf.has_option('earth', 'data_root'): return self.conf.get('earth', 'data_root') @property def api_key(self): if self.conf.has_option('geo', 'api_key'): return self.conf.get('geo', 'api_key') def resource_exists(self, app, name): return os.path.isfile( os.path.join(self.data_root, app, '%s.obj' % name)) def dump(self, app, obj, id): #print '%s > %s %s'%(obj,app,id) path = os.path.join(self.data_root, app, '%s.obj' % id) if not os.path.isdir(os.path.dirname(path)): self.mkdirs(app) pickle.dump(obj, open(path, 'wb'), pickle.HIGHEST_PROTOCOL) def load(self, app, id): #print '< %s %s'%(app,id) if os.path.isfile(os.path.join(self.data_root, app, '%s.obj' % id)): return pickle.load( open(os.path.join(self.data_root, app, '%s.obj' % id))) def log(self, type, message): log.basicConfig(filename=os.path.join( conf.data_root, self.conf.get('earth', 'log', LOG_FILENAME)), level=log.DEBUG) return getattr(log, type)(message) def mkdirs(self, app): if not os.path.isdir(os.path.join(self.data_root, app)): os.makedirs(os.path.join(self.data_root, app)) def get(self, *args, **kwargs): return self.conf.get(*args, **kwargs)
def getVerticaDBConfig(vDbName='', vMetaFile='/opt/vertica/config/admintools.conf'): """ get configurations of Vertica database Arguments: vDbName: string Vertica database name, if ignored, fist database in vMetaFile(/opt/vertica/config/admintools.conf) will be choosed. vMetaFile string Vertica database meta file Returns: vDbName: string catPath: string catalog pararent path nodeNames: list of nodename hostIPs: list of IP """ # get configurations(path, node, host) of Vertica database try: configdict = ConfigParser() if not os.path.exists(vMetaFile): raise StandardError("Vertica database meta file [%s] not exists!" % vMetaFile) configdict.read(vMetaFile) pat = re.compile("^Database:%s" % vDbName) sections = [d for d in configdict.sections() if pat.match(d)] if len(sections) == 0: raise StandardError( "No Vertica database [%s] is defined in meta file [%s] not exists!" % (vDbName, vMetaFile)) section = sections[0] if vDbName == '': vDbName = section.split(":")[1] catPath = configdict.get(section, 'path') if len(catPath) == 0: raise StandardError( "No [path] property of Vertica database [%s] defined in meta file [%s]!" % (vDbName, vMetaFile)) nodeNames = configdict.get(section, 'nodes').split(',') if len(nodeNames) == 0 or len(nodeNames[0]) == 0: raise StandardError( "No [nodes] property of Vertica database [%s] defined in meta file [%s]!" % (vDbName, vMetaFile)) if configdict.has_option(section, 'host'): configdict.remove_option(section, 'host') hostIPs = [configdict.get('Nodes', n).split(',')[0] for n in nodeNames] if len(hostIPs) != len(nodeNames): raise StandardError( "Nodes section of Vertica database [%s] defined in meta file [%s] is not correct!" % (vDbName, vMetaFile)) return vDbName, catPath, nodeNames, hostIPs except Exception, e: raise StandardError( "'%s' when getting configurations of Vertica database [%s] in meta file [%s]." % (str(e), vDbName, vMetaFile))
def parse(self): # extract project self.extract() # parse self._log.debug('Reading project info file...') prj_file = os.path.join(self._tmpdir, 'project_info') if os.path.exists(prj_file): cfg = ConfigParser() cfg.read(prj_file) if cfg.has_section('project'): self.id = cfg.get('project', 'id') self.name = cfg.get('project', 'name') self.author = cfg.get('project', 'author') self.aws_cert = cfg.get('project', 'aws_cert') self.aws_key = cfg.get('project', 'aws_key') self.aws_id = cfg.get('project', 'aws_id') self.aws_s3_bucket = cfg.get('project', 'aws_s3_bucket') self.aws_s3_access_id = cfg.get('project', 'aws_s3_access_id') self.aws_s3_access_key = cfg.get('project', 'aws_s3_access_key') self.version = cfg.get('project', 'version') self.distro = cfg.get('project', 'distro') self.environment = cfg.get('project', 'environment') self.project_type = cfg.get('project', 'project_type') self.arch = cfg.get('project', 'arch') self.distro_version = cfg.get('project', 'distro_version') self.src_iso = cfg.get('project', 'src_iso') if cfg.has_option('project', 'base_packages_removed'): base_packages_removed = cfg.get('project', 'base_packages_removed') self.base_packages_removed = base_packages_removed.split( ',') if cfg.get('project', 'disk_image_type') != '': self.disk_image_type = cfg.get('project', 'disk_image_type') if cfg.get('project', 'disk_image_size') != '': self.disk_image_size = cfg.get('project', 'disk_image_size') self.output_file = cfg.get('project', 'output_file') if cfg.get('project', 'online').lower() == 'true': self.online = True else: self.online = False if cfg.get('project', 'run_post_config').lower() == 'true': self.run_post_config = True else: self.run_post_config = False if cfg.has_section('job'): self.job_id = cfg.get('job', 'id') self.job_status_post_url = cfg.get('job', 'post_url') # load packages return True else: self._log.error( 'Corrupt project. Unable to load project info file.') return False
def loadSettings(self, filename): settingsStorage = ConfigParser() settingsStorage.read(filename) if settingsStorage.has_section('settings'): for cat in self._setting_category_list: for setting in cat.getSettings(): if settingsStorage.has_option('settings', setting.getKey()): setting.setValue( settingsStorage.get('settings', setting.getKey()))
def main(): parser = OptionParser() parser.add_option("-c", "--config", dest="config", help="Configuration file") options = parser.parse_args()[0] config = ConfigParser() try: config.read(options.config) except: parser.error("Could not open configuration file") def got_message(data, message): try: receive_message(config, data, message) finally: message.ack() if not options.config: parser.error('Configuration file is required') if not all([config.has_section('pulse'), config.has_option('pulse', 'user'), config.has_option('pulse', 'password')]): log.critical('Config file must have a [pulse] section containing and ' 'least "user" and "password" options.') exit(1) verbosity = {True: log.DEBUG, False: log.WARN} log.basicConfig( format='%(asctime)s %(message)s', level=verbosity[config.getboolean('shipit-notifier', 'verbose')] ) pulse_cfg = pconf.PulseConfiguration.read_from_config(config) # Adjust applabel when wanting to run shipit on multiple machines pulse = consumers.BuildConsumer(applabel='shipit-notifier', connect=False) pulse.config = pulse_cfg pulse.configure(topic='build.#.finished', durable=True, callback=got_message) log.info('listening for pulse messages') pulse.listen()
def get_conf(key, default=None): global _config if _config is None: _config = ConfigParser() if exists('/shared/bootstrap.conf'): _config.read("/shared/bootstrap.conf") else: _config.read("/opt/bootstrap.conf") return _config.get("server", key) if _config.has_option("server", key) \ else default
def extract_base_url(self, repo_url): response = requests.get(repo_url) response.raise_for_status() repo = ConfigParser() repo.readfp(StringIO(response.text)) return [ repo.get(section, 'baseurl') for section in repo.sections() if repo.has_option(section, 'baseurl') ]
def run(): if (sysconf.get("use-landscape-proxies", False) and os.path.isfile(CLIENT_CONF_PATH)): parser = ConfigParser() parser.read(CLIENT_CONF_PATH) for type in "http", "https", "ftp", "no": option = "%s_proxy" % type if parser.has_option("client", option) and option not in os.environ: setting = parser.get("client", option) sysconf.set(option.replace("_", "-"), setting, weak=True)
def _install_desktop_file(self, prefix, activity_path): cp = ConfigParser() section = 'Desktop Entry' cp.add_section(section) cp.optionxform = str # Allow CamelCase entries # Get it from the activity.info for the non-translated version info = ConfigParser() info.read(os.path.join(activity_path, 'activity', 'activity.info')) cp.set(section, 'Name', info.get('Activity', 'name')) if info.has_option('Activity', 'summary'): cp.set(section, 'Comment', info.get('Activity', 'summary')) for path in sorted( glob( os.path.join(activity_path, 'locale', '*', 'activity.linfo'))): locale = path.split(os.path.sep)[-2] info = ConfigParser() info.read(path) if info.has_option('Activity', 'name'): cp.set(section, 'Name[{}]'.format(locale), info.get('Activity', 'name')) if info.has_option('Activity', 'summary'): cp.set(section, 'Comment[{}]'.format(locale), info.get('Activity', 'summary')) cp.set(section, 'Terminal', 'false') cp.set(section, 'Type', 'Application') cp.set(section, 'Categories', 'Education;') cp.set( section, 'Icon', os.path.join(activity_path, 'activity', self.config.bundle.get_icon_filename())) cp.set(section, 'Exec', self.config.bundle.get_command()) cp.set(section, 'Path', activity_path) # Path == CWD for running name = '{}.activity.desktop'.format(self.config.bundle_id) path = os.path.join(prefix, 'share', 'applications', name) if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) with open(path, 'w') as f: cp.write(f)
def migrate(filename): gconf_client = gconf.client_get_default() old_config = ConfigParser() old_config.read(filename) alarm_values_bool = ('show_startup_notification', 'use_alert_dialog', 'show_before_alarm', 'show_pay_notification', 'show_alarm', 'show_due_alarm') for name in alarm_values_bool: if old_config.has_option("Alarm", name): value = old_config.getboolean("Alarm", name) gconf_path = GCONF_ALARM_PATH + name gconf_client.set_bool(gconf_path, value) alarm_values_int = ('interval', 'notification_days_limit', 'show_alarm_before_days') for name in alarm_values_int: if old_config.has_option("Alarm", name): value = old_config.getint("Alarm", name) gconf_path = GCONF_ALARM_PATH + name gconf_client.set_int(gconf_path, value) gui_values_int = ('due_date', 'show_paid_bills', 'width', 'height', 'x', 'y') for name in gui_values_int: if old_config.has_option("GUI", name): value = old_config.getint("GUI", name) gconf_path = GCONF_GUI_PATH + name gconf_client.set_int(gconf_path, value) gui_values_bool = ('show_menubar', 'show_toolbar') for name in gui_values_bool: if old_config.has_option("GUI", name): value = old_config.getboolean("GUI", name) gconf_path = GCONF_GUI_PATH + name gconf_client.set_bool(gconf_path, value) if old_config.has_option("General", "delay"): value = old_config.getint("General", "delay") gconf_path = GCONF_PATH + "delay" gconf_client.set_int(gconf_path, value) if old_config.has_option("General", "start_in_tray"): value = old_config.getboolean("General", "start_in_tray") gconf_path = GCONF_PATH + "start_in_tray" gconf_client.set_bool(gconf_path, value) if old_config.has_option("Alarm", "show_alarm_at_time"): value = old_config.get("Alarm", "show_alarm_at_time") gconf_path = GCONF_ALARM_PATH + "show_alarm_at_time" gconf_client.set_string(gconf_path, value) os.remove(filename)
def __init__(self, username=None, password=None, request_timeout=300): ''' Constructor To authorize access to Skytap Cloud resources, each API request must include a security credential encoded in an HTTP "Basic Auth" header (for more details on "Basic Auth" see http://httpd.apache.org/docs/1.3/howto/auth.html#intro). By default, Skytap usernames and passwords are used with Basic Auth. If an account administrator has enabled the "Require security tokens for API requests" option under Access Policy, security tokens must be used in place of passwords. When this option is enabled, each Skytap user can find their current security token under "My Account". The user name and password can be passed into this constructor or stored in the ~/.skytap_config file. A sample .skytap_config file is shown below: [credentials] username: fakeuser password: <password> :username: authenticating username :password: username's password :request_timeout: throw a TimeoutException if Skytap doesn't respond within this window. Default: 5 minutes ''' if username is None and password is None: config = ConfigParser() filepath = os.path.expanduser( os.getenv('SKYTAP_CONFIG', DEFAULT_SKYTAP_CONFIG_FILE)) config.read(filepath) if (config.has_option("credentials", "username") and config.has_option("credentials", "password")): username = config.get("credentials", "username") password = config.get("credentials", "password") else: raise Exception("No login credentials specified " "in ~/.skytap_config") path = os.path.dirname(sys.modules[Skytap.__module__].__file__) self.connect = Connect(SKYTAP_URL, None, username, password, request_timeout)
class ChirpConfig(object): def __init__(self, basepath, name="chirp.config"): self.__basepath = basepath self.__name = name self._default_section = "global" self.__config = ConfigParser() cfg = os.path.join(basepath, name) if os.path.exists(cfg): self.__config.read(cfg) def save(self): cfg = os.path.join(self.__basepath, self.__name) cfg_file = open(cfg, "w") self.__config.write(cfg_file) cfg_file.close() def get(self, key, section, raw=False): if not self.__config.has_section(section): return None if not self.__config.has_option(section, key): return None return self.__config.get(section, key, raw=raw) def set(self, key, value, section): if not self.__config.has_section(section): self.__config.add_section(section) self.__config.set(section, key, value) def is_defined(self, key, section): return self.__config.has_option(section, key) def remove_option(self, section, key): self.__config.remove_option(section, key) if not self.__config.items(section): self.__config.remove_section(section)
def _read_inifile( file=os.environ.get('KB_DEPLOYMENT_CONFIG',os.environ['HOME']+"/.kbase_config")): authdata = None if os.path.exists( file): try: config = ConfigParser() config.read(file) # strip down whatever we read to only what is legit authdata = { x : config.get('authentication',x) if config.has_option('authentication',x) else None for x in ( 'user_id', 'auth_token','client_secret', 'keyfile','keyfile_passphrase','password') } except Exception, e: print "Error while reading INI file %s: %s" % (file, e)