Last active
September 11, 2023 15:14
-
-
Save wavvs/dfbe5200113e3eb0cd9057b662f85b2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click | |
import re | |
import string | |
import sre_yield | |
# Usage examples: | |
# {name}\.{lname} | |
# [a-z]{lname} | |
# {lname}\.[a-z]{2} | |
# ... | |
@click.command() | |
@click.option('-f', '--format', type=str, default=None, help='String format') | |
@click.option('-ff', '--formats', type=click.File('r'), default=None, help='Strings formats (file)') | |
@click.option('-n', '--names', default=None, type=click.File('r')) | |
@click.option('-s', '--surnames', default=None, type=click.File('r')) | |
@click.option('-o', '--output', required=True, type=click.Path(exists=False), help='Output file') | |
def cli(format, formats, names, surnames, output): | |
formats_list = [] | |
if format is not None: | |
formats_list.append(format) | |
elif formats is not None: | |
formats_list.extend(formats.read().split('\n')) | |
else: | |
print('[!] Specify -f\\--format or -ff\\--formats') | |
return | |
for format in formats_list: | |
if '{name}' in format: | |
names_list = [re.escape(i) for i in names.read().split('\n') if i != ''] | |
names_regex = '(' + '|'.join(names_list) + ')' | |
format = format.replace('{name}', names_regex) | |
if '{lname}' in format: | |
surnames_list = [re.escape(i) for i in surnames.read().split('\n') if i != ''] | |
surnames_regex = '(' + '|'.join(surnames_list) + ')' | |
format = format.replace('{lname}', surnames_regex) | |
with open(output, 'w') as f: | |
for i in sre_yield.AllStrings(format): | |
f.write(i + '\n') | |
if __name__ == '__main__': | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment