|
23 | 23 | "create_writable_mission", |
24 | 24 | "get_orig_file", |
25 | 25 | "lua_pattern_to_python_regex", |
26 | | - "format_frequency" |
| 26 | + "format_frequency", |
| 27 | + "init_profanity_filter" |
27 | 28 | ] |
28 | 29 |
|
29 | 30 |
|
@@ -71,6 +72,7 @@ def desanitize(self, _filename: str = None) -> None: |
71 | 72 | except PermissionError: |
72 | 73 | self.log.error(f"Can't desanitize {filename}, no write permissions!") |
73 | 74 | raise |
| 75 | + |
74 | 76 | backup = filename.replace('.lua', '.bak') |
75 | 77 | if os.path.exists(os.path.join(self.node.config_dir, 'MissionScripting.lua')): |
76 | 78 | if _filename: |
@@ -155,17 +157,15 @@ def create_writable_mission(filename: str) -> str: |
155 | 157 | filename = filename[:-5] |
156 | 158 | try: |
157 | 159 | with open(filename, mode='a'): |
158 | | - new_filename = filename |
| 160 | + return filename |
159 | 161 | except PermissionError: |
160 | 162 | if '.dcssb' in filename: |
161 | | - new_filename = os.path.join(os.path.dirname(filename).replace('.dcssb', ''), |
162 | | - os.path.basename(filename)) |
| 163 | + return os.path.join(os.path.dirname(filename).replace('.dcssb', ''), |
| 164 | + os.path.basename(filename)) |
163 | 165 | else: |
164 | 166 | dirname = os.path.join(os.path.dirname(filename), '.dcssb') |
165 | 167 | os.makedirs(dirname, exist_ok=True) |
166 | | - new_filename = os.path.join(dirname, os.path.basename(filename)) |
167 | | - return new_filename |
168 | | - |
| 168 | + return os.path.join(dirname, os.path.basename(filename)) |
169 | 169 |
|
170 | 170 | def get_orig_file(filename: str, *, create_file: bool = True) -> Optional[str]: |
171 | 171 | if filename.endswith('.orig'): |
@@ -225,3 +225,20 @@ def format_frequency(frequency_hz: int, *, band: bool = True) -> str: |
225 | 225 | return f"{frequency_mhz:.1f} MHz ({_band})" |
226 | 226 | else: |
227 | 227 | return f"{frequency_mhz:.1f} MHz" |
| 228 | + |
| 229 | + |
| 230 | +def init_profanity_filter(node: Node): |
| 231 | + # Profanity filter |
| 232 | + language = node.config.get('language', 'en') |
| 233 | + wordlist = os.path.join(node.config_dir, 'profanity.txt') |
| 234 | + if not os.path.exists(wordlist): |
| 235 | + shutil.copy2(os.path.join('samples', 'wordlists', f"{language}.txt"), wordlist) |
| 236 | + with open(wordlist, mode='r', encoding='utf-8') as wl: |
| 237 | + words = [x.strip() for x in wl.readlines() if not x.startswith('#')] |
| 238 | + targetfile = os.path.join(os.path.expandvars(node.locals['DCS']['installation']), 'Data', 'censor.lua') |
| 239 | + bakfile = targetfile.replace('.lua', '.bak') |
| 240 | + if not os.path.exists(bakfile): |
| 241 | + shutil.copy2(targetfile, bakfile) |
| 242 | + with open(targetfile, mode='wb') as outfile: |
| 243 | + outfile.write((f"{language.upper()} = " + luadata.serialize( |
| 244 | + words, indent='\t', indent_level=0)).encode('utf-8')) |
0 commit comments