Skip to content

Instantly share code, notes, and snippets.

@thingsiplay
Created March 2, 2021 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thingsiplay/203bc057b64850b1e8c6868c0c78b6bf to your computer and use it in GitHub Desktop.
Save thingsiplay/203bc057b64850b1e8c6868c0c78b6bf to your computer and use it in GitHub Desktop.
Sync and manage your templates to a directory
#!/usr/bin/python3
# Dynamically create desktop entries in templates folder by reading all files
# from a source directory. It will add and overwrite desktop-files in
# templates folder and remove them, if no matching template from source
# directory is found.
import sys
import pathlib
import textwrap
# USER SETTINGS
# The source directory to save your template files. These files are the ones
# you end up copying when creating new file from template.
# Example: '~/My Templates'
USER_TEMPLATE_DIR = ''
# The folder where KDE 5 Plasma will save the template settings files, which
# are required to generate the menu "create new..." entry in Dolphin.
# Example: '~/.local/share/templates'
KDE_DESKTOP_DIR = '~/.local/share/templates'
# END OF USER SETTINGS
def get_new_desktop_content(template_file, template_name=None):
if not template_name:
template_name = template_file.stem
new_desktop_content = (
f'''
[Desktop Entry]
Name={template_name}...
Comment=Filename fore new {template_name} template
Type=Link
URL=file:{template_file}'''
).lstrip('\n')
return textwrap.dedent(new_desktop_content)
def get_new_desktop_path(template_folder, template_file, template_name=None):
if not template_name:
template_name = template_file.name
new_desktop_path = template_folder / f'New-{template_name}.desktop'
return new_desktop_path
def get_source_file_by_desktop_file(source_dir, desktop_file):
filename = desktop_file.name
filename = filename.lstrip('New-')
filename = filename.replace('.desktop', '')
return source_dir / filename
if USER_TEMPLATE_DIR == '' or KDE_DESKTOP_DIR == '':
print('Template folder not specified. Edit the scripts top part.'
' Be careful, files will be overwritten without asking.')
sys.exit(1)
user_source_folder = pathlib.Path(USER_TEMPLATE_DIR).expanduser()
kde_output_folder = pathlib.Path(KDE_DESKTOP_DIR).expanduser()
if not user_source_folder.exists():
user_source_folder.mkdir(parents=True, exist_ok=True)
if not kde_output_folder.exists():
kde_output_folder.mkdir(parents=True, exist_ok=True)
print('User Template Source:', user_source_folder)
print('KDE Settings Output:', kde_output_folder)
added_files = []
for path in user_source_folder.glob('*'):
if not path.name.startswith('.') and path.is_file():
new_desktop_content = get_new_desktop_content(path)
new_desktop_path = get_new_desktop_path(kde_output_folder, path)
if new_desktop_path.exists():
exist = 'updated'
else:
exist = 'new'
new_desktop_path.write_text(new_desktop_content)
added_files.append(new_desktop_path)
print(f'{exist} "{new_desktop_path}" mapped to "{path}"')
for path in kde_output_folder.glob('*.desktop'):
source_path = get_source_file_by_desktop_file(user_source_folder, path)
if not source_path in added_files and not source_path.exists():
path.rename(path.as_posix() + '.bkp')
print(f'removed "{path}" because could not find "{source_path}"')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment