Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gezp/xmacro

xmacro is a simple tool to define and parse XML macro.
https://github.com/gezp/xmacro

xacro xml

Last synced: 2 months ago
JSON representation

xmacro is a simple tool to define and parse XML macro.

Awesome Lists containing this project

README

        

# xmacro
![PyPI](https://img.shields.io/pypi/v/xmacro) ![](https://img.shields.io/pypi/l/xmacro) ![](https://img.shields.io/pypi/dm/xmacro)

`xmacro` is a simple tool to define and parse XML macro. it's inspired by [ros/xacro](https://github.com/ros/xacro) which is an XML macro language desiged for `urdf`. `xmacro` looks like a simplified version of `ros/xacro`, it's simpler, but it works well for xml files like `urdf` and `sdf`). in addition it's flexible, and also easy to use.

* `xmacro` is independent of ROS, you could install it by `pip` .
* XML namespace isn't used in `xmacro`, there are some reserved tags: `xmacro_include`, `xmacro_define_value`, `xmacro_define_block`, `xmacro_block`.
* it provides python api so that we could parse xml file in ROS2 launch file.

## Usage

Installation

```bash
# install by pip
pip install xmacro
# install from source code
git clone https://github.com/gezp/xmacro.git
cd xmacro && sudo python3 setup.py install
```
examples

```bash
# some examples in folder test/xmacro
xmacro test_xmacro_block.xml.xmacro > test_xmacro_block.xml
```

## XMLMacro Features

* Value macro
* Block macro
* Math expressions
* Include
* Python API

### Value macro

Value macro are named values that can be inserted anywhere into the XML document **except `` block**.

xmacro definition

```xml

```

generated xml

```xml

```

### Block macro

Define block macros with the macro tag ``, then specify the macro name and a list of parameters. The list of parameters should be whitespace separated.

The usage of block macros is to define `` which will be replaced with corresponding `` block.

xmacro definition

```xml

${m}

${m*(y*y+z*z)/12}
0
0
${m*(x*x+z*z)/12}
0
${m*(x*x+z*z)/12}

0 0 0.02 0 0 0

```

generated xml

```xml

0 0 0.02 0 0 0
0.2

0.0008333333333333335
0
0
0.002166666666666667
0
0.002166666666666667

```

* only support simple parameters (string and number), and block parameters isn't supported.
* it's supported to use other `xmacro_block` in `xmacro_define_block` which is recursive definition (the max nesting level is 5).

condition block

a example here (you could find more examples in `test/xmacro/test_xmacro_condition.xml.xmacro`)
```xml

0 0 0.02 0 0 0

```
* the `condition` can be `True`, `False`, `1`, `0`, we can also use math expression to define condition, but operator `<` and `>` isn't supported in math expression.
* if `condition` is `False` or `0`, the `xmacro_block` wou't be loaded.
* `condition` is reserved attribute of ``, so `condition` can't be used as `params` of ``.

### Math expressions

* within dollared-braces `${xxxx}`, you can also write simple math expressions.
* refer to examples of **Value macro** and **Block macro**
* it's implemented by calling `eval()` in python, so it's unsafe for some cases.

### Including other xmacro files

You can include other xmacro files by using the `` tag.

* it will include the xmcaro definition with tag `` and macros with tag ``.

```xml

```

* The uri for `file` means to open the file directly.
* it try to open the file with relative path `simple_car/model.sdf.xmacro` .
* you can also try to open file with absolute path `/simple_car/model.sdf.xmacro` with uri `file:///simple_car/model.sdf.xmacro`.
* `` supports to include recursively.

### Python API

you can use `xmacro` in python easily

```python
from xmacro.xmacro import XMLMacro

xmacro=XMLMacro()
#case1 parse from file
xmacro.set_xml_file(inputfile)
xmacro.generate()
xmacro.to_file(outputfile)

#case2 parse from xml string
xmacro.set_xml_string(xml_str)
xmacro.generate()
xmacro.to_file(outputfile)

#case3 generate to string
xmacro.set_xml_file(inputfile)
xmacro.generate()
xmacro.to_string()

#case4 custom macro value
xmacro.set_xml_file(inputfile)
# use custom dictionary to overwrite global macro value defined by
kv={"rplidar_a2_h":0.8}
xmacro.generate(kv)
xmacro.to_file(outputfile)
```

## Maintainer and License

maintainer : Zhenpeng Ge, [email protected]

`xmacro` is provided under MIT License.