本文整理汇总了Python中sugar3.mime.get_for_file函数的典型用法代码示例。如果您正苦于以下问题:Python get_for_file函数的具体用法?Python get_for_file怎么用?Python get_for_file使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_for_file函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: is_file_encrypted
def is_file_encrypted(filepath):
if DISABLE_SECURITY is True:
return False
filepath = filepath.replace('file://', '')
mimebook = mime.get_for_file(filepath)
return (mimebook == 'application/x-ceibal')
示例2: create_journal_entry
def create_journal_entry(self, widget, data=None):
filename = self._filechooser.get_filename()
journal_entry = datastore.create()
journal_entry.metadata['title'] = self.make_new_filename(filename)
journal_entry.metadata['title_set_by_user'] = '1'
journal_entry.metadata['keep'] = '0'
file_mimetype = mime.get_for_file(filename)
if not file_mimetype is None:
journal_entry.metadata['mime_type'] = file_mimetype
journal_entry.metadata['buddies'] = ''
if file_mimetype.startswith('image/') and file_mimetype != 'image/vnd.djvu':
preview = self.create_preview_metadata(filename)
elif file_mimetype == 'application/x-cbz':
fname = self.extract_image(filename)
preview = self.create_preview_metadata(fname)
os.remove(fname)
else:
preview = ''
if not preview == '':
journal_entry.metadata['preview'] = dbus.ByteArray(preview)
else:
journal_entry.metadata['preview'] = ''
journal_entry.file_path = filename
datastore.write(journal_entry)
self.update_log_entries += '\n' + _('File %s copied to the Journal.') % filename
self.alert(_('Success'), _('%s added to Journal.')
% self.make_new_filename(filename))
示例3: __add_cover_response_cb
def __add_cover_response_cb(self, alert, response_id, operation_function):
if response_id == Gtk.ResponseType.YES:
try:
chooser = ObjectChooser(self, what_filter='Image',
filter_type=FILTER_TYPE_GENERIC_MIME,
show_preview=True)
except:
# for compatibility with older versions
chooser = ObjectChooser(self, what_filter='Image')
try:
result = chooser.run()
if result == Gtk.ResponseType.ACCEPT:
logging.error('ObjectChooser: %r' %
chooser.get_selected_object())
jobject = chooser.get_selected_object()
if jobject and jobject.file_path:
logging.error("imagen seleccionada: %s",
jobject.file_path)
mime_type = mime.get_for_file(jobject.file_path)
extension = mime.get_primary_extension(mime_type)
tempfile_name = \
os.path.join(
self.get_activity_root(), 'instance',
'tmp%i.%s' % (time.time(), extension))
os.link(jobject.file_path, tempfile_name)
operation_function(tempfile_name)
finally:
chooser.destroy()
del chooser
elif response_id == Gtk.ResponseType.NO:
self._save_epub()
self.remove_alert(alert)
示例4: load_file
def load_file(self, jobject, title=None):
if isinstance(jobject, datastore.RawObject):
logging.debug('Loading a datastore.RawObject')
file_path = mime_path = jobject.file_path
title = jobject.metadata['title']
elif isinstance(jobject, datastore.DSObject):
# This file is stored in the Journal (datastore)
logging.debug('Loading a datastore.DSObject')
file_path = 'journal://' + jobject.object_id
mime_path = datastore.get(jobject.object_id).file_path
title = jobject.metadata['title']
else:
logging.debug('Loading a %s', type(jobject))
file_path = mime_path = jobject
mimetype = mime.get_for_file('file://' + mime_path)
logging.info('read_file mime %s', mimetype)
if mimetype == 'audio/x-mpegurl':
# is a M3U playlist:
self._load_m3u_playlist(file_path)
else:
# is not a M3U playlist
self._load_stream(file_path, title)
missing_tracks = self._get_missing_tracks()
if len(missing_tracks) > 0:
logging.info('%s tracks not found', len(missing_tracks))
self.emit('missing-tracks', missing_tracks)
# set the focus in the first row
self._set_cursor(0)
示例5: _copy_to_journal
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
示例6: _copy_to_journal
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
示例7: create_preview_metadata
def create_preview_metadata(self, filename):
file_mimetype = mime.get_for_file(filename)
if not file_mimetype.startswith('image/'):
return ''
scaled_pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(filename,
style.zoom(320), style.zoom(240))
preview_data = []
succes, preview_data = scaled_pixbuf.save_to_bufferv('png', [], [])
preview_data = ''.join(preview_data)
return preview_data
示例8: __player_error_cb
def __player_error_cb(self, widget, message, detail):
self.player.stop()
self.control.set_disabled()
logging.error('ERROR MESSAGE: %s', message)
logging.error('ERROR DETAIL: %s', detail)
file_path = self.playlist_widget._items[
self.playlist_widget.get_current_playing()]['path']
mimetype = mime.get_for_file(file_path)
title = _('Error')
msg = _('This "%s" file can\'t be played') % mimetype
self._switch_canvas(False)
self._show_error_alert(title, msg)
示例9: _copy_file
def _copy_file(self, original_uri):
uri = urlparse.urlparse(original_uri)
path = uri.path # pylint: disable=E1101
directory_, file_name = os.path.split(path)
root, ext = os.path.splitext(file_name)
if not ext or ext == '.':
mime_type = mime.get_for_file(path)
ext = '.' + mime.get_primary_extension(mime_type)
f_, new_file_path = tempfile.mkstemp(ext, root)
del f_
shutil.copyfile(path, new_file_path)
os.chmod(new_file_path, 0644)
return 'file://' + new_file_path
示例10: _guess_mime
def _guess_mime(self, file_name):
logging.error('_guess_mime %s', file_name)
mime_type = None
if file_name.endswith('.html') or file_name.endswith('.htm'):
mime_type = 'application/xhtml+xml'
elif file_name.endswith('.css'):
mime_type = 'text/css'
elif file_name.endswith('.png'):
mime_type = 'image/png'
elif file_name.endswith('.jpg') or file_name.endswith('.jpeg'):
mime_type = 'image/jpeg'
elif file_name.endswith('.gif'):
mime_type = 'image/gif'
if mime is None:
mime_type = mime.get_for_file(file_name)
return mime_type
示例11: __friend_selected_cb
def __friend_selected_cb(self, menu_item, buddy):
logging.debug("__friend_selected_cb")
file_name = model.get_file(self._metadata["uid"])
if not file_name or not os.path.exists(file_name):
logging.warn("Entries without a file cannot be sent.")
self.emit("volume-error", _("Entries without a file cannot be sent."), _("Warning"))
return
title = str(self._metadata["title"])
description = str(self._metadata.get("description", ""))
mime_type = str(self._metadata["mime_type"])
if not mime_type:
mime_type = mime.get_for_file(file_name)
filetransfer.start_transfer(buddy, file_name, title, description, mime_type)
示例12: update_preview_cb
def update_preview_cb(self, file_chooser, preview):
filename = file_chooser.get_preview_filename()
try:
file_mimetype = mime.get_for_file(filename)
if file_mimetype == 'application/x-cbz' or file_mimetype == 'application/zip':
fname = self.extract_image(filename)
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(fname,
style.zoom(320), style.zoom(240))
preview.set_from_pixbuf(pixbuf)
have_preview = True
os.remove(fname)
else:
have_preview = False
except:
have_preview = False
file_chooser.set_preview_widget_active(have_preview)
return
示例13: _set_file_path
def _set_file_path(self, file_path):
self._file_path = file_path
if self._file_path is None:
self._show_no_file()
return
mime_type = mime.get_for_file(self._file_path)
if 'image/' in mime_type:
self._show_image_viewer(image=True)
elif 'audio/' in mime_type:
self._show_image_viewer(icon='audio-x-generic')
elif 'video/' in mime_type:
self._show_image_viewer(icon='video-x-generic')
else:
response = self._show_text_viewer()
if not response:
self._show_image_viewer(icon='application-x-generic')
示例14: get_mime_type
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_
示例15: __keep_in_journal_cb
def __keep_in_journal_cb(self, menu_item):
mime_type = mime.get_from_file_name(self._document_path)
if mime_type == 'application/octet-stream':
mime_type = mime.get_for_file(self._document_path)
self._jobject = datastore.create()
title = _('Source') + ': ' + self._title
self._jobject.metadata['title'] = title
self._jobject.metadata['keep'] = '0'
self._jobject.metadata['buddies'] = ''
self._jobject.metadata['preview'] = ''
self._jobject.metadata['icon-color'] = self._color
self._jobject.metadata['mime_type'] = mime_type
self._jobject.metadata['source'] = '1'
self._jobject.file_path = self._document_path
datastore.write(self._jobject, transfer_ownership=True,
reply_handler=self.__internal_save_cb,
error_handler=self.__internal_save_error_cb)