Set User preferences.
(username,
pref_options: Annotated[Optional[List[str]],
typer.Argument()] = None,
auth_source: AuthType = AuthType.internal,
console: Optional[bool] = True,
json: Optional[bool] = False,
input_file: Optional[str] = None,
sqlite_path: Optional[str] = None,
)
| 681 | @app.command() |
| 682 | @update_sqlite_path |
| 683 | def set_prefs(username, |
| 684 | pref_options: Annotated[Optional[List[str]], |
| 685 | typer.Argument()] = None, |
| 686 | auth_source: AuthType = AuthType.internal, |
| 687 | console: Optional[bool] = True, |
| 688 | json: Optional[bool] = False, |
| 689 | input_file: Optional[str] = None, |
| 690 | sqlite_path: Optional[str] = None, |
| 691 | ): |
| 692 | """Set User preferences.""" |
| 693 | if not pref_options: |
| 694 | pref_options = [] |
| 695 | |
| 696 | if input_file: |
| 697 | from urllib.parse import unquote |
| 698 | # generate full path of file |
| 699 | try: |
| 700 | file_path = unquote(input_file) |
| 701 | except Exception as e: |
| 702 | print(str(e)) |
| 703 | return _handle_error(str(e), True) |
| 704 | import json as json_utility |
| 705 | try: |
| 706 | with open(file_path) as f: |
| 707 | data = json_utility.load(f) |
| 708 | except json_utility.decoder.JSONDecodeError as e: |
| 709 | return _handle_error(gettext("Error parsing input file %s: %s" |
| 710 | % (file_path, e)), True) |
| 711 | except Exception as e: |
| 712 | return _handle_error( |
| 713 | gettext("Error reading input file %s: [%d] %s" % |
| 714 | (file_path, e.errno, e.strerror)), True) |
| 715 | |
| 716 | pref_data = data['preferences'] |
| 717 | |
| 718 | for k, v in pref_data.items(): |
| 719 | pref_options.append(k + "=" + str(v)) |
| 720 | |
| 721 | user_id = ManagePreferences.get_user(username, auth_source) |
| 722 | table = Table(title="Updated Pref Details", box=box.ASCII) |
| 723 | table.add_column("Preference", style="green") |
| 724 | if not user_id: |
| 725 | print(USER_NOT_FOUND_STR) |
| 726 | return |
| 727 | |
| 728 | prefs = ManagePreferences.fetch_prefs(True) |
| 729 | app = create_app(config.APP_NAME + '-cli') |
| 730 | invalid_prefs = [] |
| 731 | invalid_value_prefs = [] |
| 732 | valid_prefs = [] |
| 733 | # Module preference objects are registered lazily via |
| 734 | # register_before_app_start() callbacks; pgAdmin4.py runs them at |
| 735 | # web-app startup, but the CLI never does. save_cli() now validates |
| 736 | # against the registered preference type, so we have to trigger |
| 737 | # registration ourselves. PGADMIN_RUNTIME must be set first because |
| 738 | # some register_preferences() methods (e.g. browser) check it; we |
| 739 | # use False so the full server-mode preference set (including |
| 740 | # keyboard shortcuts) is exposed to the CLI. run_before_app_start() |
nothing calls this directly
no test coverage detected