aboutsummaryrefslogtreecommitdiff
path: root/src/avp/cli.py
blob: 0176f76ce4737c17327dd8bb881ccbcc642f0bf2 (plain)
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
from PyQt6.QtWidgets import QApplication
import sys
import logging
import re
import string


from .core import Core

# Core class must store settings as class variables
# before we can use many things in AVP
Core.storeSettings()
log = logging.getLogger("AVP.Main")


def main() -> int:
    """Returns an exit code (0 for success)"""
    proj = None
    mode = "GUI"

    # Determine whether we're in GUI or commandline mode
    if len(sys.argv) > 2:
        mode = "commandline"
    elif len(sys.argv) == 2:
        if sys.argv[1].startswith("-"):
            mode = "commandline"
        else:
            # remove unsafe punctuation characters such as \/?*&^%$#
            if sys.argv[1].endswith(".avp"):
                # remove file extension
                sys.argv[1] = sys.argv[1][:-4]
            sys.argv[1] = re.sub(f"[{re.escape(string.punctuation)}]", "", sys.argv[1])
            # opening a project file with gui
            proj = sys.argv[1]

    # Create Qt Application
    app = QApplication(sys.argv)
    app.setApplicationName("audio-visualizer")

    screen = app.primaryScreen()
    if screen is None:
        dpi = None
        log.error("Could not detect DPI")
    else:
        dpi = screen.physicalDotsPerInchX()
        log.info("Detected screen DPI: %s", dpi)

    # Launch program
    if mode == "commandline":
        from .command import Command

        main = Command()
        mode = main.parseArgs()
        log.debug("Finished creating command object")

    # Both branches here may occur in one execution:
    # Commandline parsing could change mode back to GUI
    if mode == "GUI":
        from avp.gui.mainwindow import MainWindow

        mainWindow = MainWindow(proj, dpi)
        log.debug("Finished creating MainWindow")
        mainWindow.raise_()

    return app.exec()


if __name__ == "__main__":
    sys.exit(main())