Ejemplo n.º 1
0
def add_new_SFUI(df_final):
    updater = ConfigUpdater()
    updater.read('setup.cfg')
    # Subset into assigned and unassigned
    df = df_final[df_final['SFUI'] == '']
    df_final = df_final[df_final['SFUI'] != '']
    if df.empty:
        return df_final
    else:
        # Sort by SF
        df = df.sort_values(by=['SF'])
        df = df.reset_index(drop=True)
        # Assign SFUI
        assignment = int(updater['metadata']['sfui_last_assignment'].value) + 1
        for index, row in df.iterrows():
            if index == 0:
                df['SFUI'].iat[index] = assignment
            elif df['SF'].at[index] == df['SF'].at[index - 1]:
                df['SFUI'].iat[index] = assignment
            else:
                assignment += 1
                df['SFUI'].iat[index] = assignment
        # Format SFUI
        df['SFUI'] = 'S' + (df.SFUI.map('{:06}'.format))
        # Add back newly assigned
        df_final = pd.concat([df_final, df])
        df_final = df_final.reset_index(drop=True)
        # Update config file
        updater['metadata']['sfui_last_assignment'].value = assignment
        updater.update_file()
        # Return dataframe
        return df_final
Ejemplo n.º 2
0
def set_load_config(database_path = None, cluster_cookie=None):
    """
    Help to set the mission data loading.

    Parameters
    ----------
    database_path : str
        Absolute path where to save all the downloaded files.
        It not, the default path is ~/heliopy/

    cluster_cookie : str
        Cookie from ESA to download cluster data.
    """
    #Get the path of heliopyrc file
    config_path = get_config_file()
    
    # Read the preexisting config file using configupdater
    config = ConfigUpdater()
    config.read(config_path)

    # Set new data download directory if passed
    if database_path:
        config['DEFAULT']['download_dir'].value = database_path

    # Set new cluster cookie if passed
    if cluster_cookie:
        config['DEFAULT']['cluster_cookie'].value = cluster_cookie

    # Update the config file with new entries
    config.update_file()
