# Path: webtools/utils/encoding.py
# def smart_text(value, encoding="utf-8", errors="strict"):
#     if isinstance(value, str):
#         if encoding == "utf-8":
#             return value
# 
#         return value.encode('utf-8', errors).decode(encoding, errors)
# 
#     if not isinstance(value, bytes):
#         return str(value)
# 
#     return value.decode(encoding, errors)
# 
# Path: webtools/services/base.py
# class Service(objects):
#     """
#     Base class for all services.
#     """
#     def __get__(self, instance, owner):
#         return ServiceDescriptor(self, instance, owner)


from webtools.utils.encoding import smart_text
from .base import Service


def locale_to_code(locale):
    """
    Get main code from locale object.
    """
    code = locale.code
    return code.split("_")[0]


class LocaleService(object):
    def get_format(self, format_name, lang=None, handler=None):
        """
        Get format by name from global settings.
        """
        if lang is None and handler is None:
            raise RuntimeError("One of parameters 'lang' or 'handler' is mandatory")


# Path: webtools/utils/encoding.py
# def smart_text(value, encoding="utf-8", errors="strict"):
#     if isinstance(value, str):
#         if encoding == "utf-8":
#             return value
# 
#         return value.encode('utf-8', errors).decode(encoding, errors)
# 
#     if not isinstance(value, bytes):
#         return str(value)
# 
#     return value.decode(encoding, errors)
# 
# Path: webtools/services/base.py
# class Service(objects):
#     """
#     Base class for all services.
#     """
#     def __get__(self, instance, owner):
#         return ServiceDescriptor(self, instance, owner)


# Path: webtools/services/locale.py
from webtools.utils.encoding import smart_text
from .base import Service



def locale_to_code(locale):
    """
    Get main code from locale object.
    """
    code = locale.code
    return code.split("_")[0]


class LocaleService(object):
    def get_format(self, format_name, lang=None, handler=None):
        """
        Get format by name from global settings.
        """
        if lang is None and handler is None:
            raise RuntimeError("One of parameters 'lang' or 'handler' is mandatory")


        format_name = smart_text(format_name)