"""
This module provides configured workloads.
"""

import logging
import shlex

from penchy.jobs.elements import Workload
from penchy.maven import MavenDependency


# TODO: we need a module that configures logging
log = logging.getLogger("Workload")


class Dacapo(Workload):
    """
    This class represents the workload for the `DaCapo Benchmark-Suite
    <http://dacapobench.org/>_.
    """
    DEPENDENCIES = set((
        MavenDependency(
            'org.scalabench.benchmarks',
            'scala-benchmark-suite',
            '0.1.0-20110908.085753-2',
            'http://repo.scalabench.org/snapshots/',
            filename='scala-benchmark-suite-0.1.0-SNAPSHOT.jar',
            checksum='fb68895a6716cc5e77f62ed7992d027b1dbea355'
        ),
    ))

    BENCHMARKS = set((  'avrora'
                      , 'batik'
                      , 'eclipse'
                      , 'fop'
                      , 'h2'
                      , 'jython'
                      , 'luindex'
                      , 'lusearch'
                      , 'pmd'
                      , 'sunflow'
                      , 'tomcat'
                      , 'tradebeans'
                      , 'tradesoap'
                      , 'xalan'))

    def __init__(self, benchmark, iterations=1,
                 benchmark_args='', harness_args=''):
        """
        :param benchmark: benchmark to execute
        :param iterations: Number of iterations in an invocation.
        :param benchmark_args: String of additional arguments for benchmark
        :param harness_args: String of additional arguments for harness
        """
        super(Dacapo, self).__init__()

        self.benchmark = benchmark
        self.iterations = iterations

        self.harness_args = harness_args
        self.benchmark_args = benchmark_args

        # XXX: call check here?

    @property
    def arguments(self):
        """
        The arguments to call the workload in the current configuration.
        """
        return ['Harness'] + \
               ['-n', str(self.iterations)] + shlex.split(self.harness_args) + \
               [self.benchmark] + shlex.split(self.benchmark_args)

    def check(self):
        """
        Check if workload is valid. Will log errors.

        :returns: if workload is valid
        :rtype: bool
        """
        valid_benchmark = self.benchmark in self.__class__.BENCHMARKS
        if not valid_benchmark:
            log.critical("{0} is not a valid benchmark for {1}".format(
                self.benchmark, self.__class__.__name__))

        return all((valid_benchmark, ))


class ScalaBench(Dacapo):
    """
    This class represents the workload for the `Scalabench Benchmark-Suite
    <http://scalabench.org/>_.
    """
    BENCHMARKS = set((
        # dacapo
        'avrora'
        , 'batik'
        , 'eclipse'
        , 'fop'
        , 'h2'
        , 'jython'
        , 'luindex'
        , 'lusearch'
        , 'pmd'
        , 'sunflow'
        , 'tomcat'
        , 'tradebeans'
        , 'tradesoap'
        , 'xalan'
        # scalabench
        , 'actors'
        , 'apparat'
        , 'dummy'
        , 'factorie'
        , 'kiama'
        , 'scalac'
        , 'scaladoc'
        , 'scalap'
        , 'scalariform'
        , 'scalatest'
        , 'scalaxb'
        , 'specs'
        , 'tmt'))

@@ -1273,53 +1273,9 @@
 s=1,
-%0A                 benchmark_args='', harness_
+ 
 args
@@ -1339,16 +1339,45 @@
 execute%0A
+        :type benchmark: str%0A
         
@@ -1447,146 +1447,122 @@
    :
-param benchmark_args: String of additional arguments for benchmark%0A        :param harness_args: String of additional arguments for harness
+type iterations: int%0A        :param args: additional arguments for harness (shell escaped)%0A        :type args: str
 %0A   
@@ -1700,113 +1700,19 @@
 elf.
-harness_args = harness_args%0A        self.benchmark_args = benchmark_args%0A%0A        # XXX: call check here?
+args = args
 %0A%0A  
@@ -1943,16 +1943,8 @@
 elf.
-harness_
 args
@@ -1984,43 +1984,8 @@
 ark%5D
- + shlex.split(self.benchmark_args)
 %0A%0A  

Remove benchmark args from Dacapo.