__init__.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. Package containing all pip commands
  3. """
  4. from __future__ import absolute_import
  5. from pip.commands.completion import CompletionCommand
  6. from pip.commands.freeze import FreezeCommand
  7. from pip.commands.help import HelpCommand
  8. from pip.commands.list import ListCommand
  9. from pip.commands.search import SearchCommand
  10. from pip.commands.show import ShowCommand
  11. from pip.commands.install import InstallCommand
  12. from pip.commands.uninstall import UninstallCommand
  13. from pip.commands.unzip import UnzipCommand
  14. from pip.commands.zip import ZipCommand
  15. from pip.commands.wheel import WheelCommand
  16. commands_dict = {
  17. CompletionCommand.name: CompletionCommand,
  18. FreezeCommand.name: FreezeCommand,
  19. HelpCommand.name: HelpCommand,
  20. SearchCommand.name: SearchCommand,
  21. ShowCommand.name: ShowCommand,
  22. InstallCommand.name: InstallCommand,
  23. UninstallCommand.name: UninstallCommand,
  24. UnzipCommand.name: UnzipCommand,
  25. ZipCommand.name: ZipCommand,
  26. ListCommand.name: ListCommand,
  27. WheelCommand.name: WheelCommand,
  28. }
  29. commands_order = [
  30. InstallCommand,
  31. UninstallCommand,
  32. FreezeCommand,
  33. ListCommand,
  34. ShowCommand,
  35. SearchCommand,
  36. WheelCommand,
  37. ZipCommand,
  38. UnzipCommand,
  39. HelpCommand,
  40. ]
  41. def get_summaries(ignore_hidden=True, ordered=True):
  42. """Yields sorted (command name, command summary) tuples."""
  43. if ordered:
  44. cmditems = _sort_commands(commands_dict, commands_order)
  45. else:
  46. cmditems = commands_dict.items()
  47. for name, command_class in cmditems:
  48. if ignore_hidden and command_class.hidden:
  49. continue
  50. yield (name, command_class.summary)
  51. def get_similar_commands(name):
  52. """Command name auto-correct."""
  53. from difflib import get_close_matches
  54. name = name.lower()
  55. close_commands = get_close_matches(name, commands_dict.keys())
  56. if close_commands:
  57. return close_commands[0]
  58. else:
  59. return False
  60. def _sort_commands(cmddict, order):
  61. def keyfn(key):
  62. try:
  63. return order.index(key[1])
  64. except ValueError:
  65. # unordered items should come last
  66. return 0xff
  67. return sorted(cmddict.items(), key=keyfn)