py_ext/py_ext.py
changeset 4060 d2f5eb3c7d6e
parent 4056 4b2de1a0fbf9
equal deleted inserted replaced
4059:03f007a175b5 4060:d2f5eb3c7d6e
    60             csvfile.close()
    60             csvfile.close()
    61         csv_int_files[fname] = data
    61         csv_int_files[fname] = data
    62 
    62 
    63     try:
    63     try:
    64         row = data[rowidx]
    64         row = data[rowidx]
       
    65         if not row and rowidx == len(data)-1:
       
    66             raise IndexError
    65     except IndexError:
    67     except IndexError:
    66         return "#ROW_NOT_FOUND"
    68         return "#ROW_NOT_FOUND"
    67 
    69 
    68     try:
    70     try:
    69         return row[colidx]
    71         return row[colidx]
   114         return row[colidx]
   116         return row[colidx]
   115     except IndexError:
   117     except IndexError:
   116         return "#COL_NOT_FOUND"
   118         return "#COL_NOT_FOUND"
   117 
   119 
   118 
   120 
   119 csv_int_files = {}
   121 def CSVWrInt(fname, rowidx, colidx, content):
   120 def CSVWrInt(fname, rowidx, colidx, content, save):
       
   121     \"\"\"
   122     \"\"\"
   122     Update value at row/column pointed by integer indexes
   123     Update value at row/column pointed by integer indexes
   123     Assumes data starts at first row and first column, no headers.
   124     Assumes data starts at first row and first column, no headers.
   124     \"\"\"
   125     \"\"\"
   125     if save > 0:
   126 
   126         global csv_int_files
   127     global csv_int_files
   127         data = csv_int_files.get(fname, None)
   128     dialect = None
   128         if data is None:
   129     data = csv_int_files.get(fname, None)
   129             data = list()
   130     if data is None:
   130             try:
   131         data = list()
   131                 csvfile = open(fname, 'rt', encoding='utf-8')
   132         try:
   132             except IOError:
   133             csvfile = open(fname, 'rt', encoding='utf-8')
   133                 return "#FILE_NOT_FOUND"
   134         except IOError:
   134             try:
   135             return "#FILE_NOT_FOUND"
   135                 dialect = csv.Sniffer().sniff(csvfile.read(1024))
   136         try:
   136                 csvfile.seek(0)
   137             dialect = csv.Sniffer().sniff(csvfile.read(1024))
   137                 reader = csv.reader(csvfile, dialect)
   138             csvfile.seek(0)
   138                 for row in reader:
   139             reader = csv.reader(csvfile, dialect)
   139                     data.append(row)
   140             for row in reader:
   140             except csv.Error as e:
   141                 data.append(row)
   141                 return "#CSV_ERROR"
   142         except csv.Error as e:
   142             finally:
   143             return "#CSV_ERROR"
   143                 csvfile.close()
   144         finally:
   144             csv_int_files[fname] = data
   145             csvfile.close()
   145 
   146         csv_int_files[fname] = data
   146         try:
   147 
       
   148     try:
       
   149         if rowidx == len(data):
       
   150             row = []
       
   151             data.append(row)
       
   152         else:
   147             row = data[rowidx]
   153             row = data[rowidx]
   148         except IndexError:
   154     except IndexError:
   149             return "#ROW_NOT_FOUND"
   155         return "#ROW_NOT_FOUND"
   150     
   156 
   151         try:
   157     try:
       
   158         if rowidx > 0 and colidx >= len(data[0]):
       
   159             raise IndexError
       
   160         if colidx >= len(row):
       
   161             row.extend([""] * (colidx - len(row)) + [content])
       
   162         else:
   152             row[colidx] = content
   163             row[colidx] = content
   153         except IndexError:
   164     except IndexError:
   154             return "#COL_NOT_FOUND"
   165         return "#COL_NOT_FOUND"
   155             
   166 
   156         wfile = open(fname, 'rt+', encoding='utf-8')
   167     try:
   157         wdialect = csv.Sniffer().sniff(wfile.read(1024))
   168         wfile = open(fname, 'wt')
   158         wfile.seek(0)
   169         writer = csv.writer(wfile) if not(dialect) else csv.writer(wfile, dialect)
   159         writer = csv.writer(wfile, wdialect)
       
   160         for row in data:
   170         for row in data:
   161             writer.writerow(row)
   171             writer.writerow(row)
   162         wfile.truncate()
   172     finally:
   163         wfile.close()
   173         wfile.close()
   164         
   174 
   165     return "#SUCCESS"
   175     return "OK"
   166 
   176 
   167 
   177 
   168 def pyext_csv_reload():
   178 def pyext_csv_reload():
   169     global csv_int_files, csv_str_files
   179     global csv_int_files, csv_str_files
   170     csv_int_files.clear()
   180     csv_int_files.clear()