简体   繁体   中英

Python: Reading / storing configuration file in a restrictive environment?

Here's the scenario:

I am writing a program that will run inside a Telit HE910 GSM module, which has an on-board python interpreter. I would like my program to be able to read and store some parameters inside a configuration file onboard the GSM module. The file may occasionally be transferred into a separate machine for viewing and changing, so it should be both machine-readable and writeable on multiple platforms.

So far, there are multiple solutions for this issue. Here's the kicker though:

  • The GSM module does not have any python modules for parsing / writing configuration files (so I can't simply use import yaml , import json , import configparser , or even import csv )
  • The GSM module does not allow the creation of subdirectories. From my limited understanding, this prevents me from simply dumping the contents of say, the PyYAML python module into the GSM module and calling it from my program.

I found a similar question here , but I don't even know where in the GSM module's filesystem I am. import os doesn't seem to work, which is strange (contrary to documentation).

I know I can use a Python file to store some read-only configurations, but I also want to be able to write to the config file (redesigning the system to avoid this is really undesirable).

I think my best bet so far seems to be to write a simple csv parser / writer myself, unless someone has a better idea (or know how to utilise Python modules without any subdirectories).

PS: The documentation below has a list of supported modules. None of the config-related modules seem to be available however.

EDIT: I should have mentioned, the configuration file needs to be readable / writeable from ac# .NET application, not another python interpreter on the desktop.

You have _ast (§5.2.36), so it should be possible to reimplement ast.literal_eval() . At that point reading and writing becomes mostly trivial.

我不建议在一般的Python使用中使用它,但可能的选择是您发布的手册5.2.34中提到的marshal模块,以及此处的description / limitations / warnings: http ://docs.python.org/library /marshal.html

Why don't you just open a txt file and store all info as a text file? I read the Easy Script Manual for your module, there is a posix module which supports methods like open, close, unlink.

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.

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