def _get_pypi_package(self, import_name, pypi_import_names): if len(import_name.split(".")) > 5: #print "too many parts to be a pypi lib, skipping", import_name return None # if it's in the standard lib it doesn't count, # even if in might be in pypi if import_name in PythonStandardLibs.get(): #print "found in standard lib, skipping", import_name return None # maybe find flask stuff special here... if import_name in pypi_import_names: # a last check: # don't include modules that are in their filepaths # because those are more likely their personal code # with accidental pypi names than than pypi libraries if self._in_filepath(import_name): return None else: #print "found one!", import_name return import_name # if foo.bar.baz is not a pypi import name, maybe foo.bar is. try that. elif '.' in import_name: shortened_name = import_name.rsplit('.', 1)[0] # print "now trying shortened_name", shortened_name return self._get_pypi_package(shortened_name, pypi_import_names) # if there's no dot in your name, there are no more options, you're done else: return None
def _get_pypi_package(self, module_name, pypi_package_names): if len(module_name.split(".")) > 5: print "too many parts to be a pypi lib, skipping", module_name return None # if it's in the standard lib it doesn't count, # even if in might be in pypi if module_name in PythonStandardLibs.get(): print "found in standard lib, skipping", module_name return None def return_match_if_found(match, replace_with): if match in module_name.lower(): lookup_key = module_name.lower().replace(match, replace_with) # pypi_package_names is loaded as module import, it's a cache. # search the keys of pypi_package_names, which are all lowercase if lookup_key in pypi_package_names: return lookup_key return None # try the originals first found_key = return_match_if_found("", "") # try lots of things, to work around hyphens # format is {<the name you use to import>: <the official PyPi name>} special_cases = { "dateutil": "python-dateutil", "bs4": "beautifulsoup4", "yaml": "PyYAML", "Image": "Pillow", "_imaging": "Pillow" } if not found_key: if module_name in special_cases: found_key = special_cases[module_name].lower() if not found_key: if module_name in special_cases.values(): found_key = module_name.lower() if not found_key: found_key = return_match_if_found("-", "_") if not found_key: found_key = return_match_if_found("-", "-") if not found_key: found_key = return_match_if_found("_", "-") if not found_key: found_key = return_match_if_found(".", "-") if not found_key: found_key = return_match_if_found(".ext.", "-") if not found_key: found_key = return_match_if_found("ext.", "-") if found_key: official_pypi_name = pypi_package_names[found_key] # a last check: # don't include modules that are in their filepaths # because those are more likely their personal code # with accidental pypi names than than pypi libraries if self._in_filepath(found_key): return None else: print "found one!", official_pypi_name return official_pypi_name # if foo.bar.baz is not in pypi, maybe foo.bar is. let's try that. elif '.' in module_name: shortened_name = module_name.rsplit('.', 1)[0] # print "now trying shortened_name", shortened_name return self._get_pypi_package(shortened_name, pypi_package_names) # if there's no dot in your name, there are no more options, you're done else: return None