[Tutor] path

ingo ingo at ingoogni.nl
Sun Jun 30 02:01:15 EDT 2019


On 29-6-2019 15:42, Mats Wichmann wrote:
> 
> Most people don't use pathlib, and that's kind of sad, since it tries to
> mitigate the kinds of questions you just asked. Kudos for trying.

In the end, it works,

Ingo

---%<------%<------%<---
# set up some default directories and files
# for starting a new project with SQLite
# using Sublime + SQLTools.
#
# /---fullpath
#     |
#     /--- data
#     /--- db
#     |    +--- basename.db3
#     |    +--- basename.read
#     /--- ddl
#     /--- doc
#     /--- sql
#     +--- basename.sublime-project
#     +--- _FOSSIL_

import pathlib
import sys

# the last bit of the full path is used as the name for the database
# c:\newdatabase\test will create the databse
# c:\newdatabase\test\db\test.db3
print('enter full path for new db:')
fp = pathlib.Path(input())
fn = fp.name  # = os.path.basename()

dirs = {}
for sub in ['', 'data', 'db', 'ddl', 'doc', 'sql']:
    dirs[sub] = fp / sub
    try:
        dirs[sub].mkdir(parents=False, exist_ok=False)
    except FileExistsError:
        print(f'Directory already exists: {dirs[sub]}')
        sys.exit(1)

fdb = dirs['db'] / (fn+'.db3')
fdb.touch()

fr = dirs['db'] / (fn+'.read')
fr.write_text(f"""
-- template to build db from tables etc
-- using dot commands

PRAGMA foreign_keys = OFF;
--DROP TABLE IF EXISTS sometable;

BEGIN TRANSACTION;
--.read {(dirs['ddl'] / 'someddl.ddl').as_posix()}
COMMIT;

PRAGMA temp_store   = 2;
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;

BEGIN TRANSACTION;
--.read {(dirs['sql'] / 'somequery.sql').as_posix()}
COMMIT;
"""
)

fsub = dirs[''] / (fn+'.sublime-project')
fsub.write_text(f'''
{{
  "folders":[
    {{
      "path": "."
    }}
  ],
  "connections":{{
    "Connection SQLite":{{
      "database": "{fdb.as_posix()}",
      "encoding": "utf-8",
      "type": "sqlite"
    }}
  }},
  "default": "Connection SQLite"
}}'''
)

# TODO set up fossil in the fp dir




More information about the Tutor mailing list