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.

Files changed (54) hide show
  1. pyflyby/__init__.py +56 -0
  2. pyflyby/__main__.py +9 -0
  3. pyflyby/_autoimp.py +2114 -0
  4. pyflyby/_cmdline.py +531 -0
  5. pyflyby/_comms.py +221 -0
  6. pyflyby/_dbg.py +1339 -0
  7. pyflyby/_docxref.py +379 -0
  8. pyflyby/_file.py +738 -0
  9. pyflyby/_flags.py +230 -0
  10. pyflyby/_format.py +182 -0
  11. pyflyby/_idents.py +233 -0
  12. pyflyby/_import_sorting.py +165 -0
  13. pyflyby/_importclns.py +642 -0
  14. pyflyby/_importdb.py +588 -0
  15. pyflyby/_imports2s.py +639 -0
  16. pyflyby/_importstmt.py +662 -0
  17. pyflyby/_interactive.py +2605 -0
  18. pyflyby/_livepatch.py +793 -0
  19. pyflyby/_log.py +199 -0
  20. pyflyby/_modules.py +515 -0
  21. pyflyby/_parse.py +1441 -0
  22. pyflyby/_py.py +2078 -0
  23. pyflyby/_util.py +459 -0
  24. pyflyby/_version.py +7 -0
  25. pyflyby/autoimport.py +20 -0
  26. pyflyby/importdb.py +19 -0
  27. pyflyby-1.9.4.data/data/etc/pyflyby/canonical.py +10 -0
  28. pyflyby-1.9.4.data/data/etc/pyflyby/common.py +27 -0
  29. pyflyby-1.9.4.data/data/etc/pyflyby/forget.py +10 -0
  30. pyflyby-1.9.4.data/data/etc/pyflyby/mandatory.py +10 -0
  31. pyflyby-1.9.4.data/data/etc/pyflyby/numpy.py +156 -0
  32. pyflyby-1.9.4.data/data/etc/pyflyby/std.py +335 -0
  33. pyflyby-1.9.4.data/data/libexec/pyflyby/colordiff +34 -0
  34. pyflyby-1.9.4.data/data/libexec/pyflyby/diff-colorize +148 -0
  35. pyflyby-1.9.4.data/data/share/doc/pyflyby/LICENSE.txt +23 -0
  36. pyflyby-1.9.4.data/data/share/doc/pyflyby/TODO.txt +115 -0
  37. pyflyby-1.9.4.data/data/share/doc/pyflyby/testing.txt +13 -0
  38. pyflyby-1.9.4.data/data/share/emacs/site-lisp/pyflyby.el +108 -0
  39. pyflyby-1.9.4.data/scripts/collect-exports +76 -0
  40. pyflyby-1.9.4.data/scripts/collect-imports +58 -0
  41. pyflyby-1.9.4.data/scripts/find-import +38 -0
  42. pyflyby-1.9.4.data/scripts/list-bad-xrefs +34 -0
  43. pyflyby-1.9.4.data/scripts/prune-broken-imports +34 -0
  44. pyflyby-1.9.4.data/scripts/pyflyby-diff +34 -0
  45. pyflyby-1.9.4.data/scripts/reformat-imports +27 -0
  46. pyflyby-1.9.4.data/scripts/replace-star-imports +37 -0
  47. pyflyby-1.9.4.data/scripts/tidy-imports +191 -0
  48. pyflyby-1.9.4.data/scripts/transform-imports +47 -0
  49. pyflyby-1.9.4.dist-info/LICENSE.txt +23 -0
  50. pyflyby-1.9.4.dist-info/METADATA +507 -0
  51. pyflyby-1.9.4.dist-info/RECORD +54 -0
  52. pyflyby-1.9.4.dist-info/WHEEL +5 -0
  53. pyflyby-1.9.4.dist-info/entry_points.txt +3 -0
  54. pyflyby-1.9.4.dist-info/top_level.txt +1 -0
