File: change_link.py

package info (click to toggle)
dotdrop 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: sh: 13,401; python: 8,186; makefile: 3
file content (60 lines) | stat: -rwxr-xr-x 1,238 bytes parent folder | download | duplicates (2)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2018, deadc0de6

change the `link` key in all dotfiles
to a specific value

usage example:
    ./change-link.py --true ../config.yaml --ignore f_vimrc --ignore f_xinitrc
"""

import os
import sys
from docopt import docopt
from ruamel.yaml import YAML as yaml

USAGE = """
change-link.py

Usage:
  change-link.py --value=<value> [--ignore=<dotfile-name>...] <config.yaml>
  change-link.py --help

Options:
  -h --help               Show this screen.

"""

KEY = 'dotfiles'
ENTRY = 'link'


def change_link(path, value, ignores):
    """change link value"""
    with open(path, 'r', encoding='utf-8') as file:
        content = yaml(typ='safe').load(file)
    for k, val in content[KEY].items():
        if k in ignores:
            continue
        val[ENTRY] = value

    data = yaml()
    data.default_flow_style = False
    data.indent = 2
    data.typ = 'rt'
    data.dump(content, sys.stdout)


def main():
    """entry point"""
    args = docopt(USAGE)
    path = os.path.expanduser(args['<config.yaml>'])
    value = args['--value']
    ignores = args['--ignore']
    change_link(path, value, ignores)


if __name__ == '__main__':
    main()