Module SubtleXML
[hide private]
[frames] | no frames]

Source Code for Module SubtleXML

  1  #    This file is part of Subtle 
  2  # 
  3  #    This program is free software: you can redistribute it and/or modify 
  4  #    it under the terms of the GNU General Public License as published by 
  5  #    the Free Software Foundation, either version 3 of the License, or 
  6  #    (at your option) any later version. 
  7  # 
  8  #    This program is distributed in the hope that it will be useful, 
  9  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 11  #    GNU General Public License for more details. 
 12  # 
 13  #    You should have received a copy of the GNU General Public License 
 14  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 15   
 16  ## \file SouffleurXML.py 
 17  # Documentation for subtitles module of Souffleur project. 
 18  # \author Maxim Litvinov (aka DarakuTenshi) <otaky@ukr.net> 
 19   
 20  import xml.dom.minidom 
 21   
 22  from Subtitles import Sub 
 23  from Subtitles import Subtitles 
 24   
 25  ## ProjectXML class. 
 26  # Class for working whith XML formated project file. 
27 -class ProjectXML:
28 ## Constructor
29 - def __init__(self):
30 self.impl = xml.dom.minidom.getDOMImplementation() 31 self.doc = self.impl.createDocument(None, "souffleur", None) 32 self.root = self.doc.documentElement 33 34 rootAttr= self.doc.createAttribute("type") 35 rootAttr.nodeValue="project" 36 37 self.root.setAttributeNode(rootAttr) 38 39 versionEl = self.doc.createElement("version") 40 versionTxt = self.doc.createTextNode("0") 41 versionEl.appendChild(versionTxt) 42 self.root.appendChild(versionEl) 43 44 self.head = None 45 self.body = None 46 self.version = 0
47 48 ## \var impl 49 # DOMImplementation for XML parsing. 50 51 ## \var doc 52 # Document XML object. 53 54 ## \var root 55 # Root element ("souffleur" section) in the object. 56 57 ## \var head 58 # Object of the "head" section in the XML. 59 60 ## \var body 61 # Object of the "body" section in the XML. 62 63 ## \var version 64 # Version of the pfoject file format. 65 66 #============================================================================== 67 ## Load XML file. 68 # Load XML tree from the file. 69 # \param fileName - name of the XML project file. 70 # \return self object or None.
71 - def load(self, fileName):
72 self.root = None 73 self.head = None 74 self.body = None 75 self.version = None 76 self.doc = xml.dom.minidom.parse(fileName) 77 if self.doc.documentElement.nodeName != "souffleur": 78 return None 79 self.root=self.doc.documentElement 80 for i in self.root.childNodes: 81 if i.nodeName=="head": 82 self.head=i 83 elif i.nodeName=="body": 84 self.body=i 85 elif i.nodeName=="version": 86 self.version = i.childNodes[0].nodeValue 87 return self
88 89 #============================================================================== 90 ## Write XML. 91 # Write XML data to the file. 92 # \param fileName - name of file to store data.
93 - def write(self, fileName):
94 HDLR=file(fileName, "w") 95 self.doc.writexml(HDLR) 96 HDLR.close()
97 98 #============================================================================== 99 ## Add variable head. 100 # Add some variable to the head section. 101 # \param attrName - name of the attribute. 102 # \param attrValue - value of the attribute.
103 - def addHeadInfo(self, attrName, attrValue):
104 if not self.head: 105 self.head=self.doc.createElement("head") 106 self.root.appendChild(self.head) 107 108 if not attrName or not attrValue: 109 return 110 111 attrEl = self.doc.createElement(attrName) 112 attrTxt = self.doc.createTextNode(attrValue) 113 attrEl.appendChild(attrTxt) 114 self.head.appendChild(attrEl)
115 116 #============================================================================== 117 ## Add media. 118 # Add media info to the body section. 119 # \param media - Media class instance.
120 - def addMedia(self, media):
121 if not media: 122 return 123 if type(media)!=type(Media()): 124 return 125 if not self.body: 126 self.body=self.doc.createElement("body") 127 self.root.appendChild(self.body) 128 129 data = self.doc.createElement("data") 130 self.body.appendChild(data) 131 132 source = self.doc.createElement("source") 133 sType= self.doc.createAttribute("type") 134 sType.nodeValue=media.MIME 135 source.setAttributeNode(sType) 136 sTxt = self.doc.createTextNode(media.source) 137 source.appendChild(sTxt) 138 data.appendChild(source) 139 140 for i in media.Streams: 141 tmpMedia = self.doc.createElement("media") 142 data.appendChild(tmpMedia) 143 144 tmpEl = self.doc.createElement("type") 145 tmpTxt = self.doc.createTextNode(i.MIME) 146 tmpEl.appendChild(tmpTxt) 147 tmpMedia.appendChild(tmpEl) 148 149 tmpEl = self.doc.createElement("name") 150 tmpTxt = self.doc.createTextNode(i.Name) 151 tmpEl.appendChild(tmpTxt) 152 tmpMedia.appendChild(tmpEl) 153 154 tmpEl = self.doc.createElement("id") 155 tmpTxt = self.doc.createTextNode(str(i.ID)) 156 tmpEl.appendChild(tmpTxt) 157 tmpMedia.appendChild(tmpEl) 158 159 if not i.attrs: 160 continue 161 162 attrs = self.doc.createElement("attrs") 163 tmpMedia.appendChild(attrs) 164 for j in i.attrs.keys(): 165 tmpEl = self.doc.createElement(j) 166 tmpTxt = self.doc.createTextNode(i.attrs[j]) 167 tmpEl.appendChild(tmpTxt) 168 attrs.appendChild(tmpEl)
169 170 #============================================================================== 171 ## Add subtitles. 172 # Add subtitles to the body section. 173 # \param subtitle - Subtitles class instance.
174 - def addSubtitle(self, subtitle):
175 if not subtitle: 176 return 177 if type(subtitle)!=type(Subtitles()): 178 return 179 if not self.body: 180 self.body=self.doc.createElement("body") 181 self.root.appendChild(self.body) 182 183 data = self.doc.createElement("subtitles") 184 self.body.appendChild(data) 185 186 source = self.doc.createElement("source") 187 sTxt = self.doc.createTextNode(str(subtitle.subSource)) 188 source.appendChild(sTxt) 189 data.appendChild(source) 190 191 for i in subtitle.subKeys: 192 sub = self.doc.createElement("sub") 193 tmpEl = self.doc.createElement("start") 194 tmpTxt = self.doc.createTextNode(str(subtitle.subs[i].start_time)) 195 tmpEl.appendChild(tmpTxt) 196 sub.appendChild(tmpEl) 197 tmpEl = self.doc.createElement("end") 198 tmpTxt = self.doc.createTextNode(str(subtitle.subs[i].end_time)) 199 tmpEl.appendChild(tmpTxt) 200 sub.appendChild(tmpEl) 201 tmpEl = self.doc.createElement("text") 202 tmpTxt = self.doc.createTextNode(str(subtitle.subs[i].text)) 203 tmpEl.appendChild(tmpTxt) 204 sub.appendChild(tmpEl) 205 data.appendChild(sub)
206 207 #============================================================================== 208 ##Get head 209 # Get list of the head section attributes. 210 # \return list of the attrName => attrValue
211 - def getHead(self):
212 if not self.head: 213 return None 214 ret={} 215 for i in self.head.childNodes: 216 ret[i.nodeName]=i.childNodes[0].nodeValue 217 return ret
218 219 #============================================================================== 220 ## Get media. 221 # Get media info from XML. 222 # \return List of the Media class.
223 - def getMedia(self):
224 if not self.body: 225 return None 226 ret=[] 227 for i in self.body.childNodes: 228 if i.nodeName=="data": 229 tMedia=Media() 230 for j in i.childNodes: 231 if j.nodeName=="source": 232 mType=j.attributes["type"] 233 if not mType: 234 return None 235 tMedia.MIME=mType.nodeValue 236 tMedia.source=j.childNodes[0].nodeValue 237 elif j.nodeName=="media": 238 tStream = Stream() 239 for k in j.childNodes: 240 nodeName = k.nodeName 241 if nodeName == "type": 242 tStream.MIME = k.childNodes[0].nodeValue 243 elif nodeName == "id": 244 tStream.ID = k.childNodes[0].nodeValue 245 elif nodeName == "name": 246 tStream.Name = k.childNodes[0].nodeValue 247 elif nodeName == "attrs": 248 for l in k.childNodes: 249 tStream.addAttr(l.nodeName, l.childNodes[0].nodeValue) 250 tMedia.addStream(tStream) 251 ret.append(tMedia) 252 return ret
253 254 #============================================================================== 255 ##Get subtitles. 256 # Get subtitles from XML project file. 257 # \raturn Array of the Subtitles.
258 - def getSubtitle(self):
259 if not self.body: 260 return None 261 ret=[] 262 for i in self.body.childNodes: 263 if i.nodeName=="subtitles": 264 tSubtitles=Subtitles() 265 for j in i.childNodes: 266 if j.nodeName=="source": 267 tSubtitles.subSource=j.childNodes[0].nodeValue 268 elif j.nodeName=="sub": 269 tSub=Sub() 270 for k in j.childNodes: 271 nodeName = k.nodeName 272 if nodeName == "start": 273 tSub.start_time=int(k.childNodes[0].nodeValue) 274 elif nodeName == "end": 275 tSub.end_time=int(k.childNodes[0].nodeValue) 276 elif nodeName == "text": 277 tSub.text=str(k.childNodes[0].nodeValue) 278 tSubtitles.subs[tSub.start_time]=tSub 279 tSubtitles.updateKeys() 280 ret.append(tSubtitles) 281 return ret
282