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.
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.
7 The getCarving function takes the file name of an csv file and returns the carving.
12 from __future__ import absolute_import
14 from fabmetheus_utilities import archive
15 from fabmetheus_utilities import xml_simple_reader
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'
25 def getCarving(fileName=''):
26 "Get the carving for the csv file."
27 csvText = archive.getFileText(fileName)
30 csvParser = CSVSimpleParser( fileName, None, csvText )
31 lowerLocalName = csvParser.getDocumentElement().getNodeName().lower()
32 pluginModule = archive.getModuleWithDirectoryPath( getPluginsDirectoryPath(), lowerLocalName )
33 if pluginModule == None:
35 return pluginModule.getCarvingFromParser( csvParser )
37 def getLineDictionary(line):
38 "Get the line dictionary."
40 splitLine = line.split('\t')
41 for splitLineIndex in xrange( len(splitLine) ):
42 word = splitLine[ splitLineIndex ]
44 lineDictionary[ splitLineIndex ] = word
47 def getPluginsDirectoryPath():
48 "Get the plugins directory path."
49 return archive.getInterpretPluginsPath('xml_plugins')
52 class CSVElement( xml_simple_reader.XMLElement ):
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()
62 def continueParsingTable( self, line, lineStripped ):
63 "Parse replaced line."
64 if self.headingDictionary == None:
65 self.headingDictionary = getLineDictionary(line)
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:
82 self.parentNode.childNodes.append( csvElement )
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:
90 self.parentNode = oldElement
91 while leadingTabCount <= self.parentNode.getNumberOfParents():
92 self.parentNode = self.parentNode.parentNode
93 self.parentNode.childNodes.append(self)
96 def getElementFromTable( self, leadingTabCount, lineStripped, oldElement ):
97 "Parse replaced line."
98 self.headingDictionary = None
99 return self.getElementFromObject( leadingTabCount, lineStripped, oldElement )
101 def getNumberOfParents(self):
102 "Get the number of parent nodes."
103 if self.parentNode == None:
105 return self.parentNode.getNumberOfParents() + 1
108 class CSVSimpleParser( xml_simple_reader.DocumentNode ):
109 "A simple csv parser."
110 def __init__( self, parentNode, csvText ):
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:
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
130 self.oldCSVElement = CSVElement().getElementFromObject( leadingTabCount, lineStripped, self.oldCSVElement )
131 self.continueFunction = self.oldCSVElement.continueParsingObject
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:
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
146 if self.continueFunction != None:
147 self.continueFunction( line, lineStripped )
151 "Display the inset dialog."
152 if len(sys.argv) > 1:
153 getCarving(' '.join(sys.argv[1 :]))
155 if __name__ == "__main__":