• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

urbanopt / geojson-modelica-translator / 8333659328

18 Mar 2024 09:09PM UTC coverage: 89.052% (-0.04%) from 89.093%
8333659328

push

github

vtnate
change from quit to warn when loads are not found

934 of 1117 branches covered (83.62%)

Branch coverage included in aggregate %.

3 of 3 new or added lines in 1 file covered. (100.0%)

9 existing lines in 2 files now uncovered.

2588 of 2838 relevant lines covered (91.19%)

1.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.41
/geojson_modelica_translator/utils.py
1
# :copyright (c) URBANopt, Alliance for Sustainable Energy, LLC, and other contributors.
2
# See also https://github.com/urbanopt/geojson-modelica-translator/blob/develop/LICENSE.md
3

4
import logging
2✔
5
import os
2✔
6
import shutil
2✔
7
from pathlib import Path
2✔
8
from uuid import uuid4
2✔
9

10
logger = logging.getLogger(__name__)
2✔
11

12

13
def copytree(src, dst, symlinks=False, ignore=None):
2✔
14
    """
15
    Alternate version of copytree that will work if the directory already exists (use instead of shutil)
16
    """
17
    for item in os.listdir(src):
2✔
18
        s = os.path.join(src, item)
2✔
19
        d = os.path.join(dst, item)
2✔
20
        if os.path.isdir(s):
2!
UNCOV
21
            shutil.copytree(s, d, symlinks, ignore)
×
22
        else:
23
            shutil.copy2(s, d)
2✔
24

25

26
def convert_c_to_k(c):
2✔
27
    """Converts a temperature in celsius to kelvin
28

29
    :param c: float, temperature in celsius
30
    :return: float, temperature in kelvin
31
    """
32
    return c + 273.15
2✔
33

34

35
def linecount(filename: Path) -> int:
2✔
36
    """Counts the number of lines in a file
37
    Probably not the most efficient way to do this, but it works
38
    """
39
    with open(filename) as f:
2✔
40
        filelength = len(f.readlines())
2✔
41
    return filelength
2✔
42

43

44
def mbl_version():
2✔
45
    """
46
    Returns the version of the Modelica Buildings Library (MBL) used by the
47
    geojson-modelica-translator.
48
    """
49
    return "10.0.0"
2✔
50

51

52
class ModelicaPath:
2✔
53
    """
54
    Class for storing Modelica paths. This allows the path to point to
55
    the model directory, resources, and scripts directory.
56
    """
57

58
    def __init__(self, name, root_dir, overwrite=False):
2✔
59
        """
60
        Create a new modelica-based path with name of 'name'
61

62
        :param name: Name to create
63
        """
64
        self.name = name
2✔
65
        self.root_dir = root_dir
2✔
66
        self.overwrite = overwrite
2✔
67

68
        # create the directories
69
        if root_dir is not None:
2✔
70
            check_path = os.path.join(self.files_dir)
2✔
71
            self.clear_or_create_path(check_path)
2✔
72
            check_path = os.path.join(self.resources_dir)
2✔
73
            self.clear_or_create_path(check_path)
2✔
74
            check_path = os.path.join(self.scripts_dir)
2✔
75
            self.clear_or_create_path(check_path)
2✔
76

77
    def clear_or_create_path(self, path):
2✔
78
        if os.path.exists(path):
2!
UNCOV
79
            if not self.overwrite:
×
UNCOV
80
                raise Exception("Directory already exists and overwrite is false for %s" % path)
×
81
            else:
UNCOV
82
                shutil.rmtree(path)
×
83
        os.makedirs(path, exist_ok=True)
2✔
84

85
    @property
2✔
86
    def files_dir(self):
2✔
87
        """
88
        Return the path to the files (models) for the specified ModelicaPath. This path does not include the
89
        trailing slash.
90

91
        :return: string, path to where files (models) are stored, without trailing slash
92
        """
93
        if self.root_dir is None:
2✔
94
            return self.files_relative_dir
2✔
95
        else:
96
            return f"{self.root_dir}/{self.name}"
2✔
97

98
    @property
2✔
99
    def resources_relative_dir(self):
2✔
100
        """
101
        Return the relative resource directory instead of the full path. This is useful when replacing
102
        strings within modelica files which are relative to the package.
103

104
        :return: string, relative resource's data path
105
        """
106
        return f"Resources/Data/{self.name}"
2✔
107

108
    @property
2✔
109
    def scripts_relative_dir(self, platform="Dymola"):  # noqa: PLR0206
2✔
110
        # FIXME: https://docs.astral.sh/ruff/rules/property-with-parameters/
111
        """Return the scripts directory that is in the resources directory. This only returns the
112
        relative directory and is useful when replacing string values within Modelica files.
113

114
        :return: string, relative scripts path
115
        """
116
        return f"Resources/Scripts/{self.name}/{platform}"
2✔
117

118
    @property
2✔
119
    def files_relative_dir(self):
2✔
120
        """Return the path to the files relative to the project name."""
121
        return os.path.join(self.name)
2✔
122

123
    @property
2✔
124
    def resources_dir(self):
2✔
125
        """
126
        Return the path to the resources directory for the specified ModelicaPath. This path does not include
127
        the trailing slash.
128

129
        :return: string, path to where resources are stored, without trailing slash.
130
        """
131
        if self.root_dir is None:
2✔
132
            return self.resources_relative_dir
2✔
133
        else:
134
            return f"{self.root_dir}/{self.resources_relative_dir}"
2✔
135

136
    @property
2✔
137
    def scripts_dir(self):
2✔
138
        """
139
        Return the path to the scripts directory (in the resources dir) for the specified ModelicaPath.
140
        This path does not include the trailing slash.
141

142
        :return: string, path to where scripts are stored, without trailing slash.
143
        """
144
        if self.root_dir is None:
2!
UNCOV
145
            return self.scripts_relative_dir
×
146
        else:
147
            return f"{self.root_dir}/{self.scripts_relative_dir}"
2✔
148

149

150
# This is used for some test cases where we need deterministic IDs to be generated
151
USE_DETERMINISTIC_ID = bool(os.environ.get("GMT_DETERMINISTIC_ID", False))
2✔
152

153
counter = 0
2✔
154

155

156
def simple_uuid():
2✔
157
    """Generates a simple string uuid
158

159
    :return: string, uuid
160
    """
161
    global counter  # noqa: PLW0603
162

163
    if not USE_DETERMINISTIC_ID:
2!
164
        return str(uuid4()).split("-")[0]
2✔
165
    else:
UNCOV
166
        string_id = str(counter)
×
UNCOV
167
        counter += 1
×
UNCOV
168
        return string_id
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc