This commit is contained in:
yumoqing 2023-04-12 09:50:54 +08:00
parent 07c2848da3
commit c8318a276c
4 changed files with 96 additions and 9 deletions

View File

@ -1,6 +1,8 @@
from appPublic.jsonConfig import getConfig
import openpyxl as xlsx
import asyncio
from sqlor.dbpools import DBPools
from typeconv import convrec
class CBObject:
def __init__(self,db, name):
@ -9,10 +11,14 @@ class CBObject:
async def handle(self,ws):
db = DBPools()
meta = sor.I()
async with db.sqlorContext(self.db) as sor:
info = await sor.I(self.tbl)
for rec in getRecord(ws):
sor.C(self.tbl, rec)
r = [ v for v in rec.values() if v is not None ]
if len(r) == 0:
continue
rec = convrec(info, rec)
await sor.C(self.tbl, rec)
typesconv = {
"int":int,
@ -20,7 +26,7 @@ typesconv = {
"str":str,
}
async def getRecord(ws):
def getRecord(ws):
names = []
types = []
for i,r in enumerate(ws.rows):
@ -35,14 +41,16 @@ async def getRecord(ws):
else:
dic = {}
for j,c in enumerate(r):
tf = typesconv.get(types[j],None)
# tf = typesconv.get(types[j],None)
v = c.value
dic[names[j]] = v
yield rec
yield dic
async def excel2db(xlsxfile):
async def loaddatainexcel(xlsxfile):
wb = xlsx.load_workbook(xlsxfile)
dbname = [ i[2:-3] for i in wb.sheetnames if i.startswith('__')[0]
print(f'{wb.sheetnames=}')
dbname = [ i[2:-2] for i in wb.sheetnames if i.startswith('__')][0]
print(f'{dbname=}')
for name in wb.sheetnames:
if name.startswith('__'):
continue
@ -52,10 +60,13 @@ async def excel2db(xlsxfile):
if __name__ == '__main__':
import sys
config = getConfig()
import os
p = os.getcwd()
config = getConfig(p)
print(f'{config.databases=},cwd={p}')
DBPools(config.databases)
if len(sys.argv) < 2:
print('%s xlsxfile' % sys.argv[0])
sys.exit(1)
loop = asyncio.get_event_loop()
loop.run_until_complete(excel2db(sys.argv[1]))
loop.run_until_complete(loaddatainexcel(sys.argv[1]))

4
dataloader/macos/build.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
pyinstaller -y --clean dataloader.spec
echo "build success, please find the target in dist folder"

View File

@ -0,0 +1,47 @@
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['../dataloader.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[
"aiopg",
"aiomysql"
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='dataloader',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)

25
dataloader/typeconv.py Normal file
View File

@ -0,0 +1,25 @@
convfuncs = {
'int':int,
'long':int,
'llong':int,
'float':float,
'double':float,
'date':str,
'datetime':str,
'ddouble':float
}
def conv(info, name, value):
fields = info['fields']
for f in fields:
if f['name'] == name:
f = convfuncs.get(f['type'], None)
if f is None:
return value
return f(value)
return value
def convrec(info, ns):
ret = {k:conv(info, k, v) for k,v in ns.items()}
return ret