Access a class in another python script

DBS davanand.bahall at gmail.com
Thu Oct 8 02:24:35 EDT 2015


Hello,

I'm using Python 3.5 and have two python scripts where one needs access to a class in the other script for authentication purposes.

The scripts runs through GitHub to poll for all pull requests and pull requests that meet a certain condition.

The call from the second script to the class and variables in the first script are working, but I'm getting prompted for credentials several times.

I wrapped the credentials in a class and created an object in the second script to access the class so that I would not be prompted for credentials in the second script.  I've include both scripts below.  

I'm still new to python, but I think it's how I'm calling the class or the import in the second script???

Thanks in advance for your time.

main_en_pr script:

#! /usr/bin/python
import os
import github3
from github3 import login, GitHub, authorize
from getpass import getuser, getpass
import requests
import csv
import configparser
import sys
import datetime
import codecs

sys.__stdout__ = codecs.getwriter('utf8')(sys.stdout)



# Class to authenticate to GitHub
class GitAuth:
    gh = None
    def authentication(self):
        
        try:
            user = input('GitHub username: ')
        except KeyboardInterrupt:
            user = getuser()

        password = getpass('GitHub token for {0}: '.format(user))
        
        
        self.gh = login(user, password)
        return user

# Assign the class to an object
myobjectx = GitAuth()

# Assign the variable user to the function inside the class
user = myobjectx.authentication()

# Read the contents of the config file to pull in the repo name
config = configparser.ConfigParser()
config.read('repo.ini')
repo = config.get('repos', 'repo1')


result = myobjectx.gh.repository(user, repo).pull_requests('open')

# Define function to list all pull requests
def list_all_prs():
    # open csv file and create header rows

    with open('c:\\pull.csv', 'w+', newline='') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['Id', 'Login', 'Title', 'Commits', 'Changed Files'])

    # iterate through repo for pull requests based on criteria and output to csv file
    for pr in result:
        data = pr.as_dict()
        changes = (myobjectx.gh.repository(user, repo).pull_request(data['number'])).as_dict()
        # keep print to console statement for testing purposes
        # print(changes['id'], changes['user']['login'], changes['title'], changes['commits'], changes['changed_files'])


        with open('c:\\pull.csv','a+',newline='') as f:
            csv_writer = csv.writer(f)

            csv_writer.writerow([changes['id'], changes['user']['login'], changes['title'], changes['commits'],
                                 changes['changed_files']])


list_all_prs()

# Call the validation script
exec(open("one_commit_one_file_change.py").read())

+++++++++++++++++++++++++++++++++++++++

one_commit_one_file_change script:

#! /usr/bin/python
import os
import github3
from github3 import login, GitHub, authorize
from getpass import getuser, getpass
import requests
import csv
import configparser
import sys
import main_en_pr
from main_en_pr import GitAuth
import codecs
sys.__stdout__ = codecs.getwriter('utf8')(sys.stdout)

myobjecty = GitAuth()

user = myobjecty.authentication()



def one_commit_one_file_change_pr():
	
	#open csv file and create header rows
	with open('c:\\commit_filechange.csv', 'w+') as f:
		csv_writer = csv.writer(f)
		csv_writer.writerow(['Login', 'Title', 'Commits', 'Changed Files','Deletions', 'Additions'])

#iterate through repo for pull requests based on criteria and output to csv file
	for pr in main_en_pr.result:
		data = pr.as_dict()
		changes = (myobjecty.gh.repository(user, main_en_pr.repo).pull_request(data['number'])).as_dict()	
	
		if changes['commits'] == 1 and changes['changed_files'] == 1:
		#keep print to console statement for testing purposes
		#print changes['user']['login']
		
		
			with open('c:\\commit_filechange.csv', 'a+') as f:
				csv_writer = csv.writer(f)
			
				csv_writer.writerow([changes['user']['login'], changes['title'], changes['commits'], changes['changed_files']])
				
one_commit_one_file_change_pr()



More information about the Python-list mailing list