mirror of
https://github.com/SpiceBot/SpiceBot.git
synced 2026-11-10 21:50:59 -05:00
begin usage of read class
This commit is contained in:
parent
9d1339f31a
commit
0fe8f7dbf3
@ -12,7 +12,7 @@ from sopel.config.types import StaticSection, ListAttribute
|
||||
import sopel_modules
|
||||
|
||||
from .Config import config as botconfig
|
||||
from .Tools import read_directory_json_to_dict
|
||||
from .Read import read as botread
|
||||
from .Commands import commands as botcommands
|
||||
|
||||
import spicemanip
|
||||
@ -50,7 +50,7 @@ class BotJSONCommands():
|
||||
for extragifcfgdir in botconfig.SpiceBot_DictComs.extra:
|
||||
dir_to_scan.append(extragifcfgdir)
|
||||
|
||||
valid_dictcom_dict = read_directory_json_to_dict(dir_to_scan, "Dictionary Commands", "SpiceBot_DictComs")
|
||||
valid_dictcom_dict = botread.json_to_dict(dir_to_scan, "Dictionary Commands", "SpiceBot_DictComs")
|
||||
|
||||
for dict_from_file in list(valid_dictcom_dict.keys()):
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ from sopel.config.types import StaticSection, ValidatedAttribute, ListAttribute
|
||||
import sopel_modules
|
||||
|
||||
from .Config import config as botconfig
|
||||
from .Tools import read_directory_json_to_dict
|
||||
from .Read import read as botread
|
||||
from .Commands import commands as botcommands
|
||||
|
||||
import spicemanip
|
||||
@ -46,7 +46,7 @@ class BotGif():
|
||||
for extragifcfgdir in botconfig.SpiceBot_Gif.extra:
|
||||
dir_to_scan.append(extragifcfgdir)
|
||||
|
||||
valid_gif_api_dict = read_directory_json_to_dict(dir_to_scan, "Gif API", "SpiceBot_Gif")
|
||||
valid_gif_api_dict = botread.json_to_dict(dir_to_scan, "Gif API", "SpiceBot_Gif")
|
||||
|
||||
for gif_api in list(valid_gif_api_dict.keys()):
|
||||
botconfig.define_section(gif_api, GifAPISection, validate=False)
|
||||
|
||||
@ -4,6 +4,11 @@ from __future__ import unicode_literals, absolute_import, division, print_functi
|
||||
|
||||
import sopel_modules
|
||||
|
||||
import os
|
||||
import codecs
|
||||
|
||||
from .Logs import logs
|
||||
|
||||
|
||||
class BotRead():
|
||||
|
||||
@ -13,5 +18,59 @@ class BotRead():
|
||||
def config_dir(self):
|
||||
return
|
||||
|
||||
def json_to_dict(directories, configtypename="Config File", log_from='read_directory_json_to_dict', logging=True):
|
||||
|
||||
if not isinstance(directories, list):
|
||||
directories = [directories]
|
||||
|
||||
configs_dict = {}
|
||||
filesprocess, fileopenfail, filecount = [], 0, 0
|
||||
for directory in directories:
|
||||
if os.path.exists(directory) and os.path.isdir(directory):
|
||||
if len(os.listdir(directory)) > 0:
|
||||
for file in os.listdir(directory):
|
||||
filepath = os.path.join(directory, file)
|
||||
if os.path.isfile(filepath) and filepath.endswith('.json'):
|
||||
filesprocess.append(filepath)
|
||||
|
||||
for filepath in filesprocess:
|
||||
|
||||
# Read dictionary from file, if not, enable an empty dict
|
||||
filereadgood = True
|
||||
inf = codecs.open(filepath, "r", encoding='utf-8')
|
||||
infread = inf.read()
|
||||
try:
|
||||
dict_from_file = eval(infread)
|
||||
except Exception as e:
|
||||
filereadgood = False
|
||||
if logging:
|
||||
logs.log(log_from, "Error loading %s: %s (%s)" % (configtypename, e, filepath))
|
||||
dict_from_file = dict()
|
||||
# Close File
|
||||
inf.close()
|
||||
|
||||
# gather file stats
|
||||
slashsplit = str(filepath).split("/")
|
||||
filename = slashsplit[-1]
|
||||
filename_base = os.path.basename(filename).rsplit('.', 1)[0]
|
||||
|
||||
if not filereadgood or not isinstance(dict_from_file, dict):
|
||||
fileopenfail += 1
|
||||
else:
|
||||
filecount += 1
|
||||
dict_from_file["filepath"] = str(filepath)
|
||||
dict_from_file["filename"] = str(filename_base)
|
||||
configs_dict[filename_base] = dict_from_file
|
||||
|
||||
if filecount:
|
||||
if logging:
|
||||
logs.log(log_from, 'Registered %d %s dict files,' % (filecount, configtypename))
|
||||
logs.log(log_from, '%d %s dict files failed to load' % (fileopenfail, configtypename), True)
|
||||
else:
|
||||
if logging:
|
||||
logs.log(log_from, "Warning: Couldn't load any %s dict files" % (configtypename))
|
||||
|
||||
return configs_dict
|
||||
|
||||
|
||||
read = BotRead()
|
||||
|
||||
@ -4,7 +4,7 @@ from __future__ import unicode_literals, absolute_import, division, print_functi
|
||||
|
||||
import sopel_modules
|
||||
|
||||
from .Tools import read_directory_json_to_dict
|
||||
from .Read import read as botread
|
||||
|
||||
import os
|
||||
from fake_useragent import UserAgent
|
||||
@ -24,7 +24,7 @@ class Sherlock():
|
||||
usercfgdir = os.path.join(configsdir, "SpiceBot_Sherlock")
|
||||
dir_to_scan.append(usercfgdir)
|
||||
|
||||
valid_usernames_dict = read_directory_json_to_dict(dir_to_scan, "Sherlock", "SpiceBot_Sherlock")
|
||||
valid_usernames_dict = botread.json_to_dict(dir_to_scan, "Sherlock", "SpiceBot_Sherlock")
|
||||
|
||||
for social_network in list(valid_usernames_dict.keys()):
|
||||
self.dict[social_network] = valid_usernames_dict[social_network]
|
||||
|
||||
@ -316,61 +316,3 @@ def service_manip(servicename, dowhat, log_from='service_manip'):
|
||||
os.system("sudo service " + str(servicename) + " " + str(dowhat))
|
||||
except Exception as e:
|
||||
logs.log(log_from, str(dowhat).title() + "ing " + str(servicename) + ".service Failed: " + str(e))
|
||||
|
||||
|
||||
"""Config Reading Functions"""
|
||||
|
||||
|
||||
def read_directory_json_to_dict(directories, configtypename="Config File", log_from='read_directory_json_to_dict', logging=True):
|
||||
|
||||
if not isinstance(directories, list):
|
||||
directories = [directories]
|
||||
|
||||
configs_dict = {}
|
||||
filesprocess, fileopenfail, filecount = [], 0, 0
|
||||
for directory in directories:
|
||||
if os.path.exists(directory) and os.path.isdir(directory):
|
||||
if len(os.listdir(directory)) > 0:
|
||||
for file in os.listdir(directory):
|
||||
filepath = os.path.join(directory, file)
|
||||
if os.path.isfile(filepath) and filepath.endswith('.json'):
|
||||
filesprocess.append(filepath)
|
||||
|
||||
for filepath in filesprocess:
|
||||
|
||||
# Read dictionary from file, if not, enable an empty dict
|
||||
filereadgood = True
|
||||
inf = codecs.open(filepath, "r", encoding='utf-8')
|
||||
infread = inf.read()
|
||||
try:
|
||||
dict_from_file = eval(infread)
|
||||
except Exception as e:
|
||||
filereadgood = False
|
||||
if logging:
|
||||
logs.log(log_from, "Error loading %s: %s (%s)" % (configtypename, e, filepath))
|
||||
dict_from_file = dict()
|
||||
# Close File
|
||||
inf.close()
|
||||
|
||||
# gather file stats
|
||||
slashsplit = str(filepath).split("/")
|
||||
filename = slashsplit[-1]
|
||||
filename_base = os.path.basename(filename).rsplit('.', 1)[0]
|
||||
|
||||
if not filereadgood or not isinstance(dict_from_file, dict):
|
||||
fileopenfail += 1
|
||||
else:
|
||||
filecount += 1
|
||||
dict_from_file["filepath"] = str(filepath)
|
||||
dict_from_file["filename"] = str(filename_base)
|
||||
configs_dict[filename_base] = dict_from_file
|
||||
|
||||
if filecount:
|
||||
if logging:
|
||||
logs.log(log_from, 'Registered %d %s dict files,' % (filecount, configtypename))
|
||||
logs.log(log_from, '%d %s dict files failed to load' % (fileopenfail, configtypename), True)
|
||||
else:
|
||||
if logging:
|
||||
logs.log(log_from, "Warning: Couldn't load any %s dict files" % (configtypename))
|
||||
|
||||
return configs_dict
|
||||
|
||||
@ -27,6 +27,7 @@ from .Gif import *
|
||||
from .Sherlock import *
|
||||
from .Reddit import *
|
||||
# from .DictComs import *
|
||||
from .Read import *
|
||||
|
||||
|
||||
from .configure import *
|
||||
|
||||
Loading…
Reference in New Issue
Block a user