Commit 0c21dd61 authored by Martin Matějek's avatar Martin Matějek

Remove leftover file

parent ba114b24
#!/usr/bin/env python3
import argparse
import os
import yaml
"""
Simple tool to find and extract specific strings from yml plugin files,
which will be translated later together with strings from jinja templates.
"""
def generator_or_none(func):
"""Wrap a generator function, returning None if it's empty. """
def inner(*args, **kwargs):
# peek at the first item; return None if it doesn't exist
try:
next(func(*args, **kwargs))
except StopIteration:
return None
# return original generator otherwise first item will be missing
return func(*args, **kwargs)
return inner
def load_plugins(plugin_dir):
"""
Load content of plugins one by one on demand
Skip and continue on failure
"""
for file in os.listdir(plugin_dir):
if not file.startswith('.') and file.endswith('.yml'):
filepath = os.path.abspath(os.path.join(plugin_dir, file))
try:
with open(filepath, 'r') as f:
data = yaml.load(f)
yield (file, data)
except FileNotFoundError:
print("Failed to open plugin file '{}'".format(file))
except yaml.YAMLError:
print("Failed to deserialize yaml file '{}'".format(file))
@generator_or_none
def fetch_strings(data):
"""Fetch specific strings in yaml file for translation"""
if data and 'actions' in data:
for a in data['actions']:
yield (a['name'], a['title'])
def create_argparser():
"""Create new argument parser"""
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--plugins-dir", help="set directory to search for plugins", nargs="?", default="plugins")
parser.add_argument("-o", "--output", help="set POT file to output strings to", nargs="?", default="messages.pot")
return parser
def process_args(parser, args):
plugins = load_plugins(args.plugins_dir)
for filename, data in plugins:
print("Processing plugin '{}'".format(filename))
strings = fetch_strings(data)
if not strings:
print("No suitable strings found... Skiping {}".format(filename))
continue
try:
with open(args.output, 'a') as f:
for name, title in strings:
lines = '#: {}::{}\nmsgid "{}"\nmsgstr ""\n\n'.format(filename, name, title)
f.write(lines)
except OSError:
print("Failed to write to POT file!")
def main():
parser = create_argparser()
args = parser.parse_args()
process_args(parser, args)
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment