mathmaker  0.6(alpha)
mamk_misc/doc/mathmaker4doxygen/lib/common/cfg.py
00001 # -*- coding: utf-8 -*-
00002 
00003 # Mathmaker creates automatically maths exercises sheets
00004 # with their answers
00005 # Copyright 2006-2014 Nicolas Hainaux <nico_h@users.sourceforge.net>
00006 
00007 # This file is part of Mathmaker.
00008 
00009 # Mathmaker is free software; you can redistribute it and/or modify
00010 # it under the terms of the GNU General Public License as published by
00011 # the Free Software Foundation; either version 3 of the License, or
00012 # any later version.
00013 
00014 # Mathmaker is distributed in the hope that it will be useful,
00015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 # GNU General Public License for more details.
00018 
00019 # You should have received a copy of the GNU General Public License
00020 # along with Mathmaker; if not, write to the Free Software
00021 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 import software
00024 from lib import error
00025 
00026 # CFG FILE DATA
00027 FILE_NAME = software.ROOT_PATH \
00028             + software.NAME + '.' \
00029             + software.CFG_FILE_SUFFIX
00030 COMMENT_TOKEN = '#'
00031 CATEGORY_TOKEN = '['
00032 
00033 
00034 # --------------------------------------------------------------------------
00035 ##
00036 #   @brief Gets the value of the given option.
00037 #   For instance, cfg.get_value_from_file(latex.FORMAT, "ENCODING") gets the
00038 #   encoding value for LaTeX
00039 #   @param  category Options' category (LOCALES, LATEX...)
00040 #   @param  nom Exact name of the option
00041 def get_value_from_file(category, option_name):
00042     category_was_found = False
00043     category_was_found_at_least_once = False
00044 
00045     try:
00046         f = open(FILE_NAME, mode = 'r')
00047     except NameError:
00048         raise error.UnreachableData("the file named : " + str(FILE_NAME))
00049 
00050     for line in f:
00051         # jump over the comment lines of the file
00052         if not line[0] == COMMENT_TOKEN:
00053             # check first if a category was found :
00054             if line[0] == CATEGORY_TOKEN:
00055                 if line[1:len(line)-2] == category:
00056                     category_was_found = True
00057                     category_was_found_at_least_once = True
00058                 else:
00059                     category_was_found = False
00060 
00061             # at this point, no comment nor category token has been found
00062             # on the current line. if a category was previously found, let's
00063             # check if the option we're looking for is on the current line
00064             elif category_was_found:
00065                 if line[0:len(option_name)] == option_name:
00066                     return line[len(option_name)+1:len(line)-1]
00067 
00068     if category_was_found_at_least_once:
00069         raise error.UnreachableData("the option " + option_name               \
00070                                     + " in the category " + category          \
00071                                     + " of the cfg file.")
00072 
00073     else:
00074         raise error.UnreachableData("the category " + category                \
00075                                     + " of the cfg file.")
00076 
00077 
00078 
00079 
00080