def test_items(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    sect_names, sects = zip(*updater.items())
    exp_sect_namess = [
        "metadata",
        "options",
        "options.packages.find",
        "options.extras_require",
        "test",
        "tool:pytest",
        "aliases",
        "bdist_wheel",
        "build_sphinx",
        "devpi:upload",
        "flake8",
        "pyscaffold",
    ]
    assert list(sect_names) == exp_sect_namess
    assert all([isinstance(s, Section) for s in sects])

    opt_names, opts = zip(*updater.items("devpi:upload"))
    exp_opt_names = ["no-vcs", "formats"]
    assert list(opt_names) == exp_opt_names
    assert all([isinstance(o, Option) for o in opts])
Ejemplo n.º 4
0
def test_update_no_changes(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    old_mtime = os.path.getmtime(setup_cfg_path)
    updater.update_file()
    new_mtime = os.path.getmtime(setup_cfg_path)
    assert old_mtime != new_mtime
Ejemplo n.º 5
0
def test_updater_to_dict(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    parser = ConfigParser()
    parser.read(setup_cfg_path)
    parser_dict = {sect: dict(parser[sect]) for sect in parser.sections()}
    assert updater.to_dict() == parser_dict
Ejemplo n.º 6
0
def test_validate_format(setup_cfg_path):
    updater = ConfigUpdater(allow_no_value=False)
    updater.read(setup_cfg_path)
    updater.validate_format()
    updater.set('metadata', 'author')
    with pytest.raises(ParsingError):
        updater.validate_format()
Ejemplo n.º 7
0
class SingleConfigFileRule(SinglePathRule):
    """
    Extend :class:`hammurabi.rules.base.Rule` to handle parsed content
    manipulations on a single file.
    """
    def __init__(
        self,
        name: str,
        path: Optional[Path] = None,
        section: Optional[str] = None,
        **kwargs,
    ) -> None:
        self.section = self.validate(section, required=True)
        self.updater = ConfigUpdater()

        super().__init__(name, path, **kwargs)

    def pre_task_hook(self) -> None:
        """
        Parse the configuration file for later use.
        """

        logging.debug('Parsing "%s" configuration file', self.param)
        self.updater.read(self.param)

    @abstractmethod
    def task(self) -> Any:
        """
Ejemplo n.º 8
0
def test_iter_section(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    # iter_blocks will give us everything, iter_sections just the section objects and
    # __iter__ should work as iter_blocks
    assert len([block for block in updater.iter_blocks()]) == 14
    assert len([block for block in updater.iter_sections()]) == 12
    assert len([entry for entry in updater]) == 12
Ejemplo n.º 9
0
def test_section_to_dict(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    parser = ConfigParser()
    parser.read(setup_cfg_path)
    updater_dict = updater['metadata'].to_dict()
    parser_dict = dict(parser['metadata'])
    assert updater_dict == parser_dict
Ejemplo n.º 10
0
def test_get_options(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    options = updater.options('options.packages.find')
    exp_options = ['where', 'exclude']
    assert options == exp_options
    with pytest.raises(NoSectionError):
        updater.options("non_existent_section")
Ejemplo n.º 11
0
def test_validate_format_propagates_kwargs(setup_cfg_path):
    updater = ConfigUpdater(allow_no_value=True)
    updater.read(setup_cfg_path)
    updater.set("pyscaffold", "namespace")
    assert updater["pyscaffold"]["namespace"].value is None
    assert str(
        updater["pyscaffold"]["namespace"]) == "namespace\n"  # no `=` sign
    assert updater.validate_format() is True
Ejemplo n.º 12
0
def test_iter_items_section(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)

    result = list(k for k, v in updater['metadata'].items())
    assert result == [
        'name', 'description', 'author', 'author-email', 'license', 'url',
        'long-description', 'platforms', 'classifiers'
    ]
Ejemplo n.º 13
0
def test_get_section(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    section = updater['metadata']
    assert section._structure
    with pytest.raises(KeyError):
        updater['non_existent_section']
    with pytest.raises(ValueError):
        updater._get_section_idx('non_existent_section')
Ejemplo n.º 14
0
def change_language(new_language: str) -> None:
    """
    Updates the language in the config file
    :param new_language: The new language code. Can be any of 'en', 'ro' and 'fr'
    """
    updater = ConfigUpdater()
    updater.read('settings.config')
    updater['APPLICATION_SETTINGS']['language'].value = new_language
    updater.update_file()
Ejemplo n.º 15
0
def test_iter_option(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    section = updater["metadata"]
    # iter_blocks will give us everything, iter_options just the option objects and
    # __iter__ should work as iter_blocks
    assert len([entry for entry in section.iter_blocks()]) == 12
    assert len([entry for entry in section.iter_options()]) == 9
    assert len([entry for entry in section]) == 9
Ejemplo n.º 16
0
def test_sections(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    exp_sections = [
        'metadata', 'options', 'options.packages.find',
        'options.extras_require', 'test', 'tool:pytest', 'aliases',
        'bdist_wheel', 'build_sphinx', 'devpi:upload', 'flake8', 'pyscaffold'
    ]
    assert updater.sections() == exp_sections
Ejemplo n.º 17
0
def test_get_method(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    value = updater.get('metadata', 'license').value
    assert value == 'mit'
    with pytest.raises(NoSectionError):
        updater.get('non_existent_section', 'license')
    with pytest.raises(NoOptionError):
        updater.get('metadata', 'wrong_key')
def test_get_method(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    value = updater.get("metadata", "license").value
    assert value == "mit"
    with pytest.raises(NoSectionError):
        updater.get("non_existent_section", "license")
    with pytest.raises(NoOptionError):
        updater.get("metadata", "wrong_key")
    assert updater.get("metadata", "wrong_key", fallback=None) is None
def test_eq(setup_cfg_path):
    updater1 = ConfigUpdater()
    updater1.read(setup_cfg_path)
    updater2 = ConfigUpdater()
    updater2.read(setup_cfg_path)
    assert updater1 == updater2
    updater1.remove_section("metadata")
    assert updater1 != updater2
    assert updater1 != updater2["metadata"]
    assert updater2["metadata"] != updater2["metadata"]["author"]
    assert not updater1.remove_section("metadata")
Ejemplo n.º 20
0
def test_eq(setup_cfg_path):
    updater1 = ConfigUpdater()
    updater1.read(setup_cfg_path)
    updater2 = ConfigUpdater()
    updater2.read(setup_cfg_path)
    assert updater1 == updater2
    updater1.remove_section('metadata')
    assert updater1 != updater2
    assert updater1 != updater2['metadata']
    assert updater2['metadata'] != updater2['metadata']['author']
    assert not updater1.remove_section('metadata')
Ejemplo n.º 21
0
def test_get_section(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    section = updater["metadata"]
    assert section._structure
    with pytest.raises(KeyError):
        updater["non_existent_section"]

    # `get_section` is equivalent to dict.get
    assert updater.get_section("non_existent_section") is None
    default = {}
    assert updater.get_section("non_existent_section", default) is default
Ejemplo n.º 22
0
def read_config(path: Optional[Path] = None) -> Box:
    """Return the config"""
    config = Box(box_it_up=True)
    if path:
        config_path_locations: Tuple[Path, ...] = (path,)
    else:
        config_path_locations = (
            Path(Path("/etc") / "awattprice" / "config.ini"),
            Path(os.path.expanduser("~")) / ".config" / "awattprice" / "config.ini",
        )
    found_config_file = False
    for path in config_path_locations:
        if path.exists():
            found_config_file = True
            break
    else:
        log.info(f"No config file found in {path.parent}. Creating one...")
        path = Path(os.path.expanduser("~")) / ".config" / "awattprice" / "config.ini"
        config_updater = bootstrap_config(path)
    if path.parent.exists() and not path.parent.is_dir():
        log.error(f"Expected the config directory {path.parent} to be a directory.")
        sys.exit(1)

    if not verify_file_permissions(path):
        log.error(f"Could not ensure secure file permissions for {path}. Fix them and try again.")
        sys.exit(1)

    if found_config_file:
        config_updater = ConfigUpdater()
        try:
            config_updater.read(path.as_posix())
        except Exception as e:
            log.error(f"Could not read the config from {path}: {e}")
            sys.exit(1)

    config = Box(config_updater.to_dict(), box_dots=True)
    config["config_file_path"] = path

    # Strip off quotes that made it into the config.ini file
    config.file_location.data_dir = config.file_location.data_dir.strip("\"'")
    config.file_location.log_dir = config.file_location.log_dir.strip("\"'")
    config.file_location.apns_dir = config.file_location.apns_dir.strip("\"'")

    config_to_bool(config)

    config.notifications.dev_team_id = config.notifications.dev_team_id.strip("\"'")
    config.notifications.apns_encryption_key_id = config.notifications.apns_encryption_key_id.strip("\"'")
    config.notifications.apns_encryption_key = config.notifications.apns_encryption_key.strip("\"'")

    return config
Ejemplo n.º 23
0
def test_get_options(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    options = updater.options("options.packages.find")
    exp_options = ["where", "exclude"]
    assert options == exp_options
    with pytest.raises(NoSectionError):
        updater.options("non_existent_section")

    # Section objects also have a "get" method similar to dict
    section = updater["metadata"]
    assert section.get("name").value == "configupdater"
    assert section.get("non_existent_option") is None
    assert section.get("non_existent_option", []) == []
def test_iter_items_section(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)

    result = list(k for k, v in updater["metadata"].items())
    assert result == [
        "name",
        "description",
        "author",
        "author-email",
        "license",
        "url",
        "long-description",
        "platforms",
        "classifiers",
    ]
Ejemplo n.º 25
0
def test_items(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    sect_names, sects = zip(*updater.items())
    exp_sect_namess = [
        'metadata', 'options', 'options.packages.find',
        'options.extras_require', 'test', 'tool:pytest', 'aliases',
        'bdist_wheel', 'build_sphinx', 'devpi:upload', 'flake8', 'pyscaffold'
    ]
    assert list(sect_names) == exp_sect_namess
    assert all([isinstance(s, Section) for s in sects])

    opt_names, opts = zip(*updater.items('devpi:upload'))
    exp_opt_names = ['no-vcs', 'formats']
    assert list(opt_names) == exp_opt_names
    assert all([isinstance(o, Option) for o in opts])
def test_sections(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    exp_sections = [
        "metadata",
        "options",
        "options.packages.find",
        "options.extras_require",
        "test",
        "tool:pytest",
        "aliases",
        "bdist_wheel",
        "build_sphinx",
        "devpi:upload",
        "flake8",
        "pyscaffold",
    ]
    assert updater.sections() == exp_sections
Ejemplo n.º 27
0
def read_setupcfg(path: PathLike, filename=SETUP_CFG) -> ConfigUpdater:
    """Reads-in a configuration file that follows a setup.cfg format.
    Useful for retrieving stored information (e.g. during updates)

    Args:
        path: path where to find the config file
        filename: if ``path`` is a directory, ``name`` will be considered a file
            relative to ``path`` to read (default: setup.cfg)

    Returns:
        Object that can be used to read/edit configuration parameters.
    """
    path = Path(path)
    if path.is_dir():
        path = path / (filename or SETUP_CFG)

    updater = ConfigUpdater()
    updater.read(path, encoding="utf-8")

    logger.report("read", path)

    return updater
Ejemplo n.º 28
0
def save(struct: "Structure", opts: "ScaffoldOpts") -> "ActionParams":
    """Save the given opts as preferences in a PyScaffold's config file."""
    config = ConfigUpdater()

    if not opts.get("save_config"):
        file = info.config_file()
    else:
        file = Path(opts["save_config"])

    if file.exists():
        config.read(file, encoding="utf-8")
    else:
        config.read_string(
            "# PyScaffold's configuration file, see:\n"
            "# https://pyscaffold.org/en/latest/configuration.html\n#\n"
            "# Accepted in `metadata`: author, author_email and license.\n"
            "# Accepted in `pyscaffold`: extensions (and associated opts).\n"
        )

    if "metadata" not in config:
        config.add_section("metadata")

    # We will add metadata just if they are not the default ones
    metadata = config["metadata"]
    defaults = [
        ("author", opts["author"], info.username()),
        ("author_email", opts["email"], info.email()),
        ("license", opts["license"], api.DEFAULT_OPTIONS["license"]),
    ]

    metadata.update({k: v for k, v, default in defaults if v != default})
    templates.add_pyscaffold(config, opts)

    operations.create(file, str(config), opts)  # operations provide logging and pretend

    return struct, opts
Ejemplo n.º 29
0
def test_iter_section(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    # we test ne number of blocks, not sections
    assert len([block for block in updater]) == 14
Ejemplo n.º 30
0
def test_len_updater(setup_cfg_path):
    updater = ConfigUpdater()
    updater.read(setup_cfg_path)
    # we test the number of blocks, not sections
    assert len(updater) == 14