Skip to content

Commit

Permalink
Add python to list of languages
Browse files Browse the repository at this point in the history
  • Loading branch information
ylogx committed May 21, 2015
1 parent 4adc1ed commit 55b1d58
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tests/compiler/language/test_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python

from __future__ import print_function
from universal.compiler.language.python import Python

try:
import unittest
import unittest.mock
from unittest.mock import patch
from unittest.mock import call
except ImportError as e:
import mock
from mock import patch
from mock import call


class TestGpp(unittest.TestCase):
def setUp(self):
self.filename_py = 'foobar.py'
self.compiler = Python(self.filename_py)

@patch('universal.compiler.language.python.perform_system_command')
def test_compile(self, mock_sys_cmd):
self.compiler.compile()
self.assertFalse(mock_sys_cmd.called)


@patch('os.path.exists')
@patch('universal.compiler.language.python.perform_system_command')
def test_run_output_when_no_input_file_available(self, mock_sys_cmd, mock_path_exists):
mock_path_exists.return_value = False

self.compiler.run()

mock_sys_cmd.assert_called_once_with(AnyStringContaining('python'))

@patch('os.path.exists')
@patch('universal.compiler.language.python.perform_system_command')
def test_run_output_when_input_file_available(self, mock_sys_cmd, mock_path_exists):
mock_path_exists.return_value = True

self.compiler.run()

mock_sys_cmd.assert_called_once_with(AnyStringContaining('foobar.input'))


class AnyStringContaining(str):
def __eq__(self, other):
return self in other


if __name__ == '__main__':
unittest.main()
3 changes: 3 additions & 0 deletions universal/compiler/compiler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ..util import get_file_tuple
from .language import Gcc
from .language import Gpp
from .language import Python

class Compiler:

Expand All @@ -20,5 +21,7 @@ def get_compiler(self):
compiler = Gcc(self.filename)
elif extension == Gpp.extension():
compiler = Gpp(self.filename)
elif extension == Python.extension():
compiler = Python(self.filename)
return compiler

1 change: 1 addition & 0 deletions universal/compiler/language/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__author__ = 'shubham'
from .gcc import Gcc
from .gpp import Gpp
from .python import Python
from .language import Language
35 changes: 35 additions & 0 deletions universal/compiler/language/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import print_function
import os
from .language import Language
from universal.config import EXECUTABLE_PYTHON
from universal.util import perform_system_command, get_file_tuple


class Python(Language):
@staticmethod
def extension():
return 'py'

def __init__(self, filename):
""" :param filename: file to use
"""
self.filename = filename

def compile(self):
""" :return: 0 if compilation successful
"""
return 0

def run(self):
command_run = get_command_to_run(self.filename)
return perform_system_command(command_run)


def get_command_to_run(filename):
(directory, name, extension) = get_file_tuple(filename)

command_run = EXECUTABLE_PYTHON + " " + filename
test_file = directory + "/" + name + ".input"
if os.path.exists(test_file):
command_run += " < " + test_file
return command_run

0 comments on commit 55b1d58

Please sign in to comment.