(self, import_location, export_location)
Export/Import search templates to/from file.

        Args:
            import_location: Path to the yaml file to import templates.
            export_location: Path to the yaml file to export templates.
Export/Import search templates to/from file.
Export / Import search templates to / from file .
def run(self, import_location, export_location):
        """Export/Import search templates to/from file.

        Args:
            import_location: Path to the yaml file to import templates.
            export_location: Path to the yaml file to export templates.
        """

        if export_location:
            search_templates = []
            for search_template in SearchTemplate.query.all():
                labels = []
                for label in search_template.labels:
                    if label.label.startswith('supported_os:'):
                        labels.append(label.label.replace(
                            'supported_os:', ''))
                search_templates.append({
                    'name': search_template.name,
                    'query_string': search_template.query_string,
                    'query_dsl': search_template.query_dsl,
                    'supported_os': labels
                })

            with open(export_location, 'w') as fh:
                yaml.safe_dump(search_templates, stream=fh)

        if import_location:
            try:
                with open(import_location, 'rb') as fh:
                    search_templates = yaml.safe_load(fh)
            except IOError as e:
                sys.stdout.write('Unable to open file: {0!s}\n'.format(e))
                sys.exit(1)

            for search_template in search_templates:
                name = search_template['name']
                query_string = search_template['query_string']
                query_dsl = search_template['query_dsl']

                # Skip search template if already exits.
                if SearchTemplate.query.filter_by(name=name).first():
                    continue

                imported_template = SearchTemplate(
                    name=name,
                    user=User(None),
                    query_string=query_string,
                    query_dsl=query_dsl)

                # Add supported_os labels.
                for supported_os in search_template['supported_os']:
                    label_name = 'supported_os:{0:s}'.format(supported_os)
                    label = SearchTemplate.Label.get_or_create(
                        label=label_name, user=None)
                    imported_template.labels.append(label)

                # Set flag to identify local vs import templates.
                remote_flag = SearchTemplate.Label.get_or_create(
                    label='remote_template', user=None)
                imported_template.labels.append(remote_flag)

                db_session.add(imported_template)
                db_session.commit()
def run ( self , import_location , export_location ) : if export_location : search_templates = [ ] for search_template in SearchTemplate . query . all ( ) : labels = [ ] for label in search_template . labels : if label . label . startswith ( 'supported_os:' ) : labels . append ( label . label . replace ( 'supported_os:' , '' ) ) search_templates . append ( { 'name' : search_template . name , 'query_string' : search_template . query_string , 'query_dsl' : search_template . query_dsl , 'supported_os' : labels } ) with open ( export_location , 'w' ) as fh : yaml . safe_dump ( search_templates , stream = fh ) if import_location : try : with open ( import_location , 'rb' ) as fh : search_templates = yaml . safe_load ( fh ) except IOError as e : sys . stdout . write ( 'Unable to open file: {0!s}\n' . format ( e ) ) sys . exit ( 1 ) for search_template in search_templates : name = search_template [ 'name' ] query_string = search_template [ 'query_string' ] query_dsl = search_template [ 'query_dsl' ] # Skip search template if already exits. if SearchTemplate . query . filter_by ( name = name ) . first ( ) : continue imported_template = SearchTemplate ( name = name , user = User ( None ) , query_string = query_string , query_dsl = query_dsl ) # Add supported_os labels. for supported_os in search_template [ 'supported_os' ] : label_name = 'supported_os:{0:s}' . format ( supported_os ) label = SearchTemplate . Label . get_or_create ( label = label_name , user = None ) imported_template . labels . append ( label ) # Set flag to identify local vs import templates. remote_flag = SearchTemplate . Label . get_or_create ( label = 'remote_template' , user = None ) imported_template . labels . append ( remote_flag ) db_session . add ( imported_template ) db_session . commit ( )