Skip to content
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

feat: python builder #60

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
140 changes: 9 additions & 131 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,137 +1,15 @@
# mrz
# mrz, but for python

Parse MRZ (Machine Readable Zone) from identity documents.
This is a port of https://github.com/cheminfo/mrz but for python.

<h3 align="center">
### Installation

<a href="https://www.zakodium.com">
<img src="https://www.zakodium.com/brand/zakodium-logo-white.svg" width="50" alt="Zakodium logo" />
</a>

<p>
Maintained by <a href="https://www.zakodium.com">Zakodium</a>
</p>

[![NPM version][npm-image]][npm-url]
[![build status][ci-image]][ci-url]
[![npm download][download-image]][download-url]

</h3>

## Installation

`$ npm install mrz`

## Example

```js
const parse = require('mrz').parse;

const mrz = [
'I<UTOD23145890<1233<<<<<<<<<<<',
'7408122F1204159UTO<<<<<<<<<<<6',
'ERIKSSON<<ANNA<MARIA<<<<<<<<<<',
];

var result = parse(mrz);
console.log(result);
```bash
npm run build-py
```

## API

### `parse(mrz, [options])`

Parses the provided MRZ. The argument can be an array of lines or a single string
including line breaks. This function throws an error if the input is in an
unsupported format. It will never throw an error when there are invalid fields
in the MRZ. Instead, the `result.valid` value will be `false` and
details about the invalid fields can be found in `result.details`.

#### Options

##### `options.autocorrect`

If set to `true`, some ambiguous characters will be automatically corrected by the parser if the field is supposed to
only contain numeric or alphabetic characters.
For example, in a date field, the letter "O" will be converted to the number "0".

Information about autocorrected characters will be added to the result details.

Default: `false`.

#### Shape of the parse result

##### result.format

String identifying the format of the parsed MRZ. Supported formats are:

- TD1 (identity card with three MRZ lines)
- TD2 (identity card with two MRZ lines)
- TD3 (passport)
- SWISS_DRIVING_LICENSE
- FRENCH_NATIONAL_ID

##### result.valid

`true` if all fields are valid. `false` otherwise.
### Usage

##### result.fields

Object mapping field names to their respective value. The value is set to `null`
if it is invalid. The value may be different from the raw value. For example,
`result.fields.sex` will be "male" when the raw value was "M".

##### result.documentNumber

The document number, as can be found in the visual elements of the document, outside the MRZ. For some documents, it may
be composed of multiple parsed fields. It may also not include the MRZ field named `documentNumber`. If any of the used
fields is invalid, this field will be set to `null`.

##### result.details

Array of objects describing all parsed fields. Its structure is:

- label {string} - Full english term for the field.
- field {string|null} - Name of the field in `result.fields`. Null for some fields such as separators that don't contain a value.
- value {string} - Value of the field (if it's valid) or `null`.
- valid {boolean} - Whether the field is valid.
- ranges {Array} - Array of ranges that are necessary to compute this field.
Ranges are objects with `line`, `start`, `end` and `raw`.
- line {number} - Index of the line where the field's value is located.
- start {number} - Index of the start of the field's value in `line`.
- end {number} - Index of the end of the field's value in `line`.
- error {undefined|string} - Contains a message describing the error if the field is invalid.
- autocorrect {array} - Contains indices of characters that were autocorrected and their original value.

### `formats`

Static mapping of supported formats.

### `states`

Static mapping of state code to state name.

## Specifications

### TD1, TD2 and TD3

https://www.icao.int/publications/pages/publication.aspx?docnum=9303

### Swiss driving license

https://www.astra.admin.ch/dam/astra/fr/dokumente/dokumente-strassenverkehr/kreisschreiben/ch-fak.pdf.download.pdf/Le%20permis%20de%20conduire%20suisse%20format%20carte%20de%20cr%C3%A9dit%20(PCC).pdf

### French national id

https://fr.wikipedia.org/wiki/Carte_nationale_d%27identit%C3%A9_en_France#Codage_bande_%C3%A0_lecture_optique

## License

[MIT](./LICENSE)

[npm-image]: https://img.shields.io/npm/v/mrz.svg
[npm-url]: https://npmjs.org/package/mrz
[ci-image]: https://github.com/cheminfo/mrz/workflows/Node.js%20CI/badge.svg?branch=main
[ci-url]: https://github.com/cheminfo/mrz/actions?query=workflow%3A%22Node.js+CI%22
[download-image]: https://img.shields.io/npm/dm/mrz.svg
[download-url]: https://npmjs.org/package/mrz
```python
test.var.get('window').callprop('parse', ['I<UTOD23145890<1233<<<<<<<<<<<','7408122F1204159UTO<<<<<<<<<<<6','ERIKSSON<<ANNA<MARIA<<<<<<<<<<'])
```
17 changes: 15 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
module.exports = {
presets: ['@babel/preset-typescript'],
plugins: ['@babel/plugin-transform-modules-commonjs'],
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
targets: {
ie: '11',
},
},
],
],
plugins: [
'@babel/plugin-transform-modules-commonjs',
'@babel/plugin-transform-template-literals',
],
};
2 changes: 2 additions & 0 deletions builder/buildPython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import js2py
js2py.translate_file('dist/mrz.js', 'dist/mrz.py')
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lib-esm"
],
"scripts": {
"build-py": "npm run prepack && npm run build && pip3 install js2py && python3 builder/buildPython.py",
"build": "npm run buildStates && cheminfo-build",
"buildStates": "node builder/createStatesJs.js",
"check-types": "tsc --noEmit",
Expand Down Expand Up @@ -47,7 +48,11 @@
},
"homepage": "https://github.com/cheminfo/mrz#readme",
"devDependencies": {
"@babel/cli": "^7.24.8",
"@babel/core": "^7.25.2",
"@babel/plugin-transform-modules-commonjs": "^7.24.6",
"@babel/plugin-transform-template-literals": "^7.24.7",
"@babel/preset-env": "^7.25.2",
"@babel/preset-typescript": "^7.24.6",
"@types/jest": "^29.5.12",
"cheminfo-build": "^1.2.0",
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
import states from './generated/states';
import parse from './parse/parse';

export { formats, states, parse };
export * from './types';
(window as any).formats = formats;

Check warning on line 5 in src/index.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected any. Specify a different type
(window as any).states = states;

Check warning on line 6 in src/index.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected any. Specify a different type
(window as any).parse = parse;

Check warning on line 7 in src/index.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected any. Specify a different type
9 changes: 3 additions & 6 deletions src/parsers/frenchDrivingLicence/parseDocumentCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default function parseDocumentCode(source: string) {
throw new Error(`invalid document code: ${source}. must be 2 char length`);
}

const [first, second] = source;
const first = source[0];
const second = source[1];

validateFirstChar(first, source);
validateSecondChar(second, source);
Expand All @@ -19,12 +20,8 @@ export default function parseDocumentCode(source: string) {

function validateFirstChar(char: string, source: string) {
if (!knownPrefixCode.includes(char)) {
const formatter = new Intl.ListFormat('en-US', {
style: 'short',
type: 'disjunction',
});
throw new Error(
`invalid document code: ${source}. First character must be ${formatter.format(knownPrefixCode)}`,
`invalid document code: ${source}. First character must be ???`,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2021",
"target": "ES5",
"useUnknownInCatchVariables": false,
"skipLibCheck": true
},
Expand Down
25 changes: 25 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path');

module.exports = {
entry: './src/index.js', // Your entry file
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
globalObject: 'this', // Ensure global object reference
library: {
type: 'window', // Expose the library globally
},
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
],
},
mode: 'production',
};
Loading