PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/feedvalidator/opensearch.py

https://github.com/dh-benamor/restful-openerp
Python | 148 lines | 146 code | 2 blank | 0 comment | 0 complexity | 1a65d18e2924e1e446d632f497f73d3c MD5 | raw file
  1. from validators import *
  2. from logging import *
  3. import re
  4. class OpenSearchDescription(validatorBase):
  5. def __init__(self):
  6. self.exampleFound = 0
  7. validatorBase.__init__(self)
  8. def validate(self):
  9. name=self.name.replace("opensearch_",'')
  10. if not "ShortName" in self.children:
  11. self.log(MissingElement({"parent":name, "element":"ShortName"}))
  12. if not "Description" in self.children:
  13. self.log(MissingElement({"parent":name, "element":"Description"}))
  14. if not "Url" in self.children:
  15. self.log(MissingElement({"parent":name, "element":"Url"}))
  16. if not self.exampleFound:
  17. self.log(ShouldIncludeExample({}))
  18. def do_ShortName(self):
  19. return lengthLimitedText(16), noduplicates()
  20. def do_Description(self):
  21. return lengthLimitedText(1024), noduplicates()
  22. def do_Url(self):
  23. return Url()
  24. def do_Contact(self):
  25. return addr_spec(), noduplicates()
  26. def do_Tags(self):
  27. return lengthLimitedText(256), noduplicates()
  28. def do_LongName(self):
  29. return lengthLimitedText(48), noduplicates()
  30. def do_Image(self):
  31. return Image()
  32. def do_Query(self):
  33. return Query()
  34. def do_Developer(self):
  35. return lengthLimitedText(64), noduplicates()
  36. def do_Attribution(self):
  37. return lengthLimitedText(256), noduplicates()
  38. def do_SyndicationRight(self):
  39. return SyndicationRight(), noduplicates()
  40. def do_AdultContent(self):
  41. return AdultContent(), noduplicates()
  42. def do_Language(self):
  43. return Language()
  44. def do_InputEncoding(self):
  45. return Charset()
  46. def do_OutputEncoding(self):
  47. return Charset()
  48. class Url(validatorBase):
  49. def getExpectedAttrNames(self):
  50. return [(None,attr) for attr in ['template', 'type', 'indexOffset',
  51. 'pageOffset']]
  52. def prevalidate(self):
  53. self.validate_required_attribute((None,'template'), Template())
  54. self.validate_required_attribute((None,'type'), MimeType)
  55. self.validate_optional_attribute((None,'indexOffset'), Integer)
  56. self.validate_optional_attribute((None,'pageOffset'), Integer)
  57. class Template(rfc2396_full):
  58. tparam = re.compile("{((?:[-a-zA-Z0-9._~]|%[a-fA-F0-9]{2})+:?(?:[-a-zA-Z0-9._~]|%[a-fA-F0-9]{2})*)\??}")
  59. valuelist = ['searchTerms', 'count', 'startIndex', 'startPage', 'language',
  60. 'inputEncoding', 'outputEncoding']
  61. def validate(self):
  62. for pname in self.tparam.findall(self.value):
  63. if pname.find(':')<0:
  64. if pname not in self.valuelist:
  65. self.log(InvalidLocalParameter({'value':pname}))
  66. else:
  67. prefix,name = pname.split(':',1)
  68. if not self.parent.namespaceFor(prefix):
  69. self.log(UndeclaredPrefix({'value':prefix}))
  70. self.value = self.tparam.sub(r'\1',self.value)
  71. rfc2396_full.validate(self)
  72. class Image(rfc2396_full):
  73. def getExpectedAttrNames(self):
  74. return [(None,attr) for attr in ['height', 'width', 'type']]
  75. def prevalidate(self):
  76. self.validate_required_attribute((None,'height'), nonNegativeInteger)
  77. self.validate_required_attribute((None,'width'), nonNegativeInteger)
  78. self.validate_required_attribute((None,'type'), MimeType)
  79. class Query(validatorBase):
  80. def getExpectedAttrNames(self):
  81. return [(None,attr) for attr in ['role', 'title', 'totalResults',
  82. 'searchTerms', 'count', 'startIndex', 'startPage', 'language',
  83. 'inputEncoding', 'outputEncoding', 'parameter']]
  84. def prevalidate(self):
  85. self.validate_required_attribute((None,'role'), QueryRole)
  86. self.validate_optional_attribute((None,'title'), lengthLimitedText(256))
  87. self.validate_optional_attribute((None,'title'), nonhtml)
  88. self.validate_optional_attribute((None,'totalResults'), nonNegativeInteger)
  89. self.validate_optional_attribute((None,'searchTerms'), UrlEncoded)
  90. self.validate_optional_attribute((None,'count'), nonNegativeInteger)
  91. self.validate_optional_attribute((None,'startIndex'), Integer)
  92. self.validate_optional_attribute((None,'startPage'), Integer)
  93. self.validate_optional_attribute((None,'language'), iso639)
  94. self.validate_optional_attribute((None,'inputEncoding'), Charset)
  95. self.validate_optional_attribute((None,'outputEncoding'), Charset)
  96. if self.attrs.has_key((None,"role")) and \
  97. self.attrs.getValue((None,"role")) == "example":
  98. self.parent.exampleFound = 1
  99. class QueryRole(enumeration):
  100. error = InvalidLocalRole
  101. valuelist = ['request', 'example', 'related', 'correction', 'subset',
  102. 'superset']
  103. def validate(self):
  104. if self.value.find(':')<0:
  105. enumeration.validate(self)
  106. else:
  107. prefix,name = self.value.split(':',1)
  108. if not self.parent.namespaceFor(prefix):
  109. self.log(UndeclaredPrefix({'value':prefix}))
  110. class UrlEncoded(validatorBase):
  111. def validate(self):
  112. from urllib import quote, unquote
  113. import re
  114. for value in self.value.split():
  115. if type(value) == unicode: value = value.encode('utf-8')
  116. value = re.sub('%\w\w', lambda x: x.group(0).upper(), value)
  117. if value != quote(unquote(value)):
  118. self.log(NotURLEncoded({}))
  119. break
  120. class SyndicationRight(enumeration):
  121. error = InvalidSyndicationRight
  122. valuelist = ['open','limited','private','closed']
  123. def validate(self):
  124. self.value = self.value.lower()
  125. enumeration.validate(self)
  126. class AdultContent(enumeration):
  127. error = InvalidAdultContent
  128. valuelist = ['false', 'FALSE', '0', 'no', 'NO',
  129. 'true', 'TRUE', '1', 'yes', 'YES']
  130. class Language(iso639):
  131. def validate(self):
  132. if self.value != '*':
  133. iso639.validate(self)