Skip to content

Commit

Permalink
Use importlib; read modules from filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Jan 30, 2021
1 parent 1bd8460 commit d70bedb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 91 deletions.
113 changes: 24 additions & 89 deletions mathics/builtin/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = {}
Expand Down
2 changes: 0 additions & 2 deletions mathics/builtin/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4444,8 +4444,6 @@ class ResetDirectory(Builtin):

def apply(self, evaluation):
"ResetDirectory[]"
global DIRECTORY_STACK

try:
tmp = DIRECTORY_STACK.pop()
except IndexError:
Expand Down

0 comments on commit d70bedb

Please sign in to comment.