refactor entrypoints-related code
This commit is contained in:
parent
8cea5b6059
commit
30eda39a7c
3 changed files with 23 additions and 22 deletions
|
@ -1,21 +1,15 @@
|
|||
from __future__ import print_function
|
||||
import sys
|
||||
import argparse
|
||||
from pkg_resources import iter_entry_points
|
||||
from entrypoints_utils import get_one_entrypoint
|
||||
import json
|
||||
import logging
|
||||
from logging import getLogger
|
||||
log = getLogger('audiogen')
|
||||
|
||||
|
||||
def get_audiogenerator(kind):
|
||||
'''Messes with entrypoints to return an audiogenerator function'''
|
||||
points = tuple(iter_entry_points(group='larigira.audiogenerators',
|
||||
name=kind))
|
||||
if not points:
|
||||
raise ValueError('cant find a generator for ', kind)
|
||||
if len(points) > 1:
|
||||
logging.warning("Found more than one audiogenerator for '%s'" % kind)
|
||||
gen = points[0]
|
||||
return gen.load()
|
||||
return get_one_entrypoint('larigira.audiogenerators', kind)
|
||||
|
||||
|
||||
def get_parser():
|
||||
|
@ -49,7 +43,7 @@ def main():
|
|||
spec = read_spec(args.audiospec[0])
|
||||
errors = tuple(check_spec(spec))
|
||||
if errors:
|
||||
logging.error("Errors in audiospec")
|
||||
log.error("Errors in audiospec")
|
||||
for err in errors:
|
||||
print(err) # TODO: to stderr
|
||||
sys.exit(1)
|
||||
|
|
13
larigira/entrypoints_utils.py
Normal file
13
larigira/entrypoints_utils.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
from logging import getLogger
|
||||
log = getLogger('entrypoints_utils')
|
||||
from pkg_resources import iter_entry_points
|
||||
|
||||
|
||||
def get_one_entrypoint(group, kind):
|
||||
'''Messes with entrypoints to return an entrypoint of a given group/kind'''
|
||||
points = tuple(iter_entry_points(group=group, name=kind))
|
||||
if not points:
|
||||
raise ValueError('cant find an entrypoint %s:%s' % (group, kind))
|
||||
if len(points) > 1:
|
||||
log.warning("Found more than one timeform for %s:%s" % (group, kind))
|
||||
return points[0].load()
|
|
@ -4,22 +4,16 @@ main module to read and get informations about alarms
|
|||
from __future__ import print_function
|
||||
import sys
|
||||
import argparse
|
||||
from pkg_resources import iter_entry_points
|
||||
from entrypoints_utils import get_one_entrypoint
|
||||
import json
|
||||
import logging
|
||||
from logging import getLogger
|
||||
log = getLogger('timegen')
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def get_timegenerator(kind):
|
||||
'''Messes with entrypoints to return an timegenerator function'''
|
||||
points = tuple(iter_entry_points(group='larigira.timegenerators',
|
||||
name=kind))
|
||||
if not points:
|
||||
raise ValueError('cant find a generator for ', kind)
|
||||
if len(points) > 1:
|
||||
logging.warning("Found more than one timegenerator for '%s'" % kind)
|
||||
gen = points[0]
|
||||
return gen.load()
|
||||
return get_one_entrypoint('larigira.timegenerators', kind)
|
||||
|
||||
|
||||
def get_parser():
|
||||
|
@ -65,7 +59,7 @@ def main():
|
|||
spec = read_spec(args.timespec[0])
|
||||
errors = tuple(check_spec(spec))
|
||||
if errors:
|
||||
logging.error("Errors in timespec")
|
||||
log.error("Errors in timespec")
|
||||
for err in errors:
|
||||
print(err) # TODO: to stderr
|
||||
sys.exit(1)
|
||||
|
|
Loading…
Reference in a new issue