Browse Source

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]
main
John-Mark Gurney 16 years ago
parent
commit
fbccb8f7dd
2 changed files with 40 additions and 11 deletions
  1. +23
    -1
      pymediaserv
  2. +17
    -10
      pymeds.py

+ 23
- 1
pymediaserv View File

@@ -3,13 +3,35 @@
from twisted.internet import reactor from twisted.internet import reactor
from twisted.application import service from twisted.application import service
from twisted.python import log, usage from twisted.python import log, usage
import ConfigParser
import pymeds import pymeds
import os.path
import sys import sys


defconfigfile = 'pymeds.ini'
class ConfigFile(pymeds.Options):
optParameters = [ [ 'config', 'c', defconfigfile,
'INI style config file', ], ]

if __name__ == '__main__': if __name__ == '__main__':
config = pymeds.Options()
config = ConfigFile()
try: try:
config.checkpath = False
config.parseOptions() 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: except usage.UsageError, errortext:
print '%s: %s' % (sys.argv[0], errortext) print '%s: %s' % (sys.argv[0], errortext)
print '%s: Try --help for usage details.' % sys.argv[0] print '%s: Try --help for usage details.' % sys.argv[0]


+ 17
- 10
pymeds.py View File

@@ -47,15 +47,15 @@ import os.path
import random import random
import socket import socket
import string import string
import urlparse
from twisted.application import internet, service from twisted.application import internet, service
from twisted.python import usage from twisted.python import usage


def generateuuid(): def generateuuid():
if False:
return 'uuid:asdflkjewoifjslkdfj'
return ''.join([ 'uuid:'] + map(lambda x: random.choice(string.letters), xrange(20))) return ''.join([ 'uuid:'] + map(lambda x: random.choice(string.letters), xrange(20)))


class Options(usage.Options): class Options(usage.Options):
checkpath = True
optParameters = [ optParameters = [
[ 'title', 't', 'My Media Server', 'Title of the server.', ], [ 'title', 't', 'My Media Server', 'Title of the server.', ],
[ 'path', 'p', 'media', 'Root path of the media to be served.', ], [ 'path', 'p', 'media', 'Root path of the media to be served.', ],
@@ -63,7 +63,7 @@ class Options(usage.Options):


def postOptions(self): def postOptions(self):
p = self['path'] 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` raise usage.UsageError, 'path %s does not exist' % `p`


def parseArgs(self, *args): def parseArgs(self, *args):
@@ -83,6 +83,10 @@ class Options(usage.Options):
'port must be between 1024 and 65535') 'port must be between 1024 and 65535')
self['port'] = port self['port'] = port


class PyMedS(service.MultiService):
def startService(self):
service.MultiService.startService(self)

def makeService(config): def makeService(config):
listenAddr = config['addr'] listenAddr = config['addr']
listenPort = config['port'] listenPort = config['port']
@@ -98,7 +102,10 @@ def makeService(config):
internet.MulticastServer(SSDP_PORT, s, internet.MulticastServer(SSDP_PORT, s,
listenMultiple=True).setServiceParent(serv) listenMultiple=True).setServiceParent(serv)


uuid = generateuuid()
uuid = config.get('uuid', None)
if uuid is None:
uuid = generateuuid()

urlbase = 'http://%s:%d/' % (listenAddr, listenPort) urlbase = 'http://%s:%d/' % (listenAddr, listenPort)


# Create SOAP server and content server # Create SOAP server and content server
@@ -123,11 +130,11 @@ def makeService(config):
root = WebServer() root = WebServer()
debug.insertnamespace('root', root) debug.insertnamespace('root', root)
content = resource.Resource() 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, 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) debug.insertnamespace('cds', cds)
root.putChild('ContentDirectory', cds) root.putChild('ContentDirectory', cds)
cds = cds.control cds = cds.control
@@ -137,7 +144,8 @@ def makeService(config):




# Purely to ensure some sane mime-types. On MacOSX I need these. # 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 = static.File('pymediaserv')
medianode.contentTypes.update( { medianode.contentTypes.update( {
# From: http://support.microsoft.com/kb/288102 # From: http://support.microsoft.com/kb/288102
@@ -171,7 +179,6 @@ def makeService(config):
internet.TCPServer(listenPort, site).setServiceParent(serv) internet.TCPServer(listenPort, site).setServiceParent(serv)


# we need to do this after the children are there, since we send notifies # we need to do this after the children are there, since we send notifies
import urlparse
rdxml = urlparse.urljoin(urlbase, 'root-device.xml') rdxml = urlparse.urljoin(urlbase, 'root-device.xml')
s.register('%s::upnp:rootdevice' % uuid, s.register('%s::upnp:rootdevice' % uuid,
'upnp:rootdevice', rdxml) 'upnp:rootdevice', rdxml)


Loading…
Cancel
Save