| 1 | # MichaelsCSVParser.py |
|---|
| 2 | # -*- coding: utf8 -*- |
|---|
| 3 | # vim:fileencoding=utf8 ai ts=4 sts=4 et sw=4 |
|---|
| 4 | # Copyright 2008 Simon Cross <hodgestar@gmail.com> |
|---|
| 5 | # GPL - see COPYING for details |
|---|
| 6 | |
|---|
| 7 | """ |
|---|
| 8 | Parse cards from a CSV file. |
|---|
| 9 | """ |
|---|
| 10 | |
|---|
| 11 | from sutekh.core.CardSetHolder import CardSetHolder |
|---|
| 12 | from sutekh.core.CardLookup import DEFAULT_LOOKUP |
|---|
| 13 | from sqlobject import sqlhub |
|---|
| 14 | import csv |
|---|
| 15 | |
|---|
| 16 | class MichaelsCSVParser(object): |
|---|
| 17 | """Parser cards from a CSV file into a CardSetHolder. |
|---|
| 18 | |
|---|
| 19 | Cards should be listed in columns and specify at least the card name and |
|---|
| 20 | card count. Each row may optionally include the name of the expansion |
|---|
| 21 | the card comes from. |
|---|
| 22 | """ |
|---|
| 23 | |
|---|
| 24 | def __init__(self): |
|---|
| 25 | self.oCS = CardSetHolder() |
|---|
| 26 | |
|---|
| 27 | def _commit_holder(self, oCardLookup): |
|---|
| 28 | """Commit contents of the card set holder to |
|---|
| 29 | the database""" |
|---|
| 30 | oOldConn = sqlhub.processConnection |
|---|
| 31 | sqlhub.processConnection = oOldConn.transaction() |
|---|
| 32 | |
|---|
| 33 | self.oCS.create_physical_cl(oCardLookup) |
|---|
| 34 | |
|---|
| 35 | sqlhub.processConnection.commit() |
|---|
| 36 | sqlhub.processConnection = oOldConn |
|---|
| 37 | |
|---|
| 38 | def _process_row(self,iName,iAdv,iExpStart,aExps,aRow): |
|---|
| 39 | sName = aRow[iName].strip() |
|---|
| 40 | if iAdv is not None and aRow[iAdv].strip(): |
|---|
| 41 | sName += " (Advanced)" |
|---|
| 42 | |
|---|
| 43 | if not sName: |
|---|
| 44 | return # skip rows with no name |
|---|
| 45 | |
|---|
| 46 | for sExp, sCount in zip(aExps,aRow[iExpStart:]): |
|---|
| 47 | try: |
|---|
| 48 | iCount = int(sCount) |
|---|
| 49 | self.oCS.add(iCount, sName, sExp) |
|---|
| 50 | except StandardError, e: |
|---|
| 51 | pass |
|---|
| 52 | |
|---|
| 53 | def parse(self, fIn, oCardLookup=DEFAULT_LOOKUP): |
|---|
| 54 | oCsvFile = csv.reader(fIn) |
|---|
| 55 | |
|---|
| 56 | oIter = iter(oCsvFile) |
|---|
| 57 | |
|---|
| 58 | aCounts = oIter.next() |
|---|
| 59 | aHeaders = oIter.next() |
|---|
| 60 | |
|---|
| 61 | iName = aHeaders.index('Name') |
|---|
| 62 | |
|---|
| 63 | try: |
|---|
| 64 | iAdv = aHeaders.index('Adv') |
|---|
| 65 | except ValueError, e: |
|---|
| 66 | iAdv = None |
|---|
| 67 | |
|---|
| 68 | iExpStart = aHeaders.index('Jyhad') |
|---|
| 69 | aExps = aHeaders[iExpStart:] |
|---|
| 70 | # Jyhad,VTES,DS,AH,Sabbat,SW,FN,BL,CE,Anarchs,BH,Gehenna,Tenth,KMW,LoB,NoR,Third,SoC,LotN,Promo |
|---|
| 71 | |
|---|
| 72 | for aRow in oIter: |
|---|
| 73 | try: |
|---|
| 74 | self._process_row(iName, iAdv, iExpStart, aExps, aRow) |
|---|
| 75 | except ValueError, e: |
|---|
| 76 | raise ValueError("Line %d in CSV file could not be parsed (%s)" % (oCsvFile.line_num, e)) |
|---|
| 77 | |
|---|
| 78 | self._commit_holder(oCardLookup) |
|---|