def from_data(cls, kind, pod_path, hashed, meta): """Create the route info from data.""" # Need to reconstruct the path filter object. if 'path_filter' in meta: meta['path_filter'] = grow_path_filter.PathFilter( ignored=meta['path_filter'].get('ignored'), included=meta['path_filter'].get('included')) if 'static_filter' in meta: meta['static_filter'] = grow_path_filter.PathFilter( ignored=meta['static_filter'].get('ignored'), included=meta['static_filter'].get('included')) return cls(kind, pod_path=pod_path, hashed=hashed, meta=meta)
def path_filter(self): """Path filter for the static document.""" static_filter = self.filter if static_filter: return grow_path_filter.PathFilter( static_filter.get('ignore_paths'), static_filter.get('include_paths')) return self.pod.path_filter
def path_filter(self): """Filter for testing path formats.""" if self.env.name == environment.Name.DEV or self.env.name is None: filter_config = self.yaml.get('filter', {}) else: if 'deployments' not in self.yaml: raise ValueError('No pod-specific deployments configured.') filter_config = (self.yaml['deployments'].get(self.env.name, {}).get( 'filter', {})) return grow_path_filter.PathFilter(filter_config.get('ignore_paths'), filter_config.get('include_paths'))
def test_filter_constructor(self): """Simple ignore filters work.""" self.filter = path_filter.PathFilter(['foo.bar'], ['/bar/foo.bar']) # Normal files work. self.assertTrue(self.filter.is_valid('/sitemap.xml')) self.assertTrue(self.filter.is_valid('/index.html')) self.assertTrue(self.filter.is_valid('/static/images/logo.png')) self.assertTrue(self.filter.is_valid('/.grow/index.html')) # Defaults are not kept. self.assertTrue(self.filter.is_valid('/.DS_STORE')) self.assertTrue(self.filter.is_valid('/.htaccess')) # Custom filters work. self.assertFalse(self.filter.is_valid('/foo.bar')) # Included filter overrides the ignores. self.assertTrue(self.filter.is_valid('/foo/bar/foo.bar'))
def add_all_static(self, concrete=True, unchanged_pod_paths=None): """Add all pod docs to the router.""" with self.pod.profile.timer('Router.add_all_static'): unchanged_pod_paths = unchanged_pod_paths or set() skipped_paths = [] for config in self.pod.static_configs: if config.get('dev') and not self.pod.env.dev: continue fingerprinted = config.get('fingerprinted', False) localization = config.get('localization') static_filter = config.get('filter', {}) if static_filter: path_filter = grow_path_filter.PathFilter( static_filter.get('ignore_paths'), static_filter.get('include_paths')) else: path_filter = self.pod.path_filter static_dirs = config.get('static_dirs') if not static_dirs: static_dirs = [config.get('static_dir')] if localization: localized_static_dirs = localization.get('static_dirs') if not localized_static_dirs: localized_static_dirs = [ localization.get('static_dir') ] if concrete: # Enumerate static files. for static_dir in static_dirs: for root, dirs, files in self.pod.walk(static_dir): for directory in dirs: if directory.startswith('.'): dirs.remove(directory) pod_dir = root.replace(self.pod.root, '') for file_name in files: pod_path = os.path.join(pod_dir, file_name) # Skip when the doc is in the unchanged pod paths set. if pod_path in unchanged_pod_paths: continue static_doc = self.pod.get_static(pod_path, locale=None) self.add_static_doc(static_doc, concrete=concrete) if localization: # TODO handle the localized static files? pass else: serve_at = self.pod.path_format.format_pod( config['serve_at'], parameterize=True) route_info = RouteInfo('static', meta={ 'path_format': serve_at, 'source_formats': static_dirs, 'localized': False, 'localization': localization, 'fingerprinted': fingerprinted, 'static_filter': static_filter, 'path_filter': path_filter, }) self._add_to_routes(serve_at + '*', route_info, concrete=concrete, fingerprinted=fingerprinted) if localization: localized_serve_at = self.pod.path_format.format_pod( localization.get('serve_at'), parameterize=True) route_info = RouteInfo( 'static', meta={ 'path_format': localized_serve_at, 'source_formats': localized_static_dirs, 'localized': True, 'localization': localization, 'fingerprinted': fingerprinted, 'static_filter': static_filter, 'path_filter': path_filter, }) self._add_to_routes(localized_serve_at + '*', route_info, concrete=concrete, fingerprinted=fingerprinted) if skipped_paths: self.pod.logger.info('Ignored {} static files.'.format( len(skipped_paths)))
def setUp(self): self.filter = path_filter.PathFilter()
def add_all_static(self, concrete=True): """Add all pod docs to the router.""" with self.pod.profile.timer('Router.add_all_static'): skipped_paths = [] for config in self.pod.static_configs: if config.get('dev') and not self.pod.env.dev: continue fingerprinted = config.get('fingerprinted', False) localization = config.get('localization') static_filter = config.get('filter', {}) if static_filter: path_filter = grow_path_filter.PathFilter( static_filter.get('ignore_paths'), static_filter.get('include_paths')) else: path_filter = self.pod.path_filter if concrete or fingerprinted: # Enumerate static files. for root, dirs, files in self.pod.walk( config.get('static_dir')): for directory in dirs: if directory.startswith('.'): dirs.remove(directory) pod_dir = root.replace(self.pod.root, '') for file_name in files: pod_path = os.path.join(pod_dir, file_name) static_doc = self.pod.get_static(pod_path, locale=None) self.add_static_doc(static_doc) else: serve_at = self.pod.path_format.format_pod( config['serve_at'], parameterize=True) self.routes.add( serve_at + '*', RouteInfo( 'static', { 'path_format': serve_at, 'source_format': config.get('static_dir'), 'localized': False, 'localization': localization, 'fingerprinted': fingerprinted, 'static_filter': static_filter, 'path_filter': path_filter, })) if localization: localized_serve_at = self.pod.path_format.format_pod( localization.get('serve_at'), parameterize=True) self.routes.add( localized_serve_at + '*', RouteInfo( 'static', { 'path_format': localized_serve_at, 'source_format': localization.get('static_dir'), 'localized': True, 'localization': localization, 'fingerprinted': fingerprinted, 'static_filter': static_filter, 'path_filter': path_filter, })) if skipped_paths: self.pod.logger.info('Ignored {} static files.'.format( len(skipped_paths)))