Is there a way to escape newlines using ConfigParser?
There is this option --addons-path
in config where you can provide multiple paths for addons, but paths are long and there are many of paths so writing everything in one line, looks really bad. So I thought to put addons paths in multiple lines, but ConfigParser does not allow it. I get this error:
Starting odoo-server: Traceback (most recent call last):
File "/home/oerp/openerp80/odoo/openerp-server", line 5, in <module>
openerp.cli.main()
File "/home/oerp/openerp80/odoo/openerp/cli/__init__.py", line 68, in main
o.run(args)
File "/home/oerp/openerp80/odoo/openerp/cli/server.py", line 180, in run
main(args)
File "/home/oerp/openerp80/odoo/openerp/cli/server.py", line 140, in main
openerp.tools.config.parse_config(args)
File "/home/oerp/openerp80/odoo/openerp/tools/config.py", line 358, in parse_config
self._parse_config(args)
File "/home/oerp/openerp80/odoo/openerp/tools/config.py", line 403, in _parse_config
self.load()
File "/home/oerp/openerp80/odoo/openerp/tools/config.py", line 605, in load
p.read([self.rcfile])
File "/usr/lib/python2.7/ConfigParser.py", line 305, in read
self._read(fp, filename)
File "/usr/lib/python2.7/ConfigParser.py", line 546, in _read
raise e
ConfigParser.ParsingError: File contains parsing errors: /etc/odoo-server.conf
[line 3]: '/home/oerp/openerp80/addons,\n'
[line 4]: '/home/oerp/openerp80/community-addons\n'
Is there a way to write paths in multiple lines, but make ConfigParser treat it as one line (ignoring newlines)?
Update
Config file sample (works):
someparam = blabla
; this is part I want to modify
addons_path = /some/dir1,/some/dir2,/some/dir3
; some other params
Config file I want to look like, but it does not work:
someparam = blabla
; this is part I want to modify
addons_path = /some/dir1,
/some/dir2,
/some/dir3
; some other params
PS Indentation can be different as long as I could put paths in multiple lines, so it would not just go into one long line.
The answers before were a step in the right direction.
In Odoo 9 it works like this (You need to indent the lines and keep the commas but it's still possible to have multiple paths in one line.):
[options]
addons_path =
/some/dir1,
/some/dir2,/some/dir3,
/some/dir4,
/some/dir5
To achieve this in an older version you can modify the odoo/openerp/tools/config.py
Search this line
os.path.abspath(os.path.expanduser(os.path.expandvars(x)))
and replace it with this
os.path.abspath(os.path.expanduser(os.path.expandvars(x.strip())))
If you indent the lines that add information to your addons_path
parameter, they will be parsed as one string. Complete example, no commas needed between values (I am using StringIO
for better demostration, in most cases you will have a file object):
import ConfigParser
import StringIO
config_as_string = '''
[something]
someparam = blabla
; this is part I want to modify
addons_path =
/some/dir1
/some/dir2
/some/dir3
; some other params
'''
config_as_file = StringIO.StringIO(config_as_string)
config = ConfigParser.ConfigParser()
config.readfp(config_as_file)
addons_path = config.get('something', 'addons_path')
Then the addons_path
is a single string:
>>> print addons_path
/some/dir1
/some/dir2
/some/dir3
And as list:
>>> addons_path.split()
['/some/dir1', '/some/dir2', '/some/dir3']
Do these examples help? The key is indentation, I guess.
My suggestion would be to store things in a map if a map is what you're storing (or a list in your case) and after fetching just use a parser to read the values as they are defined
Like this:
[Section]
map1: {key: value,
key: value,
.
.
.
}
list1: [value1,
value2,
.
.
.
]
If You want to just make ConfigParser read multiple lines as if they were in a single line that refer to this post :
It basicaly states that:
The ConfigParser _read() method's .doc string says:
Continuations are represented by an embedded newline then leading whitespace.
Meaning that Your solution would be doing something like this:
[SECTION]
param=
item1
item2
item3
NOTE: This has not been tested and Is a re quote from another post. I'll remove this if someone confirms this works
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.