Skip to content

Commit

Permalink
feat: Add sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauric Desauw authored and altor committed Sep 28, 2021
1 parent 40d17ce commit 8587b9f
Show file tree
Hide file tree
Showing 46 changed files with 897 additions and 452 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 160
ignore = W504, F401
8 changes: 6 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ confidence=
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use "--disable=all --enable=classes
# --disable=W".
disable=R0903, R0902, C0200
disable=R0903, R0902, C0200,
C0114, C0103, R1705, R0913,
W0102, C0112, R1720, R0915,
W0237, R1732, W1514, R1718,
C0206, W0236, W0223,
print-statement,
parameter-unpacking,
unpacking-in-except,
Expand Down Expand Up @@ -326,7 +330,7 @@ indent-after-paren=4
indent-string=' '

# Maximum number of characters on a single line.
max-line-length=100
max-line-length=160

# Maximum number of lines in a module.
max-module-lines=1000
Expand Down
6 changes: 3 additions & 3 deletions powerapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2018, INRIA
# Copyright (c) 2018, University of Lille
# Copyright (c) 2021, INRIA
# Copyright (c) 2021, University of Lille
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -27,4 +27,4 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

__version__ = "0.10.0"
__version__ = "0.10.1"
31 changes: 25 additions & 6 deletions powerapi/cli/config_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,34 @@
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from typing import Dict
import logging
import os

from typing import Dict


class ConfigValidator:
"""
Validate powerapi config and initialize missing default values
"""
@staticmethod
def validate(config: Dict):

"""
Validate powerapi config and initialize missing default values
"""
if 'verbose' not in config:
config['verbose'] = logging.NOTSET
if 'stream' not in config:
config['stream'] = False
if 'output' not in config:
logging.error("no output configuration found")
return False

for output_type in config['output']:
output_config = config['output'][output_type]
if 'model' not in output_config:
if 'model' not in output_config:
output_config['model'] = 'HWPCReport'
if 'name' not in output_config:
if 'name' not in output_config:
output_config['name'] = 'default_pusher'

if 'input' not in config:
Expand All @@ -58,5 +66,16 @@ def validate(config: Dict):
input_config['model'] = 'PowerReport'
if 'name' not in input_config:
input_config['name'] = 'default_puller'

if not ConfigValidator._validate_input(config):
return False
return True

@staticmethod
def _validate_input(config: Dict):
for _, input_config in config['input'].items():
if input_config['type'] == 'csv':
for file_name in input_config['files']:
if not os.access(file_name, os.R_OK):
return False
return True

94 changes: 37 additions & 57 deletions powerapi/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import getopt
import sys
from copy import deepcopy

from powerapi.exception import PowerAPIException
Expand All @@ -42,9 +42,9 @@ def _extract_minus(arg):
def _cast_argument_value(arg_name, val, action):
if not action.is_flag:
try:
return action.type(val)
except ValueError:
raise BadTypeException(arg_name, action.type)
return action.arg_type(val)
except ValueError as exn:
raise BadTypeException(arg_name, action.arg_type) from exn
return val


Expand All @@ -62,6 +62,9 @@ def _find_longest_name(names):
# EXCEPTION #
#############
class ParserException(PowerAPIException):
"""
Base Exception for parser error
"""
def __init__(self, argument_name):
PowerAPIException.__init__(self)
self.argument_name = argument_name
Expand All @@ -79,16 +82,6 @@ class ComponentAlreadyExistException(ParserException):
"""


class BadValueException(ParserException):
"""
Exception raised when attempting to parse a value that doens't respect the
check function of its argument
"""
def __init__(self, argument_name, msg):
ParserException.__init__(self, argument_name)
self.msg = msg


class SubParserWithoutNameArgumentException(PowerAPIException):
"""
Exception raised when a subparser without argument name is added to a parser
Expand Down Expand Up @@ -138,13 +131,11 @@ def __init__(self, argument_name):

class BadTypeException(ParserException):
"""
Exception raised when an argument is parsed with a value of an incorrect
type
Exception raised when an argument is parsed with a value of an incorrect type
"""
def __init__(self, argument_name, type):
def __init__(self, argument_name, arg_type):
ParserException.__init__(self, argument_name)
self.type_name = type.__name__
self.type_name = arg_type.__name__
self.article = 'an' if self.type_name in ('a', 'e', 'i', 'o', 'u', 'y') else 'a'


Expand Down Expand Up @@ -189,38 +180,41 @@ class ParserAction:
"""
Action binded to an argument
"""
def __init__(self, name_list, is_flag, action, default_value, check_fun,
check_msg, help_str, type):
def __init__(self, name_list, is_flag, action, default_value, help_str, arg_type):
self.name_list = name_list
self.is_flag = is_flag
self.action = action
self.default_value = default_value
self.check_fun = check_fun
self.check_msg = check_msg
self.help_str = help_str
self.type = type
self.arg_type = arg_type


class SubParserGroup:

""""""
def __init__(self, group_name, help_str=''):
self.group_name = group_name
self.help_str = help_str
self.subparsers = {}

def contains(self, name):
""""""
return name in self.subparsers

