Ticket #10637: trac_10637-scripts.patch

File trac_10637-scripts.patch, 2.5 KB (added by kcrisman, 9 years ago)

Apply to local/bin

  • new file sage-sws2rst

    # HG changeset patch
    # User Pablo Angulo <pablo.angulo@uam.es>
    # Date 1302082947 -7200
    # Node ID 0d3873e28607f6f8cffbe41bb0d21bf88d817cc1
    # Parent  4a2cf53f33ce82b0c858c815c8cfc3c65d6e139d
    Trac 10637 - add sage-sws2rst script to local/bin
    
    diff --git a/sage-sws2rst b/sage-sws2rst
    new file mode 100755
    - +  
     1#!/usr/bin/env python
     2# -*- coding: utf-8 -*-
     3
     4import sys
     5import tarfile
     6import os
     7import shutil
     8import codecs
     9import tempfile
     10from sagenb.misc.worksheet2rst import worksheet2rst
     11
     12def process_sws(file_name):
     13   
     14    sws_file = tarfile.open(file_name, mode='r:bz2')
     15    #TODO: python complains about using tempnam, but I don't
     16    #know hot to fix it or see any danger
     17#    tempname = os.tempnam('.')
     18    tempname = os.path.join(tempfile.gettempdir(), file_name)
     19    sws_file.extractall(tempname)
     20    base_name = os.path.split(os.path.splitext(file_name)[0])[1]
     21    base_name_clean = base_name.replace(' ','_')
     22
     23    #Images
     24    images_dir = base_name_clean + '_media'
     25    if not os.path.exists(images_dir):
     26        os.mkdir(images_dir)
     27   
     28    #"data" dir
     29    data_path = os.path.join(tempname,'sage_worksheet','data')
     30    if os.path.exists(data_path):
     31        for image in os.listdir(data_path):
     32            try:
     33                shutil.move(os.path.join(data_path, image), os.path.join(images_dir, image.replace(' ','_')))
     34            except shutil.Error:
     35                pass
     36   
     37    #cells
     38    cells_path = os.path.join(tempname,'sage_worksheet','cells')   
     39    for cell in os.listdir(cells_path):
     40        cell_path = os.path.join(cells_path, cell)
     41        for image in os.listdir(cell_path):
     42            shutil.copy2(os.path.join(cell_path, image),
     43                         os.path.join(images_dir, 'cell_%s_%s'%(cell,image)))
     44   
     45    #read html file, parse it, write rst file
     46    file = codecs.open(os.path.join('.',tempname,'sage_worksheet','worksheet.html'),
     47                          mode='r',
     48                          encoding='utf-8')
     49    html_text = file.read()
     50    file.close()
     51    rst_text = worksheet2rst(html_text, images_dir = images_dir)
     52    rst_file = base_name_clean + '.rst'
     53    out_file = codecs.open(rst_file, mode='w',
     54                  encoding='utf-8')
     55    out_file.write(rst_text)
     56    out_file.close()
     57   
     58    shutil.rmtree(tempname)
     59   
     60if __name__=='__main__':
     61    if len(sys.argv)<=1:
     62        print 'First argument should be a sws file'
     63        sys.exit()
     64    for file_name in sys.argv[1:]:
     65        print file_name
     66        process_sws(file_name)
     67
     68