From d70bedbb26b03bff593407d4ab9dcd0a9e9af8cb Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 30 Jan 2021 07:32:56 -0500 Subject: [PATCH] Use importlib; read modules from filesystem --- mathics/builtin/__init__.py | 113 ++++++++---------------------------- mathics/builtin/files.py | 2 - 2 files changed, 24 insertions(+), 91 deletions(-) diff --git a/mathics/builtin/__init__.py b/mathics/builtin/__init__.py index c2dbaf412a..adc3a02c6f 100755 --- a/mathics/builtin/__init__.py +++ b/mathics/builtin/__init__.py @@ -1,51 +1,18 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +import glob +import importlib +import re +import os.path as osp +from mathics.settings import ENABLE_FILES_MODULE -from mathics.builtin import ( - algebra, - arithmetic, - assignment, - attributes, - calculus, - combinatorial, - compilation, - comparison, - constants, - control, - datentime, - diffeqns, - evaluation, - exptrig, - functional, - graphics, - graphics3d, - image, - inout, - integer, - iohooks, - linalg, - lists, - logic, - manipulate, - quantities, - numbertheory, - numeric, - options, - patterns, - plot, - physchemdata, - randomnumbers, - recurrence, - specialfunctions, - scoping, - strings, - structure, - system, - tensors, - xmlformat, - optimization, -) +# Get a list of file in this directory. We'll exclude from the start +# files with leading characters we don't want like __init__ with its leading underscore. +__py_files__ = [ + osp.basename(f[0:-3]) + for f in glob.glob(osp.join(osp.dirname(__file__), "[a-z]*.py")) +] from mathics.builtin.base import ( Builtin, @@ -57,55 +24,23 @@ from mathics.settings import ENABLE_FILES_MODULE -modules = [ - algebra, - arithmetic, - assignment, - attributes, - calculus, - combinatorial, - compilation, - comparison, - constants, - control, - datentime, - diffeqns, - evaluation, - exptrig, - functional, - graphics, - graphics3d, - image, - inout, - integer, - iohooks, - linalg, - lists, - logic, - manipulate, - quantities, - numbertheory, - numeric, - options, - patterns, - plot, - physchemdata, - randomnumbers, - recurrence, - specialfunctions, - scoping, - strings, - structure, - system, - tensors, - xmlformat, - optimization, +exclude_files = set(("files", "codetables", "base", "importexport", "colors")) +module_names = [ + f for f in __py_files__ if re.match("^[a-z0-9]+$", f) if f not in exclude_files ] if ENABLE_FILES_MODULE: - from mathics.builtin import files, importexport + module_names += ["files", "importexport"] + +modules = [] - modules += [files, importexport] +for module_name in module_names: + try: + module = importlib.import_module("mathics.builtin." + module_name) + except: + # print("XXX", module_name) + continue + modules.append(module) builtins = [] builtins_by_module = {} diff --git a/mathics/builtin/files.py b/mathics/builtin/files.py index 434e3937ea..13ca8615d8 100644 --- a/mathics/builtin/files.py +++ b/mathics/builtin/files.py @@ -4444,8 +4444,6 @@ class ResetDirectory(Builtin): def apply(self, evaluation): "ResetDirectory[]" - global DIRECTORY_STACK - try: tmp = DIRECTORY_STACK.pop() except IndexError: