home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_715 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  8.1 KB  |  222 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. You can also specify the path to an SQL dump which will be used instead of trying to dump the database. This can be useful when dumping fails, but dumping with sqlite3 works.')
  23.     parser.add_option('-p', '--py-console', help = 'Run python console', default = False, action = 'store_true')
  24.     return parser
  25.  
  26.  
  27. def reinit_db(dbpath, callback = None, sql_dump = None):
  28.     if not os.path.exists(dbpath):
  29.         raise ValueError(dbpath + ' does not exist')
  30.     os.path.exists(dbpath)
  31.     connect = connect
  32.     import calibre.library.sqlite
  33.     closing = closing
  34.     import contextlib
  35.     import shutil
  36.     conn = connect(dbpath, False)
  37.     uv = conn.get('PRAGMA user_version;', all = False)
  38.     conn.execute('PRAGMA writable_schema=ON')
  39.     conn.commit()
  40.     if sql_dump is None:
  41.         sql_lines = conn.dump()
  42.     else:
  43.         sql_lines = open(sql_dump, 'rb').read()
  44.     conn.close()
  45.     dest = dbpath + '.tmp'
  46.     
  47.     try:
  48.         
  49.         try:
  50.             nconn = _[1]
  51.             nconn.execute('create temporary table temp_sequence(id INTEGER PRIMARY KEY AUTOINCREMENT)')
  52.             nconn.commit()
  53.             if sql_dump is None:
  54.                 for i, line in enumerate(sql_lines):
  55.                     
  56.                     try:
  57.                         nconn.execute(line)
  58.                     except:
  59.                         None if callable(callback) else closing(connect(dest, False))
  60.                         import traceback
  61.                         prints('SQL line %r failed with error:' % line)
  62.                         prints(traceback.format_exc())
  63.                         continue
  64.                     finally:
  65.                         pass
  66.  
  67.                 
  68.             else:
  69.                 nconn.executescript(sql_lines)
  70.             nconn.execute('pragma user_version=%d' % int(uv))
  71.             nconn.commit()
  72.         finally:
  73.             pass
  74.  
  75.         os.remove(dbpath)
  76.         shutil.copyfile(dest, dbpath)
  77.     finally:
  78.         if os.path.exists(dest):
  79.             os.remove(dest)
  80.         
  81.  
  82.     prints('Database successfully re-initialized')
  83.  
  84.  
  85. def migrate(old, new):
  86.     prefs = prefs
  87.     import calibre.utils.config
  88.     LibraryDatabase = LibraryDatabase
  89.     import calibre.library.database
  90.     LibraryDatabase2 = LibraryDatabase2
  91.     import calibre.library.database2
  92.     ProgressBar = ProgressBar
  93.     import calibre.utils.terminfo
  94.     terminal_controller = terminal_controller
  95.     import calibre
  96.     
  97.     class Dummy(ProgressBar):
  98.         
  99.         def setLabelText(self, x):
  100.             pass
  101.  
  102.         
  103.         def setAutoReset(self, y):
  104.             pass
  105.  
  106.         
  107.         def reset(self):
  108.             pass
  109.  
  110.         
  111.         def setRange(self, min, max):
  112.             self.min = min
  113.             self.max = max
  114.  
  115.         
  116.         def setValue(self, val):
  117.             self.update(float(val) / getattr(self, 'max', 1))
  118.  
  119.  
  120.     db = LibraryDatabase(old)
  121.     db2 = LibraryDatabase2(new)
  122.     db2.migrate_old(db, Dummy(terminal_controller, 'Migrating database...'))
  123.     prefs['library_path'] = os.path.abspath(new)
  124.     print 'Database migrated to', os.path.abspath(new)
  125.  
  126.  
  127. def debug_device_driver():
  128.     debug = debug
  129.     import calibre.devices
  130.     debug(ioreg_to_tmp = True, buf = sys.stdout)
  131.     if iswindows:
  132.         raw_input('Press Enter to continue...')
  133.     
  134.  
  135.  
  136. def add_simple_plugin(path_to_plugin):
  137.     import tempfile
  138.     import zipfile
  139.     import shutil
  140.     tdir = tempfile.mkdtemp()
  141.     open(os.path.join(tdir, 'custom_plugin.py'), 'wb').write(open(path_to_plugin, 'rb').read())
  142.     odir = os.getcwd()
  143.     os.chdir(tdir)
  144.     zf = zipfile.ZipFile('plugin.zip', 'w')
  145.     zf.write('custom_plugin.py')
  146.     zf.close()
  147.     main = main
  148.     import calibre.customize.ui
  149.     main([
  150.         'calibre-customize',
  151.         '-a',
  152.         'plugin.zip'])
  153.     os.chdir(odir)
  154.     shutil.rmtree(tdir)
  155.  
  156.  
  157. def main(args = sys.argv):
  158.     debug = debug
  159.     import calibre.constants
  160.     debug()
  161.     if len(args) > 2 and args[1] in ('-e', '--exec-file'):
  162.         sys.argv = [
  163.             args[2]] + args[3:]
  164.         ef = os.path.abspath(args[2])
  165.         base = os.path.dirname(ef)
  166.         sys.path.insert(0, base)
  167.         g = globals()
  168.         g['__name__'] = '__main__'
  169.         execfile(ef, g)
  170.         return None
  171.     (opts, args) = option_parser().parse_args(args)
  172.     if opts.gui:
  173.         main = main
  174.         import calibre.gui2.main
  175.         main([
  176.             'calibre'])
  177.     elif opts.viewer:
  178.         main = main
  179.         import calibre.gui2.viewer.main
  180.         vargs = [
  181.             'ebook-viewer',
  182.             '--debug-javascript']
  183.         if len(args) > 1:
  184.             vargs.append(args[-1])
  185.         
  186.         main(vargs)
  187.     elif opts.py_console:
  188.         main = main
  189.         import calibre.utils.pyconsole.main
  190.         main()
  191.     elif opts.command:
  192.         sys.argv = args[:1]
  193.         exec opts.command
  194.     elif opts.debug_device_driver:
  195.         debug_device_driver()
  196.     elif opts.migrate:
  197.         if len(args) < 3:
  198.             print 'You must specify the path to library1.db and the path to the new library folder'
  199.             return 1
  200.         migrate(args[1], args[2])
  201.     elif opts.add_simple_plugin is not None:
  202.         add_simple_plugin(opts.add_simple_plugin)
  203.     elif opts.paths:
  204.         prints('CALIBRE_RESOURCES_PATH=' + sys.resources_location)
  205.         prints('CALIBRE_EXTENSIONS_PATH=' + sys.extensions_location)
  206.         prints('CALIBRE_PYTHON_PATH=' + os.pathsep.join(sys.path))
  207.     elif opts.reinitialize_db is not None:
  208.         sql_dump = None
  209.         if len(args) > 1 and os.access(args[-1], os.R_OK):
  210.             sql_dump = args[-1]
  211.         
  212.         reinit_db(opts.reinitialize_db, sql_dump = sql_dump)
  213.     else:
  214.         ipython = ipython
  215.         import calibre
  216.         ipython()
  217.     return 0
  218.  
  219. if __name__ == '__main__':
  220.     sys.exit(main())
  221.  
  222.