| 1 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | Created on Fri Sep  5 12:26:40 2014 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | :module-author: steffen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | :filename: config.py | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | This module provides a highlevel layer for reading and writing config files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | There must be a file called "config.ini" in the root-folder of the project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | The file has to be of the following structure to be imported correctly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # this is a comment \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # the filestructure is like: \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | [netCDF] \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | RootFolder = c://netCDF \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | FilePrefix = cd2_ \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | [mySQL] \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | host = localhost \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | user = guest \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | password = root \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | database = znes \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | [SectionName] \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | OptionName = value \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | Option2 = value2 \n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | import configparser as cp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | FILENAME = 'config.ini' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | FILE = os.path.join(os.path.expanduser("~"), '.oemof', FILENAME) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | cfg = cp.RawConfigParser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | _loaded = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | def load_config(filename): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     Load data from config file to `cfg` that can be accessed by get, set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     afterwards. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     Specify absolute or relative path to your config file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     :param filename: Relative or absolute path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     :type filename: str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     if filename is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         filename = '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     abs_filename = os.path.join(os.getcwd(), filename) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     global FILE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     # find the config file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     if os.path.isfile(filename): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         FILE = filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     elif os.path.isfile(abs_filename): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         FILE = abs_filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     elif os.path.isfile(FILE): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if os.path.dirname(filename): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             file_not_found = filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             file_not_found = abs_filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         file_not_found_message(file_not_found) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     # load config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     init(FILE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | def file_not_found_message(file_not_found): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     Show error message incl. help if file not found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     :param filename: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     :type filename: str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     logging.error( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         Config file {file} cannot be found.  Make sure this file exists! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         An exemplary section in the config file looks as follows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         [database] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         username = username under which to connect to the database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         database = name of the database from which to read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         host     = host to connect to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         port     = port to connect to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         For further advice, see in the docs (https://oemofdb.readthedocs.io) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         how to format the config. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         """.format( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             file=file_not_found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | def main(): | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | def init(FILE): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     Read config file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     :param FILE: Absolute path to config file (incl. filename) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     :type FILE: str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         cfg.read(FILE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         global _loaded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         _loaded = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     except Exception: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         file_not_found_message(FILE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | def get(section, key): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     returns the value of a given key of a given section of the main | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     config file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     :param section: the section. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     :type section: str. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     :param key: the key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     :type key: str. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     :returns: the value which will be casted to float, int or boolean. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     if no cast is successfull, the raw string will be returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     # FILE = 'config_misc' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     if not _loaded: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         init(FILE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return cfg.getfloat(section, key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     except Exception: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             return cfg.getint(section, key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         except Exception: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 return cfg.getboolean(section, key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             except Exception: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 return cfg.get(section, key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | def set(section, key, value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     sets a value to a [section] key - pair. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     if the section doesn't exist yet, it will be created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     :param section: the section. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     :type section: str. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     :param key: the key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     :type key: str. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     :param value: the value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     :type value: float, int, str. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     if not _loaded: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     if not cfg.has_section(section): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         cfg.add_section(section) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     cfg.set(section, key, value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     with open(FILE, 'w') as configfile: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         cfg.write(configfile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | if __name__ == "__main__": | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 182 |  |  |     main() | 
            
                                                        
            
                                    
            
            
                | 183 |  |  |  |