Universal CLI tool for editing SVG, HTML, and XML files using XPath and CSS selectors
Project description
xqr
xpath xquery xqr
๐ ๏ธ Universal File Editor
Powerful CLI tool for editing SVG, HTML, and XML files using XPath and CSS selectors. Edit your structured documents directly from the command line or through a web interface.
๐ Features
- Multiple File Formats: SVG, HTML, XML support with automatic format detection
- XPath Queries: Full XPath 1.0 support for precise element selection
- CSS Selectors: CSS selector support for HTML files
- Multiple Interfaces: CLI commands, interactive shell, and web server
- REST API: Programmatic access via HTTP endpoints
- Batch Processing: Automate edits across multiple files
- Backup System: Automatic backup creation before modifications
๐ฆ Installation
Using Poetry (Recommended)
# Clone the repository
git clone https://github.com/yourusername/universal-file-editor.git
cd universal-file-editor
# Install with Poetry
poetry install
# Activate the environment
poetry shell
Using pip
pip install universal-file-editor
๐ฏ Quick Start
1. Create Example Files
file-editor examples
2. Basic Usage
# Load and query a file
file-editor load example.svg
file-editor query "//text[@id='text1']"
# Update content
file-editor set "//text[@id='text1']" "New Content"
file-editor save
3. Interactive Shell
file-editor shell
๐ > load example.html
๐ > query //title
๐ > set //title "Updated Title"
๐ > save
๐ > exit
4. Web Interface
file-editor server --port 8080
# Open http://localhost:8080 in your browser
๐ Usage Examples
SVG Files
# Update text elements
file-editor set "//text[@id='title']" "New Chart Title"
# Change colors
file-editor set "//rect[@id='bar1']" "blue" --type attribute --attr fill
# Update metadata
file-editor set "//metadata/description" "Updated chart description"
HTML Files
# Update page title
file-editor set "//title" "New Page Title"
# Change meta description
file-editor set "//meta[@name='description']" "New description" --type attribute --attr content
# Update content by CSS selector (in shell mode)
query #main-heading
set #main-heading "Welcome to Our Site"
XML Data Files
# Update configuration values
file-editor set "//setting[@name='timeout']" "60" --type attribute --attr value
# Modify data records
file-editor set "//record[@id='1']/email" "newemail@example.com"
# Update metadata
file-editor set "//metadata/version" "2.0"
๐ง Advanced Features
Batch Processing
#!/bin/bash
# Update multiple files
for file in *.svg; do
file-editor load "$file"
file-editor set "//metadata/updated" "$(date)"
file-editor save
done
REST API Usage
# Load file
curl -X POST http://localhost:8080/api/load \
-H "Content-Type: application/json" \
-d '{"file_path": "example.svg"}'
# Query elements
curl -X POST http://localhost:8080/api/query \
-H "Content-Type: application/json" \
-d '{"query": "//text[@id=\"title\"]", "type": "xpath"}'
# Update element
curl -X POST http://localhost:8080/api/update \
-H "Content-Type: application/json" \
-d '{"xpath": "//text[@id=\"title\"]", "type": "text", "value": "New Title"}'
XPath Examples
# Find elements by ID
//element[@id='myid']
# Find elements by attribute value
//rect[@fill='red']
# Find elements containing text
//text[contains(., 'Hello')]
# Find elements by position
//record[position()=1]
# Find elements with specific child
//record[email='john@example.com']
CSS Selector Examples (HTML only)
# By ID
#main-title
# By class
.navigation-item
# By attribute
input[type='text']
# Descendant selectors
div.content p
# Pseudo-selectors
li:first-child
๐๏ธ Project Structure
universal-file-editor/
โโโ pyproject.toml # Poetry configuration
โโโ README.md # This file
โโโ file_editor/ # Main package
โ โโโ __init__.py # Package initialization
โ โโโ core.py # Core FileEditor class
โ โโโ cli.py # Command-line interface
โ โโโ server.py # HTTP server
โ โโโ examples.py # Example file generator
โโโ tests/ # Test suite
โโโ __init__.py
โโโ test_core.py
โโโ test_cli.py
โโโ test_server.py
๐งช Development
Setting up Development Environment
# Clone repository
git clone https://github.com/yourusername/universal-file-editor.git
cd universal-file-editor
# Install with development dependencies
poetry install
# Install pre-commit hooks
pre-commit install
# Run tests
poetry run pytest
# Run with coverage
poetry run pytest --cov=file_editor
# Format code
poetry run black file_editor/
# Type checking
poetry run mypy file_editor/
Running Tests
# Run all tests
poetry run pytest
# Run specific test file
poetry run pytest tests/test_core.py
# Run with verbose output
poetry run pytest -v
# Run with coverage report
poetry run pytest --cov=file_editor --cov-report=html
๐ Requirements
- Python: 3.8+
- lxml: For XPath support (automatically installed)
- beautifulsoup4: For CSS selector support (automatically installed)
๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Run the test suite (
poetry run pytest) - Format your code (
poetry run black file_editor/) - Commit your changes (
git commit -am 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Links
- Homepage: https://github.com/yourusername/universal-file-editor
- Documentation: https://github.com/yourusername/universal-file-editor#readme
- Issues: https://github.com/yourusername/universal-file-editor/issues
- PyPI: https://pypi.org/project/universal-file-editor/
๐ Why Universal File Editor?
Traditional file editing requires specialized tools for each format:
- SVG files โ Inkscape, Adobe Illustrator
- HTML files โ Web browsers, text editors
- XML files โ XML editors, IDEs
Universal File Editor provides a single, consistent interface for all structured document formats, enabling:
- Automation: Script repetitive edits across thousands of files
- Integration: Embed in CI/CD pipelines and build processes
- Consistency: Use the same XPath/CSS knowledge across all formats
- Accessibility: No specialized software required - works anywhere Python runs
Perfect for developers, system administrators, and content managers who work with structured data files.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xqr-0.1.0.tar.gz.
File metadata
- Download URL: xqr-0.1.0.tar.gz
- Upload date:
- Size: 20.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.15.3-200.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb081c3c3a4c311c055498965d4c6630ba86032706399e2ca2c018011b2081b2
|
|
| MD5 |
dcb3badbb604f31039c0242c04f208a0
|
|
| BLAKE2b-256 |
931e2ed74695060bc577268d085b843f3223c2f0555b7f45dbf5c6ea21e58ceb
|
File details
Details for the file xqr-0.1.0-py3-none-any.whl.
File metadata
- Download URL: xqr-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.15.3-200.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc980ee702e601dec07862c8e72f0781adb0a6de7d86dfde725b1f763cf845ae
|
|
| MD5 |
21b2741d2be9e6b206d80f4e5e8d4927
|
|
| BLAKE2b-256 |
e14f72b2abcc54673ceec7bbbc2e0f1e7ea1eeb153cd8d77ad60a111d89fa400
|