-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbinpythonfull.py
More file actions
628 lines (595 loc) · 22.6 KB
/
binpythonfull.py
File metadata and controls
628 lines (595 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
"""
@header binpython.py
@author xingyujie https://github.com/xingyujie
@abstract BINPython main file
"""
#BINPython By:XINGYUJIE AGPL-V3.0 LICENSE Release
#Please follow the LICENSE AGPL-V3
#full version
####################################
#build configure
ver="0.32-build-full"
libs_warning="1"
#1 is ture 0 is false.
#Changing the value to 0 will close the prompt that the library does not exist
buildversion = "plus" #plus version and standard version
releases_ver="offical" + buildversion + "-building"
importlibs="os" #Don't use "import xxx"
#Imported library name, please use "importlibs="<library name>" instead of "import <library name>"
#Please note: The "importlibs" function does not support loading functions (such as from xxxx import xxxx, if necessary, please write it in the following location. However, please note that this operation may have the risk of error reporting, please report issues or solve it yourself
#xxxxxxxxxxxxxx
#from xxxx import xxxx
#xxxxxxxxxxxxxx
####################################
#BINPython function and variable START
class binpythoninfo:
def ver():
print(ver)
def buildversion():
print(buildversion)
def libs_warning():
print(libs_warning)
def releases_ver():
print(releases_ver)
def build_importlibs():
print(importlibs)
#get system info(windows or linux ...)
import platform
sys = platform.system()
#if system is windows, then enable setwindowtitle() function
if sys == "Windows":
class binpythonwin:
def setwindowtitle(titlename):
import ctypes
ctypes.windll.kernel32.SetConsoleTitleW(titlename)
#print binpython all configure function
def binpythonallconf():
print("ver: " + ver + " buildversion: " + buildversion + " libs_warning settings:" + libs_warning + " releases full version: " + releases_ver + " custom library that has been build: " + importlibs)
#if system is windows, show default window title
if sys == "Windows":
import ctypes
ctypes.windll.kernel32.SetConsoleTitleW("BINPython " + ver)
#binpython self import function
def self_import(name):
__import__(name)
try:
self_import(importlibs)
#get libswarning
except ImportError:
if libs_warning == "1":
print("Warning: Custom import library %s does not exist, please check the source code library configuration and rebuild" % importlibs)
print("")
#run python files option(-f)
def optreadfile():
import sys
getfile = sys.argv[1]
getfilecode = open(getfile,encoding = "utf-8")
exec(getfilecode.read())
input("Run finished. Enter to Shell.")
sys.exit(0)
try:
#base import
import getopt
import sys
import platform
import os
import timeit
import pdb
#fix for exit()
from sys import exit
#import for http_server
import http.server
import socketserver
#except ImportError:
except ImportError:
print("Unable to use any library, the program does not work properly, please rebuild")
#gui import
try:
import tkinter
import tkinter as tk
from tkinter import *
import turtle
def importpygame():
import pygame
import pygame.locals
import pyglet
null = importpygame
#warning for gui
except ImportError:
if libs_warning == "1":
print("Warning: Some GUI (graphical) libraries for BINPython do not exist, such as tkinter and turtle. Because they are not built when they are built. If you need to fix this warning, please complete the support libraries imported in the source code at build time (use pip or build it yourself), if your system does not support these libraries, you can remove or change this hint in the source code and rebuild")
print("")
#import math
try:
import fractions
import cmath
except ImportError:
if libs_warning == "1":
print("Warning: Some math or computation libraries for BINPython do not exist, such as fractions and cmath. Because they weren't built when they were built. If you need to fix this warning, please complete the support libraries imported in the source code when building (use pip or build it yourself), if your system does not support these libraries, you can remove or change this prompt in the source code and rebuild")
print("")
#import for normal
try:
#str
import rlcompleter
import array
import xlrd
except ImportError:
if libs_warning == "1":
print("Warning: Some libraries for functions, data types, etc. for BINPython do not exist, such as rlcomplter and array. Because they weren't built when they were built. If you need to fix this warning, please complete the support libraries imported in the source code when building (use pip or build it yourself), if your system does not support these libraries, you can remove or change this prompt in the source code and rebuild")
print("")
try:
import filecmp
import tempfile
except ImportError:
if libs_warning == "1":
print("Warning: Some file manipulation libraries for BINPython do not exist, such as filecmp and tempfile. Because they weren't built when they were built. If you need to fix this warning, please complete the support libraries imported in the source code when building (use pip or build it yourself), if your system does not support these libraries, you can remove or change this prompt in the source code and rebuild")
print("")
#main BINPython
def binpython_welcome_text():
print("BINPython " + ver + "-" + releases_ver + " (Python Version:" + platform.python_version() + ")By:XINGYUJIE https://github.com/xingyujie/binpython[Running on " + platform.platform() + " " + platform.version() + "]")
print('Type "about", "help", "copyright", "credits" or "license" for more information.')
def binpython_shell():
while True:
try:
pycmd=input(">>> ")
if pycmd in globals().keys():
print(globals()[pycmd])
continue
elif pycmd == 'about':
print("BINPython By:XINGYUJIE[https://github.com/xingyujie] AGPL-3.0 LICENSE Release")
elif pycmd == 'help':
print("Type help() for interactive help, or help(object) for help about object.")
elif pycmd == 'copyright':
print("""
Copyright (c) 2001-2022 Python Software Foundation.
All Rights Reserved.
Copyright (c) 2000 BeOpen.com.
All Rights Reserved.
Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
""")
elif pycmd == 'credits':
print("""
Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
for supporting Python development. See www.python.org for more information.
""")
elif pycmd == 'license':
print("Type license() to see the full license text")
else:
exec(pycmd)
except KeyboardInterrupt:
print("KeyboardInterrupt")
sys.exit()
except Exception as err:
print(err)
try:
optreadfile()
except:
pass
#def
#helpinfo base
helpinfobase = """
Usage: binpython [OPTIONS]
Options:
<filename> Enter Python Filename and run (*.py)
-f --file Enter Python Filename and run (*.py), But this options is no Run finished prompt
-h --help View this help
-s <port> --server=<port> Start a simple web server that supports html and file transfer (http.server)
-v --version View BINPython Version
-g --gui View GUI About and build info
-i --idle Open BINPython IDLE Code Editor
"""
#helpinfo plus
helpinfoplus = """
-p --plus Open BINPython IDE Plus Code Editor(beta) with http web server
-e --example Run various code examples through BINPython
"""
#base + plus, print full help
def outputfullhelp():
try:
f = open("binpython_config/help.txt",encoding = "utf-8")
print(f.read())
except:
print(helpinfobase)
if buildversion == "plus":
print("Additional options for the plus version")
print(helpinfoplus)
#set about info
about = "BINPython " + ver + "-" + releases_ver + " By:XINGYUJIE[https://github.com/xingyujie/binpython] AGPL-3.0 LICENSE Release"
#getopt
try:
#set options
opts,args = getopt.getopt(sys.argv[1:],'-h-f:-s:-g-i-p-e-v',['help','file=','server=','gui','idle','plus','example','version'])
#set getopt error prompt
except getopt.GetoptError as err:
print("Please check help:")
print("The parameters you use do not exist or are not entered completely, please check help!!!!")
#then show full help(outputfullhelp())
outputfullhelp()
sys.exit()
#get every option and run
for opt_name,opt_value in opts:
if opt_name in ('-h','--help'):
#-h show full help function
outputfullhelp()
sys.exit()
if opt_name in ('-v','--version'):
#-v show version(read custom config)
try:
f = open("binpython_config/version.py",encoding = "utf-8")
exec(f.read())
print("Powered by: BINPython[https://github.com/xingyujie/binpython] AGPL 3.0")
except:
print("BINPython " + ver + "-" + releases_ver + " By:XINGYUJIE[https://github.com/xingyujie/binpython] AGPL-3.0 LICENSE Release")
print("Python " + platform.python_version())
sys.exit()
if opt_name in ('-f','--file'):
#-f runfile(or no option)
file = opt_value
f = open(file,encoding = "utf-8")
exec(f.read())
sys.exit()
if opt_name in ('-s','--server'):
#-s set simple http server(support html or files transfer)
server_port = opt_value
exec("""
PORT = """ + server_port + """
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
""")
if opt_name in ('-g','--gui'):
#-g gui show gui about info(based on tkinter)
from tkinter import *
root = Tk()
root.title("Welcome to BINPython")
root.geometry('600x300')
text =Text(root, width=35, heigh=15)
text.pack()
text.insert("insert", "BINPython" + about)
print(text.get("1.3", "1.end"))
####
text=Label(root,text="Welcome to BINPython" + ver,bg="yellow",fg="red",font=('Times', 20, 'bold italic'))
text.pack()
button=Button(root,text="EXIT",command=root.quit)
button.pack(side="bottom")
root.mainloop()
sys.exit()
def show():
os.system('cls' if os.name == 'nt' else 'clear')
print("<<<<<<<<<<START>>>>>>>>>>")
exec(e1.get(1.0, END))
#tkinter ide Simulation environment
if opt_name in ('-i','--idle'):
import tkinter as tk
from tkinter import *
master = tk.Tk()
master.title("BINPython IDLE")
tk.Label(master, text="Type Code", height=5).grid(row=0)
e1 = Text(master,
width=150,
height=40,)
e1.grid(row=0, column=1, padx=10, pady=5)
tk.Button(master, text="Run", width=10, command=show).grid(row=3, column=0, sticky="w", padx=10, pady=5)
tk.Button(master, text="EXIT", width=10, command=master.quit).grid(row=3, column=1, sticky="e", padx=10, pady=5)
master.mainloop()
sys.exit()
#-p binpython ideplus
if opt_name in ('-p','--plus'):
import pywebio.input
from pywebio.input import *
from pywebio.output import *
from pywebio import *
from pywebio.session import *
import sys
import subprocess
import os
import webbrowser
print("______________________________________")
print("BINPython WEB IDE STARTED")
print("""
Welcome to BINPython IDEPlus!
The service starts on port 22948 (http), the program will automatically open the browser, if not, please manually open http://localhost:22948 in the browser
""")
#open browser
webbrowser.open("http://localhost:22940")
#IDE Plus main
def line():
put_text('_______________________',
sep=' '
)
#set bootstrap ui(bar)
def head():
set_env(title="BINPython IDE Plus", auto_scroll_bottom=True)
put_html("""
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="http://localhost:22940"></a>
<img src="https://github.com/xingyujie/binpython/blob/main/py.ico?raw=true" width="30" height="30" class="d-inline-block align-top" alt="">
BINPython IDEPlus
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="http://localhost:22940/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://localhost:22940/?app=ideplus">IDEPlus</a>
</li>
</a>
</ul>
</div>
</nav>
""")
def plushead():
set_env(title="BINPython IDE Plus", auto_scroll_bottom=True)
put_html("""
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="http://localhost:22940"></a>
<img src="https://github.com/xingyujie/binpython/blob/main/py.ico?raw=true" width="30" height="30" class="d-inline-block align-top" alt="">
BINPython IDEPlus
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="http://localhost:22940/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="http://localhost:22940/?app=ideplus">IDEPlus</a>
</li>
</a>
</ul>
</div>
</nav>
""")
def aboutideplus():
head()
put_markdown("## About BINPython IDEPlus")
put_markdown("""
BINPython is made by:
[xingyujie(Edward Hsing)](https://github.com/xingyujie)
AGPL-V3.0 Release
""")
def welcomecard():
put_html("""
<div class="jumbotron">
<h1 class="display-3">Welcome to BINPython!</h1>
<p class="lead">This is a portable IDE environment for BINPython</p>
<hr class="my-4">
<p>Fast and portable, runs via BINPython</p>
<p class="lead">
<a class="btn btn-primary btn-lg" href="http://localhost:22940/?app=ideplus" role="button">Try IDE Plus!</a>
</p>
</div>
""")
#index ui
def index():
head()
welcomecard()
put_markdown("## Features")
put_link("IDEPlus", url='http://localhost:22940/?app=ideplus')
line()
put_link("About BINPython", url='http://localhost:22940/?app=aboutideplus')
line()
put_link("View Code", url='http://localhost:22940/?app=viewcode')
def idehead():
set_env(title="BINPython IDE Plus", auto_scroll_bottom=True)
put_html("<h1>BINPython IDE Plus</h1>")
put_text('Welcome to BINPython IDE Plus, Please type code',
sep=' '
)
line()
toast("BINPython IDE Plus is a beta version. May be removed or changed in the future")
#ideplus
def ideplus():
plushead()
idehead()
res = textarea('BINPython IDE Plus', rows=45, code={
'mode': "python",
'theme': 'darcula'
})
exec(res)
toast("Run finished!")
put_success("Finished, Please see BINPython Window or Terminal")
put_text("Here is the code you wrote")
put_code(res, language='python')
savecodefilename = pywebio.input.input('Do you want to save the code you wrote to a file? Enter a filename and save it')
f = open(savecodefilename, 'wb+')
f.write(res.encode("utf-8"))
toast("Successfully saved")
put_success("The save is successful, and the code is saved to binpython file path" + " \nThe file name is " + '"' + savecodefilename + '"')
#view code
def viewcode():
plushead()
viewcode_code = pywebio.input.input("Please input file path:")
f = open(viewcode_code,encoding = "utf-8")
put_code(f.read(), language='python')
if __name__ == '__main__':
start_server([index, ideplus, aboutideplus, viewcode], debug=True, port= 22940)
pywebio.session.hold()
#binpython examples
if opt_name in ('-e','--example'):
print("Welcome to BINPython example")
print("""
List of examples:
1.Turtle
2.Tkinter GUI
3.Tornado WEB Server
4.PywebIO WEB
5.Hello World
6.HTTP requests
7.BINPython() function example
""")
examplenum = input("Please enter the option you want to enter(like 1):")
#turtle
if examplenum == "1":
def exampleturtle():
import turtle
import time
turtle.speed(10)
turtle.color("red", "yellow")
turtle.begin_fill()
for _ in range(50):
turtle.forward(200)
turtle.left(170)
turtle.end_fill()
turtle.done()
exampleturtle()
sys.exit()
#tkinter
if examplenum == "2":
def exampletkinter():
import tkinter as tk
app = tk.Tk()
app.title('BINPython tkinter example')
theLabel = tk.Label(app, text='Welcome to BINPython!!!')
theLabel.pack()
app.mainloop()
exampletkinter()
sys.exit()
#tornado
if examplenum == "3":
def exampletornado():
import asyncio
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, BINPython!")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
async def main():
app = make_app()
app.listen(8080)
await asyncio.Event().wait()
if __name__ == "__main__":
asyncio.run(main())
exit()
print("Server Started listen on port:8080(http://ip:8080)")
exampletornado()
sys.exit()
#pywebio
if examplenum == "4":
import pywebio.input
from pywebio.input import *
from pywebio.output import *
from pywebio import *
from pywebio.session import *
print("Listen on port 9000(http://ip:9000)")
def examplepywebio():
def main():
set_env(title="Welcome to BINPython", auto_scroll_bottom=True)
put_html("<h1>Welcome to BINPython</h1>")
if __name__ == '__main__':
start_server(main, debug=True, host='0.0.0.0', port='9000')
pywebio.session.hold()
examplepywebio()
sys.exit()
#helloworld
if examplenum == "5":
binpythonwin.setwindowtitle("Hi, Welcome to BINPython")
name = input("hi...What's your name:")
print("hi," + name)
sys.exit()
#requests
if examplenum == "6":
import requests
def examplerequests():
print(requests.get("http://google.com"))
examplerequests()
sys.exit()
#binpython function
if examplenum == "7":
print("BINPython() function example")
import time
binpythoninfo.ver()
binpythoninfo.buildversion()
binpythoninfo.libs_warning()
binpythoninfo.build_importlibs()
binpythonwin.setwindowtitle("Title name(str)")
binpythonallconf()
time.sleep(5)
#main shell
binpython_shell()
sys.exit()
#go main shell
#custom welcome script
try:
f = open("binpython_config/welcome.py",encoding = "utf-8")
exec(f.read())
print("Powered by: BINPython[https://github.com/xingyujie/binpython] AGPL 3.0")
except:
binpython_welcome_text()
#debug mode show many info
try:
f = open("binpython_debug",encoding = "utf-8")
print("Debug mode:on")
print("BINPython debug INFO")
def TestPlatform():
import platform
print(platform.python_version())
print(platform.architecture())
print(platform.node())
print(platform.platform())
print(platform.processor())
print(platform.python_build())
print(platform.python_compiler())
print(platform.python_implementation())
print(platform.python_revision())
print(platform.release())
print(platform.system())
print(platform.version())
print(platform.uname())
TestPlatform()
except:
pass
#binpython custom function plugin
#plugin start
class binpythonplugin:
plugin_name = ''
def name(key):
global plugin_name
plugin_name = key
#print(plugin_name)
plugin_anthor = ''
def anthor(key):
global plugin_anthor
plugin_anthor = key
#description
plugin_description = ''
def description(key):
global plugin_description
plugin_description = key
def showinfo():
print("BINPython Plugin Info:")
print("Plugin Name: " + plugin_name)
print("Plugin Anthor: " + plugin_anthor)
print("Plugin description: " + plugin_description)
plugin_loadmain = ''
def loadmain(key):
global plugin_loadmain
plugin_loadmain = key
#binpython_plugin_loadmain("function.py")
try:
f = open("binpython_plugin/pluginconfig.binpython",encoding = "utf-8")
exec(f.read())
f = open("binpython_plugin/" + plugin_loadmain,encoding = "utf-8")
exec(f.read())
binpython_shell()
except:
pass
try:
filename = open("startupfile.conf",encoding = "utf-8")
startupcode = open(filename.read(),encoding = "utf-8")
exec(startupcode.read())
except:
binpython_shell()
#go shell
binpython_shell()