11# -*- coding: utf-8 -*-
2- from .logger import logger
3- logger .debug ("Started 'reading flika.py'" )
4- import sys , os
2+ """
3+ Main module for the flika application.
4+ """
5+
6+ # Standard library imports
7+ import os
8+ import sys
59import platform
610import optparse
711import warnings
12+ import pathlib
13+ from typing import Any
814
915# Set Jupyter to use platformdirs (fixes deprecation warning)
1016os .environ ['JUPYTER_PLATFORM_DIRS' ] = '1'
1117
18+ # Third-party imports
19+ import numpy as np
20+ import beartype
21+
22+ # Local application imports
23+ from flika .logger import logger
24+ from flika .version import __version__
25+ from flika .app .application import FlikaApplication
26+
27+ logger .debug ("Started 'reading flika.py'" )
1228logger .debug ("Started 'reading flika.py, importing numpy'" )
13- import numpy as np # type: ignore
1429logger .debug ("Completed 'reading flika.py, importing numpy'" )
15- from .version import __version__
16- from .app .application import FlikaApplication
1730
1831# Filter out known warnings
1932warnings .filterwarnings ("ignore" , category = np .exceptions .VisibleDeprecationWarning )
2033
21- def parse_arguments (argv ):
34+ @beartype .beartype
35+ def parse_arguments (argv : list [str ]) -> tuple [Any , list [str ]]:
2236 ''' Parses command line arguments for valid flika args
2337
2438 Arguments:
@@ -59,19 +73,19 @@ def parse_arguments(argv):
5973
6074 err_msg = verify (parser , argv )
6175 if err_msg :
62- sys .stderr .write ('\n %s \n ' % err_msg )
76+ sys .stderr .write (f '\n { err_msg } \n ' )
6377 parser .print_help ()
6478 sys .exit (1 )
6579
6680 return parser .parse_args (argv )
6781
68- def verify ( parser , argv ):
69- """ verify(parser, argv)
70- Check for input errors
82+ @ beartype . beartype
83+ def verify (parser : optparse . OptionParser , argv : list [ str ]) -> str | None :
84+ """ Check for input errors
7185
7286 Arguments:
7387 parser: OptionParser instance
74- argv (list) : Argument list
88+ argv: Argument list
7589
7690 Returns:
7791 An error message in the event of an input error, or None
@@ -83,32 +97,37 @@ def verify(parser, argv):
8397
8498 return err_msg
8599
86- def ipython_qt_event_loop_setup ():
100+ @beartype .beartype
101+ def ipython_qt_event_loop_setup () -> None :
102+ """Set up the IPython Qt event loop if running inside IPython."""
87103 try :
88104 __IPYTHON__
89105 except NameError :
90106 return # If __IPYTHON__ is not defined, we are not in ipython
91107 else :
92108 print ("Starting flika inside IPython" )
93- from IPython import get_ipython # type: ignore
109+ from IPython import get_ipython
94110 ipython = get_ipython ()
95111 ipython .run_line_magic ("gui" , "qt" )
96112
97- def load_files (files ):
98- from .process .file_ import open_file
113+ @beartype .beartype
114+ def load_files (files : list [str ]) -> None :
115+ from flika .process .file_ import open_file
99116 for f in files :
100117 open_file (f )
101118
102- def start_flika (files = []):
119+ @beartype .beartype
120+ def start_flika (files : list [str ]| None = None ) -> FlikaApplication :
103121 """Run a flika session and exit, beginning the event loop
104122
105123 Parameters:
106- files (list) : An optional list of data files to load.
124+ files: An optional list of data files to load.
107125
108126 Returns:
109127 A flika application object with optional files loaded
110-
111128 """
129+ if files is None :
130+ files = []
112131 logger .debug ("Started 'flika.start_flika()'" )
113132 print ('Starting flika' )
114133 fa = FlikaApplication ()
@@ -118,26 +137,37 @@ def start_flika(files=[]):
118137 logger .debug ("Completed 'flika.start_flika()'" )
119138 return fa
120139
121- def exec_ ():
140+ @beartype .beartype
141+ def exec_ () -> int :
142+ """Execute the flika application."""
122143 fa = start_flika (sys .argv [1 :])
123144 return fa .app .exec_ ()
124145
125- def post_install ():
146+ @beartype .beartype
147+ def post_install () -> None :
126148 if platform .system () == 'Windows' :
127149 print ("Creating start menu shortcut..." )
128- import winshell # type: ignore
129- icon_path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'images' , 'favicon.ico' )
130- flika_exe = os .path .join (sys .exec_prefix , 'Scripts' , 'flika.exe' )
131- link_path = os .path .join (winshell .programs (), "flika.lnk" )
132- with winshell .shortcut (link_path ) as link :
133- link .path = flika_exe
134- link .description = "flika"
135- link .icon_location = (icon_path , 0 )
136- link_path = os .path .join (winshell .desktop (), "flika.lnk" )
137- with winshell .shortcut (link_path ) as link :
138- link .path = flika_exe
139- link .description = "flika"
140- link .icon_location = (icon_path , 0 )
150+ try :
151+ import winshell
152+ import importlib .resources as pkg_resources
153+ from flika import images
154+
155+ # Use importlib.resources instead of os.path
156+ with pkg_resources .path (images , 'favicon.ico' ) as icon_path :
157+ # Use Path for path manipulation
158+ flika_exe = pathlib .Path (sys .exec_prefix ) / 'Scripts' / 'flika.exe'
159+ link_path = pathlib .Path (winshell .programs ()) / "flika.lnk"
160+ with winshell .shortcut (str (link_path )) as link :
161+ link .path = str (flika_exe )
162+ link .description = "flika"
163+ link .icon_location = (str (icon_path ), 0 )
164+ link_path = pathlib .Path (winshell .desktop ()) / "flika.lnk"
165+ with winshell .shortcut (str (link_path )) as link :
166+ link .path = str (flika_exe )
167+ link .description = "flika"
168+ link .icon_location = (str (icon_path ), 0 )
169+ except ImportError :
170+ print ("winshell package not found. Shortcuts not created." )
141171
142172if __name__ == '__main__' :
143173 start_flika (sys .argv [1 :])
0 commit comments