<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#    Copyright (C) 2017-2021 CIRCL Computer Incident Response Center Luxembourg (securitymadein.lu gie)
#    Copyright (C) 2017-2021 <NAME>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


import argparse
import json
import sys
import traceback
from pathlib import Path
from stix.core import STIXPackage
from typing import Union

sys.path.append(str(Path(__file__).resolve().parent / 'misp-stix'))
from misp_stix_converter import MISPtoSTIX1EventsParser, _get_json_events, _get_xml_events


def _handle_errors(errors: dict):
    for identifier, values in errors.items():
        values = '\n - '.join(values)
        if identifier != 'attributes_collection':
            identifier = f'MISP event {identifier}'
        print(f'Errors encountered while parsing {identifier}:\n - {values}', file=sys.stderr)


def _process_misp_files(orgname: str, version: str, return_format:str, input_names: Union[list, None], debug: bool):
    print(orgname, file=sys.stderr)
    print(version, file=sys.stderr)
    print(return_format, file=sys.stderr)
    print(input_names, file=sys.stderr)
    if input_names is None:
        print('No input file provided.', file=sys.stderr)
        print(json.dumps({'success': 1}))
        return
    try:
        parser = MISPtoSTIX1EventsParser(orgname, version)
        for name in input_names[:-1]:
            parser.parse_json_content(name)
            with open(f'{name}.out', 'wt', encoding='utf-8') as f:
                f.write(f'')
        name = input_names[-1]
        parser.parse_json_content(name)
        with open(f'{name}.out', 'wt', encoding='utf-8') as f:
            f.write(globals()[f'_get_{return_format}_events'](parser.stix_package))
        errors = parser.errors
        if errors:
            _handle_errors(errors)
        print(json.dumps({'success': 1}))
    except Exception as e:
        print(json.dumps(
            {
                'error': e.__str__(),<commit_msg>fix: [stix1 export] Removed debugging prints<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#    Copyright (C) 2017-2021 CIRCL Computer Incident Response Center Luxembourg (securitymadein.lu gie)
#    Copyright (C) 2017-2021 <NAME>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


import argparse
import json
import sys
import traceback
from pathlib import Path
from stix.core import STIXPackage
from typing import Union

sys.path.append(str(Path(__file__).resolve().parent / 'misp-stix'))
from misp_stix_converter import MISPtoSTIX1EventsParser, _get_json_events, _get_xml_events


def _handle_errors(errors: dict):
    for identifier, values in errors.items():
        values = '\n - '.join(values)
        if identifier != 'attributes_collection':
            identifier = f'MISP event {identifier}'
        print(f'Errors encountered while parsing {identifier}:\n - {values}', file=sys.stderr)


def _process_misp_files(orgname: str, version: str, return_format:str, input_names: Union[list, None], debug: bool):
    if input_names is None:
        print('No input file provided.', file=sys.stderr)
        print(json.dumps({'success': 1}))
        return
    try:
        parser = MISPtoSTIX1EventsParser(orgname, version)
        for name in input_names[:-1]:
            parser.parse_json_content(name)
            with open(f'{name}.out', 'wt', encoding='utf-8') as f:
                f.write(f'')
        name = input_names[-1]
        parser.parse_json_content(name)
        with open(f'{name}.out', 'wt', encoding='utf-8') as f:
            f.write(globals()[f'_get_{return_format}_events'](parser.stix_package))
        errors = parser.errors
        if errors:
            _handle_errors(errors)
        print(json.dumps({'success': 1}))
    except Exception as e:
        print(json.dumps(
            {
                'error': e.__str__(),
{'fraction_non_alphanumeric': 0.10488421871959525, 'fraction_numerical': 0.010702471297917883, 'mean_word_length': 3.138486312399356, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3077670', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1488, 'n_words': 486}