def validate(self, obj, value): value = self.coerce_str(obj, value) if isinstance(value, str) and py3compat.isidentifier(value, dotted=True): return value self.error(obj, value)
def is_importable_file(path): """Returns True if the provided path is a valid importable module""" name, extension = os.path.splitext( path ) return import_re.match(path) and py3compat.isidentifier(name)
def validate(self, obj, value): value = self.coerce_str(obj, value) if isinstance(value, str) and py3compat.isidentifier(value): return value self.error(obj, value)
def module_list(path): """ Return the list containing the names of the modules available in the given folder. """ # sys.path has the cwd as an empty string, but isdir/listdir need it as '.' if path == '': path = '.' if os.path.isdir(path): folder_list = os.listdir(path) elif path.endswith('.egg'): try: folder_list = [f for f in zipimporter(path)._files] except: folder_list = [] else: folder_list = [] if not folder_list: return [] # A few local constants to be used in loops below isfile = os.path.isfile pjoin = os.path.join basename = os.path.basename def is_importable_file(path): """Returns True if the provided path is a valid importable module""" name, extension = os.path.splitext( path ) return import_re.match(path) and py3compat.isidentifier(name) if path.endswith('.egg') and isfile(path): folder_list = [x[0] for x in (y.split('/', 3) for y in (p for p in folder_list if p.endswith('/__init__.py'))) if len(x) == 2 and py3compat.isidentifier(x[0])] else: # Now find actual path matches for packages or modules folder_list = [p for p in folder_list if any(isfile(pjoin(path, p, '__init__' + suffix[0])) for suffix in imp.get_suffixes()) or is_importable_file(p) ] return [basename(p).split('.')[0] for p in folder_list]
def is_importable_file(path): """Returns True if the provided path is a valid importable module""" name, extension = os.path.splitext( path ) return import_re.match(path) and py3compat.isidentifier(name)