Package timeside :: Package tools :: Module cache
[hide private]
[frames] | no frames]

Source Code for Module timeside.tools.cache

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright (C) 2006-2010 Guillaume Pellerin 
  5   
  6  # <yomguy@parisson.com> 
  7   
  8  # This software is a computer program whose purpose is to stream audio 
  9  # and video data through icecast2 servers. 
 10   
 11  # This software is governed by the CeCILL license under French law and 
 12  # abiding by the rules of distribution of free software. You can use, 
 13  # modify and/ or redistribute the software under the terms of the CeCILL 
 14  # license as circulated by CEA, CNRS and INRIA at the following URL 
 15  # "http://www.cecill.info". 
 16   
 17  # As a counterpart to the access to the source code and  rights to copy, 
 18  # modify and redistribute granted by the license, users are provided only 
 19  # with a limited warranty and the software's author, the holder of the 
 20  # economic rights, and the successive licensors have only limited 
 21  # liability. 
 22   
 23  # In this respect, the user's attention is drawn to the risks associated 
 24  # with loading, using,  modifying and/or developing or reproducing the 
 25  # software by the user in light of its specific status of free software, 
 26  # that may mean that it is complicated to manipulate, and that also 
 27  # therefore means that it is reserved for developers and  experienced 
 28  # professionals having in-depth computer knowledge. Users are therefore 
 29  # encouraged to load and test the software's suitability as regards their 
 30  # requirements in conditions enabling the security of their systems and/or 
 31  # data to be ensured and, more generally, to use and operate it in the 
 32  # same conditions as regards security. 
 33   
 34  # The fact that you are presently reading this means that you have had 
 35  # knowledge of the CeCILL license and that you accept its terms. 
 36   
 37  # Author: Guillaume Pellerin <yomguy@parisson.com> 
 38   
 39  import os 
 40  import xml.dom.minidom 
 41   
 42   
43 -class Cache(object):
44
45 - def __init__(self, dir, params=None):
46 self.dir = dir 47 self.params = params 48 self.files = self.get_files()
49
50 - def get_files(self):
51 list = [] 52 for root, dirs, files in os.walk(self.dir): 53 for file in files: 54 list.append(file) 55 return list
56
57 - def exists(self, file):
58 self.files = self.get_files() 59 return file in self.files
60
61 - def write_bin(self, data, file):
62 path = self.dir + os.sep + file 63 f = open(path, 'w') 64 f.write(data) 65 f.close()
66
67 - def read_bin(self, file):
68 path = self.dir + os.sep + file 69 f = open(path, 'r') 70 data = f.read() 71 f.close() 72 return data
73
74 - def read_stream_bin(self, file):
75 path = self.dir + os.sep + file 76 chunk_size = 0x1000 77 f = open(path, 'r') 78 while True: 79 _chunk = f.read(chunk_size) 80 if not len(_chunk): 81 break 82 yield _chunk 83 f.close()
84
85 - def write_stream_bin(self, chunk, file_object):
86 file_object.write(chunk)
87
88 - def read_analyzer_xml(self, file):
89 list = [] 90 path = self.dir + os.sep + file 91 doc = xml.dom.minidom.parse(path) 92 for data in doc.documentElement.getElementsByTagName('data') : 93 name = data.getAttribute('name') 94 id = data.getAttribute('id') 95 unit = data.getAttribute('unit') 96 value = data.getAttribute('value') 97 list.append({'name': name, 'id': id, 'unit': unit, 'value': value}) 98 return list
99
100 - def write_analyzer_xml(self, data_list, file):
101 path = self.dir + os.sep + file 102 doc = xml.dom.minidom.Document() 103 root = doc.createElement('telemeta') 104 doc.appendChild(root) 105 for data in data_list: 106 name = data['name'] 107 id = data['id'] 108 unit = data['unit'] 109 value = data['value'] 110 node = doc.createElement('data') 111 node.setAttribute('name', name) 112 node.setAttribute('id', id) 113 node.setAttribute('unit', unit) 114 node.setAttribute('value', str(value)) 115 root.appendChild(node) 116 f = open(path, "w") 117 f.write(xml.dom.minidom.Document.toprettyxml(doc)) 118 f.close()
119