-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wpiutil] DataLog: Use codegen for entry types
Signed-off-by: Jade Turner <[email protected]>
- Loading branch information
1 parent
de82ed4
commit 44c1538
Showing
17 changed files
with
487 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright (c) FIRST and other WPILib contributors. | ||
# Open Source Software; you can modify and/or share it under the terms of | ||
# the WPILib BSD license file in the root directory of this project. | ||
import argparse | ||
import json | ||
import sys | ||
from pathlib import Path | ||
from typing import Any, Dict | ||
|
||
from jinja2 import Environment, FileSystemLoader | ||
from jinja2.environment import Template | ||
|
||
|
||
def render_template( | ||
template: Template, output_dir: Path, filename: str, ty: Dict[str, Any] | ||
): | ||
output_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
output_file = output_dir / filename | ||
output_file.write_text(template.render(ty), encoding="utf-8") | ||
|
||
|
||
def generate_log_entries(output_root, template_root): | ||
with (template_root / "types.json").open(encoding="utf-8") as f: | ||
types = json.load(f)["types"] | ||
|
||
env = Environment( | ||
loader=FileSystemLoader(str(template_root)), | ||
autoescape=False, | ||
keep_trailing_newline=True, | ||
) | ||
|
||
java_root_path = Path(output_root) / "java/edu/wpi/first/util/datalog" | ||
java_template = env.get_template("LogEntry.java.jinja") | ||
|
||
for ty in types: | ||
entry_name = f"{ty['name']}LogEntry.java" | ||
render_template(java_template, java_root_path, entry_name, ty) | ||
|
||
|
||
def main(argv): | ||
script_path = Path(__file__).resolve() | ||
dirname = script_path.parent | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--output_directory", | ||
help="Optional. If set, will output the generated files to this directory, otherwise it will use a path relative to the script", | ||
default=dirname / "src/main/generated", | ||
type=Path, | ||
) | ||
parser.add_argument( | ||
"--template_root", | ||
help="Optional. If set, will use this directory as the root for the jinja templates", | ||
default=dirname / "src/main/generate", | ||
type=Path, | ||
) | ||
args = parser.parse_args(argv) | ||
|
||
generate_log_entries(args.output_directory, args.template_root) | ||
|
||
|
||
if __name__ == "__main__": | ||
main(sys.argv[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
// THIS FILE WAS GENERATED BY generate_datalog_entries.py DO NOT EDIT IT MANUALLY | ||
|
||
package edu.wpi.first.util.datalog; | ||
|
||
{% if IsArray %} | ||
import java.util.Arrays; | ||
{% endif %} | ||
|
||
/** Log {{ DataType }} values. */ | ||
public class {{ name }}LogEntry extends DataLogEntry { | ||
/** The data type for {{ DataType }} values. */ | ||
public static final String kDataType = "{{ DataType }}"; | ||
|
||
/** | ||
* Constructs a {{ DataType }} log entry. | ||
* | ||
* @param log datalog | ||
* @param name name of the entry | ||
* @param metadata metadata | ||
* @param timestamp entry creation timestamp (0=now) | ||
*/ | ||
public {{ name }}LogEntry(DataLog log, String name, String metadata, long timestamp) { | ||
super(log, name, kDataType, metadata, timestamp); | ||
} | ||
|
||
/** | ||
* Constructs a {{ DataType }} log entry. | ||
* | ||
* @param log datalog | ||
* @param name name of the entry | ||
* @param metadata metadata | ||
*/ | ||
public {{ name }}LogEntry(DataLog log, String name, String metadata) { | ||
this(log, name, metadata, 0); | ||
} | ||
|
||
/** | ||
* Constructs a {{ DataType }} log entry. | ||
* | ||
* @param log datalog | ||
* @param name name of the entry | ||
* @param timestamp entry creation timestamp (0=now) | ||
*/ | ||
public {{ name }}LogEntry(DataLog log, String name, long timestamp) { | ||
this(log, name, "", timestamp); | ||
} | ||
|
||
/** | ||
* Constructs a {{ DataType }} log entry. | ||
* | ||
* @param log datalog | ||
* @param name name of the entry | ||
*/ | ||
public {{ name }}LogEntry(DataLog log, String name) { | ||
this(log, name, 0); | ||
} | ||
|
||
/** | ||
* Appends a record to the log. | ||
* | ||
* @param value Value to record | ||
* @param timestamp Time stamp (0 to indicate now) | ||
*/ | ||
public void append({{ AppendType }} value, long timestamp) { | ||
m_log.append{{ name }}(m_entry, value, timestamp); | ||
} | ||
|
||
/** | ||
* Appends a record to the log. | ||
* | ||
* @param value Value to record | ||
*/ | ||
public void append({{ AppendType }} value) { | ||
m_log.append{{ name }}(m_entry, value, 0); | ||
} | ||
|
||
{% if IsArray %} | ||
/** | ||
* Updates the last value and appends a record to the log if it has changed. | ||
* | ||
* <p>Note: the last value is local to this class instance; using update() with two instances | ||
* pointing to the same underlying log entry name will likely result in unexpected results. | ||
* | ||
* @param value Value to record | ||
* @param timestamp Time stamp (0 to indicate now) | ||
*/ | ||
public synchronized void update({{ AppendType }} value, long timestamp) { | ||
if (!equalsLast(value)) { | ||
copyToLast(value); | ||
append(value, timestamp); | ||
} | ||
} | ||
|
||
/** | ||
* Updates the last value and appends a record to the log if it has changed. | ||
* | ||
* <p>Note: the last value is local to this class instance; using update() with two instances | ||
* pointing to the same underlying log entry name will likely result in unexpected results. | ||
* | ||
* @param value Value to record | ||
*/ | ||
public void update({{ AppendType }} value) { | ||
update(value, 0); | ||
} | ||
|
||
/** | ||
* Gets whether there is a last value. | ||
* | ||
* <p>Note: the last value is local to this class instance and updated only with update(), not | ||
* append(). | ||
* | ||
* @return True if last value exists, false otherwise. | ||
*/ | ||
public synchronized boolean hasLastValue() { | ||
return m_lastValue != null; | ||
} | ||
|
||
/** | ||
* Gets the last value. | ||
* | ||
* <p>Note: the last value is local to this class instance and updated only with update(), not | ||
* append(). | ||
* | ||
* @return Last value, or false if none. | ||
*/ | ||
@SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull") | ||
public synchronized {{ AppendType }} getLastValue() { | ||
if (m_lastValue == null) { | ||
return null; | ||
} | ||
return Arrays.copyOf(m_lastValue, m_lastValueLen); | ||
} | ||
|
||
private boolean equalsLast({{ AppendType }} value) { | ||
if (m_lastValue == null || m_lastValueLen != value.length) { | ||
return false; | ||
} | ||
return Arrays.equals(m_lastValue, 0, value.length, value, 0, value.length); | ||
} | ||
|
||
private void copyToLast({{ AppendType }} value) { | ||
if (m_lastValue == null || m_lastValue.length < value.length) { | ||
m_lastValue = Arrays.copyOf(value, value.length); | ||
} else { | ||
System.arraycopy(value, 0, m_lastValue, 0, value.length); | ||
} | ||
m_lastValueLen = value.length; | ||
} | ||
|
||
private int m_lastValueLen; | ||
{% else %} | ||
/** | ||
* Updates the last value and appends a record to the log if it has changed. | ||
* | ||
* <p>Note: the last value is local to this class instance; using update() with two instances | ||
* pointing to the same underlying log entry name will likely result in unexpected results. | ||
* | ||
* @param value Value to record | ||
* @param timestamp Time stamp (0 to indicate now) | ||
*/ | ||
public synchronized void update({{ AppendType }} value, long timestamp) { | ||
{%- if AppendType == "String" %} | ||
if (!m_hasLastValue || !m_lastValue.equals(value)) { | ||
{%- else %} | ||
if (!m_hasLastValue || m_lastValue != value) { | ||
{%- endif %} | ||
m_lastValue = value; | ||
m_hasLastValue = true; | ||
append(value, timestamp); | ||
} | ||
} | ||
|
||
/** | ||
* Updates the last value and appends a record to the log if it has changed. | ||
* | ||
* <p>Note: the last value is local to this class instance; using update() with two instances | ||
* pointing to the same underlying log entry name will likely result in unexpected results. | ||
* | ||
* @param value Value to record | ||
*/ | ||
public void update({{ AppendType }} value) { | ||
update(value, 0); | ||
} | ||
|
||
/** | ||
* Gets whether there is a last value. | ||
* | ||
* <p>Note: the last value is local to this class instance and updated only with update(), not | ||
* append(). | ||
* | ||
* @return True if last value exists, false otherwise. | ||
*/ | ||
public synchronized boolean hasLastValue() { | ||
return m_hasLastValue; | ||
} | ||
|
||
/** | ||
* Gets the last value. | ||
* | ||
* <p>Note: the last value is local to this class instance and updated only with update(), not | ||
* append(). | ||
* | ||
* @return Last value, or false if none. | ||
*/ | ||
public synchronized {{ AppendType }} getLastValue() { | ||
return m_lastValue; | ||
} | ||
|
||
private boolean m_hasLastValue; | ||
{% endif %} | ||
|
||
private {{ AppendType }} m_lastValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"types": [ | ||
{ | ||
"name": "Double", | ||
"DataType": "double", | ||
"AppendType": "double", | ||
"IsArray": false | ||
}, | ||
{ | ||
"name": "DoubleArray", | ||
"DataType": "double[]", | ||
"AppendType": "double[]", | ||
"IsArray": true | ||
}, | ||
{ | ||
"name": "Float", | ||
"DataType": "float", | ||
"AppendType": "float", | ||
"IsArray": false | ||
}, | ||
{ | ||
"name": "FloatArray", | ||
"DataType": "float[]", | ||
"AppendType": "float[]", | ||
"IsArray": true | ||
}, | ||
{ | ||
"name": "Integer", | ||
"DataType": "int64", | ||
"AppendType": "long", | ||
"IsArray": false | ||
}, | ||
{ | ||
"name": "IntegerArray", | ||
"DataType": "int64[]", | ||
"AppendType": "long[]", | ||
"IsArray": true | ||
}, | ||
{ | ||
"name": "Boolean", | ||
"DataType": "boolean", | ||
"AppendType": "boolean", | ||
"IsArray": false | ||
}, | ||
{ | ||
"name": "BooleanArray", | ||
"DataType": "boolean[]", | ||
"AppendType": "boolean[]", | ||
"IsArray": true | ||
}, | ||
{ | ||
"name": "String", | ||
"DataType": "String", | ||
"AppendType": "String", | ||
"IsArray": false | ||
}, | ||
{ | ||
"name": "StringArray", | ||
"DataType": "string[]", | ||
"AppendType": "String[]", | ||
"IsArray": true | ||
} | ||
] | ||
} |
Oops, something went wrong.