Complete overhaul of imports

Explicit is better than implicit.
This commit is contained in:
Alessio Sergi 2014-04-17 17:04:04 +02:00
parent db37ff4ad2
commit b7bbd69a65
30 changed files with 151 additions and 198 deletions

View File

@ -21,8 +21,8 @@ Init the Glances software
"""
# Import system lib
import sys
import signal
import sys
# Import Glances libs
# Note: others Glances libs will be imported optionnaly

View File

@ -21,21 +21,18 @@ Manage the Glances' client
"""
# Import system libs
import sys
import socket
import json
import socket
import sys
try:
from xmlrpc.client import ServerProxy, ProtocolError
except ImportError: # Python 2
from xmlrpclib import ServerProxy, ProtocolError
# Import Glances libs
from glances.core.glances_globals import __version__
from glances.outputs.glances_curses import glancesCurses
from glances.core.glances_stats import GlancesStatsClient
try:
# Python 2
from xmlrpclib import ServerProxy, ProtocolError
except ImportError:
# Python 3
from xmlrpc.client import ServerProxy, ProtocolError
from glances.outputs.glances_curses import glancesCurses
class GlancesClient():

View File

@ -17,24 +17,25 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__appname__ = 'glances'
# Import Glances lib
from glances.core.glances_globals import *
# Import system libs
import os
try:
# Python 2
from ConfigParser import RawConfigParser
from ConfigParser import NoOptionError
except ImportError:
# Python 3
from configparser import RawConfigParser
from configparser import NoOptionError
except ImportError: # Python 2
from ConfigParser import RawConfigParser
from ConfigParser import NoOptionError
# Import Glances lib
from glances.core.glances_globals import (
__appname__,
is_Linux,
is_python3,
work_path
)
class Config:
class Config(object):
"""
This class is used to access/read config file, if it exists

View File

@ -19,31 +19,31 @@
# Glances informations
__appname__ = 'glances'
__version__ = "2.0_Alpha02"
__author__ = "Nicolas Hennion <nicolas@nicolargo.com>"
__license__ = "LGPL"
__version__ = '2.0_Alpha02'
__author__ = 'Nicolas Hennion <nicolas@nicolargo.com>'
__license__ = 'LGPL'
# Import system libs
import sys
import os
import gettext
import locale
import os
import sys
# Import PsUtil
# Import psutil
try:
from psutil import __version__ as __psutil_version__
except ImportError:
print('PsUtil module not found. Glances cannot start.')
print('psutil module not found. Glances cannot start.')
sys.exit(1)
# PSutil version
# psutil version
psutil_version = tuple([int(num) for num in __psutil_version__.split('.')])
# Check PsUtil version
# Check psutil version
psutil_min_version = (2, 0, 0)
if (psutil_version < psutil_min_version):
print('PsUtil version %s detected.' % __psutil_version__)
print('PsUtil 2.0 or higher is needed. Glances cannot start.')
print('psutil version %s detected.' % __psutil_version__)
print('psutil 2.0 or higher is needed. Glances cannot start.')
sys.exit(1)
# Path definitions
@ -75,7 +75,7 @@ else:
locale_dir = None
gettext.install(gettext_domain, locale_dir)
# Instances shared between all Glances's scripts
# Instances shared between all Glances' scripts
# ===============================================
# glances_processes for processcount and processlist plugins

View File

@ -18,8 +18,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
import time
from datetime import datetime
import time
# Import Glances libs
from glances.core.glances_globals import glances_processes

View File

@ -21,15 +21,16 @@ Main Glances script
"""
# Import system libs
import sys
import os
import argparse
import sys
# Import Glances libs
from glances.core.glances_globals import __appname__, __version__, __author__, __license__
from glances.core.glances_globals import *
# !!! Todo: rename class
from glances.core.glances_config import Config
from glances.core.glances_globals import (
__appname__,
__psutil_version__,
__version__
)
class GlancesMain(object):
@ -76,7 +77,6 @@ class GlancesMain(object):
output_file = None
output_folder = None
def __init__(self):
# Init and manage command line arguments
self.init_arg()
@ -92,7 +92,6 @@ class GlancesMain(object):
# Load the configuration file
self.config.load()
def init_arg(self):
"""
Init all the command line arguments
@ -106,7 +105,7 @@ class GlancesMain(object):
self.parser.add_argument('-v', '--version',
action='version',
version=_('%s v%s with PsUtil v%s')
% (__appname__.capitalize(), __version__, psutil_version))
% (__appname__.capitalize(), __version__, __psutil_version__))
# Client mode: set the client IP/name
self.parser.add_argument('-C', '--config',
help=_('path to the configuration file'))
@ -191,7 +190,6 @@ class GlancesMain(object):
self.parser.add_argument('-f', '--file',
help=_('set the html output folder or csv file'))
def parse_arg(self):
"""
Parse command line argument
@ -266,7 +264,6 @@ class GlancesMain(object):
return args
def __get_password(self, description='', confirm=False):
"""
Read a password from the command line (with confirmation if confirm = True)
@ -288,21 +285,18 @@ class GlancesMain(object):
sys.stdout.write(_("[Warning] Passwords did not match, please try again...\n"))
return self.__get_password(description=description, confirm=confirm)
def is_standalone(self):
"""
Return True if Glances is running in standalone mode
"""
return not self.client_tag and not self.server_tag and not self.webserver_tag
def is_client(self):
"""
Return True if Glances is running in client mode
"""
return self.client_tag and not self.server_tag
def is_server(self):
"""
Return True if Glances is running in server mode

