File: Pictures.py

package info (click to toggle)
mobyle 1.5.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,272 kB
  • ctags: 2,745
  • sloc: python: 22,649; sh: 57; makefile: 31; xml: 6; ansic: 5
file content (35 lines) | stat: -rw-r--r-- 1,677 bytes parent folder | download | duplicates (4)
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
############################################################################################
#                                                                                          #
# The code below is from the pycaptcha-0.4 package ( http://releases.navi.cx/pycaptcha/ )  #
# Copyright (c) 2004 Micah Dowty  (see COPYING in this directory for further details )     #                     #
# and adapted by Bertrand Neron, for the purpose of Mobyle                                 #                     #
#                                                                                          #
############################################################################################

import os, random , glob


class ImageFactory(object):
    """Given a list of files and/or directories, this picks a random file.
       Directories are searched for files matching any of a list of extensions.
       Files are relative to our data directory plus a subclass-specified base path.
       """
    extensions = [".png"]
    picturesPath = os.path.abspath(os.path.join(os.path.dirname(__file__), "data" , "pictures" ) )
    
    def __init__(self, image_category ):
        self.image_category = image_category
        self._fullPaths = None


    def _findFullPaths(self):
        """From our given file list, find a list of full paths to files"""
        return glob.glob( os.path.join( self.picturesPath , self.image_category , '*.png' ) )

    def pick(self):
        if self._fullPaths is None:
            self._fullPaths = self._findFullPaths()
        return random.choice(self._fullPaths)
    
abstract = ImageFactory("abstract")
nature = ImageFactory("nature")