#! /usr/bin/python

# Copyright (C) 2000 Intevation GmbH <intevation@intevation.de>
# Author: Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the LGPL (>=v2)
# Read the file COPYING coming with MapIt! for details.

"""Usage: convertmarkerdefs input output

Convert MapIt! marker definitions from the old format (version 0.7 and
earlier) to the new format 0.8 and later).
"""

# Description of the old format (copied and slightly edited from the
# webpage (CVS revision 1.4)):

# The markerdefs file is a text file with line oriented format. Empty
# lines and lines starting with a hash (#) are ignored. The other lines
# contain either object or group definitions.
#
# A group is defined with a line containing four comma separated values.
# For example (the examples here are in German because the example data
# is in German at the moment):
#
#        db,bahnhof,Bahnhfe
#
# The first value is the name of the symbol to use. The filename of the
# symbol image is built from the values of the configuration variables
# marker_dir (the directory) and marker_ext (file name extension). A
# good file format for the symbol images is PNG because it can contain
# transparency information.
#
# The second value is the identifier of the group. This identifier has
# to be unique and should only contain characters that can be used in
# URLs and HTML without quoting (even though MapIt! treats them
# properly).
#
# The third value is the label of the group. It is used in the list box
# where the user can select which objects to show in the map. This label
# doesn't have to be unique, although several objects and groups with
# the same label will be confusing for the user.
#
# An object is defined by a line with six comma separated values. For
# example:
#
#       458862396.00,583250959.00,db,bahnhof,Berlin-Hermsdorf
#
# The first two values are the coordinates of the object. The two
# following values have the same meaning as the symbol name and
# identifier of a group. If the identifier of an object is the same as
# the identifier of a previously defined group, the object is associated
# with that group. Otherwise the identifier is only used for that
# object. The last value is again the label used in the HTML-page.


# Description of the new format:

# The markerdefs file is a text file with line oriented format. Empty
# lines and lines starting with a hash (#) are ignored. The other lines
# contain either object or group definitions.
#
# A group is defined with a line starting with the keyword "group:",
# followed by three comma separated values. For example (the examples
# here are in German because the example data is in German at the
# moment):
#
#        group: db,bahnhof,http://bahnhof/,Bahnhfe
#
# The first value is the name of the symbol to use. The filename of the
# symbol image is built from the values of the configuration variables
# marker_dir (the directory) and marker_ext (file name extension). A
# good file format for the symbol images is PNG because it can contain
# transparency information.
#
# The second value is the identifier of the group. This identifier has
# to be unique and should only contain characters that can be used in
# URLs and HTML without quoting (even though MapIt! treats them
# properly).
#
# The third parameter is a URL to be used in the client side image map
# for all markers of the group that don't define their own URL. The URL
# may be empty.
#
# The fourth value is the label of the group. It is used in the list box
# where the user can select which objects to show in the map. This label
# doesn't have to be unique, although several objects and groups with
# the same label will be confusing for the user.
#
# Only the first three commas are significant, so that the label may
# contain commas without special quoting.
#
# An object is defined by a line with six comma separated values. For
# example:
#
#       458862396.00,583250959.00,db,bahnhof,http://bahnhof/,Berlin-Hermsdorf
#
# The first two values are the coordinates of the object. The two
# following values have the same meaning as the symbol name and
# identifier of a group. If the identifier of an object is the same as
# the identifier of a previously defined group, the object is associated
# with that group. Otherwise the identifier is only used for that
# object. The last two values are again a URL and the label used in the
# HTML-page.
#
# If a marker contained in group does not define a URL, i.e. if the
# field is empty, the URL of the group is used, if the group has a URL
# associated with it.
#
# Only the first five commas are significant, so that the label may
# contain commas without special quoting.



import sys
from string import strip, split, join

def convert_file(infile, outfile):
    result = []
    lines = infile.readlines()
    for line_number in range(len(lines)):
        line = lines[line_number]
        stripped = strip(line)
        if not stripped or stripped[0] == '#':
            # it's an empty line or comment, copy verbatim
            result.append(line)
        else:
            items = split(line, ',')
            if len(items) == 3:
                # a group definition. Insert an empty URL and prefix the
                # keyword
                items.insert(2, '')
                result.append('group: ' + join(items, ','))
            else:
                # must be a normal object, but check the number of items
                # anyway
                if len(items) != 5:
                    sys.stdout.write("Line %d: Unknown line type. "
                                     "Ignoring it\n" % (line_number,))
                else:
                    # The line's OK, so simply insert an empty URL
                    items.insert(4, '')
                    result.append(join(items, ','))
    outfile.writelines(result)

def main():
    if len(sys.argv) != 3:
        print __doc__
        sys.exit(1)
    convert_file(open(sys.argv[1]), open(sys.argv[2], "w"))


if __name__ == '__main__':
    main()

