~michel-slm/myrepos-utils

ref: 3d77db259ae41895f62f2cbfe1fb49fa85606b65 myrepos-utils/myrepos_utils/utils.py -rw-r--r-- 1.2 KiB
3d77db25Michel Alexandre Salim Add sort command 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# (c) Michel Alexandre Salim
#
# Fedora-License-Identifier: GPLv2+
# SPDX-2.0-License-Identifier: GPL-2.0+
# SPDX-3.0-License-Identifier: GPL-2.0-or-later
#
# This program is free software.
# For more information on the license, see COPYING.md.
# For more information on free software, see
# <https://www.gnu.org/philosophy/free-sw.en.html>.

import configparser
import os

CONFIG = configparser.ConfigParser()
CONFIG.read(os.path.expanduser("~/.mrconfig"))


def get_repos(config: configparser.ConfigParser = CONFIG) -> list[str]:
    return list(config.sections())


def find_repo(
    query: list[str], config: configparser.ConfigParser = CONFIG
) -> list[str]:
    import re

    repos = get_repos(config)
    r = f".*{'.*'.join(query)}.*"
    return [os.path.expanduser(f"~/{repo}") for repo in repos if re.match(r, repo)]

def sort(config: configparser.ConfigParser = CONFIG) -> configparser.ConfigParser:
    sorted_config = configparser.ConfigParser()
    for sec in sorted(config.sections()):
        sorted_config.add_section(sec)
        for k, v in config[sec].items():
            sorted_config[sec][k] = v
    return sorted_config