A Python application for uploading text segments, table of contents, and commentary mappings to the WebBuddhist API. This project provides tools for processing and uploading Buddhist texts with their associated metadata and structure.
- Features
- Project Structure
- Prerequisites
- Installation
- Configuration
- Usage
- File Structure Requirements
- Testing
- Logging
- API Endpoints
- Troubleshooting
- Segment Upload: Upload text segments to WebBuddhist API
- Table of Contents Upload: Upload structured TOC with segment references
- Text Mapping: Map commentary texts to root texts
- Metadata Upload: Upload text metadata and information
- Comprehensive Testing: Full test suite with mocking
- Logging: Detailed logging for all operations
- Error Handling: Robust error handling and validation
payload_generator_pecha/
βββ README.md # This file
βββ requirements.txt # Python dependencies
βββ .venv/ # Virtual environment
βββ utils.py # Utility functions
βββ segment_uploader_webuddhist.py # Main segment uploader
βββ toc_uploader_webuddhist.py # Table of contents uploader
βββ src/
β βββ metadata_uploader.py # Metadata uploader
βββ mapping/
β βββ text_mapping.py # Text mapping functionality
β βββ mapping_models.py # Data models for mapping
β βββ mapping_data/ # Mapping data files
βββ test/
β βββ test_segment_uploader_webuddhist.py # Test suite
β βββ data/ # Test data files
β βββ dummy_segment_upload_payload.json
β βββ dummy_api_response.json
β βββ dummy_segment_content_with_segment_id.json
βββ data/
β βββ metadata.json # Project metadata
βββ [text_name]/ # Text-specific directories
β βββ [text_name]_payload/ # Payload files
β β βββ [text_name]_root_text_segment_payload.json
β β βββ [text_name]_root_text_toc_payload.json
β β βββ [text_name]_commentary_[1-3]_text_segment_payload.json
β βββ [text_name]_api_response/ # API response files
β βββ [text_name]_segment_content_with_segment_id.json
βββ pecha_segment_upload_payload/ # Pre-generated payloads
βββ choejuk_root_text.json
βββ choejuk_commentary_1.json
βββ ... (other text files)
- Python 3.12 or higher
- Virtual environment support
- Internet connection for API calls
- WebBuddhist API credentials
git clone <repository-url>
cd payload_generator_pecha
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -r requirements.txt
If requirements.txt
doesn't exist, install manually:
pip install requests pytest Levenshtein pydantic
python -m pytest --version
python -c "import requests; print('Dependencies installed successfully')"
The application uses environment variables for configuration to keep sensitive information secure.
# Copy the template file
cp env.template .env
# Edit the .env file with your actual values
nano .env # or use your preferred editor
# WebBuddhist API Configuration
WEBUDDHIST_API_BASE_URL=https://your-api-domain.com
WEBUDDHIST_SEGMENTS_ENDPOINT=/api/v1/segments
WEBUDDHIST_TOC_ENDPOINT=/api/v1/texts/table-of-content
WEBUDDHIST_AUTH_ENDPOINT=/api/v1/auth/login
# Optional: Pre-configure credentials (not recommended for production)
# [email protected]
# WEBUDDHIST_PASSWORD=your-password
# Environment
ENVIRONMENT=development
LOG_LEVEL=INFO
- Never commit your
.env
file to version control - The
.env
file is already in.gitignore
- For production, use proper secret management systems
- If credentials are not in
.env
, the application will prompt for them
You'll need WebBuddhist API credentials. You can either:
- Set them in
.env
file (not recommended for production) - Enter them when prompted (recommended for security)
Upload text segments to the WebBuddhist API.
python segment_uploader_webuddhist.py
- Text name: Enter the name of your text (e.g.,
heart_sutra
,diamond_cutter
) - Root or commentary: Enter
root
orcommentary_1
,commentary_2
,commentary_3
- Email/Password: Enter your WebBuddhist API credentials
[text_name]/
βββ [text_name]_payload/
β βββ [text_name]_[root_or_commentary]_text_segment_payload.json
βββ [text_name]_api_response/ # Created automatically
βββ [text_name]_[root_or_commentary]_segment_content_with_segment_id.json
$ python segment_uploader_webuddhist.py
Enter the text name: heart_sutra
Enter the root or commentary_[1,2,3]: root
Enter your email: [email protected]
Enter your password: ********
This will:
- Read
heart_sutra/heart_sutra_payload/heart_sutra_root_text_segment_payload.json
- Upload segments to the API
- Save response to
heart_sutra/heart_sutra_api_response/heart_sutra_root_segment_content_with_segment_id.json
- Log activities to
segment_upload_log.txt
Upload table of contents with segment references.
python toc_uploader_webuddhist.py
- Segments must be uploaded first (requires segment ID mapping file)
- TOC payload file must exist
[text_name]/
βββ [text_name]_payload/
β βββ [text_name]_[root_or_commentary]_text_toc_payload.json
βββ [text_name]_api_response/
βββ [text_name]_[root_or_commentary]_segment_content_with_segment_id.json
Map commentary texts to root texts.
python mapping/text_mapping.py
Upload text metadata.
python src/metadata_uploader.py
{
"text_id": "uuid-string",
"segments": [
{
"content": "Text content here\n",
"type": "source",
"mapping": []
}
]
}
{
"text_id": "uuid-string",
"sections": [
{
"title": "Chapter 1",
"segments": [
{
"segment_id": "segment-content-to-match"
}
]
}
]
}
# Activate virtual environment first
source .venv/bin/activate
# Run tests with pytest
python -m pytest
# Run with verbose output
python -m pytest -v
# Run specific test file
python -m pytest test/test_segment_uploader_webuddhist.py -v
python -m pytest --cov=segment_uploader_webuddhist test/
The test suite includes:
- 16 comprehensive test cases
- Mock external dependencies (API calls, file I/O, user input)
- Error handling tests (network errors, file permissions, invalid data)
- Edge case testing (empty data, malformed responses)
- Integration tests (full workflow testing)
Test data is stored in test/data/
:
dummy_segment_upload_payload.json
- Sample input payloaddummy_api_response.json
- Mock API responsedummy_segment_content_with_segment_id.json
- Expected output format
segment_upload_log.txt
- Segment upload activitiestoc_upload_log.txt
- TOC upload activities
2024-01-01 12:00:00,000 [INFO] Uploading segments to webuddhist
2024-01-01 12:00:01,000 [INFO] Segments uploaded, 200
2024-01-01 12:00:01,100 [INFO] Segments uploaded successfully for text_id: uuid-string
The application uses configurable API endpoints defined in your .env
file:
- URL:
{WEBUDDHIST_API_BASE_URL}{WEBUDDHIST_SEGMENTS_ENDPOINT}
- Method: POST
- Headers:
Authorization: Bearer <token>
- Body: JSON payload with segments
- URL:
{WEBUDDHIST_API_BASE_URL}{WEBUDDHIST_TOC_ENDPOINT}
- Method: POST
- Headers:
Authorization: Bearer <token>
- Body: JSON payload with TOC structure
- URL:
{WEBUDDHIST_API_BASE_URL}{WEBUDDHIST_AUTH_ENDPOINT}
- Method: POST
- Body: JSON with email and password
ModuleNotFoundError: No module named 'segment_uploader_webuddhist'
Solution: Make sure virtual environment is activated and you're in the project root:
source .venv/bin/activate
cd /path/to/payload_generator_pecha
python -m pytest
FileNotFoundError: [Errno 2] No such file or directory: 'text_name/text_name_payload/...'
Solution: Ensure your file structure matches the expected format:
- Create the text directory:
mkdir [text_name]
- Create payload directory:
mkdir [text_name]/[text_name]_payload
- Place your JSON payload file in the correct location
401 Unauthorized
Solution:
- Verify your WebBuddhist API credentials
- Check if your account has the necessary permissions
- Ensure the API endpoints are correct
ConnectionError: Failed to establish a new connection
Solution:
- Check your internet connection
- Verify the API endpoints are accessible
- Check if there are any firewall restrictions
For detailed debugging, you can modify the logging level in the scripts:
logging.basicConfig(level=logging.DEBUG)
Use the test suite to verify functionality without making actual API calls:
python -m pytest test/test_segment_uploader_webuddhist.py::TestSegmentUploader::test_upload_segments_to_webuddhist_success -v
-
Prepare your text files:
mkdir heart_sutra mkdir heart_sutra/heart_sutra_payload # Place your segment payload JSON file
-
Upload segments:
python segment_uploader_webuddhist.py # Enter: heart_sutra # Enter: root # Enter credentials
-
Upload table of contents:
python toc_uploader_webuddhist.py # Enter: heart_sutra # Enter: root # Enter credentials
-
Verify uploads:
# Check log files tail segment_upload_log.txt tail toc_upload_log.txt # Check generated response files ls heart_sutra/heart_sutra_api_response/
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass:
python -m pytest
- Submit a pull request
[Add your license information here]
For issues and questions:
- Check the troubleshooting section
- Review the test cases for usage examples
- Check log files for detailed error information
- Create an issue in the repository
Note: Always test with small datasets first before uploading large collections of texts.