obsolete/exp_anki.py
changeset 668 e22ffba8ecc0
parent 667 5f69f0776c37
child 669 82da364db365
equal deleted inserted replaced
667:5f69f0776c37 668:e22ffba8ecc0
     1 import sys
     1 import sys
       
     2 import os
     2 import tempfile
     3 import tempfile
       
     4 import shutil
     3 
     5 
     4 FONAME = "test.apkg"
     6 FONAME = "test.apkg"
     5 
     7 
     6 # Looks like anki libs change working directory to media directory of current deck
     8 # Looks like anki libs change working directory to media directory of current deck
     7 # Therefore absolute path should be stored before creating temporary deck
     9 # Therefore absolute path should be stored before creating temporary deck
     8 FONAME = os.path.abspath(FONAME)
    10 FONAME = os.path.abspath(FONAME)
     9 FBASENAME, _ = os.path.splitext(os.path.basename(FONAME))
    11 FBASENAME, _ = os.path.splitext(os.path.basename(FONAME))
       
    12 
       
    13 CWD = os.getcwd()
    10 TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))
    14 TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))
       
    15 
       
    16 def cleanup(type_, ex, traceback):
       
    17     os.chdir(CWD)
       
    18     shutil.rmtree(TMPDIR)
       
    19 
       
    20 sys.excepthook = cleanup
    11 
    21 
    12 sys.path.append('/usr/share/anki')
    22 sys.path.append('/usr/share/anki')
    13 import anki
    23 import anki
    14 from anki.exporting import AnkiPackageExporter
    24 from anki.exporting import AnkiPackageExporter
    15 
    25 
    86 
    96 
    87 # model.add(deck)
    97 # model.add(deck)
    88 # model.save()
    98 # model.save()
    89 
    99 
    90 export = AnkiPackageExporter(collection)
   100 export = AnkiPackageExporter(collection)
       
   101 try:
       
   102     os.remove(FONAME)
       
   103 except OSError as ex:
       
   104     if ex.errno not in [2]:
       
   105         raise
    91 export.exportInto(FONAME)
   106 export.exportInto(FONAME)
    92 
   107 
    93 cleanup()
   108 cleanup()