简体   繁体   English

Python:Configparser-转义换行符?

[英]Python: Configparser - escape newlines?

Is there a way to escape newlines using ConfigParser? 有没有一种方法可以使用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. config中有--addons-path这个选项,您可以在其中提供多个插件路径,但是路径很长,并且有很多路径,因此将所有内容写在一行中看起来真的很糟糕。 So I thought to put addons paths in multiple lines, but ConfigParser does not allow it. 所以我想将插件路径放在多行中,但是ConfigParser不允许这样做。 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)? 有没有一种方法可以在多行中写入路径,但是使ConfigParser将其视为一行(忽略换行符)?

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. PS缩进可以有所不同,只要我可以将路径放在多行中,那么它就不会只是排成一行。

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.): 在Odoo 9中,它的工作方式如下(您需要缩进行并保留逗号,但仍然可以在一行中包含多个路径。):

[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 要在旧版本中实现此目的,您可以修改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. 如果缩进将信息添加到addons_path参数的行,它们将被解析为一个字符串。 Complete example, no commas needed between values (I am using StringIO for better demostration, in most cases you will have a file object): 完整的示例,值之间不需要逗号(我使用StringIO进行更好的演示,在大多数情况下,您将有一个文件对象):

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: 那么addons_path是一个字符串:

>>> 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 : 如果您只想让ConfigParser读取多行,就像它们位于单行中一样,则引用此文章

It basicaly states that: 它基本指出:

The ConfigParser _read() method's .doc string says: ConfigParser _read()方法的.doc字符串表示:

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 如果有人确认可以使用,我将删除它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM