2023-08-16 00:20:26 +10:00
|
|
|
# SPDX-FileCopyrightText: 2009-2023 Blender Authors
|
2023-06-15 13:09:04 +10:00
|
|
|
#
|
2022-02-11 09:07:11 +11:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2009-10-29 20:55:45 +00:00
|
|
|
|
2023-09-05 10:49:20 +10:00
|
|
|
# Copyright (c) 2009 Fernando Perez, https://www.stani.be
|
2022-02-09 16:00:03 +11:00
|
|
|
|
2023-09-03 21:35:03 +10:00
|
|
|
# Original copyright (see doc-string):
|
2018-07-03 06:27:53 +02:00
|
|
|
# ****************************************************************************
|
2009-10-29 20:55:45 +00:00
|
|
|
# Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
|
|
|
|
#
|
|
|
|
# Distributed under the terms of the BSD License. The full license is in
|
|
|
|
# the file COPYING, distributed as part of this software.
|
2018-07-03 06:27:53 +02:00
|
|
|
# ****************************************************************************
|
2009-10-29 20:55:45 +00:00
|
|
|
|
|
|
|
"""Completer for import statements
|
|
|
|
|
|
|
|
Original code was from IPython/Extensions/ipy_completers.py. The following
|
|
|
|
changes have been made:
|
|
|
|
- ported to python3
|
|
|
|
- pep8 polishing
|
|
|
|
- limit list of modules to prefix in case of "from w"
|
|
|
|
- sorted modules
|
|
|
|
- added sphinx documentation
|
2018-09-03 16:49:08 +02:00
|
|
|
- complete() returns a blank list of the module isn't found
|
2009-10-29 20:55:45 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2012-02-08 04:37:37 +00:00
|
|
|
TIMEOUT_STORAGE = 3 # Time in secs after which the root-modules will be stored
|
2009-10-29 20:55:45 +00:00
|
|
|
TIMEOUT_GIVEUP = 20 # Time in secs after which we give up
|
|
|
|
|
|
|
|
ROOT_MODULES = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_root_modules():
|
|
|
|
"""
|
|
|
|
Returns a list containing the names of all the modules available in the
|
2012-02-08 04:37:37 +00:00
|
|
|
folders of the python-path.
|
2009-10-29 20:55:45 +00:00
|
|
|
|
|
|
|
:returns: modules
|
|
|
|
:rtype: list
|
|
|
|
"""
|
|
|
|
global ROOT_MODULES
|
|
|
|
modules = []
|
2022-09-14 16:18:59 +10:00
|
|
|
if not (ROOT_MODULES is None):
|
2009-10-29 20:55:45 +00:00
|
|
|
return ROOT_MODULES
|
|
|
|
from time import time
|
|
|
|
t = time()
|
|
|
|
store = False
|
|
|
|
for path in sys.path:
|
|
|
|
modules += module_list(path)
|
|
|
|
if time() - t >= TIMEOUT_STORAGE and not store:
|
|
|
|
# Caching the list of root modules, please wait!
|
|
|
|
store = True
|
|
|
|
if time() - t > TIMEOUT_GIVEUP:
|
|
|
|
# This is taking too long, we give up.
|
|
|
|
ROOT_MODULES = []
|
|
|
|
return []
|
|
|
|
|
|
|
|
modules += sys.builtin_module_names
|
|
|
|
|
2009-12-07 14:09:53 +00:00
|
|
|
# needed for modules defined in C
|
|
|
|
modules += sys.modules.keys()
|
|
|
|
|
2023-07-02 19:38:26 +10:00
|
|
|
modules = set(modules)
|
|
|
|
modules.discard("__init__")
|
|
|
|
modules = sorted(list(modules))
|
2009-10-29 20:55:45 +00:00
|
|
|
if store:
|
|
|
|
ROOT_MODULES = modules
|
|
|
|
return modules
|
|
|
|
|
|
|
|
|
|
|
|
def module_list(path):
|
|
|
|
"""
|
|
|
|
Return the list containing the names of the modules available in
|
|
|
|
the given folder.
|
|
|
|
|
2022-09-19 14:22:31 +10:00
|
|
|
:arg path: folder path
|
2009-10-29 20:55:45 +00:00
|
|
|
:type path: str
|
|
|
|
:returns: modules
|
|
|
|
:rtype: list
|
|
|
|
"""
|
|
|
|
|
|
|
|
if os.path.isdir(path):
|
|
|
|
folder_list = os.listdir(path)
|
|
|
|
elif path.endswith('.egg'):
|
|
|
|
from zipimport import zipimporter
|
|
|
|
try:
|
|
|
|
folder_list = [f for f in zipimporter(path)._files]
|
|
|
|
except:
|
|
|
|
folder_list = []
|
|
|
|
else:
|
|
|
|
folder_list = []
|
2023-03-01 22:12:18 +11:00
|
|
|
# folder_list = glob.glob(os.path.join(path,'*'))
|
2013-03-28 19:33:14 +00:00
|
|
|
folder_list = [
|
2016-07-29 21:22:27 +10:00
|
|
|
p for p in folder_list
|
|
|
|
if (os.path.exists(os.path.join(path, p, '__init__.py')) or
|
|
|
|
p[-3:] in {'.py', '.so'} or
|
|
|
|
p[-4:] in {'.pyc', '.pyo', '.pyd'})]
|
2009-10-29 20:55:45 +00:00
|
|
|
|
|
|
|
folder_list = [os.path.basename(p).split('.')[0] for p in folder_list]
|
|
|
|
return folder_list
|
|
|
|
|
|
|
|
|
|
|
|
def complete(line):
|
|
|
|
"""
|
|
|
|
Returns a list containing the completion possibilities for an import line.
|
|
|
|
|
2022-09-19 14:22:31 +10:00
|
|
|
:arg line:
|
2009-10-29 20:55:45 +00:00
|
|
|
|
|
|
|
incomplete line which contains an import statement::
|
|
|
|
|
|
|
|
import xml.d
|
|
|
|
from xml.dom import
|
|
|
|
|
|
|
|
:type line: str
|
|
|
|
:returns: list of completion possibilities
|
|
|
|
:rtype: list
|
|
|
|
|
|
|
|
>>> complete('import weak')
|
|
|
|
['weakref']
|
2009-10-30 09:34:57 +00:00
|
|
|
>>> complete('from weakref import C')
|
|
|
|
['CallableProxyType']
|
2009-10-29 20:55:45 +00:00
|
|
|
"""
|
|
|
|
import inspect
|
|
|
|
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
|
|
|
def try_import(mod, *, only_modules=False):
|
2009-10-29 20:55:45 +00:00
|
|
|
|
|
|
|
def is_importable(module, attr):
|
|
|
|
if only_modules:
|
|
|
|
return inspect.ismodule(getattr(module, attr))
|
|
|
|
else:
|
2022-09-14 16:18:59 +10:00
|
|
|
return not (attr[:2] == '__' and attr[-2:] == '__')
|
2009-10-29 20:55:45 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
m = __import__(mod)
|
|
|
|
except:
|
|
|
|
return []
|
|
|
|
mods = mod.split('.')
|
|
|
|
for module in mods[1:]:
|
|
|
|
m = getattr(m, module)
|
|
|
|
if (not hasattr(m, '__file__')) or (not only_modules) or\
|
|
|
|
(hasattr(m, '__file__') and '__init__' in m.__file__):
|
|
|
|
completion_list = [attr for attr in dir(m)
|
2016-07-29 21:22:27 +10:00
|
|
|
if is_importable(m, attr)]
|
2009-10-30 09:34:57 +00:00
|
|
|
else:
|
|
|
|
completion_list = []
|
2009-10-29 20:55:45 +00:00
|
|
|
completion_list.extend(getattr(m, '__all__', []))
|
|
|
|
if hasattr(m, '__file__') and '__init__' in m.__file__:
|
|
|
|
completion_list.extend(module_list(os.path.dirname(m.__file__)))
|
|
|
|
completion_list = list(set(completion_list))
|
|
|
|
if '__init__' in completion_list:
|
|
|
|
completion_list.remove('__init__')
|
|
|
|
return completion_list
|
|
|
|
|
2009-10-30 09:34:57 +00:00
|
|
|
def filter_prefix(names, prefix):
|
|
|
|
return [name for name in names if name.startswith(prefix)]
|
|
|
|
|
2009-10-29 20:55:45 +00:00
|
|
|
words = line.split(' ')
|
|
|
|
if len(words) == 3 and words[0] == 'from':
|
|
|
|
return ['import ']
|
2019-08-27 19:17:28 +10:00
|
|
|
if len(words) < 3 and (words[0] in {'import', 'from'}):
|
2009-10-29 20:55:45 +00:00
|
|
|
if len(words) == 1:
|
|
|
|
return get_root_modules()
|
|
|
|
mod = words[1].split('.')
|
|
|
|
if len(mod) < 2:
|
2009-10-30 09:34:57 +00:00
|
|
|
return filter_prefix(get_root_modules(), words[-1])
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
|
|
|
completion_list = try_import('.'.join(mod[:-1]), only_modules=True)
|
2009-10-29 20:55:45 +00:00
|
|
|
completion_list = ['.'.join(mod[:-1] + [el]) for el in completion_list]
|
2009-10-30 09:34:57 +00:00
|
|
|
return filter_prefix(completion_list, words[-1])
|
2009-10-29 20:55:45 +00:00
|
|
|
if len(words) >= 3 and words[0] == 'from':
|
|
|
|
mod = words[1]
|
2009-10-30 09:34:57 +00:00
|
|
|
return filter_prefix(try_import(mod), words[-1])
|
2010-04-25 15:21:46 +00:00
|
|
|
|
|
|
|
# get here if the import is not found
|
2023-09-03 21:35:03 +10:00
|
|
|
# import invalid_module
|
|
|
|
# ^, in this case return nothing
|
2010-04-25 15:21:46 +00:00
|
|
|
return []
|