def get_data():
    # List 'app-cache'
    ##configure = Configure(debug,path=filename)
    cachepath = Configure._Configure__joinpath(filename, 'app-cache')
    listing = sorted(os.listdir(cachepath))

    # TODO, What to do with other cache
    if listing:
        latest = max(listing) ## Latest date
        if debug:
            print('Using latest cached data, {0}.'.format(latest))
        return pickle.load(open(Configure._Configure__joinpath(cachepath, latest + os.sep + 'data.p'),'rb'))
    else: ## If no cache, recursion
        if debug:
            print('No cache, running CalendarReports.')
        CalendarReports.main(None)
        main()
def read_history():
    history = configure._get_path('HISTORY')
    old = []
    for name in configure.log['REQUEST_ORDER']:
        fullpath = Configure._Configure__joinpath(history,name)
        old.append(History(debug,fullpath,name))

    # Set/save new timeMin for available history
    # Get calendar order in config
    for calendar in configure.config['REQUEST']['PARAMETERS']['CALENDARS']:
        for r in old:
            if os.path.split(r.source)[1]==calendar['name']:
                print('Updating request parameter, {0}, from {1} to {2}'.format('timeMin',calendar['timeMin'],r.reports['REQUESTFROM']))
                calendar['timeMin'] = r.reports['REQUESTFROM']

    if configure.save_request_config():
        print('JSON request parameters saved.')
    else:
        print('Saving JSON request parameters failed.')

    return old
def read_history():
    history = configure._get_path('HISTORY')
    old = []
    for name in configure.log['REQUEST_ORDER']:
        fullpath = Configure._Configure__joinpath(history, name)
        old.append(History(debug, fullpath, name))

    # Set/save new timeMin for available history
    # Get calendar order in config
    for calendar in configure.config['REQUEST']['PARAMETERS']['CALENDARS']:
        for r in old:
            if os.path.split(r.source)[1] == calendar['name']:
                print(
                    'Updating request parameter, {0}, from {1} to {2}'.format(
                        'timeMin', calendar['timeMin'],
                        r.reports['REQUESTFROM']))
                calendar['timeMin'] = r.reports['REQUESTFROM']

    if configure.save_request_config():
        print('JSON request parameters saved.')
    else:
        print('Saving JSON request parameters failed.')

    return old
### Parser for command-line arguments.
##parser = argparse.ArgumentParser(
##    description=__doc__,
##    formatter_class=argparse.RawDescriptionHelpFormatter,
##    parents=[tools.argparser])

# File path
filename = os.path.dirname(__file__)

### Flags for cache read and write
##use_cache = True
##write_cache = False

# Create configuration
configure = Configure(debug, path=filename)


# Read event history and update config
# TODO, deprecate JSON file storage
def read_history():
    history = configure._get_path('HISTORY')
    old = []
    for name in configure.log['REQUEST_ORDER']:
        fullpath = Configure._Configure__joinpath(history, name)
        old.append(History(debug, fullpath, name))

    # Set/save new timeMin for available history
    # Get calendar order in config
    for calendar in configure.config['REQUEST']['PARAMETERS']['CALENDARS']:
        for r in old: