def test_transmit_closes_subscriber(self):
        def mock_transmit(*args):
            raise PublishQueueError.bad_schedule_error()

        with self.app.app_context():
            subscriber = self.app.data.find('subscribers', None, None)[0]
            output_channel = self.app.data.find('output_channels', None, None)[0]
            publish_service = PublishService()
            publish_service._transmit = mock_transmit
            with assert_raises(PublishQueueError):
                publish_service.transmit(self.queue_items[0], None, subscriber, None, output_channel)
            subscriber = self.app.data.find('subscribers', None, None)[0]
            self.assertFalse(subscriber.get('is_active'))
            self.assertIsNotNone(subscriber.get('last_closed'))
            self.assertTrue(output_channel.get('is_active'))
            self.assertIsNone(output_channel.get('last_closed'))
Exemplo n.º 2
0
    def test_transmit_closes_subscriber(self):
        def mock_transmit(*args):
            raise PublishQueueError.bad_schedule_error()

        with self.app.app_context():
            subscriber = self.app.data.find("subscribers", None, None)[0]

            publish_service = PublishService()
            publish_service._transmit = mock_transmit

            with assert_raises(PublishQueueError):
                publish_service.transmit(self.queue_items[0])

            subscriber = self.app.data.find("subscribers", None, None)[0]
            self.assertFalse(subscriber.get("is_active"))
            self.assertIsNotNone(subscriber.get("last_closed"))
Exemplo n.º 3
0
    def test_transmit_closes_subscriber(self):
        def mock_transmit(*args):
            raise PublishQueueError.bad_schedule_error()

        with self.app.app_context():
            subscriber = self.app.data.find('subscribers', None, None)[0]

            publish_service = PublishService()
            publish_service._transmit = mock_transmit

            with assert_raises(PublishQueueError):
                publish_service.transmit(self.queue_items[0])

            subscriber = self.app.data.find('subscribers', None, None)[0]
            self.assertFalse(subscriber.get('is_active'))
            self.assertIsNotNone(subscriber.get('last_closed'))
    def test_close_subscriber_does_close(self):
        with self.app.app_context():
            subscriber = self.app.data.find_one("subscribers", None)
            self.assertTrue(subscriber.get("is_active"))

            PublishService().close_transmitter(subscriber, PublishQueueError.bad_schedule_error())
            subscriber = self.app.data.find_one("subscribers", None)
            self.assertFalse(subscriber.get("is_active"))
Exemplo n.º 5
0
    def test_close_subscriber_doesnt_close(self):
        with self.app.app_context():
            subscriber = self.app.data.find_one('subscribers', None)
            self.assertTrue(subscriber.get('is_active'))

            PublishService().close_transmitter(subscriber, PublishQueueError.unknown_format_error())
            subscriber = self.app.data.find_one('subscribers', None)
            self.assertTrue(subscriber.get('is_active'))
Exemplo n.º 6
0
 def _transmit(self, queue_item, subscriber):
     try:
         config = queue_item['destination']['config']
         file_path = config['file_path']
         if not path.isabs(file_path):
             file_path = "/" + file_path
         with open(path.join(file_path, PublishService.get_filename(queue_item)), 'wb') as f:
             f.write(queue_item['encoded_item'])
     except Exception as ex:
         raise PublishFileError.fileSaveError(ex, config)
Exemplo n.º 7
0
 def get_filename(cls, queue_item):
     orig = PublishService.get_filename(queue_item)
     name, ext = os.path.splitext(orig)
     try:
         item = etree.fromstring(queue_item['formatted_item'].encode('utf-8'))
         filename = item.find('ContentItem').find('FileName').text
         return '{}{}'.format(filename, ext)
     except (etree.XMLSyntaxError, AttributeError):
         pass
     return '{}{}'.format('-'.join(name.split('-')[:-2]), ext)
Exemplo n.º 8
0
 def _transmit(self, queue_item, subscriber):
     try:
         config = queue_item['destination']['config']
         file_path = config['file_path']
         if not path.isabs(file_path):
             file_path = "/" + file_path
         with open(path.join(file_path, PublishService.get_filename(queue_item)), 'wb') as f:
             f.write(queue_item['encoded_item'])
     except Exception as ex:
         raise PublishFileError.fileSaveError(ex, config)
