new bpy function bpy.path.module_names(path, recursive=False)

addon's and python initialization both had this inline.
This commit is contained in:
Campbell Barton 2010-09-08 04:55:37 +00:00
parent 5b428e9158
commit 1a41d2fc29
3 changed files with 45 additions and 35 deletions

@ -173,3 +173,33 @@ def ensure_ext(filepath, ext, case_sensitive=False):
else:
return filepath + ext
def module_names(path, recursive=False):
"""
Return a list of modules which can be imported from *path*.
:arg path: a directory to scan.
:type path: string
:arg recursive: Also return submodule names for packages.
:type recursive: bool
:return: a list of strings.
:rtype: list
"""
from os.path import join, isfile
modules = []
for filename in sorted(_os.listdir(path)):
if filename.endswith(".py") and filename != "__init__.py":
modules.append(filename[0:-3])
elif ("." not in filename):
directory = join(path, filename)
if isfile(join(directory, "__init__.py")):
modules.append(filename)
if recursive:
for mod_name in module_names(directory, True):
modules.append("%s.%s" % (filename, mod_name))
return modules

@ -70,16 +70,8 @@ def modules_from_path(path, loaded_modules):
modules = []
for f in sorted(_os.listdir(path)):
if f.endswith(".py"):
# python module
mod = _test_import(f[0:-3], loaded_modules)
elif ("." not in f) and (_os.path.isfile(_os.path.join(path, f, "__init__.py"))):
# python package
mod = _test_import(f, loaded_modules)
else:
mod = None
for mod_name in _bpy.path.module_names(path):
mod = _test_import(mod_name, loaded_modules)
if mod:
modules.append(mod)
@ -280,7 +272,7 @@ def smpte_from_seconds(time, fps=None):
'''
Returns an SMPTE formatted string from the time in seconds: "HH:MM:SS:FF".
If the fps is not given the current scene is used.
If the *fps* is not given the current scene is used.
'''
import math
@ -312,7 +304,7 @@ def smpte_from_frame(frame, fps=None, fps_base=None):
'''
Returns an SMPTE formatted string from the frame: "HH:MM:SS:FF".
If the fps and fps_base are not given the current scene is used.
If *fps* and *fps_base* are not given the current scene is used.
'''
if fps is None:

@ -875,31 +875,19 @@ class USERPREF_PT_addons(bpy.types.Panel):
modules_stale = set(USERPREF_PT_addons._addons_fake_modules.keys())
for path in paths:
for f in sorted(os.listdir(path)):
if f.endswith(".py"):
mod_name = f[0:-3]
mod_path = os.path.join(path, f)
elif ("." not in f) and (os.path.isfile(os.path.join(path, f, "__init__.py"))):
mod_name = f
mod_path = os.path.join(path, f, "__init__.py")
else:
mod_name = ""
mod_path = ""
for mod_name in bpy.path.module_names(path):
modules_stale -= {mod_name}
mod = USERPREF_PT_addons._addons_fake_modules.get(mod_name)
if mod:
if mod.__time__ != os.path.getmtime(mod_path):
print("Reloading", mod_name, mod.__time__, os.path.getmtime(mod_path), mod_path)
del USERPREF_PT_addons._addons_fake_modules[mod_name]
mod = None
if mod_name:
if mod_name in modules_stale:
modules_stale.remove(mod_name)
mod = USERPREF_PT_addons._addons_fake_modules.get(mod_name)
if mod is None:
mod = fake_module(mod_name, mod_path)
if mod:
if mod.__time__ != os.path.getmtime(mod_path):
print("Reloading", mod_name)
del USERPREF_PT_addons._addons_fake_modules[mod_name]
mod = None
if mod is None:
mod = fake_module(mod_name, mod_path)
if mod:
USERPREF_PT_addons._addons_fake_modules[mod_name] = mod
USERPREF_PT_addons._addons_fake_modules[mod_name] = mod
# just incase we get stale modules, not likely
for mod_stale in modules_stale: