Esempio n. 1
0
 def _get_twda_holders(self):
     """Return all the TWDA holders in the current database"""
     aHolders = []
     for oCS in PhysicalCardSet.select():
         oMatch = self.oTWDARegex.match(oCS.name)
         if oMatch:
             aHolders.append(oCS)
     return aHolders
 def _cache_starters(self):
     """Lookup the starter decks and cache them to avoid repeated queries"""
     for oCS in PhysicalCardSet.select():
         for _sType, oRegex in (('Starters', self.oStarterRegex),
                                ('Fixed', self.oFixedRegex),
                                ('Demos', self.oDemoRegex)):
             oMatch = oRegex.match(oCS.name)
             if oMatch:
                 self._aStarters.append(oCS)
 def check_enabled(self):
     """check for starter decks in the database and disable menu if not"""
     bEnabled = False
     for oCS in PhysicalCardSet.select():
         oMatch = self.oStarterRegex.match(oCS.name)
         if oMatch:
             bEnabled = True
             break
     return bEnabled
Esempio n. 4
0
 def _get_twda_names(self):
     """Get names of all the TWDA entries in the current database"""
     aNames = []
     for oCS in PhysicalCardSet.select():
         if not oCS.parent or not oCS.inuse:
             continue
         oMatch = self.oTWDARegex.match(oCS.parent.name)
         if oMatch:
             aNames.append(oCS.name)
     return aNames
Esempio n. 5
0
    def _get_decks(self, aUrls, aDates, aHashes):
        """Unzip a file containing the decks."""
        aZipHolders = []
        aToUnzip, aToReplace = self._get_decks_to_download(aUrls, aDates,
                                                           aHashes)
        if not aToUnzip:
            return False
        # We download everything first, so we don't delete card sets which
        # fail to download.
        oBinLogHandler = BinnedCountLogHandler()
        oProgressDialog = ProgressDialog()
        oProgressDialog.set_description("Downloading TWDA data")
        oLogger = Logger('Download zip files')
        oLogger.addHandler(oBinLogHandler)
        oBinLogHandler.set_dialog(oProgressDialog)
        oBinLogHandler.set_tot_bins(len(aToUnzip))
        oProgressDialog.show()
        # We sort the list of urls to download for cosmetic reasons
        for sUrl, sTWDA, sHash in sorted(aToUnzip, key=lambda x: x[1]):
            oFile = urlopen_with_timeout(sUrl,
                                         fErrorHandler=gui_error_handler,
                                         bBinary=True)
            oProgressDialog.set_description('Downloading %s' % sTWDA)
            try:

                sData = fetch_data(oFile, sHash=sHash,
                                   oLogHandler=oBinLogHandler,
                                   fErrorHandler=gui_error_handler)
            except HashError:
                do_complaint_error("Checksum failed for %s\nSkipping"
                                   % sTWDA)
                # Don't delete this, since we're not replacing it
                aToReplace.remove(sTWDA)
                continue
            oZipFile = ZipFileWrapper(BytesIO(sData))
            aZipHolders.append(oZipFile)
            oBinLogHandler.inc_cur_bin()
        oProgressDialog.destroy()

        # Bomb out if we're going to end up doing nothing
        if not aZipHolders:
            return False

        # Delete all TWDA entries in the holders we replace
        # We do this to handle card sets being removed from the TWDA
        # correctly
        aToDelete = []
        for oCS in list(PhysicalCardSet.select()):
            if not oCS.parent:
                continue
            if oCS.parent.name in aToReplace:
                aToDelete.append(oCS.name)

        return unzip_files_into_db(aZipHolders, "Adding TWDA Data",
                                   self.parent, aToDelete)
def write_all_pcs(sDir=''):
    """Write all the Physical Card Sets into files in the given directory"""
    oPhysicalCardSets = PhysicalCardSet.select()
    aList = []
    for oPCS in oPhysicalCardSets:
        sFName = safe_filename(oPCS.name)
        sFileName = gen_temp_file('pcs_' + sFName + '_', sDir)
        oWriter = PhysicalCardSetXmlFile(sFileName)
        aList.append(oWriter)
        oWriter.write(oPCS.name)
    return aList
 def old_database_count(self, oConn):
     """Check number of items in old DB fro progress bars, etc."""
     oVer = DatabaseVersion()
     iCount = 14  # Card property tables
     if oVer.check_tables_and_versions([AbstractCard],
                                       [AbstractCard.tableversion], oConn):
         iCount += AbstractCard.select(connection=oConn).count()
     elif oVer.check_tables_and_versions([AbstractCard], [5], oConn):
         iCount += AbstractCard_v5.select(connection=oConn).count()
     if oVer.check_tables_and_versions([PhysicalCard],
                                       [PhysicalCard.tableversion], oConn):
         iCount += PhysicalCard.select(connection=oConn).count()
     elif oVer.check_tables_and_versions([PhysicalCard], [2], oConn):
         iCount += PhysicalCard_v2.select(connection=oConn).count()
     if oVer.check_tables_and_versions([PhysicalCardSet],
                                       [PhysicalCardSet.tableversion],
                                       oConn):
         iCount += PhysicalCardSet.select(connection=oConn).count()
     elif oVer.check_tables_and_versions([PhysicalCardSet], [6], oConn):
         iCount += PhysicalCardSet.select(connection=oConn).count()
     return iCount
    def test_zip_file(self):
        """Test zip file handling"""
        self.maxDiff = None
        # pylint: disable=too-many-statements
        # Want a single test case to avoid re-initialising the database
        sTempFileName = self._create_tmp_file()
        oZipFile = ZipFileWrapper(sTempFileName)
        aPhysCards = get_phys_cards()

        oMyCollection = PhysicalCardSet(name='My Collection')

        for oCard in aPhysCards:
            # pylint: disable=no-member
            # SQLObject confuses pylint
            oMyCollection.addPhysicalCard(oCard.id)
            oMyCollection.syncUpdate()

        oPhysCardSet1 = PhysicalCardSet(name=CARD_SET_NAMES[0],
                                        parent=oMyCollection)
        oPhysCardSet1.comment = 'A test comment'
        oPhysCardSet1.author = 'A test author'

        self.assertNotEqual(oPhysCardSet1.parent, None)
        self.assertEqual(oPhysCardSet1.parent.id, oMyCollection.id)

        for iLoop in range(5):
            # pylint: disable=no-member
            # SQLObject confuses pylint
            oPhysCardSet1.addPhysicalCard(aPhysCards[iLoop].id)
            oPhysCardSet1.syncUpdate()
            if iLoop > 3:
                # Add a duplicate
                oPhysCardSet1.addPhysicalCard(aPhysCards[iLoop].id)
                oPhysCardSet1.syncUpdate()

        oPhysCardSet2 = PhysicalCardSet(name=CARD_SET_NAMES[1])
        oPhysCardSet2.comment = 'Test 2 comment'
        oPhysCardSet2.author = 'A different author'

        for iLoop in range(2, 7):
            # pylint: disable=no-member
            # SQLObject confuses pylint
            oPhysCardSet2.addPhysicalCard(aPhysCards[iLoop].id)
            oPhysCardSet2.syncUpdate()

        oHandler = SutekhCountLogHandler()
        oZipFile.do_dump_all_to_zip(oHandler)
        self.assertEqual(oHandler.fTot, 3)
        dEntries = oZipFile.get_all_entries()
        self.assertEqual(len(dEntries), 3)
        self.assertTrue(oPhysCardSet2.name in dEntries)
        self.assertTrue(oPhysCardSet1.name in dEntries)
        self.assertTrue(oMyCollection.name in dEntries)
        self.assertEqual(dEntries[oPhysCardSet1.name][2], oMyCollection.name)

        # Check it loads correctly
        # Destroy some existing data
        # pylint: disable=not-an-iterable
        # SQLObject confuses pylint
        aCardSet1 = sorted([x.abstractCard.name for x in oPhysCardSet1.cards])
        aCardSet2 = sorted([x.abstractCard.name for x in oPhysCardSet2.cards])
        # pylint: enable=not-an-iterable

        delete_physical_card_set(oMyCollection.name)
        delete_physical_card_set(oPhysCardSet1.name)

        self.assertEqual(PhysicalCardSet.select().count(), 1)

        oZipFile.do_restore_from_zip(oLogHandler=oHandler)
        self.assertEqual(oZipFile.get_warnings(), [])
        self.assertEqual(oHandler.fTot, 3)

        self.assertEqual(PhysicalCardSet.select().count(), 3)

        oMyCollection = IPhysicalCardSet('My Collection')
        oPhysCardSet1 = IPhysicalCardSet(CARD_SET_NAMES[0])
        oPhysCardSet2 = IPhysicalCardSet(CARD_SET_NAMES[1])
        self.assertEqual(oPhysCardSet1.comment, 'A test comment')
        self.assertEqual(oPhysCardSet2.author, 'A different author')
        self.assertNotEqual(oPhysCardSet1.parent, None)
        self.assertEqual(oPhysCardSet1.parent.id, oMyCollection.id)
        self.assertEqual(
            sorted([x.abstractCard.name for x in oPhysCardSet1.cards]),
            aCardSet1)
        self.assertEqual(oPhysCardSet2.parent, None)
        self.assertEqual(
            sorted([x.abstractCard.name for x in oPhysCardSet2.cards]),
            aCardSet2)

        self.assertEqual(
            sorted([x.abstractCard.name for x in oMyCollection.cards]),
            sorted([x.abstractCard.name for x in aPhysCards]))
        self.assertEqual(len(oPhysCardSet1.cards), 6)
        self.assertEqual(len(oPhysCardSet2.cards), 5)