Skip to content

Commit

Permalink
change module: getopt to argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
eltonfabricio10 committed Nov 27, 2022
1 parent 9272911 commit 49bb323
Show file tree
Hide file tree
Showing 7 changed files with 714 additions and 783 deletions.
7 changes: 3 additions & 4 deletions bigbashview/usr/lib/python3/dist-packages/bbv/globaldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import os

APP_NAME = "BigBashView"
APP_VERSION = "3.6.6"
APP_VERSION = "3.6.8"
PROGDIR = os.path.dirname(os.path.realpath(__file__))
ICON = os.sep.join((PROGDIR, "img", "icone.png"))
LOGO = os.sep.join((PROGDIR, "img", "logo.png"))
TITLE = None
ROOT_FILE = False

def ADDRESS(): return '127.0.0.1'
def PORT(): return 9000
ADDRESS = '127.0.0.1'
PORT = 19000
289 changes: 113 additions & 176 deletions bigbashview/usr/lib/python3/dist-packages/bbv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (C) 2008 Wilson Pinto Júnior <wilson@openlanhouse.org>
# Copyright (C) 2011 Thomaz de Oliveira dos Reis <thor27@gmail.com>
# Copyright (C) 2009 Bruno Goncalves Araujo
# Copyright (C) 2019 Elton Fabrício Ferreira <eltonfabricio10@gmail.com>
# Copyright (C) 2022 Elton Fabrício Ferreira <eltonfabricio10@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -17,148 +17,144 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import argparse
import sys
import os
import getopt

from bbv import globaldata
from bbv.server.bbv2server import run_server

class Main:
width = 0
height = 0
toolkit = "auto"
url = "/"
window_state = None
color = None

class Main:
"""Start bbv"""
def __init__(self):
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], 'hs:vn:w:i:c:t:', ['help', 'size=', 'version', "name=",
'window_state=', 'icon=', 'color=', 'toolkit='])
except getopt.error as msg:
print(msg)
print('For help use -h or --help')
sys.exit(2)

if args:
self.url = args[0]

for o, a in opts:
if o in ('-h', '--help'):
self.help()

elif o in ('-v', '--version'):
print(globaldata.APP_NAME, globaldata.APP_VERSION)
sys.exit()

elif o in ('-s', '--size'):
args = a.split('x')

if len(args) != 2:
self.help()

for i in args:
if not i.isdigit():
self.help()

self.width, self.height = args

# Window Size
self.width = int(self.width)
self.height = int(self.height)

elif o in ('-t', '--toolkit'):
if a in ("gtk", "qt"):
self.toolkit = a

elif o in ('-w', '--window_state'):
if a in ("maximized", "maximizedTop", "fullscreen", "fixed",
"fixedTop", "frameless", "framelessTop"):
self.window_state = a

elif o in ('-i', '--icon'):
if os.path.exists(a):
globaldata.ICON = a

elif o in ('-c', '--color'):
if a in ('black', 'none'):
self.color = a
def formatter(prog):
return argparse.RawTextHelpFormatter(
prog,
max_help_position=100,
width=None)

parser = argparse.ArgumentParser(
prog='bigbashview',
description='''
____ _ ____ _ __ ___
| _ \\(_) | _ \\ | |\\ \\ / (_)
| |_) |_ __ _| |_) | __ _ ___| |_\\ \\ / / _ _____ __
| _ <| |/ _` | _ < / _` / __| '_ \\ \\/ / | |/ _ \\ \\ /\\ / /
| |_) | | (_| | |_) | (_| \\__ \\ | | \\ / | | __/\\ V V /
|____/|_|\\__, |____/ \\__,_|___/_| |_|\\/ |_|\\___| \\_/\\_/
__/ |
|___/
elif o in ('-n', '--name'):
globaldata.TITLE = a
BigBashView is a script to run Bash+HTML in a Desktop WebView.
''',
epilog='''
PlainText Extension: .txt Text Content
--------------------------------------------------------
HTML Extensions: .html |.htm HTML Content
--------------------------------------------------------
Executable Extensions: .sh |.run Shell Script
.sh.html|.sh.htm Html Markup
.sh.php PHP Script
.sh.py Python Script
.sh.lua Lua Script
.sh.rb Ruby Script
.sh.pl Perl Script
.sh.lisp Lisp Script
.sh.jl Julia Script
--------------------------------------------------------
Note: All executable files must have the shebang''',
formatter_class=formatter)

