from .compat import decode, encode


def read_data(path):
    """
    Reads a file and returns a json encoded representation of the file.
    """
    db = open(path, "r+")
    content = db.read()

    obj = decode(content)

    db.close()

    return obj

def write_data(path, obj):
    """
    Writes to a file and returns the updated file content.
    """
    with open(path, "w+") as db:
        db.write(encode(obj))

    return obj

def is_valid(file_path):
    """
    Check to see if a file exists or is empty
    """
    from os import path, stat

    return path.exists(file_path) and stat(file_path).st_size > 0

@@ -40,24 +40,29 @@
 f read_data(
+file_
 path):%0A    %22
@@ -140,24 +140,92 @@
 le.%0A    %22%22%22%0A
+%0A    if not is_valid(file_path):%0A        write_data(file_path, %7B%7D)%0A%0A
     db = ope
@@ -222,24 +222,29 @@
   db = open(
+file_
 path, %22r+%22)%0A
@@ -587,16 +587,17 @@
 is empty
+.
 %0A    %22%22%22
@@ -628,16 +628,191 @@
 , stat%0A%0A
+    can_open = False%0A%0A    try:%0A        with open(file_path) as fp:%0A            can_open = True%0A    except IOError:%0A        return False%0A%0A    is_file = path.isfile(file_path)%0A%0A
     retu
@@ -836,16 +836,28 @@
 le_path)
+ and is_file
  and sta

Create a new file if the path is invalid.