<commit_before>import Rx from 'rxjs/Rx';

import {
  createCommOpenMessage,
  createCommMessage,
} from './comm';

import {
  createCellAfter,
  createCellBefore,
  createCellAppend,
} from '../actions';

import {
  TelluriumError,
} from '../middlewares';

import * as uuid from 'uuid';

export function convertFileEpic(action$, store) {
  return action$.ofType('CONVERT_FILE')
    .map((action) => {
      const state = store.getState();
      const channels = state.app.channels;
      if (!channels || !channels.iopub || !channels.shell) {
        throw new Error('kernel not connected');
      }

      const identity = uuid.v4();

      const source_format = action.filetype === 'python' ? 'python' : action.filetype === 'sbml' ? 'antimony' :
        action.filetype === 'omex' ? 'omex' : action.filetype === 'cellml' ? 'cellml' :
        () => {throw new TelluriumError('Source filetype not recognized.', 'ERROR IMPORTING ARCHIVE')};
      const target_format = action.filetype === 'python' ? 'python' : action.filetype === 'sbml' ? 'antimony' :
        action.filetype === 'omex' ? 'omex' : action.filetype === 'cellml' ? 'antimony' :
        () => {throw new TelluriumError('Source filetype not recognized.', 'ERROR IMPORTING ARCHIVE')};

      const commOpen = createCommOpenMessage(identity, 'convert_file_comm', {target_format: source_format, path: action.path});
      const childMessages = channels.iopub.childOf(commOpen);

      channels.shell.next(commOpen);
      return childMessages
        .ofMessageType(['comm_msg'])
        .map((message) => {
          if (message.content.data.status === 'error') {
            const notificationSystem = state.app.get('notificationSystem');
            // notificationSystem.addNotification({
            //   title: message.content.data.error,
            //   autoDismiss: 4,
            //   level: 'error',
            // });
            if (source_format === 'antimony') {
              throw new TelluriumError(message.content.data.error, 'ERROR IMPORTING SBML');
            } else {
              throw new TelluriumError(message.content.data.error, 'ERROR IMPORTING ARCHIVE');
            }
            // return Rx.Observable.throw(new Error('Unable to import archive.'));
            // return Rx.Observable.of({
            //   type: ERROR_GENERAL,
            //   payload: message.content.data.error,
            //   error: true,
            // });
          }
          if (action.id) {
            // we have a cell id
            if (action.position === 'below') {
              return createCellAfter(target_format, action.id, message.content.data.content)
            } else if (action.position === 'above') {
              return createCellBefore(target_format, action.id, message.content.data.content)
            }
          } else {
            // we don't have a cell id - just append to end
            return createCellAppend(target_format, message.content.data.content)
          }
        });
    })
    .mergeAll();
}

// used for saving non-notebook files
export function saveFileEpic(action$, store) {
  return action$.ofType('SAVE_FILE_FROM_STRING')
    .map((action) => {
      const state = store.getState();
      const channels = state.app.channels;
      if (!channels || !channels.iopub || !channels.shell) {
        throw new Error('kernel not connected');
      }

      const identity = uuid.v4();

      const source_format = action.source_format;
      const target_format = action.target_format;
      const source_str = action.source_str;
      const path = action.path;

      const commOpen = createCommOpenMessage(identity, 'save_file_comm', {
        source_format: source_format,
        target_format: target_format,
        source_str:    source_str,
        path: path});

      const childMessages = channels.iopub.childOf(commOpen);

      channels.shell.next(commOpen);

      return childMessages
        .ofMessageType(['comm_msg'])
        .map((message) => {
          // TODO: throw here on error
          if (message.content.data.status === 'okay') {
            // success
            const notificationSystem = state.app.get('notificationSystem');
            notificationSystem.addNotification({
              title: `Saved ${message.content.data.file}`,
              autoDismiss: 2,
              level: 'success',
            });
            return Rx.Observable.of({
              type: 'ERROR_SAVING',
              payload: 'error saving',
              error: true,
            });
          } else {
            throw new TelluriumError(message.content.data.error, 'ERROR SAVING');
          }
        }); // TODO: add take until
    })
    .mergeAll()
    .filter(() => false);
}

// used for saving non-notebook files
export function getNotebookPathEpic(action$, store) {
  return action$.ofType('COMM_OPEN')
    .filter((message) => message.target_name == "get_notebook_location")
    .map((message) => {
      const state = store.getState();
      const channels = state.app.channels;
      if (!channels || !channels.iopub || !channels.shell) {
        throw new Error('kernel not connected');
      }
      // console.log('open ', message.comm_id);
      // console.log('filename ', state.metadata.get('filename'));

      const reply = createCommMessage(message.comm_id, {location: state.metadata.get('filename') || ''});
      channels.shell.next(reply);
      // console.log('sent reply');
      return reply;
    })
    .filter(() => false);
}
<commit_msg>update importer epic - handle sbml-only combine archives<commit_after>import Rx from 'rxjs/Rx';

import {
  createCommOpenMessage,
  createCommMessage,
} from './comm';

import {
  createCellAfter,
  createCellBefore,
  createCellAppend,
} from '../actions';

import {
  TelluriumError,
} from '../middlewares';

import * as uuid from 'uuid';

