mkv

Untitled

mkv
Sep 5th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. class StatusParser(html.parser.HTMLParser):
  2.     def __init__(self):
  3.         html.parser.HTMLParser.__init__(self)
  4.         self.result = {}
  5.         self._td = None         # in a TD? class or 'yes'
  6.         self._h3 = False        # in a H3?
  7.         self._key = None        # content of prev. TD with class
  8.         self._tab = False       # skip next </table> ?
  9.         self._mount = None      # mount name
  10.         self._value = None      # field value
  11.     def handle_starttag(self, tag, attrs):
  12.         #print('{}   ---   {}   ---   {}', self, tag, attrs)
  13.         if tag == 'td':
  14.             self._td = 'yes'
  15.             if len(attrs) > 0:
  16.                 if attrs[0][0] == 'class':
  17.                     self._td = attrs[0][1]
  18.                     if (self._td == 'streamdata'):
  19.                         self._value = ''
  20.         elif tag == 'h3' and self._td:
  21.             self._h3 = True
  22.             self._tab = True
  23.     def handle_endtag(self, tag):
  24.         if tag == 'td':
  25.             if self._value:
  26.                 self.result[self._mount][self._key[:-1]] = self._value
  27.             self._value = None
  28.             self._td = None
  29.         elif tag == 'h3':
  30.             self._h3 = False
  31.         elif tag == 'table':
  32.             if not self._tab:
  33.                 self._mount = None
  34.             self._tab = False
  35.     def handle_data(self, data):
  36.         if self._h3 and self._td:
  37.             self._mount = data.split(' ')[2]
  38.             self.result[self._mount] = OrderedMultiDict()
  39.         elif self._td and self._mount:
  40.             if self._td == 'streamdata':
  41.                 self._value += data
  42.             else:
  43.                 self._key = data
  44.     def handle_charref(self, name):
  45.         if self._value:
  46.             if name.startswith('x'):
  47.                 c = chr(int(name[1:], 16))
  48.             else:
  49.                 c = chr(int(name))
  50.             self._value += c
  51.     def handle_entityref(self, name):
  52.         if self._value:
  53.             self._value += chr(name2codepoint[name])
Advertisement
Add Comment
Please, Sign In to add comment