Skip to content

Commit

Permalink
writer.py: Added functionality for adding comments
Browse files Browse the repository at this point in the history
Comment objects, if flagged for addition can now be added to the
markup.bcf file alongside viewpointReference objects.
  • Loading branch information
podestplatz committed Jun 9, 2019
1 parent e013043 commit 3eeb7f8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
43 changes: 41 additions & 2 deletions src/bcf/markup.py
Expand Up @@ -4,7 +4,7 @@
from typing import List # used for custom type annotations
from bcf.uri import Uri
from bcf.modification import Modification
from bcf.topic import Topic, SnippetType
from bcf.topic import Topic
from interfaces.state import State
from interfaces.hierarchy import Hierarchy
from interfaces.identifiable import Identifiable
Expand Down Expand Up @@ -122,19 +122,23 @@ def __str__(self):

def getEtElement(self, elem):

import copy
elem.attrib["Guid"] = str(self.id)
elem.tail = "\n\t"

if self.file is not None:
fileElem = ET.SubElement(elem, "Viewpoint")
fileElem.text = str(self.file)
fileElem.tail = "\n\t\t"

if self.snapshot is not None:
snapElem = ET.SubElement(elem, "Snapshot")
snapElem.text = str(self.snapshot)
snapElem.tail = "\n\t\t"

if self.index != -1:
indexElem = ET.SubElement(elem, "Index")
indexElem.text = str(self.index)
indexElem.tail = "\n\t\t"

return elem

Expand Down Expand Up @@ -200,6 +204,41 @@ def __str__(self):
return ret_str


def getEtElement(self, elem):

elem.tag = self.xmlName
elem.attrib["Guid"] = str(self.id)
elem.tail = "\n\t"

dateElem = ET.SubElement(elem, "Date")
dateElem.text = self.creation.date.isoformat("T", "seconds")
dateElem.tail = "\n\t\t"

authorElem = ET.SubElement(elem, "Author")
authorElem.text = self.creation.author
authorElem.tail = "\n\t\t"

commentElem = ET.SubElement(elem, "Comment")
commentElem.text = self.comment
commentElem.tail = "\n\t\t"

if self.viewpoint is not None:
vpElem = ET.SubElement(elem, "Viewpoint")
vpElem.attrib["Guid"] = str(self.viewpoint.id)
vpElem.tail = "\n\t\t"

if self.lastModification is not None:
modDateElem = ET.SubElement(elem, "ModifiedDate")
modDateElem.text = self.lastModification.date.isoformat("T", "seconds")
modDateElem.tail = "\n\t\t"

modAuthorElem = ET.SubElement(elem, "ModifiedAuthor")
modAuthorElem.text = self.lastModification.author
modAuthorElem.tail = "\n\t\t"

return elem


class Markup(Hierarchy, State, XMLName):

""" Every topic folder has exactly one markup.bcf file. This forms the
Expand Down
2 changes: 1 addition & 1 deletion src/bcf/topic.py
Expand Up @@ -54,7 +54,7 @@ def __str__(self):

class BimSnippet(Hierarchy, State, XMLName):
def __init__(self,
type: SnippetType = None,
type: str = "",
external: bool = False,
reference: Uri = None,
schema: Uri = None,
Expand Down
34 changes: 27 additions & 7 deletions src/bcf/writer.py
Expand Up @@ -8,7 +8,7 @@
import bcf.reader as reader
from interfaces.hierarchy import Hierarchy
from interfaces.identifiable import Identifiable
from bcf.markup import (Markup, ViewpointReference)
from bcf.markup import (Markup, ViewpointReference, Comment)

"""
`elementHierarchy` contains for each element, the writer supports writing, the
Expand Down Expand Up @@ -69,12 +69,22 @@

def getUniqueIdOfListElementInHierarchy(element):

"""
Looks through the hierarchy of an object up to the project. If somewhere on
the way up an element is identified as list element (may occur more than
once inside the same XML element) then the id of that element is returned.
It is assumed that max. one such list element is found. If `element` itself
is a list element `None` is returned. If no list element is found `None` is
returned.
"""

elementHierarchy = Hierarchy.checkAndGetHierarchy(element)
if not elementHierarchy:
return None

listElement = None
for item in elementHierarchy:
# climb up the hierarchy starting with the direct parent
for item in elementHierarchy[1:]:
if item.__class__.__name__ in listElements:
listElement = item

Expand Down Expand Up @@ -110,6 +120,11 @@ def getFileOfElement(element):

def getTopicOfElement(element):

"""
Returns the topic of an element. This is used to generate the right path to
the file that shall be edited.
"""

elementHierarchy = Hierarchy.checkAndGetHierarchy(element)
if not elementHierarchy: # just check for sanity
return None
Expand All @@ -122,6 +137,10 @@ def getTopicOfElement(element):
if isinstance(element, ViewpointReference):
return element.containingObject.topic

# All comments are assigned to just one topic.
if isinstance(element, Comment):
return element.containingObject.topic

strHierarchy = [ item.__class__.__name__ for item in elementHierarchy ]
if not "Topic" in strHierarchy:
return None
Expand Down Expand Up @@ -166,8 +185,7 @@ def getParentElement(element, etRoot):
etParent = etRoot
if reader.DEBUG:
print(etParent)

if listElemId:
else:
idAttrName = getIdAttrName(listElementId)
etParent = etRoot.find(".//*[@{}='{}']".format(idAttrName,
str(listElemId)))
Expand Down Expand Up @@ -226,10 +244,11 @@ def addElement(element):

newEtElement = element.getEtElement(ET.Element(element.xmlName))
if reader.DEBUG:
print("Type of elem: {}".format(newEtElement.__class__.__name__))
print("Type of parent: {}".format(etParent.__class__.__name__))
print("Element that gets inserted: ")
print(ET.tostring(newEtElement, encoding='utf8', method='xml'))

etParent.insert(insertionIndex, newEtElement)
xmlTree.write(filePath)
xmlTree.write(filePath, encoding="utf8")


if __name__ == "__main__":
Expand All @@ -238,3 +257,4 @@ def addElement(element):
argFile = sys.argv[1]
project = reader.readBcfFile(argFile)
addElement(project.topicList[0].viewpoints[0])
addElement(project.topicList[0].comments[0])

0 comments on commit 3eeb7f8

Please sign in to comment.