View File

@ -17,8 +17,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system lib
from psutil import process_iter, AccessDenied, NoSuchProcess
import psutil
# Import Glances lib
from glances.core.glances_globals import is_BSD, is_Mac, is_Windows
@ -136,7 +135,7 @@ class glancesProcesses:
# Get the process IO counters
proc_io = proc.io_counters()
io_new = [proc_io.read_bytes, proc_io.write_bytes]
except AccessDenied:
except psutil.AccessDenied:
# Access denied to process IO (no root account)
# Put 0 in all values (for sort) and io_tag = 0 (for display)
procstat['io_counters'] = [0, 0] + [0, 0]
@ -174,7 +173,7 @@ class glancesProcesses:
time_since_update = getTimeSinceLastUpdate('process_disk')
# For each existing process...
for proc in process_iter():
for proc in psutil.process_iter():
try:
# Get stats using the PSUtil
procstat = self.__get_process_stats(proc)
@ -200,7 +199,7 @@ class glancesProcesses:
self.processcount['thread'] += proc.num_threads()
except:
pass
except (NoSuchProcess, AccessDenied):
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
else:
# Update processlist

View File

@ -18,34 +18,23 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
import sys
import socket
import json
from base64 import b64decode
from hashlib import md5
import json
import socket
import sys
try:
from xmlrpc.server import SimpleXMLRPCRequestHandler
from xmlrpc.server import SimpleXMLRPCServer
except ImportError: # Python 2
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
from SimpleXMLRPCServer import SimpleXMLRPCServer
# Import Glances libs
from glances.core.glances_globals import __version__
from glances.core.glances_stats import GlancesStatsServer
from glances.core.glances_timer import Timer
# Other imports
try:
# Python 2
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
from SimpleXMLRPCServer import SimpleXMLRPCServer
except ImportError:
# Python 3
from xmlrpc.server import SimpleXMLRPCRequestHandler
from xmlrpc.server import SimpleXMLRPCServer
try:
# Python 2
from xmlrpclib import ServerProxy, ProtocolError
except ImportError:
# Python 3
from xmlrpc.client import ServerProxy, ProtocolError
class GlancesXMLRPCHandler(SimpleXMLRPCRequestHandler):
"""

View File

@ -17,13 +17,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
import collections
import os
import sys
import collections
# Import Glances libs
from glances.core.glances_globals import *
class GlancesStats(object):

View File

@ -17,19 +17,15 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system lib
import os
import sys
try:
from bottle import Bottle, template, static_file, TEMPLATE_PATH
except ImportError:
print('Bottle module not found. Glances cannot start in web server mode.')
sys.exit(1)
# Import Glances lib
from glances.core.glances_timer import Timer
from glances.core.glances_globals import glances_logs, glances_processes
class glancesBottle:
"""

