Commit 7b668cb19d4e30fb9219d4041e3452ab1fc1343d
1 parent
93e13002
Exists in
master
and in
1 other branch
changes to file serving by moving read() into the try block
Showing
1 changed file
with
7 additions
and
9 deletions
Show diff stats
aprendizations/serve.py
| ... | ... | @@ -218,16 +218,16 @@ class FileHandler(BaseHandler): |
| 218 | 218 | content_type = mimetypes.guess_type(filename)[0] |
| 219 | 219 | |
| 220 | 220 | try: |
| 221 | - f = open(filepath, 'rb') | |
| 221 | + with open(filepath, 'rb') as f: | |
| 222 | + data = f.read() | |
| 222 | 223 | except FileNotFoundError: |
| 223 | 224 | logging.error(f'File not found: {filepath}') |
| 224 | 225 | except PermissionError: |
| 225 | 226 | logging.error(f'No permission: {filepath}') |
| 226 | - except Exception as e: | |
| 227 | - raise e | |
| 227 | + except Exception: | |
| 228 | + logging.error(f'Error reading: {filepath}') | |
| 229 | + raise | |
| 228 | 230 | else: |
| 229 | - data = f.read() | |
| 230 | - f.close() | |
| 231 | 231 | self.set_header("Content-Type", content_type) |
| 232 | 232 | self.write(data) |
| 233 | 233 | await self.flush() |
| ... | ... | @@ -426,11 +426,9 @@ def parse_cmdline_arguments(): |
| 426 | 426 | # ---------------------------------------------------------------------------- |
| 427 | 427 | def get_logger_config(debug=False): |
| 428 | 428 | if debug: |
| 429 | - filename = 'logger-debug.yaml' | |
| 430 | - level = 'DEBUG' | |
| 429 | + filename, level = 'logger-debug.yaml', 'DEBUG' | |
| 431 | 430 | else: |
| 432 | - filename = 'logger.yaml' | |
| 433 | - level = 'INFO' | |
| 431 | + filename, level = 'logger.yaml', 'INFO' | |
| 434 | 432 | |
| 435 | 433 | config_dir = environ.get('XDG_CONFIG_HOME', '~/.config/') |
| 436 | 434 | config_file = path.join(path.expanduser(config_dir), APP_NAME, filename) | ... | ... |