<commit_before>#!/usr/bin/env python
"""
Manages recipient contracts of kWh rights for a Generation-kWh investor.
"""

import erppeek
import datetime
from dateutil.relativedelta import relativedelta
import dbconfig
from yamlns import namespace as ns
from generationkwh.isodates import isodate

def parseArgumments():
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)

    subparsers = parser.add_subparsers(
        title="Subcommands",
        dest='subcommand',
        )
    list = subparsers.add_parser('list',
        help="list assignments",
        )
    expire = subparsers.add_parser('expire',
        help="expire assignments",
        )
    default = subparsers.add_parser('default',
        help="create contract assignations following the by default criteria",
        )
    clear = subparsers.add_parser('clear',
        help="clear investments objects",
        )
    for sub in expire,:
        sub.add_argument('contract')
        sub.add_argument('member')
    for sub in default,:
        """
        sub.add_argument(
            '-n','--number',
            dest='idmode',
            action='store_const',
            const='number',
            default='memberid',
            help="select members by its member code number, "
                "instead member database id",
            )
        sub.add_argument(
            '-p','--partner',
            dest='idmode',
            action='store_const',
            const='partner',
            help="select members by its partner database id, "
                "instead member database id",
            )
        sub.add_argument(
            '--all',
            action='store_true',
            help='apply it to all members with investments',
            )
        sub.add_argument(
            '--force',
            action='store_true',
            help='create even without ',
            )
        """
        sub.add_argument(
            dest='members',
            action='append',
            nargs='*',
            help='investors of Generation-kWh (see --partner and --number)',
            )

    return parser.parse_args(namespace=ns())

def preprocessMembers(args):
    """Turns members in which ever format to the ones required by commands"""

def clear(**args):
    c.GenerationkwhAssignment.dropAll()

def list(csv=False,**args):
    """
        List active investments between start and stop, both included,
        for the member of for any member if member is None.
        If start is not specified, it lists activated before stop.
        If stop is not specified, it list activated and not deactivated
        before start.
        If neither start or stop are specified all investments are listed
        active or not.
    """
    def buildcsv(data):
        return u''.join((
            u"\t".join((
                unicode(c) for c in line
                ))+'\n'
            for line in data))

    csvdata = buildcsv([(
            'member id',
            'priority',
            'contract id',
            'contractref',
            'member name',
            'end_date'
        )]+[
        (
            r['member_id'][0],
            r['priority'],
            r['contract_id'][0],
            r['contract_id'][1],
            r['member_id'][1],
            r['end_date']
        ) for r in c.GenerationkwhAssignment.read([],[
            'member_id',
            'contract_id',
            'priority',
            'end_date'
            ],)
        ])
    if csv: return csvdata
    print csvdata

def expire(
        member=None,
        contract=None,
        **_):
    c.GenerationkwhAssignment.expire([(int(contract),int(member))])

def default(
        members=None,
        force=False,
        **_):
    if force: clear()
    c.GenerationkwhAssignment.createDefaultForMembers(members[0])


c = erppeek.Client(**dbconfig.erppeek)<commit_msg>Added -p option to default subcommand<commit_after>#!/usr/bin/env python
"""
Manages recipient contracts of kWh rights for a Generation-kWh investor.
"""

import erppeek
import datetime
from dateutil.relativedelta import relativedelta
import dbconfig
from yamlns import namespace as ns
from generationkwh.isodates import isodate

def parseArgumments():
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)

    subparsers = parser.add_subparsers(
        title="Subcommands",
        dest='subcommand',
        )
    list = subparsers.add_parser('list',
        help="list assignments",
        )
    expire = subparsers.add_parser('expire',
        help="expire assignments",
        )
    default = subparsers.add_parser('default',
        help="create contract assignations following the by default criteria",
        )
    clear = subparsers.add_parser('clear',
        help="clear investments objects",
        )
    for sub in expire,:
        sub.add_argument('contract')
        sub.add_argument('member')
    for sub in default,:
        
        sub.add_argument(
            '-p','--partner',
            dest='idmode',
            action='store_const',
            const='partner',
            help="select members by its partner database id, "
                "instead member database id",
            )
        """
        sub.add_argument(
            '-n','--number',
            dest='idmode',
            action='store_const',
            const='number',
            default='memberid',
            help="select members by its member code number, "
                "instead member database id",
            )
        sub.add_argument(
            '--all',
            action='store_true',
            help='apply it to all members with investments',
            )
        sub.add_argument(
            '--force',
            action='store_true',
            help='create even without ',
            )
        """
        sub.add_argument(
            dest='members',
            action='append',
            nargs='*',
            help='investors of Generation-kWh (see --partner and --number)',
            )

    return parser.parse_args(namespace=ns())

def preprocessMembers(members=None,idmode=None):
    """Turns members in which ever format to the ones required by commands"""
    if idmode=="partner":
        members = c.GenerationkwhTesthelper.get_members_by_partners(members).values()
    else:
        pass
    return members
def clear(**args):
    c.GenerationkwhAssignment.dropAll()

def list(csv=False,**args):
    """
        List active investments between start and stop, both included,
        for the member of for any member if member is None.
        If start is not specified, it lists activated before stop.
        If stop is not specified, it list activated and not deactivated
        before start.
        If neither start or stop are specified all investments are listed
        active or not.
    """
    def buildcsv(data):
        return u''.join((
            u"\t".join((
                unicode(c) for c in line
                ))+'\n'
            for line in data))

    csvdata = buildcsv([(
            'member id',
            'priority',
            'contract id',
            'contractref',
            'member name',
            'end_date'
        )]+[
        (
            r['member_id'][0],
            r['priority'],
            r['contract_id'][0],
            r['contract_id'][1],
            r['member_id'][1],
            r['end_date']
        ) for r in c.GenerationkwhAssignment.read([],[
            'member_id',
            'contract_id',
            'priority',
            'end_date'
            ],)
        ])
    if csv: return csvdata
    print csvdata

def expire(
        member=None,
        contract=None,
        **_):
    c.GenerationkwhAssignment.expire([(int(contract),int(member))])

def default(
        members=None,
        force=False,
        idmode=None,
        **_):
    if force: clear()
    members=preprocessMembers(members[0],idmode)
    c.GenerationkwhAssignment.createDefaultForMembers(members)


c = erppeek.Client(**dbconfig.erppeek)
{'fraction_non_alphanumeric': 0.11010314402883062, 'fraction_numerical': 0.0012426991425375916, 'mean_word_length': 1.875312611647017, 'pattern_counts': {'":': 1, '<': 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': '5874445', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2144, 'n_words': 562}