[Syncropated-commits] r155 - trunk/src

zimmerle at garage.maemo.org zimmerle at garage.maemo.org
Mon Jan 29 23:03:13 EET 2007


Author: zimmerle
Date: 2007-01-29 23:03:12 +0200 (Mon, 29 Jan 2007)
New Revision: 155

Added:
   trunk/src/Utils.py
Log:
first Utils.py import

Added: trunk/src/Utils.py
===================================================================
--- trunk/src/Utils.py	2007-01-29 20:44:39 UTC (rev 154)
+++ trunk/src/Utils.py	2007-01-29 21:03:12 UTC (rev 155)
@@ -0,0 +1,163 @@
+# Utils.py
+#
+#  Copyright (c) 2006 INdT (Instituto Nokia de Technologia)
+#
+#  Author: Felipe Zimmerle <felipe at zimmerle.org>
+#          Kenneth Rohde Christiansen <kenneth.christiansen at 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+#  USA
+
+
+import gobject
+import os
+from Icons import Icons
+
+class Utils(gobject.GObject):
+	"""
+	Misc methods used by others misc methods :)
+
+	"""
+
+	# Useful labels
+	(
+		byte,
+		kilo,
+		mega,
+		giga
+	) = range(4)
+	unit_labels_size = ('b','Kb', 'Mb', 'Gb')
+
+	(
+		seconds,
+		minutes,
+		hours,
+	) = range(3)
+	unit_labels_time = ('soconds', 'minutes', 'hours')
+
+
+	def get_friendly_size(self, size, unit=byte):
+		"""
+		Return the size in a readable unit
+
+		If no unit informed, assume the size is in
+		byte.
+
+		"""
+		size = float(size)
+		while (size > 1024):
+			size = float(size / 1024)
+			unit += 1
+		size = str(size)
+		size = size.split(".")[0] + "." +  size.split(".")[1][:2]
+
+		return str(size) + " " + str(Utils.unit_labels_size[unit])
+
+	def get_friendly_time(self, time, unit=seconds):
+		"""
+		Return the time in a readable unit
+
+		If no unit informed, assume the time is in
+		seconds.
+
+		"""
+		time = int(time)
+		rest = 0
+		while (time > 60):
+			last_time = time
+			time = time / 60
+			rest = last_time - (time * 60)
+			unit += 1
+
+		time = str(time)
+		rest = str(rest)
+
+		return time + ':' + rest  + ' ' + str(Utils.unit_labels_time[unit])
+
+	def resize_string(self, string, max_size, warn=True):
+		"""
+		If the string is bigger then max_size,
+		cut out string[:max_size] and return the value.
+
+		If warn is enable (default option), this method will return:
+		`string...` with the "..." in the end of the string.
+
+		"""
+		if warn:
+			max_size = max_size-3
+		if len(string) >= max_size:
+			string = string[:max_size]
+			if warn:
+				string = string + '...'
+
+		return string
+
+	def unicodify(self, string):
+		"""
+		Return the string in unicode format
+
+		"""
+		if string == None:
+			return None
+
+		string.strip("\x00")
+
+		if isinstance(string, unicode):
+			return string
+
+		for codec in ['utf-8', 'latin1']:
+			try:
+				unicode_string = unicode(string.strip("\x00"), codec)
+			except UnicodeDecodeError, e:
+				continue
+			break
+		return unicode_string
+
+
+	def get_glade_fullpath(self, glade_file):
+		"""
+		Return the full path of glade's directory
+
+		../data/
+
+		"""
+		path = os.path.join('..', 'data')
+		path = os.path.join(path, glade_file)
+
+		return path
+
+	def is_path_to(self, dirs, path):
+		"""
+		Return if a path is path to another path :)
+
+		"""
+		for i in dirs:
+			slashed_dir = str(os.path.join(path, ''))
+			if i.startswith(slashed_dir) == True:
+				return True
+		return False
+
+	
+	def is_after_to(self, dirs, path):
+		"""
+		Return True if the `dir` are in `path`
+
+		"""
+		for i in dirs:
+			if os.path.split(path)[0] == i:
+				return True
+		return False
+
+



More information about the Syncropated-commits mailing list