Skip to content

Modify the database, generate new files, and an error occurs #65

@MYAN1998

Description

@MYAN1998

Hello,thank you for your contribution.Below is the code I wrote using your library.

from pya2l import DB
import pya2l.model as model
import time
import re

# 实例化数据库对象
db = DB()

# 根据A2L创建对应的数据库,执行之后,会在目录下创建A2L对应的数据库
try:
    # A2L_11.a2l 替换成对应的A2L文件命
    session = db.import_a2l("A2L_11.a2l")
    time.sleep(5)
except Exception:
    pass

# 打开已存在的数据库
session_db = db.open_existing("A2L_11")

# 选取数据库所有测量的量
measurements = session_db.query(model.Measurement).order_by(model.Measurement.name).all()
for m in measurements:
    # 匹配条件"XXXXs数字s数字_或XXXXu数字s数字_的变量"
    # .*s\d+s\d+_:表示任意字符(.*)后跟 s,再后跟一个或多个数字,接着是 s,然后是一个或多个数字,最后是下划线。
    # .*u\d+s\d+_:表示任意字符(.*)后跟 u,再后跟一个或多个数字,接着是 s,然后是一个或多个数字,最后是下划线。
    pattern = re.compile(r'(s\d+s(\d+)_.*|u\d+s(\d+)_.*)')
    match = pattern.search(m.name)
    if match:
        if match.group(2):
            num = match.group(2)
            m.conversion = "BitConver" + str(num)
        elif match.group(3):
            num = match.group(3)
            m.conversion = "BitConver" + str(num)
session_db.commit()
session_db.close()
out_file = "new.a2l"
db.export_a2l("A2L_11",out_file,"utf-8")

When running the last line of code, an "database is locked" error occurs.I modified your source code and temporarily solved the problem.
I modified the function named export_a2l in the init.py file.

def export_a2l(session, output: typing.Union[TextIOWrapper, str, typing.Any] = sys.stdout, encoding="latin1"):  # noqa: UP007
    """
    Parameters
    ----------
    file_name: str
        Name of the A2L exported.

    encoding: str
        File encoding like "latin-1" or "utf-8".
    """
    # session = open_existing(db_name)
    namespace = dict(session=session, model=model)
    data = doTemplateFromText(A2L_TEMPLATE, namespace, formatExceptions=False, encoding=encoding)
    if isinstance(output, TextIOWrapper):
        output(data)
    else:
        with open(file=output, mode="w", encoding=encoding) as outf:
            outf.write(data)

It should be an error caused by the sentence "open_existing(db_name)".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions