diff --git a/yunohost_service.py b/yunohost_service.py index 3f04d101..eae0fd3f 100644 --- a/yunohost_service.py +++ b/yunohost_service.py @@ -218,25 +218,28 @@ def _get_services(): def _tail(file, n, offset=None): - """Reads a n lines from f with an offset of offset lines. The return + """ + Reads a n lines from f with an offset of offset lines. The return value is a tuple in the form ``(lines, has_more)`` where `has_more` is an indicator that is `True` if there are more lines in the file. + """ avg_line_length = 74 to_read = n + (offset or 0) - with open(file, 'r') as f: - while 1: - try: - f.seek(-(avg_line_length * to_read), 2) - except IOError: - # woops. apparently file is smaller than what we want - # to step back, go to the beginning instead - f.seek(0) - pos = f.tell() - lines = f.read().splitlines() - if len(lines) >= to_read or pos == 0: - return lines[-to_read:offset and -offset or None] - #return lines[-to_read:offset and -offset or None], \ - # len(lines) > to_read or pos > 0 - avg_line_length *= 1.3 + try: + with open(file, 'r') as f: + while 1: + try: + f.seek(-(avg_line_length * to_read), 2) + except IOError: + # woops. apparently file is smaller than what we want + # to step back, go to the beginning instead + f.seek(0) + pos = f.tell() + lines = f.read().splitlines() + if len(lines) >= to_read or pos == 0: + return lines[-to_read:offset and -offset or None] + avg_line_length *= 1.3 + + except IOError: return []