PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/feedvalidator/media.py

https://github.com/dh-benamor/restful-openerp
Python | 356 lines | 352 code | 4 blank | 0 comment | 3 complexity | 7d8968a062050b4ac62b6a0cc214080c MD5 | raw file
  1. from validators import *
  2. class media_elements:
  3. def do_media_adult(self):
  4. self.log(DeprecatedMediaAdult({"parent":self.name, "element":"media:adult"}))
  5. return truefalse(), noduplicates()
  6. def do_media_category(self):
  7. return media_category()
  8. def do_media_copyright(self):
  9. return media_copyright(), noduplicates()
  10. def do_media_credit(self):
  11. return media_credit()
  12. def do_media_description(self):
  13. return media_title(), noduplicates()
  14. def do_media_keywords(self):
  15. return text()
  16. def do_media_hash(self):
  17. return media_hash()
  18. def do_media_player(self):
  19. return media_player()
  20. def do_media_rating(self):
  21. return media_rating()
  22. def do_media_restriction(self):
  23. return media_restriction()
  24. def do_media_text(self):
  25. return media_text()
  26. def do_media_title(self):
  27. return media_title(), noduplicates()
  28. def do_media_thumbnail(self):
  29. return media_thumbnail()
  30. class media_category(nonhtml,rfc2396_full):
  31. def getExpectedAttrNames(self):
  32. return [(None,u'label'),(None, u'scheme')]
  33. def prevalidate(self):
  34. self.name = "label"
  35. self.value = self.attrs.get((None,u'label'))
  36. if self.value: nonhtml.validate(self)
  37. self.name = "scheme"
  38. self.value = self.attrs.get((None,u'scheme'))
  39. if self.value: rfc2396_full.validate(self)
  40. self.name = "media_category"
  41. self.value = ""
  42. class media_copyright(nonhtml,rfc2396_full):
  43. def getExpectedAttrNames(self):
  44. return [(None,u'url')]
  45. def prevalidate(self):
  46. self.name = "url"
  47. self.value = self.attrs.get((None,u'url'))
  48. if self.value: rfc2396_full.validate(self)
  49. self.name = "media_copyright"
  50. self.value = ""
  51. class media_credit(text,rfc2396_full):
  52. EBU = [
  53. "actor", "adaptor", "anchor person", "animal trainer", "animator",
  54. "announcer", "armourer", "art director", "artist/performer",
  55. "assistant camera", "assistant chief lighting technician",
  56. "assistant director", "assistant producer", "assistant visual editor",
  57. "author", "broadcast assistant", "broadcast journalist", "camera operator",
  58. "carpenter", "casting", "causeur", "chief lighting technician", "choir",
  59. "choreographer", "clapper loader", "commentary or commentator",
  60. "commissioning broadcaster", "composer", "computer programmer",
  61. "conductor", "consultant", "continuity checker", "correspondent",
  62. "costume designer", "dancer", "dialogue coach", "director",
  63. "director of photography", "distribution company", "draughtsman",
  64. "dresser", "dubber", "editor/producer", "editor", "editor", "ensemble",
  65. "executive producer", "expert", "fight director", "floor manager",
  66. "focus puller", "foley artist", "foley editor", "foley mixer",
  67. "graphic assistant", "graphic designer", "greensman", "grip",
  68. "hairdresser", "illustrator", "interviewed guest", "interviewer",
  69. "key character", "key grip", "key talents", "leadman", "librettist",
  70. "lighting director", "lighting technician", "location manager",
  71. "lyricist", "make up artist", "manufacturer", "matte artist",
  72. "music arranger", "music group", "musician", "news reader", "orchestra",
  73. "participant", "photographer", "post", "producer", "production assistant",
  74. "production company", "production department", "production manager",
  75. "production secretary", "programme production researcher",
  76. "property manager", "publishing company", "puppeteer", "pyrotechnician",
  77. "reporter", "rigger", "runner", "scenario", "scenic operative",
  78. "script supervisor", "second assistant camera",
  79. "second assistant director", "second unit director", "set designer",
  80. "set dresser", "sign language", "singer", "sound designer", "sound mixer",
  81. "sound recordist", "special effects", "stunts", "subtitles",
  82. "technical director", "term", "translation", "transportation manager",
  83. "treatment/programme proposal", "vision mixer", "visual editor",
  84. "visual effects", "wardrobe", "witness",
  85. # awaiting confirmation
  86. "artist", "performer", "editor", "producer", "treatment",
  87. "treatment proposal", "programme proposal",
  88. ]
  89. def getExpectedAttrNames(self):
  90. return [(None, u'role'),(None,u'scheme')]
  91. def prevalidate(self):
  92. scheme = self.attrs.get((None, 'scheme')) or 'urn:ebu'
  93. role = self.attrs.get((None, 'role'))
  94. if role:
  95. if scheme=='urn:ebu' and role not in self.EBU:
  96. self.log(InvalidCreditRole({"parent":self.parent.name, "element":self.name, "attr":"role", "value":role}))
  97. elif role != role.lower():
  98. self.log(InvalidCreditRole({"parent":self.parent.name, "element":self.name, "attr":"role", "value":role}))
  99. self.value = scheme
  100. self.name = "scheme"
  101. if scheme != 'urn:ebu': rfc2396_full.validate(self)
  102. self.name = "media_credit"
  103. self.value = ""
  104. class media_hash(text):
  105. def getExpectedAttrNames(self):
  106. return [(None,u'algo')]
  107. def prevalidate(self):
  108. self.algo = self.attrs.get((None, 'algo'))
  109. if self.algo and self.algo not in ['md5', 'sha-1']:
  110. self.log(InvalidMediaHash({"parent":self.parent.name, "element":self.name, "attr":"algo", "value":self.algo}))
  111. def validate(self):
  112. self.value = self.value.strip()
  113. if not re.match("^[0-9A-Za-z]+$",self.value):
  114. self.log(InvalidMediaHash({"parent":self.parent.name, "element":self.name, "value":self.value}))
  115. else:
  116. if self.algo == 'sha-1':
  117. if len(self.value) != 40:
  118. self.log(InvalidMediaHash({"parent":self.parent.name, "element":self.name, "algo":self.algo, "value":self.value}))
  119. else:
  120. if len(self.value) != 32:
  121. self.log(InvalidMediaHash({"parent":self.parent.name, "element":self.name, "algo":self.algo, "value":self.value}))
  122. class media_rating(rfc2396_full):
  123. def getExpectedAttrNames(self):
  124. return [(None, u'scheme')]
  125. def validate(self):
  126. scheme = self.attrs.get((None, 'scheme')) or 'urn:simple'
  127. if scheme == 'urn:simple':
  128. if self.value not in ['adult', 'nonadult']:
  129. self.log(InvalidMediaRating({"parent":self.parent.name, "element":self.name, "scheme":scheme, "value":self.value}))
  130. elif scheme == 'urn:mpaa':
  131. if self.value not in ['g', 'm', 'nc-17', 'pg', 'pg-13', 'r', 'x']:
  132. self.log(InvalidMediaRating({"parent":self.parent.name, "element":self.name, "scheme":scheme, "value":self.value}))
  133. elif scheme == 'urn:v-chip':
  134. if self.value not in ['14+', '18+', 'c', 'c8', 'g', 'pg',
  135. 'tv-14', 'tv-g', 'tv-ma', 'tv-pg', 'tv-y', 'tv-y7', 'tv-y7-fv']:
  136. self.log(InvalidMediaRating({"parent":self.parent.name, "element":self.name, "scheme":scheme, "value":self.value}))
  137. elif scheme == 'urn:icra':
  138. code = '([nsvlocx]z [01]|(n[a-c]|s[a-f]|v[a-j]|l[a-c]|o[a-h]|c[a-b]|x[a-e]) 1)'
  139. if not re.match(r"^r \(%s( %s)*\)$" %(code,code),self.value):
  140. self.log(InvalidMediaRating({"parent":self.parent.name, "element":self.name, "scheme":scheme, "value":self.value}))
  141. pass
  142. else:
  143. self.value = scheme
  144. self.name = 'scheme'
  145. rfc2396_full.validate(self)
  146. class media_restriction(text,rfc2396_full,iso3166):
  147. def getExpectedAttrNames(self):
  148. return [(None, u'relationship'),(None,u'type')]
  149. def validate(self):
  150. relationship = self.attrs.get((None, 'relationship'))
  151. if not relationship:
  152. self.log(MissingAttribute({"parent":self.parent.name, "element":self.name, "attr":"relationship"}))
  153. elif relationship not in ['allow','disallow']:
  154. self.log(InvalidMediaRestrictionRel({"parent":self.parent.name, "element":self.name, "attr":"relationship", "value":relationship}))
  155. type = self.attrs.get((None, 'type'))
  156. if not type:
  157. if self.value and self.value not in ['all','none']:
  158. self.log(InvalidMediaRestriction({"parent":self.parent.name, "element":self.name, "value":self.value}))
  159. elif type == 'country':
  160. self.name = 'country'
  161. countries = self.value.upper().split(' ')
  162. for self.value in countries:
  163. iso3166.validate(self)
  164. elif type == 'uri':
  165. rfc2396_full.validate(self)
  166. else:
  167. self.log(InvalidMediaRestrictionType({"parent":self.parent.name, "element":self.name, "attr":"type", "value":type}))
  168. class media_player(validatorBase,positiveInteger,rfc2396_full):
  169. def getExpectedAttrNames(self):
  170. return [(None,u'height'),(None,u'url'),(None, u'width')]
  171. def validate(self):
  172. self.value = self.attrs.get((None, 'url'))
  173. if self.value:
  174. self.name = "url"
  175. rfc2396_full.validate(self)
  176. else:
  177. self.log(MissingAttribute({"parent":self.parent.name, "element":self.name, "attr":"url"}))
  178. self.value = self.attrs.get((None, 'height'))
  179. self.name = "height"
  180. if self.value: positiveInteger.validate(self)
  181. self.value = self.attrs.get((None, 'width'))
  182. self.name = "width"
  183. if self.value: positiveInteger.validate(self)
  184. class media_text(nonhtml):
  185. def getExpectedAttrNames(self):
  186. return [(None,u'end'),(None,u'lang'),(None,u'start'),(None, u'type')]
  187. def prevalidate(self):
  188. self.type = self.attrs.get((None, 'type'))
  189. if self.type and self.type not in ['plain', 'html']:
  190. self.log(InvalidMediaTextType({"parent":self.parent.name, "element":self.name, "attr":"type", "value":self.type}))
  191. start = self.attrs.get((None, 'start'))
  192. if start and not media_thumbnail.npt_re.match(start):
  193. self.log(InvalidNPTTime({"parent":self.parent.name, "element":self.name, "attr":"start", "value":start}))
  194. else:
  195. self.log(ValidNPTTime({"parent":self.parent.name, "element":self.name, "attr":"start", "value":start}))
  196. end = self.attrs.get((None, 'end'))
  197. if end and not media_thumbnail.npt_re.match(end):
  198. self.log(InvalidNPTTime({"parent":self.parent.name, "element":self.name, "attr":"end", "value":end}))
  199. else:
  200. self.log(ValidNPTTime({"parent":self.parent.name, "element":self.name, "attr":"end", "value":end}))
  201. lang = self.attrs.get((None, 'lang'))
  202. if lang: iso639_validate(self.log,lang,'lang',self.parent)
  203. def validate(self):
  204. if self.type == 'html':
  205. self.validateSafe(self.value)
  206. else:
  207. nonhtml.validate(self, ContainsUndeclaredHTML)
  208. class media_title(nonhtml):
  209. def getExpectedAttrNames(self):
  210. return [(None, u'type')]
  211. def prevalidate(self):
  212. self.type = self.attrs.get((None, 'type'))
  213. if self.type and self.type not in ['plain', 'html']:
  214. self.log(InvalidMediaTextType({"parent":self.parent.name, "element":self.name, "attr":"type", "value":self.type}))
  215. def validate(self):
  216. if self.type == 'html':
  217. self.validateSafe(self.value)
  218. else:
  219. nonhtml.validate(self, ContainsUndeclaredHTML)
  220. class media_thumbnail(validatorBase,positiveInteger,rfc2396_full):
  221. npt_re = re.compile("^(now)|(\d+(\.\d+)?)|(\d+:\d\d:\d\d(\.\d+)?)$")
  222. def getExpectedAttrNames(self):
  223. return [(None,u'height'),(None,u'time'),(None,u'url'),(None, u'width')]
  224. def validate(self):
  225. time = self.attrs.get((None, 'time'))
  226. if time and not media_thumbnail.npt_re.match(time):
  227. self.log(InvalidNPTTime({"parent":self.parent.name, "element":self.name, "attr":"time", "value":time}))
  228. else:
  229. self.log(ValidNPTTime({"parent":self.parent.name, "element":self.name, "attr":"time", "value":time}))
  230. self.value = self.attrs.get((None, 'url'))
  231. if self.value:
  232. self.name = "url"
  233. rfc2396_full.validate(self)
  234. else:
  235. self.log(MissingAttribute({"parent":self.parent.name, "element":self.name, "attr":"url"}))
  236. self.value = self.attrs.get((None, 'height'))
  237. self.name = "height"
  238. if self.value: positiveInteger.validate(self)
  239. self.value = self.attrs.get((None, 'width'))
  240. self.name = "width"
  241. if self.value: positiveInteger.validate(self)
  242. from extension import extension_everywhere
  243. class media_content(validatorBase, media_elements, extension_everywhere,
  244. positiveInteger, rfc2396_full, truefalse, nonNegativeInteger):
  245. def getExpectedAttrNames(self):
  246. return [
  247. (None,u'bitrate'),
  248. (None,u'channels'),
  249. (None,u'duration'),
  250. (None,u'expression'),
  251. (None,u'fileSize'),
  252. (None,u'framerate'),
  253. (None,u'height'),
  254. (None,u'isDefault'),
  255. (None,u'lang'),
  256. (None,u'medium'),
  257. (None,u'samplingrate'),
  258. (None,u'type'),
  259. (None,u'url'),
  260. (None,u'width')
  261. ]
  262. def validate(self):
  263. self.value = self.attrs.get((None,u'bitrate'))
  264. if self.value and not re.match('\d+\.?\d*', self.value):
  265. self.log(InvalidFloat({"parent":self.parent.name, "element":self.name,
  266. "attr": 'bitrate', "value":self.value}))
  267. self.value = self.attrs.get((None, 'channels'))
  268. self.name = "channels"
  269. if self.value: nonNegativeInteger.validate(self)
  270. self.value = self.attrs.get((None,u'duration'))
  271. if self.value and not re.match('\d+\.?\d*', self.value):
  272. self.log(InvalidFloat({"parent":self.parent.name, "element":self.name,
  273. "attr": 'duration', "value":self.value}))
  274. self.value = self.attrs.get((None,u'expression'))
  275. if self.value and self.value not in ['sample', 'full', 'nonstop']:
  276. self.log(InvalidMediaExpression({"parent":self.parent.name, "element":self.name, "value": self.value}))
  277. self.value = self.attrs.get((None, 'fileSize'))
  278. self.name = "fileSize"
  279. if self.value: positiveInteger.validate(self)
  280. self.value = self.attrs.get((None,u'framerate'))
  281. if self.value and not re.match('\d+\.?\d*', self.value):
  282. self.log(InvalidFloat({"parent":self.parent.name, "element":self.name,
  283. "attr": 'framerate', "value":self.value}))
  284. self.value = self.attrs.get((None, 'height'))
  285. self.name = "height"
  286. if self.value: positiveInteger.validate(self)
  287. self.value = self.attrs.get((None, 'isDefault'))
  288. if self.value: truefalse.validate(self)
  289. self.value = self.attrs.get((None, 'lang'))
  290. if self.value: iso639_validate(self.log,self.value,'lang',self.parent)
  291. self.value = self.attrs.get((None,u'medium'))
  292. if self.value and self.value not in ['image', 'audio', 'video', 'document', 'executable']:
  293. self.log(InvalidMediaMedium({"parent":self.parent.name, "element":self.name, "value": self.value}))
  294. self.value = self.attrs.get((None,u'samplingrate'))
  295. if self.value and not re.match('\d+\.?\d*', self.value):
  296. self.log(InvalidFloat({"parent":self.parent.name, "element":self.name,
  297. "attr": 'samplingrate', "value":self.value}))
  298. self.value = self.attrs.get((None,u'type'))
  299. if self.value and not mime_re.match(self.value):
  300. self.log(InvalidMIMEAttribute({"parent":self.parent.name, "element":self.name, "attr":'type'}))
  301. self.name = "url"
  302. self.value = self.attrs.get((None,u'url'))
  303. if self.value: rfc2396_full.validate(self)
  304. self.value = self.attrs.get((None, 'width'))
  305. self.name = "width"
  306. if self.value: positiveInteger.validate(self)
  307. class media_group(validatorBase, media_elements):
  308. def do_media_content(self):
  309. return media_content()
  310. def validate(self):
  311. if len([child for child in self.children if child=='media_content']) < 2:
  312. self.log(MediaGroupWithoutAlternatives({}))