def __init__(self, settings_module): # update this dict from global settings (but only for ALL_CAPS settings) for setting in dir(global_settings): if setting.isupper(): setattr(self, setting, getattr(global_settings, setting)) # store the settings module in case someone later cares self.SETTINGS_MODULE = settings_module mod = importlib.import_module(self.SETTINGS_MODULE) tuple_settings = ( "INSTALLED_APPS", "TEMPLATE_DIRS", "LOCALE_PATHS", ) self._explicit_settings = set() for setting in dir(mod): if setting.isupper(): setting_value = getattr(mod, setting) if (setting in tuple_settings and not isinstance(setting_value, (list, tuple))): raise ImproperlyConfigured( "The %s setting must be a list or a tuple. " % setting) setattr(self, setting, setting_value) self._explicit_settings.add(setting) if not self.SECRET_KEY: raise ImproperlyConfigured( "The SECRET_KEY setting must not be empty.") if self.is_overridden('DEFAULT_CONTENT_TYPE'): warnings.warn('The DEFAULT_CONTENT_TYPE setting is deprecated.') if hasattr(time, 'tzset') and self.TIME_ZONE: # When we can, attempt to validate the timezone. If we can't find # this file, no check happens and it's harmless. zoneinfo_root = '/usr/share/zoneinfo' if (os.path.exists(zoneinfo_root) and not os.path.exists( os.path.join(zoneinfo_root, *(self.TIME_ZONE.split('/'))))): raise ValueError("Incorrect timezone setting: %s" % self.TIME_ZONE) # Move the time zone info into os.environ. See ticket #2315 for why # we don't do this unconditionally (breaks Windows). os.environ['TZ'] = self.TIME_ZONE time.tzset()
def detectURL(self, headURL = '', itertime=0): """ get url :headURL :the former URL which need to be merged :return: (url,url_module) """ from tornado.web import URLSpec urlList = [] # print urlList for it in self.patterns: if isinstance(it,URLSpec): urlList.append(it) continue else: url,url_module = it # iterable get the URL if isinstance(url_module,urlPackage): # BFS get URL then extend it urlList.extend(url_module.detectURL(headURL='%s%s' %(headURL,url),itertime=itertime+1)) elif isinstance(url_module,str): #print url_module # for individual one then add it to the list if headURL: assemblyURL = '%s%s' %(headURL,url) else: assemblyURL = '%s' %url # convert it to URLSpec urlList.append(URLSpec(assemblyURL,url_module,name=url_module)) # set up mapper for self.URLMapper[url_module] = assemblyURL else: raise ImproperlyConfigured('URL only accept string or URLpackage object(you can use include)') return urlList
def __init__(self,url_module): # check whether this file is properly configured if isinstance(url_module,str): # url_module is string , just import it urlconf_module = import_module(url_module) patterns = getattr(urlconf_module, 'urlpatterns', []) # get this if isinstance(patterns,list): self.patterns = patterns else: raise ImproperlyConfigured( 'url_module should contain a list-typed urlpattern variable' )
def _setup(self, name=None): """ Load the settings module pointed to by the environment variable. This is used the first time we need any settings at all, if the user has not previously configured the settings manually. """ settings_module = os.environ.get(ENVIRONMENT_VARIABLE) if not settings_module: desc = ("setting %s" % name) if name else "settings" raise ImproperlyConfigured( "Requested %s, but settings are not configured. " "You must either define the environment variable %s " "or call settings.configure() before accessing settings." % (desc, ENVIRONMENT_VARIABLE)) self._wrapped = Settings(settings_module)
def get_routes(self, handler): """ Augment `self.routes` with any dynamically generated routes. Returns a list of the Route class. """ # converting to list as iterables are good for one pass, known host needs to be checked again and again for # different functions. known_actions = list( itertools.chain(*[ route.mapping.values() for route in self.routes if not route.is_dynamic ])) extra_actions = handler.get_extra_actions() # checking action names against the known actions list not_allowed = [ action.__name__ for action in extra_actions if action.__name__ in known_actions ] if not_allowed: msg = ('Cannot use the @action decorator on the following ' 'methods, as they are existing routes: %s') raise ImproperlyConfigured(msg % ', '.join(not_allowed)) # partition detail and list actions detail_actions = [action for action in extra_actions if action.detail] list_actions = [ action for action in extra_actions if not action.detail ] routes = [] dynamic_routes = [] for route in self.routes: if route.is_dynamic and route.detail: dynamic_routes += [ self._get_dynamic_route(route, action) for action in detail_actions ] elif route.is_dynamic and not route.detail: dynamic_routes += [ self._get_dynamic_route(route, action) for action in list_actions ] else: routes.append(route) return dynamic_routes + routes
def uiDict(self): if not self.searchApp: # return a null dict return {} else : returnedDict = {} for app in self.searchApp: appUrlPath = '%s.urls' %(app) appUrlConf = import_module(appUrlPath) appUIDict = getattr(appUrlConf,'UImodule',{}) # traverse dict and try to import it if isinstance(appUIDict,dict): # merge dict returnedDict.update(appUIDict) else: raise ImproperlyConfigured('UImodule in %s should be configured in dict way' %(app) ) return returnedDict
def periodTask(self): ''' generate a task recursively :return: ''' if not self.searchApp: # return a null dict return None else : returnedList = [] for app in self.searchApp: appUrlPath = '%s.urls' %(app) appUrlConf = import_module(appUrlPath) appUIDict = getattr(appUrlConf,'cron',[]) # traverse dict and try to import it if isinstance(appUIDict,list): # merge dict returnedList.extend(appUIDict) else: raise ImproperlyConfigured('UImodule in %s should be configured in dict way' %(app) ) return returnedList
def test_raises_exception(self): with self.assertRaises(ImproperlyConfigured): raise ImproperlyConfigured()
def __setattr__(self, name, value): if name in ("MEDIA_URL", "STATIC_URL") and value and not value.endswith('/'): raise ImproperlyConfigured("If set, %s must end with a slash" % name) object.__setattr__(self, name, value)