# Path: igor/common.py
# class routes(object):
#     static_ui_data = '/ui/<filename>'
#     static_data = '/static/data/<filename>'
# 
#     testsuite_submit = '/jobs/submit/<tname>/with/<pname>/on/<hname>'
# 
#     jobs = '/jobs'
#     job = '/jobs/<cookie>'
#     job_start = '/jobs/<cookie>/start'
#     job_abort = '/jobs/<cookie>/abort'
#     job_status = '/jobs/<cookie>/status'
#     job_report = '/jobs/<cookie>/report'
#     job_report_junit = '/jobs/<cookie>/report/junit'
#     job_testsuite = '/jobs/<cookie>/testsuite'
#     job_artifacts = '/jobs/<cookie>/artifacts'
#     job_artifacts_archive = '/jobs/<cookie>/archive'  # FIXME
#     job_artifact = '/jobs/<cookie>/artifacts/<name>'
# 
#     job_step_skip = '/jobs/<cookie>/step/<n:int>/skip'
#     job_step_finish = '/jobs/<cookie>/step/<n:int>/<result:re:success|failed>'
#     job_step_result = '/jobs/<cookie>/step/<n:int>/result'
#     job_step_annotate = '/jobs/<cookie>/step/current/annotate'
# 
#     job_set_boot_profile = '/jobs/<cookie>/set/enable_pxe/<enable_pxe>'
#     job_set_kernelargs = '/jobs/<cookie>/set/kernelargs/<kernelargs>'
# 
#     job_bootstrap = '/testjob/<cookie>'
# 
#     testsuites = '/testsuites'
#     testsuites_validate = '/testsuites/validate'
#     testsuite_summary = '/testsuites/<name>/summary'
#     testsuite_archive = '/testsuites/<name>/download'
# 
#     testplans = '/testplans'
#     testplan = '/testplans/<name>'
#     testplan_start = '/testplans/<name>/submit'
#     testplan_abort = '/testplans/<name>/abort'
#     testplan_status = '/testplans/<name>/status'
#     testplan_report = '/testplans/<name>/report'
#     testplan_report_junit = '/testplans/<name>/report/junit'
#     testplan_summary = ''
# 
#     profiles = '/profiles'
#     profile = '/profiles/<pname>'
#     profile_delete = '/profiles/<pname>/remove'
#     profile_set_kernelargs = '/profiles/<pname>/kargs'
# 
#     hosts = '/hosts'
# 
#     testcase_source = '/testcases/<suitename>/<setname>/<casename>/source'
# 
#     server_log = '/server/log'
# 
#     datastore = '/store'
#     datastore_file = '/store/<filename>'
#     datastore_file_trigger = '/store/<filename>/trigger'


from igor.common import routes
from lxml import etree
import io
import logging
import os
import re
import subprocess
import tarfile
import tempfile
import time
import urllib
import urllib2

    def url(self, route="/", query={}, **route_args):
        """Returns the URL of the remote Igord server with the given route
        """
        _route = route
        if route_args:
            _route = re.sub("<([^>:]*)(:[^>]*)?>", "{\\1}", _route)
            _route = _route.format(**route_args)
        _query = "?" + urllib.urlencode(query.items()) if query else ""
        if "<" in _route:
            self.logger.debug("Route: %s" % route)
            self.logger.debug("RouteArgs: %s" % route_args)
            self.logger.debug("Query: %s" % query)
            self.logger.debug("New Route: %s" % _route)
            self.logger.debug("New Query: %s" % _query)
            raise RuntimeError("Some placeholders weren't filled in route.")
        return "http://{host}:{port}{route}{query}".format(host=self.host,
                                                           port=self.port,
                                                           route=_route,
                                                           query=_query)

    def route_request(self, route, **route_args):
        """Request a route and return an XML tree
        """
        url = self.url(route, {"format": "xml"}, **route_args)
        pagedata = self._http.request(url)
        tree = etree.XML(pagedata) if pagedata else None
        return tree

    def jobs(self):

