-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_helpers.py
67 lines (59 loc) · 1.86 KB
/
config_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import sys
import os
import player
def initialize_game_environment(path):
'''
This function modify `sys.path` to import configuration file
from specified path.
Argument `path` may be a configuration file name or directory
where this file is located.
'''
path = os.path.abspath(path)
if os.path.isdir(path):
dirname = path
else:
dirname = os.path.dirname(path)
sys.path.insert(0, dirname)
def _return_player(string):
quotes = 0
current_string = ''
word_list = list()
valid_char = 1
for ch in string:
valid_char = 1
if (ch == '\'' or ch == '\"'):
quotes += 1
valid_char = 0
if (ch == ' ' and quotes == 0):
if (len(current_string) > 0):
word_list.append(current_string)
current_string = ''
valid_char = 0
if (quotes == 2):
word_list.append(current_string)
current_string = ''
quotes = 0
if (valid_char):
current_string += ch
if (len(current_string) > 1):
if (current_string[-1] == '\n' or current_string[-1] == '\0'):
current_string = current_string[0:-1]
word_list.append(current_string)
if (len(word_list) > 2):
#word_list[0] - author's name
#word_list[1] - bot's name
#word_list[2] - bot path
return player.Player(word_list[2], word_list[0], word_list[1])
def players_parse(path):
"""This function parses config file and returns list of Players"""
if (os.path.exists(path) == 0):
return list()
config_file = open(path, 'r')
lines = config_file.readlines()
config_file.close()
players_list = list()
for line in lines:
player = _return_player(line)
if (player is not None):
players_list.append(player)
return players_list