Omit using sys.modules if possible
Does not work though: still TypeError due to some global in multiproccessing that got None
This commit is contained in:
parent
c334be416d
commit
c429d9eeb2
|
@ -3,7 +3,12 @@
|
|||
import os
|
||||
import sys
|
||||
|
||||
from datetime import datetime
|
||||
import socket
|
||||
from multiprocessing import cpu_count
|
||||
|
||||
from powerline.lib import memoize, urllib_read, urllib_urlencode, add_divider_highlight_group
|
||||
from powerline.lib.vcs import guess
|
||||
|
||||
|
||||
def hostname(only_if_ssh=False):
|
||||
|
@ -12,7 +17,6 @@ def hostname(only_if_ssh=False):
|
|||
:param bool only_if_ssh:
|
||||
only return the hostname if currently in an SSH session
|
||||
'''
|
||||
import socket
|
||||
if only_if_ssh and not os.environ.get('SSH_CLIENT'):
|
||||
return None
|
||||
return socket.gethostname()
|
||||
|
@ -37,7 +41,6 @@ def user():
|
|||
|
||||
def branch():
|
||||
'''Return the current VCS branch.'''
|
||||
from powerline.lib.vcs import guess
|
||||
repo = guess(path=os.path.abspath(os.getcwd()))
|
||||
if repo:
|
||||
return repo.branch()
|
||||
|
@ -96,7 +99,6 @@ def date(format='%Y-%m-%d', istime=False):
|
|||
:param str format:
|
||||
strftime-style date format string
|
||||
'''
|
||||
from datetime import datetime
|
||||
return [{
|
||||
'contents': datetime.now().strftime(format),
|
||||
'highlight_group': (['time'] if istime else []) + ['date'],
|
||||
|
@ -106,8 +108,6 @@ def date(format='%Y-%m-%d', istime=False):
|
|||
|
||||
def fuzzy_time():
|
||||
'''Display the current time as fuzzy time, e.g. "quarter past six".'''
|
||||
from datetime import datetime
|
||||
|
||||
hour_str = ['twelve', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven']
|
||||
minute_str = {
|
||||
5: 'five past',
|
||||
|
@ -188,7 +188,6 @@ def uptime(format='{days:02d}d {hours:02d}h {minutes:02d}m'):
|
|||
'''
|
||||
try:
|
||||
import psutil
|
||||
from datetime import datetime
|
||||
seconds = int((datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).total_seconds())
|
||||
except ImportError:
|
||||
try:
|
||||
|
@ -361,11 +360,10 @@ def system_load(format='{avg:.1f}', threshold_good=1, threshold_bad=2):
|
|||
:param float threshold_bad:
|
||||
threshold for "bad load" highlighting
|
||||
'''
|
||||
import multiprocessing
|
||||
cpu_count = multiprocessing.cpu_count()
|
||||
cpu_num = cpu_count()
|
||||
ret = []
|
||||
for avg in os.getloadavg():
|
||||
normalized = avg / cpu_count
|
||||
normalized = avg / cpu_num
|
||||
if normalized < threshold_good:
|
||||
hl = 'system_load_good'
|
||||
elif normalized < threshold_bad:
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
from tests import vim
|
||||
|
||||
|
||||
globals().update(vim._init())
|
|
@ -8,6 +8,8 @@ import tests.vim as vim_module
|
|||
import sys
|
||||
import os
|
||||
import json
|
||||
from .lib import Args, urllib_read
|
||||
import tests.lib as lib
|
||||
|
||||
|
||||
VBLOCK = chr(ord('V') - 0x40)
|
||||
|
@ -55,20 +57,32 @@ class TestConfig(TestCase):
|
|||
finally:
|
||||
vim_module._g.pop('powerline_config_path')
|
||||
|
||||
def test_tmux(self):
|
||||
import powerline.segments.common
|
||||
from imp import reload
|
||||
reload(powerline.segments.common)
|
||||
old_urllib_read = powerline.segments.common.urllib_read
|
||||
powerline.segments.common.urllib_read = urllib_read
|
||||
from powerline.shell import ShellPowerline
|
||||
try:
|
||||
ShellPowerline(Args(ext=['tmux'])).renderer.render()
|
||||
finally:
|
||||
powerline.segments.common.urllib_read = old_urllib_read
|
||||
|
||||
|
||||
old_cwd = None
|
||||
|
||||
|
||||
def setUpModule():
|
||||
global old_cwd
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'path')))
|
||||
old_cwd = os.getcwd()
|
||||
sys.modules['vim'] = vim_module._get_module()
|
||||
from powerline.segments import vim
|
||||
globals()['vim'] = vim
|
||||
|
||||
|
||||
def tearDownModule():
|
||||
global old_cwd
|
||||
sys.modules.pop('vim')
|
||||
os.chdir(old_cwd)
|
||||
old_cwd = None
|
||||
sys.path.pop(0)
|
||||
|
|
|
@ -29,7 +29,7 @@ class TestShell(TestCase):
|
|||
class TestCommon(TestCase):
|
||||
def test_hostname(self):
|
||||
with replace_env('SSH_CLIENT', '192.168.0.12 40921 22'):
|
||||
with replace_module('socket', gethostname=lambda: 'abc'):
|
||||
with replace_module_module(common, 'socket', gethostname=lambda: 'abc'):
|
||||
self.assertEqual(common.hostname(), 'abc')
|
||||
self.assertEqual(common.hostname(only_if_ssh=True), 'abc')
|
||||
os.environ.pop('SSH_CLIENT')
|
||||
|
@ -46,10 +46,9 @@ class TestCommon(TestCase):
|
|||
self.assertEqual(common.user(), [{'contents': 'def', 'highlight_group': ['superuser', 'user']}])
|
||||
|
||||
def test_branch(self):
|
||||
vcslib = new_module('powerline.lib.vcs', guess=lambda path: Args(branch=lambda: os.path.basename(path)))
|
||||
with replace_module('powerline.lib.vcs', vcslib):
|
||||
with replace_module_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path))):
|
||||
self.assertEqual(common.branch(), 'tests')
|
||||
vcslib.guess = lambda path: None
|
||||
with replace_module_attr(common, 'guess', lambda path: None):
|
||||
self.assertEqual(common.branch(), None)
|
||||
|
||||
def test_cwd(self):
|
||||
|
@ -104,13 +103,13 @@ class TestCommon(TestCase):
|
|||
common.cwd(dir_limit_depth=2, dir_shorten_len=2),
|
||||
|
||||
def test_date(self):
|
||||
with replace_module('datetime', datetime=Args(now=lambda: Args(strftime=lambda fmt: fmt))):
|
||||
with replace_module_attr(common, 'datetime', Args(now=lambda: Args(strftime=lambda fmt: fmt))):
|
||||
self.assertEqual(common.date(), [{'contents': '%Y-%m-%d', 'highlight_group': ['date'], 'divider_highlight_group': None}])
|
||||
self.assertEqual(common.date(format='%H:%M', istime=True), [{'contents': '%H:%M', 'highlight_group': ['time', 'date'], 'divider_highlight_group': 'time:divider'}])
|
||||
|
||||
def test_fuzzy_time(self):
|
||||
time = Args(hour=0, minute=45)
|
||||
with replace_module('datetime', datetime=Args(now=lambda: time)):
|
||||
with replace_module_attr(common, 'datetime', Args(now=lambda: time)):
|
||||
self.assertEqual(common.fuzzy_time(), 'quarter to one')
|
||||
time.hour = 23
|
||||
time.minute = 59
|
||||
|
@ -134,7 +133,7 @@ class TestCommon(TestCase):
|
|||
|
||||
def test_system_load(self):
|
||||
with replace_module_module(common, 'os', getloadavg=lambda: (7.5, 3.5, 1.5)):
|
||||
with replace_module('multiprocessing', cpu_count=lambda: 2):
|
||||
with replace_module_attr(common, 'cpu_count', lambda: 2):
|
||||
self.assertEqual(common.system_load(),
|
||||
[{'contents': '7.5 ', 'highlight_group': ['system_load_ugly', 'system_load'], 'draw_divider': True, 'divider_highlight_group': 'background:divider'},
|
||||
{'contents': '3.5 ', 'highlight_group': ['system_load_bad', 'system_load'], 'draw_divider': False, 'divider_highlight_group': 'background:divider'},
|
||||
|
@ -311,14 +310,14 @@ old_cwd = None
|
|||
|
||||
def setUpModule():
|
||||
global old_cwd
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'path')))
|
||||
old_cwd = os.getcwd()
|
||||
os.chdir(os.path.dirname(__file__))
|
||||
sys.modules['vim'] = vim_module._get_module()
|
||||
from powerline.segments import vim
|
||||
globals()['vim'] = vim
|
||||
|
||||
|
||||
def tearDownModule():
|
||||
global old_cwd
|
||||
sys.modules.pop('vim')
|
||||
os.chdir(old_cwd)
|
||||
sys.path.pop(0)
|
||||
|
|
16
tests/vim.py
16
tests/vim.py
|
@ -234,22 +234,22 @@ class _Buffer(object):
|
|||
_buf_scopes.pop(bufnr)
|
||||
|
||||
|
||||
_module = None
|
||||
_dict = None
|
||||
|
||||
|
||||
def _get_module():
|
||||
global _module
|
||||
def _init():
|
||||
global _dict
|
||||
|
||||
if _module:
|
||||
return _module
|
||||
if _dict:
|
||||
return _dict
|
||||
|
||||
import imp
|
||||
_module = imp.new_module('vim')
|
||||
_dict = {}
|
||||
for varname, value in globals().items():
|
||||
if varname[0] != '_':
|
||||
setattr(_module, varname, value)
|
||||
_dict[varname] = value
|
||||
_new()
|
||||
return _module
|
||||
return _dict
|
||||
|
||||
|
||||
def _get_segment_info():
|
||||
|
|
Loading…
Reference in New Issue