ARCH_CONFIG_PATH = "/etc/rest_arch/env.yaml" APP_CONFIG_PATH = "app.yaml" APP_TYPE_BARE = "bare" HUSKAR_CACHE_DIR = "/var/run/skt_huskar" DEFAULT_APP_PORT = 8010 DEFAULT_WORKER_CLASS = 'gevent' DEFAULT_WORKER_NUMBER_DEV = 1 DEFAULT_WORKER_NUMBER_PROD = 2 ENV_DEV = 'dev' class AppConfigLoadFailException(SKTBaseException):
    """Failed to load app.yaml, not found or yaml invalid""" MOBILE_RE = re.compile('^1[34578]\d{9}$')
ALL_PHONE_RE = re.compile('^1[34578]\d{9}$|^[2-9]\d{6,7}(-\d{1,4})?$'
                          '|^6[1-9]{2,5}$')
EMAIL_RE = re.compile('\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*')
UNIT_MAP = {u'十': 10, u'百': 100, u'千': 1000, u'万': 10000, u'亿': 100000000}
NUM_MAP = u"零一二三四五六七八九"
LIB_DIR_PATH = os.path.dirname(os.path.abspath(__file__))
DEPRECATION_PATTERN = 'option %r is deprecating, please use %r instead'
def is_mobile(s):
def is_phone(s):
def is_email(s):
def datetime2utc(dt):
def utc2datetime(t):
def strptime2date(date_str):
def chinese2num(s):
def md5(s):
def sha1(s):
def get_unique_string(length=31):
    def __init__(self, fn):
    def __get__(self, inst, cls):
    def __init__(self, *envvars):
    def configured(self):
    def _normalize(self, varname):
    def __getitem__(self, key):
    def get_int(self, key):
    def __setitem__(self, key, value):
    def __init__(self, val):
    def is_empty(val):
def warn_deprecation(logger_or_name, old, new, lvl=logging.WARNING,
                     pat=DEPRECATION_PATTERN):
class CachedProperty(object):
class EnvvarReader(dict):
class EmptyValue(object):
import sys
import yaml
import logging
import rest_arch
from .consts import (
    ARCH_CONFIG_PATH,
    APP_CONFIG_PATH,
    APP_TYPE_BARE,
    HUSKAR_CACHE_DIR,
    DEFAULT_APP_PORT,
    DEFAULT_WORKER_CLASS,
    DEFAULT_WORKER_NUMBER_DEV,
    DEFAULT_WORKER_NUMBER_PROD,
    ENV_DEV
)
from .exc import AppConfigLoadFailException
from ..utils import EnvvarReader, cached_property
        from .env import is_in_dev
        from .env import env, is_in_prod
        from .env import is_in_prod
    @cached_property
    def app_uri(self):
        app = self._get_conf('app', None)
        if app is None:
            raise RuntimeError("Missing `app` in app.yaml")
        return app

    @cached_property
    def app_settings_uri(self):
        return self.config['settings']

    @cached_property
    def port(self):

        port = self.envvar.get_int('skt_app_port')
        if port:
            return port
        if is_in_dev():
            return self.DEFAULT_APP_PORT
        return self.PORT_MAP.get(self.app_name, self.DEFAULT_APP_PORT)

    @cached_property
    def canonical_port(self):

        if not is_in_prod():
            port = self.port
        else:
            port = self.envvar.get_int('skt_app_haproxy_port')
            if not port:
                port = haproxy_port_map.get(self.app_name)
        if not port:
            raise RuntimeError(
                'no canonical port can be determined for app: %r in env: %r'
                % (self.app_name, env()))
        return port

    def get_app_n_workers(self):

        worker_num = self.envvar.get_int('skt_app_worker_num')
        if worker_num:
            return worker_num
        if not is_in_prod():
            return DEFAULT_WORKER_NUMBER_DEV
        return worker_num_map.get(self.app_name, DEFAULT_WORKER_NUMBER_PROD)

    @cached_property
    def worker_class(self):
        return self._get_conf('worker_class', self.DEFAULT_WORKER_CLASS)

    def get_app_binds(self):
        return '0.0.0.0:%d' % self.port

    @cached_property
    def logger_name(self):
        return self.app_name.split('.')[-1]

    @cached_property
    def arch_deps(self):
        with open(self.requirements_file) as f:
            arch_version = None
            for line in f:
                if "rest_arch" in line:
                    arch_version = line.strip()
        return arch_version or "rest_arch==%s" % rest_arch.__version__

    @cached_property
    def message_consumers(self):
        consumers = self.config.get('message_consumer')
        if not consumers:
            raise AppConfigLoadFailException(
                'message.consumer should contains something in app.yaml.')

        return consumers

    @cached_property
    def message_consumers_settings(self):
        return self.config.get('message_consumer_settings', {})

    @cached_property
    def huskar_app_name(self):
        return self.app_name

    @cached_property
    def async_workers(self):
        return self.config.get('async_workers')

    @cached_property
    def requirements_file(self):
        return self.config.get('requirements', 'requirements.txt')

    @cached_property
    def timeout(self):
        return self._get_conf('timeout')

    def get_service_register(self):
        return 'huskar_sdk:ServiceWatcher'

    def get_service_register_conf(self):
        load_arch_config()
        conf = {}
        conf.update(arch_config.zookeeper_config)
        conf.update({
            'team': 'arch',
            'cluster': arch_config.cluster,
            'service_name': self.app_name,
            'cache_dir': HUSKAR_CACHE_DIR,
        })
        return conf

    def _get_conf(self, key, default=None):
        """Help to try config frist on key 'services' and then root.
        """
        if 'services' in self.config and \
                self.TYPE_NAME in self.config['services']:
            return self.config['services'][self.TYPE_NAME].get(key, default)
        return self.config.get(key, default)


class WsgiAppConfig(BareAppConfig):
    TYPE_NAME = "wsgi"

    DEFAULT_APP_PORT = DEFAULT_APP_PORT