HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

C IDE in Python with Tkinter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withtkinterpythonide

Problem

I have decided to code an IDE for C using Python with Tkinter. I tried to use my best functional style and keep functions small. This programme works perfectly in Linux with Python 2.7, but it should be compatible with both Windows and Python 3.x.

```
# -- coding: utf-8 --
import sys
from subprocess import Popen, PIPE, STDOUT
import datetime
import glob
import os
import platform

if sys.version_info.major == 2:
import Tkinter as tk
import tkMessageBox as pop_up
import tkFileDialog
else:
import Tkinter as tk
import tkinter.tkMessageBox as pop_up
import tkinter.tkFileDialog as tkFileDialog

TITLE = "C ide"

WINDOWS_ENDING = ".exe"
LINUX_ENDING = ".out"

EMPTY_TITLE_ERROR_MESSAGE_SAVE = "Please write the name of the file you want to save in the given field."
EMPTY_TITLE_ERROR_MESSAGE_OPEN = "Please write the name of the file you want to open in the given field."
INVALID_CHARACTERS_MESSAGE = "Unicode does not allow accented letters, please replace them in the following way: è -> e', à -> a'."
SAVING_SUCCESS_MESSAGE = "Your text is now stored in the {filename} file"

NO_ERROR = ('', None)

def replace_old_title(new_title):
"""
Replace the old content of the widget
file_title with a new title.
"""
file_title.delete(0, tk.END)
file_title.insert(tk.INSERT, new_title)

def replace_old_text(new_text):
"""
Replace the old content of the widget
main_text with a new title.
"""
main_text.delete("1.0", tk.END)
main_text.insert(tk.INSERT, new_text, "a")

def open_():
"""
Opens a file using the built-in file explorer,
and displays its text in the main text field.
"""
filename = tkFileDialog.askopenfilename()
replace_old_title(filename)
with open(filename) as f:
replace_old_text(f.read())

def title_is_empty():
"""
Return True if the tite is empty.
"""
return not file_title.get()

def invalid_characters_in_title():
"""
Handles invalid characters in th

Solution


  • It is mostly PEP8 conform, nice! One thing: Use just one empty line between


functions.

  • The imports datetime, glob and os are not used.



-
I needed to change the tkinter-imports for python3 on linux:

if sys.version_info.major == 2:
    import Tkinter as tk
    import tkMessageBox as pop_up
    import tkFileDialog
else:
    import tkinter as tk
    import tkinter.messagebox as pop_up
    import tkinter.filedialog as tkFileDialog


-
exec_bash(): Please read
https://docs.python.org/3/library/subprocess.html#security-considerations.
You pass in data gathered from the document the user edits and could be provided by someone. A possible attack vector would be something like this example:

// FLAGS; echo $USER;

int main( int argc, const char* argv[] ) {
}


-
system_is()/decide_ending(): This could be simplified using a constant
dict with a mapping from os-name to file-ending.

-
execute(): Why the use of nice_format_for_execute()? This could be
simplified to:

result = exec_bash("./" + filename + decide_ending())
return result[0]


-
delete(): You could just do return string.replace(...). It would be nice
to use str.lstrip() to just remove the chars from the beginning.

-
get_flags(): Duplicate variable flags. And this could be shorter with
the side effect of making delete() redundant:

lines = main_text.get(1.0, tk.END).splitlines()
first_line = lines[0]
if first_line.startswith("// FLAGS"):
    return first_line.lstrip("// FLAGS")
return ""


-
open_(): The with statement can fail and would throw an exception:

>>> with open("doesnotexist") as fob:
...   pass
Traceback (most recent call last):
  File "", line 1, in 
FileNotFoundError: [Errno 2] No such file or directory: 'doesnotexist'


-
Please wrap the code at the end of the file in a main() function. This
creates the problem of variables like file_title. Please do not use
global, but consider wrapping your functions in a class.

Code Snippets

if sys.version_info.major == 2:
    import Tkinter as tk
    import tkMessageBox as pop_up
    import tkFileDialog
else:
    import tkinter as tk
    import tkinter.messagebox as pop_up
    import tkinter.filedialog as tkFileDialog
// FLAGS; echo $USER;

int main( int argc, const char* argv[] ) {
}
result = exec_bash("./" + filename + decide_ending())
return result[0]
lines = main_text.get(1.0, tk.END).splitlines()
first_line = lines[0]
if first_line.startswith("// FLAGS"):
    return first_line.lstrip("// FLAGS")
return ""
>>> with open("doesnotexist") as fob:
...   pass
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'doesnotexist'

Context

StackExchange Code Review Q#71174, answer score: 5

Revisions (0)

No revisions yet.