View File

@ -17,8 +17,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system lib
import sys
import threading
import msvcrt
try:
import colorconsole
import colorconsole.terminal
@ -26,12 +28,10 @@ except ImportError:
print('Colorconsole module not found. Glances cannot start in standalone mode.')
sys.exit(1)
import msvcrt
import threading
try:
import Queue as queue
except ImportError:
import queue
except ImportError: # Python 2
import Queue as queue
class ListenGetch(threading.Thread):

View File

@ -21,11 +21,11 @@
import sys
# Import Glances lib
from glances.core.glances_timer import Timer
from glances.core.glances_globals import glances_logs, glances_processes, is_Windows
from glances.core.glances_timer import Timer
# Import curses lib for "normal" operating system and consolelog for Windows
if (not is_Windows):
if not is_Windows:
try:
import curses
import curses.panel

View File

@ -21,8 +21,8 @@
from datetime import datetime
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_globals import glances_logs
from glances.plugins.glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):

View File

@ -20,8 +20,7 @@
Batinfo (batterie) plugin
"""
# Import system libs
# batinfo library (optional; Linux-only)
# Batinfo library (optional; Linux-only)
try:
import batinfo
except ImportError:

View File

@ -17,11 +17,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
# Check for PSUtil already done in the glances_core script
from psutil import cpu_count
import psutil
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
@ -55,8 +52,8 @@ class Plugin(GlancesPlugin):
# Return None if undefine
core_stats = {}
try:
core_stats["phys"] = cpu_count(logical=False)
core_stats["log"] = cpu_count()
core_stats["phys"] = psutil.cpu_count(logical=False)
core_stats["log"] = psutil.cpu_count()
except NameError:
core_stats = None

View File

@ -20,11 +20,8 @@
Glances CPU plugin
"""
# Import system libs
# Check for PSUtil already done in the glances_core script
from psutil import cpu_times_percent
import psutil
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
@ -57,7 +54,7 @@ class Plugin(GlancesPlugin):
"""
# Grab CPU using the PSUtil cpu_times_percent method
cputimespercent = cpu_times_percent(interval=0.0, percpu=False)
cputimespercent = psutil.cpu_times_percent(interval=0.0, percpu=False)
# Get all possible value for CPU stats
# user

View File

@ -20,12 +20,11 @@
Glances CPU plugin
"""
# Import system lib
from psutil import disk_io_counters
import psutil
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_timer import getTimeSinceLastUpdate
from glances.plugins.glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
@ -62,7 +61,7 @@ class Plugin(GlancesPlugin):
# write_bytes: number of bytes written
# read_time: time spent reading from disk (in milliseconds)
# write_time: time spent writing to disk (in milliseconds)
diskiocounters = disk_io_counters(perdisk=True)
diskiocounters = psutil.disk_io_counters(perdisk=True)
# Previous disk IO stats are stored in the diskio_old variable
diskio = []

View File

@ -17,10 +17,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system lib
from psutil import disk_partitions, disk_usage
import psutil
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
@ -48,14 +46,14 @@ class glancesGrabFs:
# Grab the stats using the PsUtil disk_partitions
# If 'all'=False return physical devices only (e.g. hard disks, cd-rom drives, USB keys)
# and ignore all others (e.g. memory partitions such as /dev/shm)
fs_stat = disk_partitions(all=False)
fs_stat = psutil.disk_partitions(all=False)
for fs in range(len(fs_stat)):
fs_current = {}
fs_current['device_name'] = fs_stat[fs].device
fs_current['fs_type'] = fs_stat[fs].fstype
fs_current['mnt_point'] = fs_stat[fs].mountpoint
# Grab the disk usage
fs_usage = disk_usage(fs_current['mnt_point'])
fs_usage = psutil.disk_usage(fs_current['mnt_point'])
fs_current['size'] = fs_usage.total
fs_current['used'] = fs_usage.used
fs_current['avail'] = fs_usage.free

View File

