Skip to content

Commit

Permalink
move application specific files to a separate dir
Browse files Browse the repository at this point in the history
move globals back into qtpyvcp init
  • Loading branch information
KurtJacobson committed May 5, 2020
1 parent 16d1324 commit 9b78688
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 161 deletions.
9 changes: 5 additions & 4 deletions examples/mini/__init__.py
Expand Up @@ -2,9 +2,9 @@

"""Main entry point for MiniVCP.
This module contains the code necessary to be able to launch MiniVCP
directly from the command line, without using qtpyvcp. It handles
parsing command line args and starting the main application.
This module contains the code required to run MiniVCP directly, without
having to pass it as an argument to qtpyvcp. It handles parsing command
line args and starting the main application.
Example:
Assuming the dir this file is located in is on the PATH, you can
Expand Down Expand Up @@ -33,7 +33,8 @@ def main(opts=None):
vcp_name='Mini',
vcp_version=__version__)

qtpyvcp.run_vcp(opts, VCP_CONFIG_FILE)
# run the main application
qtpyvcp.app.run(opts, VCP_CONFIG_FILE)


if __name__ == '__main__':
Expand Down
132 changes: 6 additions & 126 deletions qtpyvcp/__init__.py
@@ -1,65 +1,8 @@
#!/usr/bin/env python

"""
QtPyVCP - Qt and Python based Virtual Control Panel framework for LinuxCNC.
Usage:
qtpyvcp --ini=INI [<vcp>] [options]
qtpyvcp (-h | --help)
qtpyvcp (-v | --version)
Required Arguments:
--ini INI Path to INI file, relative to ~/linuxcnc/configs.
Display Options:
--vcp VCP The name of the VCP to launch. If not specified a
graphical chooser dialog will be shown.
--theme THEME The Qt theme to use, defaults to system theme.
--stylesheet STYLESHEET
Path to QSS file containing styles to be applied
to specific Qt and/or QtPyVCP widget classes.
--size WIDTHxHEIGHT
Initial size of the window in pixels.
--position XPOSxYPOS
Initial position of the window, specified as the
coordinates of the top left corner of the window
relative to the top left corner of the screen.
--fullscreen BOOL Flag to start with window fullscreen.
--maximize BOOL Flag to start with window maximized.
--hide-menu-bar Hides the menu bar, if present.
--hide-status-bar Hides the status bar, if present.
--hide-cursor Hide the mouse cursor.
--confirm-exit BOOL Whether to show dialog to confirm exit.
Application Options:
--log-level=(DEBUG | INFO | WARN | ERROR | CRITICAL)
Sets the log level. Default INFO
--config-file=FILE Specifies the YML config file.
--log-file=FILE Specifies the log file. Overrides INI setting.
--pref-file=FILE Specifies the preference file. Overrides INI setting.
--qt-api=(pyqt5 | pyqt | pyside2 | pyside)
Specify the Qt Python binding to use.
--perfmon Monitor and log system performance.
--develop Development mode. Enables live reloading of QSS styles.
--command_line_args <args>...
Additional args passed to the QtApplication.
General Options:
--chooser Forces the graphical VCP chooser to be shown. If a VCP
was specified it will be ignored. Useful for overriding
a VCP specified in an INI file.
-h --help Show this help and exit.
-v --version Show version.
Note:
When specifying QtPyVCP in the INI using [DISPLAY]DISPLAY=qtpyvcp [...]
the --ini parameter will be passed by the linuxcnc startup script so does
not need to be specified.
"""
import os

import os
from collections import OrderedDict

from qtpyvcp.lib.types import DotDict

from ._version import get_versions
Expand All @@ -69,8 +12,6 @@
QTPYVCP_DIR = os.path.abspath(os.path.dirname(__file__))
TOP_DIR = os.path.dirname(QTPYVCP_DIR)

DEFAULT_CONFIG_FILE = os.path.join(QTPYVCP_DIR, 'yaml_lib/default_config.yml')

# globals
CONFIG = {}
OPTIONS = DotDict()
Expand All @@ -79,68 +20,7 @@
WINDOWS = {}
SETTINGS = {}


def main():
"""QtPyVCP Main entry point
This method is called when running `qtpyvcp` directly from the
command line. The command line options are generated from the
docstring at the beginning of this module.
"""
from qtpyvcp.utilities.opt_parser import parse_opts
opts = parse_opts(__doc__)
run_vcp(opts, None)


def run_vcp(opts, config_file=None):
"""VCP Entry Point
This method is used by individual VCPs to launch QtPyVCP. This
method should NOT be called directly. Options are generated either
from a specification in the VCP, or from a generic fallback generated
by the `opt_parser` utility.
Args:
opts (OptDict) : Dictionary of command line options as generated
by the opt_parser utility.
config_file (str, optional) : A YAML format config file to load.
If `config_file` is not None it will be merged with any other
config files and the VCP will be loaded from that info, else
the VCP will be loaded solely from the options passed in the
options dict.
"""
if config_file is None:
# we are probably running from a entry point or a bare .ui file
from qtpyvcp.vcp_launcher import load_vcp
load_vcp(opts)

else:
# almost certainly being called from a VCP's __init__.py file

# list of YAML config files, in order of highest to lowest priority
config_files = list()

# cmd line or INI files should have highest priority
if opts.config_file is not None:
config_files.append(opts.config_file)

# env files should have second highest priority
env_cfgs = os.getenv('VCP_CONFIG_FILES')
if env_cfgs is not None:
config_files.extend(env_cfgs.strip(':').split(':'))

# VCP specific config files
config_files.append(config_file)

# default config file has lowest priority
config_files.append(DEFAULT_CONFIG_FILE)

from qtpyvcp.utilities.config_loader import load_config_files
config = load_config_files(*config_files)

from qtpyvcp.vcp_launcher import launch_application
launch_application(opts, config)


if __name__ == '__main__':
main()
# for backward compatibility
from qtpyvcp.app import run
def run_vcp(*args, **kwargs):
run(*args, **kwargs)
37 changes: 19 additions & 18 deletions qtpyvcp/actions/program_actions.py
Expand Up @@ -494,42 +494,43 @@ def _optional_stop_bindOk(widget):

FILTER_TEMP = None

def openFilterProgram(self, fname, flt):
def openFilterProgram(infile, prog_name):
temp_dir = _mktemp()
tmp = os.path.join(temp_dir, os.path.basename(fname))
print 'temp', temp_dir
flt = FilterProgram(flt, fname, tmp, lambda r: r or self._loadFilterResult(tmp))
outfile = os.path.join(temp_dir, os.path.basename(infile) + ".ngc")
FilterProgram(prog_name, infile, outfile, lambda r: r or _loadFilterResult(outfile))

def _loadFilterResult(self, fname):
def _loadFilterResult(fname):
if fname:
CMD.program_open(fname)

def _mktemp(self):
def _mktemp():
global FILTER_TEMP
if FILTER_TEMP is not None:
return FILTER_TEMP
FILTER_TEMP = tempfile.mkdtemp(prefix='emcflt-', suffix='.d')
atexit.register(lambda: shutil.rmtree(FILTER_TEMP))
return FILTER_TEMP

# slightly reworked code from gladevcp
# loads a filter program and collects the result
progress_re = re.compile("^FILTER_PROGRESS=(\\d*)$")
class FilterProgram:
def __init__(self, program_filter, infilename, outfilename, callback=None):
def __init__(self, prog_name, infile, outfile, callback=None):
import subprocess
outfile = open(outfilename, "w")
infilename_q = infilename.replace("'", "'\\''")
outfile = open(outfile, "w")
infile = infile.replace("'", "'\\''")

env = dict(os.environ)
env['AXIS_PROGRESS_BAR'] = '1'
p = subprocess.Popen(["sh", "-c", "%s '%s'" % (program_filter, infilename_q)],
stdin=subprocess.PIPE,
stdout=outfile,
stderr=subprocess.PIPE,
env=env)
p.stdin.close() # No input for you
self.p = p
self.p = subprocess.Popen(["sh", "-c", "%s '%s'" % (prog_name, infile)],
stdin=subprocess.PIPE,
stdout=outfile,
stderr=subprocess.PIPE,
env=env)

self.p.stdin.close() # No input for you
self.stderr_text = []
self.program_filter = program_filter
self.program_filter = prog_name
self.callback = callback
# self.gid = STATUS.onValueChanged('periodic', self.update)
#progress = Progress(1, 100)
Expand All @@ -541,7 +542,7 @@ def update(self, w):
STATUS.disconnect(self.gid)
return False

r,w,x = select.select([self.p.stderr], [], [], 0)
r, w, x = select.select([self.p.stderr], [], [], 0)
if not r:
return True
stderr_line = self.p.stderr.readline()
Expand Down
131 changes: 131 additions & 0 deletions qtpyvcp/app/__init__.py
@@ -0,0 +1,131 @@
#!/usr/bin/env python

"""
QtPyVCP - Qt and Python based Virtual Control Panel framework for LinuxCNC.
Usage:
qtpyvcp --ini=INI [<vcp>] [options]
qtpyvcp (-h | --help)
qtpyvcp (-v | --version)
Required Arguments:
--ini INI Path to INI file, relative to ~/linuxcnc/configs.
Display Options:
--vcp VCP The name of the VCP to launch. If not specified a
graphical chooser dialog will be shown.
--theme THEME The Qt theme to use, defaults to system theme.
--stylesheet STYLESHEET
Path to QSS file containing styles to be applied
to specific Qt and/or QtPyVCP widget classes.
--size WIDTHxHEIGHT
Initial size of the window in pixels.
--position XPOSxYPOS
Initial position of the window, specified as the
coordinates of the top left corner of the window
relative to the top left corner of the screen.
--fullscreen BOOL Flag to start with window fullscreen.
--maximize BOOL Flag to start with window maximized.
--hide-menu-bar Hides the menu bar, if present.
--hide-status-bar Hides the status bar, if present.
--hide-cursor Hide the mouse cursor.
--confirm-exit BOOL Whether to show dialog to confirm exit.
Application Options:
--log-level=(DEBUG | INFO | WARN | ERROR | CRITICAL)
Sets the log level. Default INFO
--config-file=FILE Specifies the YML config file.
--log-file=FILE Specifies the log file. Overrides INI setting.
--pref-file=FILE Specifies the preference file. Overrides INI setting.
--qt-api=(pyqt5 | pyqt | pyside2 | pyside)
Specify the Qt Python binding to use.
--perfmon Monitor and log system performance.
--develop Development mode. Enables live reloading of QSS styles.
--command_line_args <args>...
Additional args passed to the QtApplication.
General Options:
--chooser Forces the graphical VCP chooser to be shown. If a VCP
was specified it will be ignored. Useful for overriding
a VCP specified in an INI file.
-h --help Show this help and exit.
-v --version Show version.
Note:
When specifying QtPyVCP in the INI using [DISPLAY]DISPLAY=qtpyvcp [...]
the --ini parameter will be passed by the linuxcnc startup script so does
not need to be specified.
"""

import os

import qtpyvcp

DEFAULT_CONFIG_FILE = os.path.join(qtpyvcp.QTPYVCP_DIR, 'yaml_lib/default_config.yml')


def main():
"""QtPyVCP Main entry point
This method is called when running `qtpyvcp` directly from the
command line. The command line options are generated from the
docstring at the beginning of this module.
"""
from qtpyvcp.utilities.opt_parser import parse_opts
opts = parse_opts(__doc__)
run(opts, None)


def run(opts, config_file=None):
"""VCP Entry Point
This method is used by individual VCPs to launch QtPyVCP. This
method should NOT be called directly. Options are generated either
from a specification in the VCP, or from a generic fallback generated
by the `opt_parser` utility.
Args:
opts (OptDict) : Dictionary of command line options as generated
by the opt_parser utility.
config_file (str, optional) : A YAML format config file to load.
If `config_file` is not None it will be merged with any other
config files and the VCP will be loaded from that info, else
the VCP will be loaded solely from the options passed in the
options dict.
"""
if config_file is None:
# we are probably running from a entry point or a bare .ui file
from qtpyvcp.app.launcher import load_vcp
load_vcp(opts)

else:
# almost certainly being called from a VCP's __init__.py file

# list of YAML config files, in order of highest to lowest priority
config_files = list()

# cmd line or INI files should have highest priority
if opts.config_file is not None:
config_files.append(opts.config_file)

# env files should have second highest priority
env_cfgs = os.getenv('VCP_CONFIG_FILES')
if env_cfgs is not None:
config_files.extend(env_cfgs.strip(':').split(':'))

# VCP specific config files
config_files.append(config_file)

# default config file has lowest priority
config_files.append(DEFAULT_CONFIG_FILE)

from qtpyvcp.utilities.config_loader import load_config_files
config = load_config_files(*config_files)

from qtpyvcp.app.launcher import launch_application
launch_application(opts, config)


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion qtpyvcp/application.py → qtpyvcp/app/application.py
Expand Up @@ -42,7 +42,7 @@ def __init__(self, theme=None, stylesheet=None, custom_fonts=[]):

opts = qtpyvcp.OPTIONS

from qtpyvcp.core import Prefs, Info
from qtpyvcp.utilities.prefs import Prefs, Info
self.info = Info()
self.prefs = Prefs()
self.status = getPlugin('status')
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion qtpyvcp/vcp_launcher.py → qtpyvcp/app/launcher.py
Expand Up @@ -150,7 +150,7 @@ def _load_vcp_from_ui_file(ui_file, opts):
from qtpyvcp.utilities.config_loader import load_config_files
cfg_files = [opts.config_file or '']
cfg_files.extend(os.getenv('VCP_CONFIG_FILES', '').split(':'))
cfg_files.append(qtpyvcp.DEFAULT_CONFIG_FILE)
cfg_files.append(qtpyvcp.app.DEFAULT_CONFIG_FILE)
config = load_config_files(*cfg_files)
kwargs = config['windows']['mainwindow'].get('kwargs', {})
kwargs.update({'ui_file': ui_file})
Expand Down
2 changes: 0 additions & 2 deletions qtpyvcp/core.py

This file was deleted.

0 comments on commit 9b78688

Please sign in to comment.