# Path: igor/common.py
# class routes(object):
#     static_ui_data = '/ui/<filename>'
#     static_data = '/static/data/<filename>'
# 
#     testsuite_submit = '/jobs/submit/<tname>/with/<pname>/on/<hname>'
# 
#     jobs = '/jobs'
#     job = '/jobs/<cookie>'
#     job_start = '/jobs/<cookie>/start'
#     job_abort = '/jobs/<cookie>/abort'
#     job_status = '/jobs/<cookie>/status'
#     job_report = '/jobs/<cookie>/report'
#     job_report_junit = '/jobs/<cookie>/report/junit'
#     job_testsuite = '/jobs/<cookie>/testsuite'
#     job_artifacts = '/jobs/<cookie>/artifacts'
#     job_artifacts_archive = '/jobs/<cookie>/archive'  # FIXME
#     job_artifact = '/jobs/<cookie>/artifacts/<name>'
# 
#     job_step_skip = '/jobs/<cookie>/step/<n:int>/skip'
#     job_step_finish = '/jobs/<cookie>/step/<n:int>/<result:re:success|failed>'
#     job_step_result = '/jobs/<cookie>/step/<n:int>/result'
#     job_step_annotate = '/jobs/<cookie>/step/current/annotate'
# 
#     job_set_boot_profile = '/jobs/<cookie>/set/enable_pxe/<enable_pxe>'
#     job_set_kernelargs = '/jobs/<cookie>/set/kernelargs/<kernelargs>'
# 
#     job_bootstrap = '/testjob/<cookie>'
# 
#     testsuites = '/testsuites'
#     testsuites_validate = '/testsuites/validate'
#     testsuite_summary = '/testsuites/<name>/summary'
#     testsuite_archive = '/testsuites/<name>/download'
# 
#     testplans = '/testplans'
#     testplan = '/testplans/<name>'
#     testplan_start = '/testplans/<name>/submit'
#     testplan_abort = '/testplans/<name>/abort'
#     testplan_status = '/testplans/<name>/status'
#     testplan_report = '/testplans/<name>/report'
#     testplan_report_junit = '/testplans/<name>/report/junit'
#     testplan_summary = ''
# 
#     profiles = '/profiles'
#     profile = '/profiles/<pname>'
#     profile_delete = '/profiles/<pname>/remove'
#     profile_set_kernelargs = '/profiles/<pname>/kargs'
# 
#     hosts = '/hosts'
# 
#     testcase_source = '/testcases/<suitename>/<setname>/<casename>/source'
# 
#     server_log = '/server/log'
# 
#     datastore = '/store'
#     datastore_file = '/store/<filename>'
#     datastore_file_trigger = '/store/<filename>/trigger'


# Path: igor/client/main.py
from igor.common import routes
from lxml import etree
import io
import logging
import os
import re
import subprocess
import tarfile
import tempfile
import time
import urllib
import urllib2


    def url(self, route="/", query={}, **route_args):
        """Returns the URL of the remote Igord server with the given route
        """
        _route = route
        if route_args:
            _route = re.sub("<([^>:]*)(:[^>]*)?>", "{\\1}", _route)
            _route = _route.format(**route_args)
        _query = "?" + urllib.urlencode(query.items()) if query else ""
        if "<" in _route:
            self.logger.debug("Route: %s" % route)
            self.logger.debug("RouteArgs: %s" % route_args)
            self.logger.debug("Query: %s" % query)
            self.logger.debug("New Route: %s" % _route)
            self.logger.debug("New Query: %s" % _query)
            raise RuntimeError("Some placeholders weren't filled in route.")
        return "http://{host}:{port}{route}{query}".format(host=self.host,
                                                           port=self.port,
                                                           route=_route,
                                                           query=_query)

    def route_request(self, route, **route_args):
        """Request a route and return an XML tree
        """
        url = self.url(route, {"format": "xml"}, **route_args)
        pagedata = self._http.request(url)
        tree = etree.XML(pagedata) if pagedata else None
        return tree

    def jobs(self):

        return self.route_request(routes.jobs)