Commits

Anonymous committed b5318dc

- add a way to call concatenate() with the configuration directly (as a dictionary), and not be required to have a configuration text file

Comments (0)

Files changed (2)

concatenate_files.py

 '''
 
 
-def concatenate( pathToConfig, resultingFileName="result.txt", basePath="" ):
+def concatenate( config, resultingFileName="result.txt", basePath="" ):
 
     '''
 
 
     Arguments:
 
-        pathToConfig      (string) : path to the configuration file
+        config (string or dictionary) : 
+            when is a string     -> path to the configuration file
+            when is a dictionary -> its the actual configuration, following the same pattern as the content of the file
+        
         resultingFileName (string) : name of the file which will have the result (can be a relative path too)
         basePath          (string) : the paths in the configuration file are relative to this
 
         basePath = os.path.dirname( os.path.abspath(__file__) )
 
 
-        # read the configuration file
-    try:
-        configFile = open( pathToConfig, "r", encoding="utf-8" )
+        # means the configuration is in a file, we have to open it, and parse it
+    if isinstance( config, str ):
+        
+        
+            # read the configuration file
+        try:
+            configFile = open( config, "r", encoding="utf-8" )
+    
+        except IOError:
+    
+            raise IOError( "Couldn't open the configuration file: " + config )
+    
+    
+            # convert to an object from the json representation
+        try:
+            configContent = json.loads( configFile.read() )
+    
+        except ValueError:
+            raise CouldntConvertFromJsonError( "Invalid configuration (couldn't convert from the json representation): " + config )
+    
+    
+        configFile.close()
 
-    except IOError:
-
-        raise IOError( "Couldn't open the configuration file: " + pathToConfig )
-
-
-        # convert to an object from the json representation
-    try:
-        configContent = json.loads( configFile.read() )
-
-    except ValueError:
-        raise CouldntConvertFromJsonError( "Invalid configuration (couldn't convert from the json representation): " + pathToConfig )
-
-
-    configFile.close()
+        # the configuration was sent as a dictionary
+    else:
+        configContent = config
 
 
         # check if we have a dictionary in the configuration

unit_test/test.py

 
         concatenate( os.path.join( TEST_DATA_PATH, "config.txt" ), "result.txt", TEST_DATA_PATH )
 
-        with open( "result.txt", 'r' ) as f:
+        with open( "result.txt", 'r', encoding="utf-8" ) as f:
             result = f.read()
 
         self.assertEqual( compareText, result )
 
         concatenate( os.path.join( TEST_DATA_PATH, "config2.txt" ), "result.txt", TEST_DATA_PATH )
 
-        with open( "result.txt", 'r' ) as f:
+        with open( "result.txt", 'r', encoding="utf-8" ) as f:
             result = f.read()
 
 
         self.assertEqual( text1 + text2 + text3, result )
 
 
+
+    def test_dictionaryArgument( self ):
+        
+        '''
+            Tests calling concatenate() directly with a dictionary (in the right format), and compare with the same configuration, but
+                reading it from a file ( "config.txt" )
+        '''
+
+        concatenate( os.path.join( TEST_DATA_PATH, "config.txt" ), "result.txt", TEST_DATA_PATH )
+
+        with open( "result.txt", 'r', encoding="utf-8" ) as f:
+            result1 = f.read()
+        
+        
+        config = {
+                "":
+                    [
+                    "aa.txt"
+                    ],
+            
+                "test":
+                    [
+                    "bb.txt",
+                    "doesntExist.txt",
+                    "cc.txt"
+                    ],
+            
+                "test/test2":
+                    [
+                    ]
+                  }
+
+        concatenate( config, "result.txt", TEST_DATA_PATH )
+        
+        with open( "result.txt", 'r', encoding="utf-8" ) as f:
+            result2 = f.read()
+            
+            
+        self.assertEqual( result1, result2 )
+    
+
 if __name__ == '__main__':
 
     unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.