chiark / gitweb /
Add back the ultimaker platform, and made the platform mesh simpler.
[cura.git] / Cura / slice / cura_sf / fabmetheus_utilities / fabmetheus_tools / interpret_plugins / csv.py
1 """
2 This page is in the table of contents.
3 The csv.py script is an import translator plugin to get a carving from an csv file.
4
5 An import plugin is a script in the interpret_plugins folder which has the function getCarving.  It is meant to be run from the interpret tool.  To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
6
7 The getCarving function takes the file name of an csv file and returns the carving.
8
9 """
10
11
12 from __future__ import absolute_import
13
14 from fabmetheus_utilities import archive
15 from fabmetheus_utilities import xml_simple_reader
16 import sys
17
18
19 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
20 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
21 __date__ = '$Date: 2008/21/04 $'
22 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
23
24
25 def getCarving(fileName=''):
26         "Get the carving for the csv file."
27         csvText = archive.getFileText(fileName)
28         if csvText == '':
29                 return None
30         csvParser = CSVSimpleParser( fileName, None, csvText )
31         lowerLocalName = csvParser.getDocumentElement().getNodeName().lower()
32         pluginModule = archive.getModuleWithDirectoryPath( getPluginsDirectoryPath(), lowerLocalName )
33         if pluginModule == None:
34                 return None
35         return pluginModule.getCarvingFromParser( csvParser )
36
37 def getLineDictionary(line):
38         "Get the line dictionary."
39         lineDictionary = {}
40         splitLine = line.split('\t')
41         for splitLineIndex in xrange( len(splitLine) ):
42                 word = splitLine[ splitLineIndex ]
43                 if word != '':
44                         lineDictionary[ splitLineIndex ] = word
45         return lineDictionary
46
47 def getPluginsDirectoryPath():
48         "Get the plugins directory path."
49         return archive.getInterpretPluginsPath('xml_plugins')
50
51
52 class CSVElement( xml_simple_reader.XMLElement ):
53         "A csv element."
54         def continueParsingObject( self, line, lineStripped ):
55                 "Parse replaced line."
56                 splitLineStripped = lineStripped.split('\t')
57                 key = splitLineStripped[0]
58                 value = splitLineStripped[1]
59                 self.attributes[key] = value
60                 self.addToIdentifierDictionaries()
61
62         def continueParsingTable( self, line, lineStripped ):
63                 "Parse replaced line."
64                 if self.headingDictionary == None:
65                         self.headingDictionary = getLineDictionary(line)
66                         return
67                 csvElement = self
68                 oldAttributesLength = len( self.attributes )
69                 if oldAttributesLength > 0:
70                         csvElement = CSVElement()
71                 csvElement.parentNode = self.parentNode
72                 csvElement.localName = self.localName
73                 lineDictionary = getLineDictionary(line)
74                 for columnIndex in lineDictionary.keys():
75                         if columnIndex in self.headingDictionary:
76                                 key = self.headingDictionary[ columnIndex ]
77                                 value = lineDictionary[ columnIndex ]
78                                 csvElement.attributes[key] = value
79                 csvElement.addToIdentifierDictionaries()
80                 if len( csvElement.attributes ) == 0 or oldAttributesLength == 0 or self.parentNode == None:
81                         return
82                 self.parentNode.childNodes.append( csvElement )
83
84         def getElementFromObject( self, leadingTabCount, lineStripped, oldElement ):
85                 "Parse replaced line."
86                 splitLine = lineStripped.split('\t')
87                 self.localName = splitLine[1]
88                 if leadingTabCount == 0:
89                         return self
90                 self.parentNode = oldElement
91                 while leadingTabCount <= self.parentNode.getNumberOfParents():
92                         self.parentNode = self.parentNode.parentNode
93                 self.parentNode.childNodes.append(self)
94                 return self
95
96         def getElementFromTable( self, leadingTabCount, lineStripped, oldElement ):
97                 "Parse replaced line."
98                 self.headingDictionary = None
99                 return self.getElementFromObject( leadingTabCount, lineStripped, oldElement )
100
101         def getNumberOfParents(self):
102                 "Get the number of parent nodes."
103                 if self.parentNode == None:
104                         return 0
105                 return self.parentNode.getNumberOfParents() + 1
106
107
108 class CSVSimpleParser( xml_simple_reader.DocumentNode ):
109         "A simple csv parser."
110         def __init__( self, parentNode, csvText ):
111                 "Add empty lists."
112                 self.continueFunction = None
113                 self.extraLeadingTabCount = None
114                 self.lines = archive.getTextLines( csvText )
115                 self.oldCSVElement = None
116                 self.documentElement = None
117                 for line in self.lines:
118                         self.parseLine(line)
119
120         def getNewCSVElement( self, leadingTabCount, lineStripped ):
121                 "Get a new csv element."
122                 if self.documentElement != None and self.extraLeadingTabCount == None:
123                         self.extraLeadingTabCount = 1 - leadingTabCount
124                 if self.extraLeadingTabCount != None:
125                         leadingTabCount += self.extraLeadingTabCount
126                 if lineStripped[ : len('_table') ] == '_table' or lineStripped[ : len('_t') ] == '_t':
127                         self.oldCSVElement = CSVElement().getElementFromTable( leadingTabCount, lineStripped, self.oldCSVElement )
128                         self.continueFunction = self.oldCSVElement.continueParsingTable
129                         return
130                 self.oldCSVElement = CSVElement().getElementFromObject( leadingTabCount, lineStripped, self.oldCSVElement )
131                 self.continueFunction = self.oldCSVElement.continueParsingObject
132
133         def parseLine(self, line):
134                 "Parse a gcode line and add it to the inset skein."
135                 lineStripped = line.lstrip()
136                 if len( lineStripped ) < 1:
137                         return
138                 leadingPart = line[ : line.find( lineStripped ) ]
139                 leadingTabCount = leadingPart.count('\t')
140                 if lineStripped[ : len('_') ] == '_':
141                         self.getNewCSVElement( leadingTabCount, lineStripped )
142                         if self.documentElement == None:
143                                 self.documentElement = self.oldCSVElement
144                                 self.documentElement.document = self
145                         return
146                 if self.continueFunction != None:
147                         self.continueFunction( line, lineStripped )
148
149
150 def main():
151         "Display the inset dialog."
152         if len(sys.argv) > 1:
153                 getCarving(' '.join(sys.argv[1 :]))
154
155 if __name__ == "__main__":
156         main()