#!/usr/bin/env python3

import sys
from unidata import *


def usage():
	print(f'{sys.argv[0]} [SORTABLE]')
	print('')
	print('[SORTABLE] - file with a list of acceptable codepoints')


def pass_codepoint(codepoint: str, acceptable: list[str]) -> bool:
	return codepoint in acceptable


def load_acceptable(filename: str) -> list[str]:
	return [codepoint.strip() for codepoint in open(filename)]


def format_weight(weight: str) -> str:
	weights = [w.strip('[]*.') for w in weight.split('][')]
	return '.'.join(weights)


if __name__ == '__main__':
	if len(sys.argv) < 2:
		usage()
		sys.exit(1)

	sortable = sys.argv[1]
	acceptable = load_acceptable(sortable)

	for line in sys.stdin:
		assert isinstance(line, str)

		if unidata_comment(line):
			continue

		tokens = line.split(';')
		codepoints = tokens[0].strip().split(' ')
		weight = tokens[1].strip().strip(';').split('#')[0].strip()
		codepoint = codepoints[0]  # if it's contraction - check first codepoint

		if not pass_codepoint(codepoint, acceptable):
			continue

		sink = sys.stderr if len(codepoints) > 1 else sys.stdout
		print(' '.join(codepoints), format_weight(weight), file=sink)
