import logging
import re

# for db.link
from google.appengine.ext import db

from BeautifulSoup import BeautifulSoup

from datafeeds.parser_base import ParserBase


class UsfirstLegacyTeamDetailsParser(ParserBase):
    """
    Facilitates building TBAVideos store from TBA.
    """
    @classmethod
    def parse(self, html):
        """
        Parse the information table on USFIRSTs site to extract relevant team
        information. Return a dictionary.
        """
        team = dict()
        soup = BeautifulSoup(html,
                convertEntities=BeautifulSoup.HTML_ENTITIES)

        if soup.find(text='No team found.') is not None:
            logging.error('FIRST lacks team.')
            return None

        for tr in soup.findAll('tr'):
            tds = tr.findAll('td')
            if len(tds) > 1:
                field = str(tds[0].string)
                if field == "Team Number":
                    team["team_number"] = int(tds[1].b.string)
                if field == "Team Name":
                    team["name"] = unicode(tds[1].string)
                if field == "Team Location":
                    #TODO: Filter out &nbsp;'s and stuff -greg 5/21/2010
                    team["address"] = unicode(tds[1].string)
                #if field == "Rookie Season": #Unused
                #    team["rookie_season"] = int(tds[1].string)
                if field == "Team Nickname":
                    team["nickname"] = unicode(tds[1].string)
                if field == "Team Website":
                    try:
                        website_str = re.sub(r'^/|/$', '', unicode(tds[1].a["href"]))  # strip starting and trailing slashes
                        if not (website_str.startswith('http://') or website_str.startswith('https://')):
                            website_str = 'http://%s' % website_str
                        team['website'] = db.Link(website_str)
                    except Exception, details:
                        logging.info("Team website is invalid for team %s." % team['team_number'])
                        logging.info(details)
        return team, False

@@ -1690,17 +1690,16 @@
  if not 
-(
 website_
@@ -1724,18 +1724,23 @@
 tp://') 
-or
+and not
  website
@@ -1766,17 +1766,16 @@
 tps://')
-)
 :%0A      

Simplify logic with De Morgan's law