简体   繁体   English

Python:在受限环境中读取/存储配置文件?

[英]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. 我正在编写一个将在Telit HE910 GSM模块内部运行的程序,该模块具有板载python解释器。 I would like my program to be able to read and store some parameters inside a configuration file onboard the GSM module. 我希望我的程序能够读取并在GSM模块板上的配置文件中存储一些参数。 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 ) GSM模块没有任何用于解析/写入配置文件的python模块(因此,我不能简单地使用import yamlimport jsonimport configparser甚至是import csv
  • The GSM module does not allow the creation of subdirectories. GSM模块不允许创建子目录。 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. 据我有限的理解,这使我无法简单地将PyYAML python模块的内容转储到GSM模块中并从程序中调用它。

I found a similar question here , but I don't even know where in the GSM module's filesystem I am. 我在这里找到了类似的问题,但我什至不知道我在GSM模块的文件系统中的位置。 import os doesn't seem to work, which is strange (contrary to documentation). import os似乎不起作用,这很奇怪(与文档相反)。

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). 我知道我可以使用Python文件存储一些只读配置,但是我也希望能够写入配置文件(重新设计系统以避免这种情况确实是不可取的)。

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). 我认为到目前为止,我最好的选择似乎是自己编写一个简单的csv解析器/编写器,除非有人有更好的主意(或知道如何利用没有任何子目录的Python模块)。

PS: The documentation below has a list of supported modules. PS:以下文档列出了支持的模块。 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. 编辑:我应该提到,配置文件需要从ac#.NET应用程序中可读/可写,而不是桌面上的另一个python解释器。

You have _ast (§5.2.36), so it should be possible to reimplement ast.literal_eval() . 您具有_ast (第5.2.336节),因此应该可以重新实现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? 为什么不打开一个txt文件并将所有信息存储为文本文件呢? I read the Easy Script Manual for your module, there is a posix module which supports methods like open, close, unlink. 我为您的模块阅读了Easy Script手册,其中有一个posix模块,它支持诸如open,close,unlink之类的方法。

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

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