My attempt at programming python

×

Error message

  • Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /home4/ccollins/public_html/ccollins/includes/common.inc).
  • Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in menu_set_active_trail() (line 2405 of /home4/ccollins/public_html/ccollins/includes/menu.inc).
I've been wanting to get into python for a while but never had the time.
This last week i didn't have anything to do so.......
I have written a perl script to get the environment variable out of multiple files.
If i have the same variable in both files the last file will replace the first files variable.

env.ini

[DB] 
CARDTESTSID = CARDTST  
CARDTESTSVER = cardnewTest.litho.com  
CARDTESTUSER = INTEGRATION  
CARDTESTPASS = INTEGRATION  
CARDTESTPRT = 1521  
CARDSID = CARD  
CARDSVER = jetunx3.itho.com  
CARDUSER = INTEGRATION  
CARDPASS = INTEGRATION01  
CARDPRT = 1521
[DIRECTORIES]  
 baseName = /jet/prod/   Dir = /app/jet1/Incoming/fifth_third/archive
[EMAIL]   
emailSENDER =some@litho.com   
emailRECIPIENTS = some@litho.com

vantive.conf

[ERROR]   
error=DISCREPANCY   
ok=SUCCESS
[EMAIL]   
emailSENDER = jetitmanager@litho.com   
emailRECIPIENTS = christopherc@litho.com
[DIRECTORIES]   
dir = /app/jet1/Incoming/fifth_third/archive
[DB]   
CARDTESTSID = NEW

envexample.pl

use strict;
use warnings;
use Data::Dumper;
my $ref = {};
 
&getEnv($ref);
 
sub getEnv {     my $ref = shift(@_);     
push @{$ref->{config}->{env}} , "env.ini";     
push @{$ref->{config}->{env}} , "vantiv.conf";
#     
foreach my $file ( @{$ref->{config}->{env}} ){         # find the env file        
 if ( -e $file ) {             
# check ./            
print "Found $file \n";       
}         
else {             
print "Could not find env $file \n";             
next;        
 }     
my $section;     
open( FILE, "< $file" );         
while () {            
 my $line = $_;             
chomp($line);            
 next if $line =~ /^\s*\#/;            
 if ( $line =~ /\s*(\[\w+\])/ ){                 
$section = $1;             
$section =~ s/\[|\]//g;             
next;    
 }     
next unless $line =~ /=/;         
my ( $key, $variable ) = split( /=/, $_, 2 );         
$variable = &trim($variable);         
$key = &trim($key);         
$key = uc($key);        
$ref->{config}->{$section}->{$key} = $variable;        
 }         
close FILE;     }     
return;
}
sub trim {      
my $s = shift;     
$s =~ s/^\s+|\s+$//g;     
return $s;
}

Now i have a python script that does the same thing 

envexample.py

#!/usr/bin/python
import os.path
import re
import pprint

def getEnv(ref):

    for file in ref['config']:

        print( file['env'] )

        if os.path.isfile(file['env']):

            fh = open(file['env'],"r+")

            for line in fh:

                matchsec = re.search(r"\s*\[(\w+)\]",line)

                if match:
                    section = matchsec.group(1)
                    continue

                # match on equals
                match = re.search(r"=",line)

                if match:
                    # split key value pairs
                    sp = line.split( "=" , 2)

                    # Strip the line to remove whitespace.
                    key = sp[0].strip()
                    value = sp[1].strip()

                    # change the key to upper
                    key = key.upper()
                    ref[key] = value

                    if section in ref:
                        # if there just change value
                        ref[section][key] = value
                        ref[key] = value
                    else:
                        # if not then add the key and value
                        ref[section] = {}
                        ref[section][key] = value
                    #    ref[key] = value
                else:
                    continue
            fh.close()
        else:
            print "file could not be found ", file
# end getEnv

#--- MAIN
# make a dictionary
ref = { }
# add a list with dictionary
ref['config'] = [         {'env':'env.ini'},         {'env':'vantiv.conf'}     ]
getEnv(ref)
pp = pprint.PrettyPrinter()
pp.pprint( ref )