annotate python/dlna.py @ 718:cd9d65e6e2ab

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