Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix float classes #34

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions CMakeLists.txt

This file was deleted.

11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ Python tool for code generation from CIM data model for several programming lang

```bash
pip install -e .
cimgen --outdir=output/cpp/CGMES_2.4.15_27JAN2020 --schemadir=cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=cpp
cimgen --outdir=output/cpp/CGMES_2.4.15_27JAN2020 --schemadir=cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=cpp --cgmes_version=cgmes_v2_4_15
```

This will build version `CGMES_2.4.15_27JAN2020` in the subfolder `CGMES_2.4.15_27JAN2020_cpp`.
This will build version `CGMES_2.4.15_27JAN2020` in the subfolder `output/cpp/CGMES_2.4.15_27JAN2020`.

If you wish to build an alternative version, you can see available options in the subfolder called `cgmes_schema`.
For the schema `CGMES_3.0.0` you have to use the option `--cgmes_version=cgmes_v3_0_0`.

#### Generating C++ files in a Docker container

```bash
docker build --tag cimgen --file Dockerfile .
docker run --volume "$(pwd)/output:/output" cimgen --outdir=/output/cpp/CGMES_2.4.15_27JAN2020 --schemadir=/cimgen/cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=cpp
docker run --volume "$(pwd)/output:/output" cimgen --outdir=/output/cpp/CGMES_2.4.15_27JAN2020 --schemadir=/cimgen/cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=cpp --cgmes_version=cgmes_v2_4_15
```

### Generating Python files
Expand All @@ -34,7 +35,7 @@ docker run --volume "$(pwd)/output:/output" cimgen --outdir=/output/cpp/CGMES_2.

```bash
pip install -e .
cimgen --outdir=output/python/CGMES_2.4.15_27JAN2020 --schemadir=cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=python
cimgen --outdir=output/python/CGMES_2.4.15_27JAN2020 --schemadir=cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=python --cgmes_version=cgmes_v2_4_15
```

`outdir` can be set to whichever absolute path you wish to create the files in.
Expand All @@ -43,7 +44,7 @@ cimgen --outdir=output/python/CGMES_2.4.15_27JAN2020 --schemadir=cgmes_schema/CG

```bash
docker build --tag cimgen --file Dockerfile .
docker run --volume "$(pwd)/output:/output" cimgen --outdir=/output/python/CGMES_2.4.15_27JAN2020 --schemadir=/cimgen/cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=python
docker run --volume "$(pwd)/output:/output" cimgen --outdir=/output/python/CGMES_2.4.15_27JAN2020 --schemadir=/cimgen/cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=python --cgmes_version=cgmes_v2_4_15
```

## License
Expand Down
9 changes: 7 additions & 2 deletions cimgen/cimgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def _get_rid_of_hash(name):

class CIMComponentDefinition:
def __init__(self, rdfsEntry):
self.about = rdfsEntry.about()
self.attribute_list = []
self.comment = rdfsEntry.comment()
self.instance_list = []
Expand Down Expand Up @@ -272,6 +273,8 @@ def _simple_float_attribute(attr):
return False

def is_a_float(self):
if self.about == "Float":
return True
simple_float = False
for attr in self.attribute_list:
if CIMComponentDefinition._simple_float_attribute(attr):
Expand All @@ -283,11 +286,12 @@ def is_a_float(self):
return True

candidate_array = {"value": False, "unit": False, "multiplier": False}
optional_attributes = ["denominatorUnit", "denominatorMultiplier"]
for attr in self.attribute_list:
key = attr["label"]
if key in candidate_array:
candidate_array[key] = True
else:
elif key not in optional_attributes:
return False
for key in candidate_array:
if not candidate_array[key]:
Expand Down Expand Up @@ -522,6 +526,7 @@ def _write_python_files(elem_dict, lang_pack, output_path, version):
initial_indent="",
subsequent_indent=" " * 6,
)
class_details["attributes"].sort(key=lambda d: d["label"])
_write_files(class_details, output_path, version)


Expand Down Expand Up @@ -717,7 +722,7 @@ def cim_generate(directory, output_path, version, lang_pack):
t0 = time()

# iterate over files in the directory and check if they are RDF files
for file in os.listdir(directory):
for file in sorted(os.listdir(directory)):
if file.endswith(".rdf"):
logger.info('Start of parsing file "%s"', file)

