Пример #1
0
    def test_getPrintableContents(self):
        '''Checks that the TuflowFile object returns it's contents in a format
        appropriate for writing to a tuflow model file.
        '''
        sf = tfp.TuflowFile(1, self.path_with_comment, self.hex_hash, 3,
                            self.real_command, self.fake_root)
        self.assertEqual(
            'Read MI Z Line RIDGE == mi\someplace_2d_zln_bridge_v1.0.mif ! Some comment',
            sf.getPrintableContents(), 'printable contents do not match')

        sf = None
        sf = tfp.TuflowFile(1, self.fake_abs_path, self.hex_hash, 3,
                            self.real_command)
        p = sf.getPrintableContents()
        #print 'printing p: \n' + p
        self.assertEqual(
            'Read MI Z Line RIDGE == c:\\first\\second\\third\\fourth\\TuflowFile.mid',
            sf.getPrintableContents(),
            'printable contents for absolute path do not match')

        mv = tfp.ModelVariables(1, self.real_variables, self.hex_hash, 3,
                                self.real_command_var)
        self.assertEqual(
            'Map Output Data Types == h v q d MB1 ZUK0 ! Output: Levels, Velocities, Unit Flows, Depths, Mass Error & Hazard',
            mv.getPrintableContents(), 'printable contents do not match')
Пример #2
0
    def test_getRelativePath(self):
        '''Check that we can retrieve the relative path without problems
        '''
        # Check that the relative path is set and returned correctly
        sf = None
        sf = tfp.TuflowFile(1, self.real_relative_path, self.hex_hash, 3,
                            self.real_command, self.fake_root)
        self.assertEqual(self.real_relative_path, sf.getRelativePath(),
                         'relative path does not match')

        # Check that setting an absolute path sets the relative path to None
        sf = None
        sf = tfp.TuflowFile(1, self.fake_abs_path, self.hex_hash, 3,
                            self.real_command)
        self.assertFalse(sf.getRelativePath(), 'relative path is not False')
Пример #3
0
 def test_getNameAndExtension(self):
     '''Check that we can get the file name with the extension without problems
     '''
     sf = None
     sf = tfp.TuflowFile(1, self.fake_abs_path, self.hex_hash, 3,
                         self.real_command)
     self.assertEqual('TuflowFile.mid', sf.getFileNameAndExtension(),
                      'name and extension do not match')
Пример #4
0
 def test_instance(self):
     sf = tfp.TuflowFile(1, 'c:\\madeuppath\\file.mif', self.hex_hash, 3,
                         'Read GIS')
     self.assertIsInstance(sf, tfp.TuflowFilePart,
                           'TuflowFile is not TuflowFilePartInstance')
     mv = tfp.ModelVariables(1, self.real_variables, self.hex_hash, 4,
                             self.real_command_var)
     self.assertIsInstance(mv, tfp.TuflowFilePart,
                           'ModelVariables is not TuflowFilePartInstance')
Пример #5
0
    def test_setFileName(self):
        '''Check that the fileName is set properly.
        '''
        sf = tfp.TuflowFile(1, self.fake_abs_path, self.hex_hash, 3,
                            self.real_command)
        sf.setFileName('newfile')
        self.assertEqual('newfile.mid', sf.getFileNameAndExtension(),
                         'fileName with no extension not set properly')

        sf = tfp.TuflowFile(1, self.fake_abs_path, self.hex_hash, 3,
                            self.real_command)
        sf.setFileName('newfile1.shp', True)
        self.assertEqual('newfile1.mid', sf.getFileNameAndExtension(),
                         'fileName and removed extension not set properly')

        sf.setFileName('newfile2.shp', True, True)
        self.assertEqual('newfile2.shp', sf.getFileNameAndExtension(),
                         'fileName and extension not set properly')
Пример #6
0
    def test_getAbsolutePath(self):
        '''Check that we can retrive the absolute path without problems
        '''
        # Check the absolute path is set and returned correctly
        sf = None
        sf = tfp.TuflowFile(1, self.path_with_comment, self.hex_hash, 3,
                            self.real_command, self.fake_root,
                            self.fake_parent_relative_root)
        self.assertEqual(
            os.path.join(self.fake_root,
                         '..\model\mi\someplace_2d_zln_bridge_v1.0.mif'),
            sf.getAbsolutePath(), 'abspath does not match')

        # Check that the root and relative root are set and combined correctly
        sf = None
        sf = tfp.TuflowFile(1, self.real_relative_path, self.hex_hash, 3,
                            self.real_command, self.fake_root,
                            self.fake_parent_relative_root)
        self.assertEqual(
            os.path.join(self.fake_root, self.fake_parent_relative_root,
                         self.real_relative_path), sf.getAbsolutePath(),
            'abspath is not root + relative root')
