__init__.py 2.3 KB

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