cddl2py 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cli.ts DELETED
@@ -1,42 +0,0 @@
1
- import fs from 'node:fs/promises'
2
- import path from 'node:path'
3
- import yargs from 'yargs'
4
-
5
- import { parse } from 'cddl'
6
-
7
- import { transform } from './index.js'
8
- import { pkg } from './constants.js'
9
-
10
- export default async function cli (argv = process.argv.slice(2)) {
11
- const parser = yargs(argv)
12
- .usage(`${pkg.name}\n${pkg.description}\n\nUsage:\ncddl2py ./path/to/spec.cddl > ./path/to/types.py`)
13
- .epilog(`v${pkg.version}\nCopyright ${(new Date()).getFullYear()} ${pkg.author}`)
14
- .version(pkg.version)
15
- .option('p', {
16
- alias: 'pydantic',
17
- type: 'boolean',
18
- description: 'Generate Pydantic BaseModel classes instead of TypedDict',
19
- default: false
20
- })
21
- .help('help')
22
- .alias('h', 'help')
23
- .alias('v', 'version')
24
-
25
- const args = await parser.argv
26
-
27
- if (args._.length === 0) {
28
- parser.showHelp()
29
- return process.exit(0)
30
- }
31
-
32
- const absoluteFilePath = path.resolve(process.cwd(), args._[0] as string)
33
- const hasAccess = await fs.access(absoluteFilePath).then(() => true, () => false)
34
-
35
- if (!hasAccess) {
36
- console.error(`Couldn't find or access source CDDL file at "${absoluteFilePath}"`)
37
- return process.exit(1)
38
- }
39
-
40
- const ast = parse(absoluteFilePath)
41
- console.log(transform(ast, { pydantic: args.p as boolean }))
42
- }
package/src/constants.ts DELETED
@@ -1,32 +0,0 @@
1
- import fs from 'node:fs/promises'
2
- import url from 'node:url'
3
- import path from 'node:path'
4
-
5
- const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
6
- export const pkg = JSON.parse(await fs.readFile(path.join(__dirname, '..', 'package.json'), 'utf-8'))
7
-
8
- export const GENERATED_FILE_COMMENT = `# Auto-generated by cddl2py v${pkg.version}\n# Do not edit manually.\n`
9
-
10
- export const NATIVE_TYPE_MAP: Record<string, string> = {
11
- any: 'Any',
12
- number: 'Union[int, float]',
13
- int: 'int',
14
- uint: 'int',
15
- nint: 'int',
16
- float: 'float',
17
- float16: 'float',
18
- float32: 'float',
19
- float64: 'float',
20
- bool: 'bool',
21
- bstr: 'bytes',
22
- bytes: 'bytes',
23
- tstr: 'str',
24
- text: 'str',
25
- str: 'str',
26
- nil: 'None',
27
- null: 'None',
28
- }
29
-
30
- export const PYDANTIC_NATIVE_TYPE_MAP: Record<string, string> = {
31
- ...NATIVE_TYPE_MAP,
32
- }