66import os
77import sys
88from pathlib import Path
9+ from typing import List , Tuple
910
1011from munch import Munch
1112
12- if sys .version_info < (3 , 8 ):
13- from importlib_metadata import version
14- else :
15- from importlib .metadata import version
16-
1713if sys .version_info < (3 , 10 ):
18- import importlib_resources
14+ import importlib_resources # type: ignore[import-not-found]
1915else :
2016 import importlib .resources as importlib_resources
2117
22- VERSION = version ('procman' )
23-
2418
2519class FileTypeError (Exception ):
2620 """Raise when the file extension is not '.yml' or '.yaml'"""
2721
2822 __module__ = Exception .__module__
2923
3024
31- def load_config (file_encoding = 'utf-8' , file_extension = '.yaml' ):
25+ def load_config (
26+ ufile : str = '' , file_encoding : str = 'utf-8' , file_extension : str = '.yaml'
27+ ) -> Tuple [Munch , Path ]:
3228 """
3329 Load yaml configuration file and munchify the data. If ENV path or local
3430 file is not found in current directory, the default cfg will be loaded.
3531
32+ :param ufile: path string for config file
33+ :type ufile: str
3634 :param file_encoding: file encoding of config file
3735 :type file_encoding: str
3836 :param file_extension: file extension with leading separator
3937 :type file_extension: str
40- :return tuple : Munch cfg obj, Path obj
38+ :return: Munch and Path objects
4139 :raises FileTypeError: if the input file is not yml
4240 """
4341 proc_cfg = os .getenv ('PROCMAN_CFG' , default = '' )
44- defconfig_file = f'.procman{ file_extension } '
42+ defconfig_file = ufile or f'.procman{ file_extension } '
4543
4644 cfgfile = Path (proc_cfg ) if proc_cfg else Path (defconfig_file )
4745 if not cfgfile .name .lower ().endswith (('.yml' , '.yaml' )):
48- raise FileTypeError (f"FileTypeError: unknown file extension: { cfgfile .name } " )
46+ msg = 'invalid YAML extension: %s' % cfgfile .name
47+ raise (FileTypeError (msg ))
4948 if not cfgfile .exists ():
5049 cfgobj = load_base_config ()
5150 else :
@@ -55,22 +54,23 @@ def load_config(file_encoding='utf-8', file_extension='.yaml'):
5554 return cfgobj , cfgfile .resolve ()
5655
5756
58- def get_userscripts (demo_mode = False , file_encoding = 'utf-8' , file_extension = '.yaml' ) :
57+ def get_userscripts (usr_cfg : Munch , usr_file : Path , demo_mode : bool = False ) -> List [ str ] :
5958 """
6059 Get user scripts from Munchified user cfg.
6160
6261 :return: list of scripts
6362 """
64- uscripts = []
65- usr_cfg , usr_file = load_config (file_encoding , file_extension )
63+ uscripts : List = []
6664 ucfg = load_base_config () if not usr_file .exists () or demo_mode else usr_cfg
6765 for item in [x for x in ucfg .scripts if x .proc_enable ]:
6866 proc_list = [item .proc_label ]
6967 if demo_mode :
7068 if getattr (sys , 'frozen' , False ):
7169 scripts_path = os .path .join (os .path .dirname (sys .executable ), 'procman' )
7270 else :
73- scripts_path = importlib_resources .files ('procman' )
71+ pkg_path = importlib_resources .files ('procman' )
72+ with importlib_resources .as_file (pkg_path ) as path :
73+ scripts_path = str (path )
7474 proc_str = (
7575 f'{ item .proc_runner } ' if item .proc_runner else ''
7676 ) + f'{ os .path .join (scripts_path , item .proc_dir , item .proc_name )} '
@@ -92,7 +92,7 @@ def get_userscripts(demo_mode=False, file_encoding='utf-8', file_extension='.yam
9292 return uscripts
9393
9494
95- def load_base_config ():
95+ def load_base_config () -> Munch :
9696 """
9797 Load initial procman config with our baseline example values. This is
9898 used to both run the example flask app and provide a user-facing example
0 commit comments