Add fixes for flake8 to that stuff

This commit is contained in:
ZyX 2013-04-06 18:45:57 +04:00
parent 42ee82c1de
commit e68bae6409
4 changed files with 35 additions and 30 deletions

View File

@ -12,8 +12,8 @@ from threading import RLock
from powerline.lib.monotonic import monotonic from powerline.lib.monotonic import monotonic
from powerline.lib.inotify import INotify, INotifyError from powerline.lib.inotify import INotify, INotifyError
class INotifyWatch(INotify):
class INotifyWatch(INotify):
is_stat_based = False is_stat_based = False
def __init__(self, expire_time=10): def __init__(self, expire_time=10):

View File

@ -4,13 +4,18 @@ from __future__ import unicode_literals, absolute_import
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import sys, os, errno import sys
import os
import errno
class INotifyError(Exception): class INotifyError(Exception):
pass pass
_inotify = None _inotify = None
def load_inotify(): def load_inotify():
''' Initialize the inotify library ''' ''' Initialize the inotify library '''
global _inotify global _inotify
@ -53,8 +58,8 @@ def load_inotify():
_inotify = (init1, add_watch, rm_watch, read) _inotify = (init1, add_watch, rm_watch, read)
return _inotify return _inotify
class INotify(object):
class INotify(object):
# See <sys/inotify.h> for the flags defined below # See <sys/inotify.h> for the flags defined below
# Supported events suitable for MASK parameter of INOTIFY_ADD_WATCH. # Supported events suitable for MASK parameter of INOTIFY_ADD_WATCH.
@ -98,7 +103,8 @@ class INotify(object):
NONBLOCK = 0x800 NONBLOCK = 0x800
def __init__(self, cloexec=True, nonblock=True): def __init__(self, cloexec=True, nonblock=True):
import ctypes, struct import ctypes
import struct
self._init1, self._add_watch, self._rm_watch, self._read = load_inotify() self._init1, self._add_watch, self._rm_watch, self._read = load_inotify()
flags = 0 flags = 0
if cloexec: if cloexec:
@ -170,6 +176,3 @@ class INotify(object):
def process_event(self, *args): def process_event(self, *args):
raise NotImplementedError() raise NotImplementedError()

View File

