powerline/client/powerline.py
ZyX 06211cbe63 Unify imports
Now imports follow the following structure:

1. __future__ line: exactly one line allowed:

        from __future__ import (unicode_literals, division, absolute_import, print_function)

   (powerline.shell is the only exception due to problems with argparse).
2. Standard python library imports in a form `import X`.
3. Standard python library imports in a form `from X import Y`.
4. and 5. 2. and 3. for third-party (non-python and non-powerline imports).
6. 3. for powerline non-test imports.
7. and 8. 2. and 3. for powerline testing module imports.

Each list entry is separated by exactly one newline from another import. If
there is module docstring it goes between `# vim:` comment and `__future__`
import. So the structure containing all items is the following:

    #!/usr/bin/env python
    # vim:fileencoding=utf-8:noet

    '''Powerline super module'''

    import sys

    from argparse import ArgumentParser

    import psutil

    from colormath.color_diff import delta_e_cie2000

    from powerline.lib.unicode import u

    import tests.vim as vim_module

    from tests import TestCase

.
2014-09-01 00:25:24 +04:00

100 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
import sys
import socket
import errno
import os
from locale import getpreferredencoding
try:
from posix import environ
except ImportError:
from os import environ
if len(sys.argv) < 2:
print('Must provide at least one argument.', file=sys.stderr)
raise SystemExit(1)
platform = sys.platform.lower()
use_filesystem = 'darwin' in platform
del platform
if sys.argv[1] == '--socket':
address = sys.argv[2]
if not use_filesystem:
address = '\0' + address
del sys.argv[1:3]
else:
address = ('/tmp/powerline-ipc-%d' if use_filesystem else '\0powerline-ipc-%d') % os.getuid()
sock = socket.socket(family=socket.AF_UNIX)
def eintr_retry_call(func, *args, **kwargs):
while True:
try:
return func(*args, **kwargs)
except EnvironmentError as e:
if getattr(e, 'errno', None) == errno.EINTR:
continue
raise
try:
eintr_retry_call(sock.connect, address)
except Exception:
# Run the powerline renderer
args = ['powerline-render'] + sys.argv[1:]
os.execvp('powerline-render', args)
fenc = getpreferredencoding() or 'utf-8'
def tobytes(s):
if isinstance(s, bytes):
return s
else:
return s.encode(fenc)
args = [tobytes('%x' % (len(sys.argv) - 1))]
args.extend((tobytes(s) for s in sys.argv[1:]))
try:
cwd = os.getcwd()
except EnvironmentError:
pass
else:
if not isinstance(cwd, bytes):
cwd = cwd.encode(fenc)
args.append(cwd)
args.extend((tobytes(k) + b'=' + tobytes(v) for k, v in environ.items()))
EOF = b'\0\0'
for a in args:
eintr_retry_call(sock.sendall, a + b'\0')
eintr_retry_call(sock.sendall, EOF)
received = []
while True:
r = sock.recv(4096)
if not r:
break
received.append(r)
sock.close()
if sys.version_info < (3,):
sys.stdout.write(b''.join(received))
else:
sys.stdout.buffer.write(b''.join(received))