filemanager.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19  '''
20 from xml.dom import minidom
21 from visualstates.core.state import State
22 from visualstates.core.namespace import Namespace
23 from visualstates.configs.rosconfig import RosConfig
24 import os
25 import xml
26 
27 class FileManager():
28  def __init__(self):
29  self.fullPath = ""
30 
31  def getFullPath(self):
32  return self.fullPath
33 
34  def setFullPath(self, path):
35  # check for the xml extensition if missing add the extension
36  if path.rfind('.xml') < 0:
37  path += '.xml'
38  self.fullPath = path
39 
40  def save(self, rootState, config, libraries, globalNamespace):
41  doc = minidom.Document()
42  root = doc.createElement('VisualStates')
43  doc.appendChild(root)
44 
45  # save config data
46  if config is not None:
47  root.appendChild(config.createNode(doc))
48 
49  # save global namespace
50  globalNamespaceElement = globalNamespace.createNode(doc, globalNamespace=True)
51  root.appendChild(globalNamespaceElement)
52 
53  # save libraries
54  libraryElement = doc.createElement('libraries')
55  for lib in libraries:
56  libElement = doc.createElement('library')
57  libElement.appendChild(doc.createTextNode(lib))
58  libraryElement.appendChild(libElement)
59  root.appendChild(libraryElement)
60 
61  root.appendChild(self.createDocFromState(rootState, doc))
62  xmlStr = doc.toprettyxml(indent=' ')
63  with open(self.fullPath, 'w') as f:
64  f.write(xmlStr)
65 
66  def createDocFromState(self, state, doc):
67  stateElement = state.createElement(doc)
68  return stateElement
69 
70  def open(self, fullPath):
71  try:
72  doc = minidom.parse(fullPath)
73  except xml.parsers.expat.ExpatError:
74  return None, None, None, None
75 
76  if len(doc.getElementsByTagName('VisualStates')) == 0:
77  return None, None, None, None
78  self.setFullPath(fullPath)
79 
80  globalNamespaceNode = doc.getElementsByTagName('VisualStates')[0].getElementsByTagName('global_namespace')[0]
81  globalNamespace = Namespace('', '')
82  globalNamespace.parse(globalNamespaceNode)
83 
84  rootNode = doc.getElementsByTagName('VisualStates')[0].getElementsByTagName('state')[0]
85  rootState = State(0, 'root', True, None)
86  rootState.parse(rootNode)
87 
88  # parse configs
89  config = None
90  if len(doc.getElementsByTagName('VisualStates')[0].getElementsByTagName('config')) > 0:
91  configElement = doc.getElementsByTagName('VisualStates')[0].getElementsByTagName('config')[0]
92  config = RosConfig()
93  config.loadNode(configElement)
94 
95  libraries = []
96  # parse libraries
97  libraryElements = doc.getElementsByTagName('VisualStates')[0].getElementsByTagName('libraries')
98  if len(libraryElements) > 0:
99  libraryElements = libraryElements[0].getElementsByTagName('library')
100  for libElement in libraryElements:
101  if len(libElement.childNodes) > 0:
102  libraries.append(libElement.childNodes[0].nodeValue)
103 
104  return rootState, config, libraries, globalNamespace
105 
106  def hasFile(self):
107  return len(self.fullPath) > 0
108 
109  def setPath(self, path):
110  self.fullPath = path
111 
112  def getFileName(self):
113  name = ''
114  if self.fullPath.rfind(os.sep) >= 0:
115  name = self.fullPath[self.fullPath.rfind(os.sep)+1:len(self.fullPath)]
116 
117  # remove the file extension
118  if len(name) > 0:
119  name = name[0:name.rfind('.')]
120 
121  return name
122 
123  def getPath(self):
124  path = ''
125  if self.fullPath.rfind(os.sep) >= 0:
126  path = self.fullPath[0:self.fullPath.rfind(os.sep)]
127  return path
def save(self, rootState, config, libraries, globalNamespace)
Definition: filemanager.py:40


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20