Skip to content

Commit

Permalink
Fix flake8 complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
ZyX-I committed Mar 13, 2014
1 parent 076134a commit c3b4654
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 49 deletions.
1 change: 1 addition & 0 deletions powerline/bindings/vim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def buffer_name(buf):
return buf.name
else:
vim_bufname = vim_get_func('bufname')

def buffer_name(buf): # NOQA
try:
name = buf.name
Expand Down
3 changes: 2 additions & 1 deletion powerline/lib/file_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
from powerline.lib.monotonic import monotonic
from powerline.lib.inotify import INotify, INotifyError


def realpath(path):
return os.path.abspath(os.path.realpath(path))

class INotifyWatch(INotify):

class INotifyWatch(INotify):
is_stat_based = False

def __init__(self, expire_time=10):
Expand Down
6 changes: 3 additions & 3 deletions powerline/lib/humanize_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def humanize_bytes(num, suffix='B', si_prefix=False):
unit, decimals = unit_list[exponent]
if unit and not si_prefix:
unit = unit.upper() + 'i'
return '{{quotient:.{decimals}f}} {{unit}}{{suffix}}'\
.format(decimals=decimals)\
.format(quotient=quotient, unit=unit, suffix=suffix)
return ('{{quotient:.{decimals}f}} {{unit}}{{suffix}}'
.format(decimals=decimals)
.format(quotient=quotient, unit=unit, suffix=suffix))
10 changes: 6 additions & 4 deletions powerline/lib/tree_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
class NoSuchDir(ValueError):
pass


class BaseDirChanged(ValueError):
pass

class DirTooLarge(ValueError):

class DirTooLarge(ValueError):
def __init__(self, bdir):
ValueError.__init__(self, 'The directory {0} is too large to monitor. Try increasing the value in /proc/sys/fs/inotify/max_user_watches'.format(bdir))


def realpath(path):
return os.path.abspath(os.path.realpath(path))

class INotifyTreeWatcher(INotify):

class INotifyTreeWatcher(INotify):
is_dummy = False

def __init__(self, basedir, ignore_event=None):
Expand Down Expand Up @@ -141,7 +143,6 @@ def __call__(self):


class DummyTreeWatcher(object):

is_dummy = True

def __init__(self, basedir):
Expand All @@ -150,8 +151,8 @@ def __init__(self, basedir):
def __call__(self):
return False

class TreeWatcher(object):

class TreeWatcher(object):
def __init__(self, expire_time=10):
self.watches = {}
self.last_query_times = {}
Expand Down Expand Up @@ -203,6 +204,7 @@ def __call__(self, path, logger=None, ignore_event=None):
self.watches[path] = DummyTreeWatcher(path)
return False


if __name__ == '__main__':
w = INotifyTreeWatcher(sys.argv[-1])
w()
Expand Down
37 changes: 27 additions & 10 deletions powerline/lib/vcs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# vim:fileencoding=utf-8:noet
from __future__ import absolute_import

import os, errno
import os
import errno
from threading import Lock
from collections import defaultdict

vcs_props = (
('git', '.git', os.path.exists),
('mercurial', '.hg', os.path.isdir),
('bzr', '.bzr', os.path.isdir),
)


def generate_directories(path):
if os.path.isdir(path):
Expand All @@ -24,28 +19,34 @@ def generate_directories(path):
break
yield path


_file_watcher = None


def file_watcher():
global _file_watcher
if _file_watcher is None:
from powerline.lib.file_watcher import create_file_watcher
_file_watcher = create_file_watcher()
return _file_watcher


_branch_watcher = None


def branch_watcher():
global _branch_watcher
if _branch_watcher is None:
from powerline.lib.file_watcher import create_file_watcher
_branch_watcher = create_file_watcher()
return _branch_watcher


branch_name_cache = {}
branch_lock = Lock()
file_status_lock = Lock()


def get_branch_name(directory, config_file, get_func):
global branch_name_cache
with branch_lock:
Expand Down Expand Up @@ -79,8 +80,8 @@ def get_branch_name(directory, config_file, get_func):
branch_name_cache[config_file] = get_func(directory, config_file)
return branch_name_cache[config_file]

