def static(path, absolute=False, mangle_name=True): """ Simple static file maintainer which automatically paths and versions files being served out of static. In the case of JS and CSS where g.uncompressedJS is set, the version of the file is set to be random to prevent caching and it mangles the path to point to the uncompressed versions. """ dirname, filename = os.path.split(path) extension = os.path.splitext(filename)[1] is_text = extension in static_text_extensions should_cache_bust = False path_components = [] actual_filename = None if mangle_name else filename # If building an absolute url, default to https because we like it and the # static server should support it. scheme = 'https' if absolute else None if g.static_domain: domain = g.static_domain else: path_components.append(c.site.static_path) if g.uncompressedJS: # unminified static files are in type-specific subdirectories if not dirname and is_text: path_components.append(static_text_extensions[extension]) should_cache_bust = True actual_filename = filename domain = g.domain if absolute else None path_components.append(dirname) if not actual_filename: actual_filename = g.static_names.get(filename, filename) path_components.append(actual_filename) actual_path = os.path.join(*path_components) query = None if path and should_cache_bust: file_id = static_mtime(actual_path) or random.randint(0, 1000000) query = 'v=' + str(file_id) return urlparse.urlunsplit(( scheme, domain, actual_path, query, None ))
def static(path, allow_gzip=True): """ Simple static file maintainer which automatically paths and versions files being served out of static. In the case of JS and CSS where g.uncompressedJS is set, the version of the file is set to be random to prevent caching and it mangles the path to point to the uncompressed versions. """ dirname, filename = os.path.split(path) extension = os.path.splitext(filename)[1] is_text = extension in static_text_extensions can_gzip = is_text and 'gzip' in request.accept_encoding should_gzip = allow_gzip and can_gzip should_cache_bust = False path_components = [] actual_filename = None if not c.secure and g.static_domain: scheme = "http" domain = g.static_domain suffix = ".gzip" if should_gzip and g.static_pre_gzipped else "" elif c.secure and g.static_secure_domain: scheme = "https" domain = g.static_secure_domain suffix = ".gzip" if should_gzip and g.static_secure_pre_gzipped else "" else: path_components.append(c.site.static_path) if g.uncompressedJS: # unminified static files are in type-specific subdirectories if not dirname and is_text: path_components.append(static_text_extensions[extension]) should_cache_bust = True actual_filename = filename scheme = None domain = None suffix = "" path_components.append(dirname) if not actual_filename: actual_filename = g.static_names.get(filename, filename) path_components.append(actual_filename + suffix) actual_path = os.path.join(*path_components) query = None if path and should_cache_bust: file_id = static_mtime(actual_path) or random.randint(0, 1000000) query = 'v=' + str(file_id) return urlparse.urlunsplit((scheme, domain, actual_path, query, None))
def static(path, allow_gzip=True): """ Simple static file maintainer which automatically paths and versions files being served out of static. In the case of JS and CSS where g.uncompressedJS is set, the version of the file is set to be random to prevent caching and it mangles the path to point to the uncompressed versions. """ dirname, filename = os.path.split(path) extension = os.path.splitext(filename)[1] is_text = extension in static_text_extensions can_gzip = is_text and "gzip" in request.accept_encoding should_gzip = allow_gzip and can_gzip should_cache_bust = False path_components = [] actual_filename = None if not c.secure and g.static_domain: scheme = "http" domain = g.static_domain suffix = ".gzip" if should_gzip and g.static_pre_gzipped else "" elif c.secure and g.static_secure_domain: scheme = "https" domain = g.static_secure_domain suffix = ".gzip" if should_gzip and g.static_secure_pre_gzipped else "" else: path_components.append(c.site.static_path) if g.uncompressedJS: # unminified static files are in type-specific subdirectories if not dirname and is_text: path_components.append(static_text_extensions[extension]) should_cache_bust = True actual_filename = filename scheme = None domain = None suffix = "" path_components.append(dirname) if not actual_filename: actual_filename = g.static_names.get(filename, filename) path_components.append(actual_filename + suffix) actual_path = os.path.join(*path_components) query = None if path and should_cache_bust: file_id = static_mtime(actual_path) or random.randint(0, 1000000) query = "v=" + str(file_id) return urlparse.urlunsplit((scheme, domain, actual_path, query, None))