#***************************************************************************
#*                                                                         *
#*   Copyright (c) 2013 - Juergen Riegel <FreeCAD@juergen-riegel.net>      *  
#*                                                                         *
#*   This program is free software; you can redistribute it and/or modify  *
#*   it under the terms of the GNU Lesser General Public License (LGPL)    *
#*   as published by the Free Software Foundation; either version 2 of     *
#*   the License, or (at your option) any later version.                   *
#*   for detail see the LICENCE text file.                                 *
#*                                                                         *
#*   This program is distributed in the hope that it will be useful,       *
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
#*   GNU Library General Public License for more details.                  *
#*                                                                         *
#*   You should have received a copy of the GNU Library General Public     *
#*   License along with this program; if not, write to the Free Software   *
#*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
#*   USA                                                                   *
#*                                                                         *
#***************************************************************************






# here the usage description if you use this tool from the command line ("__main__")
CommandlineUsage = """Material - Tool to work with FreeCAD Material definition cards

Usage:
   Material [Options] card-file-name
   
Options:
 -c, --output-csv=file-name     write a comma seperated grid with the material data
 
Exit:
 0      No Error or Warning found
 1      Argument error, wrong or less Arguments given

Tool to work with FreeCAD Material definition cards

Examples:
  
   Material  "StandardMaterial/Steel.FCMat" 
 
Autor:
  (c) 2013 Juergen Riegel
  mail@juergen-riegel.net    
  Licence: LGPL

Version:
  0.1
"""

def importFCMat(fileName):
    "Read a FCMat file into a dictionary"
    import ConfigParser
    Config = ConfigParser.ConfigParser()
    Config.optionxform = str
    Config.read(fileName)
    dict1 = {}
    for section in Config.sections():
        options = Config.options(section)
        for option in options:
            dict1[option] = Config.get(section, option)

    return dict1
    
def exportFCMat(fileName,matDict):
    "Write a material dictionary to a FCMat file"
    import ConfigParser,string
    Config = ConfigParser.ConfigParser()
    
    # create groups
    for x in matDict.keys():
        grp,key = string.split(x,sep='_')
        if not Config.has_section(grp):
            Config.add_section(grp)

    # fill groups
    for x in matDict.keys():
        grp,key = string.split(x,sep='_')
        Config.set(grp,key,matDict[x])
    
    Preamble = "# This is a FreeCAD material-card file\n\n"
    # Writing our configuration file to 'example.cfg'
    with open(fileName, 'wb') as configfile:
        configfile.write(Preamble)
        Config.write(configfile)

    
    
    
    
if __name__ == '__main__':
    import sys, getopt
    try:
        opts, args = getopt.getopt(sys.argv[1:], "c:", ["output-csv="])
    except getopt.GetoptError:
        # print help information and exit:
        sys.stderr.write(CommandlineUsage)
        sys.exit(1)

    # checking on the options
    for o, a in opts:
        if o in ("-c", "--output-csv"):
            print "writing file: " + a +"\n"
            OutPath = a

    # runing through the files
    FileName = args[0]

    kv_map = importFCMat(FileName)
    for k in kv_map.keys():
        print `k` + " : " + `kv_map[k]`
    sys.exit(0) # no error
    



@@ -170,16 +170,21 @@
 (c) 2013
+-2015
  - Juerg
@@ -226,16 +226,9 @@
 et%3E 
-     *  
+*
 %0A#* 
@@ -1611,21 +1611,16 @@
 ******%0A%0A
-%0A%0A%0A%0A%0A
 # here t
@@ -1826,20 +1826,17 @@
 le-name%0A
-   
 %0A
+
 Options:
@@ -1920,18 +1920,17 @@
 al data%0A
- 
 %0A
+
 Exit:%0A 0
@@ -2079,18 +2079,16 @@
 amples:%0A
-  
 %0A   Mate
@@ -2123,19 +2123,17 @@
 l.FCMat%22
- %0A 
+%0A
 %0AAutor:%0A
@@ -2183,20 +2183,16 @@
 egel.net
-    
 %0A  Licen
@@ -2221,16 +2221,17 @@
 .1%0A%22%22%22%0A%0A
+%0A
 def impo
@@ -2611,20 +2611,17 @@
 n dict1%0A
-    
+%0A
 %0Adef exp
@@ -2721,17 +2721,28 @@
 igParser
-,
+%0A    import 
 string%0A 
@@ -2781,21 +2781,17 @@
 arser()%0A
-    
 %0A
+
     # cr
@@ -3078,21 +3078,17 @@
 ict%5Bx%5D)%0A
-    
 %0A
+
     Prea
@@ -3311,28 +3311,9 @@
 e)%0A%0A
-    %0A    %0A    %0A    
 %0A
+
 if _
@@ -3349,17 +3349,27 @@
 port sys
-,
+%0A    import
  getopt%0A
@@ -3719,16 +3719,17 @@
  %22 + a +
+ 
 %22%5Cn%22%0A   
@@ -3923,16 +3923,17 @@
 .exit(0)
+ 
  # no er
@@ -3940,11 +3940,4 @@
 ror%0A
-    %0A%0A%0A

Fix white space errors in Material.py