-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: add type annotations and clean up code #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,10 @@ import * as readline from "node:readline/promises"; | |
| import { stdin as input } from "node:process"; | ||
| import type { Sequelize } from "sequelize"; | ||
| import { parse } from "atsds-bnf"; | ||
| import { Fact, Idea, initializeDatabase, insertOrIgnore } from "./orm.ts"; | ||
| import { Fact, Idea, insertOrIgnore } from "./orm.ts"; | ||
| import { strRuleGetStrIdea } from "./utility.ts"; | ||
|
|
||
| export async function main(sequelize: Sequelize) { | ||
| export async function main(sequelize: Sequelize): Promise<void> { | ||
| const rl = readline.createInterface({ | ||
| input, | ||
| terminal: false, | ||
|
|
@@ -19,10 +19,9 @@ export async function main(sequelize: Sequelize) { | |
|
|
||
| try { | ||
| const ds = parse(data); | ||
| const dsStr = ds.toString(); | ||
|
|
||
| await insertOrIgnore(Fact, dsStr); | ||
| const idea = strRuleGetStrIdea(dsStr); | ||
| await insertOrIgnore(Fact, ds); | ||
| const idea = strRuleGetStrIdea(ds); | ||
| if (idea) { | ||
| await insertOrIgnore(Idea, idea); | ||
|
Comment on lines
21
to
26
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import asyncio | ||
| import tempfile | ||
| import pathlib | ||
| from typing import Annotated, Optional | ||
| from typing import Annotated, Optional, Awaitable | ||
| import tyro | ||
| from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession | ||
| from .orm import initialize_database | ||
| from .search import main as search | ||
| from .egg import main as egg | ||
|
|
@@ -11,7 +12,7 @@ | |
| from .load import main as load | ||
| from .dump import main as dump | ||
|
|
||
| component_map = { | ||
| component_map: dict[str, callable[[async_sessionmaker[AsyncSession]], Awaitable[None]]] = { | ||
| "search": search, | ||
| "egg": egg, | ||
| "input": input, | ||
|
Comment on lines
+15
to
18
|
||
|
|
@@ -29,7 +30,7 @@ async def run(addr: str, components: list[str]) -> None: | |
| coroutines = [component_map[component](session) for component in components] | ||
| except KeyError as e: | ||
| print(f"error: unsupported component: {str(e)}") | ||
| raise asyncio.CancelledError() | ||
| return | ||
|
|
||
| await asyncio.wait( | ||
| [asyncio.create_task(coro) for coro in coroutines], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ import { main as load } from "./load.ts"; | |
| import { main as output } from "./output.ts"; | ||
| import { initializeDatabase } from "./orm.ts"; | ||
|
|
||
| type ComponentMain = (addr: string, sequelize: Sequelize) => Promise<void>; | ||
| type ComponentMain = (sequelize: Sequelize) => Promise<void>; | ||
|
|
||
| const componentMap: Record<string, ComponentMain> = { | ||
| search, | ||
|
|
@@ -22,27 +22,27 @@ const componentMap: Record<string, ComponentMain> = { | |
| dump, | ||
| }; | ||
|
|
||
| async function run(addr: string, components: string[]) { | ||
| async function run(addr: string, components: string[]): Promise<void> { | ||
| const sequelize = await initializeDatabase(addr); | ||
|
|
||
| for (const name of components) { | ||
| if (!(name in componentMap)) { | ||
| console.error(`error: unsupported component: ${name}`); | ||
| return; | ||
| } | ||
| } | ||
|
Comment on lines
+25
to
33
|
||
|
|
||
| const sequelize = await initializeDatabase(addr); | ||
|
|
||
| const promises = components.map((name) => { | ||
| const component = componentMap[name]!; | ||
| return component(addr, sequelize); | ||
| return component(sequelize); | ||
| }); | ||
|
|
||
| await Promise.race(promises); | ||
|
|
||
| await sequelize.close(); | ||
| } | ||
|
|
||
| export function cli() { | ||
| export async function cli(): Promise<void> { | ||
| const program = new Command(); | ||
|
|
||
| program | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as in
load.ts:insertOrIgnoreandstrRuleGetStrIdeaboth takestring, but this code passes theparse()result directly. Convert the parsed value to a string (e.g.,ds.toString()) before inserting/extracting the idea.