forcing everything python3, might end up with bugs
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright 2014 Ville Rantanen
|
||||
#
|
||||
@@ -17,14 +17,21 @@
|
||||
#
|
||||
|
||||
'''SC based CSV editor.'''
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
from builtins import chr
|
||||
from builtins import str
|
||||
from builtins import range
|
||||
from past.utils import old_div
|
||||
from builtins import object
|
||||
__author__ = "Ville Rantanen"
|
||||
|
||||
__version__ = "0.2"
|
||||
|
||||
import sys,os
|
||||
import csv
|
||||
from argparse import ArgumentParser
|
||||
from argparse import ArgumentParser
|
||||
import unicodedata, re
|
||||
import subprocess
|
||||
import shutil
|
||||
@@ -46,14 +53,14 @@ def which(program):
|
||||
|
||||
return None
|
||||
|
||||
class SCReader:
|
||||
class SCReader(object):
|
||||
""" Class for reading SC files.
|
||||
|
||||
"""
|
||||
|
||||
"""
|
||||
def __init__(self,fileobject):
|
||||
self.file=fileobject
|
||||
self.parserre=re.compile('.* ([A-Z]+)([0-9]+) = (.*)')
|
||||
|
||||
|
||||
def _parse_row(self,string):
|
||||
col=None
|
||||
row=None
|
||||
@@ -64,14 +71,14 @@ class SCReader:
|
||||
row=self.try_int(m.group(2))
|
||||
content=m.group(3)
|
||||
return col,row,content
|
||||
|
||||
|
||||
def try_int(self,string):
|
||||
|
||||
|
||||
try:
|
||||
return int(string)
|
||||
except:
|
||||
return string
|
||||
|
||||
|
||||
def alpha_to_column(self,alpha):
|
||||
''' Returns a column number from spreadsheet column alphabet '''
|
||||
n=0
|
||||
@@ -80,28 +87,28 @@ class SCReader:
|
||||
o+=(ord(char.upper())-64)*(26**n)
|
||||
n+=1
|
||||
return int(o-1)
|
||||
|
||||
|
||||
def next(self):
|
||||
|
||||
|
||||
def __next__(self):
|
||||
''' Returns the next row in the table, three items: column, row, content'''
|
||||
return self._parse_row(self.reader.next())
|
||||
return self._parse_row(next(self.reader))
|
||||
|
||||
def __iter__(self):
|
||||
for row in self.file:
|
||||
yield self._parse_row(row)
|
||||
|
||||
class SCWriter:
|
||||
class SCWriter(object):
|
||||
""" Class for writing SC files.
|
||||
|
||||
"""
|
||||
|
||||
"""
|
||||
def __init__(self,fileobject):
|
||||
self.file=fileobject
|
||||
self.row=0
|
||||
self.col=0
|
||||
self.col_lengths=[]
|
||||
|
||||
|
||||
def parse_row(self,string):
|
||||
|
||||
|
||||
self.col=0
|
||||
for el in row:
|
||||
self.write_element(self.row,self.col,el)
|
||||
@@ -111,34 +118,34 @@ class SCWriter:
|
||||
self.col_lengths[self.col]=max(len(el),self.col_lengths[self.col])
|
||||
self.col+=1
|
||||
self.row+=1
|
||||
|
||||
|
||||
def write_element(self,row,col,content):
|
||||
|
||||
|
||||
colalpha=self.column_to_alpha(col)
|
||||
content=content.strip('"')
|
||||
|
||||
|
||||
if self.is_num(content):
|
||||
self.file.write('let '+colalpha+str(row)+' = ' + str(self.to_num(content))+'\n')
|
||||
else:
|
||||
self.file.write('rightstring '+colalpha+str(row)+' = "' + content + '"\n')
|
||||
|
||||
|
||||
|
||||
|
||||
def column_to_alpha(self,column):
|
||||
''' Returns a column alphabet from column number '''
|
||||
o=chr(column+64+1)
|
||||
if column>25:
|
||||
return self.column_to_alpha((column / 26) -1) + self.column_to_alpha(column % 26);
|
||||
return self.column_to_alpha((old_div(column, 26)) -1) + self.column_to_alpha(column % 26);
|
||||
return o
|
||||
|
||||
|
||||
def write_row(self,row):
|
||||
''' Writes a row as a SC file part '''
|
||||
self.parse_row(row)
|
||||
|
||||
|
||||
def write_formats(self):
|
||||
|
||||
|
||||
for col in range(len(self.col_lengths)):
|
||||
self.file.write('format '+self.column_to_alpha(col)+' '+str(self.col_lengths[col])+' 2 0\n')
|
||||
|
||||
|
||||
def is_num(self,string):
|
||||
''' returns the True if string can be converted to number safely '''
|
||||
try:
|
||||
@@ -152,7 +159,7 @@ class SCWriter:
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
return False
|
||||
|
||||
def to_num(self,string):
|
||||
@@ -168,12 +175,12 @@ class SCWriter:
|
||||
return num
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
return string
|
||||
|
||||
def setup_options():
|
||||
''' Setup the command line options '''
|
||||
|
||||
|
||||
parser=ArgumentParser()
|
||||
parser.add_argument("-v","--version",action='version', version=__version__)
|
||||
parser.add_argument("-b",action="store_false",dest="backup",default=True,
|
||||
@@ -185,8 +192,8 @@ def setup_options():
|
||||
parser.add_argument("tsv",type=str,action="store",
|
||||
help="TSV file to edit")
|
||||
options=parser.parse_args()
|
||||
return options
|
||||
|
||||
return options
|
||||
|
||||
def csv_write(screader,fileout):
|
||||
''' writes a CSV from SCReader iterator '''
|
||||
content=[]
|
||||
|
||||
Reference in New Issue
Block a user