import sys
import cgi
import string
import re

class Tag:
   """A Tag URI"""
   def __init__(self, tag):
      self.tag = tag
      self.domain = ''
      self.date = ''
      self.ldate = ''
      self.sdate = ''
      self.path = ''
      self.httpdate = ''
      self.httppath = ''
      self.http = ''

   def parse(self):
      """Parses a tag using a RegExp into the component parts."""
      for m in re.compile(r'tag:([^,]+),([^:]+):(.*)', re.S).findall(self.tag):
         self.domain = m[0]
         self.date = m[1]
         self.path = m[2]

   def convert(self):
      self.parse()
      self.dateparse()
      self.datetohttpdate()
      self.httppath = string.replace(self.path, ':', '/')
      self.http = 'http://'+self.domain+'/'+self.httpdate+'/'+self.httppath

   def dateparse(self):
      """Takes date and parses it into either ldate or sdate and ldate"""
      if self.date[0] == 't': 
         self.sdate = self.date[1:]
         year, month, day = [], 1, 1
         for m in re.compile(r'([^-]+).*', re.S).findall(self.sdate):
            year.append(int(m))
         for m in re.compile(r'[^-]+-([^-]+)', re.S).findall(self.sdate):
            if m: month = int(m)
         for m in re.compile(r'[^-]+-[^-]+-([^-]+)', re.S).findall(self.sdate):
            if m: day = int(m)
         year = str(year[0] + 2000)
         if month <= 9: month = '0' + str(month)
         month = str(month)
         if day <= 9: day = '0' + str(day)
         day = str(day)
         date = year + '-' + month + '-' + day
         self.ldate = date
      else: self.ldate = self.date

   def datetohttpdate(self):
      """Converts the date into an HTTP date."""
      if self.sdate:
         year, month, day = [], 1, 1
         for m in re.compile(r'([^-]+)', re.S).findall(self.sdate):
            year.append(int(m))
         for m in re.compile(r'[^-]+-([^-]+)', re.S).findall(self.sdate):
            if m: month = int(m)
         for m in re.compile(r'[^-]+-[^-]+-([^-]+)', re.S).findall(self.sdate):
            if m: day = int(m)
         year = str(year[0] + 2000)
         if month <= 9: month = '0' + str(month)
         month = str(month)
         if day <= 9: day = '0' + str(day)
         day = str(day)
         if month == '01' and day == '01':
            date = year
         elif month != '01' and day == '01':
            date = year + '/' + month
         else:
            date = year + '/' + month + '/' + day
         self.httpdate = date
      else: self.httpdate = string.replace(self.date,'-','/')

def parse(form):
   tag = form['tag'].value
   tag = Tag(tag)
   tag.convert()
   tag = tag.http
   print 'Content-Type: text/html\n'
   print '<html xmlns="http://www.w3.org/1999/xhtml" \n'
   print '   xml:lang="en" >\n'
   print '<head>\n<title>tag</title>'
   print '</head>\n<body>\n<a href="' + tag + '">' + tag + '</a>\n'
   print '</body>\n</html>'

def doform():
   form = cgi.FieldStorage()
   form_ok = 0
   if form.has_key('tag'):
      parse(form)
   else:
      print 'Content-Type: text/html\n'
      print '<h1>Error</h1>'
      print 'Please fill in the tag field.'

def run():
   sys.argv.append('')
   if sys.argv[1]:
      tag = Tag(str(sys.argv[1]))
      tag.convert()
      print tag.http
   else: doform()

if __name__ == '__main__':
   run()
