File: expat_nzb.py

package info (click to toggle)
python-pynzb 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 152 kB
  • ctags: 59
  • sloc: python: 317; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 1,282 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from xml.parsers import expat

from pynzb.base import BaseNZBParser, NZBFile, NZBSegment

class ExpatNZBParser(BaseNZBParser):
    def start_element(self, name, attrs):
        if name == 'file':
            self.current_file = NZBFile(
                poster = attrs['poster'],
                date = attrs['date'],
                subject = attrs['subject']
            )
        if name == 'segment':
            self.current_segment = NZBSegment(
                bytes = attrs['bytes'],
                number = attrs['number']
            )
    
    def end_element(self, name):
        if name == 'file':
            self.files.append(self.current_file)
        elif name == 'group':
            self.current_file.add_group(self.current_data)
        elif name == 'segment':
            self.current_segment.message_id(self.current_data)
            self.current_file.add_segment(self.current_segment)
    
    def char_data(self, data):
        self.current_data = data
    
    def parse(self, xml):
        self.files = []
        parser = expat.ParserCreate()
        parser.StartElementHandler = self.start_element
        parser.EndElementHandler = self.end_element
        parser.CharacterDataHandler = self.char_data
        parser.Parse(xml)
        return self.files