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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cStringIO
  5. import os
  6. import string
  7. import sys
  8. import tempfile
  9. import time
  10. import types
  11. import Gnuplot as Gnuplot_ori
  12. import Numeric
  13. from IPython.genutils import popkey, xsys
  14. gp = Gnuplot_ori.gp
  15.  
  16. try:
  17.     OptionException = Gnuplot_ori.PlotItems.OptionException
  18. except AttributeError:
  19.     OptionException = Gnuplot_ori.Errors.OptionError
  20.  
  21. Data = Gnuplot_ori.Data
  22. Func = Gnuplot_ori.Func
  23. GridData = Gnuplot_ori.GridData
  24. PlotItem = Gnuplot_ori.PlotItem
  25. PlotItems = Gnuplot_ori.PlotItems
  26. if Gnuplot_ori.__version__ <= '1.6':
  27.     _BaseFileItem = PlotItems.File
  28.     _BaseTempFileItem = PlotItems.TempFile
  29.     
  30.     class File(_BaseFileItem):
  31.         _option_list = _BaseFileItem._option_list.copy()
  32.         _option_list.update({
  33.             'index': (lambda self, index: self.set_option_index(index)) })
  34.         
  35.         def __init__(self, *args, **kw):
  36.             self._option_sequence = [
  37.                 'binary',
  38.                 'index',
  39.                 'using',
  40.                 'smooth',
  41.                 'axes',
  42.                 'title',
  43.                 'with']
  44.             _BaseFileItem.__init__(self, *args, **kw)
  45.  
  46.         __newdoc = "Additional Keyword arguments added by IPython:\n\n             'index=<int>' -- similar to the `index` keyword in Gnuplot.\n                 This allows only some of the datasets in a file to be\n                 plotted. Datasets within a file are assumed to be separated\n                 by _pairs_ of blank lines, and the first one is numbered as\n                 0 (similar to C/Python usage)."
  47.         __init__.__doc__ = PlotItems.File.__init__.__doc__ + __newdoc
  48.         
  49.         def set_option_index(self, index):
  50.             if index is None:
  51.                 self.clear_option('index')
  52.             elif type(index) in [
  53.                 type(''),
  54.                 type(1)]:
  55.                 self._options['index'] = (index, 'index %s' % index)
  56.             elif type(index) is type(()):
  57.                 self._options['index'] = (index, 'index %s' % string.join(map(repr, index), ':'))
  58.             else:
  59.                 raise OptionException('index=%s' % (index,))
  60.             return index is None
  61.  
  62.  
  63.     _FileClass = File
  64. elif Gnuplot_ori.__version__ == '1.7':
  65.     _FileClass = _BaseFileItem = PlotItems._FileItem
  66.     _BaseTempFileItem = PlotItems._TempFileItem
  67.     File = PlotItems.File
  68. else:
  69.     _FileClass = _BaseFileItem = PlotItems._FileItem
  70.     _BaseTempFileItem = PlotItems._NewFileItem
  71.     File = PlotItems.File
  72.  
  73. def eps_fix_bbox(fname):
  74.     print 'Fixing eps file: <%s>' % fname
  75.     xsys('ps2eps -f -q -l %s' % fname)
  76.     if fname.endswith('.eps'):
  77.         os.rename(fname + '.eps', fname)
  78.     
  79.  
  80.  
  81. def is_list1d(x, containers = [
  82.     types.ListType,
  83.     types.TupleType]):
  84.     x_type = type(x)
  85.     if (x_type == Numeric.ArrayType or len(x.shape) == 1) and x_type in containers:
  86.         pass
  87.     return type(x[0]) not in containers + [
  88.         Numeric.ArrayType]
  89.  
  90.  
  91. def zip_items(items, titles = None):
  92.     
  93.     class StandaloneItem(Exception):
  94.         pass
  95.  
  96.     
  97.     def get_titles(titles):
  98.         
  99.         try:
  100.             title = titles[tit_ct[0]]
  101.         except IndexError:
  102.             titles = None
  103.             title = None
  104.  
  105.         tit_ct[0] += 1
  106.         return (title, titles)
  107.  
  108.     new_items = []
  109.     if titles:
  110.         tit_ct = [
  111.             0]
  112.     
  113.     n = 0
  114.     while n < len(items):
  115.         item = items[n]
  116.         
  117.         try:
  118.             if is_list1d(item):
  119.                 if n == len(items) - 1:
  120.                     raise StandaloneItem
  121.                 n == len(items) - 1
  122.                 next_item = items[n + 1]
  123.                 if next_item is None:
  124.                     n += 1
  125.                     raise StandaloneItem
  126.                 next_item is None
  127.                 if is_list1d(next_item):
  128.                     if titles:
  129.                         (title, titles) = get_titles(titles)
  130.                     else:
  131.                         title = None
  132.                     new_items.append(Data(zip(item, next_item), title = title))
  133.                     n += 1
  134.                 else:
  135.                     raise StandaloneItem
  136.             is_list1d(next_item)
  137.             new_items.append(item)
  138.         except StandaloneItem:
  139.             if titles:
  140.                 (title, titles) = get_titles(titles)
  141.             else:
  142.                 title = None
  143.             new_items.append(Data(zip(range(len(item)), item), title = title))
  144.         except AttributeError:
  145.             new_items.append(item)
  146.  
  147.         n += 1
  148.     return new_items
  149.  
  150.  
  151. class String(_FileClass):
  152.     
  153.     def __init__(self, data_str, **keyw):
  154.         self.tmpfile = _BaseTempFileItem()
  155.         tmpfile = file(self.tmpfile.filename, 'w')
  156.         tmpfile.write(data_str)
  157.         _BaseFileItem.__init__(self, self.tmpfile, **keyw)
  158.  
  159.  
  160.  
  161. class Gnuplot(Gnuplot_ori.Gnuplot):
  162.     
  163.     def xrange(self, min = '*', max = '*'):
  164.         self('set xrange [%s:%s]' % (min, max))
  165.  
  166.     
  167.     def yrange(self, min = '*', max = '*'):
  168.         self('set yrange [%s:%s]' % (min, max))
  169.  
  170.     
  171.     def zrange(self, min = '*', max = '*'):
  172.         self('set zrange [%s:%s]' % (min, max))
  173.  
  174.     
  175.     def x2range(self, min = '*', max = '*'):
  176.         self('set xrange [%s:%s]' % (min, max))
  177.  
  178.     
  179.     def y2range(self, min = '*', max = '*'):
  180.         self('set yrange [%s:%s]' % (min, max))
  181.  
  182.     
  183.     def z2range(self, min = '*', max = '*'):
  184.         self('set zrange [%s:%s]' % (min, max))
  185.  
  186.     
  187.     def rrange(self, min = '*', max = '*'):
  188.         self('set rrange [%s:%s]' % (min, max))
  189.  
  190.     
  191.     def trange(self, min = '*', max = '*'):
  192.         self('set trange [%s:%s]' % (min, max))
  193.  
  194.     
  195.     def urange(self, min = '*', max = '*'):
  196.         self('set urange [%s:%s]' % (min, max))
  197.  
  198.     
  199.     def vrange(self, min = '*', max = '*'):
  200.         self('set vrange [%s:%s]' % (min, max))
  201.  
  202.     
  203.     def set_ps(self, option):
  204.         self('set terminal postscript %s ' % option)
  205.         self('set terminal %s' % gp.GnuplotOpts.default_term)
  206.  
  207.     
  208.     def __plot_ps(self, plot_method, *items, **keyw):
  209.         psargs = {
  210.             'filename': None,
  211.             'mode': None,
  212.             'eps': None,
  213.             'enhanced': None,
  214.             'color': None,
  215.             'solid': None,
  216.             'duplexing': None,
  217.             'fontname': None,
  218.             'fontsize': None,
  219.             'debug': 0 }
  220.         for k in psargs.keys():
  221.             if keyw.has_key(k):
  222.                 psargs[k] = keyw[k]
  223.                 del keyw[k]
  224.                 continue
  225.         
  226.         hardcopy = popkey(keyw, 'hardcopy', psargs['filename'] is not None)
  227.         titles = popkey(keyw, 'titles', 0)
  228.         if hardcopy:
  229.             if psargs['filename'] is None:
  230.                 raise ValueError, 'If you request hardcopy, you must give a filename.'
  231.             psargs['filename'] is None
  232.             self('set term dumb')
  233.             if os.name == 'posix':
  234.                 self('set output "/dev/null"')
  235.             
  236.         
  237.         new_items = zip_items(items, titles)
  238.         plot_method(self, *new_items, **keyw)
  239.         if hardcopy:
  240.             if psargs['filename'].endswith('.eps'):
  241.                 psargs['eps'] = 1
  242.             
  243.             self.hardcopy(**psargs)
  244.         
  245.  
  246.     
  247.     def plot(self, *items, **keyw):
  248.         self._Gnuplot__plot_ps(Gnuplot_ori.Gnuplot.plot, *items, **keyw)
  249.  
  250.     
  251.     def plot2(self, arg, **kw):
  252.         if hasattr(arg, 'keys'):
  253.             keys = arg.keys()
  254.             keys.sort()
  255.         else:
  256.             keys = range(len(arg))
  257.         pitems = [ Data(zip(range(len(arg[k])), arg[k]), title = `k`) for k in keys ]
  258.         self.plot(*pitems, **kw)
  259.  
  260.     
  261.     def splot(self, *items, **keyw):
  262.         self._Gnuplot__plot_ps(Gnuplot_ori.Gnuplot.splot, *items, **keyw)
  263.  
  264.     
  265.     def replot(self, *items, **keyw):
  266.         self._Gnuplot__plot_ps(Gnuplot_ori.Gnuplot.replot, *items, **keyw)
  267.  
  268.     
  269.     def hardcopy(self, filename = None, mode = None, eps = None, enhanced = None, color = None, solid = None, duplexing = None, fontname = None, fontsize = None, debug = 0):
  270.         if filename is None:
  271.             filename = gp.GnuplotOpts.default_lpr
  272.             lpr_output = 1
  273.         elif filename.endswith('.eps'):
  274.             eps = 1
  275.         
  276.         lpr_output = 0
  277.         setterm = [
  278.             'set',
  279.             'terminal',
  280.             'postscript']
  281.         if eps:
  282.             setterm.append('eps')
  283.         elif mode is not None:
  284.             setterm.append(mode)
  285.         
  286.         if enhanced is None:
  287.             enhanced = gp.GnuplotOpts.prefer_enhanced_postscript
  288.         
  289.         if enhanced is not None:
  290.             if enhanced:
  291.                 setterm.append('enhanced')
  292.             else:
  293.                 setterm.append('noenhanced')
  294.         
  295.         if color is not None:
  296.             if color:
  297.                 setterm.append('color')
  298.             else:
  299.                 setterm.append('monochrome')
  300.         
  301.         if solid is not None:
  302.             if solid:
  303.                 setterm.append('solid')
  304.             else:
  305.                 setterm.append('dashed')
  306.         
  307.         if duplexing is not None:
  308.             setterm.append(duplexing)
  309.         
  310.         if fontname is not None:
  311.             setterm.append('"%s"' % fontname)
  312.         
  313.         if fontsize is not None:
  314.             setterm.append('%s' % fontsize)
  315.         
  316.         self(string.join(setterm))
  317.         self.set_string('output', filename)
  318.         self.refresh()
  319.         if not lpr_output:
  320.             maxtries = 20
  321.             delay = 0.1
  322.             for i in range(maxtries):
  323.                 time.sleep(0.05)
  324.                 if os.path.isfile(filename):
  325.                     if debug:
  326.                         print 'Hardcopy to file <%s> success at attempt #%s.' % (filename, i + 1)
  327.                     
  328.                     break
  329.                 
  330.                 time.sleep(delay)
  331.                 self(string.join(setterm))
  332.                 self.set_string('output', filename)
  333.                 self.refresh()
  334.             
  335.             if not os.path.isfile(filename):
  336.                 print >>sys.stderr, 'ERROR: Tried %s times and failed to create hardcopy file `%s`' % (maxtries, filename)
  337.             
  338.         
  339.         self('set terminal %s' % gp.GnuplotOpts.default_term)
  340.         self.set_string('output')
  341.  
  342.  
  343.