App-PythonToPerl

 view release on metacpan or  search on metacpan

lib/Python/File.pm  view on Meta::CPAN

#    my Python::Component::arrayref $python_preparsed =
        $self->python_file_to_python_preparsed( $python_includes, $python_functions, $python_classes);
#print 'in python_file_to_perl_file(), ret from python_file_to_python_preparsed(), have     $self = ', Dumper($self), "\n";
#print 'in python_file_to_perl_file(), ret from python_file_to_python_preparsed(), have     $python_files = ', Dumper($python_files), "\n";
print 'in python_file_to_perl_file(), ret from python_file_to_python_preparsed(), have $self->{python_preparsed} = ', Dumper($self->{python_preparsed}), "\n";
#print 'in python_file_to_perl_file(), have $python_includes = ', Dumper($python_includes), "\n";
#print 'in python_file_to_perl_file(), have $python_functions = ', Dumper($python_functions), "\n";
#print 'in python_file_to_perl_file(), have $python_classes = ', Dumper($python_classes), "\n";

    # error or warning if no pre-parsed components
    if ((not exists  $self->{python_preparsed}) or
        (not defined $self->{python_preparsed})) {
        croak 'ERROR EPYFI052: non-existent or undefined Python pre-parsed components, croaking';
    }
    elsif ((scalar @{$self->{python_preparsed}}) == 0) {
        carp 'WARNING WPYFI052: empty Python pre-parsed components, carping';
    }

    # DE-PARSE PYTHON FILE BACK INTO PYTHON AKA ROUND-TRIPPING
print 'in python_file_to_perl_file(), about to call python_preparsed_to_python_source()...', "\n";
    # DEV NOTE: capturing the return value below is purely optional,
    # the source code output will also be stored in the Python::File object's python_source_code_full property 
#    my string $python_source_code = 
        $self->python_preparsed_to_python_source();
print 'in python_file_to_perl_file(), ret from python_preparsed_to_python_source(), have $self->{python_source_code_full} = ', "\n", $self->{python_source_code_full}, "\n";
#die 'TMP DEBUG, PYTHON ROUND-TRIPPING';

    # DE-PARSE & TRANSLATE PYTHON FILE INTO PERL SOURCE CODE
print 'in python_file_to_perl_file(), about to call python_preparsed_to_perl_source()...', "\n";
    # DEV NOTE: capturing the return value below is purely optional,
    # the source code output will also be stored in the Python::File object's perl_source_code_full property 
#    my string $perl_source_code = 
        $self->python_preparsed_to_perl_source($openai);
print 'in python_file_to_perl_file(), ret from python_preparsed_to_perl_source(), have $self->{perl_source_code_full} = ', "\n", $self->{perl_source_code_full}, "\n";

    # SAVE PERL FILE
print 'in python_file_to_perl_file(), about to call python_file_path_to_perl_file_path()...', "\n";
    # DEV NOTE: capturing the return value below is purely optional,
    # the file name output will also be stored in the Python::File object's perl_file_path property 

    # generate Perl file path based on Python file path
#    my string $perl_file_path = 
        $self->python_file_path_to_perl_file_path();
print 'in python_file_to_perl_file(), ret from python_file_path_to_perl_file_path(), have $self->{perl_file_path} = \'', $self->{perl_file_path}, '\'', "\n";

    # open new Perl source code file, write all contents to disk, close file
print 'in python_file_to_perl_file(), about to save Perl file...', "\n";
    open (my filehandleref $PERL_FILE, '>', $self->{perl_file_path})
        or croak 'ERROR EPYFI05x: failed to open Perl source code file \'', $self->{perl_file_path}, 
            '\' for writing, received OS error message \'', $OS_ERROR, '\', croaking';  # DEV NOTE: $OS_ERROR == $!
    print {$PERL_FILE} $self->{perl_source_code_full};
    close($PERL_FILE)
        or croak 'ERROR EPYFI05y: failed to close Perl source code file \'', $self->{perl_file_path}, 
            '\' after writing, received OS error message \'', $OS_ERROR, '\', croaking';
print 'in python_file_to_perl_file(), ret from calls to save Perl file', "\n";

    # set file executability permission based on presence of shebang;
    # an executable script has a shebang, a non-executable module AKA library does not have a shebang
    if((defined $self->{python_preparsed}->[0]) and ($self->{python_preparsed}->[0]->isa('Python::Shebang'))) {
print 'in python_file_to_perl_file(), setting executable permission of Perl file', "\n";
        chmod(S_IXUSR, $self->{perl_file_path})
            or croak 'ERROR EPYFI05z: failed to chmod u+x Perl source code file \'', $self->{perl_file_path}, 
                '\' after closing, received OS error message \'', $OS_ERROR, '\', croaking';
    }

print 'in python_file_to_perl_file(), about to return $self->{perl_file_path} = \'', $self->{perl_file_path}, '\'', "\n";
    return $self->{perl_file_path};
}


# PYFI04x
sub python_file_path_to_perl_file_path {
# convert Python file path to Perl file path
    { my string $RETURN_TYPE };
    ( my Python::File $self ) = @ARG;

    # DEV NOTE, PYFI040: $openai not used in this subroutine, no need to error check
    # DEV NOTE, PYFI041: $self->{python_source_code} not used in this subroutine, no need to error check

    # error or warning if no pre-parsed components;
    # must have already pre-parsed to check for shebang
    if ((not exists  $self->{python_preparsed}) or
        (not defined $self->{python_preparsed})) {
#        croak 'ERROR EPYFI042a: non-existent or undefined Python pre-parsed components, croaking';
        carp 'WARNING WPYFI042a: non-existent or undefined Python pre-parsed components, carping';
    }
    elsif ((scalar @{$self->{python_preparsed}}) == 0) {
        carp 'WARNING WPYFI042b: empty Python pre-parsed components, carping';
    }

    # error if no file path
    if ((not exists  $self->{python_file_path}) or
        (not defined $self->{python_file_path}) or
        ($self->{python_file_path} eq '')) {
        croak 'ERROR EPYFI043: non-existent or undefined or empty Python file path, croaking';
    }

    # split file path into volume (ignored in Unix operating systems), directory, and file name
    my string $python_volume;
    my string $python_directory;
    my string $python_file_name;
    ($python_volume, $python_directory, $python_file_name) = File::Spec->splitpath($self->{python_file_path});

    # ensure file name was split correctly
    if ((not defined $python_file_name) or
        ($python_file_name eq '')) {
        croak 'ERROR EPYFI044: undefined or empty Python file name, croaking';
    }

    # always base Perl file name on original Python file name
    my string $perl_file_name = $python_file_name;

    # set file suffix based on presence of shebang;
    # an executable script has a shebang, a non-executable module AKA library does not have a shebang
    my string $perl_file_suffix = 'pl';



# NEED FIX: re-enable Perl file suffix selection below
# NEED FIX: re-enable Perl file suffix selection below
# NEED FIX: re-enable Perl file suffix selection below



( run in 0.619 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )