| 1 | # CSVImporter.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 | import gtk |
|---|
| 8 | from sutekh.io.MichaelsCSVParser import MichaelsCSVParser |
|---|
| 9 | from sutekh.core.SutekhObjects import PhysicalCard, AbstractCardSet, PhysicalCardSet |
|---|
| 10 | from sutekh.core.CardLookup import LookupFailed |
|---|
| 11 | from sutekh.gui.PluginManager import CardListPlugin |
|---|
| 12 | from sutekh.gui.SutekhDialog import SutekhDialog, do_complaint_error |
|---|
| 13 | |
|---|
| 14 | class MichaelImporter(CardListPlugin): |
|---|
| 15 | dTableVersions = { |
|---|
| 16 | AbstractCardSet: [3], |
|---|
| 17 | PhysicalCardSet: [4], |
|---|
| 18 | PhysicalCard: [2], |
|---|
| 19 | } |
|---|
| 20 | aModelsSupported = [AbstractCardSet, PhysicalCardSet, PhysicalCard] |
|---|
| 21 | |
|---|
| 22 | def __init__(self,*args,**kws): |
|---|
| 23 | super(MichaelImporter,self).__init__(*args,**kws) |
|---|
| 24 | |
|---|
| 25 | def get_menu_item(self): |
|---|
| 26 | """ |
|---|
| 27 | Overrides method from base class. |
|---|
| 28 | """ |
|---|
| 29 | if not self.check_versions() or not self.check_model_type(): |
|---|
| 30 | return None |
|---|
| 31 | iDF = gtk.MenuItem("Import Michael's CSV File") |
|---|
| 32 | iDF.connect("activate", self.activate) |
|---|
| 33 | return iDF |
|---|
| 34 | |
|---|
| 35 | def get_desired_menu(self): |
|---|
| 36 | return "Plugins" |
|---|
| 37 | |
|---|
| 38 | def activate(self,oWidget): |
|---|
| 39 | oDlg = self.make_dialog() |
|---|
| 40 | oDlg.run() |
|---|
| 41 | |
|---|
| 42 | def make_dialog(self): |
|---|
| 43 | self.oDlg = SutekhDialog("Choose CSV File",None, |
|---|
| 44 | gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, |
|---|
| 45 | (gtk.STOCK_OK, gtk.RESPONSE_OK, |
|---|
| 46 | gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)) |
|---|
| 47 | |
|---|
| 48 | self.oFileChooser = gtk.FileChooserWidget(gtk.FILE_CHOOSER_ACTION_OPEN) |
|---|
| 49 | self.oDlg.vbox.pack_start(self.oFileChooser) |
|---|
| 50 | |
|---|
| 51 | self.oDlg.connect("response", self.handle_response) |
|---|
| 52 | self.oDlg.set_size_request(400,400) |
|---|
| 53 | self.oDlg.show_all() |
|---|
| 54 | |
|---|
| 55 | return self.oDlg |
|---|
| 56 | |
|---|
| 57 | def handle_response(self,oWidget,oResponse): |
|---|
| 58 | if oResponse == gtk.RESPONSE_OK: |
|---|
| 59 | |
|---|
| 60 | oParser = MichaelsCSVParser() |
|---|
| 61 | |
|---|
| 62 | sFile = self.oFileChooser.get_filename() |
|---|
| 63 | fIn = file(sFile,"rb") |
|---|
| 64 | |
|---|
| 65 | try: |
|---|
| 66 | oParser.parse(fIn, oCardLookup=self.cardlookup) |
|---|
| 67 | finally: |
|---|
| 68 | fIn.close() |
|---|
| 69 | |
|---|
| 70 | self.oDlg.destroy() |
|---|
| 71 | |
|---|
| 72 | # pylint: disable-msg=C0103 |
|---|
| 73 | # accept plugin name |
|---|
| 74 | plugin = MichaelImporter |
|---|