def absolutePathAllTypes(self, user_vars=None): """Get the absolute paths for all_types. If the file has other file types in all_types (e.g. .shp, shx, .dbf) this will return the absolute paths for all of them. Args: user_vars(dict): a dict containing variable placeholder values as keys and the actual values as values. See TuflowPart.resolvePlaceholder() method for more information. """ rel_roots = self.getRelativeRoots([]) paths = [] if self.all_types: all_types = self.all_types else: all_types = [self.extension] for a in all_types: f = self.filename + '.' + a if self.has_own_root: fpath = PathHolder.absolutePath(self, filename=f) else: fpath = PathHolder.absolutePath(self, filename=f, relative_roots=rel_roots) # Replace any variable placeholders if given if user_vars: fpath = TuflowPart.resolvePlaceholder(fpath, user_vars) paths.append(fpath) return paths
def getRelativePath(self, all_types=False): """Returns the full relative path for this object. Most paths stated in tuflow model files are done with relative paths. If this is the case with the paths given to the constructor or set later this will return it, otherwise it will return False. Args: all_types=False (Bool): if set to True the relative path of this object and any files associated with this file name will be returned. Returns: Relative path of this object or a list containing the relative paths of all files associated with this file name TODO: Is it a bit of a burden sending back either a single relative path OR a list if there's multiple files? I think this should be changed so that it always returns a list. that would be a lot easier for the calling code to deal with. """ if all_types and not self.all_types is None: all_names = [] for n in self.all_types: all_names.append( PathHolder.getRelativePath(self, False) + '.' + n) return all_names elif all_types and self.all_types is None: return [PathHolder.getRelativePath(self)] else: return PathHolder.getRelativePath(self)
def test_finalFolder(self): '''Check that it does actually return the final folder in the stack ''' ph = PathHolder(self.fake_abs_path) self.assertEqual('fourth', ph.finalFolder(), 'final folder is not correct') ph = PathHolder(self.fake_relative_path) self.assertEqual('relative', ph.finalFolder(), 'final folder is not correct')
def filenameAndExtension(self, user_vars=None): if user_vars: name = self.resolvePlaceholder(self.filename, user_vars) if self.extension: name += '.' + self.extension return name else: return PathHolder.filenameAndExtension(self)
def test_absolutePath(self): '''Test that the absolute path is returned correctly. ''' ph = PathHolder(self.fake_abs_path) abs_path = ph.absolutePath() self.assertEqual(self.fake_abs_path, abs_path, 'absolutePath() fail') ph = PathHolder(self.fake_relative_path) self.assertFalse( ph.absolutePath(), 'absolutePath() with relative root does not return False') ph = PathHolder(self.fake_relative_path, self.fake_root) q = ph.absolutePath() self.assertEqual('c:\\some\\fake\\root\\TuflowFile.txt', ph.absolutePath(), 'absolutePath() with root and relative root fail')
def test_absolutePath(self): '''Test that the absolute path is returned correctly. ''' ph = PathHolder(self.fake_abs_path) abs_path = ph.absolutePath() self.assertEqual(self.fake_abs_path, abs_path, 'absolutePath() fail') ph = PathHolder(self.fake_relative_path) self.assertFalse( ph.absolutePath(), 'absolutePath() with relative root does not return False') ph = PathHolder(self.fake_relative_path, self.fake_root) q = ph.absolutePath() pth = os.path.join(self.prefix, 'some', 'fake', 'root', 'TuflowFile.txt') self.assertEqual(pth, ph.absolutePath(), 'absolutePath() with root and relative root fail')
def test_setFinalFolder(self): '''Check that the new final folder is set properly. ''' ph = PathHolder(self.fake_abs_path) ph.setFinalFolder('fifth') self.assertEqual('c:\\first\\second\\third\\fifth', ph.root, 'set absolute final folder fail') ph = PathHolder(self.fake_relative_path) ph.setFinalFolder('fifth') self.assertEqual('..\\fifth', ph.relative_root, 'set relative final folder fail')
def test_setFinalFolder(self): '''Check that the new final folder is set properly. ''' ph = PathHolder(self.fake_abs_path) ph.setFinalFolder('fifth') pth = os.path.join(self.prefix, 'first', 'second', 'third', 'fifth') self.assertEqual(pth, ph.root, 'set absolute final folder fail') ph = PathHolder(self.fake_relative_path) ph.setFinalFolder('fifth') self.assertEqual(os.path.join('..', 'fifth'), ph.relative_root, 'set relative final folder fail')
def absolutePath(self, user_vars=None): """Get the absolute path of this object. Args: user_vars(dict): a dict containing variable placeholder values as keys and the actual values as values. See TuflowPart.resolvePlaceholder() method for more information. """ if self.has_own_root: abs_path = PathHolder.absolutePath(self) else: rel_roots = self.getRelativeRoots([]) abs_path = PathHolder.absolutePath(self, relative_roots=rel_roots) # Replace any variable placeholders if given if user_vars: abs_path = TuflowPart.resolvePlaceholder(abs_path, user_vars) return abs_path
def test_setupVars(self): '''Test the _setupVars method. This protected method is called by the constructor when a new file path is handed to the newly created object. It should then set everything up for later access. ''' # Check that it loads an absolute path properly ph = PathHolder(self.fake_abs_path) self.assertEqual('c:\\first\\second\\third\\fourth', ph.root, 'roots do not match') self.assertEqual('TuflowFile', ph.filename, 'filenames do not match') self.assertEqual('txt', ph.extension, 'extensions do not match') self.assertIsNone(ph.relative_root, 'relative root should be None') # Check that it loads a relative path properly ph = PathHolder(self.fake_relative_path) self.assertEqual('..\\relative', ph.relative_root, 'relative roots do not match') self.assertEqual('TuflowFile', ph.filename, 'filenames do not match') self.assertEqual('txt', ph.extension, 'extensions do not match') self.assertIsNone(ph.root, 'root should be None')
def test_absolutePath(self): '''Test that the absolute path is returned correctly. ''' ph = PathHolder(self.fake_abs_path) abs_path = ph.absolutePath() self.assertEqual(self.fake_abs_path, abs_path, 'absolutePath() fail') ph = PathHolder(self.fake_relative_path) self.assertFalse(ph.absolutePath(), 'absolutePath() with relative root does not return False') ph = PathHolder(self.fake_relative_path, self.fake_root) q = ph.absolutePath() self.assertEqual('c:\\some\\fake\\root\\TuflowFile.txt', ph.absolutePath(), 'absolutePath() with root and relative root fail')
def getAbsolutePath(self, all_types=False): """Get the absolute path of the file path stored in this object. Note: If there is no root variables set it will return False because there is no way of knowing what the absolute path will be. Args: all_types=False (Bool): if set to True the absolute path of this object and any files associated with this file name will be returned. Returns: Absolute path of this object or a list containing the absolute paths of all files associated with this file name. """ if all_types and not self.all_types is None: all_names = self.getFileNameAndExtensionAllTypes() for i, n in enumerate(all_names): all_names[i] = PathHolder.getAbsolutePath(self, n) return all_names elif all_types and self.all_types is None: return [PathHolder.getAbsolutePath(self)] else: return PathHolder.getAbsolutePath(self)
def __init__(self, parent, obj_type='file', **kwargs): """Constructor. **kwargs: - 'path': 'relative\path\to\file.ext' - 'command': 'command string' - 'comment': 'comment at the end of the command' Args: hash(str): unique code for this object. parent(str): unique hash code for this object. Raises: keyError: if kwargs 'root', 'command', 'path' are not given. """ root = kwargs['root'] # raises keyerror self.command = kwargs['command'] path = kwargs['path'] self.comment = kwargs.get('comment', '') TuflowPart.__init__(self, parent, obj_type, **kwargs) PathHolder.__init__(self, path, root) self.TOP_CLASS = 'file' self.all_types = None self.has_own_root = False
def __init__(self, global_order, path, hex_hash, type, command, root=None, parent_relative_root='', category=None, parent_hash=None, child_hash=None): """Constructor. Most tuflow files are referenced relative to the file that they are called from. This method eakes a 'root' as an optional argument. The root will allow for an absolute path to be created from the relative path. This means that the file stored in this object will be able to store it's absolute path as well the path relative to the callng file. E.g. ..\bc_dbase\bc.csv c:\actual\path\runs\..\bc_dbase\bc.csv Args: global_order(int): order that the file object was read in. path (str): the path to the file that this object holds the meta data for. hex_hash(str): hexidecimal code for this object. type(int): the TuflowTypes value for this object. command (str): the command that was used to call this path in the model file. root=None (str): the path to the directory that is the root of this file. parent_relative_root=''(str): the path section relative to the main file read in (the tcf or ecf). category=None(str): Any category specification for the file. parent=None(str): the hash code of any parent that this file may have. This will be non-None if the file is read in after a pipe command (e.g. with: file_1.mif | file_2.mif | file_3.mif if this file is file_3 then the parent will be the hash code for file_2. child=None(str): the hash code of any child that this file may have. This will be non-None if the file is read in after a pipe command (e.g. with: file_1.mif | file_2.mif | file_3.mif if this file is file_1 then the child will be the hash code for file_2. See Also: :class:'TuflowTypes' :class:'TuflowFilePart' """ if not isinstance(path, basestring): raise TypeError TuflowFilePart.__init__(self, global_order, hex_hash, type) self.command = command self.category = category self.comment = None self.all_types = None self.parent_hash = parent_hash self.child_hash = child_hash # If it's an absolute path then we will set the root to be that rather # than the normal root used for absolute paths so this will be True. self.has_own_root = False self.file_name_is_prefix = False # Tuflow commands are allowed comments after them denoted by '!' so we # split on this to make sure it's removed from the path name. if '!' in path: path, self.comment = path.split('!', 1) self.comment = self.comment.strip() elif '#' in path: path, self.comment = path.split('#', 1) self.comment = self.comment.strip() # Call superclass constructor PathHolder.__init__(self, path, root) self.parent_relative_root = parent_relative_root
def setUp(self): '''Set up stuff that will be used throughout the class. ''' self.path_holder = PathHolder('c:\\fake\\path\\to\\file.dat') # Create a couple of unit to use in the methods. self.unit_contents1 = \ ['RIVER (Culvert Exit) CH:7932 - Trimmed to BT\n', 'SECTION\n', '1.067\n', ' 15.078 1.111111 1000\n', ' 18\n', ' 5.996 37.560 0.080 1.000LEFT 291391.67 86582.61LEFT 16 \n', ' 6.936 37.197 0.035* 1.000 291391.43 86581.70 \n', ' 7.446 36.726 0.035 1.000 291391.30 86581.21 \n', ' 7.635 35.235 0.035 1.000 291391.25 86581.03 \n', ' 8.561 35.196 0.035 1.000 291391.01 86580.13 \n', ' 9.551 35.190 0.035 1.000BED 291390.75 86579.18 \n', ' 10.323 35.229 0.035 1.000 291390.55 86578.43 \n', ' 10.904 35.319 0.035 1.000 291390.40 86577.87 \n', ' 12.542 35.637 0.035 1.000 291389.98 86576.29 \n', ' 13.740 35.593 0.035 1.000 291389.67 86575.13 \n', ' 13.788 35.592 0.035 1.000 291389.66 86575.09 \n', ' 13.944 36.148 0.035 1.000 291389.62 86574.93 \n', ' 15.008 36.559 0.080* 1.000 291389.34 86573.91 \n', ' 16.355 37.542 0.080 1.000 291389.00 86572.60 \n', ' 17.424 38.518 0.080 1.000 291388.72 86571.57 \n', ' 18.449 39.037 0.080 1.000 291388.46 86570.58 \n', ' 19.416 39.146 0.080 1.000 291388.21 86569.65 \n', ' 19.420 39.133 0.080 1.000RIGHT 291388.21 86569.65RIGHT 4095 \n'] self.unit_contents2 = \ ['RIVER (Culvert Exit) CH:7932 - Trimmed to BT\n', 'SECTION\n', '1.068\n', ' 15.078 1.111111 1000\n', ' 18\n', ' 5.996 37.560 0.080 1.000LEFT 291391.67 86582.61LEFT 16 \n', ' 6.936 37.197 0.035* 1.000 291391.43 86581.70 \n', ' 7.446 36.726 0.035 1.000 291391.30 86581.21 \n', ' 7.635 35.235 0.035 1.000 291391.25 86581.03 \n', ' 8.561 35.196 0.035 1.000 291391.01 86580.13 \n', ' 9.551 35.190 0.035 1.000BED 291390.75 86579.18 \n', ' 10.323 35.229 0.035 1.000 291390.55 86578.43 \n', ' 10.904 35.319 0.035 1.000 291390.40 86577.87 \n', ' 12.542 35.637 0.035 1.000 291389.98 86576.29 \n', ' 13.740 35.593 0.035 1.000 291389.67 86575.13 \n', ' 13.788 35.592 0.035 1.000 291389.66 86575.09 \n', ' 13.944 36.148 0.035 1.000 291389.62 86574.93 \n', ' 15.008 36.559 0.080* 1.000 291389.34 86573.91 \n', ' 16.355 37.542 0.080 1.000 291389.00 86572.60 \n', ' 17.424 38.518 0.080 1.000 291388.72 86571.57 \n', ' 18.449 39.037 0.080 1.000 291388.46 86570.58 \n', ' 19.416 39.146 0.080 1.000 291388.21 86569.65 \n', ' 19.420 39.133 0.080 1.000RIGHT 291388.21 86569.65RIGHT 4095 \n'] self.unit_contents3 = \ ['RIVER (Culvert Exit) CH:7932 - Trimmed to BT\n', 'SECTION\n', '1.069\n', ' 15.078 1.111111 1000\n', ' 18\n', ' 5.996 37.560 0.080 1.000LEFT 291391.67 86582.61LEFT 16 \n', ' 6.936 37.197 0.035* 1.000 291391.43 86581.70 \n', ' 7.446 36.726 0.035 1.000 291391.30 86581.21 \n', ' 7.635 35.235 0.035 1.000 291391.25 86581.03 \n', ' 8.561 35.196 0.035 1.000 291391.01 86580.13 \n', ' 9.551 35.190 0.035 1.000BED 291390.75 86579.18 \n', ' 10.323 35.229 0.035 1.000 291390.55 86578.43 \n', ' 10.904 35.319 0.035 1.000 291390.40 86577.87 \n', ' 12.542 35.637 0.035 1.000 291389.98 86576.29 \n', ' 13.740 35.593 0.035 1.000 291389.67 86575.13 \n', ' 13.788 35.592 0.035 1.000 291389.66 86575.09 \n', ' 13.944 36.148 0.035 1.000 291389.62 86574.93 \n', ' 15.008 36.559 0.080* 1.000 291389.34 86573.91 \n', ' 16.355 37.542 0.080 1.000 291389.00 86572.60 \n', ' 17.424 38.518 0.080 1.000 291388.72 86571.57 \n', ' 18.449 39.037 0.080 1.000 291388.46 86570.58 \n', ' 19.416 39.146 0.080 1.000 291388.21 86569.65 \n', ' 19.420 39.133 0.080 1.000RIGHT 291388.21 86569.65RIGHT 4095 \n'] self.unit_contents4 = \ ['Baseline 1% AEP Run', '#REVISION#1', ' 62 0.750 0.900 0.100 0.002 12', ' 10.000 0.020 0.010 0.700 0.101 0.701 0.000', 'RAD FILE', '..\\..\\..\\..\\..\\..\\..\\..\\rgh\\roughness.rad' ] # Use the unit factory to convert the text input into actual river units. factory = iuf() i, self.header = factory.createUnit(self.unit_contents4, 0, 'header', 1) i, self.river1 = factory.createUnit(self.unit_contents1, 0, 'river', 1, 1) i, self.river2 = factory.createUnit(self.unit_contents2, 0, 'river', 2, 1) i, self.river3 = factory.createUnit(self.unit_contents3, 0, 'river', 3, 1)
def setUp(self): '''Set up stuff that will be used throughout the class. ''' self.fake_path = 'c:\\fake\\path\\to\\datfile.dat' self.path_holder = PathHolder(self.fake_path) # Setup some data to create units with self.riv1_head = {'distance': 10} self.riv1_row = { 'main': [ { rdt.CHAINAGE: 0.0, rdt.ELEVATION: 20.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 2.0, rdt.ELEVATION: 10.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 4.0, rdt.ELEVATION: 10.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 6.0, rdt.ELEVATION: 10.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 8.0, rdt.ELEVATION: 20.0, rdt.ROUGHNESS: 0.04 }, ] } self.riv2_head = {'distance': 12} self.riv2_row = { 'main': [ { rdt.CHAINAGE: 0.0, rdt.ELEVATION: 20.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 2.0, rdt.ELEVATION: 10.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 4.0, rdt.ELEVATION: 10.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 6.0, rdt.ELEVATION: 10.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 8.0, rdt.ELEVATION: 20.0, rdt.ROUGHNESS: 0.04 }, ] } self.riv3_head = {'distance': 14} self.riv3_row = { 'main': [ { rdt.CHAINAGE: 0.0, rdt.ELEVATION: 20.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 2.0, rdt.ELEVATION: 10.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 4.0, rdt.ELEVATION: 10.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 6.0, rdt.ELEVATION: 10.0, rdt.ROUGHNESS: 0.04 }, { rdt.CHAINAGE: 8.0, rdt.ELEVATION: 20.0, rdt.ROUGHNESS: 0.04 }, ] } self.brg1_row = { 'main': [ { rdt.CHAINAGE: 0.0, rdt.ELEVATION: 20.0 }, { rdt.CHAINAGE: 2.0, rdt.ELEVATION: 10.0 }, { rdt.CHAINAGE: 4.0, rdt.ELEVATION: 10.0 }, { rdt.CHAINAGE: 6.0, rdt.ELEVATION: 20.0 }, ], 'opening': [{ rdt.OPEN_START: 0.0, rdt.OPEN_END: 2.0 }, { rdt.OPEN_START: 4.0, rdt.OPEN_END: 6.0 }] } # Create a new IsisUnitCollection object self.dat = DatCollection.initialisedDat(self.fake_path) self.riv1 = iuf.FmpUnitFactory.createUnit('river', name='riv1', head_data=self.riv1_head, row_data=self.riv1_row) self.riv2 = iuf.FmpUnitFactory.createUnit('river', name='riv2', head_data=self.riv2_head, row_data=self.riv2_row) self.riv3 = iuf.FmpUnitFactory.createUnit('river', name='riv3', head_data=self.riv3_head, row_data=self.riv3_row) self.brg1 = iuf.FmpUnitFactory.createUnit('arch', name='brg1', name_ds='brg1ds', row_data=self.brg1_row) self.brg2 = iuf.FmpUnitFactory.createUnit('arch', name='brg2', name_ds='brg2ds', row_data=self.brg1_row) self.brg3 = iuf.FmpUnitFactory.createUnit('usbpr', name='brg3', name_ds='brg3ds', row_data=self.brg1_row)