@ -22,8 +22,12 @@ Just a stupid plugin to display the help screen
"""
# Import Glances libs
from glances.core.glances_globals import (
__appname__,
__psutil_version__,
__version__
)
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_globals import __appname__, __version__, __psutil_version__
class Plugin(GlancesPlugin):

View File

@ -21,11 +21,11 @@ Glances load plugin
"""
# Import system libs
from os import getloadavg
import os
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
from glances.plugins.glances_core import Plugin as CorePlugin
from glances.plugins.glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
@ -60,7 +60,7 @@ class Plugin(GlancesPlugin):
# Get the load using the os standard lib
try:
load = getloadavg()
load = os.getloadavg()
except OSError:
self.stats = {}
else:

View File

@ -20,11 +20,8 @@
Glances virtual memory plugin
"""
# Import system libs
# Check for PSUtil already done in the glances_core script
from psutil import virtual_memory
import psutil
# from ..plugins.glances_plugin import GlancesPlugin
from glances.plugins.glances_plugin import GlancesPlugin
@ -53,7 +50,7 @@ class Plugin(GlancesPlugin):
"""
# Grab MEM using the PSUtil virtual_memory method
vm_stats = virtual_memory()
vm_stats = psutil.virtual_memory()
# Get all the memory stats (copy/paste of the PsUtil documentation)
# total: total physical memory available.

View File

@ -20,11 +20,8 @@
Glances swap memory plugin
"""
# Import system libs
# Check for PSUtil already done in the glances_core script
from psutil import swap_memory
import psutil
# from ..plugins.glances_plugin import GlancesPlugin
from glances.plugins.glances_plugin import GlancesPlugin
@ -53,7 +50,7 @@ class Plugin(GlancesPlugin):
"""
# Grab SWAP using the PSUtil swap_memory method
sm_stats = swap_memory()
sm_stats = psutil.swap_memory()
# Get all the swap stats (copy/paste of the PsUtil documentation)
# total: total swap memory in bytes

View File

@ -18,8 +18,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import Glances lib
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_monitor_list import monitorList as glancesMonitorList
from glances.plugins.glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):

View File

@ -21,11 +21,11 @@ Glances Network interface plugin
"""
# Import system libs
from psutil import net_io_counters
import psutil
# Import Glances lib
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_timer import getTimeSinceLastUpdate
from glances.plugins.glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):
@ -50,7 +50,6 @@ class Plugin(GlancesPlugin):
# Init stats
self.network_old = []
def update(self):
"""
Update network stats
@ -58,7 +57,7 @@ class Plugin(GlancesPlugin):
"""
# Grab network interface stat using the PsUtil net_io_counter method
netiocounters = net_io_counters(pernic=True)
netiocounters = psutil.net_io_counters(pernic=True)
# Previous network interface stats are stored in the network_old variable
network = []

View File

@ -20,9 +20,8 @@
CPU stats (per cpu)
"""
# Import system libs
# Check for PSUtil already done in the glances_core script
from psutil import cpu_times
# Check for psutil already done in the glances_core script
import psutil
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
@ -58,7 +57,7 @@ class Plugin(GlancesPlugin):
"""
# Grab CPU using the PSUtil cpu_times method
# Per-CPU
percputime = cpu_times(percpu=True)
percputime = psutil.cpu_times(percpu=True)
percputime_total = []
for i in range(len(percputime)):
percputime_total.append(percputime[i].user +

View File

@ -18,8 +18,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_globals import glances_processes
from glances.plugins.glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):

View File

@ -21,8 +21,8 @@
from datetime import timedelta
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
from glances.core.glances_globals import glances_processes
from glances.plugins.glances_plugin import GlancesPlugin
class Plugin(GlancesPlugin):

View File

@ -17,11 +17,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
# Check for PSUtil already done in the glances_core script
from psutil import __version__ as __psutil_version__
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin

View File

@ -17,8 +17,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
# sensors library (optional; Linux-only)
# Sensors library (optional; Linux-only)
try:
import sensors
except ImportError:
@ -28,7 +27,6 @@ except ImportError:
from glances.core.glances_globals import is_python3
from glances.plugins.glances_hddtemp import Plugin as HddTempPlugin
from glances.plugins.glances_plugin import GlancesPlugin
# from glances.core.glances_timer import getTimeSinceLastUpdate
class glancesGrabSensors: