Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #!/usr/bin/env python
  2. ''' Tool for sorting imports alphabetically, and automatically separated into sections.
  3. Copyright (C) 2013 Timothy Edmund Crosley
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  7. to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all copies or
  9. substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  11. TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  12. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  13. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  14. OTHER DEALINGS IN THE SOFTWARE.
  15. '''
  16. from __future__ import absolute_import, division, print_function, unicode_literals
  17. import argparse
  18. import glob
  19. import os
  20. import re
  21. import sys
  22. from concurrent.futures import ProcessPoolExecutor
  23. import functools
  24. import setuptools
  25. from isort import SortImports, __version__
  26. from isort.settings import DEFAULT_SECTIONS, default, from_path, should_skip
  27. from .pie_slice import itemsview
  28. INTRO = r"""
  29. /#######################################################################\
  30. `sMMy`
  31. .yyyy- `
  32. ##soos## ./o.
  33. ` ``..-..` ``...`.`` ` ```` ``-ssso```
  34. .s:-y- .+osssssso/. ./ossss+:so+:` :+o-`/osso:+sssssssso/
  35. .s::y- osss+.``.`` -ssss+-.`-ossso` ssssso/::..::+ssss:::.
  36. .s::y- /ssss+//:-.` `ssss+ `ssss+ sssso` :ssss`
  37. .s::y- `-/+oossssso/ `ssss/ sssso ssss/ :ssss`
  38. .y-/y- ````:ssss` ossso. :ssss: ssss/ :ssss.
  39. `/so:` `-//::/osss+ `+ssss+-/ossso: /sso- `osssso/.
  40. \/ `-/oooo++/- .:/++:/++/-` .. `://++/.
  41. isort your Python imports for you so you don't have to
  42. VERSION {0}
  43. \########################################################################/
  44. """.format(__version__)
  45. shebang_re = re.compile(br'^#!.*\bpython[23w]?\b')
  46. def is_python_file(path):
  47. if path.endswith('.py'):
  48. return True
  49. try:
  50. with open(path, 'rb') as fp:
  51. line = fp.readline(100)
  52. except IOError:
  53. return False
  54. else:
  55. return bool(shebang_re.match(line))
  56. class SortAttempt(object):
  57. def __init__(self, incorrectly_sorted, skipped):
  58. self.incorrectly_sorted = incorrectly_sorted
  59. self.skipped = skipped
  60. def sort_imports(file_name, **arguments):
  61. try:
  62. result = SortImports(file_name, **arguments)
  63. return SortAttempt(result.incorrectly_sorted, result.skipped)
  64. except IOError as e:
  65. print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e))
  66. return None
  67. def iter_source_code(paths, config, skipped):
  68. """Iterate over all Python source files defined in paths."""
  69. for path in paths:
  70. if os.path.isdir(path):
  71. if should_skip(path, config, os.getcwd()):
  72. skipped.append(path)
  73. continue
  74. for dirpath, dirnames, filenames in os.walk(path, topdown=True):
  75. for dirname in list(dirnames):
  76. if should_skip(dirname, config, dirpath):
  77. skipped.append(dirname)
  78. dirnames.remove(dirname)
  79. for filename in filenames:
  80. filepath = os.path.join(dirpath, filename)
  81. if is_python_file(filepath):
  82. if should_skip(filename, config, dirpath):
  83. skipped.append(filename)
  84. else:
  85. yield filepath
  86. else:
  87. yield path
  88. class ISortCommand(setuptools.Command):
  89. """The :class:`ISortCommand` class is used by setuptools to perform
  90. imports checks on registered modules.
  91. """
  92. description = "Run isort on modules registered in setuptools"
  93. user_options = []
  94. def initialize_options(self):
  95. default_settings = default.copy()
  96. for (key, value) in itemsview(default_settings):
  97. setattr(self, key, value)
  98. def finalize_options(self):
  99. "Get options from config files."
  100. self.arguments = {}
  101. computed_settings = from_path(os.getcwd())
  102. for (key, value) in itemsview(computed_settings):
  103. self.arguments[key] = value
  104. def distribution_files(self):
  105. """Find distribution packages."""
  106. # This is verbatim from flake8
  107. if self.distribution.packages:
  108. package_dirs = self.distribution.package_dir or {}
  109. for package in self.distribution.packages:
  110. pkg_dir = package
  111. if package in package_dirs:
  112. pkg_dir = package_dirs[package]
  113. elif '' in package_dirs:
  114. pkg_dir = package_dirs[''] + os.path.sep + pkg_dir
  115. yield pkg_dir.replace('.', os.path.sep)
  116. if self.distribution.py_modules:
  117. for filename in self.distribution.py_modules:
  118. yield "%s.py" % filename
  119. # Don't miss the setup.py file itself
  120. yield "setup.py"
  121. def run(self):
  122. arguments = self.arguments
  123. wrong_sorted_files = False
  124. arguments['check'] = True
  125. for path in self.distribution_files():
  126. for python_file in glob.iglob(os.path.join(path, '*.py')):
  127. try:
  128. incorrectly_sorted = SortImports(python_file, **arguments).incorrectly_sorted
  129. if incorrectly_sorted:
  130. wrong_sorted_files = True
  131. except IOError as e:
  132. print("WARNING: Unable to parse file {0} due to {1}".format(python_file, e))
  133. if wrong_sorted_files:
  134. exit(1)
  135. def create_parser():
  136. parser = argparse.ArgumentParser(description='Sort Python import definitions alphabetically '
  137. 'within logical sections.')
  138. inline_args_group = parser.add_mutually_exclusive_group()
  139. parser.add_argument('-a', '--add-import', dest='add_imports', action='append',
  140. help='Adds the specified import line to all files, '
  141. 'automatically determining correct placement.')
  142. parser.add_argument('-ac', '--atomic', dest='atomic', action='store_true',
  143. help="Ensures the output doesn't save if the resulting file contains syntax errors.")
  144. parser.add_argument('-af', '--force-adds', dest='force_adds', action='store_true',
  145. help='Forces import adds even if the original file is empty.')
  146. parser.add_argument('-b', '--builtin', dest='known_standard_library', action='append',
  147. help='Force sortImports to recognize a module as part of the python standard library.')
  148. parser.add_argument('-c', '--check-only', action='store_true', dest="check",
  149. help='Checks the file for unsorted / unformatted imports and prints them to the '
  150. 'command line without modifying the file.')
  151. parser.add_argument('-ca', '--combine-as', dest='combine_as_imports', action='store_true',
  152. help="Combines as imports on the same line.")
  153. parser.add_argument('-cs', '--combine-star', dest='combine_star', action='store_true',
  154. help="Ensures that if a star import is present, nothing else is imported from that namespace.")
  155. parser.add_argument('-d', '--stdout', help='Force resulting output to stdout, instead of in-place.',
  156. dest='write_to_stdout', action='store_true')
  157. parser.add_argument('-df', '--diff', dest='show_diff', action='store_true',
  158. help="Prints a diff of all the changes isort would make to a file, instead of "
  159. "changing it in place")
  160. parser.add_argument('-ds', '--no-sections', help='Put all imports into the same section bucket', dest='no_sections',
  161. action='store_true')
  162. parser.add_argument('-dt', '--dont-order-by-type', dest='dont_order_by_type',
  163. action='store_true', help='Only order imports alphabetically, do not attempt type ordering')
  164. parser.add_argument('-e', '--balanced', dest='balanced_wrapping', action='store_true',
  165. help='Balances wrapping to produce the most consistent line length possible')
  166. parser.add_argument('-f', '--future', dest='known_future_library', action='append',
  167. help='Force sortImports to recognize a module as part of the future compatibility libraries.')
  168. parser.add_argument('-fas', '--force-alphabetical-sort', action='store_true', dest="force_alphabetical_sort",
  169. help='Force all imports to be sorted as a single section')
  170. parser.add_argument('-fass', '--force-alphabetical-sort-within-sections', action='store_true',
  171. dest="force_alphabetical_sort", help='Force all imports to be sorted alphabetically within a '
  172. 'section')
  173. parser.add_argument('-ff', '--from-first', dest='from_first',
  174. help="Switches the typical ordering preference, showing from imports first then straight ones.")
  175. parser.add_argument('-fgw', '--force-grid-wrap', nargs='?', const=2, type=int, dest="force_grid_wrap",
  176. help='Force number of from imports (defaults to 2) to be grid wrapped regardless of line '
  177. 'length')
  178. parser.add_argument('-fss', '--force-sort-within-sections', action='store_true', dest="force_sort_within_sections",
  179. help='Force imports to be sorted by module, independent of import_type')
  180. parser.add_argument('-i', '--indent', help='String to place for indents defaults to " " (4 spaces).',
  181. dest='indent', type=str)
  182. parser.add_argument('-j', '--jobs', help='Number of files to process in parallel.',
  183. dest='jobs', type=int)
  184. parser.add_argument('-k', '--keep-direct-and-as', dest='keep_direct_and_as_imports', action='store_true',
  185. help="Turns off default behavior that removes direct imports when as imports exist.")
  186. parser.add_argument('-l', '--lines', help='[Deprecated] The max length of an import line (used for wrapping '
  187. 'long imports).',
  188. dest='line_length', type=int)
  189. parser.add_argument('-lai', '--lines-after-imports', dest='lines_after_imports', type=int)
  190. parser.add_argument('-lbt', '--lines-between-types', dest='lines_between_types', type=int)
  191. parser.add_argument('-le', '--line-ending', dest='line_ending',
  192. help="Forces line endings to the specified value. If not set, values will be guessed per-file.")
  193. parser.add_argument('-ls', '--length-sort', help='Sort imports by their string length.',
  194. dest='length_sort', action='store_true')
  195. parser.add_argument('-m', '--multi-line', dest='multi_line_output', type=int, choices=[0, 1, 2, 3, 4, 5],
  196. help='Multi line output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-grid, '
  197. '5-vert-grid-grouped, 6-vert-grid-grouped-no-comma).')
  198. inline_args_group.add_argument('-nis', '--no-inline-sort', dest='no_inline_sort', action='store_true',
  199. help='Leaves `from` imports with multiple imports \'as-is\' (e.g. `from foo import a, c ,b`).')
  200. parser.add_argument('-nlb', '--no-lines-before', help='Sections which should not be split with previous by empty lines',
  201. dest='no_lines_before', action='append')
  202. parser.add_argument('-ns', '--dont-skip', help='Files that sort imports should never skip over.',
  203. dest='not_skip', action='append')
  204. parser.add_argument('-o', '--thirdparty', dest='known_third_party', action='append',
  205. help='Force sortImports to recognize a module as being part of a third party library.')
  206. parser.add_argument('-ot', '--order-by-type', dest='order_by_type',
  207. action='store_true', help='Order imports by type in addition to alphabetically')
  208. parser.add_argument('-p', '--project', dest='known_first_party', action='append',
  209. help='Force sortImports to recognize a module as being part of the current python project.')
  210. parser.add_argument('-q', '--quiet', action='store_true', dest="quiet",
  211. help='Shows extra quiet output, only errors are outputted.')
  212. parser.add_argument('-r', '--remove-import', dest='remove_imports', action='append',
  213. help='Removes the specified import from all files.')
  214. parser.add_argument('-rc', '--recursive', dest='recursive', action='store_true',
  215. help='Recursively look for Python files of which to sort imports')
  216. parser.add_argument('-s', '--skip', help='Files that sort imports should skip over. If you want to skip multiple '
  217. 'files you should specify twice: --skip file1 --skip file2.', dest='skip', action='append')
  218. parser.add_argument('-sd', '--section-default', dest='default_section',
  219. help='Sets the default section for imports (by default FIRSTPARTY) options: ' +
  220. str(DEFAULT_SECTIONS))
  221. parser.add_argument('-sg', '--skip-glob', help='Files that sort imports should skip over.', dest='skip_glob',
  222. action='append')
  223. inline_args_group.add_argument('-sl', '--force-single-line-imports', dest='force_single_line', action='store_true',
  224. help='Forces all from imports to appear on their own line')
  225. parser.add_argument('-sp', '--settings-path', dest="settings_path",
  226. help='Explicitly set the settings path instead of auto determining based on file location.')
  227. parser.add_argument('-t', '--top', help='Force specific imports to the top of their appropriate section.',
  228. dest='force_to_top', action='append')
  229. parser.add_argument('-tc', '--trailing-comma', dest='include_trailing_comma', action='store_true',
  230. help='Includes a trailing comma on multi line imports that include parentheses.')
  231. parser.add_argument('-up', '--use-parentheses', dest='use_parentheses', action='store_true',
  232. help='Use parenthesis for line continuation on length limit instead of slashes.')
  233. parser.add_argument('-v', '--version', action='store_true', dest='show_version')
  234. parser.add_argument('-vb', '--verbose', action='store_true', dest="verbose",
  235. help='Shows verbose output, such as when files are skipped or when a check is successful.')
  236. parser.add_argument('--virtual-env', dest='virtual_env',
  237. help='Virtual environment to use for determining whether a package is third-party')
  238. parser.add_argument('-vn', '--version-number', action='version', version=__version__,
  239. help='Returns just the current version number without the logo')
  240. parser.add_argument('-w', '--line-width', help='The max length of an import line (used for wrapping long imports).',
  241. dest='line_length', type=int)
  242. parser.add_argument('-wl', '--wrap-length', dest='wrap_length',
  243. help="Specifies how long lines that are wrapped should be, if not set line_length is used.")
  244. parser.add_argument('-ws', '--ignore-whitespace', action='store_true', dest="ignore_whitespace",
  245. help='Tells isort to ignore whitespace differences when --check-only is being used.')
  246. parser.add_argument('-y', '--apply', dest='apply', action='store_true',
  247. help='Tells isort to apply changes recursively without asking')
  248. parser.add_argument('files', nargs='*', help='One or more Python source files that need their imports sorted.')
  249. arguments = {key: value for key, value in itemsview(vars(parser.parse_args())) if value}
  250. if 'dont_order_by_type' in arguments:
  251. arguments['order_by_type'] = False
  252. return arguments
  253. def main():
  254. arguments = create_parser()
  255. if arguments.get('show_version'):
  256. print(INTRO)
  257. return
  258. if 'settings_path' in arguments:
  259. sp = arguments['settings_path']
  260. arguments['settings_path'] = os.path.abspath(sp) if os.path.isdir(sp) else os.path.dirname(os.path.abspath(sp))
  261. if not os.path.isdir(arguments['settings_path']):
  262. print("WARNING: settings_path dir does not exist: {0}".format(arguments['settings_path']))
  263. if 'virtual_env' in arguments:
  264. venv = arguments['virtual_env']
  265. arguments['virtual_env'] = os.path.abspath(venv)
  266. if not os.path.isdir(arguments['virtual_env']):
  267. print("WARNING: virtual_env dir does not exist: {0}".format(arguments['virtual_env']))
  268. file_names = arguments.pop('files', [])
  269. if file_names == ['-']:
  270. SortImports(file_contents=sys.stdin.read(), write_to_stdout=True, **arguments)
  271. else:
  272. if not file_names:
  273. file_names = ['.']
  274. arguments['recursive'] = True
  275. if not arguments.get('apply', False):
  276. arguments['ask_to_apply'] = True
  277. config = from_path(os.path.abspath(file_names[0]) or os.getcwd()).copy()
  278. config.update(arguments)
  279. wrong_sorted_files = False
  280. skipped = []
  281. if arguments.get('recursive', False):
  282. file_names = iter_source_code(file_names, config, skipped)
  283. num_skipped = 0
  284. if config['verbose'] or config.get('show_logo', False):
  285. print(INTRO)
  286. jobs = arguments.get('jobs')
  287. if jobs:
  288. executor = ProcessPoolExecutor(max_workers=jobs)
  289. for sort_attempt in executor.map(functools.partial(sort_imports, **arguments), file_names):
  290. if not sort_attempt:
  291. continue
  292. incorrectly_sorted = sort_attempt.incorrectly_sorted
  293. if arguments.get('check', False) and incorrectly_sorted:
  294. wrong_sorted_files = True
  295. if sort_attempt.skipped:
  296. num_skipped += 1
  297. else:
  298. for file_name in file_names:
  299. try:
  300. sort_attempt = SortImports(file_name, **arguments)
  301. incorrectly_sorted = sort_attempt.incorrectly_sorted
  302. if arguments.get('check', False) and incorrectly_sorted:
  303. wrong_sorted_files = True
  304. if sort_attempt.skipped:
  305. num_skipped += 1
  306. except IOError as e:
  307. print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e))
  308. if wrong_sorted_files:
  309. exit(1)
  310. num_skipped += len(skipped)
  311. if num_skipped and not arguments.get('quiet', False):
  312. if config['verbose']:
  313. for was_skipped in skipped:
  314. print("WARNING: {0} was skipped as it's listed in 'skip' setting"
  315. " or matches a glob in 'skip_glob' setting".format(was_skipped))
  316. print("Skipped {0} files".format(num_skipped))
  317. if __name__ == "__main__":
  318. main()