本文整理汇总了Python中superdesk.errors.ParserError.nitfParserError方法的典型用法代码示例。如果您正苦于以下问题:Python ParserError.nitfParserError方法的具体用法?Python ParserError.nitfParserError怎么用?Python ParserError.nitfParserError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类superdesk.errors.ParserError
的用法示例。
在下文中一共展示了ParserError.nitfParserError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _update
# 需要导入模块: from superdesk.errors import ParserError [as 别名]
# 或者: from superdesk.errors.ParserError import nitfParserError [as 别名]
def _update(self, provider):
self.provider = provider
self.path = provider.get('config', {}).get('path', None)
if not self.path:
return []
for filename in get_sorted_files(self.path, sort_by=FileSortAttributes.created):
try:
filepath = os.path.join(self.path, filename)
if os.path.isfile(filepath):
stat = os.lstat(filepath)
last_updated = datetime.fromtimestamp(stat.st_mtime, tz=utc)
if self.is_latest_content(last_updated, provider.get('last_updated')):
with open(filepath, 'r') as f:
item = self.parser.parse_message(etree.fromstring(f.read()), provider)
self.move_file(self.path, filename, provider=provider, success=True)
yield [item]
else:
self.move_file(self.path, filename, provider=provider, success=True)
except etreeParserError as ex:
logger.exception("Ingest Type: AAP - File: {0} could not be processed".format(filename))
self.move_file(self.path, filename, provider=provider, success=False)
raise ParserError.nitfParserError(ex, provider)
except ParserError as ex:
self.move_file(self.path, filename, provider=provider, success=False)
except Exception as ex:
self.move_file(self.path, filename, provider=provider, success=False)
raise ProviderError.ingestError(ex, provider)
push_notification('ingest:update')
示例2: parse_message
# 需要导入模块: from superdesk.errors import ParserError [as 别名]
# 或者: from superdesk.errors.ParserError import nitfParserError [as 别名]
def parse_message(self, tree, provider):
item = {}
try:
docdata = tree.find('head/docdata')
# set the default type.
item['type'] = ITEM_CLASS_TEXT
item['guid'] = item['uri'] = docdata.find('doc-id').get('id-string')
item['urgency'] = docdata.find('urgency').get('ed-urg', '5')
item['pubstatus'] = docdata.attrib.get('management-status', 'usable')
item['firstcreated'] = get_norm_datetime(docdata.find('date.issue'))
item['versioncreated'] = get_norm_datetime(docdata.find('date.issue'))
item['expiry'] = get_norm_datetime(docdata.find('date.expire'))
item['subject'] = get_subjects(tree)
item['body_html'] = get_content(tree)
item['place'] = get_places(docdata)
item['keywords'] = get_keywords(docdata)
if docdata.find('ed-msg') is not None:
item['ednote'] = docdata.find('ed-msg').attrib.get('info')
item['headline'] = tree.find('body/body.head/hedline/hl1').text
elem = tree.find('body/body.head/abstract')
item['abstract'] = elem.text if elem is not None else ''
elem = tree.find('body/body.head/dateline/location/city')
item['dateline'] = elem.text if elem is not None else ''
item['byline'] = get_byline(tree)
parse_meta(tree, item)
item.setdefault('word_count', get_word_count(item['body_html']))
return item
except Exception as ex:
raise ParserError.nitfParserError(ex, provider)
示例3: parse_message
# 需要导入模块: from superdesk.errors import ParserError [as 别名]
# 或者: from superdesk.errors.ParserError import nitfParserError [as 别名]
def parse_message(self, tree, provider):
item = {}
try:
docdata = tree.find("head/docdata")
# set the default type.
item["type"] = ITEM_CLASS_TEXT
item["guid"] = item["uri"] = docdata.find("doc-id").get("id-string")
item["urgency"] = docdata.find("urgency").get("ed-urg", "5")
item["pubstatus"] = docdata.attrib.get("management-status", "usable")
item["firstcreated"] = get_norm_datetime(docdata.find("date.issue"))
item["versioncreated"] = get_norm_datetime(docdata.find("date.issue"))
item["expiry"] = get_norm_datetime(docdata.find("date.expire"))
item["subject"] = get_subjects(tree)
item["body_html"] = get_content(tree)
item["place"] = get_places(docdata)
item["keywords"] = get_keywords(docdata)
if docdata.find("ed-msg") is not None:
item["ednote"] = docdata.find("ed-msg").attrib.get("info")
item["headline"] = super().trim_headline(tree.find("body/body.head/hedline/hl1").text)
elem = tree.find("body/body.head/abstract")
item["abstract"] = elem.text if elem is not None else ""
elem = tree.find("body/body.head/dateline/location/city")
item["dateline"] = elem.text if elem is not None else ""
item["byline"] = get_byline(tree)
parse_meta(tree, item)
item.setdefault("word_count", get_word_count(item["body_html"]))
return item
except Exception as ex:
raise ParserError.nitfParserError(ex, provider)
示例4: parse
# 需要导入模块: from superdesk.errors import ParserError [as 别名]
# 或者: from superdesk.errors.ParserError import nitfParserError [as 别名]
def parse(self, xml, provider=None):
item = {}
try:
docdata = xml.find('head/docdata')
# set the default type.
item[ITEM_TYPE] = CONTENT_TYPE.TEXT
item['guid'] = item['uri'] = docdata.find('doc-id').get('id-string')
if docdata.find('urgency') is not None:
item['urgency'] = int(docdata.find('urgency').get('ed-urg', '5'))
item['pubstatus'] = (docdata.attrib.get('management-status', 'usable')).lower()
item['firstcreated'] = get_norm_datetime(docdata.find('date.issue'))
item['versioncreated'] = get_norm_datetime(docdata.find('date.issue'))
if docdata.find('date.expire') is not None:
item['expiry'] = get_norm_datetime(docdata.find('date.expire'))
item['subject'] = get_subjects(xml)
item['body_html'] = get_content(xml)
item['place'] = get_places(docdata)
item['keywords'] = get_keywords(docdata)
if xml.find('head/tobject/tobject.property') is not None:
genre = xml.find('head/tobject/tobject.property').get('tobject.property.type')
genre_map = superdesk.get_resource_service('vocabularies').find_one(req=None, _id='genre')
if genre_map is not None:
item['genre'] = [x for x in genre_map.get('items', []) if x['name'] == genre]
if docdata.find('ed-msg') is not None:
item['ednote'] = docdata.find('ed-msg').attrib.get('info')
if xml.find('body/body.head/hedline/hl1') is not None:
item['headline'] = xml.find('body/body.head/hedline/hl1').text
else:
if xml.find('head/title') is not None:
item['headline'] = xml.find('head/title').text
elem = xml.find('body/body.head/abstract/p')
item['abstract'] = elem.text if elem is not None else ''
if elem is None:
elem = xml.find('body/body.head/abstract')
item['abstract'] = elem.text if elem is not None else ''
elem = xml.find('body/body.head/dateline/location/city')
if elem is not None:
self.set_dateline(item, city=elem.text)
item['byline'] = get_byline(xml)
parse_meta(xml, item)
item.setdefault('word_count', get_word_count(item['body_html']))
return item
except Exception as ex:
raise ParserError.nitfParserError(ex, provider)
示例5: parse
# 需要导入模块: from superdesk.errors import ParserError [as 别名]
# 或者: from superdesk.errors.ParserError import nitfParserError [as 别名]
def parse(self, xml, provider=None):
item = {ITEM_TYPE: CONTENT_TYPE.TEXT, # set the default type.
}
try:
self.do_mapping(item, xml, SETTINGS_MAPPING_PARAM)
elem = xml.find('body/body.head/dateline/location/city')
if elem is not None:
self.set_dateline(item, city=elem.text)
item.setdefault('word_count', get_word_count(item['body_html'], no_html=True))
except Exception as ex:
raise ParserError.nitfParserError(ex, provider)
return item
示例6: test_raise_nitfParserError
# 需要导入模块: from superdesk.errors import ParserError [as 别名]
# 或者: from superdesk.errors.ParserError import nitfParserError [as 别名]
def test_raise_nitfParserError(self):
with assert_raises(ParserError) as error_context:
try:
ex = Exception("Testing nitfParserError")
raise ex
except Exception:
raise ParserError.nitfParserError(ex, self.provider)
exception = error_context.exception
self.assertTrue(exception.code == 1006)
self.assertTrue(exception.message == "NITF input could not be processed")
self.assertIsNotNone(exception.system_exception)
self.assertEqual(exception.system_exception.args[0], "Testing nitfParserError")
self.assertEqual(len(self.mock_logger_handler.messages['error']), 1)
self.assertEqual(self.mock_logger_handler.messages['error'][0],
"ParserError Error 1006 - NITF input could not be processed: "
"Testing nitfParserError on channel TestProvider")
示例7: AAPIngestService
# 需要导入模块: from superdesk.errors import ParserError [as 别名]
# 或者: from superdesk.errors.ParserError import nitfParserError [as 别名]
import os
import logging
from datetime import datetime
from .nitf import NITFParser
from superdesk.io.file_ingest_service import FileIngestService
from superdesk.utc import utc, timezone
from superdesk.notification import push_notification
from superdesk.io import register_provider
from ..etree import etree, ParseError as etreeParserError
from superdesk.utils import get_sorted_files, FileSortAttributes
from superdesk.errors import ParserError, ProviderError
logger = logging.getLogger(__name__)
PROVIDER = 'aap'
errors = [ParserError.nitfParserError().get_error_description(),
ProviderError.ingestError().get_error_description(),
ParserError.parseFileError().get_error_description()]
class AAPIngestService(FileIngestService):
"""AAP Ingest Service"""
def __init__(self):
self.tz = timezone('Australia/Sydney')
self.parser = NITFParser()
def prepare_href(self, href):
return href
def _update(self, provider):
示例8: parse
# 需要导入模块: from superdesk.errors import ParserError [as 别名]
# 或者: from superdesk.errors.ParserError import nitfParserError [as 别名]
def parse(self, xml, provider=None):
if self.metadata_mapping is None:
self._generate_mapping()
item = {ITEM_TYPE: CONTENT_TYPE.TEXT} # set the default type.
try:
for key, mapping in self.metadata_mapping.items():
if not mapping:
# key is ignored
continue
try:
xpath = mapping["xpath"]
except KeyError:
# no xpath, we must have a callable
try:
value = mapping["callback"](xml)
except KeyError:
logging.warn("invalid mapping for key {}, ignoring it".format(key))
continue
except SkipValue:
continue
else:
elem = xml.find(xpath)
if elem is None:
try:
value = mapping["default"]
except KeyError:
# if there is not default value we skip the key
continue
else:
# we have an element,
# do we want a filter, an attribute or the content?
try:
# filter
value = mapping["filter"](elem)
except KeyError:
try:
attribute = mapping["attribute"]
except KeyError:
# content
value = "".join(elem.itertext())
else:
# attribute
value = elem.get(attribute, mapping.get("default_attr"))
try:
# filter_value is applied on found value
value = mapping["filter_value"](value)
except KeyError:
pass
if "key_hook" in mapping:
mapping["key_hook"](item, value)
else:
item[key] = value
elem = xml.find("body/body.head/dateline/location/city")
if elem is not None:
self.set_dateline(item, city=elem.text)
item.setdefault("word_count", get_word_count(item["body_html"]))
return item
except Exception as ex:
raise ParserError.nitfParserError(ex, provider)