Commit b6480d92 authored by scmalte's avatar scmalte

Added utils/logging.py, adapted setup.py, started working on revise.py

parent c5675fde
.vscode/
build/
mossutils.egg-info/
mossutils/utils/__pycache__/
mossutils/__pycache__/
import mosspy
import argparse
# import logging
import pprint
import logging
from .utils import logging as logutils
DEFAULT_USER_ID="3795777", # Malte's Moss ID
DEFAULT_LANGUAGE="cc" # C++
......@@ -12,7 +13,6 @@ DEFAULT_FILE_PATTERN="./*/main.cpp"
DEFAULT_REPORT_FILE="moss-report.html"
DEFAULT_REPORT_SUBDIR="_moss-report"
DEFAULT_CONNECTIONS=8
DEFAULT_LOGGING_LEVEL="INFO"
def run_moss(
user_id=DEFAULT_USER_ID,
......@@ -32,36 +32,39 @@ def run_moss(
comment_string = str(comments)
# # A nicely formatted string representation requires a bit of hacking
# import pprint
# comment_string = pprint.pformat(comments, indent=2)
# comment_string = comment_string.replace("\n", "<br/>")
# comment_string = comment_string.replace(" ", "&nbsp;&nbsp;")
# Initialisation
logging.info("User id: {}".format(user_id))
logging.info("Language: {}".format(language))
logging.debug("User id: {}".format(user_id))
logging.debug("Language: {}".format(language))
moss = mosspy.Moss(user_id, language)
# Configure
logging.info("Ignore limit: {}".format(ignore_limit))
logging.info("Matches: {}".format(number_of_matching_files))
# logging.info("Comment: {}".format(comment_string))
logging.debug("Ignore limit: {}".format(ignore_limit))
logging.debug("Matches: {}".format(number_of_matching_files))
# logging.debug("Comment: {}".format(comment_string))
moss.setIgnoreLimit(ignore_limit)
moss.setNumberOfMatchingFiles(number_of_matching_files)
moss.setCommentString(comment_string)
# Add template file(s)
logging.info("Template: {}".format(template))
logging.debug("Template: {}".format(template))
if template:
moss.addBaseFile(template)
# Add submission file
logging.info("File pattern: {}".format(file_pattern))
logging.debug("File pattern: {}".format(file_pattern))
moss.addFilesByWildcard(file_pattern)
exit(0)
logging.info("Sending files to Moss")
url = moss.send() # Submission Report URL
......@@ -124,12 +127,7 @@ def configure_cli_parser(parser):
default=DEFAULT_REPORT_SUBDIR,
help="Directory for individual report files (default: {})".format(DEFAULT_REPORT_SUBDIR))
parser.add_argument(
"--log-level",
type=str,
choices=("OFF", "DEBUG", "INFO", "WARNING", "ERROR"),
default=DEFAULT_LOGGING_LEVEL,
help="Log level (default: {})".format(DEFAULT_LOGGING_LEVEL))
logutils.add_loglevel_argument(parser)
parser.add_argument(
"pattern",
......@@ -142,21 +140,7 @@ def main():
configure_cli_parser(parser)
args = parser.parse_args()
# See https://docs.python.org/3.8/library/logging.html#logging-levels
log_levels = {
"OFF": logging.NOTSET,
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR
}
log_level = log_levels[args.log_level]
# See https://docs.python.org/3.8/library/logging.html#logrecord-attributes
log_format = "%(message)s"
logging.basicConfig(level=log_level, format=log_format)
logutils.configure_level_and_format(args.log_level)
run_moss(
user_id=args.user_id,
......@@ -168,5 +152,6 @@ def main():
report_file=args.report_file,
report_subdir=args.report_dir)
if __name__ == '__main__':
main()
import re
import logging
import argparse
from bs4 import BeautifulSoup
from .utils import logging as logutils
DEFAULT_REPORT_INPUT_FILE="moss-report.html"
DEFAULT_REPORT_OUTPUT_FILE="moss-report-revised.html"
def get_match_percentage(match_text):
percentage_pattern = r'\((\d+)%\)$'
percentage_string = re.search(percentage_pattern, match_text).group(1)
return int(percentage_string)
# moss_report_input_filename = 'moss-report.html'
# moss_report_ouput_filename = 'moss-report-fixed.html'
def main():
with open(moss_report_input_filename) as input_file:
# We use BeautifulSoup because of it allows easy DOM manipulation and has
# a nice pretty-printing feature. The lxml parser is used because it is able
# to parse the totally broken HTML report file that Moss generates.
doc = BeautifulSoup(input_file, features='lxml')
doc.body.table['class'] = 'sortable'
ths = doc.find_all('th')
th = doc.new_tag('th')
th.string = 'Avg. %'
ths[1].insert_after(th)
for row in doc.find_all('tr'):
for a in row.find_all('a'):
a['target'] = '_blank'
# print(row)
cols = row.find_all('td')
first_match_text = cols[0].a.string
second_match_text = cols[1].a.string
first_percentage = get_match_percentage(first_match_text)
second_percentage = get_match_percentage(second_match_text)
avg_percentage = (first_percentage + second_percentage) / 2
td = doc.new_tag('td')
td.string = str(avg_percentage)
row.insert(2, td)
# <script src="sorttable.js"></script>
script = doc.new_tag('script')
script['src'] = 'sorttable.js' # Library from https://kryogenix.org/code/browser/sorttable/
doc.html.head.append(script)
# <link href="style.css" rel="stylesheet">
link = doc.new_tag('link')
link['href'] = 'style.css'
link['rel'] = 'stylesheet'
doc.html.head.append(link)
# <script src="script.js"></script>
script = doc.new_tag('script')
script['src'] = 'script.js'
doc.html.body.append(script)
with open(moss_report_ouput_filename, 'w') as output_file:
output_file.write(doc.prettify()) # 'utf-8'
if __name__ == '__main__':
main()
\ No newline at end of file
import logging
import argparse
# See https://docs.python.org/3.8/library/logging.html#logging-levels
LOG_LEVELS = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"OFF": logging.CRITICAL + 1
}
DEFAULT_LOGGING_LEVEL="DEBUG"
DEFAULT_LOG_FORMAT="[%(levelname)s] %(message)s"
def add_loglevel_argument(parser, default_level=DEFAULT_LOGGING_LEVEL):
parser.add_argument(
"--log-level",
type=str,
choices=("OFF", "DEBUG", "INFO", "WARNING", "ERROR"),
default=default_level,
help="Log level (default: {})".format(default_level))
def configure_level_and_format(level_string=DEFAULT_LOGGING_LEVEL, format=DEFAULT_LOG_FORMAT):
# See https://docs.python.org/3.8/library/logging.html#logrecord-attributes
logging.basicConfig(level=LOG_LEVELS[level_string], format=format)
......@@ -8,12 +8,20 @@ setup(
author='Malte Schwerhoff',
author_email='malte.schwerhoff@inf.ethz.ch',
license='unlicensed',
packages=['mossutils'],
packages=[
'mossutils',
'mossutils.utils'
],
install_requires=[
'mosspy',
'bs4',
'lxml'
],
# scripts=['bin/mossutils-moss'],
entry_points = {
"console_scripts": ['mu-moss = mossutils.moss:main']
"console_scripts": [
'mu-moss = mossutils.moss:main',
'mu-revise = mossutils.revise:main'
]
},
zip_safe=False)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment