studsauger.py 2.89 KB
Newer Older
Kiste's avatar
Kiste committed
1
2
3
4
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
Kiste's avatar
Kiste committed
5
import os
Kiste's avatar
Kiste committed
6
7
import keyring
import yaml
Kiste's avatar
Kiste committed
8
from logik import logik
Kiste's avatar
Kiste committed
9
10
11
12
13
14
15

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--path', help='Pfad für lokalen Dateibaum')
    parser.add_argument('-u', '--user', help='Stud.IP Benutzername')
    parser.add_argument('-pw', '--password', help='Stud.IP Passwort')
    parser.add_argument('-b', '--blacklist', help='Blacklist für Veranstaltungen, mehrmals angeben für mehrere Veranstaltungen', action='append')
Kiste's avatar
Kiste committed
16
    parser.add_argument('-a', '--allsemesters', help='Alle Semester statt nur dem aktuellen', action="store_true")
Kiste's avatar
Kiste committed
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
61
62
63
64
65
66
67
68
69
70
71
72
73
    parser.add_argument('-c', '--config', help='YAML-Konfigurationsdatei, wird ohne Angabe in $XDG_CONFIG_HOME/studsauger/config.yaml oder $HOME/.config/studsauger/config.yaml gesucht')
    args = parser.parse_args()

    configpath = None
    if args.config:
        if os.path.isfile(args.config):
            configpath = args.config
        else:
            print('Die Konfigurationsdatei "' + args.config + '" existiert nicht!')
            exit(1)
    elif os.path.isfile(os.environ['XDG_CONFIG_HOME'] + '/studsauger/config.yaml'):
        configpath = os.environ['XDG_CONFIG_HOME'] + '/studsauger/config.yaml'
    elif os.path.isfile(os.environ['HOME'] + '/.config/studsauger/config.yaml'):
        configpath = os.environ['HOME'] + '/.config/studsauger/config.yaml'

    if configpath:
        with open(configpath, 'r') as stream:
            try:
                config = yaml.load(stream)
            except yaml.YAMLError as exc:
                print(exc)
    else:
        config = {}

    if args.path:
        path = args.path
    elif 'path' in config:
        path = config['path']
    else:
        print('Kein Pfad angegeben!')
        exit(1)
    if args.user:
        user = args.user
    elif 'user' in config:
        user = config['user']
    else:
        print('Kein Benutzer angegeben!')
        exit(1)
    keyringpw = keyring.get_password('studsauger', user)
    if keyringpw:
        password = keyringpw
        if args.password or 'password' in config:
            print('Passwort für Benutzer "' + user + '" ist im Keyring und darf nicht angegeben werden!')
            exit(1)
    elif args.password:
        password = args.password
    elif 'password' in config:
        password = config['password']
    else:
        print('Kein Passwort angegeben!')
        exit(1)
    if args.blacklist:
        blacklist = args.blacklist
    elif 'blacklist' in config:
        blacklist = config['blacklist']
    else:
        blacklist = {}
Kiste's avatar
Kiste committed
74
75
76
77
78
79
80
81
82
    if args.allsemesters:
        allsemesters = True
    elif 'allsemesters' in config:
        allsemesters = config['allsemesters']
    else:
        print('Kein Semesterauswahl angegeben!')
        exit(1)

    print(allsemesters)
Kiste's avatar
Kiste committed
83

Kiste's avatar
Kiste committed
84
    logik(path, user, password, blacklist, allsemesters)
Kiste's avatar
Kiste committed
85
86
87

if __name__ == '__main__':
    main()