The new woob repository is here: https://gitlab.com/woob/woob. This gitlab will be removed soon.

The new woob repository is here: https://gitlab.com/woob/woob. This gitlab will be removed soon.

recipe.py 2.72 KB
Newer Older
1 2 3
#! /usr/bin/env python
# -*- coding: utf-8 -*-

4
# Copyright(C) 2013-2019      Laurent Bachelier, Sébastien Jean
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from __future__ import print_function
23 24

import codecs
25
import datetime
26
import os
27 28 29 30
import sys

from mako.lookup import TemplateLookup

31
from weboob import __version__
32 33 34 35 36 37 38 39

WEBOOB_MODULES = os.getenv(
    'WEBOOB_MODULES',
    os.path.realpath(os.path.join(os.path.dirname(__file__), '../../modules')))
BOILERPLATE_PATH = os.getenv(
    'BOILERPLATE_PATH',
    os.path.realpath(os.path.join(os.path.dirname(__file__), 'boilerplate_data')))

40
TEMPLATES = TemplateLookup(directories=[BOILERPLATE_PATH], input_encoding='utf-8')
41 42 43 44 45 46 47 48 49 50 51 52 53


def write(target, contents):
    if not os.path.isdir(os.path.dirname(target)):
        os.makedirs(os.path.dirname(target))
    if os.path.exists(target):
        print("%s already exists." % target, file=sys.stderr)
        sys.exit(4)
    with codecs.open(target, mode='w', encoding='utf-8') as f:
        f.write(contents)
    print('Created %s' % target)


54
class Recipe(object):
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    @classmethod
    def configure_subparser(cls, subparsers):
        subparser = subparsers.add_parser(cls.NAME)
        subparser.add_argument('name', help='Module name')
        subparser.set_defaults(recipe=cls)
        return subparser

    def __init__(self, args):
        self.name = args.name.lower().replace(' ', '')
        self.classname = args.name.title().replace(' ', '')
        self.year = datetime.date.today().year
        self.author = args.author
        self.email = args.email
        self.version = __version__
        self.login = False

    def write(self, filename, contents):
        return write(os.path.join(WEBOOB_MODULES, self.name, filename), contents)

    def template(self, name, **kwargs):
        if '.' not in name:
            name += '.pyt'
        return TEMPLATES.get_template(name) \
            .render(r=self,
                    # workaround, as it's also a mako directive
                    coding='# -*- coding: utf-8 -*-',
                    login=self.login,
                    **kwargs)

    def generate(self):
        raise NotImplementedError()