141 lines
3.8 KiB
Python
Executable File
141 lines
3.8 KiB
Python
Executable File
#!/usr/bin/python
|
|
import csv
|
|
import os
|
|
import getopt
|
|
import sys
|
|
import time
|
|
|
|
class sql_generator(object):
|
|
def __init__( self ):
|
|
self.project = None
|
|
self.subdir = None
|
|
self.ins_ism_asset = list()
|
|
self.csv_header = None
|
|
self.default_header = []
|
|
|
|
def initialize(self):
|
|
if self.project == None:
|
|
print >> sys.stderr, "No project given"
|
|
sys.exit(1)
|
|
|
|
def execute(self):
|
|
|
|
print '\nDo not forget to convert the files from CP850 to UTF-8!\n'
|
|
print 'for f in `ls -1 *csv`; do iconv -f cp850 -t utf-8 -o $f.out $f; mv $f.out $f; done'
|
|
print 'or'
|
|
print 'for f in `ls -1 *csv`; do iconv -f iso8859-1 -t utf-8 -o $f.out $f; mv $f.out $f; done\n\n'
|
|
print 'depending on the used excel version / export settings'
|
|
|
|
self.read_csv('ism_asset.csv', self.ism_asset);
|
|
|
|
|
|
f = open(self.project + '_ism_values_asset.sql', 'wt')
|
|
head = """-- # $Id$
|
|
-- # $HeadURL$
|
|
-- # $Author$"""
|
|
f.write(head)
|
|
warning = '\n\n--- This file is automatically generated by ism_asset_csv_to_sql.py from csv (xls) files - do not modify here directly!'
|
|
f.write(warning)
|
|
f.write('\nBEGIN;\n-- ism_assets\n')
|
|
f.write('\n'.join(str(e) for e in self.ins_ism_asset))
|
|
f.write('\nCOMMIT;\n-- END\n')
|
|
|
|
def read_csv(self, filename, func):
|
|
print 'parsing %s' % filename
|
|
reader = csv.reader(open(os.path.join('./', filename), 'rt'), delimiter = ';')
|
|
self.parseHeader(reader)
|
|
for a in reader:
|
|
func(a)
|
|
|
|
def parseHeader(self, reader):
|
|
self.translation_header = []
|
|
self.default_header = []
|
|
self.csv_header = next(reader, None)
|
|
for i in range(0, len(self.csv_header)):
|
|
h = self.csv_header[i]
|
|
if '[' in h and h.endswith(']'):
|
|
lang = h[h.index('[') + 1:-1]
|
|
if lang == self.default_language:
|
|
self.default_header.append(i)
|
|
else:
|
|
self.translation_header.append(i)
|
|
self.translation_lang.append(lang)
|
|
|
|
def toBool(self, val):
|
|
if val == '':
|
|
return 'false'
|
|
if val == 'true' or val == '1':
|
|
return 'true'
|
|
if val == 'false' or val == '0':
|
|
return 'false'
|
|
if bool(val):
|
|
return 'true'
|
|
return 'false'
|
|
|
|
def toInt(self, val):
|
|
if val == '':
|
|
return 'NULL'
|
|
return int(val, 0)
|
|
|
|
def toFloat(self, val):
|
|
val = val.replace(',', '.')
|
|
if val == '':
|
|
return 0
|
|
return float(val)
|
|
|
|
def toString(self, val):
|
|
if val == '':
|
|
return 'NULL'
|
|
return "'" + val + "'"
|
|
|
|
def toJson(self, val):
|
|
return self.toString(val) # for now
|
|
|
|
def ism_asset(self, line):
|
|
if len(line[0]) == 0:
|
|
return
|
|
# Bezeichnung;TKZ;ArtikelNr.;SerienNr;Menge
|
|
article_no = self.toInt(line[2])
|
|
serial_no = self.toInt(line[3])
|
|
lastdigits = str(serial_no)[-4:]
|
|
name = str(article_no) + "_" + str(serial_no)
|
|
|
|
#lastdigits.strip("0")
|
|
optical_code = "{ \"opticalCode\" : " + lastdigits.lstrip("0") + "}"
|
|
props = self.toJson(optical_code)
|
|
barcode = "OPS_" + str(article_no) + "0" + str(serial_no) + "_" + str(article_no) + "0" + str(serial_no) + "_0" + str(serial_no)
|
|
last_update = int(time.time()) * 1000
|
|
|
|
self.ins_ism_asset.append("INSERT INTO ism.ism_asset (name, mounting_point_type_id, barcode, asset_state_id, properties) VALUES (%s, 6, %s, 0, %s);" % (self.toString(name), self.toString(barcode), props))
|
|
|
|
|
|
def usage():
|
|
print "Usage: %s <Options>" % sys.argv[0]
|
|
print "usage: $0 -p <project>"
|
|
print " -p (--project) : guez / ahtc "
|
|
|
|
def main():
|
|
|
|
p = sql_generator()
|
|
|
|
try:
|
|
opts, args = getopt.getopt(sys.argv[1:], "p:", ["project"])
|
|
except getopt.GetoptError, err:
|
|
print >> sys.stderr, err
|
|
usage()
|
|
sys.exit(2)
|
|
for o, a in opts:
|
|
if o in ("-p", "--project"):
|
|
p.project = a
|
|
print >> sys.stderr, "PROJECT: %s" % a
|
|
|
|
p.initialize()
|
|
|
|
p.execute()
|
|
|
|
sys.exit(0)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|