Пример #1
0
def _get_pairing_details(tag):
  T = MAIN.TOURNAMENT_PAIRING
  rows = [{
      T.AWAY: False,
      T.PENDING: True,
      T.MATCH_ID: None,
      T.TEAM_ID: None,
      T.TEAM_BOLD: False,
      },{
      T.AWAY: True,
      T.PENDING: True,
      T.MATCH_ID: None,
      T.TEAM_ID: None,
      T.TEAM_BOLD: False,
      }]
  tds = tag('td')
  if len(tds) == 3:
    if tds[0].a:
      href = tds[0].a['href']
      m = common_regex.href_match.search(href)
      for away in range(2):
        rows[away][T.MATCH_ID] = int(m.group('match_id'))
        rows[away][T.PENDING] = False
    away = False
    for ch in tds[1].children:
      if ch.name == 'br':
        away = True
      elif ch.name == 'a':
        href = ch['href']
        m = common_regex.href_team.search(href)
        if m:
          team_id = int(m.group('team_id'))
          rows[away][T.TEAM_ID] = team_id
          if filler.is_filler(team_id):
            rows[away][T.TEAM_FILLER] = True
          if 'bold' in ch['style']:
            rows[away][T.TEAM_BOLD] = True
            rows[away][T.PENDING] = False
            rows[(not away)][T.PENDING] = False
  return rows
Пример #2
0
def import_(text, tournament_id, tfmt_, target=None):
  tournament_id = int(tournament_id)
  target = (target if target is not None else {})
  T = MAIN.TOURNAMENT_PAIRING
  pairing_rows = target.setdefault(T, OrderedDict())
  temp_pairing_rows = []
  group = ElementTree.fromstring(text)
  matches = group.find('matches')
  if matches is None:
    return target
  sides = 'home', 'away'
  for match in matches:
    items = dict(match.items())
    match_id = match.get('id')
    if match_id:
      match_id = int(match_id)
    round_ = int(items['round'])
    pos = items['position']
    if pos:
      pos = int(pos)
    else:
      pos = 0
    if round_ == 0 and pos == 0 and not match_id:
      continue  # open round robin descheduled
    tags = [match.find(s) for s in sides]
    team_ids = [tag.get('id') for tag in tags]
    team_ids = [int(i) for i in team_ids if i is not None]
    tds = [tag.find('TD') for tag in tags]
    scores = [int(td.text) for td in tds if td is not None]
    team_bolds = [(s > scores[1-i])
        for i, s in enumerate(scores)]
    team_fillers = [filler.is_filler(team_id)
        for team_id in team_ids]
    rows = [dict() for _ in range(2)]
    for i in range(2):
      d = rows[i]
      d[T.TOURNAMENT_ID] = tournament_id
      d[T.ROUND] = round_
      d[T.POS] = pos
      d[T.AWAY] = bool(i)
      if match_id:
        d[T.PENDING] = False
        d[T.MATCH_ID] = match_id
      if team_ids:
        d[T.TEAM_ID] = team_ids[i]
        d[T.TEAM_FILLER] = team_fillers[i]
      if team_bolds:
        d[T.TEAM_BOLD] = team_bolds[i]
    temp_pairing_rows.extend(rows)
  round0pos = 1
  posdicts_zeroed = defaultdict(int)
  posdicts_bracketed = {}
  rounds = None
  for d in temp_pairing_rows:
    away = d[T.AWAY]
    round_ = d[T.ROUND]
    pos = d[T.POS]
    if round_ == 0 or pos == 0:
      if not away:
        posdicts_zeroed[round_] += 1
      pos = posdicts_zeroed[round_]
    elif tfmt_ in bracketed:
      if rounds is None:
        rounds = get_rounds(round_, pos)
      rounddict = posdicts_bracketed.setdefault(round_,
          posdict(round_, rounds))
      pos = rounddict[pos]
    d[T.POS] = pos
    key = d[T.TOURNAMENT_ID], round_, d[T.POS], away
    pairing_rows[key] = d
  return target