<commit_before>__author__ = 'vipin'

import json
from os import getcwd, path as pth
from subprocess import Popen, PIPE, SubprocessError
from requests.auth import HTTPBasicAuth
import requests

def _parse_token():
   '''
   :return: Application token fot Github API
   '''
   cur_dir = pth.dirname(__file__)
   token_file = pth.abspath(pth.join(cur_dir, pth.pardir, 'secret.txt'))
   with open(token_file, "r") as filehandler:
       return filehandler.read()

def _git_cmd(cmd_str, repo=getcwd()):
    proc = Popen(cmd_str, stdout=PIPE, stderr=PIPE)
    try:
        output, error = proc.communicate()
    except SubprocessError as err:
        print(err)
    else:
        if error:
            return error.decode('unicode_escape')
        else:
            return output.decode('unicode_escape')

# Unit tests
import unittest

class AuthTest(unittest.TestCase):
    @unittest.skip
    def test_parse_token(self,):
        self.assertEqual(_parse_token(), '<KEY>')

    @unittest.skip
    def test_connection(self,):
        user = _parse_token()
        pwd = '<PASSWORD>'
        resp = requests.get('https://api.github.com/users/vipnambiar/repos', auth=HTTPBasicAuth(user, pwd))
        self.assertEqual(resp.status_code, 200)

        content = json.loads(resp.text)
        for item in content:
            for key, val in item.items():
                print('{0}: {1}'.format(key, val))
            print()

    def test_git_cmd(self):
        print(_git_cmd(['git', 'add', 'test_api.py'], repo='/home/vipin/Projects/test_github_api'))
        self.assertTrue(1)<commit_msg>Refactored try/except block in _git_cmd function<commit_after>__author__ = 'vipin'

import json
from os import getcwd, path as pth
from subprocess import Popen, PIPE, SubprocessError
from requests.auth import HTTPBasicAuth
import requests

def _parse_token():
   '''
   :return: Application token fot Github API
   '''
   cur_dir = pth.dirname(__file__)
   token_file = pth.abspath(pth.join(cur_dir, pth.pardir, 'secret.txt'))
   with open(token_file, "r") as filehandler:
       return filehandler.read()

def _git_cmd(cmd_str, repo=getcwd()):
    try:
        proc = Popen(cmd_str, stdout=PIPE, stderr=PIPE)
        output, error = proc.communicate()
    except Exception as err:
        print(err)
    else:
        if error:
            return error.decode('unicode_escape')
        else:
            return output.decode('unicode_escape')

# Unit tests
import unittest

class AuthTest(unittest.TestCase):
    @unittest.skip
    def test_parse_token(self,):
        self.assertEqual(_parse_token(), '<KEY>')

    @unittest.skip
    def test_connection(self,):
        user = _parse_token()
        pwd = '<PASSWORD>'
        resp = requests.get('https://api.github.com/users/vipnambiar/repos', auth=HTTPBasicAuth(user, pwd))
        self.assertEqual(resp.status_code, 200)

        content = json.loads(resp.text)
        for item in content:
            for key, val in item.items():
                print('{0}: {1}'.format(key, val))
            print()

    def test_git_cmd(self):
        print(_git_cmd('git commit -m "removed pass cmd as string"', repo='/home/vipin/Projects/test_github_api'))
        self.assertTrue(1)
{'fraction_non_alphanumeric': 0.12110834371108344, 'fraction_numerical': 0.0037359900373599006, 'mean_word_length': 2.9764851485148514, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7349091', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1032, 'n_words': 237}