Skip to content

Commit

Permalink
[globals] remove importStar, import stdlib symbols deliberately
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Sep 10, 2024
1 parent cc43b1d commit 85ddd76
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
18 changes: 9 additions & 9 deletions visidata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ def getGlobals():
'Return the VisiData globals dict.'
return globals()

from math import *

from .utils import *

from .extensible import *
Expand Down Expand Up @@ -144,17 +142,19 @@ def importFeatures():

import visidata.deprecated

vd.importStar('builtins')
vd.importStar('copy')
vd.importStar('math')
vd.importStar('random')
vd.importStar('itertools')
vd.importStar('curses')
vd.importModule('copy', 'copy deepcopy'.split())
vd.importModule('builtins', 'abs all any ascii bin bool bytes callable chr complex dict dir divmod enumerate eval filter float format getattr hex int len list map max min next oct ord pow range repr reversed round set sorted str sum tuple type zip'.split())
vd.importModule('math', 'acos acosh asin asinh atan atan2 atanh ceil copysign cos cosh degrees dist erf erfc exp expm1 fabs factorial floor fmod frexp fsum gamma gcd hypot isclose isfinite isinf isnan isqrt lcm ldexp lgamma log log1p log10 log2 modf radians remainder sin sinh sqrt tan tanh trunc prod perm comb nextafter ulp pi e tau inf nan'.split())
vd.importModule('random', 'randrange randint choice choices sample uniform gauss lognormvariate'.split())
vd.importModule('string', 'ascii_letters ascii_lowercase ascii_uppercase digits hexdigits punctuation printable whitespace'.split())
vd.importModule('json')
vd.importModule('itertools')
vd.importModule('curses')

import visidata.experimental # import nothing by default but make package accessible

vd.finalInit() # call all VisiData.init() from modules

importFeatures()

vd.addGlobals(globals())
vd.addGlobals(vd=vd) # globals())
14 changes: 5 additions & 9 deletions visidata/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,17 @@ def loadConfigAndPlugins(vd, args=AttrDict()):


@VisiData.api
def importModule(vd, pkgname):
def importModule(vd, pkgname, symbols=[]):
'Import the given *pkgname*, setting vd.importingModule to *pkgname* before import and resetting to None after.'
modparts = pkgname.split('.')
vd.importingModule = modparts[-1]
r = importlib.import_module(pkgname)
vd.importingModule = None
vd.importedModules.append(r)
vd.addGlobals({pkgname:r})
if symbols:
vd.addGlobals({k:getattr(r, k) for k in symbols if hasattr(r, k)})

return r


Expand All @@ -506,14 +510,6 @@ def importSubmodules(vd, pkgname):
vd.importModule(pkgname + '.' + module.name)


@VisiData.api
def importStar(vd, pkgname):
'Add all symbols from *pkgname* into visidata globals.'
m = vd.importModule(pkgname)
vd.addGlobals({pkgname:m})
vd.addGlobals(m.__dict__)


@VisiData.api
def importExternal(vd, modname, pipmodname=''):
pipmodname = pipmodname or modname
Expand Down

0 comments on commit 85ddd76

Please sign in to comment.