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!')
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 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 " \ "[email protected]" 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 " \ "[email protected]" sys.exit(1)
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("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": "true", "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 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)
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 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
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 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 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 _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 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 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 _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)
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 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_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 __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
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 _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')
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 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 _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 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 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 _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')
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')