annotate python/dlna.py @ 654:d325f380e2fb

add a front-end to minidlna
author Jeff Hammel <k0scist@gmail.com>
date Fri, 28 Mar 2014 23:01:22 -0700
parents
children cd73d951ae5b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
654
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 import argparse
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 import os
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 import subprocess
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import sys
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import tempfile
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 from which import which
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 here = os.path.dirname(os.path.realpath(__file__))
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 string = (str, unicode)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 def main(args=sys.argv[1:]):
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 dlna = which('minidlna')
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 assert dlna
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 parser = argparse.ArgumentParser(description=__doc__)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 parser.add_argument('--name', dest='name', default='protest servant',
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 help="friendly name")
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 parser.add_argument('--db-dir', dest='db_dir',
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 default=os.path.join(os.environ['HOME'], 'minidlna'),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 help='db directory')
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 parser.add_argument('-p', '--port', dest='port', default=8200, type=int,
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 help="port")
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 parser.add_argument('audio', nargs='+')
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 options = parser.parse_args(args)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 lines = [('friendly_name', options.name),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 ('db_dir', options.db_dir),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 ('log_dir', options.db_dir),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 ('inotify', 'yes'),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 ('enable_tivo', 'yes')]
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 lines.extend([('media_dir', 'A,{}'.format(os.path.abspath(d)))
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 for d in options.audio])
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 config = '\n'.join(['{}={}'.format(*line) for line in lines])
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 print (config)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 fd, name = tempfile.mkstemp()
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 os.write(fd, config)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 os.close(fd)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 command = [dlna, '-f', name, '-d', '-p', str(options.port)]
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 print (subprocess.list2cmdline(command))
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 subprocess.check_call(command)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 os.remove(name)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 if __name__ == '__main__':
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 main()