# GPixPod - the free and open source way to manage photos on your POD!
# Copyright (C) 2006 Flavio Gargiulo (FLAGAR.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.
 
from struct import *
import time, os
 
def readChunk(file, offset, size=4):
    """ Read a chunk of data from a file """
    file.seek(offset)
    return file.read(size)
 
def unpackChunk(file, offset, format='l'):
    """ Unpack a chunk of data using the given format """
    size = calcsize(format)
    chunk = readChunk(file, offset, size)
    return unpack(format, chunk)
 
def updateChunk(chunk, pos, format, value):
    """ Update part of a chunk with new value with the given format """
    size = calcsize(format)
    return chunk[0:pos]+pack(format, value)+chunk[pos+size:]
 
def getPadding(length):
    """ Calculate the padding on a 4-bytes boundary basis """
    # Maybe it could be more elegant with division and rounding?
    padding = 4 - (length % 4)
    if padding < 4:
        return padding
    else:
        return 0
 
def appleTime():
    """ Get current time in Apple format (+2082844800", Apple time is from 1904-01-01) """
    return int(time.time())+2082844800
 
def getRatioSize(width, height, max_width, max_height):
    """ Recalculate width and height keeping ratio aspect scaling within the size limits """
    # Calculating proper width/height with ratio within limits
    new_width = width
    new_height = height
    if new_width > max_width or new_height > max_height:
        new_width = max_width
        new_height = max_width*height/width
    if new_height > max_height:
        new_height = max_height
        new_width = max_height*width/height
    return (new_width, new_height)
 
def getIpodModel(ipod_mountpoint):
    """ Get iPod model """
    sysinfo_filename = os.path.join(ipod_mountpoint, 'iPod_Control', 'Device', 'SysInfo')
    if os.path.isfile(sysinfo_filename):
        sysinfo_file = open(sysinfo_filename)
        for sysinfo_line in sysinfo_file:
            if sysinfo_line.split(':')[0] == 'boardHwSwInterfaceRev':
	        ipod_mcode = sysinfo_line.split(' ')[1]
    	        # Infos got from http://ipodlinux.org/Generations
	        if ipod_mcode == '0x00010000':
	            return '1G'
	        elif ipod_mcode == '0x00020000':
	            return '2G'
	        elif ipod_mcode == '0x00030001':
	            return '3G'
	        elif ipod_mcode == '0x00040013':
	            return '1G Mini'
	        elif ipod_mcode == '0x00050013':
	            return '4G'
	        elif ipod_mcode == '0x00060000':
	            return 'Photo'
	        elif ipod_mcode == '0x00060004':
	            return 'Color'
	        elif ipod_mcode == '0x00070002':
	            return '2G Mini'
	        elif ipod_mcode == '0x000B0005':
	            return '5G'
	        elif ipod_mcode == '0x000C0005':
	            return 'Nano'
                else:
	            return 'Unrecognized'
    else:
        print "Problems opening %s. Does this file exist? If it doesn't exist, then it should be an iPod shuffle?" % sysinfo_filename
 
 
code/utils-py.txt · Last modified: 2006/04/15 12:43
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki