Supplied an option value, return the data based on what the value is. If val is a URL, you'll get back the fetched content, if val is a file path it will be loaded and get back the contents, and if a string it will simply be returned back. Args: val (str)
(self, val: str)
| 144 | self._socksProxy = socksProxy |
| 145 | |
| 146 | def optValueToData(self, val: str) -> str: |
| 147 | """Supplied an option value, return the data based on what the |
| 148 | value is. If val is a URL, you'll get back the fetched content, |
| 149 | if val is a file path it will be loaded and get back the contents, |
| 150 | and if a string it will simply be returned back. |
| 151 | |
| 152 | Args: |
| 153 | val (str): option name |
| 154 | |
| 155 | Returns: |
| 156 | str: option data |
| 157 | """ |
| 158 | if not isinstance(val, str): |
| 159 | self.error(f"Invalid option value {val}") |
| 160 | return None |
| 161 | |
| 162 | if val.startswith('@'): |
| 163 | fname = val.split('@')[1] |
| 164 | self.info(f"Loading configuration data from: {fname}") |
| 165 | |
| 166 | try: |
| 167 | with open(fname, "r") as f: |
| 168 | return f.read() |
| 169 | except Exception as e: |
| 170 | self.error(f"Unable to open option file, {fname}: {e}") |
| 171 | return None |
| 172 | |
| 173 | if val.lower().startswith('http://') or val.lower().startswith('https://'): |
| 174 | try: |
| 175 | self.info(f"Downloading configuration data from: {val}") |
| 176 | session = self.getSession() |
| 177 | res = session.get(val) |
| 178 | |
| 179 | return res.content.decode('utf-8') |
| 180 | except BaseException as e: |
| 181 | self.error(f"Unable to open option URL, {val}: {e}") |
| 182 | return None |
| 183 | |
| 184 | return val |
| 185 | |
| 186 | def error(self, message: str) -> None: |
| 187 | """Print and log an error message |