From abb135c0f34b3c010f55d4f77058dccaa9246e39 Mon Sep 17 00:00:00 2001 From: Corinne Bosley Date: Tue, 10 Oct 2017 14:54:21 +0100 Subject: [PATCH 1/3] Removed deprecated Rules Container class --- lib/iris/fileformats/rules.py | 90 ----------------------------------- 1 file changed, 90 deletions(-) diff --git a/lib/iris/fileformats/rules.py b/lib/iris/fileformats/rules.py index ee1c958dcc..da1a50d0a6 100644 --- a/lib/iris/fileformats/rules.py +++ b/lib/iris/fileformats/rules.py @@ -549,96 +549,6 @@ def conditional_warning(self, condition, warning): warnings.warn(warning) -class RulesContainer(object): - """ - A collection of :class:`Rule` instances, with the ability to read rule - definitions from files and run the rules against given fields. - - .. deprecated:: 1.10 - - """ - def __init__(self, filepath=None, rule_type=FunctionRule): - """Create a new rule set, optionally adding rules from the specified file. - - The rule_type defaults to :class:`FunctionRule`, - e.g for CM loading actions that return objects, such as *AuxCoord(...)* - - rule_type can also be set to :class:`ProcedureRule` - e.g for PP saving actions that do not return anything, such as *pp.lbuser[3] = 16203* - """ - if _enable_rules_deprecations: - warn_deprecated( - "the `iris.fileformats.rules.RulesContainer class is deprecated.") - self._rules = [] - self.rule_type = rule_type - if filepath is not None: - self.import_rules(filepath) - - def import_rules(self, filepath): - """Extend the rule collection with the rules defined in the specified file.""" - # Define state constants - IN_CONDITION = 1 - IN_ACTION = 2 - - rule_file = os.path.expanduser(filepath) - conditions = [] - actions = [] - state = None - - with open(rule_file, 'r') as file: - for line in file: - line = line.rstrip() - if line == "IF": - if conditions and actions: - self._rules.append(self.rule_type(conditions, actions)) - conditions = [] - actions = [] - state = IN_CONDITION - elif line == "THEN": - state = IN_ACTION - elif len(line) == 0: - pass - elif line.strip().startswith('#'): - pass - elif state == IN_CONDITION: - conditions.append(line) - elif state == IN_ACTION: - actions.append(line) - else: - raise Exception('Rule file not read correctly at line: ' + - line) - if conditions and actions: - self._rules.append(self.rule_type(conditions, actions)) - - def verify(self, cube, field): - """ - Add to the given :class:`iris.cube.Cube` by running this set of - rules with the given field. - - Args: - - * cube: - An instance of :class:`iris.cube.Cube`. - * field: - A field object relevant to the rule set. - - Returns: (cube, matching_rules) - - * cube - the resultant cube - * matching_rules - a list of rules which matched - - """ - matching_rules = [] - factories = [] - for rule in self._rules: - if rule.evaluates_true(cube, field): - matching_rules.append(rule) - rule_factories = rule.run_actions(cube, field) - if rule_factories: - factories.extend(rule_factories) - return RuleResult(cube, matching_rules, factories) - - def scalar_coord(cube, coord_name): """Try to find a single-valued coord with the given name.""" found_coord = None From e2ceb9033ca44d68659475a94c3b63e619df1a51 Mon Sep 17 00:00:00 2001 From: Corinne Bosley Date: Tue, 10 Oct 2017 15:36:28 +0100 Subject: [PATCH 2/3] fixed a swarm of failing tests --- lib/iris/fileformats/pp.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 934c6cab42..134a9357cc 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -1905,21 +1905,6 @@ def _field_gen(filename, read_data_bytes, little_ended=False): yield pp_field -def _ensure_save_rules_loaded(): - """Makes sure the standard save rules are loaded.""" - - # Uses these module-level variables - global _save_rules - - if _save_rules is None: - # Load the pp save rules - rules_filename = os.path.join(iris.config.CONFIG_PATH, - 'pp_save_rules.txt') - with iris.fileformats.rules._disable_deprecation_warnings(): - _save_rules = iris.fileformats.rules.RulesContainer( - rules_filename, iris.fileformats.rules.ProcedureRule) - - # Stash codes not to be filtered (reference altitude and pressure fields). _STASH_ALLOW = [STASH(1, 0, 33), STASH(1, 0, 1)] From 8206a151f4c7d5245f35e13ada4e37657c8867dd Mon Sep 17 00:00:00 2001 From: Corinne Bosley Date: Wed, 11 Oct 2017 11:04:56 +0100 Subject: [PATCH 3/3] removed save rules from some more tests --- lib/iris/fileformats/pp.py | 2 -- lib/iris/tests/integration/test_pp.py | 9 --------- lib/iris/tests/test_cube_to_pp.py | 1 - 3 files changed, 12 deletions(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 53cf1af5e3..cb83f2ee79 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -2208,8 +2208,6 @@ def save_pairs_from_cube(cube, field_coords=None, target=None): # On the flip side, record which Cube metadata has been "used" and flag up # unused? - _ensure_save_rules_loaded() - n_dims = len(cube.shape) if n_dims < 2: raise ValueError('Unable to save a cube of fewer than 2 dimensions.') diff --git a/lib/iris/tests/integration/test_pp.py b/lib/iris/tests/integration/test_pp.py index 235205d591..ff106588fd 100644 --- a/lib/iris/tests/integration/test_pp.py +++ b/lib/iris/tests/integration/test_pp.py @@ -73,7 +73,6 @@ def test_soil_level_round_trip(self): field.lbvc = 0 field.brsvd = [None] * 4 field.brlev = None - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, field) # Check the vertical coordinate is as originally specified. @@ -111,7 +110,6 @@ def test_soil_depth_round_trip(self): field.lbvc = 0 field.brlev = None field.brsvd = [None] * 4 - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, field) # Check the vertical coordinate is as originally specified. @@ -144,7 +142,6 @@ def test_potential_temperature_level_round_trip(self): field = iris.fileformats.pp.PPField3() field.lbfc = 0 field.lbvc = 0 - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, field) # Check the vertical coordinate is as originally specified. @@ -214,7 +211,6 @@ def field_with_data(scale=1): pressure_field.lbvc = 0 pressure_field.brsvd = [None, None] pressure_field.lbuser = [None] * 7 - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(pressure_cube, pressure_field) data_field = iris.fileformats.pp.PPField3() @@ -306,7 +302,6 @@ def test_hybrid_height_with_non_standard_coords(self): field.lbvc = 0 field.brsvd = [None, None] field.lbuser = [None] * 7 - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, field) self.assertEqual(field.blev, delta) @@ -343,7 +338,6 @@ def test_hybrid_pressure_with_non_standard_coords(self): field.lbvc = 0 field.brsvd = [None, None] field.lbuser = [None] * 7 - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, field) self.assertEqual(field.bhlev, delta) @@ -409,7 +403,6 @@ def field_with_data(scale=1): data_field.lbvc = 0 data_field.brsvd = [None, None] data_field.lbuser = [None] * 7 - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(data_cube, data_field) # Check the data field has the vertical coordinate as originally @@ -446,7 +439,6 @@ def convert_cube_to_field(self, cube): field.lbfc = 0 field.lbvc = 0 field.lbtim = 0 - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, field) return field @@ -633,7 +625,6 @@ def create_cube(self, longitude_coord='longitude'): def convert_cube_to_field(self, cube): field = iris.fileformats.pp.PPField3() field.lbvc = 0 - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, field) return field diff --git a/lib/iris/tests/test_cube_to_pp.py b/lib/iris/tests/test_cube_to_pp.py index 6ee905a1ff..df2f570fe9 100644 --- a/lib/iris/tests/test_cube_to_pp.py +++ b/lib/iris/tests/test_cube_to_pp.py @@ -184,7 +184,6 @@ def test_365_calendar_export(self): # Add an extra "fill_value" property, as used by the save rules. cube.fill_value = None pp_field = mock.MagicMock(spec=PPField3) - iris.fileformats.pp._ensure_save_rules_loaded() iris.fileformats.pp._save_rules.verify(cube, pp_field) self.assertEqual(pp_field.lbtim.ic, 4)