From cae224593667e473fb0add0a15b79cfe7016fa59 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Tue, 4 Sep 2012 16:52:22 -0400 Subject: [PATCH] MM2: Cleaned up pure.py a little --- airtime_mvc/application/models/RabbitMq.php | 12 +++-- .../media-monitor2/media/monitor/pure.py | 46 ++++++++++++++++++- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/airtime_mvc/application/models/RabbitMq.php b/airtime_mvc/application/models/RabbitMq.php index c91342373..cba216f8e 100644 --- a/airtime_mvc/application/models/RabbitMq.php +++ b/airtime_mvc/application/models/RabbitMq.php @@ -25,7 +25,8 @@ class Application_Model_RabbitMq $CC_CONFIG["rabbitmq"]["password"], $CC_CONFIG["rabbitmq"]["vhost"]); $channel = $conn->channel(); - $channel->access_request($CC_CONFIG["rabbitmq"]["vhost"], false, false, true, true); + $channel->access_request($CC_CONFIG["rabbitmq"]["vhost"], false, false, + true, true); $EXCHANGE = 'airtime-pypo'; $channel->exchange_declare($EXCHANGE, 'direct', false, true); @@ -50,7 +51,8 @@ class Application_Model_RabbitMq $CC_CONFIG["rabbitmq"]["password"], $CC_CONFIG["rabbitmq"]["vhost"]); $channel = $conn->channel(); - $channel->access_request($CC_CONFIG["rabbitmq"]["vhost"], false, false, true, true); + $channel->access_request($CC_CONFIG["rabbitmq"]["vhost"], false, false, + true, true); $EXCHANGE = 'airtime-media-monitor'; $channel->exchange_declare($EXCHANGE, 'direct', false, true); @@ -73,7 +75,8 @@ class Application_Model_RabbitMq $CC_CONFIG["rabbitmq"]["password"], $CC_CONFIG["rabbitmq"]["vhost"]); $channel = $conn->channel(); - $channel->access_request($CC_CONFIG["rabbitmq"]["vhost"], false, false, true, true); + $channel->access_request($CC_CONFIG["rabbitmq"]["vhost"], false, false, + true, true); $EXCHANGE = 'airtime-pypo'; $channel->exchange_declare($EXCHANGE, 'direct', false, true); @@ -84,7 +87,8 @@ class Application_Model_RabbitMq $temp['event_type'] = $event_type; $temp['server_timezone'] = Application_Model_Preference::GetTimezone(); if ($event_type == "update_recorder_schedule") { - $temp['shows'] = Application_Model_Show::getShows($now, $end_timestamp, $excludeInstance=NULL, $onlyRecord=TRUE); + $temp['shows'] = Application_Model_Show::getShows($now, + $end_timestamp, $excludeInstance=NULL, $onlyRecord=TRUE); } $data = json_encode($temp); $msg = new AMQPMessage($data, array('content_type' => 'text/plain')); diff --git a/python_apps/media-monitor2/media/monitor/pure.py b/python_apps/media-monitor2/media/monitor/pure.py index e7e2a57a5..3c2da02f9 100644 --- a/python_apps/media-monitor2/media/monitor/pure.py +++ b/python_apps/media-monitor2/media/monitor/pure.py @@ -2,6 +2,7 @@ import copy import subprocess import os +import math import shutil import re import sys @@ -11,6 +12,9 @@ import operator as op from os.path import normpath from itertools import takewhile +# you need to import reduce in python 3 +try: from functools import reduce +except: pass from configobj import ConfigObj from media.monitor.exceptions import FailedToSetLocale, FailedToCreateDir @@ -280,8 +284,6 @@ def organized_path(old_path, root_path, orig_md): """ filepath = None ext = extension(old_path) - # The blocks for each if statement look awfully similar. Perhaps there is a - # way to simplify this code def default_f(dictionary, key): if key in dictionary: return len(dictionary[key]) == 0 else: return True @@ -465,6 +467,46 @@ def file_playable(pathname): return_code = subprocess.call(command, shell=True) return (return_code == 0) +def toposort(data): + for k, v in data.items(): + v.discard(k) # Ignore self dependencies + extra_items_in_deps = reduce(set.union, data.values()) - set(data.keys()) + data.update({item:set() for item in extra_items_in_deps}) + while True: + ordered = set(item for item,dep in data.items() if not dep) + if not ordered: break + for e in sorted(ordered): yield e + data = dict((item,(dep - ordered)) for item,dep in data.items() + if item not in ordered) + assert not data, "A cyclic dependency exists amongst %r" % data + +def truncate_to_length(item, length): + """ + Truncates 'item' to 'length' + """ + if isinstance(item, int): item = str(item) + if isinstance(item, basestring): + if len(item) > length: return item[0:length] + else: return item + +def format_length(mutagen_length): + """ + Convert mutagen length to airtime length + """ + t = float(mutagen_length) + h = int(math.floor(t / 3600)) + t = t % 3600 + m = int(math.floor(t / 60)) + s = t % 60 + # will be ss.uuu + s = str(s) + seconds = s.split(".") + s = seconds[0] + # have a maximum of 6 subseconds. + if len(seconds[1]) >= 6: ss = seconds[1][0:6] + else: ss = seconds[1][0:] + return "%s:%s:%s.%s" % (h, m, s, ss) + if __name__ == '__main__': import doctest doctest.testmod()