rpm-software-management / spec-cleaner

spec-cleaner
BSD 3-Clause "New" or "Revised" License
28 stars 34 forks source link

make contextmanager from FileUtils. #235

Closed mimi1vx closed 6 years ago

sleep-walker commented 6 years ago

@mimi1vx could you describe what you desire?

mimi1vx commented 6 years ago

@sleep-walker

instead this:

def load_keywords_whitelist():
    keywords = []

    files = FileUtils()
    files.open_datafile(BRACKETING_EXCLUDES)
    for line in files.f:
        keywords.append(line.rstrip('\n'))
    files.close()

    return keywords

--> instance file utils class --> call method of this class --> explicitly read file mapped to class attribute --> explicitly close file --> GC to remove instance ..

consider this code:

def load_keywords_whitelist():
    with open_datafile(BRACKETING_EXCLUDES) ad file:
         return [ line.rstrip("\n") for line in file]

And because open is context manager ...., we need only wrap it in simple function which catches exceptions interesting for us.