2013-11-12 07:06:06 +01:00
import json
import os
2013-09-04 18:30:04 +08:00
2014-02-14 16:36:57 +13:00
from util import hook , text , textgen
2012-10-11 17:47:27 +13:00
GEN_DIR = " ./plugins/data/name_files/ "
2012-10-12 09:35:05 +13:00
2012-10-11 17:47:27 +13:00
def get_generator ( _json ) :
data = json . loads ( _json )
2013-09-19 10:42:44 +12:00
return textgen . TextGenerator ( data [ " templates " ] ,
2014-02-13 14:34:50 +13:00
data [ " parts " ] , default_templates = data [ " default_templates " ] )
2011-11-22 14:36:12 +13:00
2012-04-02 09:17:55 -07:00
2012-04-23 21:38:21 +12:00
@hook.command ( autohelp = False )
def namegen ( inp , notice = None ) :
2014-02-13 14:34:50 +13:00
""" namegen [generator] -- Generates some names using the chosen generator.
' namegen list ' will display a list of all generators . """
2011-11-22 14:36:12 +13:00
2012-10-12 09:35:05 +13:00
# clean up the input
inp = inp . strip ( ) . lower ( )
2012-10-11 19:45:49 +13:00
2012-10-11 17:47:27 +13:00
# get a list of available name generators
files = os . listdir ( GEN_DIR )
all_modules = [ ]
for i in files :
2012-10-12 09:35:05 +13:00
if os . path . splitext ( i ) [ 1 ] == " .json " :
all_modules . append ( os . path . splitext ( i ) [ 0 ] )
all_modules . sort ( )
2011-11-22 14:36:12 +13:00
2012-10-11 17:47:27 +13:00
# command to return a list of all available generators
2012-10-12 09:35:05 +13:00
if inp == " list " :
2012-10-11 17:47:27 +13:00
message = " Available generators: "
2013-09-12 20:47:39 +12:00
message + = text . get_text_list ( all_modules , ' and ' )
2012-02-24 11:40:26 +13:00
notice ( message )
2011-11-22 14:45:30 +13:00
return
2012-02-03 02:05:11 +13:00
2012-04-23 21:38:21 +12:00
if inp :
2012-10-12 09:35:05 +13:00
selected_module = inp . split ( ) [ 0 ]
2012-04-23 21:38:21 +12:00
else :
# make some generic fantasy names
2012-10-12 09:35:05 +13:00
selected_module = " fantasy "
2012-02-24 11:40:26 +13:00
2012-10-11 17:47:27 +13:00
# check if the selected module is valid
if not selected_module in all_modules :
return " Invalid name generator :( "
2011-11-22 14:36:12 +13:00
2012-10-11 17:47:27 +13:00
# load the name generator
with open ( os . path . join ( GEN_DIR , " {} .json " . format ( selected_module ) ) ) as f :
2012-10-12 09:35:05 +13:00
try :
generator = get_generator ( f . read ( ) )
except ValueError as error :
return " Unable to read name file: {} " . format ( error )
2011-11-22 14:36:12 +13:00
2012-10-11 17:47:27 +13:00
# time to generate some names
2013-09-12 20:47:39 +12:00
name_list = generator . generate_strings ( 10 )
2012-02-24 11:40:26 +13:00
2012-04-23 21:38:21 +12:00
# and finally return the final message :D
2013-09-12 20:47:39 +12:00
return " Some names to ponder: {} . " . format ( text . get_text_list ( name_list , ' and ' ) )