media.py :  » Game-2D-3D » Pyzzle » pyzzle-0.9 » pyzzle » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » Pyzzle 
Pyzzle » pyzzle 0.9 » pyzzle » media.py
import os
import pygame
import pyzzle
import urlparse, urllib2

class Library:
    """An object that stores data from files.  
    The object uses a function, load, to load files within a given folder. 
    If the file is not found, returns a given default file.
    If the file has already been loaded, it returns the already loaded data."""
    def __init__(self, folder, load, default=None, download=None):
        self.folder=folder
        self.default=default
        self.download=download
        self._load=load
        self.loaded={}
    def load(self, file, **param):
        if file in self.loaded:
            return self.loaded[file]
        else:
            path=os.path.join(self.folder, file)
            if os.path.exists(path):
                data=self._load(path, **param)
                self.loaded[file]=data
            elif file==self.default:
                raise 'Could not load default file: '+str(self.default)
            elif not self.default:
                raise 'Could not load file: '+str(file)
            elif self.download:
                base=self.download
                base2 = urlparse.urljoin(base, self.folder)+'/'
                url = urlparse.urljoin(base2, file)
                url = url.replace(' ', '%20')
                url = url.replace('\\', '/')
                request = urllib2.Request(url)
                try:
                    print('downloading from '+url)
                    downloader = urllib2.urlopen(request)
                    download = open(path, "wb")
                    download.write(downloader.read())
                    download.close()
                    data=self._load(path, **param)
                    self.loaded[file]=data
                except urllib2.HTTPError, ex:
                    print(str(ex.code)+' Could not find file: '+str(url))
                    data=self.load(self.default)
            else:
                print('Could not load file: '+str(file))
                data=self.load(self.default)
            return data
    def unload(self, file):
        if file in self.loaded:
            del self.loaded[file]
            
def loadImage(path):            return pygame.image.load(path).convert_alpha()
def loadFont(path, fontSize=16):return pygame.font.Font(path, fontSize)
def loadSound(path):            return pygame.mixer.Sound(path)
def loadMovie(path):            return pygame.movie.Movie(path)
images=Library('pictures',  loadImage, default='default.gif')
movies=Library('Movies',  loadMovie)
cursors=Library('cursors',  loadImage, default='arrow.png')
fonts =Library('fonts',     loadFont)
sounds=Library('sounds/effects',    loadSound, default='../default.wav')
voices=Library('sounds/voices',    loadSound, default='../default.wav')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.