Skip to main content

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:

  1. Create a new extractor in the cobol_parser/extractors/ directory
  2. Add a corresponding method to the CobolParser class
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

legacylens_cobol_parser-0.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

legacylens_cobol_parser-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file legacylens_cobol_parser-0.1.0.tar.gz.

File metadata

  • Download URL: legacylens_cobol_parser-0.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for legacylens_cobol_parser-0.1.0.tar.gz
Algorithm Hash digest
SHA256 00846810e748c60b3d8cc5acabe32d1311ae752f488301fd981ecfc218d09901
MD5 2979bf1357fc64606e53480dc3a0f6b7
BLAKE2b-256 6c26fe52880f53c7a36ef99cf720849de7a0d710e1bcbd72f2aca4cc4a178e86

See more details on using hashes here.

File details

Details for the file legacylens_cobol_parser-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for legacylens_cobol_parser-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 998f4e7f578b4b146b2673eb7f575e3fc36afa66de0a6340d095850a1befcb5d
MD5 b3872c3934e93a4eefe28bee78a83f7c
BLAKE2b-256 5e513e629cc2895f8555d70cf169d467af257ad8fa6a7d0338b5798ac8a3f394

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page