CRUD ODL python RESTconf tests
[openflowplugin.git] / test-scripts / tools / file_loader_tool.py
1 '''
2 Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3
4 This program and the accompanying materials are made available under the
5 terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 and is available at http://www.eclipse.org/legal/epl-v10.html
7
8 Created on Jan 24, 2014
9
10 @author: vdemcak
11 '''
12
13 import logging
14
15
16 class FileLoaderTools():
17     """
18     Tool class provides the static methods for load files 
19     with expected format for testing in ODL_TESTs
20     """
21     log = logging.getLogger( 'FileLoaderTools' )
22
23     @staticmethod
24     def load_file_to_string( path_to_file ):
25         """
26         Primary use for loading the xml files as a string
27         """
28         output_string = None
29
30         try:
31             with open( path_to_file ) as f:
32                 output_string = f.read()
33         except IOError, e:
34             FileLoaderTools.log.error( 'cannot find {}: {}'.format( path_to_file, e.strerror ), exc_info = True )
35
36         return output_string
37
38     @staticmethod
39     def load_file_to_dict( path_to_file ):
40         """
41         Primary use for loading the csv files as dictionaries
42         """
43         dictionary = None
44
45         try :
46             with open( path_to_file ) as f:
47                 dictionary = dict( line.strip().split( ';' ) for line in f
48                             if not line.startswith( '#' ) )
49         except IOError, e:
50             FileLoaderTools.log.error( 'cannot find {}: {}'.format( path_to_file, e.strerror ), exc_info = True )
51         return dictionary