<commit_before>import os
import argparse
from cStringIO import StringIO
from openquake.nrmllib.hazard.parsers import GMFScenarioParser
from openquake.engine.db.models import Output, OqUser
from django.db import connection


def import_gmf_scenario(fname):
    """
    Parse the file with the GMF fields and import it into the table
    gmf_scenario. It also creates a new output record, unrelated to a job.
    Works both with XML files and tab-separated files with format
    (imt, gmvs, location).
    :returns: the generated :class:`openquake.engine.db.models.Output` object
    """
    curs = connection.cursor().cursor.cursor  # DB API cursor
    openquake = OqUser.objects.get(user_name='openquake')
    out = Output.objects.create(
        owner=openquake, display_name='Imported from %r' % fname,
        output_type='gmf_scenario')
    output_id = str(out.id)
    if fname.endswith('.xml'):
        f = StringIO()  # convert the XML into a tab-separated StringIO
        for imt, gmvs, loc in GMFScenarioParser(fname).parse():
            gmvs = '{%s}' % str(gmvs)[1:-1]
            print >> f, '\t'.join([output_id, imt, gmvs, loc])
        f.seek(0)  # rewind
    else:
        f = open(fname)
    ## import the file-like object with a COPY FROM
    try:
        curs.copy_expert(
            'copy hzrdr.gmf_scenario (output_id, imt, gmvs, location) '
            'from stdin', f)
    except:
        curs.connection.rollback()
    else:
        curs.connection.commit()
    finally:
        f.close()
    return out


## the test is here waiting for a better location
## should this functionality enter into bin/openquake?
def test_import_gmf_scenario():
    from openquake import nrmllib
    from nose.tools import assert_equal
    repodir = os.path.dirname(os.path.dirname(nrmllib.__path__[0]))
    out = import_gmf_scenario(
        os.path.join(repodir, 'examples', 'gmf-scenario.xml'))<commit_msg>Added a comment<commit_after>import os
import argparse
from cStringIO import StringIO
from openquake.nrmllib.hazard.parsers import GMFScenarioParser
from openquake.engine.db.models import Output, OqUser
from django.db import connection


def import_gmf_scenario(fname):
    """
    Parse the file with the GMF fields and import it into the table
    gmf_scenario. It also creates a new output record, unrelated to a job.
    Works both with XML files and tab-separated files with format
    (imt, gmvs, location).
    :returns: the generated :class:`openquake.engine.db.models.Output` object
    """
    curs = connection.cursor().cursor.cursor  # DB API cursor
    openquake = OqUser.objects.get(user_name='openquake')
    out = Output.objects.create(
        owner=openquake, display_name='Imported from %r' % fname,
        output_type='gmf_scenario')
    output_id = str(out.id)
    if fname.endswith('.xml'):
        f = StringIO()  # convert the XML into a tab-separated StringIO
        for imt, gmvs, loc in GMFScenarioParser(fname).parse():
            gmvs = '{%s}' % str(gmvs)[1:-1]
            print >> f, '\t'.join([output_id, imt, gmvs, loc])
        f.seek(0)  # rewind
    else:  # assume a tab-separated file
        f = open(fname)
    ## import the file-like object with a COPY FROM
    try:
        curs.copy_expert(
            'copy hzrdr.gmf_scenario (output_id, imt, gmvs, location) '
            'from stdin', f)
    except:
        curs.connection.rollback()
    else:
        curs.connection.commit()
    finally:
        f.close()
    return out


## the test is here waiting for a better location
## should this functionality enter into bin/openquake?
def test_import_gmf_scenario():
    from openquake import nrmllib
    from nose.tools import assert_equal
    repodir = os.path.dirname(os.path.dirname(nrmllib.__path__[0]))
    out = import_gmf_scenario(
        os.path.join(repodir, 'examples', 'gmf-scenario.xml'))
{'fraction_non_alphanumeric': 0.10302873414444733, 'fraction_numerical': 0.0020709293295366298, 'mean_word_length': 3.0, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2107766', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1240, 'n_words': 351}