Find file
Fetching contributors…
Cannot retrieve contributors at this time
179 lines (143 sloc) 5.67 KB
#!/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python
# -*- coding: utf-8 -*-
# title :mylife.py
# description :Application for managing my Eternity, and GPS data
# author :John S. Rutledge
# date :3/13/2011
# version :0.0.1
# python_version :2.7.0
import os
import tablib
from utils import (dirEntries, replace_txt, unicode_csv_reader,
remove_duplicates)
from db import SimpleDB
#-----------------------------------------------------------------------------
# GLOBALS
#-----------------------------------------------------------------------------
DATAFOLDER = os.path.join(os.getcwd(), 'data')
# CONNECT TO DATABASE
# db = SimpleDB(os.path.join(os.getcwd(), 'mylife.db')) # actual database
db = SimpleDB(os.path.join(os.getcwd(), 'tests/test.db')) # test database
#-----------------------------------------------------------------------------
# SELECT FILE TO IMPORT
#-----------------------------------------------------------------------------
print('''\nIf you want to add an eternity csv file to the DB then
type "eternity". If you want to add GPS data, then type "gps".''')
FILETYPE = raw_input('>>> ').lower()[0]
if FILETYPE[0] == 'e':
ROOTDIR = os.path.join(DATAFOLDER, 'eternity')
table_name = 'logs'
elif FILETYPE[0] == 'g':
ROOTDIR = os.path.join(DATAFOLDER, 'gps')
table_name = 'location'
else:
raise Exception, "you must choose 'eternity' or 'gps'"
# get all files in the directory
choices = dirEntries(ROOTDIR, False, 'csv')
print
print('Your current choices are:')
if choices:
for i, filename in enumerate(choices):
print("\t{0}. {1}".format(i, filename))
else:
raise Exception, "\tNO FILES FOUND"
print('\ntype the number of the file you want to import')
FILENUM = int(raw_input('>>> ')[0])
chosen_file = choices[FILENUM]
#-----------------------------------------------------------------------------
# DUMP DATA TO DATABASE
#-----------------------------------------------------------------------------
# get data
with open(chosen_file, 'r') as f:
d = remove_duplicates([tuple(row) for row in unicode_csv_reader(f)])
# validate and insert the data
if table_name == 'logs':
insertEternity(d)
elif table_name == 'location':
insertLocation(d)
else:
raise Exception, 'neither logs nor location'
#-----------------------------------------------------------------------------
# VALIDATION AND FORMATTING
#-----------------------------------------------------------------------------
def insertEternity(d):
'''Formats eternity csv data to ready it for import to sqlite
database fields:
id, day, start_time, stop_time, duration,
loc_id, parent, activity, note, tags
CSV file fields:
day, start time, stop time, duration,
hierarchy path, activity name, note, tags
'''
headers = ('day','start_time','stop_time',
'duration','parent','activity','note','tags')
data = tablib.Dataset(*d, headers=headers)
# replacement dicts
parent_dict = {
u'Media>': u'MEDIA',
u'MISC - Real Life>': u'REAL_LIFE',
u'Basic Routine>Meals & Snacks>': u'MEALS-SNACKS',
u'Basic Routine>': u'BASIC',
u'Salubrious Living>': u'HEALTH',
}
activity_dict = {
u'RL - MISC - Home': u'HOME',
u'RL - MISC - Outside': u'OUTSIDE',
u'へんたい': u'HENTAI', # Im not actually a hentai fan, this is for tracking how often I masterbate; yes, I realize this repo is public.
u'アニメ': u'ANIME',
u'Grocery Shopping': u'GROCERY-SHOPPING',
u'Restaurant': u'RESTAURANT',
u'Shower & Bathroom': u'SHOWER-BATHROOM'
}
for row in data.dict[1:]:
# replace txt
row['parent'] = replace_txt(row['parent'], parent_dict)
row['activity'] = replace_txt(row['activity'], activity_dict)
row['activity'] = row['activity'].upper()
#row['note'] = replace_txt(row['note'], note_dict)
#row['tags'] = replace_txt(row['tags'], tags_dict)
# check if item is a duplicate, if not, insert the item
sql = db.is_duplicate('logs', row)
if sql:
db.query(sql)
print('Finished inserting new Eternity log data')
def insertLocation(d):
'''format location csv data
'''
headers = ('latitude', 'longitude', 'elevation', 'timestamp')
data = tablib.Dataset(*d, headers=headers)
for row in data.dict:
# if row is not already in DB, then return sql to insert it, else None
sql = db.is_duplicate('location', row)
if sql:
db.query(sql)
print('Finished inserting new location data')
#-----------------------------------------------------------------------------
# TODO
#-----------------------------------------------------------------------------
'SOME FIELDS IN DB'
# BASIC SHOWER-BATHROOM
# BASIC GROCERY-SHOPPING
# REAL_LIFE Vacation
# MEALS-SNACKS RESTAURANT
# MEALS-SNACKS FAST-FOOD
# REAL_LIFE RESTAURANT
# PRODUCTIVE READING
# PRODUCTIVE ART
# PRODUCTIVE FILM
# READING Book
'''
TO RUN
>>> python mylife.py
>>> eternity
>>> 0
TODOS
- add table 'places' which translates frequently reoccurring locations
in the location table into an actual place.
example common places are: 'home', 'grocery store', 'moms house', 'etc'
- incorperate Google Chrome web history
- remove location data from eternity, and rely on GPS data instead
aka remove MISC - Real Life>
- adjust replace_txt() function to accept orderedDicts
since the order of replacement is important.
'''