예제 #1
0
def test_yaml_load(input_):
    '''
    tests the yaml_load function. Note that json formatted string are also
    recognized, and the quote character can be ' (in json, only " is
    allowed)
    '''
    expected = {'a': 9}
    assert yaml_load(input_) == expected
    if isinstance(input_, str):
        assert yaml_load(StringIO(input_)) == expected
예제 #2
0
파일: views.py 프로젝트: rizac/eGSIM
def download_request(request, key, filename):
    '''Returns the request (configuration) re-formatted according to the syntax
    inferred from filename (*.json or *.yaml) to be downloaded by the front
    end GUI.

    :param key: string in [KEY.TRELLIS, KEY.RESIDUALS, KEY.TESTING]
    '''
    formclass = _key2view(key).formclass
    inputdict = yaml_load(request.body.decode('utf-8'))
    dataform = formclass(data=inputdict)  # pylint: disable=not-callable
    if not dataform.is_valid():
        return invalidform2json(dataform)
    buffer = io.StringIO()
    ext_nodot = os.path.splitext(filename)[1][1:].lower()
    dataform.dump(buffer, syntax=ext_nodot)
    buffer.seek(0)
    if ext_nodot == 'json':
        # in the frontend the axios library expects bytes data (blob)
        # or bytes strings in order for the data to be correctly saved. Thus,
        # use text/javascript as 'application/json' does not work (or should we
        # better text/plain?)
        response = HttpResponse(buffer, content_type='text/javascript')
    else:
        response = HttpResponse(buffer, content_type='application/x-yaml')
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return response
예제 #3
0
def download_astext(request, filename, viewclass, text_sep=',', text_dec='.'):
    '''Returns the request re-formatted as text/csv.
    Uses request.body so this method should be called from a POST request
    '''
    inputdict = yaml_load(request.body.decode('utf-8'))
    response = viewclass.response_text(inputdict, text_sep, text_dec)
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return response
예제 #4
0
파일: views.py 프로젝트: rizac/eGSIM
def download_astext(request, key, filename, text_sep=',', text_dec='.'):
    '''Returns the text/csv data to be downloaded by the front end GUI.
    The request's body is the JSON data resulting from a previous
    call of the GET or POST method of any these
    views: TrellisView, ResidualsView, TestingView.

    :param key: string in [KEY.TRELLIS, KEY.RESIDUALS, KEY.TESTING]
    '''
    viewclass = _key2view(key)
    inputdict = yaml_load(request.body.decode('utf-8'))
    response = viewclass.response_text(inputdict, text_sep, text_dec)
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return response
예제 #5
0
def download_request(request, filename, formclass):
    '''Returns the request re-formatted according to the syntax
    inferred from filename (*.json or *.yaml).
    Uses request.body so this method should be called from a POST request
    '''
    inputdict = yaml_load(request.body.decode('utf-8'))
    dataform = formclass(data=inputdict)  # pylint: disable=not-callable
    if not dataform.is_valid():
        return invalidform2json(dataform)
    buffer = io.StringIO()
    ext_nodot = os.path.splitext(filename)[1][1:].lower()
    dataform.dump(buffer, syntax=ext_nodot)
    buffer.seek(0)
    if ext_nodot == 'json':
        response = HttpResponse(buffer, content_type='application/json')
    else:
        response = HttpResponse(buffer, content_type='application/x-yaml')
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    return response
예제 #6
0
 def post(self, request):
     '''processes a post request'''
     try:
         return self.response(yaml_load(request.body.decode('utf-8')))
     except Exception as err:
         return requestexc2json(err)