Пример #7
0
 def generateModelTuflowFile(self, line_type, global_order):
     """Creates a TuflowFile object from the current state.
     
     Generates the class the self params.
     
     Args:
         line_type(int): one of the TuflowLoader.types value e.g. MODEL
     
     Returns:
         TuflowFile object.
     """
     return tfp.TuflowFile(global_order, self.filename, self.head_hash,
                           line_type, self.extension, self.root,
                           self.relative_root, self.extension)
Пример #8
0
    def test_object_setup(self):
        '''Check that the constructor loads all of the parameters in properly.
        '''
        sf = tfp.TuflowFile(1, self.path_with_comment, self.hex_hash, 3,
                            self.real_command, self.fake_root)

        # Check that it works to set up with a comment and relative path
        self.assertEqual(sf.comment, 'Some comment', 'Comment does not match')
        self.assertEqual(sf.relative_root, 'mi',
                         'relative path does not match')
        self.assertEqual(sf.command, 'Read MI Z Line RIDGE',
                         'Comment not set properly')
        self.assertEqual(sf.file_name, 'someplace_2d_zln_bridge_v1.0',
                         'fileName does not match')
        self.assertEqual(sf.extension, 'mif', 'extension does not match')
        self.assertEqual(sf.getRelativePath(),
                         'mi\someplace_2d_zln_bridge_v1.0.mif',
                         'relative path does not match')
        self.assertEqual(sf.getFileNameAndExtension(),
                         'someplace_2d_zln_bridge_v1.0.mif',
                         'name and extension does not match')

        # Check that an absolute path works
        sf = None
        sf = tfp.TuflowFile(1, self.fake_abs_path, self.hex_hash, 2,
                            self.real_command)
        self.assertEqual(sf.relative_root, None,
                         'relative root should be None')
        self.assertEqual(sf.comment, None, 'Comment should be None')
        self.assertEqual(sf.command, 'Read MI Z Line RIDGE',
                         'command was not set properly')
        self.assertEqual(sf.file_name, 'TuflowFile', 'fileName does not match')
        self.assertEqual(sf.extension, 'mid', 'extension does not match')
        self.assertEqual(sf.getFileNameAndExtension(), 'TuflowFile.mid',
                         'absolute file name does not match')
        self.assertFalse(sf.getRelativePath(), 'relative path is not None')
Пример #9
0
    def test_setPathsWithAbsolutePaths(self):
        '''Check that the path variables can be set properly with a new absolute path. 
        '''
        # Check path setting works without keeping relative path
        sf = None
        path = 'c:\\some\\random\\path\\with\\file.shp'
        sf = tfp.TuflowFile(1, self.real_relative_path, self.hex_hash, 3,
                            self.real_command)
        sf.setPathsWithAbsolutePath(path, False)
        self.assertEqual(path, sf.getAbsolutePath(),
                         'absolute paths do not match')
        self.assertFalse(sf.getRelativePath(), 'relative path does not match')

        # and with keeping the relative path
        sf = None
        sf = tfp.TuflowFile(1, self.real_relative_path, self.hex_hash, 3,
                            self.real_command, self.fake_root,
                            self.fake_parent_relative_root)
        sf.setPathsWithAbsolutePath(path, True)
        self.assertEqual(
            os.path.join('c:\\some\\random\\path\\with',
                         self.fake_parent_relative_root, '..\\testinputdata',
                         'file.shp'), sf.getAbsolutePath(),
            'absolute paths do not match')
