Starting our Config Editor
February 21, 2012 4 Comments
In the last two tutorials we have been revising some things we had learned earlier about classes and GUIs. I wanted to give you an example of where you can use Python in practice, so we are going to do a short program which will use classes and Tkinter – a configuration editor for Minecraft servers. We haven’t yet seen all of the widgets that we will need for this short project, so we will do it a little slowly.
When people write computer programs they try to write the program they don’t necessarily know all the relevant details about how and where it will be run. It is often the case that certain data needs to be provided that program by the end user. For example, let’s say you wrote a greeting program which said hello to the user when they ran it. If the end user’s name was Søren Kierkegaard, how would the program know to say “Hello Søren?” – it wouldn’t. Søren would need to tell it. What’s more, it would be a little tedious if Søren had to keep telling the program what his name was every time he ran the program, so, ideally, once it was entered, the program would store the data. One place it could be stored is in a configuration file.
Minecraft is a building game. If you run your own Minecraft server the server stores its configuration information in a file called “server.properties”. Apparently, the default server.properties file looks like this:
#Minecraft server properties #Date and time of creation of file allow-nether=true level-name=world enable-query=false allow-flight=false server-port=25565 level-type=DEFAULT enable-rcon=false level-seed= server-ip= spawn-npcs=true white-list=false spawn-animals=true online-mode=true pvp=true difficulty=1 gamemode=0 max-players=20 spawn-monsters=true generate-structures=true view-distance=10 motd=A Minecraft Server
When you run the Minecraft server, it reads each line in this file, comprehends the line and does something based on that comprehension. For example, the lines beginning with “#” are comment lines, so the server ignores them. The other lines, you’ll notice all have an “=” sign in them and look like a Python assignment statement. They are doing something similar. Each line has a key – value pair. The stuff on the left of the = sign is the key, and the stuff on the right of the = sign is the value that is assigned to that key. The last line “motd” stands for “Message of the Day”. When someone logs onto a server with this server.properties file they will be greeted with the words “A Minecraft Server”. Changing this line will change the message that players see.
The act of comprehending data in this way is called “parsing” (more like “par-zing”, than “pass sing”). To parse this config file would involve: identifying lines which start with “#” (they are comments) and breaking other lines into their key,value pairs. A more advanced parser might also identify invalid configuration lines. For example, many lines you see here have values of either “true” or “false”. These, are, in fact, the only two possibilities for those keys. A line like “spawn-monsters=Potato chips” would be invalid. Equally, view-distance needs to be a whole number and so on.
While you can open up a configuration file in a text editor, what we’re going to do is write a program which allows a user to change the server.properties file by using a Tkinter GUI. We’ll use a lot of what we have already learned.
Homework: save the extract above to a file called “server.properties” in your Python for kids directory.
Homework: write a docstring describing what we need to in order to edit a configuration file called “server.properties”. This will be our set of instructions for writing the program.
Hint: step 1 will be to open the file, and the last step will be to close the file…
Pingback: Python 4 Kids: Starting our Config Editor | Python | Syngu
Pingback: Linux News » Python4Kids New Tutorial: Starting our Config Editor
Pingback: Links 23/2/2012: No More Adobe Trash on GNU/Linux, New Mageia Coming | Techrights
Actually, that motd is when you go to the multiplayer tab and choose your server, and that only allows around 5-8 words, but if your using craftbukkit with essentials, in the essentials folder there would be a file called motd.txt, and thats where you ask the program to write to.Because if you do that then when you login you’ll get whatevers inside that motd.txt.