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,16 +218,16 @@ class FileHandler(BaseHandler): | ||
218 | content_type = mimetypes.guess_type(filename)[0] | 218 | content_type = mimetypes.guess_type(filename)[0] |
219 | 219 | ||
220 | try: | 220 | try: |
221 | - f = open(filepath, 'rb') | 221 | + with open(filepath, 'rb') as f: |
222 | + data = f.read() | ||
222 | except FileNotFoundError: | 223 | except FileNotFoundError: |
223 | logging.error(f'File not found: {filepath}') | 224 | logging.error(f'File not found: {filepath}') |
224 | except PermissionError: | 225 | except PermissionError: |
225 | logging.error(f'No permission: {filepath}') | 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 | else: | 230 | else: |
229 | - data = f.read() | ||
230 | - f.close() | ||
231 | self.set_header("Content-Type", content_type) | 231 | self.set_header("Content-Type", content_type) |
232 | self.write(data) | 232 | self.write(data) |
233 | await self.flush() | 233 | await self.flush() |
@@ -426,11 +426,9 @@ def parse_cmdline_arguments(): | @@ -426,11 +426,9 @@ def parse_cmdline_arguments(): | ||
426 | # ---------------------------------------------------------------------------- | 426 | # ---------------------------------------------------------------------------- |
427 | def get_logger_config(debug=False): | 427 | def get_logger_config(debug=False): |
428 | if debug: | 428 | if debug: |
429 | - filename = 'logger-debug.yaml' | ||
430 | - level = 'DEBUG' | 429 | + filename, level = 'logger-debug.yaml', 'DEBUG' |
431 | else: | 430 | else: |
432 | - filename = 'logger.yaml' | ||
433 | - level = 'INFO' | 431 | + filename, level = 'logger.yaml', 'INFO' |
434 | 432 | ||
435 | config_dir = environ.get('XDG_CONFIG_HOME', '~/.config/') | 433 | config_dir = environ.get('XDG_CONFIG_HOME', '~/.config/') |
436 | config_file = path.join(path.expanduser(config_dir), APP_NAME, filename) | 434 | config_file = path.join(path.expanduser(config_dir), APP_NAME, filename) |