summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-10-10 08:19:09 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-10-10 12:10:10 -0400
commit84899500cb453cb6081e3afcd52fca1c713e79e7 (patch)
tree024dcdf7063416f3e803ecad27a5a24d0f2b4b0a /src
parent1af7e98c1afeeeb40747688fc5f393f98062f6a3 (diff)
downloadbcfg2-84899500cb453cb6081e3afcd52fca1c713e79e7.tar.gz
bcfg2-84899500cb453cb6081e3afcd52fca1c713e79e7.tar.bz2
bcfg2-84899500cb453cb6081e3afcd52fca1c713e79e7.zip
moved track_statistics to plugin helpers to make it usable by plugins
Diffstat (limited to 'src')
-rw-r--r--src/lib/Bcfg2/Server/Core.py30
-rw-r--r--src/lib/Bcfg2/Server/Plugin/helpers.py40
2 files changed, 40 insertions, 30 deletions
diff --git a/src/lib/Bcfg2/Server/Core.py b/src/lib/Bcfg2/Server/Core.py
index 3b8c79a9e..6ab18e36f 100644
--- a/src/lib/Bcfg2/Server/Core.py
+++ b/src/lib/Bcfg2/Server/Core.py
@@ -16,8 +16,9 @@ import Bcfg2.Logger
import Bcfg2.Server.FileMonitor
from Bcfg2.Cache import Cache
import Bcfg2.Statistics
-from Bcfg2.Compat import xmlrpclib, reduce, wraps # pylint: disable=W0622
-from Bcfg2.Server.Plugin import PluginInitError, PluginExecutionError
+from Bcfg2.Compat import xmlrpclib, reduce # pylint: disable=W0622
+from Bcfg2.Server.Plugin import PluginInitError, PluginExecutionError, \
+ track_statistics
try:
import psyco
@@ -35,31 +36,6 @@ def exposed(func):
return func
-class track_statistics(object): # pylint: disable=C0103
- """ decorator that tracks execution time for the given
- function """
-
- def __init__(self, name=None):
- self.name = name
-
- def __call__(self, func):
- if self.name is None:
- self.name = func.__name__
-
- @wraps(func)
- def inner(obj, *args, **kwargs):
- """ The decorated function """
- name = "%s:%s" % (obj.__class__.__name__, self.name)
-
- start = time.time()
- try:
- return func(obj, *args, **kwargs)
- finally:
- Bcfg2.Statistics.stats.add_value(name, time.time() - start)
-
- return inner
-
-
def sort_xml(node, key=None):
""" sort XML in a deterministic fashion """
for child in node:
diff --git a/src/lib/Bcfg2/Server/Plugin/helpers.py b/src/lib/Bcfg2/Server/Plugin/helpers.py
index 74f4dad8b..165f35f22 100644
--- a/src/lib/Bcfg2/Server/Plugin/helpers.py
+++ b/src/lib/Bcfg2/Server/Plugin/helpers.py
@@ -4,12 +4,14 @@ import os
import re
import sys
import copy
+import time
import logging
import operator
import lxml.etree
import Bcfg2.Server
import Bcfg2.Options
-from Bcfg2.Compat import CmpMixin
+import Bcfg2.Statistics
+from Bcfg2.Compat import CmpMixin, wraps
from Bcfg2.Server.Plugin.base import Debuggable, Plugin
from Bcfg2.Server.Plugin.interfaces import Generator
from Bcfg2.Server.Plugin.exceptions import SpecificityError, PluginInitError, \
@@ -77,6 +79,38 @@ def bind_info(entry, metadata, infoxml=None, default=DEFAULT_FILE_METADATA):
entry.set(attr, val)
+class track_statistics(object): # pylint: disable=C0103
+ """ Decorator that tracks execution time for the given
+ :class:`Plugin` method for reporting via ``bcfg2-admin perf`` """
+
+ def __init__(self, name=None):
+ """
+ :param name: The name under which statistics for this function
+ will be tracked. By default the name of the
+ function will be used.
+ :type name: string
+ """
+ # if this is None, it will be set later during __call_
+ self.name = name
+
+ def __call__(self, func):
+ if self.name is None:
+ self.name = func.__name__
+
+ @wraps(func)
+ def inner(obj, *args, **kwargs):
+ """ The decorated function """
+ name = "%s:%s" % (obj.__class__.__name__, self.name)
+
+ start = time.time()
+ try:
+ return func(obj, *args, **kwargs)
+ finally:
+ Bcfg2.Statistics.stats.add_value(name, time.time() - start)
+
+ return inner
+
+
class DatabaseBacked(Plugin):
""" Provides capabilities for a plugin to read and write to a
database.
@@ -92,8 +126,8 @@ class DatabaseBacked(Plugin):
option = "use_database"
def _section(self):
- """ The section to look in for
- :attr:`DatabaseBacked.option` """
+ """ The section to look in for :attr:`DatabaseBacked.option`
+ """
return self.name.lower()
section = property(_section)