<commit_before>import os
import sqlite3
from contextlib import contextmanager
from cashio.common import Transaction

log = None

schema = [
"""
CREATE TABLE  transactions (
    rawdata   CHAR(200) PRIMARY KEY,
    owner     CHAR(20) NOT NULL,
    date      DATE NOT NULL,
    amount    NUMBER NOT NULL,
    target    CHAR(50) NOT NULL,
    recipient INTEGER NULL
)""",
"""
CREATE TABLE  recipients (
    name      CHAR(50) PRIMARY KEY,
    regexp    BOOLEAN,
    category  CHAR(50) NOT NULL
)"""]


@contextmanager
def get_cursor(autocommit=False):
    path_db = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', 'db', 'cashio.db')
    with sqlite3.connect(path_db) as conn:
        yield conn.cursor()
        if autocommit:
            conn.commit()

def create_database(logger):
    """Create cashio database, with initial sql schema"""
    global log
    log = logger

    with get_cursor(True) as c:<commit_msg>Create db/ dir if missing<commit_after>import os
import sqlite3
from contextlib import contextmanager
from cashio.common import Transaction

log = None

db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', 'db')
db_path = os.path.join(db_dir, 'cashio.db')

schema = [
"""
CREATE TABLE  transactions (
    rawdata   CHAR(200) PRIMARY KEY,
    owner     CHAR(20) NOT NULL,
    date      DATE NOT NULL,
    amount    NUMBER NOT NULL,
    target    CHAR(50) NOT NULL,
    recipient INTEGER NULL
)""",
"""
CREATE TABLE  recipients (
    name      CHAR(50) PRIMARY KEY,
    regexp    BOOLEAN,
    category  CHAR(50) NOT NULL
)"""]


@contextmanager
def get_cursor(autocommit=False):
    with sqlite3.connect(db_path) as conn:
        yield conn.cursor()
        if autocommit:
            conn.commit()

def create_database(logger):
    """Create cashio database, with initial sql schema"""
    global log
    log = logger

    if not os.path.exists(db_dir):
        os.mkdir(db_dir)

    with get_cursor(True) as c:
{'fraction_non_alphanumeric': 0.1105476673427992, 'fraction_numerical': 0.013184584178498986, 'mean_word_length': 2.946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4464264', 'n_tokens_mistral': 671, 'n_tokens_neox': 628, 'n_words': 183}