<commit_before>'''
   Copyright 2015 University of Auckland

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
'''
import json
import os
from opencmiss.neon.settings import mainsettings
from opencmiss.zinc.context import Context
from opencmiss.zinc.status import OK as ZINC_OK
from opencmiss.zinc.streamregion import StreaminformationRegion
from opencmiss.zinc.streamscene import StreaminformationScene


class MainApplication(object):

    def __init__(self):
        self._saveUndoRedoIndex = 0
        self._currentUntoRedoIndex = 0

        self._location = None
        self._recents = []

        self._context = Context("Main")

        # set up standard materials and glyphs
        materialmodule = self._context.getMaterialmodule()
        materialmodule.defineStandardMaterials()
        glyphmodule = self._context.getGlyphmodule()
        glyphmodule.defineStandardGlyphs()

        self._rootRegion = None
        self._document = None
        self._createBlankDocument()

    def _isDocumentValid(self, document):
        if ("OpenCMISS-Neon Version" in document) and ("RootRegion" in document):
            return True
        return False

    def _createBlankDocument(self):
        self._rootRegion = self._context.createRegion()
        self._document = {
            "OpenCMISS-Neon Version": [mainsettings.VERSION_MAJOR, mainsettings.VERSION_MINOR, mainsettings.VERSION_PATCH],
            "RootRegion": {}
        }

    def _updateZincDataInNeonRegion(self, neonRegion, zincRegion):
        scene = zincRegion.getScene()
        streamInfo = scene.createStreaminformationScene()
        streamInfo.setIOFormat(StreaminformationScene.IO_FORMAT_DESCRIPTION)
        streamMemory = streamInfo.createStreamresourceMemory()
        scene.write(streamInfo)
        result, sceneDescription = streamMemory.getBuffer()
        neonRegion["Scene"] = json.loads(sceneDescription)
        if not neonRegion["Scene"]:
            neonRegion.pop("Scene")  # remove empty scene description
        if "ChildRegions" in neonRegion:
            for neonChild in neonRegion["ChildRegions"]:
                childName = str(neonChild["Name"])
                zincChild = zincRegion.findChildByName(childName)
                self._updateZincDataInNeonRegion(neonChild, zincChild)

    def _updateZincDataInDocument(self):
        neonRegion = self._document["RootRegion"]
        zincRegion = self._rootRegion
        self._updateZincDataInNeonRegion(neonRegion, zincRegion)

    def _readModelSources(self, sources, zincRegion):
        streamInfo = zincRegion.createStreaminformationRegion()
        for source in sources:
            if source["Type"] == "File":
                fileName = str(source["FileName"])
                resource = streamInfo.createStreamresourceFile(fileName)
                if "Time" in source:
                    streamInfo.setResourceAttributeReal(resource, StreaminformationRegion.ATTRIBUTE_TIME, source["Time"])
                # if "Format" in source:
                #    format = source["Format"]
                #    if format == "EX":
                #        can't set per-resource file format
                #        streamInfo.setResourceFileFormat(resource, StreaminformationRegion.FILE_FORMAT_EX)
        result = zincRegion.read(streamInfo)
        if result != ZINC_OK:
            print("Failed to read model source")

    def _defineZincDataFromNeonRegion(self, neonRegion, zincRegion):
        if "Model" in neonRegion:
            model = neonRegion["Model"]
            if "Sources" in model:
                self._readModelSources(model["Sources"], zincRegion)
        if "Scene" in neonRegion:
            sceneDescription = json.dumps(neonRegion["Scene"])
            scene = zincRegion.getScene()
            streamInfo = scene.createStreaminformationScene()
            streamInfo.setIOFormat(StreaminformationScene.IO_FORMAT_DESCRIPTION)
            streamInfo.createStreamresourceMemoryBuffer(sceneDescription)
            result = scene.read(streamInfo)
            if result != ZINC_OK:
                print("Failed to read scene")
        # for each neon region, ensure there is a matching zinc region in the same order, and recurse
        zincChildRef = zincRegion.getFirstChild()
        if "ChildRegions" in neonRegion:
            for neonChild in neonRegion["ChildRegions"]:
                childName = str(neonChild["Name"])
                zincChild = zincRegion.findChildByName(childName)
                if zincChildRef.isValid() and (zincChild == zincChildRef):
                    zincChildRef = zincChildRef.getNextSibling()
                else:
                    if not zincChild.isValid():
                        zincChild = zincRegion.createRegion()
                        zincChild.setName(childName)
                    zincRegion.insertChildBefore(zincChild, zincChildRef)
                self._defineZincDataFromNeonRegion(neonChild, zincChild)
        # ensure any new zinc regions (read from model sources) are added to neon region. No recursion needed
        if zincChildRef.isValid():
            if "ChildRegions" not in neonRegion:
                neonRegion["ChildRegions"] = []
            while zincChildRef.isValid():
                neonRegion["ChildRegions"].append({"Name": zincChildRef.getName()})
                zincChildRef = zincChildRef.getNextSibling()
