Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 65 additions & 69 deletions src/PyMca5/PyMcaGui/pymca/PyMcaMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
import os
import sys, getopt
import sys, argparse
import traceback
import logging
if sys.platform == 'win32':
Expand All @@ -40,79 +40,75 @@
_logger = logging.getLogger(__name__)
backend=None
if __name__ == '__main__':
options = '-f'
longoptions = ['spec=',
'shm=',
'debug=',
'qt=',
'backend=',
'nativefiledialogs=',
'PySide=',
'binding=',
'logging=',
'test']
try:
opts, args = getopt.getopt(
sys.argv[1:],
options,
longoptions)
except getopt.error:
print("%s" % sys.exc_info()[1])
sys.exit(1)
parser = argparse.ArgumentParser(description='PyMca X-Ray Fluorescence Toolkit main application.')
parser.add_argument('-f', '--fresh', action='store_true', help='Bypass user-defined default settings')
parser.add_argument('--spec', help='Specify spec file')
parser.add_argument('--shm', help='Shared memory key')
parser.add_argument('--debug', default='0', help='Enable debug mode (0 or 1)')
parser.add_argument('--qt', help='Qt version')
parser.add_argument('--backend', help='Select graphics backend: mpl (matplotlib), gl (OpenGL), silx')
parser.add_argument('--nativefiledialogs', type=int, choices=[0,1], help='Use native file dialogs (X=1) or Qt dialogs (X=0)')
parser.add_argument('--binding', choices=['pyqt5', 'pyside2', 'pyside6', 'pyqt6'], help='Select Qt binding: PyQt5 (default), PyQt6, PySide6, PySide2')
parser.add_argument('--logging', help='Set logging level: critical, error, warning (default), info, debug or numeric values 0 (critical) to 4 (debug)')
parser.add_argument('--test', action='store_true', help='Run PyMca unit tests and exit')
parser.add_argument('--PySide', help=argparse.SUPPRESS) # deprecated
parser.add_argument('sources', nargs='*', help='Data sources to open')
args = parser.parse_args()

keywords={}
debugreport = 0
qtversion = None
binding = None
for opt, arg in opts:
if opt in ('--spec'):
keywords['spec'] = arg
elif opt in ('--shm'):
keywords['shm'] = arg
elif opt in ('--debug'):
if arg.lower() not in ['0', 'false']:
debugreport = 1
_logger.setLevel(logging.DEBUG)
# --debug is also parsed later for the global logging level
elif opt in ('-f'):
keywords['fresh'] = 1
elif opt in ('--qt'):
qtversion = arg
elif opt in ('--backend'):
backend = arg
elif opt in ('--nativefiledialogs'):
if int(arg):
nativeFileDialogs = True
else:
nativeFileDialogs = False
elif opt in ('--PySide'):
print("Please use --binding=PySide6")
if args.fresh:
keywords['fresh'] = 1
if args.spec:
keywords['spec'] = args.spec
if args.shm:
keywords['shm'] = args.shm
if args.debug.lower() not in ['0', 'false']:
debugreport = 1
_logger.setLevel(logging.DEBUG)
if args.qt:
qtversion = args.qt
if args.backend:
backend = args.backend
if args.nativefiledialogs is not None:
nativeFileDialogs = bool(args.nativefiledialogs)
if args.PySide:
print("Please use --binding=PySide6")
import PySide6.QtCore
if args.binding:
binding = args.binding.lower()
if binding == "pyqt5":
import PyQt5.QtCore
elif binding == "pyside2":
import PySide2.QtCore
elif binding == "pyside6":
import PySide6.QtCore
elif opt in ('--binding'):
binding = arg.lower()
if binding == "pyqt5":
import PyQt5.QtCore
elif binding == "pyside2":
import PySide2.QtCore
elif binding == "pyside6":
import PySide6.QtCore
elif binding == "pyqt6":
import PyQt6.QtCore
else:
raise ValueError("Unknown Qt binding <%s>" % binding)
elif opt in ('--test'):
try:
from PyMca5.tests import TestAll
print("Running PyMca Unit Tests...")
result = TestAll.main()
exit_code = 0 if result.wasSuccessful() else 1
print('exit code: ', exit_code)
sys.exit(exit_code)
except Exception as e:
import traceback
print("Failed to run tests:", e)
traceback.print_exc()
sys.exit(1)
elif binding == "pyqt6":
import PyQt6.QtCore
else:
raise ValueError("Unknown Qt binding <%s>" % binding)
if args.test:
try:
from PyMca5.tests import TestAll
print("Running PyMca Unit Tests...")
result = TestAll.main()
exit_code = 0 if result.wasSuccessful() else 1
print('exit code: ', exit_code)
sys.exit(exit_code)
except Exception as e:
import traceback
print("Failed to run tests:", e)
traceback.print_exc()
sys.exit(1)

# For logging
opts = []
if args.logging:
opts.append(('--logging', args.logging))
if args.debug.lower() not in ['0', 'false']:
opts.append(('--debug', args.debug))

from PyMca5.PyMcaCore.LoggingLevel import getLoggingLevel
logging.basicConfig(level=getLoggingLevel(opts))
Comment on lines +107 to 114
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need getLoggingLevel here?

Copy link
Copy Markdown
Collaborator

@woutdenolf woutdenolf Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially we have one log level

log_level = "debug" if args.debug else args.logging

Something like

import argparse
import logging

parser = argparse.ArgumentParser(description="Example script with logging.")
parser.add_argument(
    "--logging",
    default="warning",
    choices=["debug", "info", "warning", "error", "critical"],
    help="Set logging level (default: warning)"
)
parser.add_argument(
    "--debug",
    action="store_true",
    help="Force logging level to DEBUG"
)

args = parser.parse_args()

if args.debug:
    log_level = logging.DEBUG
else:
    log_level = getattr(logging, args.logging.upper(), logging.WARNING)

logging.basicConfig(level=log_level)

Copy link
Copy Markdown
Collaborator

@woutdenolf woutdenolf Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this logic can be implemented in a re-usable way.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need backward compatibility? if yes then to forbid numerical values will be an issue.
If not this is a good looking approach.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need it.

Expand Down Expand Up @@ -1864,7 +1860,7 @@ def mousePressEvent(self,event):

#try to interpret rest of command line arguments as data sources
try:
for source in args:
for source in args.sources:
PyMcaMainWidgetInstance.sourceWidget.sourceSelector.openSource(source)
except Exception:
msg = qt.QMessageBox(PyMcaMainWidgetInstance)
Expand Down