def __init__(self): if not os.path.exists("BAG.conf"): print "*** FOUT *** Kan configuratiebestand 'BAG.conf' niet openen." print "" raw_input("Druk <enter> om af te sluiten") sys.exit() configdict = ConfigParser() configdict.read("BAG.conf") try: self.database = configdict.defaults()["database"] self.host = configdict.defaults()["host"] self.user = configdict.defaults()["user"] self.password = configdict.defaults()["password"] self.download = configdict.defaults()["download"] self.extract = configdict.defaults()["extract"] self.logging = configdict.defaults()["logging"] except: print "*** FOUT *** Inhoud van configuratiebestand 'BAG.conf' is niet volledig." print "" raw_input("Druk <enter> om af te sluiten") sys.exit() try: self.bestand = configdict.defaults()["bestand"] except: pass
def __init__(self): if not os.path.exists('BAG.conf'): print "*** FOUT *** Kan configuratiebestand 'BAG.conf' niet openen." print "" #raw_input("Druk <enter> om af te sluiten") sys.exit() configdict = ConfigParser() configdict.read('BAG.conf') try: self.database = configdict.defaults()['database'] self.host = configdict.defaults()['host'] self.user = configdict.defaults()['user'] self.password = configdict.defaults()['password'] self.download = configdict.defaults()['download'] self.extract = configdict.defaults()['extract'] self.logging = configdict.defaults()['logging'] # TODO controleer of er commandline parameters zijn meegegeven die de instellingen overrulen except: print "*** FOUT *** Inhoud van configuratiebestand 'BAG.conf' is niet volledig." print "" raw_input("Druk <enter> om af te sluiten") sys.exit() try: self.bestand = configdict.defaults()['bestand'] except: pass
def test_export_site_with_subfolders(self): from Products.GenericSetup.utils import _getDottedName self._setUpAdapters() FOLDER_IDS = ('foo', 'bar', 'baz') site = _makeFolder('site') site.title = 'AAA' site._setProperty('description', 'BBB') aside = _makeFolder('aside') dotted = _getDottedName(aside.__class__) for id in FOLDER_IDS: folder = _makeFolder(id) folder.title = 'Title: %s' % id site._setObject(id, folder) context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 2 + (2 * len(FOLDER_IDS))) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 3) for index in range(len(FOLDER_IDS)): id = FOLDER_IDS[index] self.assertEqual(objects[index][0], id) self.assertEqual(objects[index][1], dotted) filename, text, content_type = context._wrote[2 + (2 * index)] self.assertEqual(filename, '/'.join(('structure', id, '.objects'))) self.assertEqual(content_type, 'text/comma-separated-values') subobjects = [x for x in reader(StringIO(text))] self.assertEqual(len(subobjects), 0) filename, text, content_type = context._wrote[2 + (2 * index) + 1] self.assertEqual(filename, '/'.join( ('structure', id, '.properties'))) self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) defaults = parser.defaults() self.assertEqual(len(defaults), 1) self.assertEqual(defaults['title'], 'Title: %s' % id) filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) defaults = parser.defaults() self.assertEqual(len(defaults), 2) self.assertEqual(defaults['title'], 'AAA') self.assertEqual(defaults['description'], 'BBB')
def __init__(self): if not os.path.exists('BAG.conf'): print "*** FOUT *** Kan configuratiebestand 'BAG.conf' niet openen." print "" raw_input("Druk <enter> om af te sluiten") sys.exit() configdict = ConfigParser() configdict.read('BAG.conf') try: self.database = configdict.defaults()['database'] self.host = configdict.defaults()['host'] self.user = configdict.defaults()['user'] self.password = configdict.defaults()['password'] self.download = configdict.defaults()['download'] self.extract = configdict.defaults()['extract'] self.logging = configdict.defaults()['logging'] except: print "*** FOUT *** Inhoud van configuratiebestand 'BAG.conf' is niet volledig." print "" raw_input("Druk <enter> om af te sluiten") sys.exit() try: self.bestand = configdict.defaults()['bestand'] except: pass
def test_export_site_with_subfolders(self): from Products.GenericSetup.utils import _getDottedName self._setUpAdapters() FOLDER_IDS = ('foo', 'bar', 'baz') site = _makeFolder('site') site.title = 'AAA' site._setProperty('description', 'BBB') aside = _makeFolder('aside') dotted = _getDottedName(aside.__class__) for id in FOLDER_IDS: folder = _makeFolder(id) folder.title = 'Title: %s' % id site._setObject(id, folder) context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 2 + (2 *len(FOLDER_IDS))) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 3) for index in range(len(FOLDER_IDS)): id = FOLDER_IDS[index] self.assertEqual(objects[index][0], id) self.assertEqual(objects[index][1], dotted) filename, text, content_type = context._wrote[2 + (2 * index)] self.assertEqual(filename, '/'.join(('structure', id, '.objects'))) self.assertEqual(content_type, 'text/comma-separated-values') subobjects = [x for x in reader(StringIO(text))] self.assertEqual(len(subobjects), 0) filename, text, content_type = context._wrote[2 + (2 * index) + 1] self.assertEqual(filename, '/'.join(('structure', id, '.properties'))) self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) defaults = parser.defaults() self.assertEqual(len(defaults), 1) self.assertEqual(defaults['title'], 'Title: %s' % id) filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) defaults = parser.defaults() self.assertEqual(len(defaults), 2) self.assertEqual(defaults['title'], 'AAA') self.assertEqual(defaults['description'], 'BBB')
def __init__(self, args): # Derive home dir from script location self.bagextract_home = os.path.realpath(os.path.dirname(sys.argv[0]) + '/..') # Default config file config_file = os.path.realpath(self.bagextract_home + '/extract.conf') # Option: overrule config file with command line arg pointing to config file if args.config: config_file = args.config Log.log.info("Configuratiebestand is " + str(config_file)) if not os.path.exists(config_file): Log.log.fatal("ik kan het configuratiebestand '" + str(config_file) + "' ech niet vinden.") configdict = ConfigParser() try: configdict.read(config_file) except: Log.log.fatal("ik kan " + str(config_file) + " wel vinden maar niet inlezen.") try: # Zet parameters uit config bestand self.database = configdict.defaults()['database'] self.schema = configdict.defaults()['schema'] self.host = configdict.defaults()['host'] self.user = configdict.defaults()['user'] self.password = configdict.defaults()['password'] self.port = configdict.defaults()['port'] except: Log.log.fatal(" de inhoud van configuratiebestand " + str(config_file) + " is niet volledig.") try: # Optioneel: overrulen met (commandline) args if args.database: self.database = args.database if args.host: self.host = args.host if args.schema: self.schema = args.schema # default to public schema if not self.schema: self.schema = 'public' if args.username: self.user = args.username if args.port: self.port = args.port if args.no_password: # Gebruik geen wachtwoord voor de database verbinding self.password = None else: if args.password: self.password = args.password # Assign Singleton (of heeft Python daar namespaces voor?) (Java achtergrond) BAGConfig.config = self except: Log.log.fatal(" het overrulen van configuratiebestand " + str(config_file) + " via commandline loopt spaak")
def raw_configurator(ini_file, local_conf_key='app:mailcone', here=__file__, zope_conf=''): configparser = ConfigParser() configparser.read(ini_file) configparser.defaults().update(dict(here=here)) configurator(dict(here=here, __file__=ini_file, zope_conf=zope_conf), **dict(configparser.items(local_conf_key)))
def load_app(filename): filename = os.path.abspath(filename) parser = ConfigParser() parser.read([filename]) parser.filename = filename parser.defaults()['here'] = os.path.dirname(filename) parser.defaults()['__file__'] = filename return load_app_from_parser(parser)
def test_export_empty_site_with_setup_tool(self): self._setUpAdapters() site = _makeFolder('site') site._setObject('setup_tool', self._makeSetupTool()) site._updateProperty('title', 'test_export_empty_site_with_setup_tool') site._setProperty('description', 'Testing export of an empty site with setup tool.') context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 2) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 0) filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) defaults = parser.defaults() self.assertEqual(len(defaults), 2) self.assertEqual(defaults['title'], site.title) self.assertEqual(defaults['description'], site.description)
def test_export_empty_site(self): self._setUpAdapters() site = _makeFolder('site') site.title = 'test_export_empty_site' site.description = 'Testing export of an empty site.' context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 2) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 0) filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) defaults = parser.defaults() self.assertEqual(len(defaults), 1) self.assertEqual(defaults['title'], site.title)
def options(self): # If all fails we will always have default values configuration = self.defaults() try: if self.config == None or isfile(self.config) == False: configuration = self.defaults() return configuration except TypeError: # if we are getting a ready-to-go dict then we still try # to do our little translation-and-map thing and if that # comes out as empty, then we assume keys are already # translated if type(self.config) is dict: configuration = self.key_matcher(self.config, return_empty=True) if not configuration: configuration = self.defaults(self.config) return configuration # we could get an object that is dict-like but type(object) # doesn't recognize it as a dict else: configuration = self.key_matcher(self.config) return configuration else: # this will get executed *only* if we are seeing a file try: parser = ConfigParser() parser.read(self.config) file_options = parser.defaults() configuration = self.key_matcher(file_options) except Exception, error: raise OptionConfigurationError(error)
def load_environment(global_conf, app_conf): """Configure the Pylons environment via the ``pylons.config`` object """ config = PylonsConfig() # Pylons paths root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=[os.path.join(root, 'templates')]) # import our private.ini that holds keys, etc imp = global_conf.get('import') if imp: cp = ConfigParser() cp.read(imp) global_conf.update(cp.defaults()) if cp.has_section('APP'): app_conf.update(cp.items('APP')) # Initialize config with the basic options config.init_app(global_conf, app_conf, package='linkdrop', paths=paths) config['routes.map'] = make_map(config) config['pylons.app_globals'] = app_globals.Globals(config) import linkdrop.lib.helpers as h config['pylons.h'] = h # Setup cache object as early as possible import pylons pylons.cache._push_object(config['pylons.app_globals'].cache) # Create the Mako TemplateLookup, with the default auto-escaping config['pylons.app_globals'].mako_lookup = TemplateLookup( directories=paths['templates'], error_handler=handle_mako_error, module_directory=os.path.join(app_conf['cache_dir'], 'templates'), input_encoding='utf-8', default_filters=['escape'], imports=['from webhelpers.html import escape']) # Setup the SQLAlchemy database engine engine = engine_from_config(config, 'sqlalchemy.') init_model(engine) # sqlalchemy auto migration if asbool(config.get('migrate.auto')): try: # managed upgrades cschema = schema.ControlledSchema.create(engine, config['migrate.repository']) cschema.update_db_from_model(meta.Base.metadata) except exceptions.InvalidRepositoryError, e: # unmanaged upgrades diff = schemadiff.getDiffOfModelAgainstDatabase( meta.Base.metadata, engine, excludeTables=None) genmodel.ModelGenerator(diff).applyModel()
def parse_config(self): """ Reads the config file (default in /etc/vcscommit/vcscommit.cfg :rtype: ConfigParser object """ cfg = ConfigParser() cfg.read('/etc/vcscommit/vcscommit.cfg') return cfg.defaults()
def test_as_ini_no_properties(self): context = _makePropertied('no_properties') context._properties = () adapter = self._getTargetClass()(context) text = adapter.as_ini() parser = ConfigParser() parser.readfp(StringIO(text)) self.failIf(parser.sections()) default_options = parser.defaults() self.assertEqual(len(default_options), 0)
def defaults(self): """Return the defaults, with their values interpolated (with the defaults dict itself) Mainly to support defaults using values such as %(here)s """ defaults = ConfigParser.defaults(self).copy() for key, val in iteritems(defaults): defaults[key] = self.get('DEFAULT', key) or val return defaults
def put_ini(self, text): """ """ context = self.context parser = ConfigParser() parser.readfp(StringIO(text)) for option, value in parser.defaults().items(): prop_type = context.getPropertyType(option) if prop_type is None: context._setProperty(option, value, 'string') else: context._updateProperty(option, value)
class Config(object): def __init__(self): self._parser = ConfigParser() self._parser._defaults = IncludeDict(self._parser) # Copy attributes from the parser to avoid one additional # function call on each access. for attr in ["has_section", "remove_section"]: setattr(self, attr, getattr(self._parser, attr)) def read_configs(self, configs): for config in configs: match = re.match("(.*)/([^/]+)=(.*)", config) if not match: raise Exception, "Invalid config string: %s" % config (name, option, value) = match.groups() if not self._parser.has_section(name): self._parser.add_section(name) self._parser.set(name, option, value) def read_file(self, file, filename="<stream>"): logging.info("Reading configurations from: %s", filename) self._parser.readfp(file, filename) def read_filename(self, filename): if not posixpath.exists(filename): raise Exception, "No such configuration file: %s" % filename file = open(filename, "r") return self.read_file(file, filename) def get_defaults(self): attributes = self._parser.defaults() return ConfigDefaults(self, 'DEFAULT', attributes) def get_section(self, name): if self._parser.has_section(name): attributes = dict(self._parser.items(name)) return ConfigSection(self, name, attributes) return None def get_section_names(self): return self._parser.sections() def add_section(self, name): self._parser.add_section(name) return self.get_section(name)
def handle_config_option(option, opt, value, parser, *args, **kwargs): fp = open(value, 'r') del fp parser.values.config_file = value config = ConfigParser() parser.values.config = config config.read(value) for k, v in config.defaults().iteritems(): try: v = config.getboolean('DEFAULT', k) except ValueError: pass setattr(parser.values, k, v)
def read(self, rc_file): rc = {} rcparser = ConfigParser() rcparser.read(rc_file) for option, value in rcparser.defaults().items(): rc[option] = value for section in rcparser.sections(): for option in rcparser.options(section): rc[option] = rcparser.get(section, option) return rc
def parse(parser, args=None): parser.add_option("-o", "--output", action="store", help="write output to file") parser.add_option("-i", "--ini", action="store", help="load settings from INI file") parser.add_option("-j", "--json", action="store", help="load settings from JSON file") parser.add_option("-y", "--yaml", action="store", help="load settings from YAML file") (options, args) = parser.parse_args(args) d = {} if options.ini: from ConfigParser import ConfigParser conf = ConfigParser() if options.ini == '-': cfg = sys.stdin else: cfg = file(options.ini) conf.readfp(cfg) d.update(conf.defaults()) for sect in conf.sections(): d[sect] = dict(conf.items(sect)) if options.json: import json if options.json == '-': cfg = sys.stdin else: cfg = file(options.json) d.update(json.load(cfg)) if options.yaml: import yaml if options.yaml == '-': cfg = sys.stdin else: cfg = file(options.yaml) d.update(yaml.load(cfg)) for arg in args: key, val = [s.strip() for s in arg.split('=', 1)] d[key] = val options.data = d return options
def test_export_site_with_csvaware(self): from Products.GenericSetup.utils import _getDottedName self._setUpAdapters() site = _makeFolder('site') site.title = 'test_export_site_with_csvaware' site._setProperty('description', 'Testing export of an site with CSV-aware content.') aware = _makeCSVAware('aware') site._setObject('aware', aware) context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 3) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 1) self.assertEqual(objects[0][0], 'aware') self.assertEqual(objects[0][1], _getDottedName(aware.__class__)) filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) defaults = parser.defaults() self.assertEqual(len(defaults), 2) self.assertEqual(defaults['title'], site.title) self.assertEqual(defaults['description'], site.description) filename, text, content_type = context._wrote[2] self.assertEqual(filename, 'structure/aware.csv') self.assertEqual(content_type, 'text/comma-separated-values') rows = [x for x in reader(StringIO(text))] self.assertEqual(len(rows), 2) self.assertEqual(rows[0][0], 'one') self.assertEqual(rows[0][1], 'two') self.assertEqual(rows[0][2], 'three') self.assertEqual(rows[1][0], 'four') self.assertEqual(rows[1][1], 'five') self.assertEqual(rows[1][2], 'six')
def test_as_ini_string_property(self): TITLE = 'String Property' DESCR = 'Another property' context = _makePropertied('string_property') context.title = TITLE context._setProperty('description', DESCR) adapter = self._getTargetClass()(context) text = adapter.as_ini() parser = ConfigParser() parser.readfp(StringIO(text)) self.failIf(parser.sections()) default_options = parser.defaults() self.assertEqual(len(default_options), 2) self.assertEqual(default_options['title'].strip(), TITLE) self.assertEqual(default_options['description'].strip(), DESCR)
def ini2keys(fp): cp = ConfigParser() cp.optionxform = str cp.readfp(fp) default_keys = [x for x in cp.defaults()] keys = {'DEFAULT': default_keys} for section in cp.sections(): keys[section] = [ o for o in cp.options(section) if o not in default_keys ] del cp return keys
def parse_config(self): """ Parses the config file """ cfg = ConfigParser() cfg.read('/etc/vcs-post-receive/post-receive.cfg') defaults = cfg.defaults() self.bugzilla_url = defaults['bugzilla_url'] self.rest_uri = defaults['rest_uri'] self.netrc = defaults['netrc'] self.vcsopts.proxy = defaults['proxy'] self.vcs_conf = {} for vcs in self.supported_vcs: self.__dict__[vcs] = {} self.__dict__[vcs]['vcsurl'] = cfg.get(vcs, 'vcsurl') self.__dict__[vcs]['rootdir'] = cfg.get(vcs, 'rootdir')
def configurator(global_conf, **local_conf): config.here = global_conf.get('here') config.ini_file = global_conf.get('__file__') config.zope_conf = global_conf.get('zope_conf') configparser = ConfigParser() configparser.read(config.ini_file) # building 2D dicts parsed = dict() for key, value in local_conf.iteritems(): if configparser.has_section(value): parsed[key] = dict([(o, configparser.get(value, o),) for o in configparser.options(value) if not o in configparser.defaults().keys()]) config.local_configuration = local_conf config.local_configuration.update(parsed)
def load_environment(global_conf, app_conf): """Configure the Pylons environment via the ``pylons.config`` object """ config = PylonsConfig() # Pylons paths root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=[os.path.join(root, 'templates')]) # import our private.ini that holds keys, etc imp = global_conf.get('import') if imp: cp = ConfigParser() cp.read(imp) global_conf.update(cp.defaults()) if cp.has_section('APP'): app_conf.update(cp.items('APP')) # Initialize config with the basic options config.init_app(global_conf, app_conf, package='linkdrop', paths=paths) config['routes.map'] = make_map(config) config['pylons.app_globals'] = app_globals.Globals(config) import linkdrop.lib.helpers as h config['pylons.h'] = h # Setup cache object as early as possible import pylons pylons.cache._push_object(config['pylons.app_globals'].cache) # Create the Mako TemplateLookup, with the default auto-escaping config['pylons.app_globals'].mako_lookup = TemplateLookup( directories=paths['templates'], error_handler=handle_mako_error, module_directory=os.path.join(app_conf['cache_dir'], 'templates'), input_encoding='utf-8', default_filters=['escape'], imports=['from webhelpers.html import escape']) # CONFIGURATION OPTIONS HERE (note: all config options will override # any Pylons config options) return config
def test_export_site_with_exportable_simple_items(self): from Products.GenericSetup.utils import _getDottedName self._setUpAdapters() ITEM_IDS = ('foo', 'bar', 'baz') site = _makeFolder('site') site.title = 'AAA' site._setProperty('description', 'CCC') aware = _makeINIAware('aside') dotted = _getDottedName(aware.__class__) for id in ITEM_IDS: site._setObject(id, _makeINIAware(id)) context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 2 + len(ITEM_IDS)) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 3) for index in range(len(ITEM_IDS)): self.assertEqual(objects[index][0], ITEM_IDS[index]) self.assertEqual(objects[index][1], dotted) filename, text, content_type = context._wrote[index+2] self.assertEqual(filename, 'structure/%s.ini' % ITEM_IDS[index]) object = site._getOb(ITEM_IDS[index]) self.assertEqual(text.strip(), object.as_ini().strip()) self.assertEqual(content_type, 'text/plain') filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) defaults = parser.defaults() self.assertEqual(len(defaults), 2) self.assertEqual(defaults['title'], 'AAA') self.assertEqual(defaults['description'], 'CCC')
def test_export_site_with_exportable_simple_items(self): from Products.GenericSetup.utils import _getDottedName self._setUpAdapters() ITEM_IDS = ('foo', 'bar', 'baz') site = _makeFolder('site') site.title = 'AAA' site._setProperty('description', 'BBB') aware = _makeINIAware('aside') dotted = _getDottedName(aware.__class__) for id in ITEM_IDS: site._setObject(id, _makeINIAware(id)) context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 2 + len(ITEM_IDS)) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 3) for index in range(len(ITEM_IDS)): self.assertEqual(objects[index][0], ITEM_IDS[index]) self.assertEqual(objects[index][1], dotted) filename, text, content_type = context._wrote[index+2] self.assertEqual(filename, 'structure/%s.ini' % ITEM_IDS[index]) object = site._getOb(ITEM_IDS[index]) self.assertEqual(text.strip(), object.as_ini().strip()) self.assertEqual(content_type, 'text/plain') filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) defaults = parser.defaults() self.assertEqual(len(defaults), 2) self.assertEqual(defaults['title'], 'AAA') self.assertEqual(defaults['description'], 'BBB')
def parse(parser, args = None): parser.add_option("-o", "--output", action = "store", help="write output to file") parser.add_option("-i", "--ini", action = "store", help="load settings from INI file") parser.add_option("-j", "--json", action = "store", help="load settings from JSON file") parser.add_option("-y", "--yaml", action = "store", help="load settings from YAML file") (options, args) = parser.parse_args(args) d = {} if options.ini: from ConfigParser import ConfigParser conf = ConfigParser() if options.ini == '-': cfg = sys.stdin else: cfg = file(options.ini) conf.readfp(cfg) d.update(conf.defaults()) for sect in conf.sections(): d[sect] = dict(conf.items(sect)) if options.json: import json if options.json == '-': cfg = sys.stdin else: cfg = file(options.json) d.update(json.load(cfg)) if options.yaml: import yaml if options.yaml == '-': cfg = sys.stdin else: cfg = file(options.yaml) d.update(yaml.load(cfg)) for arg in args: key, val = [s.strip() for s in arg.split('=', 1)] d[key] = val options.data = d return options
def from_file(file_name): conf = ConfigParser(defaults=DEFAULT_CONF) try: conf.read(file_name) except Error: stderr.write("Error reading config file. Please ensure that the file exists, has correct permissions," "and is a valid python config file") stderr.flush() raise conf_dict = conf.defaults() typed_config_list = [] # The ugly try-catch is required due to a quirk in literal_eval of plain strings for k,v in conf_dict.items(): try: typed_val = literal_eval(v) except ValueError: typed_val = "'" + v + "'" typed_config_list.append((k,typed_val)) merged_dict = OrderedDict(dict(DEFAULT_CONF.items() + typed_config_list)) return namedtuple_from_dict(merged_dict)
def test_as_ini_other_properties(self): from DateTime.DateTime import DateTime INTPROP = 42 FLOATPROP = 3.1415926 DATESTR = '2005-11-07T12:00:00.000Z' context = _makePropertied('string_property') context._properties = () context._setProperty('int_prop', INTPROP, 'int') context._setProperty('float_prop', FLOATPROP, 'float') context._setProperty('date_prop', DateTime(DATESTR), 'date') adapter = self._getTargetClass()(context) text = adapter.as_ini() parser = ConfigParser() parser.readfp(StringIO(text)) self.failIf(parser.sections()) default_options = parser.defaults() self.assertEqual(len(default_options), 3) self.assertEqual(default_options['int_prop'], str(INTPROP)) self.assertEqual(default_options['float_prop'], str(FLOATPROP)) self.assertEqual(default_options['date_prop'], str(DateTime(DATESTR)))
def init(): from pybot.module import Modules, ModuleMethods, RemoteMethods from pybot.option import Options from pybot.server import Servers from pybot.hook import Hooks from pybot.main import Main from pybot.sqlitedb import SQLiteDB from ConfigParser import ConfigParser import os global main, modls, servers, options, hooks, mm, rm, config, db hooks = Hooks() servers = Servers() options = Options() modls = Modules() mm = ModuleMethods() rm = RemoteMethods() config = ConfigParser() defaults = config.defaults() if os.path.isfile("./pybot.conf") and os.path.isdir("pybot"): config.read("./pybot.conf") defaults["datadir"] = os.path.abspath("./data") elif os.path.isfile(os.path.expanduser("~/.pybot/config")): config.read(os.path.expanduser("~/.pybot/config")) defaults["datadir"] = os.path.expanduser("~/.pybot/data") elif os.path.isfile("/etc/pybot.conf"): config.read("/etc/pybot.conf") defaults["datadir"] = ("/var/lib/pybot") db = SQLiteDB(config.get("sqlite", "path")) if config.has_option("global", "http_proxy"): os.environ["http_proxy"] = config.get("global", "http_proxy") main = Main()
def makeone(self, xtra=None, index_name='test-func-index', count=None): from cheeseprism.wsgiapp import main cp = ConfigParser(dict(here=self.base)) with open(resource_spec(self.devini)) as fp: cp.readfp(fp) defaults = dict((x, cp.get('DEFAULT', x)) for x in cp.defaults()) count = count is None and self.count or count self.idxpath = index_path = self.base / ("%s-%s" % (count, index_name)) settings = { 'cheeseprism.file_root': index_path, 'cheeseprism.data_json': 'data.json' } settings = xtra and dict(settings, **xtra) or settings app = main(defaults, **settings) self.executor = app.registry['cp.executor'] from webtest import TestApp return TestApp(app)
def makeone(self, xtra=None, index_name='test-func-index', count=None): from cheeseprism.wsgiapp import main cp = ConfigParser(dict(here=self.base)) with open(resource_spec(self.devini)) as fp: cp.readfp(fp) defaults = dict((x, cp.get('DEFAULT', x)) for x in cp.defaults()) count = count is None and self.count or count self.idxpath = index_path = self.base / ("%s-%s" %(count, index_name)) settings = { 'cheeseprism.file_root': index_path, 'cheeseprism.data_json': 'data.json' } settings = xtra and dict(settings, **xtra) or settings app = main(defaults, **settings) self.executor = app.registry['cp.executor'] from webtest import TestApp return TestApp(app)
class ConfigDict: """Singleton style/static initialisation wrapper thing""" def __init__(self): self.configdict = ConfigParser() foundini = False paths = (os.path.expanduser('~/.uploadr.ini'), os.path.abspath('uploadr.ini')) for filename in paths: if os.path.exists(filename): print 'using uploadr.ini file "%s"' % os.path.abspath(filename) self.configdict.read(filename) foundini = True break if not foundini: raise IOError('Missing configuration file: ' + ' '.join(paths)) def get(self, configparam, default=None): """get the value from the ini file's default section.""" defaults = self.configdict.defaults() if configparam in defaults: return defaults[configparam] if default: return default raise KeyError(configparam)
class ConfigDict: """Singleton style/static initialisation wrapper thing""" def __init__(self): self.configdict = ConfigParser() foundini = False paths = (os.path.abspath('uploadr.ini'), os.path.expanduser('~/.uploadr.ini')) for filename in paths: if os.path.exists(filename): print 'using uploadr.ini file "%s"' % os.path.abspath(filename) self.configdict.read(filename) foundini = True break if not foundini: raise IOError('Missing configuration file: ' + ' '.join(paths)) def get(self, configparam, default=None): """get the value from the ini file's default section.""" defaults = self.configdict.defaults() if configparam in defaults: return defaults[configparam] if default: return default raise KeyError(configparam)
def parse(): cfg = ConfigParser() cfg.readfp(FakeSecHead(sys.stdin)) return cfg.defaults()
class Config(object): section_handlers = {} @classmethod def register_handler(cls, name, handler, force=False): if not force and cls.section_handlers.get(name): # if it happens, don't avoid it but be verbose unless force is True print 'Warning: duplicate registration of ConfigSection %s' % name raise ValueError cls.section_handlers[name] = handler @classmethod def get_handler(cls, name): return cls.section_handlers.get(name, ConfigSection) def __init__(self, inifile): self._inifile = inifile self._parser = None self._sections = {} self._dirty = False self.check_mode() def check_mode(self): try: mode = os.stat(self._inifile)[0] & 0777 except OSError: # file does not exist, this is ok return False if mode & 0177: print( "File mode %o for %s is not enough restrictive and is a " "security threat." % (mode, self._inifile)) print "Please chmod it to 600." sys.exit(1) return True def __call__(self, section=None): if not self._parser: from ConfigParser import SafeConfigParser as ConfigParser self._parser = ConfigParser() self._parser.read(self._inifile) if not section: return self else: return self[section] def __getitem__(self, section): if not self._sections.has_key(section): self._sections[section] = self.get_handler(section)(self, section) return self._sections[section] def __setitem__(self, section, options): if not self._parser.has_section(section): self._parser.add_section(section) self._sections[section] = self.get_handler(section)(self, section) sect = self._sections[section] for option in sect.keys(): sect.pop(option) for option, value in options: sect[option] = value self.touch() def __delitem__(self, section): self.touch() return self._parser.remove_section(section) def pop(self, section): self.touch() return self._parser.remove_section(section) def keys(self): return self._parser.sections() def has_key(self, section): return self._parser.has_section(section) def defaults(self): return self._parser.defaults() def touch(self): self._dirty = True def write(self, inifile=None): if not self._dirty: return if inifile is None: inifile = self._inifile try: ini = open(inifile, 'w') except IOError: if os.environ.get('SSHPROXY_WIZARD', None): return print "Could not write configuration file: %s" % inifile print "Make sure %s is writable" % inifile print "If this is the first time you're running the program, try" print "the following command:" print 'sshproxy-setup' sys.exit(1) try: #print 'writing', inifile return self._parser.write(ini) finally: self._dirty = False ini.close() os.chmod(self._inifile, 0600) def __str__(self): fp = StringIO() self._parser.write(fp) fp.seek(0L) return fp.read()
def main(conffile, logs, legends, outdir, verbose): """ Produce plots from given """ dataset = [d for f, d in logs] config = ConfigParser() config.read(conffile) for s in config.sections(): p = Plot(name=s, outdir=outdir) # defaults for d, v in config.defaults().items(): if d != 'encoding': p.__dict__[d] = v else: encoding = v # stats # conf: 200.count, 400.count # result: [["200", "count"], ["400", "count"]] try: stats = [ x.strip().rsplit('.', 1) for x in config.get(s, 'stats').split(' ') ] except: print 'error: unable to read plot "%s" stats' % s continue if config.has_option(s, 'styles'): p.styles = [x.strip() for x in config.get(s, 'styles').split(' ')] if config.has_option(s, 'legend'): # this is the legend prefix$ l = [] count = 0 clegends = config.get(s, 'legend').decode(encoding).split(',') for f in logs: l += ['%s %s' % (x.strip(), legends[count]) \ for x in clegends] count += 1 p.legends = l if config.has_option(s, 'position'): # legend position according to matplotlib standard values (1-10) val = config.get(s, 'position').decode(encoding) if val.isdigit(): p.position = int(val) else: p.position = val if config.has_option(s, 'yfactor'): try: p.__dict__['yfactor'] = map( float, config.get(s, 'yfactor').decode(encoding).split(',')) except ValueError: print 'warning: %s yfactor not a number: %s' \ % (p.name, config.get(s, yfactor)) # Text parameters - to decode into specified encoding for attr in ['title', 'xlabel', 'ylabel', 'plottype', 'yscale']: if config.has_option(s, attr): cstring = config.get(s, attr).decode(encoding) p.__dict__[attr] = cstring # Numerical parameters for attr in ['xfactor', 'dpi', 'tn_dpi']: if config.has_option(s, attr): try: p.__dict__[attr] = config.getfloat(s, attr) except ValueError: print 'warning: %s %s not a number: %s' \ % (p.name, attr, config.get(s, attr)) outfile = p.plot(stats, dataset) if verbose: print 'Generated plot %s' % outfile
class Config(object): """ Config from auth_login.conf file """ ## init function def __init__(self, object="auth_login"): self.__sections = [] self.__options = {} self.__servers = [] self._object = object self._get_config() ## Set ~/.ssh/auth_login configure file def _get_config(self): self._default_config_files = find_config_files(self._object) self._args = self._setup_args_ssh(self._object) if not self._default_config_files: self._default_config_files = self._args.config_file if not self._default_config_files: raise Autho_LoginException("Not Found configure file") self._file = self._default_config_files self._cf = ConfigParser() self._cf.read(self._file) self._default_config_files = " | ".join(self._file) ## _setup ssh conn function def _setup_args_ssh(self, project): """Initialize a ConfigOpts object for option parsing.""" prog = usage = project version = "verbose" self._project = project self._prog = prog self._version = version self._usage = usage self._oparser = argparse.ArgumentParser( prog=prog, usage=usage, description='%(prog)s - SSH Server Daemon') self._oparser.add_argument( '-v', '--version', action='version', version=version, help='Print more verbose output (set logging level to ' 'INFO instead of default WARNING level).') self._oparser.add_argument( '-D', '--debug', action='store_false', default=False, help='Print debugging output (set logging level to ' 'DEBUG instead of default WARNING level).'), self._oparser.add_argument( '--config-file', nargs='?', help='Path to a config file to use. Multiple config ' 'files can be specified, with values in later ' 'files taking precedence. The default files ' ' used are: %s' % (self._default_config_files, )) self._setup_args_wsgi() self._args = self._oparser.parse_args() return self._args ## set wsgi server startup info def _setup_args_wsgi(self): self._oparser.add_argument( '--wsgi-port', '-P', type=int, default=5000, help='Wsgi Server Port to bind (default: 5000)') self._oparser.add_argument( '--wsgi-host', '-H', default='0.0.0.0', help='Wsgi Server Host to listen to (default: 0.0.0.0)') self._oparser.add_argument('--allow-agent', '-A', action='store_true', default=False, help='Wsgi Server Configure Allow the use of the local (where wsshd is running) ' \ 'ssh-agent to authenticate. Dangerous.') #self._args = self._oparser.parse_args() #return self._args ## set wsgi client startup info def _setup_args_client(self): parser = argparse.ArgumentParser( description='Auth_Login - SSH Over WebSockets Client') parser.add_argument('--host', '-H', help='Auth_Login server host (default: 127.0.0.1)', default='127.0.0.1') parser.add_argument('--port', '-P', help='Auth_Login server port (default: 5000)', type=int, default=5000) parser.add_argument('--password', '-p', nargs='?', const='', help='Password-based authentication. ' \ 'If no password is provided you will be prompted for one') parser.add_argument('--key', '-k', nargs='?', const='', help='Private key authentication. ' \ 'Selects a file from which the private key ' \ 'for RSA or DSA authentication is read. ' \ 'The default is ~/.ssh/id_rsa and ~/.ssh/id_dsa.') parser.add_argument( '--key-passphrase', '-K', nargs='?', const='', help='Provide a passphrase for encrypted private key files.') parser.add_argument('--ssh-port', '-s', type=int, default=22, help='Ssh to server host"s port') parser.add_argument('destination', help='[user@]hostname') ## call function def __call__(self, args=None, project=None, prog=None, version=None, usage=None, default_config_files=None): """ Parse command line arguments and config files. """ if default_config_files is None: default_config_files = find_config_files(project=project, prog=prog, extension='.conf') if len(default_config_files): raise Autho_LoginException("Not Found %s file" % (self._default_config_files)) self._file = file self._cf = ConfigParser() self._cf.read(self._file) ## Get all ssh server name return list def get_all_vmnames(self): if self._cf.has_section('ssh_server'): if self._cf.has_option('ssh_server', 'ssh_server_alias'): servernames = self._cf.get('ssh_server', 'ssh_server_alias') else: raise Autho_LoginException( "Not Found %s in ssh_server_alias option file" % (self._default_config_files)) else: raise Autho_LoginException( "Not Found %s in ssh_server section file" % (self._default_config_files)) servernamelist = re.split("\,|\#|\?|\|", servernames) self.__servers = servernamelist return self.__servers ## Get all sections return list def get_all_sections(self): self.__sections = self._cf.sections() return self.__sections ## Get all options return dict def get_all_options(self): for sec in self._cf.sections(): self.__options[sec] = self._cf.options(sec) return self.__options ## Get spec one section options return dict def get_one_options(self, section=None): if section is not None: return self._cf.items(section) elif section is None or section not in self.get_all_sections(): raise Autho_LoginException("Not Found %s have %s section" % (self._default_config_files, section)) ## Get spec vmanme values return dict def get_vmname_values(self, vmname=None): vmdict = {} vmtemp = "" if vmname is None: raise Autho_LoginException( "You must speci vmname value (vmname is not None)") if vmname not in self._cf.sections(): #raise Autho_LoginException("Not Found %s sections ~/.ssh/auth_login file " % vmname) print "Waring Not Found %s sections %s file " % ( vmname, self._default_config_files) vmtemp = "DEFAULT" else: vmtemp = vmname vmtmpdict = {} for vs in self._cf.items(vmtemp): vmtmpdict[vs[0]] = vs[1] vmdict[vmname] = vmtmpdict return vmdict ## Get all vmanme values return dict def get_all_vmname_values(self): self.__servers = self.get_all_vmnames() vmsdict = {} for server in self.__servers: vmsdict[server] = self.get_vmname_values(server) return vmsdict ## Get configure default section return dict def get_default_items(self): self_defaults = self._cf.defaults() return self_defaults ## Get ssh server section and option def get_one_section(self, section=None, option=None): if not self._cf.has_section(section) or section is None: raise Autho_LoginException("Not Found %s in %s section file" % (self._default_config_files, section)) elif option is None or not self._cf.has_option(section, option): raise Autho_LoginException( "Not Found %s %s option in %s section" % (self._default_config_files, option, section)) sectiondict = {} optiondict = {} value = self._cf.get(section, option) value = re.split("\,|\#|\?|\|", value) optiondict[option] = value sectiondict[section] = optiondict[option] return sectiondict[section] def get_wsgi_args(self): args = {} for value in self.get_one_options('wsgi_server'): args[value[0]] = value[1] if "--debug" in sys.argv or "-D" in sys.argv: args['debug'] = self._args.debug if "--allow-agent" in sys.argv or "-A" in sys.argv: args['allow_agent'] = self._args.allow_agent if "--wsgi-port" in sys.argv or "-P" in sys.argv: args['wsgi_port'] = self._args.wsgi_port if "--wsgi-host" in sys.argv or "-H" in sys.argv: args['wsgi_host'] = self._args.wsgi_host return args #return self._setup_args_wsgi() def get_client_args(self): args = {} return args
def read_file(filename): config = ConfigParser() config.read(filename) return config.defaults()
import sys from lxml import etree from ConfigParser import ConfigParser """ Parse the xml template and replace the values with the values from the configuration file passed. Usage: py modifybeakerjobxml.py <XML_TEMPLATE_PATH> <CONFIG_FILE_PATH>""" doc = etree.parse(str(sys.argv[1])) parser = ConfigParser() parser.read(str(sys.argv[2])) props=parser.defaults() repos=props['repos'] if repos is not None: repos = str(repos).split() repos_element=doc.getroot().find('recipeSet/recipe/repos') i = 1 for repo in repos: new_child = etree.SubElement(repos_element, 'repo', name='repo'+str(i), url=repo) i = i + 1 element = None distro_family = props['distro_family'] if distro_family is not None: element=doc.find('recipeSet/recipe/distroRequires/and/distro_family')
def main(conffile, logs, legends, outdir, verbose): """ Produce plots from given """ dataset = [d for f, d in logs] config = ConfigParser() config.read(conffile) for s in config.sections(): p = Plot(name = s, outdir = outdir) # defaults for d, v in config.defaults().items(): if d != 'encoding': p.__dict__[d] = v else: encoding = v # stats # conf: 200.count, 400.count # result: [["200", "count"], ["400", "count"]] try: stats = [x.strip().rsplit('.', 1) for x in config.get(s, 'stats').split(' ')] except: print 'error: unable to read plot "%s" stats' % s continue if config.has_option(s, 'styles'): p.styles = [x.strip() for x in config.get(s, 'styles').split(' ')] if config.has_option(s, 'legend'): # this is the legend prefix$ l = [] count = 0 clegends = config.get(s, 'legend').decode(encoding).split(',') for f in logs: l += ['%s %s' % (x.strip(), legends[count]) \ for x in clegends] count += 1 p.legends = l if config.has_option(s, 'position'): # legend position according to matplotlib standard values (1-10) val = config.get(s, 'position').decode(encoding) if val.isdigit(): p.position = int(val) else: p.position = val if config.has_option(s, 'yfactor'): try: p.__dict__['yfactor'] = map(float,config.get(s, 'yfactor').decode(encoding).split(',')) except ValueError: print 'warning: %s yfactor not a number: %s' \ % (p.name, config.get(s, yfactor)) # Text parameters - to decode into specified encoding for attr in ['title', 'xlabel', 'ylabel', 'plottype', 'yscale']: if config.has_option(s, attr): cstring = config.get(s, attr).decode(encoding) p.__dict__[attr] = cstring # Numerical parameters for attr in ['xfactor', 'dpi', 'tn_dpi']: if config.has_option(s, attr): try: p.__dict__[attr] = config.getfloat(s, attr) except ValueError: print 'warning: %s %s not a number: %s' \ % (p.name, attr, config.get(s, attr)) outfile = p.plot(stats, dataset) if verbose: print 'Generated plot %s' % outfile
class BAGConfig: # Singleton: sole static instance of Log to have a single Log object config = None def __init__(self, args, home_path=None): # See if home dir is passed if home_path: self.bagextract_home = home_path else: # Derive home dir from script location self.bagextract_home = os.path.abspath( os.path.join(os.path.realpath(os.path.dirname(sys.argv[0])), os.path.pardir)) # Default config file self.config_file = os.path.realpath(self.bagextract_home + '/extract.conf') # Option: overrule config file with command line arg pointing to config file if args and args.config: self.config_file = args.config Log.log.debug("Configuratiebestand is " + str(self.config_file)) if not os.path.exists(self.config_file): Log.log.fatal("kan het configuratiebestand '" + str(self.config_file) + "' niet vinden") self.configdict = ConfigParser() try: self.configdict.read(self.config_file) except Exception: Log.log.fatal("" + str(self.config_file) + " kan niet worden ingelezen") try: # Zet parameters uit config bestand self.database = self.configdict.defaults()['database'] self.schema = self.configdict.defaults()['schema'] self.host = self.configdict.defaults()['host'] self.user = self.configdict.defaults()['user'] self.password = self.configdict.defaults()['password'] # Optional port config with default self.port = 5432 if self.configdict.has_option(None, 'port'): self.port = self.configdict.defaults()['port'] except Exception: Log.log.fatal("Configuratiebestand " + str(self.config_file) + " is niet volledig") # Assign Singleton (of heeft Python daar namespaces voor?) (Java achtergrond) BAGConfig.config = self if not args: return try: # Optioneel: overrulen met (commandline) args if args.database: self.database = args.database if args.host: self.host = args.host if args.schema: self.schema = args.schema # default to public schema if not self.schema: self.schema = 'public' if args.username: self.user = args.username if args.port: self.port = args.port if args.no_password: # Gebruik geen wachtwoord voor de database verbinding self.password = None else: if args.password: self.password = args.password except Exception: Log.log.fatal(" het overrulen van configuratiebestand " + str(self.config_file) + " via commandline loopt spaak") def save(self): section = self.configdict.defaults() section['database'] = self.database section['schema'] = self.schema section['host'] = self.host section['user'] = self.user section['password'] = self.password section['port'] = self.port with open(self.config_file, 'w') as configfile: # save self.configdict.write(configfile)
import os import errno from ConfigParser import ConfigParser from swift.common.ring import ring from swift.common.utils import search_tree from gluster.swift.common.Glusterfs import SWIFT_DIR reseller_prefix = "AUTH_" conf_files = search_tree(SWIFT_DIR, "proxy-server*", 'conf') if conf_files: conf_file = conf_files[0] _conf = ConfigParser() if conf_files and _conf.read(conf_file): if _conf.defaults().get("reseller_prefix", None): reseller_prefix = _conf.defaults().get("reseller_prefix") else: for key, value in _conf._sections.items(): if value.get("reseller_prefix", None): reseller_prefix = value["reseller_prefix"] break if not reseller_prefix.endswith('_'): reseller_prefix = reseller_prefix + '_' class Ring(ring.Ring): def __init__(self, serialized_path, reload_time=15, ring_name=None): self.false_node = {'zone': 1, 'weight': 100.0, 'ip': '127.0.0.1',
return configuration try: if config == None or isfile(config) == False: configuration = defaults() return configuration except TypeError: if type(config) is dict: configuration = defaults(config) else: try: parser = ConfigParser() parser.read(config) file_options = parser.defaults() # Add support for custom configuration for key, value in file_options.items(): if 'waskr.custom' in key: try: underscored = '_'.join(key.split('.')[2:]) if underscored: opt_mapper[key] = underscored except IndexError: pass # badly configured # we are not sure about the section so we # read the whole thing and loop through the items for key, value in opt_mapper.items(): try: file_value = file_options[key]
class Config(object): """ Reading of config file attributes. Any instance holds state information about Azure account, region, storage and container references """ PLATFORM = sys.platform[:3] def __init__(self, account_name=None, region_name=None, storage_account_name=None, storage_container_name=None, filename=None, platform=PLATFORM): from ..logger import log self.storage_container_name = storage_container_name self.storage_account_name = storage_account_name self.config_file = self.__lookup_config_file(platform, account_name, filename) self.config = ConfigParser() try: log.debug('Using configuration from %s', self.config_file) self.config.read(self.config_file) except Exception as e: raise AzureConfigParseError( 'Could not parse config file: "%s"\n%s' % (self.config_file, e.message)) if not self.config.defaults(): raise AzureAccountDefaultSectionNotFound( 'Empty or undefined default section in configuration file %s' % self.config_file) self.account_name = self.__import_default_account() self.selected_region_name = region_name self.region_name = None def get_storage_account_name(self): storage_account_name = self.storage_account_name if not storage_account_name: storage_account_name = self.__get_region_option( 'default_storage_account') return storage_account_name def get_storage_container_name(self): storage_container_name = self.storage_container_name if not storage_container_name: storage_container_name = self.__get_region_option( 'default_storage_container') return storage_container_name def get_subscription_id(self): return self.__get_account_option('subscription_id') def get_publishsettings_file_name(self): return self.__get_account_option('publishsettings') def get_management_url(self): return self.__get_account_option('management_url') def get_management_pem_filename(self): return self.__get_account_option('management_pem_file') def get_region_name(self): if not self.region_name: try: self.region_name = self.__import_default_region( self.selected_region_name).replace('region:', '') except AzureConfigSectionNotFound: self.region_name = self.selected_region_name return self.region_name def get_account_name(self): return self.account_name.replace('account:', '') @classmethod def get_config_file(self, account_name=None, filename=None, platform=None): paths = ConfigFilePath(account_name, platform) if filename: return filename elif account_name: return paths.default_new_account_config() else: return paths.default_config() @classmethod def get_config_file_list(self): paths = ConfigFilePath() return [paths.default_config()] + paths.account_config() @classmethod def set_default_config_file(self, account_name, platform=None): paths = ConfigFilePath(account_name, platform) account_config_file = paths.default_new_account_config() if not os.path.exists(account_config_file): raise AzureConfigAccountFileNotFound( 'Account config file %s not found' % account_config_file) default_config_file = paths.default_config() if not default_config_file: default_config_file = paths.default_new_config() default_exists = os.path.exists(default_config_file) default_islink = os.path.islink(default_config_file) if default_exists and not default_islink: message = dedent(''' Can not link %s as default account. A default account configuration file from a former azurectl version was found. Consider one of the following options to handle the config file: %s 1. Delete the configuration file if no longer needed 2. Move the configuration file with context information to ~/.config/azurectl/config.<context> ''').strip() raise AzureConfigDefaultLinkError( message % (account_config_file, default_config_file)) if default_exists: os.remove(default_config_file) os.symlink(account_config_file, default_config_file) def __check_for_section(self, section): if section and not self.config.has_section(section): raise AzureConfigSectionNotFound( 'Section %s not found in configuration file %s' % (section, self.config_file)) def __get_account_option(self, option): try: result = self.config.get(self.account_name, option) except Exception: raise AzureConfigVariableNotFound( '%s not defined for account %s in configuration file %s' % (option, self.account_name, self.config_file)) return result def __get_region_option(self, option): try: if not self.region_name: self.get_region_name() result = self.config.get('region:' + self.region_name, option) except Exception as e: message = '%s not found: %s' % (option, format(e)) raise AzureConfigVariableNotFound(message) return result def __lookup_config_file(self, platform, account_name, filename): paths = ConfigFilePath(account_name, platform) if filename: # lookup a custom config file if not os.path.isfile(filename): raise AzureAccountLoadFailed('Could not find config file: %s' % filename) elif account_name: # lookup an account config file filename = paths.default_new_account_config() if not os.path.isfile(filename): raise AzureAccountLoadFailed( 'Could not find account config file: %s %s: %s' % (paths.account_config_file, 'in home directory', paths.home_path)) else: # lookup default config file filename = paths.default_config() if not filename: raise AzureAccountLoadFailed( 'could not find default configuration file %s %s: %s' % (' or '.join(paths.config_files), 'in home directory', paths.home_path)) return filename def __import_default_region(self, region_name): defaults = self.config.defaults() if region_name: region_name = 'region:' + region_name else: try: region_name = defaults['default_region'] except Exception: raise AzureConfigRegionNotFound( 'No region referenced in configuration file %s' % self.config_file) self.__check_for_section(region_name) return region_name def __import_default_account(self): defaults = self.config.defaults() try: account_name = defaults['default_account'] except Exception: raise AzureConfigAccountNotFound( 'No account referenced in configuration file %s' % self.config_file) self.__check_for_section(account_name) return account_name
def __init__(self, args): # Derive home dir from script location self.bagextract_home = os.path.realpath( os.path.dirname(sys.argv[0]) + '/..') # Default config file config_file = os.path.realpath(self.bagextract_home + '/extract.conf') # Option: overrule config file with command line arg pointing to config file if args.config: config_file = args.config Log.log.info("Configuratiebestand is " + str(config_file)) if not os.path.exists(config_file): Log.log.fatal("ik kan het configuratiebestand '" + str(config_file) + "' ech niet vinden.") configdict = ConfigParser() try: configdict.read(config_file) except: Log.log.fatal("ik kan " + str(config_file) + " wel vinden maar niet inlezen.") try: # Zet parameters uit config bestand self.database = configdict.defaults()['database'] self.schema = configdict.defaults()['schema'] self.host = configdict.defaults()['host'] self.user = configdict.defaults()['user'] self.password = configdict.defaults()['password'] self.port = configdict.defaults()['port'] except: Log.log.fatal(" de inhoud van configuratiebestand " + str(config_file) + " is niet volledig.") try: # Optioneel: overrulen met (commandline) args if args.database: self.database = args.database if args.host: self.host = args.host if args.schema: self.schema = args.schema # default to public schema if not self.schema: self.schema = 'public' if args.username: self.user = args.username if args.port: self.port = args.port if args.no_password: # Gebruik geen wachtwoord voor de database verbinding self.password = None else: if args.password: self.password = args.password # Assign Singleton (of heeft Python daar namespaces voor?) (Java achtergrond) BAGConfig.config = self except: Log.log.fatal(" het overrulen van configuratiebestand " + str(config_file) + " via commandline loopt spaak")
def _from_ini_file(self, file_obj, user_string=None, region=None, all_conf=False): """ Parse the Cloud attributes from this file object expecting euca2ools .ini format. Use of a file object here means values can be parsed from a local file() obj as well as an ssh.sftp() remote file object. Populates self with attributes. :param user_string: 'account_id:username' string used to match config block/section :param region: string used to match region section within config this is the filepath, but when parsing from a string buffer filepath is unknown :param all_conf: boolean. If True returns a dictionary of the entire config. If false returns the values for a specific user and region as defined by the provided parameters or within the config files default settings. :returns dict of attributes. """ ret_dict = {} cf = ConfigParser() # Read the file into a ConfigParser obj... with file_obj: file_obj.seek(0) cf.readfp(file_obj) file_obj.seek(0) self.log.debug('read from file:\n{0}'.format(file_obj.read())) # Create the dictionary to sort the config parser values into, then sort it... cf_dict = {'users': {}, 'regions': {}, 'global': {}} for section in cf.sections(): self.log.debug('got section: {0}'.format(section)) sect_info = {} for opt in cf.options(section=section): self.log.debug('got opt: {0}'.format(opt)) sect_info[opt] = cf.get(section=section, option=opt) sect_type = section.split()[0] if sect_type == 'user': cf_dict['users'][section] = sect_info elif sect_type == 'region': cf_dict['regions'][section] = sect_info elif sect_type == 'global': cf_dict['global'] = sect_info else: cf_dict[section] = sect_info # Default values are values which were found outside any specific ConfigParser sections... for key, value in cf.defaults(): cf_dict[key] = value self.log.debug('got cf dict:{0}'.format(cf_dict)) if all_conf: return cf_dict # If present, apply the global config section values first... if cf_dict['global']: for key, value in cf_dict['global'].iteritems(): if key == 'default-region' and not region: region = value elif key == 'default-user' and not user_string: user_string = value else: ret_dict[key] = value # Next apply any applicable region config section values... def do_region(region, user_string): region = str(region).strip() ret_dict['region'] = region for regkey, reginfo in cf_dict['regions'].iteritems(): s_type, regkey = regkey.split() if regkey == region: for key, value in reginfo.iteritems(): if key == 'user' and not user_string: user_string = value else: ret_dict[key] = value break if region: do_region(region, user_string) # Finally apply any applicable user config section values... if user_string: for user, user_dict in cf_dict['users'].iteritems(): s_type, user_info = user.split() if str(user_info).strip().lower() == user_string: for key, value in user_dict.iteritems(): # If region wasn't provided & the user section specifies a region, use it if key == 'region' and not region: region = key do_region(region, user_string) ret_dict[key] = value break # Sanitize the values into a new dictionary to be returned... new_dict = {} for key, value in ret_dict.iteritems(): try: key = key.lower().replace('-', '_') if key.startswith("_"): self.log.warning( 'Illegal name value:"{0}", not setting attribute'. format(key)) else: self.__setattr__(key, value) new_dict[key] = value except Exception as E: self.log.error( '{0}\nFailed to set attr:{1} to value:{2}. Error:{3}'. format(get_traceback(), key, value, E)) return new_dict
import logging import logging.config import os from ConfigParser import ConfigParser DEBUG = True log_conf_path = os.path.dirname(os.path.abspath(__file__)) logging.config.fileConfig(log_conf_path + "/logging.cfg") logger = logging.getLogger("root") if DEBUG else logging.getLogger("Test") conf = ConfigParser() conf.read(log_conf_path + "/project.cfg") if __name__ == "__main__": print conf.get("DEFAULT", "test") print conf.defaults() print conf.sections()
def __init__(self, args): # Derive home dir from script location self.bagextract_home = os.path.dirname( os.path.dirname(os.path.realpath(__file__))) # Default config file config_file = os.path.realpath(self.bagextract_home + '/extract.conf') # Option: overrule config file with command line arg pointing to config file if args.config: config_file = args.config if not os.path.exists(config_file): self.logger.critical(str(config_file) + " niet gevonden") _configparser = ConfigParser() try: _configparser.read(config_file) except Exception: e = sys.exc_info()[1] self.logger.critical(str(config_file) + " \n\t" + str(e)) try: # Zet parameters uit config bestand self.soort = _configparser.defaults()['soort'] self.database = _configparser.defaults()['database'] self.schema = _configparser.defaults()['schema'] self.host = _configparser.defaults()['host'] self.user = _configparser.defaults()['user'] self.password = _configparser.defaults()['password'] self.port = _configparser.defaults()['port'] except Exception: e = sys.exc_info()[1] self.logger.critical(str(config_file) + " \n\t" + str(e)) try: # Optioneel: overrulen met (commandline) args #if args.soort: # self.soort = args.soort if args.database: self.database = args.database if args.host: self.host = args.host if args.schema: self.schema = args.schema # default to public schema if not self.schema: self.schema = 'public' if args.username: self.user = args.username if args.port: self.port = args.port if args.no_password: # Gebruik geen wachtwoord voor de database verbinding self.password = None else: if args.password: self.password = args.password # Assign Singleton (of heeft Python daar namespaces voor?) (Java achtergrond) BAGConfig.config = self except Exception: e = sys.exc_info()[1] self.logger.critical(" configuratiefout in " + str(config_file) + " " + str(e))