File: opml.py

package info (click to toggle)
python-feedvalidator 0~svn1022-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 652 kB
  • ctags: 2,452
  • sloc: python: 9,481; makefile: 27; sh: 8
file content (164 lines) | stat: -rw-r--r-- 5,578 bytes parent folder | download | duplicates (2)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""$Id: opml.py 988 2008-03-12 18:22:48Z sa3ruby $"""

__author__ = "Sam Ruby <http://intertwingly.net/> and Mark Pilgrim <http://diveintomark.org/>"
__version__ = "$Revision: 988 $"
__copyright__ = "Copyright (c) 2002 Sam Ruby and Mark Pilgrim"

from base import validatorBase
from validators import *
from logging import *
from extension import extension_everywhere
import re

#
# Outline Processor Markup Language element.
#
class opml(validatorBase, extension_everywhere):
  versionList = ['1.0', '1.1', '2.0']

  def validate(self):
    self.setFeedType(TYPE_OPML)

    if (None,'version') in self.attrs.getNames():
      if self.attrs[(None,'version')] not in opml.versionList:
        self.log(InvalidOPMLVersion({"parent":self.parent.name, "element":self.name, "value":self.attrs[(None,'version')]}))
    elif self.name != 'outlineDocument':
      self.log(MissingAttribute({"parent":self.parent.name, "element":self.name, "attr":"version"}))
    
    if 'head' not in self.children:
      self.log(MissingElement({"parent":self.name, "element":"head"}))

    if 'body' not in self.children:
      self.log(MissingElement({"parent":self.name, "element":"body"}))

  def getExpectedAttrNames(self):
    return [(None, u'version')]

  def do_head(self):
    return opmlHead()

  def do_body(self):
    return opmlBody()

class opmlHead(validatorBase, extension_everywhere):
  def do_title(self):
    return safeHtml(), noduplicates()

  def do_dateCreated(self):
    return rfc822(), noduplicates()

  def do_dateModified(self):
    return rfc822(), noduplicates()

  def do_ownerName(self):
    return safeHtml(), noduplicates()

  def do_ownerEmail(self):
    return email(), noduplicates()

  def do_ownerId(self):
    return httpURL(), noduplicates()

  def do_expansionState(self):
    return commaSeparatedLines(), noduplicates()

  def do_vertScrollState(self):
    return positiveInteger(), nonblank(), noduplicates()

  def do_windowTop(self):
    return positiveInteger(), nonblank(), noduplicates()

  def do_windowLeft(self):
    return positiveInteger(), nonblank(), noduplicates()

  def do_windowBottom(self):
    return positiveInteger(), nonblank(), noduplicates()

  def do_windowRight(self):
    return positiveInteger(), nonblank(), noduplicates()

class commaSeparatedLines(text):
  linenumbers_re=re.compile('^(\d+(,\s*\d+)*)?$')
  def validate(self):
    if not self.linenumbers_re.match(self.value):
      self.log(InvalidExpansionState({"parent":self.parent.name, "element":self.name, "value":self.value}))

class opmlBody(validatorBase, extension_everywhere):

  def validate(self):
    if 'outline' not in self.children:
      self.log(MissingElement({"parent":self.name, "element":"outline"}))

  def do_outline(self):
    return opmlOutline()

class opmlOutline(validatorBase, extension_everywhere):
  versionList = ['RSS', 'RSS1', 'RSS2', 'scriptingNews']

  def getExpectedAttrNames(self):
    return [
      (None, u'category'),
      (None, u'created'),
      (None, u'description'),
      (None, u'htmlUrl'),
      (None, u'isBreakpoint'),
      (None, u'isComment'),
      (None, u'language'),
      (None, u'text'), 
      (None, u'title'),
      (None, u'type'), 
      (None, u'url'),
      (None, u'version'),
      (None, u'xmlUrl'),
    ]

  def validate(self):

    if not (None,'text') in self.attrs.getNames():
      self.log(MissingAttribute({"parent":self.parent.name, "element":self.name, "attr":"text"}))

    if (None,'type') in self.attrs.getNames():
      if self.attrs[(None,'type')].lower() == 'rss':

        if not (None,'xmlUrl') in self.attrs.getNames():
          self.log(MissingXmlURL({"parent":self.parent.name, "element":self.name}))
        if not (None,'title') in self.attrs.getNames():
          self.log(MissingTitleAttr({"parent":self.parent.name, "element":self.name}))

      elif self.attrs[(None,'type')].lower() == 'link':

        if not (None,'url') in self.attrs.getNames():
          self.log(MissingUrlAttr({"parent":self.parent.name, "element":self.name}))

      else:

        self.log(InvalidOutlineType({"parent":self.parent.name, "element":self.name, "value":self.attrs[(None,'type')]}))

    if (None,'version') in self.attrs.getNames():
      if self.attrs[(None,'version')] not in opmlOutline.versionList:
        self.log(InvalidOutlineVersion({"parent":self.parent.name, "element":self.name, "value":self.attrs[(None,'version')]}))
 
    if len(self.attrs)>1 and not (None,u'type') in self.attrs.getNames():
      for name in u'description htmlUrl language title version xmlUrl'.split():
        if (None, name) in self.attrs.getNames():
          self.log(MissingOutlineType({"parent":self.parent.name, "element":self.name}))
          break

    self.validate_optional_attribute((None,'created'), rfc822)
    self.validate_optional_attribute((None,'description'), safeHtml)
    self.validate_optional_attribute((None,'htmlUrl'), rfc2396_full)
    self.validate_optional_attribute((None,'isBreakpoint'), truefalse)
    self.validate_optional_attribute((None,'isComment'), truefalse)
    self.validate_optional_attribute((None,'language'), iso639)
    self.validate_optional_attribute((None,'title'), safeHtml)
    self.validate_optional_attribute((None,'text'), safeHtml)
    self.validate_optional_attribute((None,'url'), rfc2396_full)

  def characters(self, string):
    if not self.value:
      if string.strip():
        self.log(UnexpectedText({"element":self.name,"parent":self.parent.name}))
        self.value = string
    
  def do_outline(self):
    return opmlOutline()