# Path: mozsvc/util.py
# def round_time(value=None, precision=2):
#     """Transforms a timestamp into a two digits Decimal.
# 
#     Arg:
#         value: timestamps representation - float or str.
#         If None, uses time.time()
# 
#         precision: number of digits to keep. defaults to 2.
# 
#     Return:
#         A Decimal two-digits instance.
#     """
#     if value is None:
#         value = time.time()
#     if not isinstance(value, str):
#         value = str(value)
#     try:
#         digits = '0' * precision
#         return Decimal(value).quantize(Decimal('1.' + digits))
#     except InvalidOperation:
#         raise ValueError(value)
# 
# def resolve_name(name, package=None):
#     """Resolve dotted name into a python object.
# 
#     This function resolves a dotted name as a reference to a python object,
#     returning whatever object happens to live at that path.  It's a simple
#     convenience wrapper around pyramid's DottedNameResolver.
# 
#     The optional argument 'package' specifies the package name for relative
#     imports.  If not specified, only absolute paths will be supported.
#     """
#     return DottedNameResolver(package).resolve(name)
# 
# def maybe_resolve_name(name_or_object, package=None):
#     """Resolve dotted name or object into a python object.
# 
#     This function resolves a dotted name as a reference to a python object,
#     returning whatever object happens to live at that path.  If the given
#     name is not a string, it is returned unchanged.
# 
#     The optional argument 'package' specifies the package name for relative
#     imports.  If not specified, only absolute paths will be supported.
#     """
#     return DottedNameResolver(package).maybe_resolve(name_or_object)
# 
# def dnslookup(url):
#     """Replaces a hostname by its IP in an url.
# 
#     Uses gethostbyname to do a DNS lookup, so the nscd cache is used.
# 
#     If gevent has patched the standard library, makes sure it uses the
#     original version because gevent uses its own mechanism based on
#     the async libevent's evdns_resolve_ipv4, which does not use
#     glibc's resolver.
#     """
#     try:
#         from gevent.socket import _socket
#         gethostbyname = _socket.gethostbyname
#     except ImportError:
#         import socket
#         gethostbyname = socket.gethostbyname
# 
#     # parsing
#     parsed_url = urllib.parse.urlparse(url)
#     host, port = urllib.parse.splitport(parsed_url.netloc)
#     user, host = urllib.parse.splituser(host)
# 
#     # resolving the host
#     host = gethostbyname(host)
# 
#     # recomposing
#     if port is not None:
#         host = '%s:%s' % (host, port)
# 
#     if user is not None:
#         host = '%s@%s' % (user, host)
# 
#     parts = [parsed_url[0]] + [host] + list(parsed_url[2:])
#     return urllib.parse.urlunparse(parts)


import unittest
import os.path
from mozsvc.util import (round_time, resolve_name, maybe_resolve_name,
                         dnslookup)
        # can take a str timestamp
        res = round_time('129084.198271987')
        self.assertEqual(str(res), '129084.20')

        # bad values raise ValueErrors
        self.assertRaises(ValueError, round_time, 'bleh')
        self.assertRaises(ValueError, round_time, object())

        # changing the precision
        res = round_time(129084.198271987, precision=3)
        self.assertEqual(str(res), '129084.198')

    def test_resolve_name(self):

        # Resolving by absolute path
        self.assertEqual(os.path.abspath, resolve_name("os.path.abspath"))
        self.assertEqual(os.path.abspath, resolve_name("os.path:abspath"))

        # Resolving by relative path to package object
        self.assertEqual(os.path.abspath, resolve_name(".path.abspath", os))
        self.assertEqual(os.path.abspath, resolve_name(".path:abspath", os))

        # Resolving by relative path to package name
        self.assertEqual(os.path.abspath, resolve_name(".abspath", "os.path"))
        self.assertEqual(os.path.abspath, resolve_name(":abspath", "os.path"))

    def test_maybe_resolve_name(self):

        self.assertEqual(os.path, maybe_resolve_name("os.path"))
        self.assertEqual(os.path, maybe_resolve_name(os.path))

