NZMATH  1.2.0
About: NZMATH is a Python based number theory oriented calculation system.
  Fossies Dox: NZMATH-1.2.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

config.py
Go to the documentation of this file.
1 """
2 config --- NZMATH configurations
3 """
4 
5 import os
6 import sys
7 import warnings
8 
9 WINDOWS_PLATFORMS = ('win32', 'win64', 'cli', )
10 
12  print("NZMATH [Version 1.2.0]")
13 
14 # ----------------
15 # Default Settings
16 # ----------------
17 #
18 # Dependencies
19 # ============
20 #
21 # Some third party / platform dependent modules are possibly used,
22 # and they are configurable.
23 #
24 # mpmath
25 # ------
26 #
27 # mpmath is a package providing multiprecision math.
28 # http://code.google.com/p/mpmath
29 # This package is used in ecpp module.
30 #
31 # If you have mpmath installed, set as the following:
32 #HAVE_MPMATH = True
33 #CHECK_MPMATH = False
34 # Or, if you don't have mpmath installed, set as the following:
35 #HAVE_MPMATH = False
36 #CHECK_MPMATH = False
37 # The default values mean "I don't know; check it later":
38 HAVE_MPMATH = None
39 CHECK_MPMATH = True
40 
41 #
42 # sqlite3
43 # -------
44 #
45 # sqlite3 is the default database module for Python, but it need to be
46 # enabled at the build time.
47 #
48 # If you have sqlite3 available, set as the following:
49 #HAVE_SQLITE3 = True
50 #CHECK_SQLITE3 = False
51 # Or, if you don't have sqlite3, set as the following:
52 #HAVE_SQLITE3 = False
53 #CHECK_SQLITE3 = False
54 # The default values mean "I don't know; check it later":
55 HAVE_SQLITE3 = None
56 CHECK_SQLITE3 = True
57 
58 
59 #
60 # net availability
61 # ----------------
62 #
63 # Some functions will connect to the Net.
64 # Desktop machines are usually connected to the Net, but notebooks
65 # may have connectivity only occasionally.
66 #
67 # If sometimes you have net connectivity, set as the following:
68 HAVE_NET = True
69 CHECK_NET = False
70 # Or, if your machine is not connected, set as the following:
71 #HAVE_NET = False
72 #CHECK_NET = False
73 # Or you can let NZMATH check connectivity every time, set as the following:
74 #HAVE_NET = None
75 #CHECK_NET = True
76 
77 #
78 # psyco
79 # -----
80 #
81 # psyco is a package providing JIT compiler for Python.
82 # http://psyco.sourceforge.net/
83 # The package is usable only on x86 CPUs.
84 # This package is used in factor/mpqs module.
85 #
86 # If you have psyco installed, and would like to use it fully, set:
87 #HAVE_PSYCO = True
88 #CHECK_PSYCO = False
89 # If you would like to disable it no matter whether it is available:
90 #HAVE_PSYCO = False
91 #CHECK_PSYCO = False
92 # The default values mean "I don't know, check it and use it if possible":
93 HAVE_PSYCO = None
94 CHECK_PSYCO = True
95 
96 #
97 # plug-ins
98 # ========
99 #
100 # math
101 # ----
102 # Python standard float/comple types and math/cmath modules only
103 # provide fixed precision (double precision), but sometimes
104 # multiprecision floating point is needed.
105 # If you have mpmath installed, set HAVE_MPMATH True and use:
106 #PLUGIN_MATH = 'mpmath'
107 #CHECK_PLUGIN_MATH = False
108 # Otherwise, use only Python float/complex as default (but use mpmath
109 # if possible):
110 PLUGIN_MATH = None
111 CHECK_PLUGIN_MATH = True
112 
113 #
114 # Assumptions
115 # ===========
116 #
117 # Some conjectures are useful for assuring the validity of a fast
118 # algorithm.
119 #
120 # All assumptions are default to False, but you can set them True if
121 # you believe them.
122 #
123 # GRH
124 # ---
125 #
126 # Generalized Riemann Hypothesis.
127 # For example, primality test is O((log n)**2) if GRH is true
128 # while O((log n)**6) or something without it.
129 #
130 # If you believe GRH as the truth:
131 #GRH = True
132 # The default is, of course, conservatively doubting it:
133 GRH = False
134 
135 #
136 # Files
137 # =====
138 #
139 # data directory
140 # --------------
141 #
142 # The directory where nzmath (static) data files are stored.
143 # The default will be os.path.join(sys.prefix, 'share', 'nzmath')
144 # or os.path.join(sys.prefix, 'Data', 'nzmath') on Windows.
145 #
146 # If your *nix computer installs NZMATH as a system program:
147 #DATADIR = '/usr/share/nzmath'
148 #CHECK_DATADIR = False
149 # If it is an optional program:
150 #DATADIR = '/usr/local/share/nzmath'
151 #CHECK_DATADIR = False
152 # Windows users may have more aggressive path:
153 #DATADIR = r'C:\Python25\Data'
154 #CHECK_DATADIR = False
155 # The default values mean "I don't know; check it later":
156 DATADIR = None
157 CHECK_DATADIR = True
158 
159 
160 # -------------------
161 # User Configurations
162 # -------------------
163 
164 confdir = os.environ.get('NZMATHCONFDIR', None)
165 if confdir is None:
166  if sys.platform in WINDOWS_PLATFORMS:
167  # "C:\Documents and Settings\%USERNAME%\Application Data\nzmath"
168  # APPDIR = "C:\Documents and Settings\%USERNAME%\Application Data"
169  appdir = os.environ.get('APPDATA', None)
170  # USERPROFILE = "C:\Documents and Settings\%USERNAME%"
171  profdir = os.environ.get('USERPROFILE', None)
172  if appdir is not None:
173  t_confdir = os.path.join(appdir, 'nzmath')
174  elif profdir is not None:
175  t_confdir = os.path.join(profdir, 'Application Data', 'nzmath')
176  else:
177  # "~/.nzmath.d/"
178  homedir = os.environ.get('HOME', None)
179  if homedir is not None:
180  t_confdir = os.path.join(homedir, '.nzmath.d')
181 
182  # nzmathconf.py exists?
183  if os.path.exists(os.path.join(t_confdir, 'nzmathconf.py')):
184  confdir = t_confdir
185 
186 if confdir is not None and os.path.exists(confdir):
187  sys.path.insert(0, confdir)
188 else:
189  sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config'))
190 
191 try:
192  # overwrite the default settings with user's nzmathconf
193  from nzmathconf import *
194 except ImportError:
195  warnings.warn("nzmathconf.py not found")
196 
197 
198 # ------
199 # Checks
200 # ------
201 #
202 # mpmath
203 # ------
204 #
205 
207  """
208  Check if mpmath is importable or not
209  """
210  try:
211  import mpmath
212  return True
213  except ImportError:
214  return False
215 
216 if CHECK_MPMATH:
217  HAVE_MPMATH = check_mpmath()
218 
219 #
220 # sqlite3
221 # -------
222 #
223 
225  """
226  Check if sqlite3 is importable or not.
227  pysqlite2 may be a substitution.
228  """
229  try:
230  try:
231  import sqlite3
232  return True
233  except ImportError:
234  import pysqlite2.dbapi2 as sqlite3
235  return True
236  except ImportError:
237  return False
238 
239 if CHECK_SQLITE3:
240  HAVE_SQLITE3 = check_sqlite3()
241 
242 
243 #
244 # net availability
245 # ----------------
246 #
247 def check_net():
248  """
249  Check the net connection by http call.
250  """
251  import urllib2
252  try:
253  urllib2.urlopen('http://sourceforge.net/projects/nzmath/')
254  return True
255  except urllib2.HTTPError:
256  # the problem is on server side, thus connected anyway
257  return True
258  except urllib2.URLError:
259  # no dns, thus no connection
260  return False
261  except Exception:
262  # I don't know the reason, but something wrong
263  return False
264 
265 if CHECK_NET:
266  HAVE_NET = check_net()
267 
268 #
269 # psyco availability
270 # ------------------
271 #
273  """
274  Check if psyco is importable or not.
275  """
276  try:
277  import psyco
278  return True
279  except ImportError:
280  return False
281 
282 if CHECK_PSYCO:
283  HAVE_PSYCO = check_psyco()
284 
285 #
286 # math plug-in
287 #
289  """
290  Return 'mpmath' if HAVE_MPMATH, None otherwise.
291  """
292  if HAVE_MPMATH:
293  return 'mpmath'
294  else:
295  return None
296 
297 if CHECK_PLUGIN_MATH:
298  PLUGIN_MATH = check_plugin_math()
299 
300 
301 #
302 # data directory
303 # --------------
304 #
306  candidates = []
307 
308  # developing environment
309  candidates.append(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'dist', 'data'))
310 
311  if DATADIR is not None:
312  candidates.append(DATADIR)
313  if sys.platform in WINDOWS_PLATFORMS:
314  candidates.append(os.path.join(sys.prefix, 'Data', 'nzmath'))
315  else:
316  candidates.append(os.path.join(sys.prefix, 'share', 'nzmath'))
317  # default install script will install here ...hmm
318  candidates.append(os.path.join(sys.prefix, 'nzmath'))
319 
320  for canddir in candidates:
321  if os.path.exists(canddir):
322  return canddir
323  return None
324 
325 if CHECK_DATADIR:
326  DATADIR = default_datadir()
327  if DATADIR is None:
328  warnings.warn('no datadir found')
329 
330 
331 # Declare exported variables.
332 
333 __all__ = ['HAVE_MPMATH', 'HAVE_SQLITE3', 'HAVE_NET', 'HAVE_PSYCO',
334  'PLUGIN_MATH', 'GRH', 'DATADIR']
nzmath.config.check_plugin_math
def check_plugin_math()
Definition: config.py:288
nzmath.config.default_datadir
def default_datadir()
Definition: config.py:305
nzmath.config.check_mpmath
def check_mpmath()
Definition: config.py:206
nzmath.config.check_sqlite3
def check_sqlite3()
Definition: config.py:224
nzmath.config.check_psyco
def check_psyco()
Definition: config.py:272
nzmath.config.check_net
def check_net()
Definition: config.py:247
nzmath.config.nzmath_info
def nzmath_info()
Definition: config.py:11