我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.conf.settings.FILE_CHARSET。
def preprocess(self): """ Preprocess (if necessary) a translatable file before passing it to xgettext GNU gettext utility. """ from django.utils.translation import templatize if not self.is_templatized: return with io.open(self.path, 'r', encoding=settings.FILE_CHARSET) as fp: src_data = fp.read() if self.domain == 'djangojs': content = prepare_js_for_gettext(src_data) elif self.domain == 'django': content = templatize(src_data, self.path[2:]) with io.open(self.work_path, 'w', encoding='utf-8') as fp: fp.write(content)
def get_template(self, template_name): tried = [] for template_file in self.iter_template_filenames(template_name): try: with io.open(template_file, encoding=settings.FILE_CHARSET) as fp: template_code = fp.read() except IOError as e: if e.errno == errno.ENOENT: tried.append(( Origin(template_file, template_name, self), 'Source does not exist', )) continue raise return Template(template_code) else: raise TemplateDoesNotExist(template_name, tried=tried, backend=self)
def preprocess(self): """ Preprocess (if necessary) a translatable file before passing it to xgettext GNU gettext utility. """ if not self.is_templatized: return encoding = settings.FILE_CHARSET if self.command.settings_available else 'utf-8' with io.open(self.path, 'r', encoding=encoding) as fp: src_data = fp.read() if self.domain == 'djangojs': content = prepare_js_for_gettext(src_data) elif self.domain == 'django': content = templatize(src_data, origin=self.path[2:], charset=encoding) with io.open(self.work_path, 'w', encoding='utf-8') as fp: fp.write(content)
def configure_from_settings(self, settings): # Default configuration self.charset = settings.FILE_CHARSET self.autorefresh = settings.DEBUG self.use_finders = settings.DEBUG self.static_prefix = urlparse(settings.STATIC_URL or '').path if settings.DEBUG: self.max_age = 0 # Allow settings to override default attributes for attr in self.config_attrs: settings_key = 'WHITENOISE_{0}'.format(attr.upper()) try: value = getattr(settings, settings_key) except AttributeError: pass else: value = decode_if_byte_string(value) setattr(self, attr, value) self.static_prefix = ensure_leading_trailing_slash(self.static_prefix) self.static_root = decode_if_byte_string(settings.STATIC_ROOT)
def _load_all_templates(directory): """ Loads all templates in a directory (recursively) and yields tuples of template tokens and template paths. """ if os.path.exists(directory): for name in os.listdir(directory): path = os.path.join(directory, name) if os.path.isdir(path): for template in _load_all_templates(path): yield template elif path.endswith('.html'): with open(path, 'rb') as fobj: source = fobj.read().decode(settings.FILE_CHARSET) if DJANGO_1_8: lexer = Lexer(source, path) else: lexer = Lexer(source) yield lexer.tokenize(), path
def custom_sql_for_model(model, style, connection): opts = model._meta app_dir = os.path.normpath(os.path.join(os.path.dirname(upath(models.get_app(model._meta.app_label).__file__)), 'sql')) output = [] # Post-creation SQL should come before any initial SQL data is loaded. # However, this should not be done for models that are unmanaged or # for fields that are part of a parent model (via model inheritance). if opts.managed: post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')] for f in post_sql_fields: output.extend(f.post_create_sql(style, model._meta.db_table)) # Find custom SQL, if it's available. backend_name = connection.settings_dict['ENGINE'].split('.')[-1] sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.model_name, backend_name)), os.path.join(app_dir, "%s.sql" % opts.model_name)] for sql_file in sql_files: if os.path.exists(sql_file): with codecs.open(sql_file, 'U', encoding=settings.FILE_CHARSET) as fp: # Some backends can't execute more than one SQL statement at a time, # so split into separate statements. output.extend(_split_statements(fp.read())) return output
def load_template_source(self, template_name, template_dirs=None): for path in self.get_template_sources(template_name): try: with io.open(path, encoding=settings.FILE_CHARSET) as file: return (file.read(), path) except IOError: pass raise TemplateDoesNotExist(template_name)
def load_template_source(self, template_name, template_dirs=None): for path in self.get_template_sources(template_name): try: with io.open(path, encoding=settings.FILE_CHARSET) as file: return file.read(), path except IOError: pass raise TemplateDoesNotExist(template_name)
def __init__(self, params): params = params.copy() options = params.pop('OPTIONS').copy() options.setdefault('debug', settings.DEBUG) options.setdefault('file_charset', settings.FILE_CHARSET) libraries = options.get('libraries', {}) options['libraries'] = self.get_templatetag_libraries(libraries) super(DjangoTemplates, self).__init__(params) self.engine = Engine(self.dirs, self.app_dirs, **options)
def decorate_templatize(func): def templatize(src, origin=None): src = to_text(src, settings.FILE_CHARSET) if origin.endswith(".pug"): html = process(src,compiler=Compiler) else: html = src return func(html, origin) return templatize
def custom_sql_for_model(model, style, connection): opts = model._meta app_dirs = [] app_dir = apps.get_app_config(model._meta.app_label).path app_dirs.append(os.path.normpath(os.path.join(app_dir, 'sql'))) # Deprecated location -- remove in Django 1.9 old_app_dir = os.path.normpath(os.path.join(app_dir, 'models/sql')) if os.path.exists(old_app_dir): warnings.warn("Custom SQL location '<app_label>/models/sql' is " "deprecated, use '<app_label>/sql' instead.", RemovedInDjango19Warning) app_dirs.append(old_app_dir) output = [] # Post-creation SQL should come before any initial SQL data is loaded. # However, this should not be done for models that are unmanaged or # for fields that are part of a parent model (via model inheritance). if opts.managed: post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')] for f in post_sql_fields: output.extend(f.post_create_sql(style, model._meta.db_table)) # Find custom SQL, if it's available. backend_name = connection.settings_dict['ENGINE'].split('.')[-1] sql_files = [] for app_dir in app_dirs: sql_files.append(os.path.join(app_dir, "%s.%s.sql" % (opts.model_name, backend_name))) sql_files.append(os.path.join(app_dir, "%s.sql" % opts.model_name)) for sql_file in sql_files: if os.path.exists(sql_file): with io.open(sql_file, encoding=settings.FILE_CHARSET) as fp: output.extend(connection.ops.prepare_sql_script(fp.read(), _allow_fallback=True)) return output
def app_prefixed_loader(template_name, template_dirs=None): packed = template_name.split('/', 1) if len(packed) == 2 and packed[0] in app_template_dirs: path = os.path.join(app_template_dirs[packed[0]], packed[1]) try: return (open(path).read().decode(settings.FILE_CHARSET), path) except IOError: pass raise TemplateDoesNotExist, template_name
def __init__(self, params): params = params.copy() options = params.pop('OPTIONS').copy() options.setdefault('autoescape', True) options.setdefault('debug', settings.DEBUG) options.setdefault('file_charset', settings.FILE_CHARSET) libraries = options.get('libraries', {}) options['libraries'] = self.get_templatetag_libraries(libraries) super(DjangoTemplates, self).__init__(params) self.engine = Engine(self.dirs, self.app_dirs, **options)
def __init__(self, params): """ Hard override of init to use our engine. """ params = params.copy() options = params.pop('OPTIONS').copy() options.setdefault('autoescape', True) options.setdefault('debug', settings.DEBUG) options.setdefault('file_charset', settings.FILE_CHARSET) libraries = options.get('libraries', {}) options['libraries'] = self.get_templatetag_libraries(libraries) super(django.DjangoTemplates, self).__init__(params) self.engine = Engine(self.dirs, self.app_dirs, **options)
def load_template_source(self, template_name, template_dirs=None): if not self._valid_template(template_name): raise TemplateDoesNotExist(template_name) try: template = get_env().get_template(template_name) except jinja2.TemplateNotFound: raise TemplateDoesNotExist(template_name) with open(template.filename, 'rb') as fp: return (fp.read().decode(settings.FILE_CHARSET), template.filename)
def get_template(self, template_name): for template_file in self.iter_template_filenames(template_name): try: with io.open(template_file, encoding=settings.FILE_CHARSET) as fp: template_code = fp.read() except IOError: continue return Template(template_code) else: raise TemplateDoesNotExist(template_name)
def __init__(self, params): params = params.copy() options = params.pop('OPTIONS').copy() options.setdefault('debug', settings.DEBUG) options.setdefault('file_charset', settings.FILE_CHARSET) super(DjangoTemplates, self).__init__(params) self.engine = Engine(self.dirs, self.app_dirs, **options)