parser.add_argument('url', default='/', nargs='?', help='URL/File')
parser.add_argument(
'-v', '--version',
action='version', version=f'%(prog)s {globaldata.APP_VERSION}',
help='BigBashView Version')
parser.add_argument(
'-c', '--color', default=None,
help='Background Color: "black" or "none"')
parser.add_argument(
'-i', '--icon', default=globaldata.ICON,
help='Window Icon: /path/to/image')
parser.add_argument('-n', '--name', help='Window Title: "Title"')
parser.add_argument(
'-s', '--size', default='0x0',
help='Window Size: [width]x[height](800x600)')
parser.add_argument(
'-t', '--toolkit',
default='auto',
help='Rendering by QtWebEngine or WebKitGTK2: qt or gtk')
parser.add_argument(
'-w', '--window_state', default=None,
help='''Window state: fullscreen, maximized, maximizedTop,
fixed, fixedTop, frameless, framelessTop''')

args = parser.parse_args()
self.url = args.url
globaldata.TITLE = args.name
geom = args.size.split('x')
try:
width, height = geom
self.width = int(width)
self.height = int(height)
except Exception:
parser.print_help()
sys.exit(1)

if os.path.exists(args.icon):
globaldata.ICON = args.icon

if args.color in ['black', 'none', None]:
self.color = args.color
else:
parser.print_help()
sys.exit(1)

if args.toolkit in ['auto', 'qt', 'gtk']:
self.toolkit = args.toolkit
else:
parser.print_help()
sys.exit(1)

if args.window_state in [
'fullscreen', 'maximized',
'maximizedTop', 'fixed',
'fixedTop', 'frameless',
'framelessTop', None
]:
self.window_state = args.window_state
else:
parser.print_help()
sys.exit(1)

# construct window
if self.toolkit == "auto":
try:
from bbv.ui import qt
has_qt = True
self.toolkit = 'qt'
except ImportError as e:
print(e)
has_qt = False

try:
from bbv.ui import gtk
has_gtk = True
except ImportError as e:
print(e)
has_gtk = False

if not has_qt and not has_gtk:
print(('bbv needs WebKitGtk2 or PySide2 '
'to run. Please install '
'the latest stable version'), file=sys.stderr)
sys.exit(1)
self.toolkit = 'gtk'

elif has_qt:
os.environ['QTWEBENGINE_CHROMIUM_FLAGS'] = '--disable-logging --disable-gpu --no-sandbox --single-process --disable-gpu-compositing --autoplay-policy=no-user-gesture-required --font-render-hinting=none'
os.environ['QT_QUICK_BACKEND'] = 'software'
os.environ['QT_XCB_GL_INTEGRATION'] = 'none'
os.environ['QSG_RENDER_LOOP'] = 'basic'
os.environ['QTWEBENGINE_DISABLE_SANDBOX'] = '1'
self.window = qt.Window()

elif has_gtk:
os.environ['GDK_BACKEND'] = 'x11'
os.environ['WEBKIT_FORCE_SANDBOX'] = '0'
self.window = gtk.Window()
if globaldata.TITLE:
self.window.set_role(globaldata.TITLE)

elif self.toolkit == "gtk":
if self.toolkit == "gtk":
try:
from bbv.ui import gtk
has_gtk = True
except ImportError as e:
print(e)
has_gtk = False

if not has_gtk:
print(('bbv needs WebKitGtk2 '
'to run. Please install '
'the latest stable version'), file=sys.stderr)