export function convertFileEpic(action$, store) {
  return action$.ofType('CONVERT_FILE')
    .map((action) => {
      const state = store.getState();
      const channels = state.app.channels;
      if (!channels || !channels.iopub || !channels.shell) {
        throw new Error('kernel not connected');
      }

      const identity = uuid.v4();

      const source_format = action.filetype === 'python' ? 'python' : action.filetype === 'sbml' ? 'antimony' :
        action.filetype === 'omex' ? 'omex' : action.filetype === 'cellml' ? 'cellml' :
        () => {throw new TelluriumError('Source filetype not recognized.', 'ERROR IMPORTING ARCHIVE')};
      let target_format = action.filetype === 'python' ? 'python' : action.filetype === 'sbml' ? 'antimony' :
        action.filetype === 'omex' ? 'omex' : action.filetype === 'cellml' ? 'antimony' :
        () => {throw new TelluriumError('Source filetype not recognized.', 'ERROR IMPORTING ARCHIVE')};

      const commOpen = createCommOpenMessage(identity, 'convert_file_comm', {target_format: source_format, path: action.path});
      const childMessages = channels.iopub.childOf(commOpen);

      channels.shell.next(commOpen);
      return childMessages
        .ofMessageType(['comm_msg'])
        .map((message) => {
          if (message.content.data.status === 'error') {
            const notificationSystem = state.app.get('notificationSystem');
            // notificationSystem.addNotification({
            //   title: message.content.data.error,
            //   autoDismiss: 4,
            //   level: 'error',
            // });
            if (source_format === 'antimony') {
              throw new TelluriumError(message.content.data.error, 'ERROR IMPORTING SBML');
            } else {
              throw new TelluriumError(message.content.data.error, 'ERROR IMPORTING ARCHIVE');
            }
            // return Rx.Observable.throw(new Error('Unable to import archive.'));
            // return Rx.Observable.of({
            //   type: ERROR_GENERAL,
            //   payload: message.content.data.error,
            //   error: true,
            // });
          }
          // no error - add cells to notebook
          const cells = message.content.data.content.cells;
          if (cells) {
            if (cells.length === 0) {
              throw new TelluriumError('Failed to import any COMBINE archive entries', 'ERROR IMPORTING ARCHIVE');
            }
            if (cells.length > 1) {
              throw new TelluriumError('Multiple cells returned - operation not supported', 'ERROR IMPORTING ARCHIVE');
            }
            const cell = cells[0];
            // for combine archives containing only SBML
            if (cell.type === 'antimony') {
              target_format = 'antimony';
            }
            if (action.id) {
              // we have a cell id
              if (action.position === 'below') {
                return createCellAfter(target_format, action.id, cell.source)
              } else if (action.position === 'above') {
                return createCellBefore(target_format, action.id, cell.source)
              }
            } else {
              // we don't have a cell id - just append to end
              return createCellAppend(target_format, cell.source)
            }
          } else {
            throw new TelluriumError('Could not import file - internal error', 'ERROR IMPORTING ARCHIVE');
          }
        });
    })
    .mergeAll();
}

// used for saving non-notebook files
export function saveFileEpic(action$, store) {
  return action$.ofType('SAVE_FILE_FROM_STRING')
    .map((action) => {
      const state = store.getState();
      const channels = state.app.channels;
      if (!channels || !channels.iopub || !channels.shell) {
        throw new Error('kernel not connected');
      }

      const identity = uuid.v4();

      const source_format = action.source_format;
      const target_format = action.target_format;
      const source_str = action.source_str;
      const path = action.path;

      const commOpen = createCommOpenMessage(identity, 'save_file_comm', {
        source_format: source_format,
        target_format: target_format,
        source_str:    source_str,
        path: path});

      const childMessages = channels.iopub.childOf(commOpen);

      channels.shell.next(commOpen);

      return childMessages
        .ofMessageType(['comm_msg'])
        .map((message) => {
          // TODO: throw here on error
          if (message.content.data.status === 'okay') {
            // success
            const notificationSystem = state.app.get('notificationSystem');
            notificationSystem.addNotification({
              title: `Saved ${message.content.data.file}`,
              autoDismiss: 2,
              level: 'success',
            });
            return Rx.Observable.of({
              type: 'ERROR_SAVING',
              payload: 'error saving',
              error: true,
            });
          } else {
            throw new TelluriumError(message.content.data.error, 'ERROR SAVING');
          }
        }); // TODO: add take until
    })
    .mergeAll()
    .filter(() => false);
}

// used for saving non-notebook files
export function getNotebookPathEpic(action$, store) {
  return action$.ofType('COMM_OPEN')
    .filter((message) => message.target_name == "get_notebook_location")
    .map((message) => {
      const state = store.getState();
      const channels = state.app.channels;
      if (!channels || !channels.iopub || !channels.shell) {
        throw new Error('kernel not connected');
      }
      // console.log('open ', message.comm_id);
      // console.log('filename ', state.metadata.get('filename'));

      const reply = createCommMessage(message.comm_id, {location: state.metadata.get('filename') || ''});
      channels.shell.next(reply);
      // console.log('sent reply');
      return reply;
    })
    .filter(() => false);
}

{'fraction_non_alphanumeric': 0.13316965797494074, 'fraction_numerical': 0.0009312563494751101, 'mean_word_length': 2.2373252946012605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2863626', 'n_tokens_mistral': 3289, 'n_tokens_neox': 3238, 'n_words': 754}