def load_template_source(self, template_name, template_dirs=None): tried = [] for filepath in self.get_template_sources(template_name, template_dirs): # First try to read from S3 if settings.USE_S3_THEME: # first try to read from cache cache_key = ".".join( [settings.SITE_CACHE_KEY, "theme", filepath]) cached_template = cache.get(cache_key) if cached_template == "tried": # Skip out of this on to the next template file continue if cached_template: return (cached_template, filepath) try: file = read_theme_file_from_s3(filepath) try: cache.set(cache_key, file) cache_group_key = "%s.theme_files_cache_list" % settings.SITE_CACHE_KEY cache_group_list = cache.get(cache_group_key) if cache_group_list is None: cache.set(cache_group_key, [cache_key]) else: cache_group_list += [cache_key] cache.set(cache_group_key, cache_group_list) return (file, filepath) finally: pass except: # Cache that we tried this file cache.set(cache_key, "tried") # Otherwise, look on to local file system. else: #print filepath try: file = open(filepath) try: return (file.read().decode(settings.FILE_CHARSET), filepath) finally: file.close() except IOError: tried.append(filepath) if tried: error_msg = "Tried %s" % tried else: error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory." raise TemplateDoesNotExist(_(error_msg))
def load_template_source(self, template_name, template_dirs=None): tried = [] for filepath in self.get_template_sources(template_name, template_dirs): # First try to read from S3 if settings.USE_S3_THEME: # first try to read from cache cache_key = ".".join([settings.SITE_CACHE_KEY, "theme", filepath]) cached_template = cache.get(cache_key) if cached_template == "tried": # Skip out of this on to the next template file continue if cached_template: return (cached_template, filepath) try: file = read_theme_file_from_s3(filepath) try: cache.set(cache_key, file) cache_group_key = "%s.theme_files_cache_list" % settings.SITE_CACHE_KEY cache_group_list = cache.get(cache_group_key) if cache_group_list is None: cache.set(cache_group_key, [cache_key]) else: cache_group_list += [cache_key] cache.set(cache_group_key, cache_group_list) return (file, filepath) finally: pass except: # Cache that we tried this file cache.set(cache_key, "tried") # Otherwise, look on to local file system. else: #print filepath try: file = open(filepath) try: return (file.read().decode(settings.FILE_CHARSET), filepath) finally: file.close() except IOError: tried.append(filepath) if tried: error_msg = "Tried %s" % tried else: error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory." raise TemplateDoesNotExist(error_msg)
def get_theme_info(theme=None): """Returns a dict of the fields from the theme.info file for the theme. A dict is preferred so we can loop through the fields. """ if theme is None: theme = get_theme() cache_key = '.'.join([settings.SITE_CACHE_KEY, 'theme_info', str(theme)]) theme_info = cache.get(cache_key) if theme_info is not None: return theme_info if is_builtin_theme(theme) or not settings.USE_S3_THEME: info_file = os.path.join(get_theme_root(theme), 'theme.info') if not os.path.isfile(info_file): return {} with open(info_file) as fp: info_str = fp.read() else: from tendenci.libs.boto_s3.utils import read_theme_file_from_s3 info_str = read_theme_file_from_s3(os.path.join(theme, 'theme.info')) theme_info = configparser.ConfigParser() try: if hasattr(theme_info, 'read_string'): # Python 3.2 theme_info.read_string(info_str, source='theme.info') else: theme_info.readfp(StringIO.StringIO(info_str), 'theme.info') except configparser.MissingSectionHeaderError: info_str = '[General]\n' + info_str if hasattr(theme_info, 'read_string'): theme_info.read_string(info_str, source='theme.info') else: theme_info.readfp(StringIO.StringIO(info_str), 'theme.info') # Return a dict of the fields rather than a ConfigParser object. # Note that ConfigParser._sections is undocumented and may not work in # future versions of Python. theme_info = theme_info._sections # Only cache if DEBUG is disabled if not settings.DEBUG: cache.set(cache_key, theme_info) return theme_info
def get_file_content(root_dir, theme, filename): """ Get the content from the file that selected from the navigation """ content = '' if settings.USE_S3_THEME: try: content = read_theme_file_from_s3(os.path.join(theme, filename)) except: pass if not content: current_file = os.path.join(root_dir, filename) if os.path.isfile(current_file): fd = open(current_file, 'r') content = fd.read() fd.close() return content
def get_contents(self, origin): if not origin.use_s3_theme: try: with open(origin.name, encoding='utf-8') as fp: return fp.read() # Python 3 only #except FileNotFoundError: # raise TemplateDoesNotExist(origin) # Python 2 and 3 except IOError as e: if e.errno == errno.ENOENT: raise TemplateDoesNotExist(origin) raise else: cache_key = ".".join( [settings.SITE_CACHE_KEY, "theme", origin.name]) cached_template = cache.get(cache_key) if cached_template == "tried": raise TemplateDoesNotExist(origin) if cached_template: return cached_template try: template = read_theme_file_from_s3(origin.name) except: # Cache that we tried this file cache.set(cache_key, "tried") raise TemplateDoesNotExist(origin) cache.set(cache_key, template) cache_group_key = "%s.theme_files_cache_list" % settings.SITE_CACHE_KEY cache_group_list = cache.get(cache_group_key) if cache_group_list is None: cache.set(cache_group_key, [cache_key]) else: cache_group_list += [cache_key] cache.set(cache_group_key, cache_group_list) return template
def get_file_content(file, ROOT_DIR=THEME_ROOT): """ Get the content from the file that selected from the navigation """ content = '' if settings.USE_S3_THEME: try: theme = get_theme() content = read_theme_file_from_s3(os.path.join(theme, file)) except: pass if not content: current_file = os.path.join(ROOT_DIR, file) if os.path.isfile(current_file): fd = open(current_file, 'r') content = fd.read() fd.close() return content