summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/leap/bitmask/util')
-rw-r--r--src/leap/bitmask/util/__init__.py7
-rw-r--r--src/leap/bitmask/util/averages.py92
-rw-r--r--src/leap/bitmask/util/keyring_helpers.py19
-rw-r--r--src/leap/bitmask/util/leap_argparse.py4
-rw-r--r--src/leap/bitmask/util/log_silencer.py9
5 files changed, 120 insertions, 11 deletions
diff --git a/src/leap/bitmask/util/__init__.py b/src/leap/bitmask/util/__init__.py
index 78efcb6e..f762a350 100644
--- a/src/leap/bitmask/util/__init__.py
+++ b/src/leap/bitmask/util/__init__.py
@@ -20,6 +20,13 @@ Some small and handy functions.
import datetime
import os
+from leap.bitmask.config import flags
+from leap.common.config import get_path_prefix as common_get_path_prefix
+
+
+def get_path_prefix():
+ return common_get_path_prefix(flags.STANDALONE)
+
def first(things):
"""
diff --git a/src/leap/bitmask/util/averages.py b/src/leap/bitmask/util/averages.py
new file mode 100644
index 00000000..65953f8f
--- /dev/null
+++ b/src/leap/bitmask/util/averages.py
@@ -0,0 +1,92 @@
+# -*- coding: utf-8 -*-
+# averages.py
+# Copyright (C) 2013 LEAP
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+Utility class for moving averages.
+
+It is used in the status panel widget for displaying up and down
+download rates.
+"""
+from leap.bitmask.util import first
+
+
+class RateMovingAverage(object):
+ """
+ Moving window average for calculating
+ upload and download rates.
+ """
+ SAMPLE_SIZE = 5
+
+ def __init__(self):
+ """
+ Initializes an empty array of fixed size
+ """
+ self.reset()
+
+ def reset(self):
+ self._data = [None for i in xrange(self.SAMPLE_SIZE)]
+
+ def append(self, x):
+ """
+ Appends a new data point to the collection.
+
+ :param x: A tuple containing timestamp and traffic points
+ in the form (timestamp, traffic)
+ :type x: tuple
+ """
+ self._data.pop(0)
+ self._data.append(x)
+
+ def get(self):
+ """
+ Gets the collection.
+ """
+ return self._data
+
+ def get_average(self):
+ """
+ Gets the moving average.
+ """
+ data = filter(None, self.get())
+ traff = [traffic for (ts, traffic) in data]
+ times = [ts for (ts, traffic) in data]
+
+ try:
+ deltatraffic = traff[-1] - first(traff)
+ deltat = (times[-1] - first(times)).seconds
+ except IndexError:
+ deltatraffic = 0
+ deltat = 0
+
+ try:
+ rate = float(deltatraffic) / float(deltat) / 1024
+ except ZeroDivisionError:
+ rate = 0
+
+ # In some cases we get negative rates
+ if rate < 0:
+ rate = 0
+
+ return rate
+
+ def get_total(self):
+ """
+ Gets the total accumulated throughput.
+ """
+ try:
+ return self._data[-1][1] / 1024
+ except TypeError:
+ return 0
diff --git a/src/leap/bitmask/util/keyring_helpers.py b/src/leap/bitmask/util/keyring_helpers.py
index 8f354f28..4b3eb57f 100644
--- a/src/leap/bitmask/util/keyring_helpers.py
+++ b/src/leap/bitmask/util/keyring_helpers.py
@@ -14,16 +14,21 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
"""
Keyring helpers.
"""
+import logging
import keyring
+from keyring.backends.file import EncryptedKeyring, PlaintextKeyring
+
+logger = logging.getLogger(__name__)
+
+
OBSOLETE_KEYRINGS = [
- keyring.backends.file.EncryptedKeyring,
- keyring.backends.file.PlaintextKeyring
+ EncryptedKeyring,
+ PlaintextKeyring
]
@@ -34,4 +39,10 @@ def has_keyring():
:rtype: bool
"""
kr = keyring.get_keyring()
- return kr is not None and kr.__class__ not in OBSOLETE_KEYRINGS
+ klass = kr.__class__
+ logger.debug("Selected keyring: %s" % (klass,))
+
+ canuse = kr is not None and klass not in OBSOLETE_KEYRINGS
+ if not canuse:
+ logger.debug("Not using this keyring since it is obsolete")
+ return canuse
diff --git a/src/leap/bitmask/util/leap_argparse.py b/src/leap/bitmask/util/leap_argparse.py
index bc21a9cf..afe5be48 100644
--- a/src/leap/bitmask/util/leap_argparse.py
+++ b/src/leap/bitmask/util/leap_argparse.py
@@ -27,7 +27,7 @@ def build_parser():
All the options for the leap arg parser
Some of these could be switched on only if debug flag is present!
"""
- epilog = "Copyright 2012 The LEAP Encryption Access Project"
+ epilog = "Copyright 2012-2013 The LEAP Encryption Access Project"
parser = argparse.ArgumentParser(description="""
Launches Bitmask""", epilog=epilog)
parser.add_argument('-d', '--debug', action="store_true",
@@ -50,6 +50,8 @@ Launches Bitmask""", epilog=epilog)
help='Makes Bitmask use standalone'
'directories for configuration and binary'
'searching')
+ parser.add_argument('-V', '--version', action="store_true",
+ help='Displays Bitmask version and exits')
# Not in use, we might want to reintroduce them.
#parser.add_argument('-i', '--no-provider-checks',
diff --git a/src/leap/bitmask/util/log_silencer.py b/src/leap/bitmask/util/log_silencer.py
index 09aa2cff..b9f69ad2 100644
--- a/src/leap/bitmask/util/log_silencer.py
+++ b/src/leap/bitmask/util/log_silencer.py
@@ -21,7 +21,7 @@ import logging
import os
import re
-from leap.common.config import get_path_prefix
+from leap.bitmask.util import get_path_prefix
class SelectiveSilencerFilter(logging.Filter):
@@ -48,12 +48,11 @@ class SelectiveSilencerFilter(logging.Filter):
'leap.common.events',
)
- def __init__(self, standalone=False):
+ def __init__(self):
"""
Tries to load silencer rules from the default path,
or load from the SILENCER_RULES tuple if not found.
"""
- self.standalone = standalone
self.rules = None
if os.path.isfile(self._rules_path):
self.rules = self._load_rules()
@@ -65,9 +64,7 @@ class SelectiveSilencerFilter(logging.Filter):
"""
The configuration file for custom ignore rules.
"""
- return os.path.join(
- get_path_prefix(standalone=self.standalone),
- "leap", self.CONFIG_NAME)
+ return os.path.join(get_path_prefix(), "leap", self.CONFIG_NAME)
def _load_rules(self):
"""