Exemplo n.º 1
0
    def test_choose_most_significant(self):
        # Mozilla's text in dnd
        mime_type = mime.choose_most_significant(
            ['text/plain', 'text/_moz_htmlcontext', 'text/unicode',
             'text/html', 'text/_moz_htmlinfo'])
        self.assertEqual(mime_type, 'text/html')

        # Mozilla's text in c&v
        mime_type = mime.choose_most_significant(
            ['text/_moz_htmlcontext',
             'STRING',
             'text/html',
             'text/_moz_htmlinfo',
             'text/x-moz-url-priv',
             'UTF8_STRING',
             'COMPOUND_TEXT'])
        self.assertEqual(mime_type, 'text/html')

        # Mozilla gif in dnd
        mime_type = mime.choose_most_significant(
            ['application/x-moz-file-promise-url',
             'application/x-moz-file-promise-dest-filename',
             'text/_moz_htmlinfo', 'text/x-moz-url-desc',
             'text/_moz_htmlcontext', 'text/x-moz-url-data',
             'text/uri-list'])
        self.assertEqual(mime_type, 'text/uri-list')

        # Mozilla url in dnd
        mime_type = mime.choose_most_significant(
            ['text/_moz_htmlcontext',
             'text/html',
             'text/_moz_htmlinfo',
             '_NETSCAPE_URL',
             'text/x-moz-url',
             'text/x-moz-url-desc',
             'text/x-moz-url-data',
             'text/plain',
             'text/unicode'])
        self.assertEqual(mime_type, 'text/x-moz-url')

        # Abiword text in dnd
        mime_type = mime.choose_most_significant(
            ['text/rtf', 'text/uri-list'])
        self.assertEqual(mime_type, 'text/uri-list')

        # Abiword text in c&v
        mime_type = mime.choose_most_significant(
            ['UTF8_STRING', 'STRING', 'text/html', 'TEXT', 'text/rtf',
             'COMPOUND_TEXT', 'application/rtf', 'text/plain',
             'application/xhtml+xml'])
        self.assertEqual(mime_type, 'application/rtf')

        # Abiword text in c&v
        mime_type = mime.choose_most_significant(
            ['GTK_TEXT_BUFFER_CONTENTS',
             'application/x-gtk-text-buffer-rich-text',
             'UTF8_STRING', 'COMPOUND_TEXT', 'TEXT', 'STRING',
             'text/plain;charset=utf-8', 'text/plain;charset=UTF-8',
             'text/plain'])
        self.assertEqual(mime_type, 'text/plain')
Exemplo n.º 2
0
    def _copy_to_journal(self):
        formats = list(self._cb_object.get_formats().keys())
        most_significant_mime_type = mime.choose_most_significant(formats)
        format_ = self._cb_object.get_formats()[most_significant_mime_type]

        transfer_ownership = False
        if most_significant_mime_type == 'text/uri-list':
            uri = format_.get_data()
            if uri.startswith('file://'):
                parsed_url = urllib.parse.urlparse(uri)
                file_path = parsed_url.path  # pylint: disable=E1101
                transfer_ownership = False
                mime_type = mime.get_for_file(file_path)
            else:
                file_path = self._write_to_temp_file(format_.get_data())
                transfer_ownership = True
                mime_type = 'text/uri-list'
        else:
            if format_.is_on_disk():
                parsed_url = urllib.parse.urlparse(format_.get_data())
                file_path = parsed_url.path  # pylint: disable=E1101
                transfer_ownership = False
                mime_type = mime.get_for_file(file_path)
            else:
                file_path = self._write_to_temp_file(format_.get_data())
                transfer_ownership = True
                sniffed_mime_type = mime.get_for_file(file_path)
                if sniffed_mime_type == 'application/octet-stream':
                    mime_type = most_significant_mime_type
                else:
                    mime_type = sniffed_mime_type

        jobject = datastore.create()
        jobject.metadata['title'] = self._cb_object.get_name()
        jobject.metadata['keep'] = '0'
        jobject.metadata['buddies'] = ''
        jobject.metadata['preview'] = ''
        settings = Gio.Settings.new('org.sugarlabs.user')
        color = settings.get_string('color')
        jobject.metadata['icon-color'] = color
        jobject.metadata['mime_type'] = mime_type
        jobject.file_path = file_path

        datastore.write(jobject, transfer_ownership=transfer_ownership)

        return jobject
Exemplo n.º 3
0
    def _copy_to_journal(self):
        formats = self._cb_object.get_formats().keys()
        most_significant_mime_type = mime.choose_most_significant(formats)
        format_ = self._cb_object.get_formats()[most_significant_mime_type]

        transfer_ownership = False
        if most_significant_mime_type == 'text/uri-list':
            uris = mime.split_uri_list(format_.get_data())
            if len(uris) == 1 and uris[0].startswith('file://'):
                parsed_url = urlparse.urlparse(uris[0])
                file_path = parsed_url.path  # pylint: disable=E1101
                transfer_ownership = False
                mime_type = mime.get_for_file(file_path)
            else:
                file_path = self._write_to_temp_file(format_.get_data())
                transfer_ownership = True
                mime_type = 'text/uri-list'
        else:
            if format_.is_on_disk():
                parsed_url = urlparse.urlparse(format_.get_data())
                file_path = parsed_url.path  # pylint: disable=E1101
                transfer_ownership = False
                mime_type = mime.get_for_file(file_path)
            else:
                file_path = self._write_to_temp_file(format_.get_data())
                transfer_ownership = True
                sniffed_mime_type = mime.get_for_file(file_path)
                if sniffed_mime_type == 'application/octet-stream':
                    mime_type = most_significant_mime_type
                else:
                    mime_type = sniffed_mime_type

        jobject = datastore.create()
        jobject.metadata['title'] = self._cb_object.get_name()
        jobject.metadata['keep'] = '0'
        jobject.metadata['buddies'] = ''
        jobject.metadata['preview'] = ''
        client = GConf.Client.get_default()
        color = client.get_string('/desktop/sugar/user/color')
        jobject.metadata['icon-color'] = color
        jobject.metadata['mime_type'] = mime_type
        jobject.file_path = file_path

        datastore.write(jobject, transfer_ownership=transfer_ownership)

        return jobject
