1

Update:

Based on the responses as of 3/16/2013 (or lack thereof), I get the impression that nothing anything like what I have in mind exists already. Thus I will build such a mechanism for myself using the configparser module which was suggested by John Y and which does look to be helpful.

Original:

I am not talking about a checkpoint or a core dump. I seek help for a solution which provides a portable text file which can be used by instances of the same program running on entirely different machines and OSes.

I am primarily interested in addressing this question in the context of Python. A few years ago I had written a fairly complex Java program for which the user interface was highly configurable. I wanted to make it easy for a user to save whatever state he had configured and be able to make that his default for the next time he used the program. Furthermore, I wanted the file written for this purpose to be human readable and editable, so that alteration of the file itself could be another approach to setting up different configurations for the program. I solved the problem by a rather brute force solution which required writing specific code for each kind of relevant data structure, both to output it and to read it back in. It was a good solution to the problem; but it was tedious to implement. I was recently looking at a Java program written by someone else, and I saw that he had a much more elegant solution to the problem. (Though the underlying data base for describing an instance of his interface was not quite as elaborate as mine was.)

Now I am writing another such program which will be even more configurable. I am writing it in Python. I am but a beginner when it comes to Python; but I must say that I really like it. I have programmed in a great many languages ranging from assembly languages and Fortran to Lisp and APL; and I must say that learning Python has provided the most pleasant experience yet. Nevertheless, I keep running across very nice extensions and capabilities of which I was not yet aware. Most of the times I did a search to answer some question I had about Python, I would discover that the issue had been addressed comprehensively here at stackoverflow. Thus I am hopeful that some folks here will get the idea of what I seek, and, if possible, point me to some tools that will facilitate the process.

We are talking about a configuration file. Instances of it can be used with a distribution to set up different possible default configurations for the first time a person uses the program. The program itself can write such a file, which, when interpreted, will set up the same configuration as when state was saved. (Note that I am not talking about the application data which the program manipulates. That can be treated as a binary file which can be saved and restored rather easily. Different problem.) It is not as simple as saving particular parameters which can be set by using Widgets in the GUI, though that is part of it. (I am using Tkinter.) The problem is further complicated by the fact that the user can freely create additional objects, each with its own window, and each of which has its own configuration to deal with.

So far, I have written quite a bit of the low level code, without getting involved in making the GUI for all the things I want to be configurable. So I am asking the question here now, because the answer may have some impact on how I go about implementing the GUI. E.g., there may be 'things' I can attach to Widgets that say, "This parameter is something that needs to be saved in the configuration file for a state-save operation." And then it happens automatically when such a save is initiated. Furthermore, mechanisms for interpreting the output when the configuration is read back in are automatically created.

If I were to undertake this entirely myself, I am thinking I would have the program write a Python script, which, when interpreted, would restore the state. In some cases, there would be direct assignments to values in some of the modules. In other cases, there would be invocations of functions which would set some things up in the same manner that applied at the time of the save. Such a file could still be interpreted to a great extent and edited to some extent even by persons not really familiar with Python.

Anyway, I am interested in other ideas about how to attack this problem. I am hopeful that some tools may exist to facilitate a solution because I don't think that what I want to accomplish is an unusual thing to want. I apologize for the long-winded explanation; but I thought that there were a number of directions in which it was possible to misinterpret what I seek, and I wanted to be sure that folks understood where I was coming from. I would appreciate any pointers.

Added:

I think that, ideally, what I seek would work like this: I would take responsibility in the code for identifying all those bits of data which constitute configuration information in the sense I have in mind. (E.g., I might have a function that does something like "Add the setting of this Widget to the list of Widget-settable parameters that need to be saved for the configuration." Or the Thingy class on instantiation might want to say "Here is another instance of Thingy in the configuration and these are its attributes which are configurable.") Then, with that knowledge at the time a state-saving operation was requested, a separate tool would take over the task of dumping a description of all the information so-identified to a file. Another separate tool could be used to read that file and restore all the data. I am prepared to write those tools; but I am hoping something like this may already have been done.

responding to luc -

I realized that I had overemphasized the word "GUI" in my description, so I edited the description above to fix that.

I am not concerned with what the Widgets look like or how they are managed geometrically. The program will take care of all that. ptkgen seems more concerned about the appearance of the Widgets and their layout than about what their settings mean from the user's point of view. I am concerned with the settings of things that are configurable via the GUI, and the effects of actions the user has taken which affect things like what windows exist and where they are on the screen.

I do see that Python has support for encoding and decoding JSON, but that seems to be at a low level - a level so low that it does not address the larger issue. You would still have to make the particular calls to dump the particular set of things that constitute the state of the application and know what you are loading at that level when the file was read. At that level, I can invent my own format which would actually be easier to write and interpret than it would be using JSON.

4

1 回答 1

0

JSON 是将数据存储为配置文件的好方法。它是人类可读的。Python 支持它。

您可能对pytkgen感兴趣,它可以从 JSON 定义文件创建 Tkinter GUI。

我自己没试过,但看起来很有趣。

我希望它有帮助

于 2013-03-14T09:04:31.440 回答