0

I have tool, which intended to generate and display configurations (sections).

INI-file looks like:

...

[test]
MATH_DLL = 035f210e-6c06-4021-8857-0759409c4bb7
MATH_DLL_XML = 805fe0f0-2627-4ced-bac5-8725ef9839ef
ASSETMANAGER_API_DLL = 426d1824-628f-47ed-8539-4a0ed292b94e
...

What I want now - add option to remove section to "main" file:

...
parser_unity.add_argument('-x', '--remove', action='store', dest='unity_uuid_remove',
                          help='Remove configuration from uuids.ini. Must be called with config_name to delete')
...

And:

def handler_unity(action_list):
...
    if action_list.unity_uuid_remove:
        from lib.unity.uuidgen import UuuidManagment
        u = UuuidManagment(logger, RDS_BASEDIR)
        u.config_remove(action_list.unity_uuid_remove)

And class and method:

class UuuidManagment(object):

    """Class for data in uuids.ini file management"""

    def __init__(self, logger, rds_basedir):

        self.config = ConfigParser.ConfigParser()
        self.config_file = os.path.join(rds_basedir, 'conf', 'unity', 'uuids.ini')
        self.config.read(self.config_file)
        self.configs = self.config.sections()

        self.logger = logger
        self.rds_basedir = rds_basedir

    ...

    def config_remove(self, config_name):

        """Remove config_name specified in argument from uuids.ini"""

        self.logger.logger.info('Remove %s' % config_name)

        print(self.config, self.configs, config_name)

        self.config.remove_section(config_name)

        with open(self.config_file, 'r+') as out:
            self.config.write(out)

But it's don't want to work.

Current configs:

d:\RDS\rdsmanager>RDSmanager.py unity -l                                                                                              
RDSmanager started at 09, Jul 2015 at 17:05:35                                                                                        
Running configlist                                                                                                                    

Currently avalable configurations:                                                                                                    

develop                                                                                                                               
debug                                                                                                                                 
qa                                                                                                                                    
version_1_0_2_staging                                                                                                                 
test                                    

Remove it:

d:\RDS\rdsmanager>RDSmanager.py unity -x test                                                                                         
RDSmanager started at 09, Jul 2015 at 17:05:40                                                                                        
Remove test                                                                                                                           
(<lib.external.ConfigParser.ConfigParser instance at 0x02346580>, ['develop', 'debug', 'qa', 'version_1_0_2_staging', 'test'], 'test')

Check again:

d:\RDS\rdsmanager>RDSmanager.py unity -l                                                                                              
RDSmanager started at 09, Jul 2015 at 17:05:42                                                                                        
Running configlist                                                                                                                    

Currently avalable configurations:                                                                                                    

develop                                                                                                                               
debug                                                                                                                                 
qa                                                                                                                                    
version_1_0_2_staging                                                                                                                 
test                                         
setevoy
  • 4,374
  • 11
  • 50
  • 87

1 Answers1

1

In config_remove you open the file in r+ mode.

You should open it in write/truncate mode w to properly rewrite the file, as you can see in the doc.

'w' for writing (truncating the file if it already exists)

For more information, this question: Confused by python file mode “w+” is related to the actual purpose of the + variants.

Community
  • 1
  • 1
Gall
  • 1,595
  • 1
  • 14
  • 22
  • Thanks. I changed to: `with open(self.config_file, 'w+') as out` - and now it's work. Stupid issue :-) – setevoy Jul 09 '15 at 14:37
  • 1
    @setevoy I added a link to a question related to the ``+`` variants for more information on what is more appropriate to use. – Gall Jul 09 '15 at 14:42