refactor entrypoints-related code

This commit is contained in:
boyska 2015-02-01 19:57:50 +01:00
parent 8cea5b6059
commit 30eda39a7c
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
3 changed files with 23 additions and 22 deletions

View file

@ -1,21 +1,15 @@
from __future__ import print_function from __future__ import print_function
import sys import sys
import argparse import argparse
from pkg_resources import iter_entry_points from entrypoints_utils import get_one_entrypoint
import json import json
import logging from logging import getLogger
log = getLogger('audiogen')
def get_audiogenerator(kind): def get_audiogenerator(kind):
'''Messes with entrypoints to return an audiogenerator function''' '''Messes with entrypoints to return an audiogenerator function'''
points = tuple(iter_entry_points(group='larigira.audiogenerators', return get_one_entrypoint('larigira.audiogenerators', kind)
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()
def get_parser(): def get_parser():
@ -49,7 +43,7 @@ def main():
spec = read_spec(args.audiospec[0]) spec = read_spec(args.audiospec[0])
errors = tuple(check_spec(spec)) errors = tuple(check_spec(spec))
if errors: if errors:
logging.error("Errors in audiospec") log.error("Errors in audiospec")
for err in errors: for err in errors:
print(err) # TODO: to stderr print(err) # TODO: to stderr
sys.exit(1) sys.exit(1)

View 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()

View file

@ -4,22 +4,16 @@ main module to read and get informations about alarms
from __future__ import print_function from __future__ import print_function
import sys import sys
import argparse import argparse
from pkg_resources import iter_entry_points from entrypoints_utils import get_one_entrypoint
import json import json
import logging from logging import getLogger
log = getLogger('timegen')
from datetime import datetime from datetime import datetime
def get_timegenerator(kind): def get_timegenerator(kind):
'''Messes with entrypoints to return an timegenerator function''' '''Messes with entrypoints to return an timegenerator function'''
points = tuple(iter_entry_points(group='larigira.timegenerators', return get_one_entrypoint('larigira.timegenerators', kind)
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()
def get_parser(): def get_parser():
@ -65,7 +59,7 @@ def main():
spec = read_spec(args.timespec[0]) spec = read_spec(args.timespec[0])
errors = tuple(check_spec(spec)) errors = tuple(check_spec(spec))
if errors: if errors:
logging.error("Errors in timespec") log.error("Errors in timespec")
for err in errors: for err in errors:
print(err) # TODO: to stderr print(err) # TODO: to stderr
sys.exit(1) sys.exit(1)