Skip to content

Commit

Permalink
Update codebase to py38 (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
janbjorge authored Jan 9, 2024
1 parent 975c21c commit ef36369
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 60 deletions.
9 changes: 4 additions & 5 deletions examples/s/d/nn/_project/aggregate_surfaces.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Use fmu-dataio for aggregated surfaces created by an aggregation service."""

from pathlib import Path
import logging
from pathlib import Path

import yaml
import numpy as np

import xtgeo
import yaml

import fmu.dataio


Expand Down Expand Up @@ -67,7 +67,6 @@ def main():
)

for operation in operations:

print(f"Running aggregation: {operation}")

# Call the aggregation machine and create an aggregated surface
Expand Down Expand Up @@ -128,7 +127,7 @@ def _parse_yaml(fname):
dict
"""

with open(fname, "r") as stream:
with open(fname) as stream:
data = yaml.safe_load(stream)
return data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
For the file case, CSV files are read from disk. The dataio function is the same.
"""
import pathlib

import pandas as pd
import fmu.dataio
from fmu.config import utilities as ut

import fmu.dataio

CFG = ut.yaml_load("../../fmuconfig/output/global_variables.yml")

IN_ROXAR = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def export_faultlines():
)

for hname in HORISONNAMES:

# RMS version for reading polygons from a project:
# poly = xtgeo.polygons_from_roxar(project, hname, RMS_POL_CATEGORY)

Expand Down
1 change: 0 additions & 1 deletion src/fmu/dataio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from fmu.dataio.dataio import AggregatedData # noqa # type: ignore
from fmu.dataio.dataio import ExportData # noqa # type: ignore
from fmu.dataio.dataio import InitializeCase # noqa # type: ignore

from fmu.dataio.dataio import read_metadata # noqa

try:
Expand Down
11 changes: 4 additions & 7 deletions src/fmu/dataio/_design_kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

# pylint: disable=logging-fstring-interpolation
import logging
import shlex
import re

import shlex

_STATUS_FILE_NAME = "DESIGN_KW.OK"

Expand Down Expand Up @@ -38,7 +37,7 @@ def run(

key_vals.update(rm_genkw_prefix(key_vals))

with open(template_file_name, "r") as template_file:
with open(template_file_name) as template_file:
template = template_file.readlines()

if valid:
Expand All @@ -63,10 +62,8 @@ def all_matched(line, template_file_name, template):
for unmatched in unmatched_templates(line):
if is_perl(template_file_name, template):
_logger.warning( # pylint: disable=logging-fstring-interpolation
(
f"{unmatched} not found in design matrix, "
f"but this is probably a Perl file"
)
f"{unmatched} not found in design matrix, "
f"but this is probably a Perl file"
)
else:
_logger.error( # pylint: disable=logging-fstring-interpolation
Expand Down
2 changes: 1 addition & 1 deletion src/fmu/dataio/_filedata_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,6 @@ def _get_path_generic(self, mode="realization", allow_forcefolder=True, info="")

# check that destination actually exists if verifyfolder is True
if self.dataio.verifyfolder and not dest.exists():
raise IOError(f"Folder {str(dest)} is not present.")
raise OSError(f"Folder {str(dest)} is not present.")

return dest
2 changes: 1 addition & 1 deletion src/fmu/dataio/_fmu_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def get_ert2_information(self):
if self.dataio.include_ert2jobs:
jobs_file = self.iter_path / "jobs.json"
if jobs_file.is_file():
with open(jobs_file, "r") as stream:
with open(jobs_file) as stream:
self.ert2["jobs"] = json.load(stream)
logger.debug("jobs.json parsed.")
logger.debug("jobs.json was not found")
Expand Down
2 changes: 1 addition & 1 deletion src/fmu/dataio/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
# https://realpython.com/python-data-classes/#basic-data-classes

import datetime
from datetime import timezone
import getpass
import logging
from dataclasses import dataclass, field
from datetime import timezone
from pathlib import Path
from typing import Any, Optional
from warnings import warn
Expand Down
4 changes: 0 additions & 4 deletions src/fmu/dataio/_oyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ def map_constructor(loader, node):
pyyaml.add_representer(OrderedDict, map_representer, Dumper=DangerDumper)


if sys.version_info < (3, 7):
pyyaml.add_constructor("tag:yaml.org,2002:map", map_constructor)


del map_constructor, map_representer

# cf. stackoverflow.com/questions/21695705/dump-an-python-object-as-yaml-file/51261042
Expand Down
12 changes: 6 additions & 6 deletions src/fmu/dataio/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,17 @@ def create_symlink(source, target):

thesource = Path(source)
if not thesource.exists():
raise IOError(f"Cannot symlink: Source file {thesource} does not exist.")
raise OSError(f"Cannot symlink: Source file {thesource} does not exist.")

thetarget = Path(target)

if thetarget.exists() and not thetarget.is_symlink():
raise IOError(f"Target file {thetarget} exists already as a normal file.")
raise OSError(f"Target file {thetarget} exists already as a normal file.")

os.symlink(source, target)

if not (thetarget.exists() and thetarget.is_symlink()):
raise IOError(f"Target file {thesource} does not exist or is not a symlink.")
raise OSError(f"Target file {thesource} does not exist or is not a symlink.")


def size(fname):
Expand Down Expand Up @@ -421,13 +421,13 @@ def read_metadata(filename: Union[str, Path]) -> dict:
"""
fname = Path(filename)
if fname.stem.startswith("."):
raise IOError(f"The input is a hidden file, cannot continue: {fname.stem}")
raise OSError(f"The input is a hidden file, cannot continue: {fname.stem}")

metafile = str(fname.parent) + "/." + fname.stem + fname.suffix + ".yml"
metafilepath = Path(metafile)
if not metafilepath.exists():
raise IOError(f"Cannot find requested metafile: {metafile}")
with open(metafilepath, "r") as stream:
raise OSError(f"Cannot find requested metafile: {metafile}")
with open(metafilepath) as stream:
metacfg = yaml.safe_load(stream)

return metacfg
Expand Down
2 changes: 1 addition & 1 deletion src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ def export(self, obj, **kwargs) -> str:
abspath = metadata["file"].get("absolute_path", None)

if not abspath:
raise IOError(
raise OSError(
"The absolute_path is None, hence no export is possible. "
"Use the ``casepath`` key to provide a valid absolute path."
)
Expand Down
7 changes: 4 additions & 3 deletions src/fmu/dataio/scripts/create_case_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
pointed towards the produced global_variables, fmu-config should run
before this script to make sure global_variables is updated."""

import yaml
import argparse
import logging
from pathlib import Path

import yaml

try:
from ert.shared.plugins.plugin_manager import hook_implementation # type: ignore
except ModuleNotFoundError:
Expand Down Expand Up @@ -124,7 +125,7 @@ def register_on_sumo(args, case_metadata_path) -> str:
return

# lazy loading of Sumo dependencies
from fmu.sumo.uploader import SumoConnection, CaseOnDisk
from fmu.sumo.uploader import CaseOnDisk, SumoConnection

# establish connection
sumo_conn = SumoConnection(env=env)
Expand All @@ -144,7 +145,7 @@ def register_on_sumo(args, case_metadata_path) -> str:
def _parse_yaml(path):
"""Parse the global variables, return as dict"""

with open(path, "r") as stream:
with open(path) as stream:
data = yaml.safe_load(stream)

return data
Expand Down
14 changes: 6 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
"""The conftest.py, providing magical fixtures to tests."""
import datetime
import inspect
import json
import logging
import os
import shutil
from functools import wraps
from pathlib import Path
import json
import yaml
import datetime

import pandas as pd
import pytest
import xtgeo
import yaml
from fmu.config import utilities as ut
from termcolor import cprint


try:
import pyarrow as pa
except ImportError:
Expand Down Expand Up @@ -147,7 +146,7 @@ def fixture_rmsglobalconfig(rmssetup):
# read the global config
os.chdir(rmssetup)
logger.info("Global config is %s", str(rmssetup / "global_variables.yml"))
with open("global_variables.yml", "r", encoding="utf8") as stream:
with open("global_variables.yml", encoding="utf8") as stream:
global_cfg = yaml.safe_load(stream)

logger.info("Ran setup for %s", "rmsglobalconfig")
Expand Down Expand Up @@ -284,7 +283,6 @@ def fixture_globalconfig2() -> dict:
globvar = {}
with open(
ROOTPWD / "tests/data/drogon/global_config2/global_variables.yml",
"r",
encoding="utf-8",
) as stream:
globvar = yaml.safe_load(stream)
Expand Down Expand Up @@ -576,15 +574,15 @@ def fixture_drogon_volumes():

def _parse_json(schema_path):
"""Parse the schema, return JSON"""
with open(schema_path, "r", encoding="utf-8") as stream:
with open(schema_path, encoding="utf-8") as stream:
data = json.load(stream)

return data


def _parse_yaml(yaml_path):
"""Parse the filename as json, return data"""
with open(yaml_path, "r", encoding="utf-8") as stream:
with open(yaml_path, encoding="utf-8") as stream:
data = yaml.safe_load(stream)

data = _isoformat_all_datetimes(data)
Expand Down
1 change: 0 additions & 1 deletion tests/test_schema/test_schema_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from copy import deepcopy

import jsonschema

import pytest

from fmu.dataio._definitions import ALLOWED_CONTENTS
Expand Down
5 changes: 2 additions & 3 deletions tests/test_units/test_dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import pathlib
import sys

from copy import deepcopy

import pytest
Expand Down Expand Up @@ -426,7 +425,7 @@ def test_norwegian_letters_globalconfig(globalvars_norw_letters, regsurf):
# export to file and reread as raw
result = pathlib.Path(edata.export(regsurf))
metafile = result.parent / ("." + str(result.stem) + ".gri.yml")
with open(metafile, "r", encoding="utf-8") as stream:
with open(metafile, encoding="utf-8") as stream:
stuff = stream.read()
assert "DRÅGØN" in stuff

Expand All @@ -453,7 +452,7 @@ def test_norwegian_letters_globalconfig_as_json(globalvars_norw_letters, regsurf

result = pathlib.Path(edata.export(regsurf))
metafile = result.parent / ("." + str(result.stem) + ".gri.json")
with open(metafile, "r", encoding="utf-8") as stream:
with open(metafile, encoding="utf-8") as stream:
stuff = stream.read()
assert "DRÅGØN" in stuff

Expand Down
16 changes: 8 additions & 8 deletions tests/test_units/test_dictionary.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Test dictionary functionality"""
import os
import json
import os
from pathlib import Path
import yaml

import pytest
import yaml

from fmu.dataio import ExportData
from fmu.dataio._utils import read_parameters_txt, nested_parameters_dict
from fmu.dataio._utils import nested_parameters_dict, read_parameters_txt


@pytest.fixture(name="direct_creation", scope="function")
Expand All @@ -31,9 +33,7 @@ def _fixture_json(fmurun_w_casemetadata):
"""
os.chdir(fmurun_w_casemetadata)
print(fmurun_w_casemetadata)
with open(
fmurun_w_casemetadata / "parameters.json", "r", encoding="utf-8"
) as stream:
with open(fmurun_w_casemetadata / "parameters.json", encoding="utf-8") as stream:
json_dict = json.load(stream)
return json_dict

Expand Down Expand Up @@ -89,10 +89,10 @@ def read_dict_and_meta(path):
tuple: the dictionary produced with corresponding metadata
"""
result_dict = None
with open(path, "r", encoding="utf-8") as stream:
with open(path, encoding="utf-8") as stream:
result_dict = json.load(stream)
path = Path(path)
with open(path.parent / f".{path.name}.yml", "r", encoding="utf-8") as meta_stream:
with open(path.parent / f".{path.name}.yml", encoding="utf-8") as meta_stream:
meta = yaml.load(meta_stream, Loader=yaml.Loader)
return result_dict, meta

Expand Down
2 changes: 1 addition & 1 deletion tests/test_units/test_fmuprovider_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

import fmu.dataio as dio
from fmu.dataio._fmu_provider import _FmuProvider, _get_folderlist, RESTART_PATH_ENVNAME
from fmu.dataio._fmu_provider import RESTART_PATH_ENVNAME, _FmuProvider, _get_folderlist

FOLDERTREE = "scratch/myfield/case/realization-13/iter-2"

Expand Down
8 changes: 4 additions & 4 deletions tests/test_units/test_initialize_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_inicase_barebone_with_export(globalconfig2, fmurun):
casemetafile = caseroot / "share/metadata/fmu_case.yml"

# check that special characters made it through
with open(casemetafile, "r") as stream:
with open(casemetafile) as stream:
metadata = yaml.safe_load(stream)

assert metadata["fmu"]["case"]["name"] == "MyCaseName_with_Æ"
Expand All @@ -48,7 +48,7 @@ def test_inicase_barebone_with_export(globalconfig2, fmurun):
# Check that special characters are encoded properly in stored metadatafile.
# yaml.safe_load() seems to sort this out, but we want files on disk to be readable.
# Therefore check by reading the raw file content.
with open(casemetafile, "r") as stream:
with open(casemetafile) as stream:
metadata_string = stream.read()

assert "æøå" in metadata_string
Expand Down Expand Up @@ -161,7 +161,7 @@ def test_inicase_generate_case_metadata_exists_but_force(
casemetafolder = fmurun_w_casemetadata.parent.parent
old_metafile = casemetafolder / "share/metadata/fmu_case.yml"

with open(old_metafile, "r", encoding="utf-8") as stream:
with open(old_metafile, encoding="utf-8") as stream:
old_content = yaml.safe_load(stream)

icase = InitializeCase(globalconfig2, verbosity="INFO")
Expand All @@ -174,7 +174,7 @@ def test_inicase_generate_case_metadata_exists_but_force(
)

new_metafile = casemetafolder / "share/metadata/fmu_case.yml"
with open(new_metafile, "r", encoding="utf-8") as stream:
with open(new_metafile, encoding="utf-8") as stream:
new_content = yaml.safe_load(stream)

logger.debug("\n%s\n", prettyprint_dict(old_content))
Expand Down
Loading

0 comments on commit ef36369

Please sign in to comment.