GenDates.py

From PeformIQ Upgrade
Revision as of 12:43, 15 July 2008 by PeterHarding (talk | contribs) (New page: =Script= <pre> #!/usr/bin/env python # # Purpose: # # $Id:$ # #------------------------------------------------------------------------------- """ Skeleton Python script ... """ ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Script

#!/usr/bin/env python
#
#  Purpose: 
#
#  $Id:$
#
#-------------------------------------------------------------------------------

"""
  Skeleton Python script

  ...
"""
#-------------------------------------------------------------------------------

import os
import sys
import getopt
import logging

#-------------------------------------------------------------------------------

from datetime import datetime, timedelta

#-------------------------------------------------------------------------------

__version__   = "1.0.0"
__id__        = "@(#)  gen_dates.py  [%s]  2008-07-15"

verbose_flg   = False

debug_level   = 0

DoW           = {
                   0 : 'Mon',
                   1 : 'Tue',
                   2 : 'Wed',
                   3 : 'Thu',
                   4 : 'Fri',
                   5 : 'Sat',
                   6 : 'Sun'
                }

#===============================================================================

def is_weekday(d):
   dow = d.weekday()

   if dow < 5:
      return True
   else:
      return False

#-------------------------------------------------------------------------------

def gen_dates():
   d_start = datetime(day=1, month=1, year=2008)
   d_end   = datetime(day=5, month=5, year=2008)
   d_now   = datetime.now()

   d = d_start

   while d < d_end:
      if is_weekday(d):
         print '%s,%s' %  (d.strftime("%d/%m/%Y"), DoW[d.weekday()])
      d += timedelta(days=1)

#-------------------------------------------------------------------------------

def gen_date_range():
   dates = []

   for n in range(no_days):
      d = date - datetime.timedelta(days=n)
      print d.strftime("%y%m%d")
      dates.append(d)

   # print date.strftime("%y%m%d")

   for d in dates:
      generate(d)

#===============================================================================

def main(argv):
   global verbose_flg
   global debug_level

#   try:
#      terminal_type = os.environ["TERM"]
#   except KeyError, e:
#      print "Set TERM environment variable and rerun!"
#      return 1

#   wrk_path  = os.getcwd()
#   wrk_dir   = os.path.basename(wrk_path)

#   print "[gen_dates]  Working directory is %s" % wrk_path

#   data_dir = wrk_dir  + '/DATA/'
#   pid_path = data_dir + PIDFILE

#   os.chdir(data_dir)

   try:
      opts, args = getopt.getopt(argv, "dD:hn:vV?",
                 ["debug", "debug_level=", "help", "no_days=", "verbose", "version"])
   except getopt.GetoptError:
      usage()
      sys.exit(2)

   for opt, arg in opts:
      if opt in ("-d", "--debug"):
         debug_level   += 1
         debug_flg = True
      elif opt in ('-D', "--debug_level"):
         debug_level    = int(a)
      elif opt in ("-t", "--target_date"):
         target_date = arg
         date = datetime.datetime.strptime(target_date, "%Y%m%d")
         print date
      elif opt in ("-h", "-?", "--help"):
         # print __doc__
         usage()
         return 1
      elif opt in ("-n", "--no_days"):
         no_days = int(arg)
      elif opt in ("-v", "--verbose"):
         verbose_flg = True

   if (debug_level > 0): print "Debugging level set to %d" % debug_level

   #init()

   gen_dates()

   return 0

#-------------------------------------------------------------------------------

if __name__ == '__main__' or __name__ == sys.argv[0]:
   try:
      sys.exit(main(sys.argv[1:]))
   except KeyboardInterrupt, e:
      print "[gen_dates]  Interrupted!"

#-------------------------------------------------------------------------------

"""
Revision History:

     Date     Who   Description
   --------   ---   ------------------------------------------------------------
   20031014   plh   Initial implementation

Problems to fix:

To Do:

Issues:

"""