python - configparser loading config files from zip -
i creating program loads , runs python scripts compressed file. along python scripts, have config file used configparser load info in uncompressed version of program.
is possible directly read config files in zip files directly configparser? or have unzip temp folder , load there?
i have tried directly giving path:
>>> sysconf = configparser.configparser() >>> sysconf.read_file("compressed.zip/config_data.conf") traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/local/lib/python3.4/configparser.py", line 691, in read_file self._read(f, source) file "/usr/local/lib/python3.4/configparser.py", line 1058, in _read raise missingsectionheadererror(fpname, lineno, line) configparser.missingsectionheadererror: file contains no section headers. file: '<???>', line: 1
didn't work. no surprises there.
then tried using zipfile
>>> zf = zipfile.zipfile("compressed.zip") >>> data = zf.read("config_data.conf") >>> sysconf = configparser.configparser() >>> sysconf.read_file(data) traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/local/lib/python3.4/configparser.py", line 691, in read_file self._read(f, source) file "/usr/local/lib/python3.4/configparser.py", line 1009, in _read if line.strip().startswith(prefix): attributeerror: 'int' object has no attribute 'strip'
and found didn't work either.
so i've resorted creating temp folder, uncompressing it, , reading conf file there. avoid if possible conf files limiting factor. can (and am) loading python modules zip file fine @ point.
i can raw text of file if there's way pass directly configparser, searching docs came empty handed.
update: tried using stringio file object, , seems work somewhat. configparser doesn't reject it, doesn't either.
>>> zf = zipfile.zipfile("compressed.zip") >>> data = zf.read(config_data.conf) >>> confdata = io.stringio(str(data)) >>> sysconf = configparser.configparser() >>> sysconf.readfp(confdata) traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/local/lib/python3.4/configparser.py", line 736, in readfp self.read_file(fp, source=filename) file "/usr/local/lib/python3.4/configparser.py", line 691, in read_file self._read(f, source) file "/usr/local/lib/python3.4/configparser.py", line 1058, in _read raise missingsectionheadererror(fpname, lineno, line) configparser.missingsectionheadererror: file contains no section headers. file: '<???>', line: 1 (continues spit out entire contents of file)
if use read_file instead, doesn't error out, doesn't load either.
>>> zf = zipfile.zipfile("compressed.zip") >>> data = zf.read(config_data.conf) >>> confdata = io.stringio(str(data)) >>> sysconf = configparser.configparser() >>> sysconf.read_file(confdata) >>> sysconf.items("general") #(this main section in file) traceback (most recent call last): file "/usr/local/lib/python3.4/configparser.py", line 824, in items d.update(self._sections[section]) keyerror: 'general' during handling of above exception, exception occurred: traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/local/lib/python3.4/configparser.py", line 827, in items raise nosectionerror(section) configparser.nosectionerror: no section: 'general'
can raw text of file if there's way pass directly configparser
try configparser.configparser.read_string
when coupled appropriate zip file, code works me:
import zipfile import configparser zf = zipfile.zipfile("compressed.zip") zf_config = zf.open("config_data.conf", "ru") zf_config_data = zf_config.read().decode('ascii') config = configparser.configparser() config.read_string(zf_config_data) assert config['today']['lunch']=='cheeseburger'
upon reflection, following might more appropriate:
import zipfile import configparser import io zf = zipfile.zipfile("compressed.zip") zf_config = zf.open("config_data.conf", "ru") zf_config = io.textiowrapper(zf_config) config = configparser.configparser() config.read_file(zf_config) assert config['today']['lunch']=='cheeseburger'
Comments
Post a Comment