annotate python/dlna.py @ 656:4d9df2370659

STUB: python/dlna.py
author Jeff Hammel <k0scist@gmail.com>
date Sat, 29 Mar 2014 10:08:48 -0700
parents cd73d951ae5b
children 7f910ce4da04
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
655
cd73d951ae5b remove ephemeral directory
Jeff Hammel <k0scist@gmail.com>
parents: 654
diff changeset
6 import shutil
654
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import subprocess
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import sys
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import tempfile
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 from which import which
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 here = os.path.dirname(os.path.realpath(__file__))
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 string = (str, unicode)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def main(args=sys.argv[1:]):
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 dlna = which('minidlna')
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 assert dlna
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 parser = argparse.ArgumentParser(description=__doc__)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 parser.add_argument('--name', dest='name', default='protest servant',
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 help="friendly name")
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 parser.add_argument('--db-dir', dest='db_dir',
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 default=os.path.join(os.environ['HOME'], 'minidlna'),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 help='db directory')
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 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
27 help="port")
656
4d9df2370659 STUB: python/dlna.py
Jeff Hammel <k0scist@gmail.com>
parents: 655
diff changeset
28 parser.add_argument('-v', '--videos', dest='videos', nargs='+',
4d9df2370659 STUB: python/dlna.py
Jeff Hammel <k0scist@gmail.com>
parents: 655
diff changeset
29 help="videos")
654
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 parser.add_argument('audio', nargs='+')
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 options = parser.parse_args(args)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 lines = [('friendly_name', options.name),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 ('db_dir', options.db_dir),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 ('log_dir', options.db_dir),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 ('inotify', 'yes'),
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 ('enable_tivo', 'yes')]
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 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
39 for d in options.audio])
656
4d9df2370659 STUB: python/dlna.py
Jeff Hammel <k0scist@gmail.com>
parents: 655
diff changeset
40 lines.extend([('media_dir', 'V,{}'.format(os.path.abspath(d)))
4d9df2370659 STUB: python/dlna.py
Jeff Hammel <k0scist@gmail.com>
parents: 655
diff changeset
41 for d in options.audio])
654
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 config = '\n'.join(['{}={}'.format(*line) for line in lines])
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 print (config)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 fd, name = tempfile.mkstemp()
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 os.write(fd, config)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 os.close(fd)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 command = [dlna, '-f', name, '-d', '-p', str(options.port)]
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 print (subprocess.list2cmdline(command))
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 subprocess.check_call(command)
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 os.remove(name)
655
cd73d951ae5b remove ephemeral directory
Jeff Hammel <k0scist@gmail.com>
parents: 654
diff changeset
54 shutil.rmtree(options.db_dir)
654
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 if __name__ == '__main__':
d325f380e2fb add a front-end to minidlna
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 main()