@ -1,26 +1,28 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:noet # vim:fileencoding=UTF-8:noet
from __future__ import (unicode_literals, absolute_import, print_function) from __future__ import (unicode_literals, absolute_import, print_function)
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import sys, os, errno import sys
import os
import errno
from time import sleep from time import sleep
from powerline.lib.monotonic import monotonic from powerline.lib.monotonic import monotonic
from powerline.lib.inotify import INotify, INotifyError from powerline.lib.inotify import INotify, INotifyError
class NoSuchDir(ValueError): class NoSuchDir(ValueError):
pass pass
class DirTooLarge(ValueError):
class DirTooLarge(ValueError):
def __init__(self, bdir): def __init__(self, bdir):
ValueError.__init__(self, 'The directory %s is too large to monitor. Try increasing the value in /proc/sys/fs/inotify/max_user_watches'%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))
class INotifyTreeWatcher(INotify): class INotifyTreeWatcher(INotify):
is_dummy = False is_dummy = False
def __init__(self, basedir): def __init__(self, basedir):
@ -49,13 +51,13 @@ class INotifyTreeWatcher(INotify):
# The entry could have been deleted between listdir() and # The entry could have been deleted between listdir() and
# add_watch(). # add_watch().
if top_level: if top_level:
raise NoSuchDir('The dir %s does not exist'%base) raise NoSuchDir('The dir {0} does not exist'.format(base))
return return
if e.errno == errno.EACCES: if e.errno == errno.EACCES:
# We silently ignore entries for which we dont have permission, # We silently ignore entries for which we dont have permission,
# unless they are the top level dir # unless they are the top level dir
if top_level: if top_level:
raise NoSuchDir('You do not have permission to monitor %s'%base) raise NoSuchDir('You do not have permission to monitor {0}'.format(base))
return return
raise raise
else: else:
@ -67,20 +69,22 @@ class INotifyTreeWatcher(INotify):
# The dir was deleted/replaced between the add_watch() # The dir was deleted/replaced between the add_watch()
# and listdir() # and listdir()
if top_level: if top_level:
raise NoSuchDir('The dir %s does not exist'%base) raise NoSuchDir('The dir {0} does not exist'.format(base))
return return
raise raise
for x in files: for x in files:
self.add_watches(os.path.join(base, x), top_level=False) self.add_watches(os.path.join(base, x), top_level=False)
elif top_level: elif top_level:
# The top level dir is a file, not good. # The top level dir is a file, not good.
raise NoSuchDir('The dir %s does not exist'%base) raise NoSuchDir('The dir {0} does not exist'.format(base))
def add_watch(self, path): def add_watch(self, path):
import ctypes import ctypes
bpath = path if isinstance(path, bytes) else path.encode(self.fenc) bpath = path if isinstance(path, bytes) else path.encode(self.fenc)
wd = self._add_watch(self._inotify_fd, ctypes.c_char_p(bpath), wd = self._add_watch(self._inotify_fd, ctypes.c_char_p(bpath),
self.DONT_FOLLOW | self.ONLYDIR | # Ignore symlinks and watch only directories # Ignore symlinks and watch only directories
self.DONT_FOLLOW | self.ONLYDIR |
self.MODIFY | self.CREATE | self.DELETE | self.MODIFY | self.CREATE | self.DELETE |
self.MOVE_SELF | self.MOVED_FROM | self.MOVED_TO | self.MOVE_SELF | self.MOVED_FROM | self.MOVED_TO |
self.ATTRIB | self.MOVE_SELF | self.DELETE_SELF) self.ATTRIB | self.MOVE_SELF | self.DELETE_SELF)
@ -88,7 +92,7 @@ class INotifyTreeWatcher(INotify):
eno = ctypes.get_errno() eno = ctypes.get_errno()
if eno == errno.ENOTDIR: if eno == errno.ENOTDIR:
return False return False
raise OSError(eno, 'Failed to add watch for: %s: %s'%(path, self.os.strerror(eno))) raise OSError(eno, 'Failed to add watch for: {0}: {1}'.format(path, self.os.strerror(eno)))
self.watched_dirs[path] = wd self.watched_dirs[path] = wd
self.watched_rmap[wd] = path self.watched_rmap[wd] = path
return True return True
@ -122,8 +126,8 @@ class INotifyTreeWatcher(INotify):
self.modified = False self.modified = False
return ret return ret
class DummyTreeWatcher(object):
class DummyTreeWatcher(object):
is_dummy = True is_dummy = True
def __init__(self, basedir): def __init__(self, basedir):
@ -132,8 +136,8 @@ class DummyTreeWatcher(object):
def __call__(self): def __call__(self):
return False return False
class TreeWatcher(object):
class TreeWatcher(object):
def __init__(self, expire_time=10): def __init__(self, expire_time=10):
self.watches = {} self.watches = {}
self.last_query_times = {} self.last_query_times = {}
@ -145,7 +149,7 @@ class TreeWatcher(object):
w = INotifyTreeWatcher(path) w = INotifyTreeWatcher(path)
except (INotifyError, DirTooLarge) as e: except (INotifyError, DirTooLarge) as e:
if logger is not None: if logger is not None:
logger.warn('Failed to watch path: %s with error: %s'%(path, e)) logger.warn('Failed to watch path: {0} with error: {1}'.format(path, e))
w = DummyTreeWatcher(path) w = DummyTreeWatcher(path)
self.watches[path] = w self.watches[path] = w
return w return w
@ -193,5 +197,3 @@ if __name__ == '__main__':
sleep(1) sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
raise SystemExit(0) raise SystemExit(0)

View File

@ -38,8 +38,8 @@ class TestLib(TestCase):
self.assertEqual(humanize_bytes(1000000000, si_prefix=True), '1.00 GB') self.assertEqual(humanize_bytes(1000000000, si_prefix=True), '1.00 GB')
self.assertEqual(humanize_bytes(1000000000, si_prefix=False), '953.7 MiB') self.assertEqual(humanize_bytes(1000000000, si_prefix=False), '953.7 MiB')
class TestFilesystemWatchers(TestCase):
class TestFilesystemWatchers(TestCase):
def do_test_for_change(self, watcher, path): def do_test_for_change(self, watcher, path):
import time import time
st = time.time() st = time.time()
@ -47,7 +47,7 @@ class TestFilesystemWatchers(TestCase):
if watcher(path): if watcher(path):
return return
time.sleep(0.1) time.sleep(0.1)
self.fail('The change to %s was not detected'%path) self.fail('The change to {0} was not detected'.format(path))
def test_file_watcher(self): def test_file_watcher(self):
from powerline.lib.file_watcher import create_file_watcher from powerline.lib.file_watcher import create_file_watcher
@ -192,6 +192,7 @@ HG_REPO = 'hg_repo' + os.environ.get('PYTHON', '')
BZR_REPO = 'bzr_repo' + os.environ.get('PYTHON', '') BZR_REPO = 'bzr_repo' + os.environ.get('PYTHON', '')
INOTIFY_DIR = 'inotify' + os.environ.get('PYTHON', '') INOTIFY_DIR = 'inotify' + os.environ.get('PYTHON', '')
def setUpModule(): def setUpModule():
global old_cwd global old_cwd
global old_HGRCPATH global old_HGRCPATH
@ -217,7 +218,6 @@ def setUpModule():
os.mkdir(INOTIFY_DIR) os.mkdir(INOTIFY_DIR)
def tearDownModule(): def tearDownModule():
global old_cwd global old_cwd
global old_HGRCPATH global old_HGRCPATH