44This module allows you to read the program configuration file and return the values.
55"""
66
7- import configparser
87import argparse
98import os
9+ import yaml
10+ from typing import Any
1011from rich_argparse import MetavarTypeRichHelpFormatter
1112from pyproxy import __version__
1213
@@ -27,49 +28,49 @@ def parse_args() -> argparse.Namespace:
2728 )
2829 parser .add_argument (
2930 "-v" , "--version" , action = "version" , version = __version__ , help = "Show version"
30- ) # noqa: E501
31+ )
3132 parser .add_argument ("--debug" , action = "store_true" , help = "Enable debug logging" )
3233 parser .add_argument ("-H" , "--host" , type = str , help = "IP address to listen on" )
3334 parser .add_argument ("-P" , "--port" , type = int , help = "Port to listen on" )
3435 parser .add_argument (
3536 "-f" ,
3637 "--config-file" ,
3738 type = str ,
38- default = "./config.ini " ,
39- help = "Path to config.ini file" ,
40- ) # noqa: E501
39+ default = "./config.yaml " ,
40+ help = "Path to config.yaml file" ,
41+ )
4142 parser .add_argument ("--access-log" , type = str , help = "Path to the access log file" )
4243 parser .add_argument ("--block-log" , type = str , help = "Path to the block log file" )
4344 parser .add_argument ("--html-403" , type = str , help = "Path to the custom 403 Forbidden HTML page" )
4445 parser .add_argument ("--no-filter" , action = "store_true" , help = "Disable URL and domain filtering" )
4546 parser .add_argument (
4647 "--filter-mode" , type = str , choices = ["local" , "http" ], help = "Filter list mode"
47- ) # noqa: E501
48+ )
4849 parser .add_argument (
4950 "--blocked-sites" ,
5051 type = str ,
5152 help = "Path to the text file containing the list of sites to block" ,
52- ) # noqa: E501
53+ )
5354 parser .add_argument (
5455 "--blocked-url" ,
5556 type = str ,
5657 help = "Path to the text file containing the list of URLs to block" ,
57- ) # noqa: E501
58+ )
5859 parser .add_argument (
5960 "--shortcuts" ,
6061 type = str ,
6162 help = "Path to the text file containing the list of shortcuts" ,
62- ) # noqa: E501
63+ )
6364 parser .add_argument (
6465 "--custom-header" ,
6566 type = str ,
6667 help = "Path to the json file containing the list of custom headers" ,
67- ) # noqa: E501
68+ )
6869 parser .add_argument (
6970 "--authorized-ips" ,
7071 type = str ,
7172 help = "Path to the txt file containing the list of authorized ips" ,
72- ) # noqa: E501
73+ )
7374 parser .add_argument ("--no-logging-access" , action = "store_true" , help = "Disable access logging" )
7475 parser .add_argument ("--no-logging-block" , action = "store_true" , help = "Disable block logging" )
7576 parser .add_argument ("--ssl-inspect" , action = "store_true" , help = "Enable SSL inspection" )
@@ -79,12 +80,12 @@ def parse_args() -> argparse.Namespace:
7980 "--inspect-certs-folder" ,
8081 type = str ,
8182 help = "Path to the generated certificates folder" ,
82- ) # noqa: E501
83+ )
8384 parser .add_argument (
8485 "--cancel-inspect" ,
8586 type = str ,
8687 help = "Path to the text file containing the list of URLs without ssl inspection" ,
87- ) # noqa: E501
88+ )
8889 parser .add_argument ("--flask-port" , type = int , help = "Port to listen on for monitoring interface" )
8990 parser .add_argument ("--flask-pass" , type = int , help = "Default password to Flask interface" )
9091 parser .add_argument ("--proxy-enable" , action = "store_true" , help = "Enable proxy after PyProxy" )
@@ -94,63 +95,65 @@ def parse_args() -> argparse.Namespace:
9495 return parser .parse_args ()
9596
9697
97- def load_config (config_path : str ) -> configparser . ConfigParser :
98+ def load_config (config_path : str ) -> dict [ str , Any ] :
9899 """
99- Loads the configuration file and returns the parsed config object .
100+ Loads the YAML configuration file and returns it as a dictionary .
100101
101102 Args:
102- config_path (str): The path to the configuration file to load.
103+ config_path (str): Path to config.yaml
103104
104105 Returns:
105- configparser.ConfigParser: The parsed configuration object.
106+ dict[str, Any]: Parsed YAML configuration
106107 """
107- config = configparser .ConfigParser (interpolation = None )
108- config .read (config_path )
108+ with open (config_path , "r" , encoding = "utf-8" ) as f :
109+ config = yaml .safe_load (f )
110+ if not isinstance (config , dict ):
111+ raise ValueError ("Invalid YAML configuration format." )
109112 return config
110113
111114
112115def get_config_value (
113116 args : argparse .Namespace ,
114- config : configparser . ConfigParser ,
117+ config : dict [ str , Any ] ,
115118 arg_name : str ,
116119 section : str ,
117120 fallback_value : str ,
118- ) -> str :
121+ ) -> Any :
119122 """
120- Retrieves the configuration value, either from the command-line
121- arguments or from the config file.
123+ Retrieves the configuration value, either from CLI args, environment vars, or YAML config.s
122124
123125 Args:
124- args (argparse.Namespace): The parsed command-line arguments object.
125- config (configparser.ConfigParser): The parsed configuration object.
126- arg_name (str): The name of the command-line argument.
127- section (str): The section in the config file where the value is located.
128- fallback_value (str): The fallback value to return if neither
129- argument nor config has a value.
126+ args (argparse.Namespace): Parsed CLI args
127+ config (dict[str, Any]): YAML config
128+ arg_name (str): Argument name
129+ section (str): Section name (as top-level YAML key)s
130+ fallback_value (Any): Default value
130131
131132 Returns:
132- str : The final value, either from command-line arguments, config file, or fallback.
133+ Any : The resulting value
133134 """
134135 arg_value = getattr (args , arg_name , None )
135- if arg_value :
136+ if arg_value not in ( None , False , "" ) :
136137 return arg_value
137138
138139 env_var_name = f"PYPROXY_{ arg_name .upper ().replace ('-' , '_' )} "
139140 env_value = os .getenv (env_var_name )
140141 if env_value :
141142 return env_value
142143
143- return config .get (section , arg_name , fallback = fallback_value )
144+ section_lower = section .lower ()
145+ if section_lower in config :
146+ section_data = config [section_lower ]
147+ if isinstance (section_data , dict ):
148+ return section_data .get (arg_name , fallback_value )
144149
150+ return fallback_value
145151
146- def str_to_bool (value : str ) -> bool :
147- """
148- Converts a string representation of truth to a boolean value.
149152
150- Args:
151- value (str): The value to convert (e.g., "true", "1", "yes").
152-
153- Returns:
154- bool: True if the string represents a true value, False otherwise.
153+ def str_to_bool (value : Any ) -> bool :
154+ """
155+ Converts a string-like values into booleans
155156 """
157+ if isinstance (value , bool ):
158+ return value
156159 return str (value ).lower () in ("yes" , "true" , "t" , "1" )
0 commit comments