Skip to content

Commit

Permalink
Code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelazoDS committed Aug 6, 2021
1 parent c7c68e8 commit d6fc354
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
21 changes: 11 additions & 10 deletions docs/DocGenerator/DocGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
VERSION = "0.1"
CONFIG_PATH = "config.yaml"


class DocGenerator:
"""
brief: Main class of DocGenerator tool.
Expand Down Expand Up @@ -53,7 +54,8 @@ def is_valid_folder(self, path):

def is_valid_file(self, file):
"""
brief: Checks if a file name should be ignored because it's in the ignore list or doesn´t match with the regexes.
brief: Checks if a file name should be ignored because it's in the ignore list
or doesn´t match with the regexes.
args:
- "file (str): File name to be controlled"
returns: "boolean: False if the file should be ignored. True otherwise."
Expand Down Expand Up @@ -84,7 +86,7 @@ def get_group_doc_path(self, group):
returns: "string: The name of the documentation group file"
"""
base_path = os.path.join(self.conf.documentation_path, os.path.basename(self.scan_path))
doc_path = os.path.join(base_path,group['name']+".group")
doc_path = os.path.join(base_path, group['name']+".group")
return doc_path

def get_test_doc_path(self, path):
Expand Down Expand Up @@ -154,8 +156,6 @@ def create_test(self, path, group_id):
logging.warning(f"Content for {path} is empty, ignoring it")
return None



def parse_folder(self, path, group_id):
"""
brief: Search in a specific folder to parse possible group files and each test file.
Expand All @@ -173,15 +173,15 @@ def parse_folder(self, path, group_id):
(root, folders, files) = next(os.walk(path))
for file in files:
if self.is_group_file(file):
new_group = self.create_group(os.path.join(root,file), group_id)
new_group = self.create_group(os.path.join(root, file), group_id)
if new_group:
group_id = new_group
break
for file in files:
if self.is_valid_file(file):
self.create_test(os.path.join(root,file), group_id)
self.create_test(os.path.join(root, file), group_id)
for folder in folders:
self.parse_folder(os.path.join(root,folder), group_id)
self.parse_folder(os.path.join(root, folder), group_id)

def run(self):
"""
Expand All @@ -194,8 +194,9 @@ def run(self):
logging.debug(f"Going to parse files on '{path}'")
self.parse_folder(path, self.__id_counter)


def start_logging(folder, debug_level=logging.INFO):
LOG_PATH = os.path.join(folder, os.path.splitext(os.path.basename(__file__))[0]+".log" )
LOG_PATH = os.path.join(folder, os.path.splitext(os.path.basename(__file__))[0]+".log")
if not os.path.exists(folder):
os.makedirs(folder)
logging.basicConfig(filename=LOG_PATH, level=debug_level)
Expand Down Expand Up @@ -224,10 +225,10 @@ def start_logging(folder, debug_level=logging.INFO):
sanity = Sanity(Config(CONFIG_PATH))
sanity.run()
elif args.index_name:
indexData=IndexData(args.index_name, Config(CONFIG_PATH))
indexData = IndexData(args.index_name, Config(CONFIG_PATH))
indexData.run()
elif args.launch_app:
indexData=IndexData(args.launch_app, Config(CONFIG_PATH))
indexData = IndexData(args.launch_app, Config(CONFIG_PATH))
indexData.run()
os.chdir("Search-UI")
os.system("ELASTICSEARCH_HOST=http://localhost:9200 npm start")
Expand Down
41 changes: 20 additions & 21 deletions docs/DocGenerator/lib/IndexData.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
brief: Wazuh DocGenerator config parser.
brief: Wazuh DocGenerator data indexer.
copyright: Copyright (C) 2015-2021, Wazuh Inc.
date: August 04, 2021
license: This program is free software; you can redistribute it
Expand All @@ -11,18 +11,18 @@
import re
import json
import requests
import logging
from elasticsearch import Elasticsearch, helpers


class IndexData:
"""
brief: Class that indexes the data from JSON files into ElasticSearch.
"""
def __init__(self, index, config):
self.conf = config
self.path = self.conf.documentation_path
self.path = config.documentation_path
self.index = index
self.regex = ".*json"
self.files = self.get_files()
self.regex = re.compile(".*json")
self.es = Elasticsearch()
self.output = []

Expand All @@ -35,47 +35,46 @@ def test_connection(self):
if res.status_code == 200:
return True
except Exception as e:
print(e)
logging.exception(f"Connection error:\n{e}")
return False

def get_files(self):
"""
brief: Recursively finds all the files that match with the regex provided.
"""
doc_files = []
r = re.compile(self.regex)
for (root, *_, files) in os.walk(self.path):
for file in files:
if r.match(file):
doc_files.append(os.path.join(root,file))
if self.regex.match(file):
doc_files.append(os.path.join(root, file))
return doc_files

def read_files_content(self):
def read_files_content(self, files):
"""
brief: It Opens every file found in the path and appends the content into a list.
brief: Opens every file found in the path and appends the content into a list.
"""
for file in self.files:
for file in files:
with open(os.path.join(self.path, file)) as f:
lines = json.load(f)
self.output.append(lines)

def remove_index(self):
"""
brief: It Deletes an index.
brief: Deletes an index.
"""
delete=self.es.indices.delete(index=self.index, ignore=[400, 404])
print(f'Delete index {self.index}\n {delete}\n')
delete = self.es.indices.delete(index=self.index, ignore=[400, 404])
logging.debug(f'Delete index {self.index}\n {delete}\n')

def run(self):
"""
brief: This calls all the methods of the Class. Finally, it uses the index name and the documents
to make a request to the BULK API.
brief: Collects all the documentation files and makes a request to the BULK API to index the new data.
"""
self.test_connection()
self.read_files_content()
files = self.get_files()
self.read_files_content(files)
if self.test_connection():
self.remove_index()
print("Indexing data...\n")
logging.debug("Indexing data...\n")
helpers.bulk(self.es, self.output, index=self.index)
out=json.dumps(self.es.cluster.health(wait_for_status='yellow', request_timeout=1), indent=4)
print(out)
out = json.dumps(self.es.cluster.health(wait_for_status='yellow', request_timeout=1), indent=4)
logging.debug(out)
File renamed without changes.

0 comments on commit d6fc354

Please sign in to comment.