From 869824755c343edaf75ea1497256faab4198c511 Mon Sep 17 00:00:00 2001 From: Jason Michalski Date: Sun, 18 Nov 2007 12:17:06 -0600 Subject: [PATCH] Cleaned up config Renamed Config.py -> config.py Removed all use of from Config import config --- beacon.py | 14 +++----------- Config.py => config.py | 19 ++++++++++++++++++- plugins/video/transcode.py | 22 +++++++++++----------- plugins/video/video.py | 6 +++--- pyTivo.py | 9 +++------ pyTivoService.py | 12 ++++-------- 6 files changed, 42 insertions(+), 40 deletions(-) rename Config.py => config.py (86%) diff --git a/beacon.py b/beacon.py index 25efc89..fe0b05b 100644 --- a/beacon.py +++ b/beacon.py @@ -1,6 +1,6 @@ from socket import * from threading import Timer -from Config import config +import config class Beacon: @@ -18,12 +18,7 @@ class Beacon: def format_beacon(self): beacon = [] - from Config import config - - if config.has_option('Server', 'GUID'): - guid = config.get('Server', 'GUID') - else: - guid = '123456' + guid = config.getGUID() beacon.append('tivoconnect=1') beacon.append('swversion=1') @@ -38,10 +33,7 @@ class Beacon: return '\n'.join(beacon) def send_beacon(self): - if config.has_option('Server', 'beacon'): - beacon_ips = config.get('Server', 'beacon') - else: - beacon_ips = '255.255.255.255' + beacon_ips = config.getBeaconAddreses() for beacon_ip in beacon_ips.split(): self.UDPSock.sendto(self.format_beacon(), (beacon_ip, 2190)) diff --git a/Config.py b/config.py similarity index 86% rename from Config.py rename to config.py index bc18dd7..df07572 100644 --- a/Config.py +++ b/config.py @@ -8,6 +8,23 @@ config = ConfigParser.ConfigParser() p = os.path.dirname(__file__) config.read(os.path.join(p, 'pyTivo.conf')) +def getGUID(): + if config.has_option('Server', 'GUID'): + guid = config.get('Server', 'GUID') + else: + guid = '123456' + return guid + +def getBeaconAddreses(): + if config.has_option('Server', 'beacon'): + beacon_ips = config.get('Server', 'beacon') + else: + beacon_ips = '255.255.255.255' + return beacon_ips + +def getPort(): + return config.get('Server', 'Port') + def get169Setting(tsn): if not tsn: return True @@ -25,7 +42,7 @@ def get169Setting(tsn): return True def getShares(): - return filter( lambda x: not(x.startswith('_tivo_') or x == 'Server'), config.sections()) + return ( (section, dict(config.items(section))) for section in config.sections() if not(section.startswith('_tivo_') or section == 'Server') ) def getDebug(): try: diff --git a/plugins/video/transcode.py b/plugins/video/transcode.py index 2731552..2b22437 100644 --- a/plugins/video/transcode.py +++ b/plugins/video/transcode.py @@ -1,14 +1,14 @@ import subprocess, shutil, os, re, sys, ConfigParser, time, lrucache -import Config +import config info_cache = lrucache.LRUCache(1000) -debug = Config.getDebug() -MAX_VIDEO_BR = Config.getMaxVideoBR() -BUFF_SIZE = Config.getBuffSize() +debug = config.getDebug() +MAX_VIDEO_BR = config.getMaxVideoBR() +BUFF_SIZE = config.getBuffSize() -FFMPEG = Config.get('Server', 'ffmpeg') +FFMPEG = config.get('Server', 'ffmpeg') def debug_write(data): if debug: @@ -46,13 +46,13 @@ def output_video(inFile, outFile, tsn=''): def transcode(inFile, outFile, tsn=''): settings = {} - settings['audio_br'] = Config.getAudioBR(tsn) - settings['video_br'] = Config.getVideoBR(tsn) + settings['audio_br'] = config.getAudioBR(tsn) + settings['video_br'] = config.getVideoBR(tsn) settings['max_video_br'] = MAX_VIDEO_BR settings['buff_size'] = BUFF_SIZE settings['aspect_ratio'] = ' '.join(select_aspect(inFile, tsn)) - cmd_string = Config.getFFMPEGTemplate(tsn) % settings + cmd_string = config.getFFMPEGTemplate(tsn) % settings cmd = [FFMPEG, '-i', inFile] + cmd_string.split() @@ -64,14 +64,14 @@ def transcode(inFile, outFile, tsn=''): kill(ffmpeg.pid) def select_aspect(inFile, tsn = ''): - TIVO_WIDTH = Config.getTivoWidth(tsn) - TIVO_HEIGHT = Config.getTivoHeight(tsn) + TIVO_WIDTH = config.getTivoWidth(tsn) + TIVO_HEIGHT = config.getTivoHeight(tsn) type, width, height, fps, millisecs = video_info(inFile) debug_write(['tsn:', tsn, '\n']) - aspect169 = Config.get169Setting(tsn) + aspect169 = config.get169Setting(tsn) debug_write(['aspect169:', aspect169, '\n']) diff --git a/plugins/video/video.py b/plugins/video/video.py index 8a832e3..54ab67e 100644 --- a/plugins/video/video.py +++ b/plugins/video/video.py @@ -5,7 +5,7 @@ from urllib import unquote_plus, quote, unquote from urlparse import urlparse from xml.sax.saxutils import escape from lrucache import LRUCache -import Config +import config SCRIPTDIR = os.path.dirname(__file__) @@ -62,8 +62,8 @@ class video(Plugin): if transcode.tivo_compatable(full_path): # Is TiVo compatible mpeg2 return int(os.stat(full_path).st_size) else: # Must be re-encoded - audioBPS = strtod(Config.getAudioBR()) - videoBPS = strtod(Config.getVideoBR()) + audioBPS = strtod(config.getAudioBR()) + videoBPS = strtod(config.getVideoBR()) bitrate = audioBPS + videoBPS return int((duration(file)/1000)*(bitrate * 1.02 / 8)) diff --git a/pyTivo.py b/pyTivo.py index 0afaa51..475d2ec 100755 --- a/pyTivo.py +++ b/pyTivo.py @@ -3,16 +3,13 @@ import beacon, httpserver, os, sys -from Config import config -import Config +import config -port = config.get('Server', 'Port') +port = config.getPort() httpd = httpserver.TivoHTTPServer(('', int(port)), httpserver.TivoHTTPHandler) -for section in Config.getShares(): - settings = {} - settings.update(config.items(section)) +for section, settings in config.getShares(): httpd.add_container(section, settings) b = beacon.Beacon() diff --git a/pyTivoService.py b/pyTivoService.py index 2f9f6be..4cd260e 100644 --- a/pyTivoService.py +++ b/pyTivoService.py @@ -1,9 +1,9 @@ -import beacon, httpserver, ConfigParser +import beacon, httpserver import win32serviceutil import win32service import win32event import select, sys -import Config +import config class PyTivoService(win32serviceutil.ServiceFramework): _svc_name_ = 'pyTivo' @@ -17,8 +17,6 @@ class PyTivoService(win32serviceutil.ServiceFramework): import sys, os - from Config import config - p = os.path.dirname(__file__) f = open(os.path.join(p, 'log.txt'), 'w') @@ -26,13 +24,11 @@ class PyTivoService(win32serviceutil.ServiceFramework): sys.stderr = f - port = config.get('Server', 'Port') + port = config.getPort() httpd = httpserver.TivoHTTPServer(('', int(port)), httpserver.TivoHTTPHandler) - for section in Config.getShares(): - settings = {} - settings.update(config.items(section)) + for section, settings in config.getShares(): httpd.add_container(section, settings) b = beacon.Beacon() -- 2.11.4.GIT