mathmaker  0.6(alpha)
mamk_misc/doc/mathmaker4doxygen/core/base.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 subprocess
00024 from lib import error
00025 from lib.common import software
00026 from lib.common import cfg
00027 
00028 # ------------------------------------------------------------------------------
00029 # --------------------------------------------------------------------------
00030 # ------------------------------------------------------------------------------
00031 ##
00032 # @class Clonable
00033 # @brief All objects that are used must be able to be copied deeply
00034 # Any Clonable are provided the clone() method, no need to reimplement it
00035 class Clonable(object):
00036 
00037 
00038 
00039 
00040 
00041     # --------------------------------------------------------------------------
00042     ##
00043     #   @brief Returns a deep copy of the object
00044     def clone(self):
00045         result = object.__new__(type(self))
00046         result.__init__(self)
00047         return result
00048 
00049 
00050 
00051 
00052 
00053 
00054 # ------------------------------------------------------------------------------
00055 # --------------------------------------------------------------------------
00056 # ------------------------------------------------------------------------------
00057 ##
00058 # @class NamedObject
00059 # @brief Abstract mother class of objects having a name
00060 class NamedObject(Clonable):
00061 
00062 
00063 
00064 
00065 
00066     # --------------------------------------------------------------------------
00067     ##
00068     #   @brief Constructor
00069     #   @warning Must be redefined
00070     def __init__(self):
00071         raise error.MethodShouldBeRedefined(self, "__init__")
00072 
00073 
00074 
00075 
00076 
00077     # --------------------------------------------------------------------------
00078     ##
00079     #   @brief Returns the name of the object
00080     def get_name(self):
00081         return self._name
00082 
00083 
00084 
00085 
00086 
00087     name = property(get_name, doc = "Name of the object")
00088 
00089 
00090 
00091 
00092 
00093     # --------------------------------------------------------------------------
00094     ##
00095     #   @brief Sets the name of the object
00096     def set_name(self, arg):
00097         if not (type(arg) == str or type(arg) == int):
00098             raise error.WrongArgument(str(type(arg)), "str|int")
00099 
00100         self._name = str(arg)
00101 
00102 
00103 
00104 
00105 
00106 # ------------------------------------------------------------------------------
00107 # --------------------------------------------------------------------------
00108 # ------------------------------------------------------------------------------
00109 ##
00110 # @class Printable
00111 # @brief All Printable objects : Exponenteds & others (Equations...)
00112 # Any Printable must reimplement the into_str() method
00113 class Printable(NamedObject):
00114 
00115 
00116 
00117 
00118 
00119     # --------------------------------------------------------------------------
00120     ##
00121     #   @brief Creates a string of the given object in the given ML
00122     #   @param options Any options
00123     #   @return The formated string
00124     def into_str(self, **options):
00125         raise error.MethodShouldBeRedefined(self, 'into_str')
00126 
00127 
00128 
00129 
00130 
00131 # ------------------------------------------------------------------------------
00132 # --------------------------------------------------------------------------
00133 # ------------------------------------------------------------------------------
00134 ##
00135 # @class Drawable
00136 # @brief All Drawable objects. Any Drawable must reimplement into_euk()
00137 # Drawable are not renamable
00138 class Drawable(NamedObject):
00139 
00140 
00141 
00142 
00143 
00144     # --------------------------------------------------------------------------
00145     ##
00146     #   @brief Returns the eukleides filename associated to the triangle
00147     def get_euk_filename(self):
00148         return self._filename + ".euk"
00149 
00150 
00151 
00152 
00153 
00154     # --------------------------------------------------------------------------
00155     ##
00156     #   @brief Returns the eps filename associated to the triangle
00157     def get_eps_filename(self):
00158         return self._filename + ".eps"
00159 
00160 
00161 
00162 
00163 
00164     euk_filename = property(get_euk_filename,
00165                             doc = "Eukleides filename associated to " \
00166                                   + "the right triangle")
00167 
00168     eps_filename = property(get_eps_filename,
00169                             doc = "eps filename associated to " \
00170                                   + "the right triangle")
00171 
00172 
00173 
00174 
00175 
00176     # --------------------------------------------------------------------------
00177     ##
00178     #   @brief Prevents Drawable objects from being renamed, since they get
00179     #          their name from other properties inside them.
00180     def set_name(self, arg):
00181         raise error.ImpossibleAction("rename this object")
00182 
00183 
00184 
00185 
00186 
00187     # --------------------------------------------------------------------------
00188     ##
00189     #   @brief Creates the euk string to put in the file
00190     #   @param options Any options
00191     #   @return The string to put in the picture file
00192     def into_euk(self, **options):
00193         raise error.MethodShouldBeRedefined(self, 'into_euk')
00194 
00195 
00196 
00197 
00198     # --------------------------------------------------------------------------
00199     ##
00200     #   @brief Creates the picture of the drawable object
00201     #   @return Nothing, just creates the picture file
00202     def into_pic(self, **options):
00203         header_comment = "% " + _( \
00204               "%(document_format)s document generated by %(software_ref)s") \
00205               % {'document_format':'eukleides',
00206                  'software_ref':software.NAME_PRINTABLE + " " \
00207                                 + software.VERSION} \
00208               + "\n"
00209 
00210         header_comment += "% "  \
00211                        + _("%(software_ref)s is free software. Its license \
00212 is %(software_license)s.") % {'software_ref' : software.NAME_PRINTABLE,
00213                              'software_license' : software.LICENSE} \
00214                        + "\n"
00215 
00216         header_comment += "% " \
00217                        + _("Further details on %(software_website)s") \
00218                            % {'software_website' : software.WEBSITE} \
00219                        + "\n"
00220 
00221         header_comment += "% " + software.COPYRIGHT \
00222                           + " " + software.AUTHOR +"\n\n"
00223 
00224 
00225         if 'create_pic_files' in options \
00226             and not options['create_pic_files'] in YES:
00227         #___
00228             pass
00229 
00230         else:
00231             f = open(self.euk_filename, 'w')
00232             f.write(header_comment + self.into_euk(**options))
00233             f.close()
00234 
00235         path_to_euktoeps = cfg.get_value_from_file("PATHS", "EUKTOEPS")
00236         options_of_euktoeps = cfg.get_value_from_file("PATHS",
00237                                                       "EUKTOEPS_OPTIONS")
00238 
00239         if 'create_pic_files' in options \
00240             and not options['create_pic_files'] in YES:
00241         #___
00242             pass
00243 
00244         else:
00245             call_euktoeps = subprocess.Popen([path_to_euktoeps,
00246                                               options_of_euktoeps,
00247                                               self.euk_filename
00248                                               ]
00249                                              )
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258