|
9 | 9 | # the GNU General License |
10 | 10 | # ---------------------------------------------------------------------- |
11 | 11 |
|
| 12 | +import os |
12 | 13 | import sys |
| 14 | +import configparser |
| 15 | + |
| 16 | +from src import api |
13 | 17 |
|
14 | 18 | # The options container |
15 | 19 | from . import options |
16 | 20 | from . import global_ |
17 | 21 | from .options import ANYTYPE |
18 | 22 |
|
| 23 | + |
19 | 24 | # ------------------------------------------------------ |
20 | 25 | # Common setup and configuration for all tools |
21 | 26 | # ------------------------------------------------------ |
| 27 | +class OPTION: |
| 28 | + OUTPUT_FILENAME = 'output_filename' |
| 29 | + INPUT_FILENAME = 'input_filename' |
| 30 | + STDERR_FILENAME = 'stderr_filename' |
| 31 | + DEBUG = 'debug_level' |
| 32 | + |
| 33 | + # File IO |
| 34 | + STDIN = 'stdin' |
| 35 | + STDOUT = 'stdout' |
| 36 | + STDERR = 'stderr' |
| 37 | + |
| 38 | + O_LEVEL = 'optimization_level' |
| 39 | + CASE_INS = 'case_insensitive' |
| 40 | + ARRAY_BASE = 'array_base' |
| 41 | + STR_BASE = 'string_base' |
| 42 | + DEFAULT_BYREF = 'default_byref' |
| 43 | + MAX_SYN_ERRORS = 'max_syntax_errors' |
| 44 | + |
| 45 | + MEMORY_MAP = 'memory_map' |
| 46 | + |
| 47 | + USE_BASIC_LOADER = 'use_basic_loader' |
| 48 | + AUTORUN = 'autorun' |
| 49 | + OUTPUT_FILE_TYPE = 'output_file_type' |
| 50 | + INCLUDE_PATH = 'include_path' |
| 51 | + |
| 52 | + CHECK_MEMORY = 'memory_check' |
| 53 | + CHECK_ARRAYS = 'array_check' |
| 54 | + |
| 55 | + STRICT_BOOL = 'strict_bool' |
| 56 | + |
| 57 | + ENABLE_BREAK = 'enable_break' |
| 58 | + EMIT_BACKEND = 'emit_backend' |
| 59 | + |
| 60 | + EXPLICIT = 'explicit' |
| 61 | + STRICT = 'strict' |
| 62 | + |
| 63 | + ARCH = 'architecture' |
| 64 | + EXPECTED_WARNINGS = 'expected_warnings' |
| 65 | + HIDE_WARNING_CODES = 'hide_warning_codes' |
| 66 | + |
| 67 | + # ASM Options |
| 68 | + ASM_ZXNEXT = 'zxnext' |
| 69 | + FORCE_ASM_BRACKET = 'force_asm_brackets' |
| 70 | + |
22 | 71 |
|
23 | 72 | OPTIONS = options.Options() |
24 | 73 |
|
25 | 74 |
|
| 75 | +def load_config_from_file(filename: str, section: str, options_: options.Options = None, stop_on_error=True) -> bool: |
| 76 | + """ Opens file and read options from the given section. If stop_on_error is set, |
| 77 | + the program stop. Otherwise the result of the operation will be |
| 78 | + returned (True on success, False on failure) |
| 79 | + """ |
| 80 | + if options_ is None: |
| 81 | + options_ = OPTIONS |
| 82 | + |
| 83 | + try: |
| 84 | + cfg = configparser.ConfigParser() |
| 85 | + cfg.read(filename, encoding='utf-8') |
| 86 | + except (configparser.DuplicateSectionError, configparser.DuplicateOptionError): |
| 87 | + api.errmsg.msg_output(f"Invalid config file '{filename}': it has duplicated fields") |
| 88 | + if stop_on_error: |
| 89 | + sys.exit(1) |
| 90 | + return False |
| 91 | + except FileNotFoundError: |
| 92 | + api.errmsg.msg_output(f"Config file '{filename}' not found") |
| 93 | + if stop_on_error: |
| 94 | + sys.exit(1) |
| 95 | + return False |
| 96 | + |
| 97 | + if section not in cfg.sections(): |
| 98 | + api.errmsg.msg_output(f"Section '{section}' not found in config file '{filename}'") |
| 99 | + if stop_on_error: |
| 100 | + sys.exit(1) |
| 101 | + return False |
| 102 | + |
| 103 | + parsing = { |
| 104 | + int: cfg.getint, |
| 105 | + float: cfg.getfloat, |
| 106 | + bool: cfg.getboolean |
| 107 | + } |
| 108 | + |
| 109 | + for opt in cfg.options(section): |
| 110 | + options_[opt].value = parsing.get(options_[opt].type, cfg.get)(option=opt) |
| 111 | + |
| 112 | + return True |
| 113 | + |
| 114 | + |
| 115 | +def save_config_into_file(filename: str, section: str, options_: options.Options = None, stop_on_error=True) -> bool: |
| 116 | + """ Save config into config ini file into the given section. If stop_on_error is set, |
| 117 | + the program stop. Otherwise the result of the operation will be |
| 118 | + returned (True on success, False on failure) |
| 119 | + """ |
| 120 | + if options_ is None: |
| 121 | + options_ = OPTIONS |
| 122 | + |
| 123 | + cfg = configparser.ConfigParser() |
| 124 | + if os.path.exists(filename): |
| 125 | + try: |
| 126 | + cfg.read(filename, encoding='utf-8') |
| 127 | + except (configparser.DuplicateSectionError, configparser.DuplicateOptionError): |
| 128 | + api.errmsg.msg_output(f"Invalid config file '{filename}': it has duplicated fields") |
| 129 | + if stop_on_error: |
| 130 | + sys.exit(1) |
| 131 | + return False |
| 132 | + |
| 133 | + cfg[section] = {} |
| 134 | + for opt_name, opt in options.Options.get_options(options_): |
| 135 | + if opt_name.startswith('__') or opt.value is None or opt_name in ('stderr', 'stdin', 'stdout'): |
| 136 | + continue |
| 137 | + |
| 138 | + if opt.type == bool: |
| 139 | + cfg[section][opt_name] = str(opt.value).lower() |
| 140 | + continue |
| 141 | + |
| 142 | + cfg[section][opt_name] = str(opt.value) |
| 143 | + |
| 144 | + try: |
| 145 | + with open(filename, 'wt', encoding='utf-8') as f: |
| 146 | + cfg.write(f) |
| 147 | + except IOError: |
| 148 | + api.errmsg.msg_output(f"Can't write config file '{filename}'") |
| 149 | + if stop_on_error: |
| 150 | + sys.exit(1) |
| 151 | + return False |
| 152 | + |
| 153 | + return True |
| 154 | + |
| 155 | + |
26 | 156 | def init(): |
| 157 | + """ |
| 158 | + Default Options and Compilation Flags |
| 159 | +
|
| 160 | + optimization -- Optimization level. Use -O flag to change. |
| 161 | + case_insensitive -- Whether user identifiers are case insensitive |
| 162 | + or not |
| 163 | + array_base -- Default array lower bound |
| 164 | + param_byref --Default parameter passing. TRUE => By Reference |
| 165 | + """ |
| 166 | + |
27 | 167 | OPTIONS.reset() |
28 | | - OPTIONS.add_option('outputFileName', str) |
29 | | - OPTIONS.add_option('inputFileName', str) |
30 | | - OPTIONS.add_option('StdErrFileName', str) |
31 | | - OPTIONS.add_option('Debug', int, 0) |
| 168 | + |
| 169 | + OPTIONS.add_option(OPTION.OUTPUT_FILENAME, str) |
| 170 | + OPTIONS.add_option(OPTION.INPUT_FILENAME, str) |
| 171 | + OPTIONS.add_option(OPTION.STDERR_FILENAME, str) |
| 172 | + OPTIONS.add_option(OPTION.DEBUG, int, 0) |
32 | 173 |
|
33 | 174 | # Default console redirections |
34 | | - OPTIONS.add_option('stdin', ANYTYPE, sys.stdin) |
35 | | - OPTIONS.add_option('stdout', ANYTYPE, sys.stdout) |
36 | | - OPTIONS.add_option('stderr', ANYTYPE, sys.stderr) |
37 | | - |
38 | | - # ---------------------------------------------------------------------- |
39 | | - # Default Options and Compilation Flags |
40 | | - # |
41 | | - # optimization -- Optimization level. Use -O flag to change. |
42 | | - # case_insensitive -- Whether user identifiers are case insensitive |
43 | | - # or not |
44 | | - # array_base -- Default array lower bound |
45 | | - # param_byref --Default parameter passing. TRUE => By Reference |
46 | | - # ---------------------------------------------------------------------- |
47 | | - OPTIONS.add_option('optimization', int, global_.DEFAULT_OPTIMIZATION_LEVEL) |
48 | | - OPTIONS.add_option('case_insensitive', bool, False) |
49 | | - OPTIONS.add_option('array_base', int, 0) |
50 | | - OPTIONS.add_option('byref', bool, False) |
51 | | - OPTIONS.add_option('max_syntax_errors', int, global_.DEFAULT_MAX_SYNTAX_ERRORS) |
52 | | - OPTIONS.add_option('string_base', int, 0) |
53 | | - OPTIONS.add_option('memory_map', str, None) |
54 | | - OPTIONS.add_option('bracket', bool, False) |
55 | | - |
56 | | - OPTIONS.add_option('use_loader', bool, False) # Whether to use a loader |
57 | | - OPTIONS.add_option('autorun', bool, False) # Whether to add autostart code (needs basic loader = true) |
58 | | - OPTIONS.add_option('output_file_type', str, 'bin') # bin, tap, tzx etc... |
59 | | - OPTIONS.add_option('include_path', str, '') # Include path, like '/var/lib:/var/include' |
60 | | - |
61 | | - OPTIONS.add_option('memoryCheck', bool, False) |
62 | | - OPTIONS.add_option('strictBool', bool, False) |
63 | | - OPTIONS.add_option('arrayCheck', bool, False) |
64 | | - OPTIONS.add_option('enableBreak', bool, False) |
65 | | - OPTIONS.add_option('emitBackend', bool, False) |
| 175 | + OPTIONS.add_option(OPTION.STDIN, ANYTYPE, sys.stdin) |
| 176 | + OPTIONS.add_option(OPTION.STDOUT, ANYTYPE, sys.stdout) |
| 177 | + OPTIONS.add_option(OPTION.STDERR, ANYTYPE, sys.stderr) |
| 178 | + |
| 179 | + OPTIONS.add_option(OPTION.O_LEVEL, int, global_.DEFAULT_OPTIMIZATION_LEVEL) |
| 180 | + OPTIONS.add_option(OPTION.CASE_INS, bool, False) |
| 181 | + OPTIONS.add_option(OPTION.ARRAY_BASE, int, 0) |
| 182 | + OPTIONS.add_option(OPTION.DEFAULT_BYREF, bool, False) |
| 183 | + OPTIONS.add_option(OPTION.MAX_SYN_ERRORS, int, global_.DEFAULT_MAX_SYNTAX_ERRORS) |
| 184 | + OPTIONS.add_option(OPTION.STR_BASE, int, 0) |
| 185 | + OPTIONS.add_option(OPTION.MEMORY_MAP, str, None) |
| 186 | + OPTIONS.add_option(OPTION.FORCE_ASM_BRACKET, bool, False) |
| 187 | + |
| 188 | + OPTIONS.add_option(OPTION.USE_BASIC_LOADER, bool, False) # Whether to use a loader |
| 189 | + OPTIONS.add_option(OPTION.AUTORUN, bool, False) # Whether to add autostart code (needs basic loader = true) |
| 190 | + OPTIONS.add_option(OPTION.OUTPUT_FILE_TYPE, str, 'bin') # bin, tap, tzx etc... |
| 191 | + OPTIONS.add_option(OPTION.INCLUDE_PATH, str, '') # Include path, like '/var/lib:/var/include' |
| 192 | + |
| 193 | + OPTIONS.add_option(OPTION.CHECK_MEMORY, bool, False) |
| 194 | + OPTIONS.add_option(OPTION.STRICT_BOOL, bool, False) |
| 195 | + OPTIONS.add_option(OPTION.CHECK_ARRAYS, bool, False) |
| 196 | + |
| 197 | + OPTIONS.add_option(OPTION.ENABLE_BREAK, bool, False) |
| 198 | + OPTIONS.add_option(OPTION.EMIT_BACKEND, bool, False) |
66 | 199 | OPTIONS.add_option('__DEFINES', dict, {}) |
67 | | - OPTIONS.add_option('explicit', bool, False) |
| 200 | + OPTIONS.add_option(OPTION.EXPLICIT, bool, False) |
68 | 201 | OPTIONS.add_option('Sinclair', bool, False) |
69 | | - OPTIONS.add_option('strict', bool, False) # True to force type checking |
70 | | - OPTIONS.add_option('zxnext', bool, False) # True to enable ZX Next ASM opcodes |
71 | | - OPTIONS.add_option('architecture', str, None) # Architecture |
72 | | - OPTIONS.add_option('expect_warnings', int, 0) # Expected Warnings that will be silenced |
73 | | - OPTIONS.add_option('hide_warning_codes', bool, False) # Whether to show WXXX warning codes or not |
| 202 | + OPTIONS.add_option(OPTION.STRICT, bool, False) # True to force type checking |
| 203 | + OPTIONS.add_option(OPTION.ASM_ZXNEXT, bool, False) # True to enable ZX Next ASM opcodes |
| 204 | + OPTIONS.add_option(OPTION.ARCH, str, None) # Architecture |
| 205 | + OPTIONS.add_option(OPTION.EXPECTED_WARNINGS, int, 0) # Expected Warnings that will be silenced |
| 206 | + OPTIONS.add_option(OPTION.HIDE_WARNING_CODES, bool, False) # Whether to show WXXX warning codes or not |
| 207 | + |
| 208 | + save_config_into_file('project.ini', 'zxbc', stop_on_error=True) |
74 | 209 |
|
75 | 210 |
|
76 | 211 | init() |
0 commit comments