More trivial stuff that bugs me.
[pyTivo/wmcbrine.git] / pyTivo.py
blob57cbcfe4d7e5c819e79135915cf5835510054a1b
1 #!/usr/bin/env python
3 import logging
4 import logging.config
5 import os
6 import ConfigParser
7 import beacon, httpserver, os, sys
8 import config
9 from plugin import GetPlugin
11 def init_logging():
12 if (config.config.has_section('loggers') and
13 config.config.has_section('handlers') and
14 config.config.has_section('formatters')):
16 logging.config.fileConfig(config.config_files)
18 elif config.getDebug():
19 logging.basicConfig(level=logging.DEBUG)
20 else:
21 logging.basicConfig(level=logging.INFO)
23 init_logging()
25 port = config.getPort()
27 httpd = httpserver.TivoHTTPServer(('', int(port)), httpserver.TivoHTTPHandler)
29 for section, settings in config.getShares():
30 httpd.add_container(section, settings)
31 # Precaching of files: does a recursive list of base path
32 if settings.get('precache', 'False').lower() == 'true':
33 plugin = GetPlugin(settings.get('type'))
34 if hasattr(plugin, 'pre_cache'):
35 print 'Pre-caching the', section, 'share.'
36 pre_cache_filter = getattr(plugin, 'pre_cache')
38 def build_recursive_list(path):
39 try:
40 for f in os.listdir(path):
41 f = os.path.join(path, f)
42 if os.path.isdir(f):
43 build_recursive_list(f)
44 else:
45 pre_cache_filter(f)
46 except:
47 pass
49 build_recursive_list(settings.get('path'))
51 b = beacon.Beacon()
52 b.add_service('TiVoMediaServer:' + str(port) + '/http')
53 b.start()
54 if 'listen' in config.getBeaconAddresses():
55 b.listen()
57 logging.getLogger('pyTivo').info('pyTivo is ready.')
59 try:
60 httpd.set_beacon(b)
61 httpd.serve_forever()
62 except KeyboardInterrupt:
63 b.stop()