/usr/share/rapid-photo-downloader/rapid/utilities.py is in rapid-photo-downloader 0.4.10-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #!/usr/bin/python
# -*- coding: latin1 -*-
### Copyright (C) 2007-2012 Damon Lynch <damonlynch@gmail.com>
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
### USA
import os
import gio
import gtk
from PIL import Image
import distutils.version
def get_full_path(path):
""" make path relative to home directory if not an absolute path """
if os.path.isabs(path):
return path
else:
return os.path.join(os.path.expanduser('~'), path)
def is_directory(path):
# for some very strange reason, doing it the GIO way fails with
# unknown type, even for directories!
return os.path.isdir(path)
if False:
d = gio.File(path)
if d.query_exists():
file_info = d.query_filesystem_info(attributes="standard::type")
file_type = file_info.get_file_type()
if file_type == gio.FILE_TYPE_DIRECTORY:
return True
return False
def format_size_for_user(bytes, zero_string="", with_decimals=True, kb_only=False):
"""Format an int containing the number of bytes into a string suitable for
printing out to the user. zero_string is the string to use if bytes == 0.
source: https://develop.participatoryculture.org/trac/democracy/browser/trunk/tv/portable/util.py?rev=3993
"""
if bytes > (1 << 30) and not kb_only:
value = (bytes / (1024.0 * 1024.0 * 1024.0))
if with_decimals:
format = "%1.1fGB"
else:
format = "%dGB"
elif bytes > (1 << 20) and not kb_only:
value = (bytes / (1024.0 * 1024.0))
if with_decimals:
format = "%1.1fMB"
else:
format = "%dMB"
elif bytes > (1 << 10):
value = (bytes / 1024.0)
if with_decimals:
format = "%1.1fKB"
else:
format = "%dKB"
elif bytes > 1:
value = bytes
if with_decimals:
format = "%1.1fB"
else:
format = "%dB"
else:
return zero_string
return format % value
def register_iconsets(icon_info):
"""
Register icons in the icon set if they're not already used
From http://faq.pygtk.org/index.py?req=show&file=faq08.012.htp
"""
icon_factory = gtk.IconFactory()
stock_ids = gtk.stock_list_ids()
for stock_id, file in icon_info:
# only load image files when our stock_id is not present
if stock_id not in stock_ids:
pixbuf = gtk.gdk.pixbuf_new_from_file(file)
iconset = gtk.IconSet(pixbuf)
icon_factory.add(stock_id, iconset)
icon_factory.add_default()
def escape(s):
"""
Replace special characters by SGML entities.
"""
entities = ("&&", "<<", ">>")
for e in entities:
s = s.replace(e[0], e[1:])
return s
def image_to_pixbuf(image):
# convert PIL image to pixbuf
# this one handles transparency, unlike the default example in the pygtk FAQ
# this is also from the pygtk FAQ
IS_RGBA = image.mode=='RGBA'
return gtk.gdk.pixbuf_new_from_data(
image.tostring(), # data
gtk.gdk.COLORSPACE_RGB, # color mode
IS_RGBA, # has alpha
8, # bits
image.size[0], # width
image.size[1], # height
(IS_RGBA and 4 or 3) * image.size[0] # rowstride
)
def pixbuf_to_image(pb):
assert(pb.get_colorspace() == gtk.gdk.COLORSPACE_RGB)
dimensions = pb.get_width(), pb.get_height()
stride = pb.get_rowstride()
pixels = pb.get_pixels()
mode = pb.get_has_alpha() and "RGBA" or "RGB"
image = Image.frombuffer(mode, dimensions, pixels,
"raw", mode, stride, 1)
if mode == "RGB":
# convert to having an alpha value, so that the image can
# act as a mask in the drop shadow paste
image = image.convert("RGBA")
return image
def pythonify_version(v):
""" makes version number a version number in distutils sense"""
return distutils.version.StrictVersion(v.replace( '~',''))
def human_readable_version(v):
""" returns a version in human readable form"""
v = v.replace('~a', ' alpha ')
v = v.replace('~b', ' beta ')
v = v.replace('~rc', ' RC ')
return v
|