A tool to extract information from COBOL programs
Project description
LegacyLens COBOL Parser
A Python tool to extract specific information from COBOL programs.
Features
This parser extracts the following information from COBOL source code:
- CALLS: Identifies CALL statements and their parameters
- I/O FILES: Extracts file operations (SELECT, OPEN, CLOSE, READ, WRITE)
- PERFORMS: Identifies PERFORM statements (procedural calls)
- SQL QUERIES: Extracts embedded SQL statements
- COPYBOOKS: Identifies COPY statements (included files)
- Support for conditional compilation
- Configurable logging with standard levels (INFO, DEBUG)
Installation
From PyPI (Recommended)
pip install legacylens_cobol_parser
From Source
# Clone the repository
git clone https://github.com/sam94dion/cobol-parser.git
cd cobol-parser
# Install in development mode
pip install -e .
Usage
Quick Start (Simplified Interface)
# Parse a file with default settings
from cobol_parser import parse_file
# Parse a file and extract everything
result = parse_file("path/to/your/cobol/program.cbl")
print(result)
# Parse a file and extract only specific information
result = parse_file(
"path/to/your/cobol/program.cbl",
extract_types=["calls", "sql_queries"]
)
print(result)
# Parse from a string
from cobol_parser import parse_string
cobol_code = """
IDENTIFICATION DIVISION.
PROGRAM-ID. EXAMPLE.
PROCEDURE DIVISION.
PERFORM PARA-A.
CALL "SUBPGM" USING WS-VAR.
PARA-A.
DISPLAY "Hello, World!".
"""
result = parse_string(cobol_code)
print(result)
As a Python Library (Advanced Usage)
from cobol_parser import CobolParser, setup_logger
import logging
# Configure logging (optional)
setup_logger(level=logging.INFO) # Options: logging.INFO, logging.DEBUG
# Create a parser instance
parser = CobolParser()
# Load COBOL source from a file
parser.load_from_file("path/to/your/cobol/program.cbl")
# Or load from a string
cobol_code = """
IDENTIFICATION DIVISION.
PROGRAM-ID. EXAMPLE.
PROCEDURE DIVISION.
PERFORM PARA-A.
CALL "SUBPGM" USING WS-VAR.
PARA-A.
DISPLAY "Hello, World!".
"""
parser.load_from_string(cobol_code)
# Extract all information
result = parser.extract_all()
print(result)
# Or extract specific information
calls = parser.extract_calls()
io_files = parser.extract_io_files()
performs = parser.extract_performs()
sql_queries = parser.extract_sql_queries()
copybooks = parser.extract_copybooks()
Logging Configuration
The parser provides a flexible logging system:
from cobol_parser import setup_logger
import logging
# Configure logging with different levels
setup_logger(level=logging.INFO) # Show only important information
setup_logger(level=logging.DEBUG) # Show all debugging information
# You can also configure logging with a custom name
setup_logger(name="my_custom_logger", level=logging.INFO)
# The logger will output messages in the format:
# YYYY-MM-DD HH:MM:SS,mmm - logger_name - LEVEL - message
Command Line Interface
# Basic usage
legacylens_cobol_parser path/to/your/cobol/program.cbl
# Specify output file
legacylens_cobol_parser path/to/your/cobol/program.cbl -o results.json
# Extract specific information
legacylens_cobol_parser path/to/your/cobol/program.cbl -e calls
# Output as text instead of JSON
legacylens_cobol_parser path/to/your/cobol/program.cbl -f text
# Set log level
legacylens_cobol_parser path/to/your/cobol/program.cbl -l info # Show only INFO messages
legacylens_cobol_parser path/to/your/cobol/program.cbl -l debug # Show all debug messages
Development
Running Tests
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
Extending the Parser
The parser is designed to be extensible. To add support for extracting additional elements:
- Create a new extractor in the
cobol_parser/extractors/directory - Add a corresponding method to the
CobolParserclass - Update the CLI interface in
cobol_parser/cli.py
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
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 legacylens_cobol_parser-0.1.20.tar.gz.
File metadata
- Download URL: legacylens_cobol_parser-0.1.20.tar.gz
- Upload date:
- Size: 38.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
996faedbab10be0f6567913f37acac35d3771a71a1fe9b0c2baf398d60c94f77
|
|
| MD5 |
2979973e0d279175e05437142d4bc50d
|
|
| BLAKE2b-256 |
0dce1183a28080c6588aeba1c461370c2fff93c093517177280d04e7b872bfca
|
File details
Details for the file legacylens_cobol_parser-0.1.20-py3-none-any.whl.
File metadata
- Download URL: legacylens_cobol_parser-0.1.20-py3-none-any.whl
- Upload date:
- Size: 22.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34bf91f5fa2a2ce0d626f480aa857ce8d2564a5d2985868345629468bd82d40b
|
|
| MD5 |
fc01cb5d83586a6924b18c74e9c75b27
|
|
| BLAKE2b-256 |
bac51c342c1c84ebd6aee005b9082c5bab33d1e9593a5208d14097e7f74489f3
|