config.py :  » Game-2D-3D » Python-Sudoku » pythonsudoku-0.13 » pythonsudoku » 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 » Python Sudoku 
Python Sudoku » pythonsudoku 0.13 » pythonsudoku » config.py
# -*- coding: utf-8 -*-

"""Module with the configuration.

This exports:
  - localedir -- path to the locales
  - options -- options for the program


Copyright (C) 2005-2008  Xos Otero <xoseotero@users.sourceforge.net>

"""

import sys
import os.path
import ConfigParser


__all__ = ["options"]


def _executable_path():
    """Return the path to the executable."""
    return os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0])))

def _exist(directory, filename):
    """Return if a filename is in a directory or in its subdirectories."""

    for root, dirs, files in os.walk(directory, topdown=False):
        if filename in files:
            return True
    return False

def _search_locales():
    """Search the locales directory.

    The localesc are searched in these locations:
    1. UNIX system directories.
    2. Installation path.

    If the locales are not found it returns the current directory.
    """

    localedirs = (os.path.join(_executable_path(), "locale/"),
                  "/usr/share/locale",
                  "/usr/share/games/locale",
                  "/usr/local/share/locale",
                  "/usr/local/share/games/locale")
    for dir in localedirs:
        if _exist(dir, "pythonsudoku.mo"):
            return dir

    # fall back to the current directory
    return "locale/"


class Options(ConfigParser.SafeConfigParser):
    def __init__(self):
        """Options for Python Sudoku."""

        ConfigParser.SafeConfigParser.__init__(self)
        self.load_defaults()
        self.load_user_cfg()

    def load_defaults(self):
        """Set the default values."""

        self.add_section("sudoku")
        self.set("sudoku", "difficulty", "normal")
        self.set("sudoku", "force", "False")
        self.set("sudoku", "handicap", "0")
        self.set("sudoku", "region_width", "3")
        self.set("sudoku", "region_height", "3")
        self.set("sudoku", "use_letters", "True")

        self.add_section("pdf")
        self.set("pdf", "page", "A4")
        self.set("pdf", "lines_colour", "black")
        self.set("pdf", "font", "Helvetica")
        self.set("pdf", "font_colour", "black")
        self.set("pdf", "font_size", "40")
        self.set("pdf", "title_font", "Helvetica")
        self.set("pdf", "title_colour", "black")
        self.set("pdf", "title_size", "72")
        self.set("pdf", "filename_font", "Helvetica")
        self.set("pdf", "filename_colour", "black")
        self.set("pdf", "filename_size", "24")

        self.add_section("print")
        # set the command to print a pdf
        # "" means command not know and so print is not possible
        # For UNIX:
        #self.set("print", "command", "lpr")
        # For MS Windows:
        #self.set("print", "command", "AcroRd32.exe /p /h") # Acrobat Reader
        #self.set("print", "command", "Acrobat.exe /p /h")  # Acrobat Writer
        self.set("print", "command", "")


        self.add_section("image")
        # set the format to always write the images in that format
        # "" means that the filename extension will be used as the format
        self.set("image", "format", "")
        self.set("image", "width", "300")
        self.set("image", "height", "300")
        self.set("image", "background", "white")
        self.set("image", "lines_colour", "black")
        self.set("image", "font",
                 os.path.join(_executable_path(), "data/FreeSans.ttf"))
        self.set("image", "font_colour", "black")
        self.set("image", "font_size", "24")

        self.add_section("gui")
        self.set("gui", "lines_colour", "black")
        self.set("gui", "font_colour", "black")

    def load_user_cfg(self):
        """Update the options with the content of the configuration files.

        The configuration files are searched in these locations:
        1. User's home directory (file .pysdk.cfg in UNIX and pysdk.cfg in the
           rest).
        2. Installation path (file pysdk.cfg).
        3. Current directory (file pysdk.cfg).
        """

        # path to the user configuration
        if os.name == "posix":
            usercfg = os.path.expanduser("~/.pysdk.cfg")
        else:
            usercfg = os.path.expanduser("~/pysdk.cfg")

        self.read((usercfg,
                   os.path.join(_executable_path(), "pysdk.cfg"),
                   "pysdk.cfg"))


# path to the locales
localedir = _search_locales()

# options for the program
options = Options()


# System dependent options
#options.set("print", "command", "COMMAND")
#options.set("image", "font", "PATH_TO_TTF_FONT")
#localedir = "DIRECTORY"
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.