CoolFace
Apppublic

Omnibus-archive/Tidy-Code-Java

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py197 linesDownload Raw Back to root
1import gradio as gr2from PIL import Image3import requests4import copy5import re6import os7import yapf8import black9import jsbuilder10 11 12def main():13 14 15    def trans(instring=None, infile=None):16        if instring != None and instring != "":17            src = instring18        elif instring == None or instring == "":19            src = "Omnibus/Tidy-Code"20        r = requests.get(f"https://huggingface.co/spaces/{src}/raw/main/{infile}")21 22        out = r.text23        return out, out, gr.Markdown.update("""""")24 25    def rem_l(inp):26        inpl = (27            inp.replace("\n \n", "\n")28            .replace("\n  \n", "\n")29            .replace("\n   \n", "\n")30            .replace("\n    \n", "\n")31            .replace("\n     \n", "\n")32            .replace("\n      \n", "\n")33            .replace("\n       \n", "\n")34            .replace("\n        \n", "\n")35            .replace("\n         \n", "\n")36            .replace("\n          \n", "\n")37            .replace("\n           \n", "\n")38            .replace("\n            \n", "\n")39            .replace("\n             \n", "\n")40        )41        inpl = (42            inpl.replace("\n             \n", "\n")43            .replace("\n            \n", "\n")44            .replace("\n           \n", "\n")45            .replace("\n          \n", "\n")46            .replace("\n         \n", "\n")47            .replace("\n        \n", "\n")48            .replace("\n       \n", "\n")49            .replace("\n      \n", "\n")50            .replace("\n     \n", "\n")51            .replace("\n    \n", "\n")52            .replace("\n   \n", "\n")53            .replace("\n  \n", "\n")54            .replace("\n \n", "\n")55        )56        out = os.linesep.join([s for s in inpl.splitlines() if s])57        return out58 59    def format_code(inp):60        res = inp.encode()61        return res62 63    def write_file(inp, formt):64        file = open("tmp2py.py", "w")65        file.writelines(inp)66        file.close()67        if formt == "Black":68            res = os.system("black tmp2py.py")69            message = "Formatted with Black"70        elif formt == "YAPF":71            res = os.system("yapf tmp2py.py")72            message = "Formatted with YAPF"73        else:74            pass75        if res == 0:76            file1 = open("tmp2py.py", "r")77            lines_o = file1.read()78            file1.close()79        else:80            lines_o = "ERROR: File could not be formatted"81            message = "ERROR: File could not be formatted"82            pass83        return lines_o, gr.Markdown.update(f"""<center><h7>{message}""")84 85    def mes():86        return gr.Markdown.update("""<center><h7>Processing""")87 88    def clear():89        return None90 91    with gr.Blocks() as gobot:92        gr.Markdown("""<center><h1>Python Code Formatter""")93        with gr.Row():94            intext = gr.Textbox(label="repo/name")95            infile = gr.Textbox(label="File", value="app.py", interactive=True)96            informt = gr.Radio(97                label="Formatter", choices=["YAPF", "Black"], value="YAPF"98            )99        with gr.Row():100            gobtn = gr.Button("Load")101            formbtn = gr.Button("Format Code")102        with gr.Accordion("Controls", open=False):103            with gr.Row():104                with gr.Column():105                    rem_lines = gr.Button("Remove Blank Lines")106                    show_encode = gr.Button("Show Encoded")107                    java_btn = gr.Button("Convert Java")108        message = gr.HTML("""""")109        with gr.Accordion(label="Main", open=True):110            with gr.Row():111                r_ex = gr.Button("Expand")112                n_ex = gr.Button("Split")113                l_ex = gr.Button("Expand")114            with gr.Row():115                py_text = gr.Textbox(label="Input Code", lines=20, max_lines=200)116                fix_py = gr.Textbox(label="Formatted Code", lines=20, max_lines=200)117        with gr.Accordion(label="Java", open=True):118                #java_c = gr.Textbox(label="Java Code", lines=20, max_lines=200)119                java_c = gr.Markdown("""""")120                121        hid_box = gr.Textbox(visible=False)122 123        def ex_r():124            return gr.update(125                label="Input Code", lines=20, max_lines=200, visible=True126            ), gr.update(label="Formatted Code", lines=20, max_lines=200, visible=False)127 128        def ex_n():129            return gr.update(130                label="Input Code", lines=20, max_lines=200, visible=True131            ), gr.update(label="Formatted Code", lines=20, max_lines=200, visible=True)132 133        def ex_l():134            return gr.update(135                label="Input Code", lines=20, max_lines=200, visible=False136            ), gr.update(label="Formatted Code", lines=20, max_lines=200, visible=True)137 138        def java_fn(inp):139            f = open('infile.txt','w')140            f.writelines(line for line in inp)141            f.close()142            fs = open('infile.txt','r')143            line_cnt = fs.readlines()144            fs.close()145            z=len(line_cnt)146            file = open('tmp.txt', 'w')147            #lin_cnt = inp.readlines()148            #z = len(lin_cnt)149            for i, line in enumerate(inp):150                if i == 0:151                    file.writelines("```")152                else:153                    pass154                line=inp[i]155                if 'def ' in line:156                    out = f'"\u0000" + js\n + {line}'157                    file.writelines(out)158                else:159                    out = line160                    file.writelines(out)161                    pass162                '''163                if i == z:164                    file.writelines("```")165                else:166                    pass167                '''168            file.close()169            fout = open('tmp.txt','r')170            out = fout.read()171            fout.close()172            return gr.Markdown.update(f"""{out}""")173            174        java_btn.click(java_fn, fix_py, java_c)175        r_ex.click(ex_r, None, [py_text, fix_py], show_progress=False)176        n_ex.click(ex_n, None, [py_text, fix_py], show_progress=False)177        l_ex.click(ex_l, None, [py_text, fix_py], show_progress=False)178        show_encode.click(format_code, py_text, fix_py)179        rem_lines.click(rem_l, hid_box, py_text)180        gobtn.click(mes, None, message)181        gobtn.click(182            trans, [intext, infile], [py_text, hid_box, message], show_progress=False183        )184        py_text.change(mes, None, message)185        py_text.change(186            write_file, [py_text, informt], [fix_py, message], show_progress=False187        )188        formbtn.click(mes, None, message)189        formbtn.click(190            write_file, [py_text, informt], [fix_py, message], show_progress=False191        )192    gobot.queue(concurrency_count=10).launch()193 194 195if __name__ == "__main__":196    main()197