dc.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Support for the Dublin Core metadata extensions
  2. # Copyright 2010-2023 Kurt McKee <contactme@kurtmckee.org>
  3. # Copyright 2002-2008 Mark Pilgrim
  4. # All rights reserved.
  5. #
  6. # This file is a part of feedparser.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions are met:
  10. #
  11. # * Redistributions of source code must retain the above copyright notice,
  12. # this list of conditions and the following disclaimer.
  13. # * Redistributions in binary form must reproduce the above copyright notice,
  14. # this list of conditions and the following disclaimer in the documentation
  15. # and/or other materials provided with the distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. from ..datetimes import _parse_date
  29. from ..util import FeedParserDict
  30. class Namespace(object):
  31. supported_namespaces = {
  32. 'http://purl.org/dc/elements/1.1/': 'dc',
  33. 'http://purl.org/dc/terms/': 'dcterms',
  34. }
  35. def _end_dc_author(self):
  36. self._end_author()
  37. def _end_dc_creator(self):
  38. self._end_author()
  39. def _end_dc_date(self):
  40. self._end_updated()
  41. def _end_dc_description(self):
  42. self._end_description()
  43. def _end_dc_language(self):
  44. self._end_language()
  45. def _end_dc_publisher(self):
  46. self._end_webmaster()
  47. def _end_dc_rights(self):
  48. self._end_rights()
  49. def _end_dc_subject(self):
  50. self._end_category()
  51. def _end_dc_title(self):
  52. self._end_title()
  53. def _end_dcterms_created(self):
  54. self._end_created()
  55. def _end_dcterms_issued(self):
  56. self._end_published()
  57. def _end_dcterms_modified(self):
  58. self._end_updated()
  59. def _start_dc_author(self, attrs_d):
  60. self._start_author(attrs_d)
  61. def _start_dc_creator(self, attrs_d):
  62. self._start_author(attrs_d)
  63. def _start_dc_date(self, attrs_d):
  64. self._start_updated(attrs_d)
  65. def _start_dc_description(self, attrs_d):
  66. self._start_description(attrs_d)
  67. def _start_dc_language(self, attrs_d):
  68. self._start_language(attrs_d)
  69. def _start_dc_publisher(self, attrs_d):
  70. self._start_webmaster(attrs_d)
  71. def _start_dc_rights(self, attrs_d):
  72. self._start_rights(attrs_d)
  73. def _start_dc_subject(self, attrs_d):
  74. self._start_category(attrs_d)
  75. def _start_dc_title(self, attrs_d):
  76. self._start_title(attrs_d)
  77. def _start_dcterms_created(self, attrs_d):
  78. self._start_created(attrs_d)
  79. def _start_dcterms_issued(self, attrs_d):
  80. self._start_published(attrs_d)
  81. def _start_dcterms_modified(self, attrs_d):
  82. self._start_updated(attrs_d)
  83. def _start_dcterms_valid(self, attrs_d):
  84. self.push('validity', 1)
  85. def _end_dcterms_valid(self):
  86. for validity_detail in self.pop('validity').split(';'):
  87. if '=' in validity_detail:
  88. key, value = validity_detail.split('=', 1)
  89. if key == 'start':
  90. self._save('validity_start', value, overwrite=True)
  91. self._save('validity_start_parsed', _parse_date(value), overwrite=True)
  92. elif key == 'end':
  93. self._save('validity_end', value, overwrite=True)
  94. self._save('validity_end_parsed', _parse_date(value), overwrite=True)
  95. def _start_dc_contributor(self, attrs_d):
  96. self.incontributor = 1
  97. context = self._get_context()
  98. context.setdefault('contributors', [])
  99. context['contributors'].append(FeedParserDict())
  100. self.push('name', 0)
  101. def _end_dc_contributor(self):
  102. self._end_name()
  103. self.incontributor = 0