Improve file handling in serializers

Bug #463463 reported by Natalia Bidart
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
wsgi-oops
Confirmed
Low
Ubuntu One Foundations+ team

Bug Description

As per facundobatista's review on lp:~nataliabidart/wsgi-oops/serializer-thread-safe, we should improve file handling using a manager like this one (untested code):

class FHManager(object):
    def __init__(self, instance_fh):
        self._instance_fh = instance_fh
        self._fh = None

    def __enter__(self, filename):
        if self._instance_fh:
            return self._instance_fh

        if os.path.isdir(filename):
            filepath = os.path.join(filename, section)
            fh = open(filepath, "w")
        else:
            filepath = filename
            fh = open(filepath, "a")

        self._fh = fh
        return fh

    def __exit__(self, type, value, tb):
        if self._fh:
            self._fh.close()

And then, within the file serializer, make a cal like this one:

with self.fh_manager(filename) as fh:
    ...

Tags: enhancement
Changed in wsgi-oops:
status: New → Confirmed
assignee: nobody → Naty Bidart (nataliabidart)
importance: Undecided → Low
Changed in wsgi-oops:
assignee: Naty Bidart (nataliabidart) → Ubuntu One Foundations+ team (ubuntuone-foundations+)
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.