Exemplo n.º 9
0
 def get_filename(cls, queue_item):
     orig = PublishService.get_filename(queue_item)
     name, ext = os.path.splitext(orig)
     try:
         item = etree.fromstring(
             queue_item["formatted_item"].encode("utf-8"))
         filename = item.find("ContentItem").find("FileName").text
         return "{}{}".format(filename, ext)
     except (etree.XMLSyntaxError, AttributeError):
         pass
     return "{}{}".format("-".join(name.split("-")[:-2]), ext)
Exemplo n.º 10
0
    def _transmit(self, queue_item, subscriber):
        config = queue_item.get('destination', {}).get('config', {})

        try:
            with ftp_connect(config) as ftp:
                filename = PublishService.get_filename(queue_item)
                b = BytesIO(queue_item['encoded_item'])
                ftp.storbinary("STOR " + filename, b)
        except PublishFtpError:
            raise
        except Exception as ex:
            raise PublishFtpError.ftpError(ex, config)
Exemplo n.º 11
0
    def _transmit(self, queue_item, subscriber):
        config = queue_item.get('destination', {}).get('config', {})

        try:
            with ftp_connect(config) as ftp:
                filename = PublishService.get_filename(queue_item)
                b = BytesIO(queue_item['encoded_item'])
                ftp.storbinary("STOR " + filename, b)
        except PublishFtpError:
            raise
        except Exception as ex:
            raise PublishFtpError.ftpError(ex, config)
Exemplo n.º 12
0
            # the value of image/media is not used yet but may be in the future
            featured = data.get('_featured')
            if mime_type is None:
                # these default values need to be used if mime_type is not found
                mime_type = 'image/jpeg' if type_ == 'image' or type_ == 'grafikk' else 'video/mpeg'
            caption = self.strip_invalid_chars(data.get('description_text'))
            self._add_media(body_content, type_, mime_type, source, caption,
                            featured)
        self._add_meta_media_counter(head, len(media_data))

    def _format_body_end(self, article, body_end):
        try:
            emails = [s.strip() for s in article['sign_off'].split('/')]
        except KeyError:
            return
        if emails:
            tagline = etree.SubElement(body_end, 'tagline')
            previous = None
            for email in emails:
                if not email:
                    continue
                a = etree.SubElement(tagline, 'a',
                                     {'href': 'mailto:{}'.format(email)})
                a.text = email
                if previous is not None:
                    previous.tail = '/'
                previous = a


PublishService.register_file_extension(NTBNITFFormatter.FORMAT_TYPE, 'xml')
Exemplo n.º 13
0
                continue
            mime_type = data.get('mimetype')
            if mime_type is None:
                # these default values need to be used if mime_type is not found
                mime_type = 'image/jpeg' if type_ == 'image' or type_ == 'grafikk' else 'video/mpeg'
            caption = self.strip_invalid_chars(data.get(
                'description_text', ''))
            self._add_media(body_content, type_, mime_type, source, caption)
        self._add_meta_media_counter(head, len(media_data))

    def _format_body_end(self, article, body_end):
        try:
            emails = [s.strip() for s in article['sign_off'].split('/')]
        except KeyError:
            return
        if emails:
            tagline = etree.SubElement(body_end, 'tagline')
            previous = None
            for email in emails:
                if not email:
                    continue
                a = etree.SubElement(tagline, 'a',
                                     {'href': 'mailto:{}'.format(email)})
                a.text = email
                if previous is not None:
                    previous.tail = '/'
                previous = a


PublishService.register_file_extension('ntbnitf', 'xml')
Exemplo n.º 14
0
from .ntb_nitf import NTBNITFFormatter


class NTBNITFMultiServiceFormatter(NTBNITFFormatter):
    """This NITF formatter generates single file with all services."""

    FORMAT_TYPE = 'ntbnitf'  # default ntb nitf formatter

    def _format_service(self, article):
        try:
            service_names = ", ".join(
                service.get("name", "")
                for service in article['anpa_category'])
            return service_names
        except (KeyError):
            pass


class NTBNITFMultiServiceMediaFormatter(NTBNITFMultiServiceFormatter):

    FORMAT_TYPE = 'ntbnitfmedia'

    def _get_media_source(self, data):
        return self._get_original_href(data)


PublishService.register_file_extension(
    NTBNITFMultiServiceFormatter.FORMAT_TYPE, 'xml')
PublishService.register_file_extension(
    NTBNITFMultiServiceMediaFormatter.FORMAT_TYPE, 'xml')