Added options to change logfile and internet check server.

This commit is contained in:
Lukáš Mandák 2015-11-26 21:14:18 +01:00
parent 9a96b92f68
commit 6d211f823c
2 changed files with 45 additions and 20 deletions

View File

@ -1,10 +1,11 @@
import sys import logging
import os import os
import sys
from threading import Thread from threading import Thread
from i3pystatus.core.exceptions import ConfigError
from i3pystatus.core.imputil import ClassFinder
from i3pystatus.core import io, util from i3pystatus.core import io, util
from i3pystatus.core.exceptions import ConfigError
from i3pystatus.core.imputil import ClassFinder
from i3pystatus.core.modules import Module from i3pystatus.core.modules import Module
@ -39,17 +40,31 @@ class Status:
""" """
The main class used for registering modules and managing I/O The main class used for registering modules and managing I/O
:param standalone: Whether i3pystatus should read i3status-compatible input from `input_stream` :param bool standalone: Whether i3pystatus should read i3status-compatible input from `input_stream`.
:param interval: Update interval in seconds :param int interval: Update interval in seconds.
:param input_stream: A file-like object that provides the input stream, if `standalone` is False. :param input_stream: A file-like object that provides the input stream, if `standalone` is False.
:param click_events: Enable click events :param bool click_events: Enable click events, if `standalone` is True.
:param str logfile: Path to log file that will be used by i3pystatus.
:param tuple internet_check: Address of server that will be used to check for internet connection by :py:class:`.internet`.
""" """
def __init__(self, standalone=False, interval=1, input_stream=sys.stdin, click_events=True): def __init__(self, standalone=False, **kwargs):
self.modules = util.ModuleList(self, ClassFinder(Module))
self.standalone = standalone self.standalone = standalone
self.click_events = click_events self.click_events = kwargs.get("click_events", True)
if standalone: interval = kwargs.get("interval", 1)
input_stream = kwargs.get("input_stream", sys.stdin)
if "logfile" in kwargs:
logger = logging.getLogger("i3pystatus")
for handler in logger.handlers:
logger.removeHandler(handler)
handler = logging.FileHandler(kwargs["logfile"], delay=True)
logger.addHandler(handler)
logger.setLevel(logging.CRITICAL)
if "internet_check" in kwargs:
util.internet.address = kwargs["internet_check"]
self.modules = util.ModuleList(self, ClassFinder(Module))
if self.standalone:
self.io = io.StandaloneIO(self.click_events, self.modules, interval) self.io = io.StandaloneIO(self.click_events, self.modules, interval)
if self.click_events: if self.click_events:
self.command_endpoint = CommandEndpoint( self.command_endpoint = CommandEndpoint(

View File

@ -335,7 +335,7 @@ def require(predicate):
.. seealso:: .. seealso::
:py:func:`internet` :py:class:`internet`
""" """
@ -351,18 +351,28 @@ def require(predicate):
return decorator return decorator
def internet(): class internet:
""" """
Checks for a internet connection by connecting to a Google DNS Checks for internet connection by connecting to a server.
server.
Used server is determined by the `address` class variable which consists of
server host name and port number.
:rtype: bool
.. seealso::
:py:func:`require`
:returns: True if internet connection is available
""" """
try: address = ("google-public-dns-a.google.com", 53)
socket.create_connection(("google-public-dns-a.google.com", 53), 1).close()
return True def __new__(cls):
except OSError: try:
return False socket.create_connection(cls.address, 1).close()
return True
except OSError:
return False
def make_graph(values, lower_limit=0.0, upper_limit=100.0, style="blocks"): def make_graph(values, lower_limit=0.0, upper_limit=100.0, style="blocks"):