def testGetTreeLocationWithSubdirs(self): testpath = 'HVAC/fields/subdir1/subdir2' folderpath, component = base_lib.GetTreeLocation(testpath) self.assertEqual(folderpath, 'HVAC/fields') self.assertEqual(base_lib.ComponentType.FIELD, component)
def testGetTreeLocationGlobal(self): testpath = 'fields' folderpath, component = base_lib.GetTreeLocation(testpath) self.assertEqual(folderpath, 'fields') self.assertEqual(base_lib.ComponentType.FIELD, component)
def SeparateConfigFiles(path_tuples): """Separates list of config files into lists of different config file kinds. If the directory name is 'fields' the file is a field definition file, if it is 'subfields' the file is a subfield def file, if it is 'states' the file is a states def file, if it is 'units' the file is a units def file, if it is 'entity_types' the file is a entity types def file. Method retains relative order of paths in each list. Args: path_tuples: a list of PathParts tuples for config files. Returns: A Config namedtuple, which contains separate lists for fields, subfields, states, type_defs, and units config files. Raises: ValueError: if a file is found in a bad folder. NotImplementedError: if an unrecognized ComponentType is found. """ fields = [] subfields = [] states = [] type_defs = [] units = [] connections = [] for path_tuple in path_tuples: _, component = base_lib.GetTreeLocation(path_tuple.relative_path) if component == base_lib.ComponentType.FIELD: fields.append(path_tuple) elif component == base_lib.ComponentType.SUBFIELD: subfields.append(path_tuple) elif component == base_lib.ComponentType.MULTI_STATE: states.append(path_tuple) elif component == base_lib.ComponentType.ENTITY_TYPE: type_defs.append(path_tuple) elif component == base_lib.ComponentType.UNIT: units.append(path_tuple) elif component == base_lib.ComponentType.CONNECTION: connections.append(path_tuple) else: raise NotImplementedError('Unimplemented component: ' + component) return Config( fields=tuple(fields), subfields=tuple(subfields), states=tuple(states), type_defs=tuple(type_defs), units=tuple(units), connections=tuple(connections))
def _OrganizeConfigFilesByFolder(config_tuples): """Returns dict mapping folder paths to a list of config files. Keys are relative paths from ontology root to the config folders. Values are lists of base_lib.PathParts referencing config files. Args: config_tuples: A list of PathParts tuples for config files. """ files_by_folder = collections.defaultdict(list) for file_tuple in config_tuples: folderpath, _ = base_lib.GetTreeLocation(file_tuple.relative_path) files_by_folder[folderpath].append(file_tuple) return files_by_folder
def testGetTreeLocationWithBadParentDirs(self): testpath = 'bad/HVAC/fields' with self.assertRaises(ValueError): _, _ = base_lib.GetTreeLocation(testpath)