Skip to main content

tool to convert short pascal scripts to python

Project description

Transmogripy

About

Transmogripy is a tool to convert short pascal scripts to python. It is originally meant to convert TaLoS scripts but can be easily adapted to many other environments.

Requirements

Transmogripy is intentionally meant to be run in bare-bones environments, so the only requirements are python >= 3.6, it can even run in embeddable systems like QGIS's python environment.

On Numpy

numpy is not a requirement, and is not even used. However, numpy might appear in the output script. This can be prevented by explicitly setting the allow_numpy parameter to False.

Usage

The main function is the convert function.

from transmogripy import convert

print(convert("""
var
   a: integer;
begin
   str := GetVal(Sym[j], 'Name') + ':' + IntToStr(Count[j]);
   a := 10; { do something
   also this}
   (* repeat until loop execution *)
   repeat
      writeln('repeat until loop execution: ', a);
      a := a + log10(12);
      l := floattoint(12.5);
   until a = 20;
end
"""
))

prints:

"""
this script was automatically converted from pascal using transmogripy v...
"""

from talos import *
import math


def main():
   str = GetVal(Sym[j], 'Name') + ':' + str(count[j])
   a = 10  # do something
   # also this
   # repeat until loop execution
   while True:
      writeln('repeat until loop execution: ', a)
      a = a + math.log10(12)
      l = int(12.5)
      if a == 20: break

Behaviour of Result :=

There are many different ways to handle pascal's Result:= code, and can be set with convert's result_behaviour parameter:

  • "variable": a temporary variable __Return__ will be created and only returned at the end of the script
  • "return": Result:= will be converted to return and a check for an early return will not be made.
  • "warn": as "return", except a warning will be issued if an early return is detected
  • "try" (default): as "warn", but if an early warning is detected, the conversion will be re-tried with the "variable" option

Comments and inline comments

The conversion attempts to convert comments, however, all inline comments (comments that have code after them in the same line) will be removed:

var
    a: integer;
begin
    a := 0;
    Result := 0;
    while a <> 10 do {end of line comment}
    begin
        Result := Result {inline comment} + a;
        a := a+1; // end-of line comment
    end;
end

converts to

"""
this script was automatically converted from pascal using transmogripy v...
"""

from talos import *


def main():
    a = 0
    __Return__ = 0
    while a != 10:  # end of line comment
        __Return__ = __Return__  + a
        a = a+1  # end-of line comment
    return __Return__

This behaviour can be changed with the remove_inline_comments parameter, setting it to False will instead raise an exception when an inline comment is encountered

Imports, headers, and segments

Some pascal function, like log, require python libraries to be imported. Transmorgopy automatically adds libraries that are referenced in the converted code.

var
    a: integer;

begin
    a := 0;
    Result := 0;
    while a <> 10 do
    begin
        Result := Result + log10(a);
        a := a+1;
    end;
end

converts to

"""
this script was automatically converted from pascal using transmogripy v...
"""

from talos import *
import math


def main():
    a = 0
    __Return__ = 0
    while a != 10:
        __Return__ = __Return__ + math.log10(a)
        a = a+1
    return __Return__

In addition, additional lines can be added before/after the converted code using the pre_words/post_words parameters. (note: pre_words's default is(from talos import *,), resulting in the from talos import * line)

Code checking

By default, Transmogripy checks the syntax of the output script, and issues a warning if any errors are found. This can be changed by setting the check_syntax parameter to False.

Not Supported

The following Pascal/Delphi features are not supported and will raise an error:

  • pre-compiler directives (foo({$IFDEF bar}bar{$ENDIF}))
  • goto statements (goto foo)
  • all clauses and keywords (if/while/etc...) must be in lowercase (While)
  • else if -> elif is only supported if else and if are on the same line
    • in general, only properly formatted scripts are supported

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

transmogripy-1.2.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file transmogripy-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: transmogripy-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7

File hashes

Hashes for transmogripy-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f10651ac2b0a9ebf236d2699820848266ebc8d00d6dc71d50caf4a6e836c3a34
MD5 8f802a51d6fe3ebe31ceb68abec0f6c9
BLAKE2b-256 85782b248ff339bf614abd328c9fb68ceb52131a514abdf56c64469e41de28bc

See more details on using hashes here.

Supported by

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