Expand Down
9 changes: 5 additions & 4 deletions cimgen/languages/cpp/lang_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def run_template(outputPath, class_details):
for template_info in templates:
class_file = os.path.join(outputPath, class_details["class_name"] + template_info["ext"])
if not os.path.exists(class_file):
with open(class_file, "w") as file:
with open(class_file, "w", encoding="utf-8") as file:
templates = files("cimgen.languages.cpp.templates")
with templates.joinpath(template_info["filename"]).open() as f:
with templates.joinpath(template_info["filename"]).open(encoding="utf-8") as f:
args = {
"data": class_details,
"template": f,
Expand Down Expand Up @@ -432,6 +432,8 @@ def set_default(dataType):
return "''"
elif dataType == "Boolean":
return "false"
elif dataType == "Float":
return "0.0"
else:
return "nullptr"

Expand Down Expand Up @@ -474,13 +476,12 @@ def _create_header_include_file(directory, header_include_filename, header, foot

lines = []

for filename in os.listdir(directory):
for filename in sorted(os.listdir(directory)):
filepath = os.path.join(directory, filename)
basepath, ext = os.path.splitext(filepath)
basename = os.path.basename(basepath)
if ext == ".hpp" and not _is_enum_class(filepath) and basename not in blacklist:
lines.append(before + basename + after)
lines.sort()
for line in lines:
header.append(line)
for line in footer:
Expand Down
4 changes: 2 additions & 2 deletions cimgen/languages/cpp/templates/cpp_object_template.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ using namespace CIMPP;
{{class_name}}::~{{class_name}}() {};

{{#attributes}}
{{#langPack.create_class_assign}}{{.}}{{/langPack.create_class_assign}}
{{#langPack.create_assign}}{{.}}{{/langPack.create_assign}}
{{/attributes}}

{{#attributes}}
{{#langPack.create_assign}}{{.}}{{/langPack.create_assign}}
{{#langPack.create_class_assign}}{{.}}{{/langPack.create_class_assign}}
{{/attributes}}

namespace CIMPP {
Expand Down
7 changes: 3 additions & 4 deletions cimgen/languages/java/lang_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def run_template(outputPath, class_details):
for template_info in templates:
class_file = os.path.join(outputPath, class_details["class_name"] + template_info["ext"])
if not os.path.exists(class_file):
with open(class_file, "w") as file:
with open(class_file, "w", encoding="utf-8") as file:
class_details["setDefault"] = _set_default
templates = files("cimgen.languages.java.templates")
with templates.joinpath(template_info["filename"]).open() as f:
with templates.joinpath(template_info["filename"]).open(encoding="utf-8") as f:
args = {
"data": class_details,
"template": f,
Expand Down Expand Up @@ -400,13 +400,12 @@ def _create_header_include_file(directory, header_include_filename, header, foot

lines = []

for filename in os.listdir(directory):
for filename in sorted(os.listdir(directory)):
filepath = os.path.join(directory, filename)
basepath, ext = os.path.splitext(filepath)
basename = os.path.basename(basepath)
if ext == ".java" and not _is_enum_class(filepath) and basename not in blacklist:
lines.append(before + 'Map.entry("' + basename + '", new cim4j.' + basename + after + "),\n")
lines.sort()
lines[-1] = lines[-1].replace("),", ")")
for line in lines:
header.append(line)
Expand Down
4 changes: 2 additions & 2 deletions cimgen/languages/javascript/lang_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ def run_template(outputPath, class_details):

def write_templated_file(class_file, class_details, template_filename):
if not os.path.exists(class_file):
with open(class_file, "w") as file:
with open(class_file, "w", encoding="utf-8") as file:
templates = files("cimgen.languages.javascript.templates")
with templates.joinpath(template_filename).open() as f:
with templates.joinpath(template_filename).open(encoding="utf-8") as f:
args = {"data": class_details, "template": f, "partials_dict": partials}
output = chevron.render(**args)
file.write(output)
Expand Down
12 changes: 6 additions & 6 deletions cimgen/languages/python/lang_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ def run_template(version_path, class_details):
for template_info in template_files:
class_file = os.path.join(version_path, class_details["class_name"] + template_info["ext"])
if not os.path.exists(class_file):
with open(class_file, "w") as file:
with open(class_file, "w", encoding="utf-8") as file:
class_details["setDefault"] = _set_default
templates = files("cimgen.languages.python.templates")
with templates.joinpath(template_info["filename"]).open() as f:
with templates.joinpath(template_info["filename"]).open(encoding="utf-8") as f:
args = {
"data": class_details,
"template": f,
Expand All @@ -95,7 +95,7 @@ def run_template(version_path, class_details):

def _create_init(path):
init_file = path + "/__init__.py"
with open(init_file, "w"):
with open(init_file, "w", encoding="utf-8"):
pass


Expand All @@ -118,17 +118,17 @@ def _create_base(path):
" return dict\n",
]

with open(base_path, "w") as f:
with open(base_path, "w", encoding="utf-8") as f:
for line in base:
f.write(line)


def resolve_headers(path):
filenames = glob.glob(path + "/*.py")
include_names = []
for filename in filenames:
for filename in sorted(filenames):
include_names.append(os.path.splitext(os.path.basename(filename))[0])
with open(path + "/__init__.py", "w") as header_file:
with open(path + "/__init__.py", "w", encoding="utf-8") as header_file:
for include_name in include_names:
header_file.write("from " + "." + include_name + " import " + include_name + " as " + include_name + "\n")
header_file.close()
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ keywords = ["cim", "cgmes", "code-generation"]
dependencies = [
"xmltodict >= 0.13.0, < 1",
"chevron >= 0.14.0, < 1",
"cmake >= 3.27.0, < 4",
"pydantic < 2",
"beautifulsoup4 >= 4.12.2, < 5"
]
Expand Down