-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add generate election package script #5721
Merged
carolinemodic
merged 8 commits into
main
from
caro/add_generate_election_package_script
Dec 11, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
609f326
add generate-election-packages script with test when it needs regen
carolinemodic 8b571c6
make default latest metadata
carolinemodic 2c9f992
add version of electionPrimaryPrecinctSplits without ballot strings
carolinemodic 0a341e5
all new fixtures generated files
carolinemodic c8b1b8c
fixtures build:resources
carolinemodic e2cd2f0
export newly added fixtures for use in package generator test
carolinemodic 7226fb2
address pr comments
carolinemodic d4f7868
fix refactor
carolinemodic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
libs/backend/src/language_and_audio/election_package_string.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { electionPrimaryPrecinctSplitsFixtures } from '@votingworks/fixtures'; | ||
import { LanguageCode, BallotLanguageConfigs } from '@votingworks/types'; | ||
import { assert } from '@votingworks/basics'; | ||
import { getAllStringsForElectionPackage } from './election_package_strings'; | ||
import { GoogleCloudTranslator } from './translator'; | ||
import { MockGoogleCloudTranslationClient } from './test_utils'; | ||
|
||
const allBallotLanguages: BallotLanguageConfigs = [ | ||
{ | ||
languages: [ | ||
LanguageCode.ENGLISH, | ||
LanguageCode.CHINESE_SIMPLIFIED, | ||
LanguageCode.CHINESE_TRADITIONAL, | ||
LanguageCode.SPANISH, | ||
], | ||
}, | ||
]; | ||
|
||
describe('getAllStringsForElectionPackage', () => { | ||
it('should extract and translate election strings correctly for english only', async () => { | ||
const translationClient = new MockGoogleCloudTranslationClient(); | ||
const mockTranslator = new GoogleCloudTranslator({ translationClient }); | ||
const [appStrings, hmpbStrings, electionStrings] = | ||
await getAllStringsForElectionPackage( | ||
electionPrimaryPrecinctSplitsFixtures.election, | ||
mockTranslator, | ||
allBallotLanguages | ||
); | ||
|
||
expect(appStrings).toBeDefined(); | ||
expect(Object.keys(appStrings)).toEqual([ | ||
LanguageCode.ENGLISH, | ||
LanguageCode.CHINESE_SIMPLIFIED, | ||
LanguageCode.CHINESE_TRADITIONAL, | ||
LanguageCode.SPANISH, | ||
]); | ||
assert(appStrings[LanguageCode.ENGLISH]); | ||
expect(Object.keys(appStrings[LanguageCode.ENGLISH])).toHaveLength(427); | ||
|
||
expect(hmpbStrings).toBeDefined(); | ||
expect(Object.keys(hmpbStrings)).toEqual([ | ||
LanguageCode.ENGLISH, | ||
LanguageCode.CHINESE_SIMPLIFIED, | ||
LanguageCode.CHINESE_TRADITIONAL, | ||
LanguageCode.SPANISH, | ||
]); | ||
assert(hmpbStrings[LanguageCode.ENGLISH]); | ||
expect(Object.keys(hmpbStrings[LanguageCode.ENGLISH])).toHaveLength(30); | ||
|
||
expect(electionStrings).toBeDefined(); | ||
expect(Object.keys(electionStrings)).toEqual([ | ||
LanguageCode.ENGLISH, | ||
LanguageCode.CHINESE_SIMPLIFIED, | ||
LanguageCode.CHINESE_TRADITIONAL, | ||
LanguageCode.SPANISH, | ||
]); | ||
assert(electionStrings[LanguageCode.ENGLISH]); | ||
expect(Object.keys(electionStrings[LanguageCode.ENGLISH])).toHaveLength(14); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
libs/backend/src/language_and_audio/election_package_strings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
BallotLanguageConfigs, | ||
Election, | ||
UiStringsPackage, | ||
} from '@votingworks/types'; | ||
import { GoogleCloudTranslator } from './translator'; | ||
import { translateAppStrings } from './app_strings'; | ||
import { translateHmpbStrings } from './ballot_strings'; | ||
import { extractAndTranslateElectionStrings } from './election_strings'; | ||
|
||
/** | ||
* Helper function to generate all necessary strings used in an election package. | ||
* Returns three packages of strings: app strings, HMPB strings, and election strings. | ||
*/ | ||
export async function getAllStringsForElectionPackage( | ||
election: Election, | ||
translator: GoogleCloudTranslator, | ||
ballotLanguageConfigs: BallotLanguageConfigs | ||
): Promise<[UiStringsPackage, UiStringsPackage, UiStringsPackage]> { | ||
const appStrings = await translateAppStrings( | ||
translator, | ||
'latest', | ||
ballotLanguageConfigs | ||
); | ||
const hmpbStrings = await translateHmpbStrings( | ||
translator, | ||
ballotLanguageConfigs | ||
); | ||
const electionStrings = await extractAndTranslateElectionStrings( | ||
translator, | ||
election, | ||
ballotLanguageConfigs | ||
); | ||
|
||
return [appStrings, hmpbStrings, electionStrings]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,3 +77,33 @@ are optional and have default values. | |
```bash | ||
./bin/generate-election config.json > election.json | ||
``` | ||
|
||
## Election Package Generator | ||
|
||
A command-line tool for generating election packages and elections with grid | ||
layouts and translations. Follow the instructions to setup Google Cloud account | ||
authentication [here](/../backend/src/language_and_audio/README.md) before | ||
using. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this README section interrupts the Election Fixture Generator section. Also, it doesn't seem to include usage instructions for Election Package Generator |
||
```bash | ||
./bin/generate-election-package -e path/to/base-election-definition.json -o path/to/output-directory | ||
``` | ||
|
||
To generate an election.json and election-package file in the specified output | ||
directory with gridLayouts and all necessary strings from the base election | ||
provided. If --isMultiLanguage is specified then the strings will include | ||
translations for all languages. If --priorElectionPackage is specified that | ||
election package will be used as a cache for translations before querying google | ||
cloud. | ||
|
||
## Election Packages Generator | ||
|
||
Wrapper script to regenerate election packages for all configured fixtures. | ||
|
||
```bash | ||
pnpm generate-election-packages | ||
``` | ||
|
||
Run with FORCE_RETRANSLATE=1 to make new translations generate for all election | ||
packages. Note you will need to run pnpm build:resources && pnpm build in | ||
libs/fixtures after running this to register the new fixtures. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env node | ||
|
||
require('esbuild-runner').install({ | ||
type: 'transform', | ||
}); | ||
|
||
require('../src/cli/generate-election-package') | ||
.main(process.argv, { | ||
stdin: process.stdin, | ||
stdout: process.stdout, | ||
stderr: process.stderr, | ||
}) | ||
.catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Generates the saved election package fixtures in libs/fixtures | ||
# By default it will reuse the translations and audio from previous runs if new strings | ||
# are not detected to be translated. To force re-translation, set the environment variable | ||
# FORCE_RETRANSLATE to true. | ||
export NODE_ENV=development | ||
|
||
# Check if FORCE_RETRANSLATE is set to true | ||
if [ -z "$FORCE_RETRANSLATE" ]; then | ||
./bin/generate-election-package -e ../fixtures/data/electionPrimaryPrecinctSplits/electionBase.json -o ../fixtures/data/electionPrimaryPrecinctSplits/ -p ../fixtures/data/electionPrimaryPrecinctSplits/election-package-default-system-settings.zip --isMultiLanguage | ||
else | ||
./bin/generate-election-package -e ../fixtures/data/electionPrimaryPrecinctSplits/electionBase.json -o ../fixtures/data/electionPrimaryPrecinctSplits/ --isMultiLanguage | ||
fi | ||
|
||
echo | ||
echo "Note: You need to run \`pnpm build:resources && pnpm build\` in libs/fixtures for the new fixtures to register" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"clean": "pnpm --filter $npm_package_name... clean:self", | ||
"clean:self": "rm -rf build && tsc --build --clean tsconfig.build.json", | ||
"generate-cvr-fixtures": "./bin/generate-cvr-fixtures", | ||
"generate-election-packages": "./bin/regenerate-election-packages", | ||
"lint": "pnpm type-check && eslint .", | ||
"lint:fix": "pnpm type-check && eslint . --fix", | ||
"test": "is-ci test:ci test:watch", | ||
|
@@ -29,18 +30,22 @@ | |
"@votingworks/basics": "workspace:*", | ||
"@votingworks/fixtures": "workspace:*", | ||
"@votingworks/fs": "workspace:*", | ||
"@votingworks/hmpb": "workspace:*", | ||
"@votingworks/image-utils": "workspace:*", | ||
"@votingworks/types": "workspace:*", | ||
"@votingworks/utils": "workspace:*", | ||
"debug": "4.3.4", | ||
"esbuild": "0.21.2", | ||
"esbuild-runner": "2.2.2", | ||
"js-sha256": "^0.9.0", | ||
"jszip": "^3.9.1", | ||
"nanoid": "^3.3.7", | ||
"uuid": "9.0.1", | ||
"yargs": "17.7.1", | ||
"zod": "3.23.5" | ||
}, | ||
"devDependencies": { | ||
"@types/debug": "4.1.8", | ||
"@types/jest": "^29.5.3", | ||
"@types/node": "20.16.0", | ||
"@types/tmp": "0.2.4", | ||
|
@@ -55,5 +60,8 @@ | |
"tmp": "^0.2.1", | ||
"ts-jest": "29.1.1" | ||
}, | ||
"engines": { | ||
"node": ">= 12" | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: change
get
totranslate
to be more specific about what's happening