# Path: mozsvc/util.py
# def round_time(value=None, precision=2):
#     """Transforms a timestamp into a two digits Decimal.
# 
#     Arg:
#         value: timestamps representation - float or str.
#         If None, uses time.time()
# 
#         precision: number of digits to keep. defaults to 2.
# 
#     Return:
#         A Decimal two-digits instance.
#     """
#     if value is None:
#         value = time.time()
#     if not isinstance(value, str):
#         value = str(value)
#     try:
#         digits = '0' * precision
#         return Decimal(value).quantize(Decimal('1.' + digits))
#     except InvalidOperation:
#         raise ValueError(value)
# 
# def resolve_name(name, package=None):
#     """Resolve dotted name into a python object.
# 
#     This function resolves a dotted name as a reference to a python object,
#     returning whatever object happens to live at that path.  It's a simple
#     convenience wrapper around pyramid's DottedNameResolver.
# 
#     The optional argument 'package' specifies the package name for relative
#     imports.  If not specified, only absolute paths will be supported.
#     """
#     return DottedNameResolver(package).resolve(name)
# 
# def maybe_resolve_name(name_or_object, package=None):
#     """Resolve dotted name or object into a python object.
# 
#     This function resolves a dotted name as a reference to a python object,
#     returning whatever object happens to live at that path.  If the given
#     name is not a string, it is returned unchanged.
# 
#     The optional argument 'package' specifies the package name for relative
#     imports.  If not specified, only absolute paths will be supported.
#     """
#     return DottedNameResolver(package).maybe_resolve(name_or_object)
# 
# def dnslookup(url):
#     """Replaces a hostname by its IP in an url.
# 
#     Uses gethostbyname to do a DNS lookup, so the nscd cache is used.
# 
#     If gevent has patched the standard library, makes sure it uses the
#     original version because gevent uses its own mechanism based on
#     the async libevent's evdns_resolve_ipv4, which does not use
#     glibc's resolver.
#     """
#     try:
#         from gevent.socket import _socket
#         gethostbyname = _socket.gethostbyname
#     except ImportError:
#         import socket
#         gethostbyname = socket.gethostbyname
# 
#     # parsing
#     parsed_url = urllib.parse.urlparse(url)
#     host, port = urllib.parse.splitport(parsed_url.netloc)
#     user, host = urllib.parse.splituser(host)
# 
#     # resolving the host
#     host = gethostbyname(host)
# 
#     # recomposing
#     if port is not None:
#         host = '%s:%s' % (host, port)
# 
#     if user is not None:
#         host = '%s@%s' % (user, host)
# 
#     parts = [parsed_url[0]] + [host] + list(parsed_url[2:])
#     return urllib.parse.urlunparse(parts)


# Path: mozsvc/tests/test_util.py
import unittest
import os.path
from mozsvc.util import (round_time, resolve_name, maybe_resolve_name,
                         dnslookup)

        # can take a str timestamp
        res = round_time('129084.198271987')
        self.assertEqual(str(res), '129084.20')

        # bad values raise ValueErrors
        self.assertRaises(ValueError, round_time, 'bleh')
        self.assertRaises(ValueError, round_time, object())

        # changing the precision
        res = round_time(129084.198271987, precision=3)
        self.assertEqual(str(res), '129084.198')

    def test_resolve_name(self):

        # Resolving by absolute path
        self.assertEqual(os.path.abspath, resolve_name("os.path.abspath"))
        self.assertEqual(os.path.abspath, resolve_name("os.path:abspath"))

        # Resolving by relative path to package object
        self.assertEqual(os.path.abspath, resolve_name(".path.abspath", os))
        self.assertEqual(os.path.abspath, resolve_name(".path:abspath", os))

        # Resolving by relative path to package name
        self.assertEqual(os.path.abspath, resolve_name(".abspath", "os.path"))
        self.assertEqual(os.path.abspath, resolve_name(":abspath", "os.path"))

    def test_maybe_resolve_name(self):

        self.assertEqual(os.path, maybe_resolve_name("os.path"))
        self.assertEqual(os.path, maybe_resolve_name(os.path))

        self.assertEqual(None, maybe_resolve_name(None))