<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://bf2tech.uturista.pt/index.php?action=history&amp;feed=atom&amp;title=Scripts%3AConParser</id>
	<title>Scripts:ConParser - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://bf2tech.uturista.pt/index.php?action=history&amp;feed=atom&amp;title=Scripts%3AConParser"/>
	<link rel="alternate" type="text/html" href="https://bf2tech.uturista.pt/index.php?title=Scripts:ConParser&amp;action=history"/>
	<updated>2026-08-06T01:14:17Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.40.1</generator>
	<entry>
		<id>https://bf2tech.uturista.pt/index.php?title=Scripts:ConParser&amp;diff=123&amp;oldid=prev</id>
		<title>Pireax: Created page with &quot;__TOC__ == Introduction  ==  This is a very simple parser for the BF2 Con files. You can use it to create BF-Style configs for your python scripts! I haven&#039;t tried it in BF2 y...&quot;</title>
		<link rel="alternate" type="text/html" href="https://bf2tech.uturista.pt/index.php?title=Scripts:ConParser&amp;diff=123&amp;oldid=prev"/>
		<updated>2018-06-22T15:34:56Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__TOC__ == Introduction  ==  This is a very simple parser for the BF2 Con files. You can use it to create BF-Style configs for your python scripts! I haven&amp;#039;t tried it in BF2 y...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
== Introduction  ==&lt;br /&gt;
&lt;br /&gt;
This is a very simple parser for the BF2 Con files. You can use it to create BF-Style configs for your python scripts! I haven&amp;#039;t tried it in BF2 yet, but it should work fine :)&lt;br /&gt;
&lt;br /&gt;
Info: Variables and Expressions are not supported! This is just for reading and saving informations!&lt;br /&gt;
&lt;br /&gt;
== Features  ==&lt;br /&gt;
&lt;br /&gt;
* Reads Con files and returns it as a python list&lt;br /&gt;
* Writes Con files from a python list to file&lt;br /&gt;
&lt;br /&gt;
== Usage  ==&lt;br /&gt;
&lt;br /&gt;
Include the code and include it to your python script - very simple :)&lt;br /&gt;
&lt;br /&gt;
Read files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;config = readCon(&amp;#039;MyConfig.con&amp;#039;) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Write files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;success = writeCon(config, &amp;#039;MyConfig.con&amp;#039;)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==  The Code  ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# ----------------------------------------------&lt;br /&gt;
# FIRE&amp;#039;S SIMPLE CON PARSER&lt;br /&gt;
#&lt;br /&gt;
# CONTACT:&lt;br /&gt;
# fire_1@gmx.de&lt;br /&gt;
# www.flmod.com&lt;br /&gt;
#&lt;br /&gt;
# USAGE (READ):&lt;br /&gt;
# config = readCon(file)&lt;br /&gt;
# Returns a list: [{key1: [values1]}, {key2: [values2, values2]}, ...]&lt;br /&gt;
#&lt;br /&gt;
# USAGE (WRITE):&lt;br /&gt;
# success = writeCon(config, file)&lt;br /&gt;
# ----------------------------------------------&lt;br /&gt;
&lt;br /&gt;
# ----------------------------------------------&lt;br /&gt;
# Imports&lt;br /&gt;
# ----------------------------------------------&lt;br /&gt;
import string&lt;br /&gt;
from bf2 import g_debug&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def readCon(conFile):&lt;br /&gt;
    config = []&lt;br /&gt;
    rem = False&lt;br /&gt;
   &lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    # Open the file&lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    if g_debug:&lt;br /&gt;
        print &amp;#039;Reading CON file...&amp;#039;&lt;br /&gt;
   &lt;br /&gt;
    try:&lt;br /&gt;
        f = file(conFile, &amp;#039;r&amp;#039;)&lt;br /&gt;
        lines = f.readlines()&lt;br /&gt;
        f.close()&lt;br /&gt;
    except IOError: # Damn..&lt;br /&gt;
        if g_debug:&lt;br /&gt;
            print &amp;#039;Error while reading CON file!!&amp;#039;&lt;br /&gt;
       &lt;br /&gt;
        return config&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
    for line in lines:&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        # Replace tabs etc.&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        line = string.replace(line, &amp;#039;\t&amp;#039;, &amp;#039;\x20&amp;#039;)&lt;br /&gt;
        line = string.strip(line, &amp;#039;\x20\n&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        # Remove comments&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        if string.strip(string.lower(line[:4])) == &amp;#039;rem&amp;#039;:&lt;br /&gt;
            continue&lt;br /&gt;
        elif string.strip(string.lower(line[:9])) == &amp;#039;beginrem&amp;#039;:&lt;br /&gt;
            rem = True&lt;br /&gt;
            continue&lt;br /&gt;
        elif string.strip(string.lower(line[:7])) == &amp;#039;endrem&amp;#039;:&lt;br /&gt;
            rem = False&lt;br /&gt;
            continue&lt;br /&gt;
        else:&lt;br /&gt;
            if rem:&lt;br /&gt;
                continue&lt;br /&gt;
&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        # Find the end of the key&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        pos = string.find(line, &amp;#039;\x20&amp;#039;)&lt;br /&gt;
        if pos &amp;lt; 0: # Not found :(&lt;br /&gt;
            continue&lt;br /&gt;
&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        # Get the key&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        key = line[:pos]&lt;br /&gt;
&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        # Get the values&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        values = string.split(line[pos + 1:], &amp;#039;\x20&amp;#039;)&lt;br /&gt;
        for value in values:&lt;br /&gt;
            value = string.strip(value, &amp;#039;\x20&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
        config.append({key: values})&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    # Return the list&lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    return config&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# --------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def writeCon(config, conFile):&lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    # Open the file&lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    if g_debug:&lt;br /&gt;
        print &amp;#039;Writing CON file...&amp;#039;&lt;br /&gt;
   &lt;br /&gt;
    try:&lt;br /&gt;
        f = file(conFile, &amp;#039;w+&amp;#039;)&lt;br /&gt;
        pass&lt;br /&gt;
    except IOError: # Damn..&lt;br /&gt;
        return False&lt;br /&gt;
   &lt;br /&gt;
    data = &amp;#039;rem *** Created with FiRe\&amp;#039;s CON parser ***\n\n&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    # Create the Con file&lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    for conf in config:&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        # Add the key&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        data += conf.items()[0][0]&lt;br /&gt;
       &lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        # Add the values&lt;br /&gt;
        # ----------------------------------------------&lt;br /&gt;
        for key in conf.items()[0][1]:&lt;br /&gt;
            data += &amp;#039; &amp;#039; + key&lt;br /&gt;
        data += &amp;#039;\n&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    # Write the config to file&lt;br /&gt;
    # ----------------------------------------------&lt;br /&gt;
    try:&lt;br /&gt;
        f.write(data)&lt;br /&gt;
        f.close()&lt;br /&gt;
    except IOError:&lt;br /&gt;
        if g_debug:&lt;br /&gt;
            print &amp;#039;Error while writing CON file!!&amp;#039;&lt;br /&gt;
        return False&lt;br /&gt;
    else:&lt;br /&gt;
        return True&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pireax</name></author>
	</entry>
</feed>