Пример #10
0
    def _getLineDetails(self, line, file_d):
        """Constructs the appropriate object from the file line.
        
        Takes a line from the file and decides what to do with it...and does
        it. This includes:
        # Checking if it's a comment or has any contents.
        # seperating the components if it's an instruction.
        # Deciding on what type of object to create (if any)
        # Creating the appropriate object and returning it. 
        
        This function is a bit messy, but it's hard to break up. It's 
        tempting to add a lot of it to :class:'TuflowFilePart' and let it
        deal with it in a nice polymorphic manner.
        
        Args:
            line(str): the file line to be dealt with.
            file_d(_FileDetails): object containing the current file data.
        
        Returns:
            Tuple - (line_val, hex_hash, command_type, ext):
                # the object created from the line.
                # the hexidecimal hash value created for the object.
                # the category that the object has been put in.
                # the extension of the file or '' if none found.
        """
        hex_hash = file_d.generateHash(file_d.filename + line +
                                       str(file_d.parent_hash))
        line = line.lstrip()
        line_val = '\n'
        ext = ''

        # It's a comment or blank line
        if line.startswith('#') or line.startswith('!'):
            line_val = line
            command_type = ft.COMMENT  #self.types.COMMENT

        elif line.strip() == '':
            command_type = ft.COMMENT  #self.types.COMMENT

        elif '==' in line or 'AUTO' in line.upper():

            # Estry AUTO
            if 'AUTO' in line.upper():
                line = self._breakAuto(line, file_d.filename)
                self.tuflow_model.has_estry_auto = True

            command, instruction = self._breakLine(line)
            found, command_type = self.types.find(command.upper())

            # TODO: Not sure about this at the moment. I think these should be
            #       going into UNKNOWN FILE, if a file, so we can still update
            #       them when needed. Even if we don't know what they do?
            if not found:
                command_type = ft.UNKNOWN  #self.types.UNKNOWN
                line_val = line
            else:
                if command_type == ft.VARIABLE:  #self.types.VARIABLE:
                    line_val = tfp.ModelVariables(self._global_order,
                                                  instruction, hex_hash,
                                                  command_type, command)
                    self._global_order += 1
                else:
                    # Do a check for MI Projection and SHP projection
                    isfile = True
                    if command.upper() == 'MI PROJECTION' or command.upper(
                    ) == 'SHP PROJECTION':
                        isfile = self._checkProjectionIsFile(instruction)

                    if not isfile:
                        line_val = line
                        command_type = ft.COMMENT  #self.types.COMMENT
                    else:
                        ext = self.extractExtension(instruction)
                        f_type = self.file_types.get(ext)

                        # It's a model file or a results file
                        # TODO: Not safe yet...needs more work.
                        if f_type == None:

                            # If it's a .trd file give it the extension of the
                            # model file type that it's in (.tcf/.tgc/etc)
                            if ext is 'trd':
                                ext = file_d.extension
                            line_val = tfp.TuflowFile(self._global_order,
                                                      instruction, hex_hash,
                                                      command_type, command,
                                                      file_d.root,
                                                      file_d.relative_root,
                                                      ext)
                            self._global_order += 1

                            if command_type is ft.RESULT:  #self.types.RESULT:
                                line_val = self._resolveResult(line_val)

                        # It's one of the files in self.types
                        else:
                            piped_files, pipe_hashes = self.checkForPipes(
                                instruction, file_d, hex_hash)
                            multi_lines = []
                            for i, p in enumerate(piped_files):
                                hex_hash = pipe_hashes[i]

                                # If there's a piped file command we need to register
                                # associated files with each other
                                hex_hash = pipe_hashes[i]
                                child_hash = None
                                parent_hash = None
                                if len(piped_files) > i + 1:
                                    child_hash = pipe_hashes[i + 1]
                                if i > 0:
                                    parent_hash = pipe_hashes[i - 1]

                                line_val = self.file_types[ext](
                                    self._global_order,
                                    p,
                                    hex_hash,
                                    command_type,
                                    command,
                                    file_d.root,
                                    file_d.relative_root,
                                    parent_hash=parent_hash,
                                    child_hash=child_hash)
                                multi_lines.append(
                                    [line_val, hex_hash, command_type, ext])

#                                 self.tuflow_model.file_parts[hex_hash] = ModelFileEntry(
#                                                                 line_val, command_type,
#                                                                 hex_hash)

# TODO: currently the same _global_order for all the
#       files in a piped command. Should they be
#       different?
                            self._global_order += 1
                            return multi_lines

#                         self.tuflow_model.file_parts[hex_hash] = ModelFileEntry(
#                                                                 line_val, command_type,
#                                                                 hex_hash)
        else:
            command_type = ft.UNKNOWN  #self.types.UNKNOWN
            line_val = line

        # Needs to be return in a list because of the multi_lines setup above.
        return [[line_val, hex_hash, command_type, ext]]