-
Notifications
You must be signed in to change notification settings - Fork 8
/
ide_writer.py
130 lines (96 loc) · 4.16 KB
/
ide_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# SPDX-FileCopyrightText: (c) 2020 Western Digital Corporation or its affiliates,
# Arseniy Aharonov <[email protected]>
#
# SPDX-License-Identifier: MIT
# noinspection PyUnresolvedReferences
from xml.dom.minidom import Document
import stat_attributes as attributes
from services import toNativePath, mkdir, abstract_method, meta_class, FactoryByLegacy, formatMakeCommand
from stat_makefile_project import StatMakefileProject
class IdeWriter(meta_class(FactoryByLegacy, object, uidAttribute='IDE')):
def __init__(self, contents, *args, **kwargs):
"""
:type contents: StatMakefileProject
"""
self._contents = contents
@abstract_method
def createRootToken(self): pass
@abstract_method
def createDirectoryToken(self, name, parentDirectoryToken): pass
@abstract_method
def addFile(self, filePath, parentDirectoryToken): pass
@abstract_method
def write(self): pass
@property
def namespace(self):
return "ide_{0}".format(self._contents.name)
def formatMakeCommand(self, target):
return formatMakeCommand(self._contents.makefile, [target], STAT_NAMESPACE=self.namespace)
class IdeCompositeWriter(IdeWriter):
writers = []
def __init__(self, contents, *args):
"""
:type contents: StatMakefileProject
"""
super(IdeCompositeWriter, self).__init__(contents, args)
self._instances = [writer(contents, args) for writer in self.writers]
pass
def createRootToken(self):
return [writer.createRootToken() for writer in self._instances]
def createDirectoryToken(self, name, parentDirectoryToken):
return [writer.createDirectoryToken(name, token)
for writer, token in zip(self._instances, parentDirectoryToken)]
def addFile(self, filePath, parentDirectoryToken):
for writer, token in zip(self._instances, parentDirectoryToken):
writer.addFile(filePath, token)
def write(self):
for writer in self._instances:
writer.write()
class IdeXmlWriter(IdeWriter):
def __init__(self, contents, *args):
"""
:type contents: StatMakefileProject
"""
super(IdeXmlWriter, self).__init__(contents, args)
self._doc = Document()
self._filename = None
def composeElement(self, name, context=(), **xmlAttributes):
element = self._doc.createElement(name)
for attribute, value in xmlAttributes.items():
element.setAttribute(attribute, value)
if context and not isinstance(context, dict):
element.appendChild(self._doc.createTextNode(context))
else:
for childName in context:
childElement = self._doc.createElement(childName)
childElement.appendChild(self._doc.createTextNode(context[childName]))
element.appendChild(childElement)
return element
def write(self):
_file = open("./{0}/{1}".format(attributes.IDE_DIRECTORY, self._filename), "w")
self._doc.writexml(_file, indent="", addindent="\t", newl="\n", encoding="utf-8")
_file.flush()
_file.close()
def formatCommandLine(self, target):
return "cd..&&" + " ".join(self.formatMakeCommand(target))
class IdeWorkspaceWriter(object):
def __init__(self, ideName, makeFile):
self.__contents = StatMakefileProject(makeFile)
self.__writer = IdeWriter.create(ideName, self.__contents)
def write(self):
mkdir(attributes.IDE_DIRECTORY, exist_ok=True)
self.__addFileTree(self.__contents.tree, self.__writer.createRootToken())
self.__writer.write()
def __addFileTree(self, fileTree, parentToken):
for fileName in fileTree.files:
self.__writer.addFile(toNativePath(fileTree[fileName]), parentToken)
for directory in fileTree.dirs:
directoryTag = self.__writer.createDirectoryToken(directory, parentToken)
self.__addFileTree(fileTree[directory], directoryTag)
class IdeProjectWriterException(Exception):
"""
Custom exception for STAT IDE-Project Writer
"""
if __name__ == '__main__':
pass