Subversion Repositories SmartDukaan

Rev

Rev 4014 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python
'''
Loads the holidays in the PublicHolidays table.

It expects a sheet to have two columns: date and occassion.

@author: Ankur Singhal
'''
import optparse
import xlrd
from elixir import *
import time

if __name__ == '__main__' and __package__ is None:
    import sys
    import os
    sys.path.insert(0, os.getcwd())

from shop2020.logistics.service.impl import DataService
from shop2020.logistics.service.impl.DataService import PublicHolidays

def load_holidays(filename, sheet_no, db_hostname):
    DataService.initialize(dbname='logistics', db_hostname=db_hostname)

    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(sheet_no)
    num_rows = sheet.nrows

    for rownum in range(1, num_rows):
        holiday_date, occasion = sheet.row_values(rownum)[0:2]
        print str(holiday_date)
        holiday_date = time.strptime(str(holiday_date), '%d/%m/%y')
        holiday_date = time.strftime('%Y-%m-%d', holiday_date)
        print holiday_date
        
        try:
            holiday = PublicHolidays.query.filter_by(date = holiday_date).one()
        except:
            holiday = PublicHolidays()
            holiday.date = holiday_date
        holiday.occasion = occasion
    session.commit()
    
def main():
    parser = optparse.OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                   default="Holidays.xls", type="string",
                   help="Read the holiday list",
                   metavar="FILE")
    parser.add_option("-H", "--host", dest="hostname",
                      default="localhost",
                      type="string", help="The HOST where the DB server is running",
                      metavar="HOST")
    (options, args) = parser.parse_args()
    if len(args) != 0:
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
    filename = options.filename
    if filename is None:
        parser.error("A filename must be provided. Use -h for more details.")
    load_holidays(filename, 0, options.hostname)

if __name__ == '__main__':
    main()