예제 #7
0
def test_trellisform_load_dump(data, expected_yaml, expected_json, areequal):
    '''test that form serialization works for ndarray given as range and
    arrays (in the first case preserves the string with colons, and that a
    Form taking the serialized yaml has the same clean() method as the original
    form's clean method'''
    form = TrellisForm(data)
    assert form.is_valid()

    with pytest.raises(ValueError):  # @UndefinedVariable
        form.dump(syntax='whatever')

    cleaned_data = form.cleaned_data
    yaml_ = form.dump(syntax='yaml')
    json_ = form.dump(syntax='json')

    # pass the yaml and json to validate and see that we obtain the same
    # dict(s):
    form_from_yaml = TrellisForm(data=yaml_load(yaml_))
    assert form_from_yaml.is_valid()
    assert areequal(cleaned_data, form_from_yaml.cleaned_data)

    form_from_json = TrellisForm(data=yaml_load(json_))
    assert form_from_json.is_valid()
    assert areequal(cleaned_data, form_from_json.cleaned_data)

    expected_json_ = """{
  "aspect": 1.2,
  "backarc": false,
  "dip": 5.0,
  "distance": [
    1.0,
    2.0,
    3.0,
    4.0,
    5.0
  ],
  "gsim": [
    "BindiEtAl2011",
    "BindiEtAl2014Rjb"
  ],
  "hypocentre_location": [
    0.5,
    0.5
  ],
  "imt": [
    "SA(0.1)",
    "SA(0.2)",
    "PGA",
    "PGV"
  ],
  "initial_point": [
    0.0,
    0.0
  ],
  "line_azimuth": 0.0,
  "magnitude": %s,
  "magnitude_scalerel": "WC1994",
  "plot_type": "m",
  "rake": 0.0,
  "strike": 0.0,
  "vs30": 760.0,
  "vs30_measured": true,
  "ztor": 0.0
}""" % expected_json

    expected_json_ = json.loads(expected_json_)
    # remove optional fields because they will not be rendered by dump:
    for key in list(expected_json_.keys()):
        if TrellisForm.is_optional(key):
            expected_json_.pop(key)
    assert areequal(json.loads(json_), expected_json_)

    expected_yaml_ = """# Ground Shaking Intensity Model(s)
gsim:
  - BindiEtAl2011
  - BindiEtAl2014Rjb

# Intensity Measure Type(s)
imt:
  - SA(0.1)
  - SA(0.2)
  - PGA
  - PGV

# Rupture Length / Width
aspect: 1.2

# Dip
dip: 5

# Magnitude(s)
magnitude:%s

# Distance(s)
distance:
  - 1
  - 2
  - 3
  - 4
  - 5

# Plot type
plot_type: m

""" % expected_yaml

    yaml_ = yaml_load(yaml_)
    expected_yaml_ = yaml_load(expected_yaml_)
    # remove optional fields because they will not be rendered by dump:
    for key in list(expected_yaml_.keys()):
        if TrellisForm.is_optional(key):
            expected_yaml_.pop(key)
    assert areequal(yaml_, expected_yaml_)
예제 #8
0
파일: test_forms.py 프로젝트: rizac/eGSIM
def test_trellisform_load_dump(
        data,
        expected_mag_yaml,
        expected_mag_json,
        # pytest fixtures:
        areequal):
    '''test that form serialization works for ndarray given as range and
    arrays (in the first case preserves the string with colons, and that a
    Form taking the serialized yaml has the same clean() method as the original
    form's clean method'''
    form = TrellisForm(data)
    assert form.is_valid()

    with pytest.raises(ValueError):  # @UndefinedVariable
        form.dump(syntax='whatever')

    cleaned_data = form.cleaned_data
    yaml_ = form.dump(syntax='yaml')
    json_ = form.dump(syntax='json')

    # pass the yaml and json to validate and see that we obtain the same
    # dict(s):
    form_from_yaml = TrellisForm(data=yaml_load(yaml_))
    assert form_from_yaml.is_valid()
    assert areequal(cleaned_data, form_from_yaml.cleaned_data)

    form_from_json = TrellisForm(data=yaml_load(json_))
    assert form_from_json.is_valid()
    assert areequal(cleaned_data, form_from_json.cleaned_data)

    expected_json_ = """{
    "aspect": 1.2,
    "dip": 5,
    "distance": [
        1,
        2,
        3,
        4,
        5
    ],
    "gsim": [
        "BindiEtAl2011",
        "BindiEtAl2014Rjb"
    ],
    "imt": [
        "SA(0.1)",
        "SA(0.2)",
        "PGA",
        "PGV"
    ],
    "initial_point": "0 1",
    "magnitude": %s,
    "plot_type": "m",
    "z1pt0": 34.5
}""" % expected_mag_json

    assert expected_json_ == json_

    expected_yaml_ = """# Ground Shaking Intensity Model(s)
gsim:
  - BindiEtAl2011
  - BindiEtAl2014Rjb

# Intensity Measure Type(s)
imt:
  - SA(0.1)
  - SA(0.2)
  - PGA
  - PGV

# Rupture Length / Width
aspect: 1.2

# Dip
dip: 5

# Magnitude(s)
magnitude:%s

# Distance(s)
distance:
  - 1
  - 2
  - 3
  - 4
  - 5

# Depth to 1 km/s VS layer (m) (Calculated from the VS30 if not given)
z1pt0: 34.5

# Location on Earth (Longitude Latitude)
initial_point: 0 1

# Plot type
plot_type: m

""" % expected_mag_yaml

    assert expected_yaml_ == yaml_
예제 #9
0
 def readyaml(self, filename):
     return yaml_load(self.path(filename))