本文整理汇总了Python中superdesk.errors.FormatterError类的典型用法代码示例。如果您正苦于以下问题:Python FormatterError类的具体用法?Python FormatterError怎么用?Python FormatterError使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FormatterError类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: format
def format(self, article, subscriber):
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
ninjs = {
'_id': article['_id'],
'version': str(article['_current_version']),
'type': self._get_type(article)
}
try:
ninjs['byline'] = self._get_byline(article)
except:
pass
for copy_property in self.direct_copy_properties:
if copy_property in article:
ninjs[copy_property] = article[copy_property]
if 'description' in article:
ninjs['description_text'] = article['description']
if article['type'] == 'composite':
ninjs['associations'] = self._get_associations(article)
return [(pub_seq_num, json.dumps(ninjs, default=json_serialize_datetime_objectId))]
except Exception as ex:
raise FormatterError.ninjsFormatterError(ex, subscriber)
示例2: format
def format(self, article, subscriber):
"""
Formats the article as require by the subscriber
:param dict article: article to be formatted
:param dict subscriber: subscriber receiving the article
:return: tuple (int, str) of publish sequence of the subscriber, formatted article as string
"""
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
body_html = article.get('body_html', '').strip('\r\n')
soup = BeautifulSoup(body_html)
for br in soup.find_all('br'):
# remove the <br> tag
br.replace_with(' {}'.format(br.get_text()))
for p in soup.find_all('p'):
# replace <p> tag with two carriage return
p.replace_with('{}\r\n\r\n'.format(p.get_text()))
article['body_text'] = soup.get_text()
# get the first category and derive the locator
category = next((iter(article.get('anpa_category', []))), None)
if category:
locator = LocatorMapper().map(article, category.get('qcode').upper())
if locator:
article['place'] = [{'qcode': locator, 'name': locator}]
return [(pub_seq_num, superdesk.json.dumps(article, default=json_serialize_datetime_objectId))]
except Exception as ex:
raise FormatterError.bulletinBuilderFormatterError(ex, subscriber)
示例3: format
def format(self, article, subscriber):
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
ninjs = {
'_id': article['_id'],
'version': str(article['_current_version']),
'type': self._get_type(article)
}
try:
ninjs['byline'] = self._get_byline(article)
except:
pass
located = article.get('dateline', {}).get('located', {}).get('city')
if located:
ninjs['located'] = article.get('dateline', {}).get('located', {}).get('city', '')
for copy_property in self.direct_copy_properties:
if copy_property in article:
ninjs[copy_property] = article[copy_property]
if 'description' in article:
ninjs['description_text'] = article['description']
if article[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE:
ninjs['associations'] = self._get_associations(article)
if article.get(EMBARGO):
ninjs['embargoed'] = article.get(EMBARGO).isoformat()
return [(pub_seq_num, json.dumps(ninjs, default=json_serialize_datetime_objectId))]
except Exception as ex:
raise FormatterError.ninjsFormatterError(ex, subscriber)
示例4: format
def format(self, article, subscriber):
"""
Create article in NewsML1.2 format
:param dict article:
:param dict subscriber:
:return [(int, str)]: return a List of tuples. A tuple consist of
publish sequence number and formatted article string.
:raises FormatterError: if the formatter fails to format an article
"""
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
newsml = etree.Element("NewsML")
SubElement(newsml, "Catalog", {'Href': 'http://www.iptc.org/std/catalog/catalog.IptcMasterCatalog.xml'})
news_envelope = SubElement(newsml, "NewsEnvelope")
news_item = SubElement(newsml, "NewsItem")
self._format_news_envelope(article, news_envelope, pub_seq_num)
self._format_identification(article, news_item)
self._format_news_management(article, news_item)
self._format_news_component(article, news_item)
return [(pub_seq_num, self.XML_ROOT + etree.tostring(newsml).decode('utf-8'))]
except Exception as ex:
raise FormatterError.newml12FormatterError(ex, subscriber)
示例5: format
def format(self, article, subscriber, codes=None):
"""
Create article in NewsML G2 format
:param dict article:
:param dict subscriber:
:param list codes: selector codes
:return [(int, str)]: return a List of tuples. A tuple consist of
publish sequence number and formatted article string.
:raises FormatterError: if the formatter fails to format an article
"""
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
is_package = self._is_package(article)
self._message_attrib.update(self._debug_message_extra)
news_message = etree.Element('newsMessage', attrib=self._message_attrib)
self._format_header(article, news_message, pub_seq_num)
item_set = self._format_item(news_message)
if is_package:
item = self._format_item_set(article, item_set, 'packageItem')
self._format_groupset(article, item)
elif article[ITEM_TYPE] in {CONTENT_TYPE.PICTURE, CONTENT_TYPE.AUDIO, CONTENT_TYPE.VIDEO}:
item = self._format_item_set(article, item_set, 'newsItem')
self._format_contentset(article, item)
else:
nitfFormater = NITFFormatter()
nitf = nitfFormater.get_nitf(article, subscriber, pub_seq_num)
newsItem = self._format_item_set(article, item_set, 'newsItem')
self._format_content(article, newsItem, nitf)
return [(pub_seq_num, self.XML_ROOT + etree.tostring(news_message).decode('utf-8'))]
except Exception as ex:
raise FormatterError.newmsmlG2FormatterError(ex, subscriber)
示例6: format
def format(self, article, subscriber, codes=None):
"""
Create article in NewsML1.2 format
:param dict article:
:param dict subscriber:
:param list codes:
:return [(int, str)]: return a List of tuples. A tuple consist of
publish sequence number and formatted article string.
:raises FormatterError: if the formatter fails to format an article
"""
try:
formatted_article = deepcopy(article)
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
self.now = utcnow()
self.string_now = self.now.strftime('%Y%m%dT%H%M%S+0000')
newsml = etree.Element("NewsML", {'Version': '1.2'})
SubElement(newsml, "Catalog", {
'Href': 'http://about.reuters.com/newsml/vocabulary/catalog-reuters-3rdParty-master_catalog.xml'})
news_envelope = SubElement(newsml, "NewsEnvelope")
news_item = SubElement(newsml, "NewsItem")
self._format_news_envelope(formatted_article, news_envelope, pub_seq_num)
self._format_identification(formatted_article, news_item)
self._format_news_management(formatted_article, news_item)
self._format_news_component(formatted_article, news_item)
return [(pub_seq_num, self.XML_ROOT + etree.tostring(newsml).decode('utf-8'))]
except Exception as ex:
raise FormatterError.newml12FormatterError(ex, subscriber)
示例7: format
def format(self, article, subscriber):
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
ninjs = {
'_id': article['_id'],
'version': str(article.get(config.VERSION, 1)),
'type': self._get_type(article)
}
try:
ninjs['byline'] = self._get_byline(article)
except:
pass
located = article.get('dateline', {}).get('located', {})
if located:
ninjs['located'] = located.get('city', '')
for copy_property in self.direct_copy_properties:
if article.get(copy_property) is not None:
ninjs[copy_property] = article[copy_property]
if article.get('body_html'):
ninjs['body_html'] = self.append_body_footer(article)
if article.get('description'):
ninjs['description_html'] = self.append_body_footer(article)
if article[ITEM_TYPE] == CONTENT_TYPE.COMPOSITE:
ninjs['associations'] = self._get_associations(article)
elif article.get('associations', {}):
ninjs['associations'] = self._format_related(article, subscriber)
if article.get(EMBARGO):
ninjs['embargoed'] = article.get(EMBARGO).isoformat()
if article.get('priority'):
ninjs['priority'] = article['priority']
else:
ninjs['priority'] = 5
if article.get('subject'):
ninjs['subject'] = self._get_subject(article)
if article.get('anpa_category'):
ninjs['service'] = self._get_service(article)
if article.get('renditions'):
ninjs['renditions'] = self._get_renditions(article)
if article.get('abstract'):
ninjs['description_text'] = article.get('abstract')
elif article.get('description_text'):
ninjs['description_text'] = article.get('description_text')
return [(pub_seq_num, json.dumps(ninjs, default=json_serialize_datetime_objectId))]
except Exception as ex:
raise FormatterError.ninjsFormatterError(ex, subscriber)
示例8: format
def format(self, article, subscriber, codes=None):
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
nitf = self.get_nitf(article, subscriber, pub_seq_num)
return [(pub_seq_num, self.XML_ROOT + etree.tostring(nitf).decode('utf-8'))]
except Exception as ex:
raise FormatterError.nitfFormatterError(ex, subscriber)
示例9: format
def format(self, article, subscriber, codes=None):
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
ninjs = self._transform_to_ninjs(article, subscriber)
return [(pub_seq_num, json.dumps(ninjs, default=json_serialize_datetime_objectId))]
except Exception as ex:
raise FormatterError.ninjsFormatterError(ex, subscriber)
示例10: format
def format(self, article, subscriber, codes=None):
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
nitf = self.get_nitf(article, subscriber, pub_seq_num)
return [{'published_seq_num': pub_seq_num,
'formatted_item': etree.tostring(nitf, encoding='ascii').decode('ascii'),
'item_encoding': 'ascii'}]
except Exception as ex:
raise FormatterError.nitfFormatterError(ex, subscriber)
示例11: format
def format(self, article, destination, selector_codes=None):
try:
pub_seq_num = superdesk.get_resource_service('output_channels').generate_sequence_number(destination)
nitf = self.get_nitf(article, destination, pub_seq_num)
return pub_seq_num, self.XML_ROOT + etree.tostring(nitf).decode('utf-8')
except Exception as ex:
raise FormatterError.nitfFormatterError(ex, destination)
示例12: format
def format(self, article, subscriber):
"""
Formats the article as require by the subscriber
:param dict article: article to be formatted
:param dict subscriber: subscriber receiving the article
:return: tuple (int, str) of publish sequence of the subscriber, formatted article as string
"""
try:
article['slugline'] = self.append_legal(article=article, truncate=True)
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
body_html = self.append_body_footer(article).strip('\r\n')
soup = BeautifulSoup(body_html, 'html.parser')
if not len(soup.find_all('p')):
for br in soup.find_all('br'):
# remove the <br> tag
br.replace_with(' {}'.format(br.get_text()))
for p in soup.find_all('p'):
# replace <p> tag with two carriage return
for br in p.find_all('br'):
# remove the <br> tag
br.replace_with(' {}'.format(br.get_text()))
para_text = p.get_text().strip()
if para_text != '':
p.replace_with('{}\r\n\r\n'.format(para_text))
else:
p.replace_with('')
article['body_text'] = re.sub(' +', ' ', soup.get_text())
# get the first category and derive the locator
category = next((iter(article.get('anpa_category', []))), None)
if category:
locator = LocatorMapper().map(article, category.get('qcode').upper())
if locator:
article['place'] = [{'qcode': locator, 'name': locator}]
article['first_category'] = category
article['first_subject'] = set_subject(category, article)
odbc_item = {
'id': article.get(config.ID_FIELD),
'version': article.get(config.VERSION),
ITEM_TYPE: article.get(ITEM_TYPE),
PACKAGE_TYPE: article.get(PACKAGE_TYPE, ''),
'headline': article.get('headline', '').replace('\'', '\'\''),
'slugline': article.get('slugline', '').replace('\'', '\'\''),
'data': superdesk.json.dumps(article, default=json_serialize_datetime_objectId).replace('\'', '\'\'')
}
return [(pub_seq_num, json.dumps(odbc_item, default=json_serialize_datetime_objectId))]
except Exception as ex:
raise FormatterError.bulletinBuilderFormatterError(ex, subscriber)
示例13: format
def format(self, article, subscriber, codes=None):
try:
pub_seq_num = superdesk.get_resource_service('subscribers').generate_sequence_number(subscriber)
nitf = self.get_nitf(article, subscriber, pub_seq_num)
strip_elements(nitf, 'body.end')
nitf_string = etree.tostring(nitf, encoding='utf-8').decode()
headers = ['<?xml version=\"1.0\" encoding=\"UTF-8\"?>',
'<!-- <!DOCTYPE nitf SYSTEM \"./nitf-3-3.dtd\"> -->']
return [{
'published_seq_num': pub_seq_num,
'formatted_item': '{}\r\n{}'.format("\r\n".join(headers), nitf_string).
replace(' \n', self.line_ender)}]
except Exception as ex:
raise FormatterError.nitfFormatterError(ex, subscriber)
示例14: format
def format(self, article, destination):
try:
nitf = etree.Element("nitf")
head = SubElement(nitf, "head")
body = SubElement(nitf, "body")
body_head = SubElement(body, "body.head")
body_content = SubElement(body, "body.content")
body_content.text = article['body_html']
body_end = SubElement(body, "body.end")
etree.Element('doc-id', attrib={'id-string': article['guid']})
self.__format_head(article, head)
self.__format_body_head(article, body_head)
self.__format_body_end(article, body_end)
return self.XML_ROOT + str(etree.tostring(nitf))
except Exception as ex:
raise FormatterError.nitfFormatterError(ex, destination)
示例15: format
def format(self, article, destination, selector_codes=None):
try:
pub_seq_num = superdesk.get_resource_service('output_channels').generate_sequence_number(destination)
nitfFormater = NITFFormatter()
nitf = nitfFormater.get_nitf(article, destination, pub_seq_num)
self._message_attrib.update(self._debug_message_extra)
newsMessage = etree.Element('newsMessage', attrib=self._message_attrib)
self._format_header(article, newsMessage, pub_seq_num)
itemSet = self._format_item(newsMessage)
if article['type'] == 'text' or article['type'] == 'preformatted':
self._format_newsitem(article, itemSet, nitf)
return pub_seq_num, self.XML_ROOT + etree.tostring(newsMessage).decode('utf-8')
except Exception as ex:
raise FormatterError.newmsmlG2FormatterError(ex, destination)