<commit_before>import logging

from pycli_tools.parsers import get_argparser

from smeterd import __version__
from smeterd import __description__
from smeterd.meter import read_one_packet
from smeterd.storage import store_single_packet



log = logging.getLogger(__name__)


DEFAULT_SERIAL='/dev/ttyUSB0'



def read_meter(args, parser):
    '''
    Read a single packet from the smart meter.
    Packets can either be printed to stdout or stored
    in a sqlite database.
    '''
    packet = read_one_packet(args.serial_port)

    if args.store and args.database:
        log.info('Storing data in database %s', args.database)
        store_single_packet(args.database, packet)

    if args.raw:
        print str(packet)
    else:
        print('Date:      %s' % packet.date)
        print('kWh1:      %s kwh' % packet.kwh1)
        print('kWh2:      %s kwh' % packet.kwh2)
        print('Gas:       %s m3' % packet.gas)



from argparse import Action
from os.path import isfile, isabs, abspath


class ExistingFile(Action):
    def __call__(self, parser, args, values, option_string=None):
        if not isfile(values):
            parser.error('File `%s` does not exist' % values)

        path = values if isabs(values) else abspath(values)
        setattr(args, self.dest, path)



def parse_and_run():
    # create the top-level parser
    parser = get_argparser(prog='smeterd',
                           version=__version__,
                           default_config='~/.smeterdrc',
                           description=__description__)
    subparsers = parser.add_subparsers()

    logging.basicConfig(format='%(asctime)-15s %(levelname)s %(message)s')

    # create the parser for the "read-meter" command
    parser_a = subparsers.add_parser('read-meter', help='Read a single P1 packet')
    parser_a.add_argument('--serial-port', default=DEFAULT_SERIAL,
                          metavar=DEFAULT_SERIAL,
                          help='serial port to read packets from (defaults to %s)' % DEFAULT_SERIAL)
    parser_a.add_argument('--store', action='store_true',
                          help='write the results to the database')
    parser_a.add_argument('--raw', action='store_true',
                          help='display packet in raw form')
    parser_a.add_argument('--database', action=ExistingFile,
                          help='sqlite database containig smeter data')<commit_msg>Set logging format through pycli_tools<commit_after>import logging

from pycli_tools.parsers import get_argparser

from smeterd import __version__
from smeterd import __description__
from smeterd.meter import read_one_packet
from smeterd.storage import store_single_packet



log = logging.getLogger(__name__)


DEFAULT_SERIAL='/dev/ttyUSB0'



def read_meter(args, parser):
    '''
    Read a single packet from the smart meter.
    Packets can either be printed to stdout or stored
    in a sqlite database.
    '''
    packet = read_one_packet(args.serial_port)

    if args.store and args.database:
        log.info('Storing data in database %s', args.database)
        store_single_packet(args.database, packet)

    if args.raw:
        print str(packet)
    else:
        print('Date:      %s' % packet.date)
        print('kWh1:      %s kwh' % packet.kwh1)
        print('kWh2:      %s kwh' % packet.kwh2)
        print('Gas:       %s m3' % packet.gas)



from argparse import Action
from os.path import isfile, isabs, abspath


class ExistingFile(Action):
    def __call__(self, parser, args, values, option_string=None):
        if not isfile(values):
            parser.error('File `%s` does not exist' % values)

        path = values if isabs(values) else abspath(values)
        setattr(args, self.dest, path)



def parse_and_run():
    # create the top-level parser
    parser = get_argparser(prog='smeterd',
                           version=__version__,
                           default_config='~/.smeterdrc',
                           logging_format='[%(asctime)-15s] %(levelname)s %(message)s',
                           description=__description__)
    subparsers = parser.add_subparsers()

    # create the parser for the "read-meter" command
    parser_a = subparsers.add_parser('read-meter', help='Read a single P1 packet')
    parser_a.add_argument('--serial-port', default=DEFAULT_SERIAL,
                          metavar=DEFAULT_SERIAL,
                          help='serial port to read packets from (defaults to %s)' % DEFAULT_SERIAL)
    parser_a.add_argument('--store', action='store_true',
                          help='write the results to the database')
    parser_a.add_argument('--raw', action='store_true',
                          help='display packet in raw form')
    parser_a.add_argument('--database', action=ExistingFile,
                          help='sqlite database containig smeter data')
{'fraction_non_alphanumeric': 0.09845520082389289, 'fraction_numerical': 0.0037075180226570545, 'mean_word_length': 2.554904831625183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '465421', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1407, 'n_words': 371}