def add_subparser(self, name, subparser):
""""""
self.subparsers[name] = subparser

def get_subparser(self, name):
""""""
return self.subparsers[name]

def __iter__(self):
return iter(self.subparsers.items())

def get_help(self):
"""
return help string
"""
s = self.group_name + ' details :\n'
for subparser_name, subparser in self.subparsers.items():
s += ' --' + self.group_name + ' ' + subparser_name + ':\n'
Expand All @@ -231,14 +225,14 @@ def get_help(self):


class Parser:

""""""
def __init__(self):
self.actions = {}
self.default_values = {}
self.action_list = []

def add_argument(self, *names, flag=False, action=store_val, default=None,
check=None, check_msg='', help='', type=str):
help='', type=str):
"""add an optional argument to the parser that will activate an action
:param str *names: names of the optional argument that will be bind to
Expand All @@ -257,13 +251,6 @@ def add_argument(self, *names, flag=False, action=store_val, default=None,
:param default: the default value attached to this argument
:param lambda check: function that is called to validate the value
required by the argument this function take one
parameter (the value) and return a boolean
:param str check msg: message displayed when the caught value doesn't
respect the check function
:param str help: string that describe the argument
:param type type: type of the value that the argument must catch
Expand All @@ -273,8 +260,7 @@ def add_argument(self, *names, flag=False, action=store_val, default=None,
added to this parser
"""
parser_action = ParserAction(list(names), flag, action, default, check,
check_msg, help, type)
parser_action = ParserAction(list(names), flag, action, default, help, type)

for name in names:
if name in self.actions:
Expand All @@ -295,7 +281,7 @@ def _get_action_list_str(self, indent):
s += ' : ' + action.help_str + '\n'
return s

def _unknow_argument_behaviour(self, arg, val, args, acc):
def _unknow_argument_behaviour(self, arg_name, val, args, acc):
raise NotImplementedError()

def _parse(self, args, acc):
Expand All @@ -311,21 +297,18 @@ def _parse(self, args, acc):
arg_long_name = _find_longest_name(action.name_list)
val = _cast_argument_value(arg_long_name, val, action)

# check value
if action.check_fun is not None and not action.check_fun(val):
raise BadValueException(arg_long_name, action.check_msg)
args, acc = action.action(arg_long_name, val, args, acc)

return args, acc


class ComponentSubParser(Parser):

""""""
def __init__(self, name):
Parser.__init__(self)
self.name = name

def _unknow_argument_behaviour(self, arg, val, args, acc):
def _unknow_argument_behaviour(self, arg_name, val, args, acc):
return args, acc

def subparse(self, token_list):
Expand All @@ -346,13 +329,15 @@ def subparse(self, token_list):

return self._parse(token_list, local_result)


def get_help(self):
"""
return help string
"""
return self._get_action_list_str(' ')


class MainParser(Parser):

""""""
def __init__(self, help_arg=True):
"""
:param bool help_arg: if True, add a -h/--help argument that display help
Expand Down Expand Up @@ -383,18 +368,14 @@ def parse(self, args):
:raise BadTypeException: when an argument is parsed with a value of an
incorrect type
:raise BadValueException: when a value that doens't respect the check
function of its argument is parsed
"""
try:
args, _ = getopt.getopt(args, self.short_arg, self.long_arg)
except getopt.GetoptError as exn:
if 'recognized' in exn.msg:
raise UnknowArgException(exn.opt)
raise UnknowArgException(exn.opt) from exn
elif 'requires' in exn.msg:
raise MissingValueException(exn.opt)
raise MissingValueException(exn.opt) from exn

# retirer les moins
args = list(map(lambda x: (_extract_minus(x[0]), x[1]), args))
Expand All @@ -404,14 +385,17 @@ def parse(self, args):
for arg_name, _ in args:
if arg_name in ('h', 'help'):
print(self.get_help())
exit(0)
sys.exit(0)

acc = deepcopy(self.default_values)

args, acc = self._parse(args, acc)
return acc

def get_help(self):
"""
return help string
"""
s = 'main arguments:\n'
s += self._get_action_list_str(' ')
s += '\n'
Expand Down Expand Up @@ -448,11 +432,8 @@ def gen_name(name):
else:
self.long_arg.append(gen_name(name))

def add_argument(self, *names, flag=False, action=store_val, default=None,
check=None, help='', type=str):
Parser.add_argument(self, *names, flag=flag, action=action,
default=default, check=check, help=help,
type=type)
def add_argument(self, *names, flag=False, action=store_val, default=None, help='', type=str):
Parser.add_argument(self, *names, flag=flag, action=action, default=default, help=help, type=type)
self._add_argument_names(names, flag)

def add_component_subparser(self, component_type, subparser, help_str=''):
Expand Down Expand Up @@ -489,7 +470,6 @@ def _action(arg, val, args, acc):
for action_name, action in subparser.actions.items():
self._add_argument_names([action_name], action.is_flag)


def add_actor_subparser(self, component_type, subparser, help_str=''):
"""
Add a subparser that will be used by the argument *component_name*
Expand Down
Loading

0 comments on commit 8587b9f

Please sign in to comment.