pyflyby 1.9.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pyflyby might be problematic. Click here for more details.
- pyflyby/__init__.py +56 -0
- pyflyby/__main__.py +9 -0
- pyflyby/_autoimp.py +2114 -0
- pyflyby/_cmdline.py +531 -0
- pyflyby/_comms.py +221 -0
- pyflyby/_dbg.py +1339 -0
- pyflyby/_docxref.py +379 -0
- pyflyby/_file.py +738 -0
- pyflyby/_flags.py +230 -0
- pyflyby/_format.py +182 -0
- pyflyby/_idents.py +233 -0
- pyflyby/_import_sorting.py +165 -0
- pyflyby/_importclns.py +642 -0
- pyflyby/_importdb.py +588 -0
- pyflyby/_imports2s.py +639 -0
- pyflyby/_importstmt.py +662 -0
- pyflyby/_interactive.py +2605 -0
- pyflyby/_livepatch.py +793 -0
- pyflyby/_log.py +199 -0
- pyflyby/_modules.py +515 -0
- pyflyby/_parse.py +1441 -0
- pyflyby/_py.py +2078 -0
- pyflyby/_util.py +459 -0
- pyflyby/_version.py +7 -0
- pyflyby/autoimport.py +20 -0
- pyflyby/importdb.py +19 -0
- pyflyby-1.9.4.data/data/etc/pyflyby/canonical.py +10 -0
- pyflyby-1.9.4.data/data/etc/pyflyby/common.py +27 -0
- pyflyby-1.9.4.data/data/etc/pyflyby/forget.py +10 -0
- pyflyby-1.9.4.data/data/etc/pyflyby/mandatory.py +10 -0
- pyflyby-1.9.4.data/data/etc/pyflyby/numpy.py +156 -0
- pyflyby-1.9.4.data/data/etc/pyflyby/std.py +335 -0
- pyflyby-1.9.4.data/data/libexec/pyflyby/colordiff +34 -0
- pyflyby-1.9.4.data/data/libexec/pyflyby/diff-colorize +148 -0
- pyflyby-1.9.4.data/data/share/doc/pyflyby/LICENSE.txt +23 -0
- pyflyby-1.9.4.data/data/share/doc/pyflyby/TODO.txt +115 -0
- pyflyby-1.9.4.data/data/share/doc/pyflyby/testing.txt +13 -0
- pyflyby-1.9.4.data/data/share/emacs/site-lisp/pyflyby.el +108 -0
- pyflyby-1.9.4.data/scripts/collect-exports +76 -0
- pyflyby-1.9.4.data/scripts/collect-imports +58 -0
- pyflyby-1.9.4.data/scripts/find-import +38 -0
- pyflyby-1.9.4.data/scripts/list-bad-xrefs +34 -0
- pyflyby-1.9.4.data/scripts/prune-broken-imports +34 -0
- pyflyby-1.9.4.data/scripts/pyflyby-diff +34 -0
- pyflyby-1.9.4.data/scripts/reformat-imports +27 -0
- pyflyby-1.9.4.data/scripts/replace-star-imports +37 -0
- pyflyby-1.9.4.data/scripts/tidy-imports +191 -0
- pyflyby-1.9.4.data/scripts/transform-imports +47 -0
- pyflyby-1.9.4.dist-info/LICENSE.txt +23 -0
- pyflyby-1.9.4.dist-info/METADATA +507 -0
- pyflyby-1.9.4.dist-info/RECORD +54 -0
- pyflyby-1.9.4.dist-info/WHEEL +5 -0
- pyflyby-1.9.4.dist-info/entry_points.txt +3 -0
- pyflyby-1.9.4.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
#!python
|
|
2
|
+
"""
|
|
3
|
+
tidy-imports *.py
|
|
4
|
+
tidy-imports < foo.py
|
|
5
|
+
|
|
6
|
+
Automatically improves python import statements.
|
|
7
|
+
|
|
8
|
+
- Adds missing imports and mandatory imports.
|
|
9
|
+
- Removes unused imports.
|
|
10
|
+
- Nicely formats imports (sorts, aligns, wraps).
|
|
11
|
+
|
|
12
|
+
If filenames are given on the command line, rewrites them. Otherwise, if
|
|
13
|
+
stdin is not a tty, read from stdin and write to stdout.
|
|
14
|
+
|
|
15
|
+
Only top-level import statements are touched.
|
|
16
|
+
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
# pyflyby/tidy-imports
|
|
20
|
+
# Copyright (C) 2011, 2012, 2014 Karl Chen.
|
|
21
|
+
# License: MIT http://opensource.org/licenses/MIT
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
from __future__ import print_function
|
|
25
|
+
|
|
26
|
+
import os
|
|
27
|
+
|
|
28
|
+
from pyflyby._cmdline import hfmt, parse_args, process_actions
|
|
29
|
+
from pyflyby._import_sorting import sort_imports
|
|
30
|
+
from pyflyby._imports2s import (canonicalize_imports,
|
|
31
|
+
fix_unused_and_missing_imports,
|
|
32
|
+
replace_star_imports,
|
|
33
|
+
transform_imports)
|
|
34
|
+
from pyflyby._parse import PythonBlock
|
|
35
|
+
|
|
36
|
+
import toml
|
|
37
|
+
TOML_AVAIL = True
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _get_pyproj_toml_config():
|
|
41
|
+
"""
|
|
42
|
+
Try to find current project pyproject.toml
|
|
43
|
+
in cwd or parents directories.
|
|
44
|
+
"""
|
|
45
|
+
if not TOML_AVAIL:
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
from pathlib import Path
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
cwd = Path(os.getcwd())
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
for pth in [cwd] + list(cwd.parents):
|
|
55
|
+
pyproj_toml = pth /'pyproject.toml'
|
|
56
|
+
if pyproj_toml.exists() and pyproj_toml.is_file():
|
|
57
|
+
return pyproj_toml.read_text()
|
|
58
|
+
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _addopts(parser):
|
|
66
|
+
"""
|
|
67
|
+
Callbacks to the parser to fill in extra options.
|
|
68
|
+
"""
|
|
69
|
+
parser.add_option('--add-missing',
|
|
70
|
+
default=True, action='store_true',
|
|
71
|
+
help=hfmt('''
|
|
72
|
+
(Default) Add missing imports.'''))
|
|
73
|
+
parser.add_option('--no-add-missing', dest='add_missing',
|
|
74
|
+
default=True, action='store_false',
|
|
75
|
+
help=hfmt('''
|
|
76
|
+
Don't add missing imports.'''))
|
|
77
|
+
parser.add_option('--remove-unused',
|
|
78
|
+
default="AUTOMATIC", action='store_true',
|
|
79
|
+
help=hfmt('''
|
|
80
|
+
Remove unused imports
|
|
81
|
+
(default unless filename == __init__.py).'''))
|
|
82
|
+
parser.add_option('--no-remove-unused', dest='remove_unused',
|
|
83
|
+
action='store_false',
|
|
84
|
+
help=hfmt('''
|
|
85
|
+
Don't remove unused imports
|
|
86
|
+
(default if filename == __init__.py).'''))
|
|
87
|
+
parser.add_option('--add-mandatory',
|
|
88
|
+
default=True, action='store_true',
|
|
89
|
+
help=hfmt('''
|
|
90
|
+
(Default) Add mandatory imports.'''))
|
|
91
|
+
parser.add_option('--no-add-mandatory', dest='add_mandatory',
|
|
92
|
+
default=True, action='store_false',
|
|
93
|
+
help=hfmt('''
|
|
94
|
+
Don't add mandatory imports.'''))
|
|
95
|
+
parser.add_option('--replace-star-imports',
|
|
96
|
+
default=False, action='store_true',
|
|
97
|
+
help=hfmt('''
|
|
98
|
+
Replace 'from foo.bar import *' with full list
|
|
99
|
+
of imports before removing unused imports.'''))
|
|
100
|
+
parser.add_option('--no-replace-star-imports',
|
|
101
|
+
dest='replace_star_imports',
|
|
102
|
+
action='store_false',
|
|
103
|
+
help=hfmt('''
|
|
104
|
+
(Default) Don't replace 'from foo.bar import
|
|
105
|
+
*'.'''))
|
|
106
|
+
parser.add_option('--canonicalize',
|
|
107
|
+
default=True, action='store_true',
|
|
108
|
+
help=hfmt('''
|
|
109
|
+
(Default) Replace imports with canonical
|
|
110
|
+
equivalent imports, according to database.'''))
|
|
111
|
+
parser.add_option('--experimental-sort-imports',
|
|
112
|
+
default=False, action='store_true',
|
|
113
|
+
help=hfmt('''
|
|
114
|
+
experimental import sorting'''))
|
|
115
|
+
parser.add_option('--no-canonicalize', dest='canonicalize',
|
|
116
|
+
default=True, action='store_false',
|
|
117
|
+
help=hfmt('''
|
|
118
|
+
Don't canonicalize imports.'''))
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def transform_callback(option, opt_str, value, group):
|
|
122
|
+
k, v = value.split("=", 1)
|
|
123
|
+
group.values.transformations[k] = v
|
|
124
|
+
parser.add_option("--transform", action='callback',
|
|
125
|
+
type="string", callback=transform_callback,
|
|
126
|
+
metavar="OLD=NEW",
|
|
127
|
+
dest="transformations", default={},
|
|
128
|
+
help=hfmt('''
|
|
129
|
+
Replace OLD with NEW in imports.
|
|
130
|
+
May be specified multiple times.'''))
|
|
131
|
+
def no_add_callback(option, opt_str, value, group):
|
|
132
|
+
group.values.add_missing = False
|
|
133
|
+
group.values.add_mandatory = False
|
|
134
|
+
parser.add_option('--no-add', action='callback',
|
|
135
|
+
callback=no_add_callback,
|
|
136
|
+
help=hfmt('''
|
|
137
|
+
Equivalent to --no-add-missing
|
|
138
|
+
--no-add-mandatory.'''))
|
|
139
|
+
def main() -> None:
|
|
140
|
+
|
|
141
|
+
config_text = _get_pyproj_toml_config()
|
|
142
|
+
if config_text:
|
|
143
|
+
default_config = toml.loads(config_text).get('tool', {}).get('pyflyby',{})
|
|
144
|
+
else:
|
|
145
|
+
default_config = {}
|
|
146
|
+
|
|
147
|
+
def _add_opts_and_defaults(parser):
|
|
148
|
+
_addopts(parser)
|
|
149
|
+
parser.set_defaults(**default_config)
|
|
150
|
+
|
|
151
|
+
options, args = parse_args(
|
|
152
|
+
_add_opts_and_defaults, import_format_params=True, modify_action_params=True, defaults=default_config)
|
|
153
|
+
|
|
154
|
+
def modify(block:PythonBlock) -> PythonBlock:
|
|
155
|
+
if options.transformations:
|
|
156
|
+
block = transform_imports(block, options.transformations,
|
|
157
|
+
params=options.params)
|
|
158
|
+
if options.replace_star_imports:
|
|
159
|
+
block = replace_star_imports(block, params=options.params)
|
|
160
|
+
block = fix_unused_and_missing_imports(
|
|
161
|
+
block, params=options.params,
|
|
162
|
+
add_missing=options.add_missing,
|
|
163
|
+
remove_unused=options.remove_unused,
|
|
164
|
+
add_mandatory=options.add_mandatory,
|
|
165
|
+
)
|
|
166
|
+
# TODO: disable sorting until we figure out #287
|
|
167
|
+
# https://github.com/deshaw/pyflyby/issues/287
|
|
168
|
+
|
|
169
|
+
# here we get a (single?) PythonBlock, we can access each statement with
|
|
170
|
+
# >>> block.statements
|
|
171
|
+
# and each statement can have a:
|
|
172
|
+
# is_import
|
|
173
|
+
# or
|
|
174
|
+
# is_comment or blank
|
|
175
|
+
|
|
176
|
+
# TODO: we do Python(str(...)) in order to unparse-reparse and get proper ast node numbers.
|
|
177
|
+
if options.experimental_sort_imports:
|
|
178
|
+
sorted_imports = PythonBlock(str(sort_imports(block)))
|
|
179
|
+
else:
|
|
180
|
+
sorted_imports = block
|
|
181
|
+
if options.canonicalize:
|
|
182
|
+
cannonical_imports = canonicalize_imports(sorted_imports, params=options.params)
|
|
183
|
+
else:
|
|
184
|
+
cannonical_imports = sorted_imports
|
|
185
|
+
return cannonical_imports
|
|
186
|
+
|
|
187
|
+
process_actions(args, options.actions, modify)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
if __name__ == '__main__':
|
|
191
|
+
main()
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!python
|
|
2
|
+
"""
|
|
3
|
+
transform-imports --transform aa.bb=xx.yy *.py
|
|
4
|
+
transform-imports --transform aa.bb=xx.yy < foo.py
|
|
5
|
+
|
|
6
|
+
Transforms::
|
|
7
|
+
from aa.bb.cc import dd, ee
|
|
8
|
+
from aa import bb
|
|
9
|
+
to::
|
|
10
|
+
from xx.yy.cc import dd, ee
|
|
11
|
+
from xx import yy as bb
|
|
12
|
+
|
|
13
|
+
If filenames are given on the command line, rewrites them. Otherwise, if
|
|
14
|
+
stdin is not a tty, read from stdin and write to stdout.
|
|
15
|
+
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
# pyflyby/transform-imports
|
|
19
|
+
# Copyright (C) 2014 Karl Chen.
|
|
20
|
+
# License: MIT http://opensource.org/licenses/MIT
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
from pyflyby._cmdline import hfmt, parse_args, process_actions
|
|
24
|
+
from pyflyby._imports2s import transform_imports
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def main():
|
|
28
|
+
transformations = {}
|
|
29
|
+
def addopts(parser):
|
|
30
|
+
def callback(option, opt_str, value, group):
|
|
31
|
+
k, v = value.split("=", 1)
|
|
32
|
+
transformations[k] = v
|
|
33
|
+
parser.add_option("--transform", action='callback',
|
|
34
|
+
type="string", callback=callback,
|
|
35
|
+
metavar="OLD=NEW",
|
|
36
|
+
help=hfmt('''
|
|
37
|
+
Replace OLD with NEW in imports.
|
|
38
|
+
May be specified multiple times.'''))
|
|
39
|
+
options, args = parse_args(
|
|
40
|
+
addopts, import_format_params=True, modify_action_params=True)
|
|
41
|
+
def modify(x):
|
|
42
|
+
return transform_imports(x, transformations, params=options.params)
|
|
43
|
+
process_actions(args, options.actions, modify)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if __name__ == '__main__':
|
|
47
|
+
main()
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Pyflyby: Copyright (c) 2011, 2012, 2013, 2014, 2015 Karl Chen
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
Docxref.py includes code derived from Epydoc, which is also distributed under
|
|
23
|
+
the same MIT license.
|