pyflyby/_log.py ADDED
@@ -0,0 +1,199 @@
1
+ # pyflyby/_log.py.
2
+ # Copyright (C) 2011, 2012, 2013, 2014, 2015, 2018 Karl Chen.
3
+ # License: MIT http://opensource.org/licenses/MIT
4
+
5
+
6
+
7
+ import builtins
8
+ from contextlib import contextmanager
9
+ import logging
10
+ from logging import Handler, Logger
11
+ import os
12
+ import sys
13
+
14
+
15
+ class _PyflybyHandler(Handler):
16
+
17
+ _pre_log_function = None
18
+ _logged_anything_during_context = False
19
+
20
+ _interactive_prefix = "\033[0m\033[33m[PYFLYBY]\033[0m "
21
+ _noninteractive_prefix = "[PYFLYBY] "
22
+
23
+ def emit(self, record):
24
+ """
25
+ Emit a log record.
26
+ """
27
+ try:
28
+ # Call pre-log hook.
29
+ if self._pre_log_function is not None:
30
+ if not self._logged_anything_during_context:
31
+ self._pre_log_function()
32
+ self._logged_anything_during_context = True
33
+ # Format (currently a no-op).
34
+ msg = self.format(record)
35
+ # Add prefix per line.
36
+ if _is_ipython() or _is_interactive(sys.stderr):
37
+ prefix = self._interactive_prefix
38
+ else:
39
+ prefix = self._noninteractive_prefix
40
+ msg = ''.join(["%s%s\n" % (prefix, line) for line in msg.splitlines()])
41
+ # First, flush stdout, to make sure that stdout and stderr don't get
42
+ # interleaved. Normally this is automatic, but when stdout is piped,
43
+ # it can be necessary to force a flush to avoid interleaving.
44
+ sys.stdout.flush()
45
+ # Write log message.
46
+ if sys.stderr.__class__.__module__.startswith("prompt_toolkit"):
47
+ with _PromptToolkitStdoutProxyRawCtx(sys.stderr):
48
+ sys.stderr.write(msg)
49
+ sys.stderr.flush()
50
+ else:
51
+ sys.stderr.write(msg)
52
+ # Flush now - we don't want any interleaving of stdout/stderr.
53
+ sys.stderr.flush()
54
+ except (KeyboardInterrupt, SystemExit):
55
+ raise
56
+ except:
57
+ self.handleError(record)
58
+
59
+ @contextmanager
60
+ def HookCtx(self, pre, post):
61
+ """
62
+ Enter a context where:
63
+ * ``pre`` is called before the first time a log record is emitted
64
+ during the context, and
65
+ * ``post`` is called at the end of the context, if any log records
66
+ were emitted during the context.
67
+
68
+ :type pre:
69
+ ``callable``
70
+ :param pre:
71
+ Function to call before the first time something is logged during
72
+ this context.
73
+ :type post:
74
+ ``callable``
75
+ :param post:
76
+ Function to call before returning from the context, if anything was
77
+ logged during the context.
78
+ """
79
+ assert self._pre_log_function is None
80
+ self._pre_log_function = pre
81
+ try:
82
+ yield
83
+ finally:
84
+ if self._logged_anything_during_context:
85
+ post()
86
+ self._logged_anything_during_context = False
87
+ self._pre_log_function = None
88
+
89
+
90
+ def _is_interactive(file):
91
+ filemod = type(file).__module__
92
+ if filemod.startswith("IPython.") or filemod.startswith("prompt_toolkit."):
93
+ # Inside IPython notebook/kernel
94
+ return True
95
+ try:
96
+ fileno = file.fileno()
97
+ except Exception:
98
+ return False # dunno
99
+ return os.isatty(fileno)
100
+
101
+
102
+ def _is_ipython():
103
+ """
104
+ Returns true if we're currently running inside IPython.
105
+ """
106
+ # This currently only works for versions of IPython that are modern enough
107
+ # to install 'builtins.get_ipython()'.
108
+ if 'IPython' not in sys.modules:
109
+ return False
110
+ if not hasattr(builtins, "get_ipython"):
111
+ return False
112
+ ip = builtins.get_ipython()
113
+ if ip is None:
114
+ return False
115
+ return True
116
+
117
+
118
+ @contextmanager
119
+ def _PromptToolkitStdoutProxyRawCtx(proxy):
120
+ """
121
+ Hack to defeat the "feature" where
122
+ prompt_toolkit.interface._StdoutProxy(sys.stderr) causes ANSI escape codes
123
+ to not be written.
124
+ """
125
+ # prompt_toolkit replaces sys.stderr with a proxy object. This proxy
126
+ # object replaces ESC (\xb1) with '?'. That breaks our colorization of
127
+ # the [PYFLYBY] log prefix. To work around this, we need to temporarily
128
+ # set _StdoutProxy._raw to True during the write() call. However, the
129
+ # write() call actually just stores a lambda to be executed later, and
130
+ # that lambda references self._raw by reference. So we can't just set
131
+ # _raw before we call sys.stderr.write(), since the _raw variable is not
132
+ # read yet at that point. We need to hook the internals so that we store
133
+ # a wrapped lambda which temporarily sets _raw to True. Yuck, this is so
134
+ # brittle. Tested with prompt_toolkit 1.0.15.
135
+ if not hasattr(type(proxy), '_do') or not hasattr(proxy, '_raw'):
136
+ yield
137
+ return
138
+ MISSING = object()
139
+ prev = proxy.__dict__.get('_do', MISSING)
140
+ original_do = proxy._do
141
+ def wrapped_do_raw(self, func):
142
+ def wrapped_func():
143
+ prev_raw = self._raw
144
+ try:
145
+ self._raw = True
146
+ func()
147
+ finally:
148
+ self._raw = prev_raw
149
+ original_do(wrapped_func)
150
+ try:
151
+ proxy._do = wrapped_do_raw.__get__(proxy)
152
+ yield
153
+ finally:
154
+ if prev is MISSING:
155
+ proxy.__dict__.pop('_do', None)
156
+ else:
157
+ proxy.__dict__ = prev
158
+
159
+
160
+ class PyflybyLogger(Logger):
161
+
162
+ _LEVELS = dict( (k, getattr(logging, k))
163
+ for k in ['DEBUG', 'INFO', 'WARNING', 'ERROR'] )
164
+
165
+ def __init__(self, name, level):
166
+ Logger.__init__(self, name)
167
+ handler = _PyflybyHandler()
168
+ self.addHandler(handler)
169
+ self.set_level(level)
170
+
171
+ def set_level(self, level):
172
+ """
173
+ Set the pyflyby logger's level to ``level``.
174
+
175
+ :type level:
176
+ ``str``
177
+ """
178
+ if isinstance(level, int):
179
+ level_num = level
180
+ else:
181
+ try:
182
+ level_num = self._LEVELS[level.upper()]
183
+ except KeyError:
184
+ raise ValueError("Bad log level %r" % (level,))
185
+ Logger.setLevel(self, level_num)
186
+
187
+ @property
188
+ def debug_enabled(self):
189
+ return self.level <= logging.DEBUG
190
+
191
+ @property
192
+ def info_enabled(self):
193
+ return self.level <= logging.INFO
194
+
195
+ def HookCtx(self, pre, post):
196
+ return self.handlers[0].HookCtx(pre, post)
197
+
198
+
199
+ logger = PyflybyLogger('pyflyby', os.getenv("PYFLYBY_LOG_LEVEL") or "INFO")