2025-07-19 15:26:32 +09:00

50 lines
1.0 KiB
Python

import io
import sqlite3
import sys
from . import db
from . import parser
class Context:
def __init__(self, dbcur: sqlite3.Cursor):
self.ostream = sys.stdout
self.dbcur = dbcur
self.dbcur.execute("BEGIN;")
def finalize(self):
self.dbcur.execute("ROLLBACK;")
self.close_output()
def permanentize(self):
self.dbcur.execute("COMMIT;")
self.dbcur.execute("BEGIN;")
def read_records(self, path):
def parse(st):
for tx in parser.parse(st):
db.apply(self.dbcur, tx)
if path == "-":
parse(sys.stdin)
else:
with open(path) as f:
parse(f)
def change_output(self, path):
self.close_output()
if path != "-":
self.ostream = open(path, "w")
def close_output(self):
if self.ostream is not sys.stdout:
self.ostream.close()
self.ostream = sys.stdout
def text(self, v):
print(v, file=self.ostream)
def sql(self, cmd: str):
self.dbcur.execute(cmd)
for row in self.dbcur:
print(*row, file=self.ostream, sep=",")