Skip to content

Commit

Permalink
Merge pull request #12 from FOSSEE/data_convertor
Browse files Browse the repository at this point in the history
Data convertor
  • Loading branch information
ThaHobbyist authored Oct 29, 2024
2 parents 6591084 + 35d5810 commit 6ac5525
Show file tree
Hide file tree
Showing 55 changed files with 2,108 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pyvnt.egg-info
manual_tests/
manual_debug/

linux64GccDPInt32Opt/
bin/

**/__pycache__/*
.vscode
pyrightconfig.json
Expand All @@ -20,6 +23,8 @@ __pycache__/

# C extensions
*.so
*.dep
*.o

# Distribution / packaging
.Python
Expand Down
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include pyvnt/Converter/Reader/cpp_src/dictionaryFile/lib/dictionaryFile.so
include pyvnt/Converter/Reader/cpp_src/dictionaryFileChecker/bin/dictionaryFileChecker.so
include pyvnt/Converter/Reader/cpp_src/dictionaryFileIterator/lib/dictionaryFileIterator.so
include pyvnt/Converter/Reader/cpp_src/libOpenFOAM.so
68 changes: 68 additions & 0 deletions pyvnt/Converter/Reader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import os
from pyvnt.Container.node import Node_C
from .dictionaryFileIterator import DictionaryFileIterator
from .dictionaryFile import DictionaryFile


def read(filepath : str, verifyFile: bool = True) -> Node_C:
'''
Reads dictionary file from the given filepath and returns a node tree
representation of it.
'''
file = DictionaryFile(filepath, verifyFile)
itr = DictionaryFileIterator(file)

path = file.filepath

root_name = path.split('/')[-1]
# print(root_name)

root = _createTree(root_name, itr)
itr.close()
file.close()

return root


def _createTree(parentName: str, itr: DictionaryFileIterator) -> Node_C:
'''
Recursively traverse the openfoam's dictionary data structure and create the
node-tree structure.
'''
data = {}
while itr.hasEntry():
key = itr.getCurrentEntryKeyword()
value = None

# print(f"{key}, {itr.isCurrentEntryList()}")

if itr.isCurrentEntryDict():
# print(key)
itr.stepIn()
value = _createTree(key, itr)
itr.stepOut()
else:
value = itr.getKeyData()
# print(value)

print(key)
# print(f"{key}, {type(value)}")
data[key] = value
itr.step()

# create node
children = []
itms = []

for key, val in data.items():
if isinstance(val,Node_C):
children.append(val)
else:
itms.append(val)

node = Node_C(parentName, None, children, *itms)

return node


4 changes: 4 additions & 0 deletions pyvnt/Converter/Reader/cpp_src/dictionaryFile/Make/files
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

dictionaryFile.C

LIB = ./lib/dictionaryFile
4 changes: 4 additions & 0 deletions pyvnt/Converter/Reader/cpp_src/dictionaryFile/Make/options
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

LIB_LIBS = \
-L$FOAM_LIBBIN \
-lOpenFOAM
48 changes: 48 additions & 0 deletions pyvnt/Converter/Reader/cpp_src/dictionaryFile/dictionaryFile.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

#include "fileName.H"
#include "IFstream.H"
#include "dictionary.H"

#include "dictionaryFile.H"
#include "dictionaryFile_PythonInterface.H"


DictionaryFile::DictionaryFile(const char* filepath)
{
Foam::fileName dictPath(filepath);
Foam::IFstream dictFileStream(dictPath);
mDictPtr = new Foam::dictionary(dictFileStream);
}

DictionaryFile::~DictionaryFile()
{
delete mDictPtr;
}

// void DictionaryFile::printEntry(const char* key)
// {
// Foam::word keyname(key);
// std::cout
// << "dictionaryName : " << mDictPtr->name().toAbsolute() << " "
// << mDictPtr->lookupEntry(keyname, false, false).keyword()
// << std::endl;
// }

// API FUNCTIONS===============================================================
void* openDictionaryFile(const char* filepath)
{
DictionaryFile* dictFile = new DictionaryFile(filepath);
return dictFile;
}

void closeDictionaryFile(void* dictionaryFile)
{
DictionaryFile* dictFile = static_cast<DictionaryFile*>(dictionaryFile);
delete dictFile;
}

// void printDictionary(void* dictionaryFile)
// {
// DictionaryFile* dictFile = static_cast<DictionaryFile*>(dictionaryFile);
// dictFile->printEntry("key1");
// }
15 changes: 15 additions & 0 deletions pyvnt/Converter/Reader/cpp_src/dictionaryFile/dictionaryFile.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#pragma once


class DictionaryFile
{
public:
Foam::dictionary* mDictPtr;

DictionaryFile(const char* filepath);
~DictionaryFile();
// void* getIterator();
// void printEntry(const char* key);
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#pragma once


extern "C"
{

void* openDictionaryFile(const char* filepath);
void closeDictionaryFile(void* dictionaryFile);
// void* getIterator(void* dictionaryFile);
// void printDictionary(void* dictionaryFile);
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

dictionaryFileChecker.C

EXE = ./bin/dictionaryFileChecker
Loading

0 comments on commit 6ac5525

Please sign in to comment.