From fbccb8f7dda4cb6bb6d4835be0a45a1fe33b7036 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 1 Jan 2009 17:24:23 -0800 Subject: [PATCH] support passing in a config file... have an option to ignore the path, but default to checking it... there probably should be a better way, maybe a checkConfig function or something... use urlparse to join the urls... [git-p4: depot-paths = "//depot/": change = 1274] --- pymediaserv | 24 +++++++++++++++++++++++- pymeds.py | 27 +++++++++++++++++---------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/pymediaserv b/pymediaserv index 5a2ddda..b98eadf 100755 --- a/pymediaserv +++ b/pymediaserv @@ -3,13 +3,35 @@ from twisted.internet import reactor from twisted.application import service from twisted.python import log, usage +import ConfigParser import pymeds +import os.path import sys +defconfigfile = 'pymeds.ini' +class ConfigFile(pymeds.Options): + optParameters = [ [ 'config', 'c', defconfigfile, + 'INI style config file', ], ] + if __name__ == '__main__': - config = pymeds.Options() + config = ConfigFile() try: + config.checkpath = False config.parseOptions() + print `config` + if os.path.exists(config['config']): + print 'foo' + scp = ConfigParser.SafeConfigParser() + scp.read(config['config']) + config.update(scp.items('pymeds')) + + # Double check config + config.checkpath = True + config.postOptions() + elif config['config'] != defconfigfile: + print 'bar' + raise usage.UsageError( + 'config file %s does not exist' % config['config']) except usage.UsageError, errortext: print '%s: %s' % (sys.argv[0], errortext) print '%s: Try --help for usage details.' % sys.argv[0] diff --git a/pymeds.py b/pymeds.py index 378788a..3fa426a 100755 --- a/pymeds.py +++ b/pymeds.py @@ -47,15 +47,15 @@ import os.path import random import socket import string +import urlparse from twisted.application import internet, service from twisted.python import usage def generateuuid(): - if False: - return 'uuid:asdflkjewoifjslkdfj' return ''.join([ 'uuid:'] + map(lambda x: random.choice(string.letters), xrange(20))) class Options(usage.Options): + checkpath = True optParameters = [ [ 'title', 't', 'My Media Server', 'Title of the server.', ], [ 'path', 'p', 'media', 'Root path of the media to be served.', ], @@ -63,7 +63,7 @@ class Options(usage.Options): def postOptions(self): p = self['path'] - if not os.path.isdir(p): + if self.checkpath and not os.path.isdir(p): raise usage.UsageError, 'path %s does not exist' % `p` def parseArgs(self, *args): @@ -83,6 +83,10 @@ class Options(usage.Options): 'port must be between 1024 and 65535') self['port'] = port +class PyMedS(service.MultiService): + def startService(self): + service.MultiService.startService(self) + def makeService(config): listenAddr = config['addr'] listenPort = config['port'] @@ -98,7 +102,10 @@ def makeService(config): internet.MulticastServer(SSDP_PORT, s, listenMultiple=True).setServiceParent(serv) - uuid = generateuuid() + uuid = config.get('uuid', None) + if uuid is None: + uuid = generateuuid() + urlbase = 'http://%s:%d/' % (listenAddr, listenPort) # Create SOAP server and content server @@ -123,11 +130,11 @@ def makeService(config): root = WebServer() debug.insertnamespace('root', root) content = resource.Resource() - # This sets up the root to be the media dir so we don't have to enumerate - # the directory + # This sets up the root to be the media dir so we don't have to + # enumerate the directory. cds = ContentDirectoryServer(config['title'], klass=FSDirectory, - path=config['path'], urlbase=os.path.join(urlbase, 'content'), - webbase=content) + path=config['path'], urlbase=urlparse.urljoin(urlbase, 'content'), + webbase=content) debug.insertnamespace('cds', cds) root.putChild('ContentDirectory', cds) cds = cds.control @@ -137,7 +144,8 @@ def makeService(config): # Purely to ensure some sane mime-types. On MacOSX I need these. - # XXX - There isn't any easier way to get to the mime-type dict that I know of. + # XXX - There isn't any easier way to get to the mime-type dict + # that I know of. medianode = static.File('pymediaserv') medianode.contentTypes.update( { # From: http://support.microsoft.com/kb/288102 @@ -171,7 +179,6 @@ def makeService(config): internet.TCPServer(listenPort, site).setServiceParent(serv) # we need to do this after the children are there, since we send notifies - import urlparse rdxml = urlparse.urljoin(urlbase, 'root-device.xml') s.register('%s::upnp:rootdevice' % uuid, 'upnp:rootdevice', rdxml)