home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_723 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  7.5 KB  |  207 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  6. import sys
  7. import os
  8. from calibre.utils.config import OptionParser
  9. from calibre.constants import iswindows
  10. from calibre import prints
  11.  
  12. def option_parser():
  13.     parser = OptionParser(usage = '%prog [options]\n\nRun an embedded python interpreter.\n')
  14.     parser.add_option('-c', '--command', help = 'Run python code.', default = None)
  15.     parser.add_option('-e', '--exec-file', default = None, help = 'Run the python code in file.')
  16.     parser.add_option('-d', '--debug-device-driver', default = False, action = 'store_true', help = 'Debug the specified device driver.')
  17.     parser.add_option('-g', '--gui', default = False, action = 'store_true', help = 'Run the GUI')
  18.     parser.add_option('-w', '--viewer', default = False, action = 'store_true', help = 'Run the ebook viewer')
  19.     parser.add_option('--paths', default = False, action = 'store_true', help = 'Output the paths necessary to setup the calibre environment')
  20.     parser.add_option('--migrate', action = 'store_true', default = False, help = 'Migrate old database. Needs two arguments. Path to library1.db and path to new library folder.')
  21.     parser.add_option('--add-simple-plugin', default = None, help = 'Add a simple plugin (i.e. a plugin that consists of only a .py file), by specifying the path to the py file containing the plugin code.')
  22.     parser.add_option('--reinitialize-db', default = None, help = 'Re-initialize the sqlite calibre database at the specified path. Useful to recover from db corruption.')
  23.     return parser
  24.  
  25.  
  26. def reinit_db(dbpath, callback = None):
  27.     if not os.path.exists(dbpath):
  28.         raise ValueError(dbpath + ' does not exist')
  29.     os.path.exists(dbpath)
  30.     connect = connect
  31.     import calibre.library.sqlite
  32.     closing = closing
  33.     import contextlib
  34.     import shutil
  35.     conn = connect(dbpath, False)
  36.     uv = conn.get('PRAGMA user_version;', all = False)
  37.     conn.execute('PRAGMA writable_schema=ON')
  38.     conn.commit()
  39.     sql_lines = conn.dump()
  40.     conn.close()
  41.     dest = dbpath + '.tmp'
  42.     
  43.     try:
  44.         
  45.         try:
  46.             nconn = _[1]
  47.             nconn.execute('create temporary table temp_sequence(id INTEGER PRIMARY KEY AUTOINCREMENT)')
  48.             nconn.commit()
  49.             for i, line in enumerate(sql_lines):
  50.                 
  51.                 try:
  52.                     nconn.execute(line)
  53.                 except:
  54.                     None if callable(callback) else closing(connect(dest, False))
  55.                     import traceback
  56.                     prints('SQL line %r failed with error:' % line)
  57.                     prints(traceback.format_exc())
  58.                     continue
  59.                 finally:
  60.                     pass
  61.  
  62.             
  63.             nconn.execute('pragma user_version=%d' % int(uv))
  64.             nconn.commit()
  65.         finally:
  66.             pass
  67.  
  68.         os.remove(dbpath)
  69.         shutil.copyfile(dest, dbpath)
  70.     finally:
  71.         if os.path.exists(dest):
  72.             os.remove(dest)
  73.         
  74.  
  75.     prints('Database successfully re-initialized')
  76.  
  77.  
  78. def migrate(old, new):
  79.     prefs = prefs
  80.     import calibre.utils.config
  81.     LibraryDatabase = LibraryDatabase
  82.     import calibre.library.database
  83.     LibraryDatabase2 = LibraryDatabase2
  84.     import calibre.library.database2
  85.     ProgressBar = ProgressBar
  86.     import calibre.utils.terminfo
  87.     terminal_controller = terminal_controller
  88.     import calibre
  89.     
  90.     class Dummy(ProgressBar):
  91.         
  92.         def setLabelText(self, x):
  93.             pass
  94.  
  95.         
  96.         def setAutoReset(self, y):
  97.             pass
  98.  
  99.         
  100.         def reset(self):
  101.             pass
  102.  
  103.         
  104.         def setRange(self, min, max):
  105.             self.min = min
  106.             self.max = max
  107.  
  108.         
  109.         def setValue(self, val):
  110.             self.update(float(val) / getattr(self, 'max', 1))
  111.  
  112.  
  113.     db = LibraryDatabase(old)
  114.     db2 = LibraryDatabase2(new)
  115.     db2.migrate_old(db, Dummy(terminal_controller, 'Migrating database...'))
  116.     prefs['library_path'] = os.path.abspath(new)
  117.     print 'Database migrated to', os.path.abspath(new)
  118.  
  119.  
  120. def debug_device_driver():
  121.     debug = debug
  122.     import calibre.devices
  123.     debug(ioreg_to_tmp = True, buf = sys.stdout)
  124.     if iswindows:
  125.         raw_input('Press Enter to continue...')
  126.     
  127.  
  128.  
  129. def add_simple_plugin(path_to_plugin):
  130.     import tempfile
  131.     import zipfile
  132.     import shutil
  133.     tdir = tempfile.mkdtemp()
  134.     open(os.path.join(tdir, 'custom_plugin.py'), 'wb').write(open(path_to_plugin, 'rb').read())
  135.     odir = os.getcwd()
  136.     os.chdir(tdir)
  137.     zf = zipfile.ZipFile('plugin.zip', 'w')
  138.     zf.write('custom_plugin.py')
  139.     zf.close()
  140.     main = main
  141.     import calibre.customize.ui
  142.     main([
  143.         'calibre-customize',
  144.         '-a',
  145.         'plugin.zip'])
  146.     os.chdir(odir)
  147.     shutil.rmtree(tdir)
  148.  
  149.  
  150. def main(args = sys.argv):
  151.     debug = debug
  152.     import calibre.constants
  153.     debug()
  154.     if len(args) > 2 and args[1] in ('-e', '--exec-file'):
  155.         sys.argv = [
  156.             args[2]] + args[3:]
  157.         ef = os.path.abspath(args[2])
  158.         base = os.path.dirname(ef)
  159.         sys.path.insert(0, base)
  160.         g = globals()
  161.         g['__name__'] = '__main__'
  162.         execfile(ef, g)
  163.         return None
  164.     (opts, args) = option_parser().parse_args(args)
  165.     if opts.gui:
  166.         main = main
  167.         import calibre.gui2.main
  168.         main([
  169.             'calibre'])
  170.     elif opts.viewer:
  171.         main = main
  172.         import calibre.gui2.viewer.main
  173.         vargs = [
  174.             'ebook-viewer',
  175.             '--debug-javascript']
  176.         if len(args) > 1:
  177.             vargs.append(args[-1])
  178.         
  179.         main(vargs)
  180.     elif opts.command:
  181.         sys.argv = args[:1]
  182.         exec opts.command
  183.     elif opts.debug_device_driver:
  184.         debug_device_driver()
  185.     elif opts.migrate:
  186.         if len(args) < 3:
  187.             print 'You must specify the path to library1.db and the path to the new library folder'
  188.             return 1
  189.         migrate(args[1], args[2])
  190.     elif opts.add_simple_plugin is not None:
  191.         add_simple_plugin(opts.add_simple_plugin)
  192.     elif opts.paths:
  193.         prints('CALIBRE_RESOURCES_PATH=' + sys.resources_location)
  194.         prints('CALIBRE_EXTENSIONS_PATH=' + sys.extensions_location)
  195.         prints('CALIBRE_PYTHON_PATH=' + os.pathsep.join(sys.path))
  196.     elif opts.reinitialize_db is not None:
  197.         reinit_db(opts.reinitialize_db)
  198.     else:
  199.         ipython = ipython
  200.         import calibre
  201.         ipython()
  202.     return 0
  203.  
  204. if __name__ == '__main__':
  205.     sys.exit(main())
  206.  
  207.