commit
e03cce48f0
|
|
@ -0,0 +1,54 @@
|
|||
from abc import ABCMeta
|
||||
from openpyxl import Workbook, load_workbook
|
||||
|
||||
|
||||
class ExcelDB:
|
||||
|
||||
def __init__(self, path, password=None, offset_row=0, offset_column=0):
|
||||
self._path = path
|
||||
self._pass = password
|
||||
self._offset_row = offset_row
|
||||
self._offset_column = offset_column
|
||||
|
||||
def init(self):
|
||||
pass
|
||||
|
||||
def save(self, model):
|
||||
wb = load_workbook(self._path)
|
||||
ws = wb[model.Meta.table]
|
||||
ws["A1"] = model.username
|
||||
wb.save(self._path)
|
||||
|
||||
def create(self, model):
|
||||
wb = load_workbook(self._path)
|
||||
ws = wb[model.Meta.table]
|
||||
next_row = len(tuple(ws.rows)) + 1
|
||||
next_column = 1 + self._offset_column
|
||||
for e in model.__dict__:
|
||||
if e in ['__module__', 'Meta', '__doc__']:
|
||||
continue
|
||||
c = ws.cell(next_row, next_column)
|
||||
c.value = model.__dict__.get(e)
|
||||
next_column = next_column + 1
|
||||
wb.save(self._path)
|
||||
|
||||
def read(self, model):
|
||||
wb = load_workbook(self._path)
|
||||
ws = wb[model.Meta.table]
|
||||
|
||||
|
||||
def create_tables(self, tables):
|
||||
wb = Workbook()
|
||||
wb.remove_sheet(wb.active)
|
||||
for t in tables:
|
||||
wb.create_sheet(t.Meta.table)
|
||||
ws = wb[t.Meta.table]
|
||||
i = 1
|
||||
for e in t.__dict__:
|
||||
if e in ['__init__', '__module__', 'Meta', '__doc__']:
|
||||
continue
|
||||
c = ws.cell(1, i)
|
||||
c.value = t.__dict__.get(e).caption
|
||||
i = i + 1
|
||||
wb.save(self._path)
|
||||
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
from models import TestModel, Test1Model
|
||||
from exceldb import ExcelDB
|
||||
|
||||
|
||||
edb = ExcelDB("./testdata/data.xlsx")
|
||||
# edb.create_tables([TestModel, Test1Model])
|
||||
|
||||
TestModel.Meta.database = edb
|
||||
test1 = TestModel(username='Cool Hacker', datum='2023-11-12')
|
||||
test1.create()
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
from abc import ABCMeta, abstractclassmethod
|
||||
|
||||
|
||||
class BaseModel:
|
||||
def _init(self, model, **params):
|
||||
for k, v in params.items():
|
||||
pp = model.__dict__.get(k)
|
||||
if pp is not None:
|
||||
self.__dict__[k] = v
|
||||
|
||||
def __init__(self, **params):
|
||||
self._row = None
|
||||
self._init(type(self), **params)
|
||||
|
||||
_column = None
|
||||
|
||||
@classmethod
|
||||
def get_column(cls):
|
||||
return cls._column
|
||||
|
||||
@classmethod
|
||||
def get(cls):
|
||||
return "GET"
|
||||
|
||||
@classmethod
|
||||
def get_by_id(cls, id):
|
||||
return "GET " + str(id)
|
||||
|
||||
def save(self):
|
||||
self.Meta.database.save(self)
|
||||
|
||||
def create(self):
|
||||
self.Meta.database.create(self)
|
||||
|
||||
class Meta:
|
||||
database = None
|
||||
|
||||
|
||||
class Field:
|
||||
def __init__(self, caption, column):
|
||||
self._caption = caption
|
||||
self._column = column
|
||||
|
||||
@property
|
||||
def caption(self):
|
||||
return self._caption
|
||||
|
||||
@property
|
||||
def column(self):
|
||||
return self._column
|
||||
|
||||
def __repr__(self):
|
||||
return self._caption
|
||||
|
||||
def __str__(self):
|
||||
return self._caption
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
from model import BaseModel, Field
|
||||
|
||||
|
||||
class TestModel(BaseModel):
|
||||
|
||||
username = Field("Username", 1)
|
||||
nickname = Field("anoter name", 2)
|
||||
datum = Field('datum', 3)
|
||||
creater = Field('creater', 4)
|
||||
test = Field('test', 5)
|
||||
|
||||
|
||||
|
||||
class Meta:
|
||||
table = "Users"
|
||||
|
||||
|
||||
class Test1Model(BaseModel):
|
||||
username = Field('NoName', 1)
|
||||
nickname = Field("anoter name", 2)
|
||||
datum = Field('datum', 3)
|
||||
creater = Field('creater', 4)
|
||||
test = Field('test', 5)
|
||||
|
||||
class Meta:
|
||||
table = 'NoNames'
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
et-xmlfile==1.1.0
|
||||
openpyxl==3.1.2
|
||||
setuptools==69.0.2
|
||||
wheel==0.42.0
|
||||
Binary file not shown.
Loading…
Reference in New Issue