# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- # # This file is part of the LibreOffice project. # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. #
import os from prompt_toolkit import prompt import xml.dom.minidom
#-------------------------- generating the xml functions --------------------------
def generate_menu_fragment(addon_name, merge_name, merge_point, merge_command, merge_fallback, merge_context, menu_items): return''.join(x for x in [ '',
f'',
f'',
f'{merge_point}'if merge_point else'',
f'{merge_command}'if merge_command else'',
f'{merge_fallback}'if merge_fallback else'',
f'{merge_context}'if merge_context else'', '',
menu_items, '', '', '', ''
] if x)
return''.join(x for x in [
f'',
f'{title}'if title else'',
f'{url}'if url else'',
f'{image_path}'if image_path else'',
f'{target}'if target else'',
submenu_fragment or'', ''
] if x)
def generate_submenu_item_fragment(submenu_name, title, url, image_path, target): return''.join(x for x in [
f'',
f'{title}'if title else'',
f'{url}'if url else'',
f'{image_path}'if image_path else'',
f'{target}'if target else'', ''
] if x)
def prompt_images():
print("\nPlease provide paths to the [ .bmp ] images (type 'skip' to skip providing an image):")
image_small = input("Path to small image: ").strip() if image_small.lower() == 'skip':
image_small = None else:
image_small = validate_image_path(image_small)
image_big = input("Path to big image: ").strip() if image_big.lower() == 'skip':
image_big = None else:
image_big = validate_image_path(image_big)
image_small_hc = input("Path to small high-contrast image: ").strip() if image_small_hc.lower() == 'skip':
image_small_hc = None else:
image_small_hc = validate_image_path(image_small_hc)
image_big_hc = input("Path to big high-contrast image: ").strip() if image_big_hc.lower() == 'skip':
image_big_hc = None else:
image_big_hc = validate_image_path(image_big_hc)
def prompt_menu_title(menu_type):
title = prompt(f"\nEnter {menu_type} item title (use '~' before the accelerator key, e.g., '~File'): ").strip() return title
def prompt_button_separator():
print("\nDo you want to add a button separator?")
print("1. Before the first button")
print("2. Between the buttons")
print("3. No separator")
choice = input("Enter your choice (1-3): ").strip() if choice == '1': return"before_first" elif choice == '2': return"between_buttons" elif choice == '3': return"none" else:
print("Invalid choice. Please select again.") return prompt_button_separator()
def prompt_image_path(merge_type): whileTrue:
image_path = prompt(f"\nEnter path to {merge_type} item image (optional, enter 'skip' to skip): ").strip().lower() if image_path == 'skip': returnNone
image_path = validate_image_path(image_path) if image_path: return image_path
print("Invalid file type. Please try again or enter 'skip' to skip.")
def validate_directory(directory_path): ifnot directory_path: returnNone# No output directory provided try: ifnot os.path.isdir(directory_path): raise ValueError("\nInvalid directory: Path is not a directory") # Additional validations can be added here if needed except ValueError as e:
print(f"Error: {e}") returnNone return directory_path
def validate_image_path(image_path): ifnot image_path: returnNone# No image path provided try: ifnot os.path.exists(image_path): raise ValueError("\nInvalid path: Path does not exist")
_, ext = os.path.splitext(image_path) if ext.lower() != '.bmp': raise ValueError("\nInvalid file type: Only .bmp files are allowed") # Additional validations can be added here if needed except ValueError as e:
print(f"Error: {e}") returnNone return image_path
#-------------------------- MAIN --------------------------
# Prompt for addon menu item
choice = prompt("\nDo you want to create the 'AddonMenu' item (Tools > Add-ons)? (y/n): ").strip().lower() if choice == 'y':
addon_menu_name = prompt("\nEnter addon menu item name (Tools > Add-ons): ").strip()
addon_menu_title = prompt_menu_title("addon menu")
addon_menu_url = prompt("\nEnter addon menu item URL: ").strip()
addon_menu_target = prompt("\nEnter addon menu item target (_self, _default, _top, _parent, or _blank): ").strip()
addon_menu_image_path = prompt("\nEnter path to addon menu item image (optional): ").strip()
addon_menu_image_path = validate_image_path(addon_menu_image_path)
addon_menu_fragment = generate_addon_menu_fragment(addon_menu_name, addon_menu_title, addon_menu_url, addon_menu_image_path, addon_menu_target)
# Prompt for help menu item
choice = prompt("\nDo you want to create the 'OfficeHelp' item (Help menu)? (y/n): ").strip().lower() if choice == 'y':
help_menu_name = prompt("\nEnter help menu item name (Help menu): ").strip()
help_menu_title = prompt_menu_title("help menu")
help_menu_url = prompt("\nEnter help menu item URL: ").strip()
help_menu_target = prompt("\nEnter help menu item target (_self, _default, _top, _parent, or _blank): ").strip()
help_menu_image_path = prompt("\nEnter path to help menu item image (optional): ").strip()
help_menu_image_path = validate_image_path(help_menu_image_path)
help_menu_fragment = generate_help_menu_fragment(help_menu_name, help_menu_title, help_menu_url, help_menu_image_path, help_menu_target)
# Prompt for images
choice = prompt("\nImages attribute defines the icons that appear next to the text in the menu and toolbar items.\nDo you want to provide images? (y/n): ").strip().lower() if choice == 'y':
image_small, image_big, image_small_hc, image_big_hc = prompt_images()
images_fragment = generate_images_fragment(image_small, image_big, image_small_hc, image_big_hc)
whileTrue: # Outer loop for gathering input for each merge
items = [] # Initialize items list for each merge type
merge_type = prompt("\n\nEnter merge type (menu or toolbar): ").strip()
while merge_type notin ['menu', 'toolbar']:
merge_type = prompt("\nInvalid input. \nEnter merge type (menu or toolbar): ").strip()
# Gather input for merge if merge_type == 'menu':
merge_name = prompt("\nEnter merge name (m name for menu): \nFor example:\nFor a menu merge operation, you could enter something like 'm1' or 'm_print_options': ").strip() else:
merge_name = prompt("\nEnter merge name (label[] for toolbar): \nFor a toolbar merge operation, you could enter something like [PrintButton] or [FormatToolbar]: ").strip()
whileTrue: # Inner loop for gathering input for items
item_name = prompt(f"\nEnter {merge_type} item name (or 'done' to finish): ").strip().lower() if item_name.lower() == 'done': break
title = prompt_menu_title(f"{merge_type} item")
url = prompt(f"\nEnter {merge_type} item URL (can be a macro path or dispatch command): ").strip()
target = prompt(f"\nEnter {merge_type} item target (_self, _default, _top, _parent, or _blank): ").strip()
image_path = prompt_image_path(merge_type)
# Prompt for submenus
submenus = []
add_submenu = prompt(f"\nAdd submenu for this {merge_type} item? (y/n): ").strip().lower() if add_submenu == 'y': whileTrue:
submenu_name = prompt("\nEnter submenu name (or 'done' to finish): ").strip().lower() if submenu_name.lower() == 'done': break
submenu_title = prompt_menu_title("submenu")
submenu_url = prompt("\nEnter submenu URL: ").strip()
submenu_target = prompt("\nEnter submenu target (_self, _default, _top, _parent, or _blank): ").strip()
submenu_image_path = prompt("\nEnter path to submenu image (optional): ").strip()
submenu_image_path = validate_image_path(submenu_image_path)
submenus.append(generate_submenu_item_fragment(submenu_name, submenu_title, submenu_url, submenu_image_path, submenu_target))
# Generate item fragment based on merge type if merge_type == 'menu':
items.append(generate_menu_item_fragment(item_name, title, url, image_path, target, submenus)) elif merge_type == 'toolbar':
merge_toolbar = prompt("\nEnter the Merge ToolBar name\n(specifies the appearance of the floating toolbar reached via View > Toolbars > Add-on) : ").strip()
separator_position = prompt_button_separator()
items.append(generate_toolbar_item_fragment(item_name, title, url, image_path, target, separator_position))
keep_merging = prompt("\n\n\nContinue to modify Menu or Toolbar? ( y or n ): ").strip().lower() if keep_merging == 'n': # Generate .xcu file
generate_xcu(merge_fragment, output_dir, addon_menu_fragment, help_menu_fragment, images_fragment) break
print("Thank you for using the Addons.xcu file generation. Have a Nice Day!")
if __name__ == "__main__":
main()
# vim: set shiftwidth=4 softtabstop=4 expandtab:
Messung V0.5
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.