print('Please install WebKitGtk2')
sys.exit(1)
os.environ['GDK_BACKEND'] = 'x11'
os.environ['WEBKIT_FORCE_SANDBOX'] = '0'
self.window = gtk.Window()
if globaldata.TITLE:
self.window.set_role(globaldata.TITLE)
self.window.set_wmclass(globaldata.TITLE, globaldata.TITLE)

elif self.toolkit == "qt":
if self.toolkit == "qt":
try:
from bbv.ui import qt
has_qt = True
except ImportError as e:
print(e)
has_qt = False

if not has_qt:
print(('bbv needs PySide2 '
'to run. Please install '
'the latest stable version'), file=sys.stderr)

print('Please install PySide6')
sys.exit(1)
os.environ['QTWEBENGINE_CHROMIUM_FLAGS'] = '--disable-logging --disable-gpu --no-sandbox --single-process --disable-gpu-compositing --autoplay-policy=no-user-gesture-required --font-render-hinting=none'
os.environ['QT_QUICK_BACKEND'] = 'software'
Expand All @@ -167,75 +163,16 @@ def __init__(self):
os.environ['QTWEBENGINE_DISABLE_SANDBOX'] = '1'
self.window = qt.Window()

@staticmethod
def help():
helper = r'''
____ _ ____ _ __ ___
| _ \(_) | _ \ | |\ \ / (_)
| |_) |_ __ _| |_) | __ _ ___| |_\ \ / / _ _____ __
| _ <| |/ _` | _ < / _` / __| '_ \ \/ / | |/ _ \ \ /\ / /
| |_) | | (_| | |_) | (_| \__ \ | | \ / | | __/\ V V /
|____/|_|\__, |____/ \__,_|___/_| |_|\/ |_|\___| \_/\_/
__/ |
|___/
~$bigbashview options arguments URL
==================================================================
Options Arguments Descriptions
------------------------------------------------------------------
-s|--size= widthxheight Window size
------------------------------------------------------------------
-t|--toolkit= gtk Rendering by WebKitGTK2
qt Rendering by QtWebEngine
------------------------------------------------------------------
-w|--window_state= fullscreen Open window in fullscreen
maximized Open maximized window
maximizedTop Open maximized window
and keep window on top
fixed Open window in fixed size
fixedTop Open window in fixed size
and keep window on top
frameless Open window frameless
framelessTop Open window frameless
and keep window on top
------------------------------------------------------------------
-i|--icon= /path/to/image Window icon
------------------------------------------------------------------
-n|--name= AppName Window title
------------------------------------------------------------------
-c|--color= black Black background
none Transparent background
------------------------------------------------------------------
-h|--help BigBashView help
-v|--version BigBashView version
==================================================================
PlainText Extension: .txt Text Content
------------------------------------------------------------------
HTML Extensions: .html |.htm HTML Content
------------------------------------------------------------------
Executable Extensions: .sh |.run Shell Script
.sh.html|.sh.htm Html Markup
.sh.php PHP Script
.sh.py Python Script
.sh.lua Lua Script
.sh.rb Ruby Script
.sh.pl Perl Script
.sh.lisp Lisp Script
.sh.jl Julia Script
------------------------------------------------------------------
*Note: All executable files must have the shebang*
'''
print(helper)
sys.exit()

def run(self, start_server=True):
server = run_server() if start_server else None

if self.url.find('://') == -1:
if not self.url.startswith('/'):
self.url = '/'+self.url
self.url = "http://%s:%s%s" % (globaldata.ADDRESS(),
globaldata.PORT(), self.url)
self.url = "http://%s:%s%s" % (
globaldata.ADDRESS,
globaldata.PORT,
self.url)

self.window.set_size(self.width, self.height, self.window_state)
self.window.style(self.color)
Expand Down
Loading

0 comments on commit 49bb323

Please sign in to comment.