def select_parser(filename, read=True): if filename.endswith(".ini"): options = config.ConfigParser() if read: options.read([filename]) options.filename = filename return options elif filename.endswith(".pkl") or filename.endswith(".p"): options = PickleParser() if read: with open(filename, "r") as fp: options._buffer = options.load(fp) options.filename = filename return options elif filename.endswith(".json"): options = JSONParser() if read: with open(filename, "r") as fp: options._buffer = options.load(fp) options.filename = filename return options else: raise NotImplementedError("Config file type not supported!")
def read(cls, config_file): #Read the options from the ini file options = config.ConfigParser() options.read([config_file]) #Check that the config file has the appropriate section section = "PowerSpectrumSettings" assert options.has_section( section), "No {0} section in configuration file {1}".format( section, config_file) #Fill in the appropriate fields settings = cls() settings.ensemble_name = options.get(section, "ensemble_name") #Read in the nbody realizations that make up the ensemble settings.nbody_realizations = list() for r in options.get(section, "nbody_realizations").split(","): try: l, h = r.split("-") settings.nbody_realizations.extend(range(int(l), int(h) + 1)) except ValueError: settings.nbody_realizations.extend([int(r)]) settings.first_snapshot = options.getint(section, "first_snapshot") settings.last_snapshot = options.getint(section, "last_snapshot") settings.fft_grid_size = options.getint(section, "fft_grid_size") settings.length_unit = getattr(u, options.get(section, "length_unit")) settings.kmin = options.getfloat(section, "kmin") * settings.length_unit**-1 settings.kmax = options.getfloat(section, "kmax") * settings.length_unit**-1 settings.num_k_bins = options.getint(section, "num_k_bins") #Return to user return settings
#A dirty hack to get around some early import/configurations ambiguities if sys.version_info[0] >= 3: import builtins else: import __builtin__ as builtins builtins._ASTROPY_SETUP_ = True from astropy_helpers.setup_helpers import (register_commands, adjust_compiler, get_debug_option, get_package_info) from astropy_helpers.git_helpers import get_git_devstr from astropy_helpers.version_helpers import generate_version_py # Get some values from the setup.cfg from distutils import config conf = config.ConfigParser() conf.read(['setup.cfg']) metadata = dict(conf.items('metadata')) PACKAGENAME = metadata.get('package_name', 'packagename') DESCRIPTION = metadata.get('description', 'Astropy affiliated package') AUTHOR = metadata.get('author', '') AUTHOR_EMAIL = metadata.get('author_email', '') LICENSE = metadata.get('license', 'unknown') URL = metadata.get('url', 'http://astropy.org') # Get the long description from the package's docstring __import__(PACKAGENAME) package = sys.modules[PACKAGENAME] LONG_DESCRIPTION = package.__doc__