<commit_msg>use readDescription and writeDescription API for serialising scene.<commit_after>'''
   Copyright 2015 University of Auckland

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
'''
import json
import os
from opencmiss.neon.settings import mainsettings
from opencmiss.zinc.context import Context
from opencmiss.zinc.status import OK as ZINC_OK
from opencmiss.zinc.streamregion import StreaminformationRegion
from opencmiss.zinc.streamscene import StreaminformationScene


class MainApplication(object):

    def __init__(self):
        self._saveUndoRedoIndex = 0
        self._currentUntoRedoIndex = 0

        self._location = None
        self._recents = []

        self._context = Context("Main")

        # set up standard materials and glyphs
        materialmodule = self._context.getMaterialmodule()
        materialmodule.defineStandardMaterials()
        glyphmodule = self._context.getGlyphmodule()
        glyphmodule.defineStandardGlyphs()

        self._rootRegion = None
        self._document = None
        self._createBlankDocument()

    def _isDocumentValid(self, document):
        if ("OpenCMISS-Neon Version" in document) and ("RootRegion" in document):
            return True
        return False

    def _createBlankDocument(self):
        self._rootRegion = self._context.createRegion()
        self._document = {
            "OpenCMISS-Neon Version": [mainsettings.VERSION_MAJOR, mainsettings.VERSION_MINOR, mainsettings.VERSION_PATCH],
            "RootRegion": {}
        }

    def _updateZincDataInNeonRegion(self, neonRegion, zincRegion):
        scene = zincRegion.getScene()
        sceneDescription = scene.writeDescription()
        neonRegion["Scene"] = json.loads(sceneDescription)
        if not neonRegion["Scene"]:
            neonRegion.pop("Scene")  # remove empty scene description
        if "ChildRegions" in neonRegion:
            for neonChild in neonRegion["ChildRegions"]:
                childName = str(neonChild["Name"])
                zincChild = zincRegion.findChildByName(childName)
                self._updateZincDataInNeonRegion(neonChild, zincChild)

    def _updateZincDataInDocument(self):
        neonRegion = self._document["RootRegion"]
        zincRegion = self._rootRegion
        self._updateZincDataInNeonRegion(neonRegion, zincRegion)

    def _readModelSources(self, sources, zincRegion):
        streamInfo = zincRegion.createStreaminformationRegion()
        for source in sources:
            if source["Type"] == "File":
                fileName = str(source["FileName"])
                resource = streamInfo.createStreamresourceFile(fileName)
                if "Time" in source:
                    streamInfo.setResourceAttributeReal(resource, StreaminformationRegion.ATTRIBUTE_TIME, source["Time"])
                # if "Format" in source:
                #    format = source["Format"]
                #    if format == "EX":
                #        can't set per-resource file format
                #        streamInfo.setResourceFileFormat(resource, StreaminformationRegion.FILE_FORMAT_EX)
        result = zincRegion.read(streamInfo)
        if result != ZINC_OK:
            print("Failed to read model source")

    def _defineZincDataFromNeonRegion(self, neonRegion, zincRegion):
        if "Model" in neonRegion:
            model = neonRegion["Model"]
            if "Sources" in model:
                self._readModelSources(model["Sources"], zincRegion)
        if "Scene" in neonRegion:
            sceneDescription = json.dumps(neonRegion["Scene"])
            scene = zincRegion.getScene()
            scene.readDescription(sceneDescription, True)
        # for each neon region, ensure there is a matching zinc region in the same order, and recurse
        zincChildRef = zincRegion.getFirstChild()
        if "ChildRegions" in neonRegion:
            for neonChild in neonRegion["ChildRegions"]:
                childName = str(neonChild["Name"])
                zincChild = zincRegion.findChildByName(childName)
                if zincChildRef.isValid() and (zincChild == zincChildRef):
                    zincChildRef = zincChildRef.getNextSibling()
                else:
                    if not zincChild.isValid():
                        zincChild = zincRegion.createRegion()
                        zincChild.setName(childName)
                    zincRegion.insertChildBefore(zincChild, zincChildRef)
                self._defineZincDataFromNeonRegion(neonChild, zincChild)
        # ensure any new zinc regions (read from model sources) are added to neon region. No recursion needed
        if zincChildRef.isValid():
            if "ChildRegions" not in neonRegion:
                neonRegion["ChildRegions"] = []
            while zincChildRef.isValid():
                neonRegion["ChildRegions"].append({"Name": zincChildRef.getName()})
                zincChildRef = zincChildRef.getNextSibling()

{'fraction_non_alphanumeric': 0.07813467834704893, 'fraction_numerical': 0.0017697548889478807, 'mean_word_length': 2.5596850393700787, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6027216', 'n_tokens_mistral': 2984, 'n_tokens_neox': 2807, 'n_words': 751}