Module.py 2.04 KB
Newer Older
Sebastian Eibl's avatar
Sebastian Eibl committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# -*- coding: utf-8 -*-

from pathlib import Path
import shutil
import os


class Module:
    def __init__(self, path, module_name='mesa_pd'):
        """Propery of a data strcuture

        Parameters
        ----------
        path : str
           path to the root waLBerla folder
        module_name : str
           name of the generated module
        """

20
21
22
23
24
25
26
27
        path = Path(path).resolve()
        self.context = {
            'path': path,
            'name': module_name,
            'module_path': path / 'src' / module_name,
            'test_path': path / 'tests' / module_name,
            'enableOpenMP': False
        }
Sebastian Eibl's avatar
Sebastian Eibl committed
28
29
30
31
32
33
34

        self.components = []

    def add(self, component):
        self.components.append(component)
        return component

35
36
37
    def enable_openmp(self, enabled):
        self.context['enableOpenMP'] = enabled

Sebastian Eibl's avatar
Sebastian Eibl committed
38
    def rename(self):
Sebastian Eibl's avatar
Sebastian Eibl committed
39
40
41
42
43
        for filename in (f for f in self.context['module_path'].glob('**/*') if f.is_file()):
            filedata = None
            #print(f'renaming module name: {filename}')
            with open(filename, encoding="utf-8") as fin:
               filedata = fin.read()
Sebastian Eibl's avatar
Sebastian Eibl committed
44

Sebastian Eibl's avatar
Sebastian Eibl committed
45
            filedata = filedata.replace('mesa_pd', self.context['name'])
Sebastian Eibl's avatar
Sebastian Eibl committed
46

Sebastian Eibl's avatar
Sebastian Eibl committed
47
48
            with open(filename, 'w', encoding="utf-8") as fout:
               fout.write(filedata)
Sebastian Eibl's avatar
Sebastian Eibl committed
49
50

    def generate(self, folder_check=True):
Sebastian Eibl's avatar
Sebastian Eibl committed
51
        print(f"This operation will overwrite the content of: {self.context['module_path']}")
Sebastian Eibl's avatar
Sebastian Eibl committed
52
53
54
55
56
57
58
        if (folder_check):
            answer = input("Continue? (y to confirm)")
            if (answer != "y"):
                return

        mesa_pd_folder = (Path(__file__).parents[2] / 'src' / 'mesa_pd').resolve()
        if (mesa_pd_folder != self.context['module_path']):
59
            if not self.context['module_path'].exists():
Sebastian Eibl's avatar
Sebastian Eibl committed
60
                self.context['module_path'].mkdir(parents=True)
Sebastian Eibl's avatar
Sebastian Eibl committed
61
62
63
64
65
66
            shutil.rmtree(self.context['module_path'])
            shutil.copytree(mesa_pd_folder, self.context['module_path'])

        for d in self.components:
            d.generate(self.context)

Sebastian Eibl's avatar
Sebastian Eibl committed
67
        self.rename()