Exemplo n.º 4
0
    def _copy_to_journal(self):
        formats = self._cb_object.get_formats().keys()
        most_significant_mime_type = mime.choose_most_significant(formats)
        format_ = self._cb_object.get_formats()[most_significant_mime_type]

        transfer_ownership = False
        if most_significant_mime_type == 'text/uri-list':
            uri = format_.get_data()
            if uri.startswith('file://'):
                parsed_url = urlparse.urlparse(uri)
                file_path = parsed_url.path  # pylint: disable=E1101
                transfer_ownership = False
                mime_type = mime.get_for_file(file_path)
            else:
                file_path = self._write_to_temp_file(format_.get_data())
                transfer_ownership = True
                mime_type = 'text/uri-list'
        else:
            if format_.is_on_disk():
                parsed_url = urlparse.urlparse(format_.get_data())
                file_path = parsed_url.path  # pylint: disable=E1101
                transfer_ownership = False
                mime_type = mime.get_for_file(file_path)
            else:
                file_path = self._write_to_temp_file(format_.get_data())
                transfer_ownership = True
                sniffed_mime_type = mime.get_for_file(file_path)
                if sniffed_mime_type == 'application/octet-stream':
                    mime_type = most_significant_mime_type
                else:
                    mime_type = sniffed_mime_type

        jobject = datastore.create()
        jobject.metadata['title'] = self._cb_object.get_name()
        jobject.metadata['keep'] = '0'
        jobject.metadata['buddies'] = ''
        jobject.metadata['preview'] = ''
        settings = Gio.Settings('org.sugarlabs.user')
        color = settings.get_string('color')
        jobject.metadata['icon-color'] = color
        jobject.metadata['mime_type'] = mime_type
        jobject.file_path = file_path

        datastore.write(jobject, transfer_ownership=transfer_ownership)

        return jobject
Exemplo n.º 5
0
    def _copy_to_journal(self):
        formats = self._cb_object.get_formats().keys()
        most_significant_mime_type = mime.choose_most_significant(formats)
        format_ = self._cb_object.get_formats()[most_significant_mime_type]

        transfer_ownership = False
        if most_significant_mime_type == 'text/uri-list':
            uris = mime.split_uri_list(format_.get_data())
            if len(uris) == 1 and uris[0].startswith('file://'):
                parsed_url = urlparse.urlparse(uris[0])
                file_path = parsed_url.path  # pylint: disable=E1101
                transfer_ownership = False
                mime_type = mime.get_for_file(file_path)
            else:
                file_path = self._write_to_temp_file(format_.get_data())
                transfer_ownership = True
                mime_type = 'text/uri-list'
        else:
            if format_.is_on_disk():
                parsed_url = urlparse.urlparse(format_.get_data())
                file_path = parsed_url.path  # pylint: disable=E1101
                transfer_ownership = False
                mime_type = mime.get_for_file(file_path)
            else:
                file_path = self._write_to_temp_file(format_.get_data())
                transfer_ownership = True
                sniffed_mime_type = mime.get_for_file(file_path)
                if sniffed_mime_type == 'application/octet-stream':
                    mime_type = most_significant_mime_type
                else:
                    mime_type = sniffed_mime_type

        jobject = datastore.create()
        jobject.metadata['title'] = self._cb_object.get_name()
        jobject.metadata['keep'] = '0'
        jobject.metadata['buddies'] = ''
        jobject.metadata['preview'] = ''
        client = GConf.Client.get_default()
        color = client.get_string('/desktop/sugar/user/color')
        jobject.metadata['icon-color'] = color
        jobject.metadata['mime_type'] = mime_type
        jobject.file_path = file_path

        datastore.write(jobject, transfer_ownership=transfer_ownership)

        return jobject
Exemplo n.º 6
0
    def get_mime_type(self):
        if not self._formats:
            return ''

        format_ = mime.choose_most_significant(self._formats.keys())
        if format_ == 'text/uri-list':
            uri_data = self._formats[format_].get_data()
            uri = urlparse.urlparse(uri_data, 'file')
            scheme = uri.scheme  # pylint: disable=E1101
            if scheme == 'file':
                path = uri.path  # pylint: disable=E1101
                if os.path.exists(path):
                    format_ = mime.get_for_file(path)
                else:
                    format_ = mime.get_from_file_name(path)
                logging.debug('Chose %r!', format_)

        return format_
Exemplo n.º 7
0
    def get_mime_type(self):
        if not self._formats:
            return ''

        format_ = mime.choose_most_significant(self._formats.keys())
        if format_ == 'text/uri-list':
            data = self._formats['text/uri-list'].get_data()
            uri = urlparse.urlparse(mime.split_uri_list(data)[0], 'file')
            scheme = uri.scheme  # pylint: disable=E1101
            if scheme == 'file':
                path = uri.path  # pylint: disable=E1101
                if os.path.exists(path):
                    format_ = mime.get_for_file(path)
                else:
                    format_ = mime.get_from_file_name(path)
                logging.debug('Chose %r!', format_)

        return format_