class FileStatusCache(dict):

class FileStatusCache(dict):
def __init__(self):
self.dirstate_map = defaultdict(set)
self.ignore_map = defaultdict(set)
Expand Down Expand Up @@ -112,8 +113,10 @@ def ignore_files(self, keypath):
for ignf in self.keypath_ignore_map[keypath]:
yield ignf


file_status_cache = FileStatusCache()


def get_file_status(directory, dirstate_file, file_path, ignore_file_name, get_func, extra_ignore_files=()):
global file_status_cache
keypath = file_path if os.path.isabs(file_path) else os.path.join(directory, file_path)
Expand Down Expand Up @@ -175,8 +178,8 @@ def get_file_status(directory, dirstate_file, file_path, ignore_file_name, get_f
file_status_cache[keypath] = ans = get_func(directory, file_path)
return ans

class TreeStatusCache(dict):

class TreeStatusCache(dict):
def __init__(self):
from powerline.lib.tree_watcher import TreeWatcher
self.tw = TreeWatcher()
Expand All @@ -196,14 +199,24 @@ def __call__(self, repo, logger):
logger.warn('Failed to check %s for changes, with error: %s'% key, e)
return self.cache_and_get(key, repo.status)


_tree_status_cache = None


def tree_status(repo, logger):
global _tree_status_cache
if _tree_status_cache is None:
_tree_status_cache = TreeStatusCache()
return _tree_status_cache(repo, logger)


vcs_props = (
('git', '.git', os.path.exists),
('mercurial', '.hg', os.path.isdir),
('bzr', '.bzr', os.path.isdir),
)


def guess(path):
for directory in generate_directories(path):
for vcs, vcs_dir, check in vcs_props:
Expand All @@ -219,8 +232,12 @@ def guess(path):
pass
return None


def debug():
''' To use run python -c "from powerline.lib.vcs import debug; debug()" some_file_to_watch '''
'''Test run guess(), repo.branch() and repo.status()
To use run python -c "from powerline.lib.vcs import debug; debug()"
some_file_to_watch '''
import sys
dest = sys.argv[-1]
repo = guess(os.path.abspath(dest))
Expand Down
11 changes: 7 additions & 4 deletions powerline/lib/vcs/bzr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@

from powerline.lib.vcs import get_branch_name, get_file_status


class CoerceIO(StringIO):
def write(self, arg):
if isinstance(arg, bytes):
arg = arg.decode('utf-8', 'replace')
return super(CoerceIO, self).write(arg)

state = None

nick_pat = re.compile(br'nickname\s*=\s*(.+)')


def branch_name_from_config_file(directory, config_file):
ans = None
try:
Expand All @@ -33,8 +34,11 @@ def branch_name_from_config_file(directory, config_file):
pass
return ans or os.path.basename(directory)

class Repository(object):

state = None


class Repository(object):
def __init__(self, directory):
if isinstance(directory, bytes):
directory = directory.decode(sys.getfilesystemencoding() or sys.getdefaultencoding() or 'utf-8')
Expand Down Expand Up @@ -75,7 +79,7 @@ def _status(self, directory, path):
return
if path:
ans = raw[:2]
if ans == 'I ': # Ignored
if ans == 'I ': # Ignored
ans = None
return ans
dirtied = untracked = ' '
Expand All @@ -90,4 +94,3 @@ def _status(self, directory, path):
def branch(self):
config_file = os.path.join(self.directory, '.bzr', 'branch', 'branch.conf')
return get_branch_name(self.directory, config_file, branch_name_from_config_file)

8 changes: 7 additions & 1 deletion powerline/lib/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import os
import re
import errno

from powerline.lib.vcs import get_branch_name as _get_branch_name, get_file_status


_ref_pat = re.compile(br'ref:\s*refs/heads/(.+)')


def branch_name_from_config_file(directory, config_file):
try:
with open(config_file, 'rb') as f:
Expand All @@ -19,6 +20,7 @@ def branch_name_from_config_file(directory, config_file):
return m.group(1).decode('utf-8', 'replace')
return raw[:7]


def git_directory(directory):
path = os.path.join(directory, '.git')
if os.path.isfile(path):
Expand All @@ -28,10 +30,12 @@ def git_directory(directory):
else:
return path


def get_branch_name(base_dir):
head = os.path.join(git_directory(base_dir), 'HEAD')
return _get_branch_name(base_dir, head, branch_name_from_config_file)


def do_status(directory, path, func):
if path:
gitd = git_directory(directory)
Expand All @@ -43,12 +47,14 @@ def do_status(directory, path, func):
path, '.gitignore', func, extra_ignore_files=tuple(os.path.join(gitd, x) for x in ('logs/HEAD', 'info/exclude')))
return func(directory, path)


