File: lircd-setup

package info (click to toggle)
lirc 0.10.2-0.10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,268 kB
  • sloc: ansic: 26,981; cpp: 9,187; sh: 5,875; python: 4,507; makefile: 1,049; xml: 63
file content (57 lines) | stat: -rwxr-xr-x 1,375 bytes parent folder | download
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
#!/usr/bin/env python3

''' Parse the [modinit] section in lirc_options.conf and run it. '''

import configparser
import subprocess
import sys

_HELP = ''' Usage: lircd_setup [lirc_options.conf file] '''

_DEFAULT_FILE = "/etc/lirc/lirc_options.conf"


def run_command(parser, key):
    try:
        code = parser.get('modinit', key)
    except configparser.NoOptionError:
        return
    else:
        cmd = ['/bin/sh', '-c', code]
        try:
             subprocess.check_call(cmd)
        except subprocess.CalledProcessError as err:
            print("Cannot execute: " + ' '.join(cmd))
            print(err)


def main():
    ''' Indeed: the main program. '''
    if len(sys.argv) == 1:
        path = _DEFAULT_FILE
    elif len(sys.argv) == 2:
        path = sys.argv[1]
    else:
        sys.stderr.write(_HELP + '\n')
        sys.exit(1)
    if sys.version_info < (3, 2):
        parser = configparser.SafeConfigParser()
    else:
        parser = configparser.ConfigParser()
    try:
        found = parser.read(path)
    except configparser.Error:
        sys.stderr.write("Cannot parse file " + path)
        sys.exit(1)
    if not parser.has_section("modinit"):
        return
    run_command(parser, 'code')
    for ix in range(1, 11):
        run_command(parser, 'code' + str(ix))


if __name__ == '__main__':
    main()


# vim: set expandtab ts=4 sw=4: