File indexing completed on 2024-04-14 03:48:49

0001 #!/usr/bin/env python
0002 
0003 #    SPDX-FileCopyrightText: 2008 Patrick Spendrin <ps_ml@gmx.de>
0004 #
0005 #    SPDX-License-Identifier: LGPL-2.0-or-later
0006 #
0007 # this script can be used to easily add new kml files
0008 # use it in the form: add_kml_tag.py Tagname
0009 # where Tagname is the name of the tag - be aware that the case does matter!!!
0010 
0011 maintainer=True
0012 import sys
0013 import os
0014 
0015 # the taglist lists all the tags of KML spec 2.1 with their respective parent tags
0016 # please keep in mind that tags are case sensitive in XML
0017 
0018 TAGLIST = """
0019 address,Feature
0020 altitude,Location,LookAt
0021 altitudeMode,Geometry,LookAt
0022 BalloonStyle,Style
0023 begin,TimeSpan
0024 bgColor,BalloonStyle,ListStyle
0025 Change,Update
0026 code,Status
0027 color,ColorStyle
0028 colorMode,ColorStyle
0029 cookie,NetworkLinkControl
0030 coordinates,Point
0031 Create,Update
0032 Delete,Update
0033 description,Feature
0034 Document,kml
0035 drawOrder,Overlay
0036 east,LatLonBox
0037 end,TimeSpan
0038 expires,NetworkLinkControl
0039 extrude,Geometry
0040 fill,PolyStyle
0041 flyToView,NetworkLink
0042 Folder,kml
0043 GroundOverlay,Folder,Document
0044 heading,Orientation,LookAt
0045 href,Icon
0046 hotSpot,IconStyle
0047 httpQuery,Link
0048 Icon,IconStyle,Overlay
0049 IconStyle,Style
0050 innerBoundaryIs,Polygon
0051 ItemIcon,ListStyle
0052 key,Pair
0053 LabelStyle,Style
0054 latitude,Location,LookAt
0055 LatLonAltBox,Region
0056 LatLonBox,GroundOverlay
0057 LinearRing,Placemark,innerBoundaryIs,outerBoundaryIs,MultiGeometry
0058 LineString,Placemark,MultiGeometry
0059 LineStyle,Style
0060 Link,Model,NetworkLink
0061 linkDescription,NetworkLinkControl
0062 linkName,NetworkLinkControl
0063 linkSnippet,NetworkLinkControl
0064 listItemType,ListStyle
0065 ListStyle,Style
0066 Location,Model
0067 Lod,Region
0068 longitude,Location,LookAt
0069 LookAt,Feature
0070 maxAltitude,LatLonAltBox
0071 maxFadeExtent,Lod
0072 maxLodPixels,Lod
0073 message,NetworkLinkControl
0074 Metadata,Feature
0075 minAltitude,LatLonAltBox
0076 minFadeExtent,Lod
0077 minLodPixels,Lod
0078 minRefreshPeriod,NetworkLinkControl
0079 Model,Placemark,MultiGeometry
0080 MultiGeometry,Placemark,MultiGeometry
0081 name,Feature
0082 NetworkLink,Container
0083 NetworkLinkControl,kml
0084 north,LatLonBox
0085 ObjArrayField,Schema
0086 ObjField,Schema
0087 open,Feature
0088 Orientation,Model
0089 outerBoundaryIs,Polygon
0090 outline,PolyStyle
0091 overlayXY,ScreenOverlay
0092 Pair,Stylemap
0093 phoneNumber,Feature
0094 Placemark,Container
0095 Point,Placemark,MultiGeometry
0096 Polygon,Placemark,MultiGeometry
0097 PolyStyle,Style
0098 range,LookAt
0099 refreshInterval,Link
0100 refreshMode,Link
0101 refreshVisibility,NetworkLink
0102 Region,Feature
0103 request,Status
0104 roll,Orientation
0105 rotation,ScreenOverlay
0106 rotationXY,ScreenOverlay
0107 Scale,Scale
0108 scale,LabelStyle
0109 Schema,Document
0110 ScreenOverlay,Container
0111 screenXY,ScreenOverlay
0112 SimpleArrayField,Schema
0113 SimpleField,Schema
0114 size,ScreenOverlay
0115 Snippet,Feature
0116 south,LatLonBox
0117 state,ItemIcon
0118 Style,Feature
0119 StyleMap,Feature
0120 styleUrl,Feature,Pair
0121 targetHref,Update
0122 tessellate,Geometry
0123 text,BalloonStyle
0124 textcolor,BalloonStyle
0125 tilt,Orientation,LookAt
0126 TimeSpan,Feature
0127 TimeStamp,Feature
0128 Update,NetworkLinkControl
0129 viewBoundScale,Link
0130 viewFormat,Link
0131 viewRefreshMode,Link
0132 viewRefreshTime,Link
0133 visibility,Feature
0134 west,LatLonBox
0135 when,TimeStamp
0136 width,LineStyle
0137 x,Scale
0138 y,Scale
0139 z,Scale
0140 """
0141 
0142 # the template header file
0143 HEADER="""/*
0144     SPDX-FileCopyrightText: 2008 Patrick Spendrin <ps_ml@gmx.de>
0145 
0146     SPDX-License-Identifier: LGPL-2.0-or-later
0147 */
0148 
0149 #ifndef KmltemplateTagHandler_h
0150 #define KmltemplateTagHandler_h
0151 
0152 #include "GeoTagHandler.h"
0153 
0154 namespace Marble
0155 {
0156 
0157 class KmltemplateTagHandler : public GeoTagHandler {
0158 public:
0159     virtual GeoNode* parse(GeoParser&) const;
0160 };
0161 
0162 }   // Marble
0163 
0164 #endif // KmltemplateTagHandler_h
0165 """
0166 
0167 # the template source file
0168 SOURCE="""/*
0169     SPDX-FileCopyrightText: 2008 Patrick Spendrin <ps_ml@gmx.de>
0170 
0171     SPDX-License-Identifier: LGPL-2.0-or-later
0172 */
0173 
0174 #include "KmltemplateTagHandler.h"
0175 
0176 #include <QDebug>
0177 
0178 #include "KmlElementDictionary.h"
0179 
0180 TEMPLATE_PARENT_HEADERS
0181 #include "GeoDataParser.h"
0182 
0183 namespace Marble
0184 {
0185 
0186 using namespace GeoDataElementDictionary;
0187 
0188 KML_DEFINE_TAG_HANDLER( template )
0189 
0190 GeoNode* KmltemplateTagHandler::parse( GeoParser& parser ) const
0191 {
0192     Q_ASSERT(parser.isStartElement() && parser.isValidElement(QLatin1String(kmlTag_template)));
0193 
0194     GeoStackItem parentItem = parser.parentElement();
0195     
0196     if( parentItem.represents( kmlTag_Parent ) ) {
0197         QString content = parser.readElementText().trimmed();
0198         
0199         parentItem.nodeAs<GeoDataParent>()->doSomething( content );
0200     }
0201 
0202     return 0;
0203 }
0204 
0205 } // Marble
0206 """
0207 
0208 path = os.getcwd() + os.sep
0209 
0210 # all known abstract parents - those need a different handling
0211 abstractParents=['Feature', 'Container', 'Geometry', 'ColorStyle', 'StyleSelector', 'SchemaField', 'Overlay', 'TimePrimitive']
0212 
0213 def add_new_copy( tagname, parents ):
0214     """ add new source files for Tag tagname """
0215 # set pretend to True to not actually generate the source files
0216     pretend = False
0217     if not ( os.path.exists( path + "Kml" + tagname.capitalize() + "TagHandler.h" ) or os.path.exists( path + "Kml" + tagname.capitalize() + "TagHandler.cpp" ) ):
0218         if not pretend:
0219             file( path + "Kml" + capitalize( tagname ) + "TagHandler.h", "wb" ).write( HEADER )
0220             file( path + "Kml" + capitalize( tagname ) + "TagHandler.cpp", "wb" ).write( SOURCE )
0221         else:
0222             print "writing " + path + "Kml" + capitalize( tagname ) + "TagHandler.h"
0223             print "writing " + path + "Kml" + capitalize( tagname ) + "TagHandler.cpp"
0224 
0225         HEADERS = ""
0226 # generate a list of the include headers according to the parent tags (which are normally equivalent to a GeoDataObject header)
0227         if tagname in parents.keys():
0228             for i in parents[ tagname ]:
0229                 if i in abstractParents:
0230                     print "abstract:", i
0231                 else:
0232                     HEADERS += "#include \\\"GeoData" + i + "\\.h\\\"\\n"
0233             firstParent = parents[ tagname ][ 0 ]
0234         else:
0235             firstParent = "Parent"
0236                 
0237         sedHeaderCommand = "sed -i -e \"s/KmltemplateTagHandler/Kml" + tagname + "TagHandler/g\" " \
0238                            + path + "Kml" + capitalize( tagname ) + "TagHandler.h"
0239 
0240         sedCPPCommand = "sed -i -e \"s/KmltemplateTagHandler/Kml" + tagname + "TagHandler/g\" " \
0241                         + "-e \"s/kmlTag_template/kmlTag_" + tagname +"/g\" " \
0242                         + "-e \"s/kmlTag_Parent/kmlTag_" + firstParent +"/g\" " \
0243                         + "-e \"s/KML_DEFINE_TAG_HANDLER( template )/KML_DEFINE_TAG_HANDLER( " + tagname +" )/g\" " \
0244                         + "-e \"s/TEMPLATE_PARENT_HEADERS/" + HEADERS + "/g\" " \
0245                         + "-e \"s/KmltemplateTagHandler.h/Kml" + capitalize( tagname ) +"TagHandler.h/g\" " \
0246                         + path + "Kml" + capitalize( tagname ) + "TagHandler.cpp"
0247         #print sedHeaderCommand
0248         print sedCPPCommand
0249         if not pretend:
0250             os.system( sedHeaderCommand )
0251             os.system( sedCPPCommand )
0252     else:
0253         print "file already existing: ", tagname
0254 
0255 def capitalize( tagname ):
0256     """ because the capitalize function does not work correctly for CamelCase """
0257     return tagname[0].capitalize() + tagname[1:]
0258 
0259 def usage( name ):
0260     print
0261     print name + " [Tag1 [...]]"
0262     print
0263     print "generate new source file for any kml tag"
0264     print "source files will be added in the directory where you execute"
0265     print "this script"
0266     
0267 
0268 parents = dict()
0269 for line in TAGLIST.split():
0270     """make up a dict of all tags parents"""
0271     tmp = line.split(',')
0272     tagname = tmp[ 0 ]
0273     parents[ tagname ] = tmp[ 1: ]
0274     
0275 if len(sys.argv) > 1:
0276     if sys.argv[ 1 ] in ['-h', '--help']:
0277         usage( sys.argv[ 0 ] )
0278         sys.exit( 0 )
0279         
0280     newTags = sys.argv[ 1: ]
0281     
0282     for tag in newTags:
0283         """ check if parents of to-be-generated are available """
0284         if maintainer==False:
0285             try:
0286                 p = parents[ tag ]
0287             except:
0288                 print "error: no such tag " + tag + "!"
0289                 continue
0290         add_new_copy( tag, parents )