def ignore_event(path, name):
# Ignore changes to the index.lock file, since they happen frequently and
# dont indicate an actual change in the working tree status
return False
return path.endswith('.git') and name == 'index.lock'


try:
import pygit2 as git

Expand Down
2 changes: 2 additions & 0 deletions powerline/lib/vcs/mercurial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from powerline.lib.vcs import get_branch_name, get_file_status


def branch_name_from_config_file(directory, config_file):
try:
with open(config_file, 'rb') as f:
Expand All @@ -15,6 +16,7 @@ def branch_name_from_config_file(directory, config_file):
except Exception:
return 'default'


class Repository(object):
__slots__ = ('directory', 'ui')

Expand Down
9 changes: 5 additions & 4 deletions powerline/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
except ImportError:
pass


def construct_returned_value(rendered_highlighted, segments, output_raw):
if output_raw:
return rendered_highlighted, ''.join((segment['_rendered_raw'] for segment in segments))
Expand Down Expand Up @@ -70,7 +71,7 @@ class Renderer(object):

character_translations = {ord(' '): NBSP}
'''Character translations for use in escape() function.
See documentation of ``unicode.translate`` for details.
'''

Expand Down Expand Up @@ -111,7 +112,7 @@ def __init__(self,

def strwidth(self, string):
'''Function that returns string width.
Is used to calculate the place given string occupies when handling
``width`` argument to ``.render()`` method. Must take east asian width
into account.
Expand All @@ -125,7 +126,7 @@ def strwidth(self, string):

def get_theme(self, matcher_info):
'''Get Theme object.
Is to be overridden by subclasses to support local themes, this variant
only returns ``.theme`` attribute.
Expand All @@ -152,7 +153,7 @@ def _get_highlighting(self, segment, mode):

def get_segment_info(self, segment_info, mode):
'''Get segment information.
Must return a dictionary containing at least ``home``, ``environ`` and
``getcwd`` keys (see documentation for ``segment_info`` attribute). This
implementation merges ``segment_info`` dictionary passed to
Expand Down
12 changes: 6 additions & 6 deletions powerline/renderers/vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ def hlstyle(self, fg=None, bg=None, attr=None):
hl_group['attr'].append('italic')
if attr & ATTR_UNDERLINE:
hl_group['attr'].append('underline')
hl_group['name'] = 'Pl_' + \
str(hl_group['ctermfg']) + '_' + \
str(hl_group['guifg']) + '_' + \
str(hl_group['ctermbg']) + '_' + \
str(hl_group['guibg']) + '_' + \
''.join(hl_group['attr'])
hl_group['name'] = ('Pl_' +
str(hl_group['ctermfg']) + '_' +
str(hl_group['guifg']) + '_' +
str(hl_group['ctermbg']) + '_' +
str(hl_group['guibg']) + '_' +
''.join(hl_group['attr']))
self.hl_groups[(fg, bg, attr)] = hl_group
vim.command('hi {group} ctermfg={ctermfg} guifg={guifg} guibg={guibg} ctermbg={ctermbg} cterm={attr} gui={attr}'.format(
group=hl_group['name'],
Expand Down
2 changes: 1 addition & 1 deletion powerline/segments/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def last_pipe_status(pl, segment_info):
@requires_segment_info
def mode(pl, segment_info, override={'vicmd': 'COMMND', 'viins': 'INSERT'}, default=None):
'''Return the current mode.
:param dict override:
dict for overriding mode strings.
:param str default:
Expand Down
Loading

0 comments on commit c3b4654

Please sign in to comment.