Code

56cec0308fb5c2b8fad7d275b429ae91f7c31305
[inkscape.git] / share / extensions / jessyInk_summary.py
1 #!/usr/bin/env python
2 # Copyright 2008, 2009 Hannes Hochreiner
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see http://www.gnu.org/licenses/.
16 # These lines are only needed if you don't put the script directly into
17 # the installation directory
18 import sys
19 # Unix
20 sys.path.append('/usr/share/inkscape/extensions')
21 # OS X
22 sys.path.append('/Applications/Inkscape.app/Contents/Resources/extensions')
23 # Windows
24 sys.path.append('C:\Program Files\Inkscape\share\extensions')
26 # We will use the inkex module with the predefined Effect base class.
27 import inkex
29 def propStrToList(str):
30         list = []
31         propList = str.split(";")
32         for prop in propList:
33                 if not (len(prop) == 0):
34                         list.append(prop.strip())
35         return list
37 def propListToDict(list):
38         dictio = {}
40         for prop in list:
41                 keyValue = prop.split(":")
43                 if len(keyValue) == 2:
44                         dictio[keyValue[0].strip()] = keyValue[1].strip()
46         return dictio
48 class JessyInk_Summary(inkex.Effect):
49         def __init__(self):
50                 # Call the base class constructor.
51                 inkex.Effect.__init__(self)
53                 self.OptionParser.add_option('--tab', action = 'store', type = 'string', dest = 'what')
55                 inkex.NSS[u"jessyink"] = u"https://launchpad.net/jessyink"
57         def effect(self):
58                 # Check version.
59                 scriptNodes = self.document.xpath("//svg:script[@jessyink:version='1.5.1']", namespaces=inkex.NSS)
61                 if len(scriptNodes) != 1:
62                         sys.stderr.write("The JessyInk script is not installed in this SVG file or has a different version than the JessyInk extensions. Please select \"install/update...\" from the \"JessyInk\" sub-menu of the \"Effects\" menu to install or update the JessyInk script.\n\n")
64                 # Find the script node, if present
65                 for node in self.document.xpath("//svg:script[@id='JessyInk']", namespaces=inkex.NSS):
66                         sys.stderr.write("JessyInk script ")
68                         if node.get("{" + inkex.NSS["jessyink"] + "}version"):
69                                 sys.stderr.write("version " + node.get("{" + inkex.NSS["jessyink"] + "}version") + " ")
71                         sys.stderr.write("installed.\n")
72         
73                 slides = []
74                 masterSlide = None 
76                 for node in self.document.xpath("//svg:g[@inkscape:groupmode='layer']", namespaces=inkex.NSS):
77                         if node.get("{" + inkex.NSS["jessyink"] + "}masterSlide"):
78                                 masterSlide = node
79                         else:
80                                 slides.append(node)
82                 if masterSlide is not None:
83                         sys.stderr.write("\nMaster slide:\n")
84                         self.describeNode(masterSlide, "\t", "<the number of the slide>", str(len(slides)), "<the title of the slide>")
86                 slideCounter = 1
88                 for slide in slides:
89                         sys.stderr.write("\nSlide " + str(slideCounter) + ":\n")
90                         self.describeNode(slide, "\t", str(slideCounter), str(len(slides)), slide.get("{" + inkex.NSS["inkscape"] + "}label"))
91                         slideCounter += 1
93         def describeNode(self, node, prefix, slideNumber, numberOfSlides, slideTitle):
94                 sys.stderr.write(prefix + "Layer name: " + node.get("{" + inkex.NSS["inkscape"] + "}label") + "\n")
95                 
96                 # Display information about transitions.
97                 transitionInAttribute = node.get("{" + inkex.NSS["jessyink"] + "}transitionIn")
98                 if transitionInAttribute:
99                         transInDict = propListToDict(propStrToList(transitionInAttribute))
100                         sys.stderr.write(prefix + "Transition in: " + transInDict["name"])
102                         if (transInDict["name"] != "appear") and transInDict.has_key("length"):
103                                 sys.stderr.write(" (" + str(int(transInDict["length"]) / 1000.0) + " s)")
105                         sys.stderr.write("\n")
107                 transitionOutAttribute = node.get("{" + inkex.NSS["jessyink"] + "}transitionOut")
108                 if transitionOutAttribute:
109                         transOutDict = propListToDict(propStrToList(transitionOutAttribute))
110                         sys.stderr.write(prefix + "Transition out: " + transOutDict["name"])
112                         if (transOutDict["name"] != "appear") and transOutDict.has_key("length"):
113                                 sys.stderr.write(" (" + str(int(transOutDict["length"]) / 1000.0) + " s)")
115                         sys.stderr.write("\n")
117                 # Display information about auto-texts.
118                 autoTexts = {"slideNumber" : slideNumber, "numberOfSlides" : numberOfSlides, "slideTitle" : slideTitle}
119                 autoTextNodes = node.xpath(".//*[@jessyink:autoText]", namespaces=inkex.NSS)
120                 
121                 if (len(autoTextNodes) > 0):
122                         sys.stderr.write("\n" + prefix + "Auto-texts:\n")
123                         
124                         for atNode in autoTextNodes:
125                                 sys.stderr.write(prefix + "\t\"" + atNode.text + "\" (object id \"" + atNode.getparent().get("id") + "\") will be replaced by")
126                                 sys.stderr.write(" \"" + autoTexts[atNode.get("{" + inkex.NSS["jessyink"] + "}autoText")] + "\".\n")
128                 # Collect information about effects.
129                 effects = {}
131                 for effectNode in node.xpath(".//*[@jessyink:effectIn]", namespaces=inkex.NSS):
132                         dictio = propListToDict(propStrToList(effectNode.get("{" + inkex.NSS["jessyink"] + "}effectIn")))
133                         dictio["direction"] = "in"
134                         dictio["id"] = effectNode.get("id")
135                         dictio["type"] = "effect"
137                         if not effects.has_key(dictio["order"]):
138                                 effects[dictio["order"]] = []
140                         effects[dictio["order"]].append(dictio)
142                 for effectNode in node.xpath(".//*[@jessyink:effectOut]", namespaces=inkex.NSS):
143                         dictio = propListToDict(propStrToList(effectNode.get("{" + inkex.NSS["jessyink"] + "}effectOut")))
144                         dictio["direction"] = "out"
145                         dictio["id"] = effectNode.get("id")
146                         dictio["type"] = "effect"
148                         if not effects.has_key(dictio["order"]):
149                                 effects[dictio["order"]] = []
151                         effects[dictio["order"]].append(dictio)
153                 for viewNode in node.xpath(".//*[@jessyink:view]", namespaces=inkex.NSS):
154                         dictio = propListToDict(propStrToList(viewNode.get("{" + inkex.NSS["jessyink"] + "}view")))
155                         dictio["id"] = viewNode.get("id")
156                         dictio["type"] = "view"
158                         if not effects.has_key(dictio["order"]):
159                                 effects[dictio["order"]] = []
161                         effects[dictio["order"]].append(dictio)
163                 order = sorted(effects.keys())
164                 orderNumber = 0
166                 # Display information about effects.
167                 for orderItem in order:
168                         if orderNumber == 0:
169                                 sys.stderr.write("\n" + prefix + "Initial effect (order number " + effects[orderItem][0]["order"]  + "):\n")
170                         else:
171                                 sys.stderr.write("\n" + prefix + "Effect " + str(orderNumber) + " (order number " + effects[orderItem][0]["order"]  + "):\n")
172                 
173                         for item in effects[orderItem]:
174                                 if item["type"] == "view":
175                                         sys.stderr.write(prefix + "\tView will be set according to object \"" + item["id"] + "\"")
176                                 else:
177                                         sys.stderr.write(prefix + "\tObject \"" + item["id"] + "\"")
178         
179                                         if item["direction"] == "in":
180                                                 sys.stderr.write(" will appear")
181                                         elif item["direction"] == "out":
182                                                 sys.stderr.write(" will disappear")
183         
184                                 if item["name"] != "appear":
185                                         sys.stderr.write(" using effect \"" + item["name"] + "\"")
186                                         
187                                 if item.has_key("length"):
188                                         sys.stderr.write(" in " + str(int(item["length"]) / 1000.0) + " s")
190                                 sys.stderr.write(".\n")
192                         orderNumber += 1
194 # Create effect instance
195 effect = JessyInk_Summary()
196 effect.affect()