# Python-JSON method for caching my objects.

import simplejson as json
import os.path
from datetime import datetime


def GetJSONObjects():

    # determine the json data file path
    filename = "objects.json"
    filepath = "/home/docmccoy/Documents/" + filename
    if os.path.isfile(filepath):
        filename = filepath
        f = open(filename, 'r')
        objects = json.load( f )
        print objects
    else:
        objects = list()

    return objects


def PutJSONObjects(objects, filename = "objects.json"):

    # determine the json data file path
    filepath = "/home/docmccoy/Documents/" + filename
    if os.path.isfile(filepath):
        filename = filepath

    f = open(filename, 'w')
    json.dump([o.__dict__ for o in objects], f, indent = 4 * ' ' )


class MyObject:
    def __init__(self, ID, url, category, osfamily, createDate):
        self.id = ID
        self.url = url
        self.category = category
        self.osfamily = osfamily
        self.createDate = createDate


o1 = MyObject(1, "http://google.com", "search", "linux", unicode(datetime.now()))
o2 = MyObject(2, "http://localhost", "mycomp", None, unicode(datetime.now()))
o3 = MyObject(3, "http://milan.com", "football", "windows", unicode(datetime.now()))

objects = list()
objects.append(o1)
objects.append(o2)
objects.append(o3)

PutJSONObjects(objects)
objects = GetJSONObjects()
PutJSONObjects(objects, "objects2.json")
