Add base.py, command_line.py and five other files

This commit is contained in:
Richard Bronkhorst 2023-06-07 21:48:28 +02:00
parent 5153a3fcdd
commit 20e63d2d0f
7 changed files with 151 additions and 0 deletions

13
base.py Normal file
View File

@ -0,0 +1,13 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
def get_session(fil):
fil = 'data/store.db'
engine = create_engine('sqlite:///' + fil)
Session = sessionmaker(bind=engine)
return engine, Session
Base = declarative_base()

76
command_line.py Normal file
View File

@ -0,0 +1,76 @@
import shlex
import inspect
import sys
import importlib
def func_supports_argcount(f, cnt):
argspec = inspect.getargspec(f)
posargs = 0 if argspec.args is None else len(argspec.args)
if argspec.args[0] == 'self':
posargs = posargs - 1
defargs = 0 if argspec.defaults is None else len(argspec.defaults)
varargs = 0 if argspec.varargs is None else len(argspec.varargs)
minargs = posargs - defargs
maxargs = posargs
if cnt < minargs:
return False
if cnt > maxargs:
return varargs > 0
return True
class CommandLine:
def __init__(self):
self.reloading = False
self.stopping = False
def stop(self):
self.stopping = True
def prompt(self):
return '> '
def handle_not_found(self, c, args):
print(f'command not found; {c}')
def handle_error(self, cmd, args, e):
print(e)
def handle_empty(self):
pass
def do_quit(self):
print('byebye!')
self.stopping = True
def do_reload(self):
self.reloading = True
def handle_cmd(self, c):
if c == '':
return self.handle_empty()
args = shlex.split(c)
cmd = args.pop(0)
str_handler = f'do_{cmd}'
if not hasattr(self, str_handler):
try:
self.handle_not_found(cmd, args)
return
except Exception as e:
self.handle_error(cmd, args, e)
handler = getattr(self, str_handler)
if not func_supports_argcount(handler, len(args)):
expect_args = ', '.join(inspect.getargspec(handler).args[1:])
print('expected args: ' + expect_args)
return
try:
handler(*args)
except Exception as e:
self.handle_error(cmd, args, e)
def run(self):
while not self.stopping and not self.reloading:
c = input(self.prompt())
self.handle_cmd(c)

13
models/sector.py Normal file
View File

@ -0,0 +1,13 @@
from sqlalchemy import Column, String, Integer, Date
from base import Base
class Sector(Base):
__tablename__ = 'sectors'
id = Column(Integer, primary_key=True)
symbol = Column(String)
magic = Column(Integer)
def __init__(self, symbol):
self.symbol = symbol

15
models/setting.py Normal file
View File

@ -0,0 +1,15 @@
from sqlalchemy import Column, String, Integer, Date
from base import Base
class Setting(Base):
__tablename__ = 'settings'
id = Column(Integer, primary_key=True)
name = Column(String)
value = Column(String)
def __init__(self, name, value):
self.name = name
self.value = value

13
models/system.py Normal file
View File

@ -0,0 +1,13 @@
from sqlalchemy import Column, String, Integer, Date
from base import Base
class System(Base):
__tablename__ = 'systems'
id = Column(Integer, primary_key=True)
symbol = Column(String)
def __init__(self, symbol):
self.symbol = symbol

13
models/waypoint.py Normal file
View File

@ -0,0 +1,13 @@
from sqlalchemy import Column, String, Integer, Date
from base import Base
class Waypoint(Base):
__tablename__ = 'waypoints'
id = Column(Integer, primary_key=True)
symbol = Column(String)
def __init__(self, symbol):
self.symbol = symbol

8
store.py Normal file
View File

@ -0,0 +1,8 @@
from base import get_session, Base
from models.sector import Sector
from models.system import System
class Store:
def __init__(self, fil):
self.engine, self.session = get_session(fil)
Base.metadata.create_all(self.engine)