Commit fbf91d70 authored by JEAN Sébastien's avatar JEAN Sébastien Committed by Laurent Bachelier

tools: Boilerplate refactoring for extended usage

parent db462916
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright(C) 2013-2019 Laurent Bachelier, Sébastien Jean
#
# 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
import argparse
import inspect
import subprocess
import os
import sys
from importlib import import_module
BOILERPLATE_PATH = os.getenv(
'BOILERPLATE_PATH',
os.path.realpath(os.path.join(os.path.dirname(__file__), 'boilerplate_data')))
sys.path.append(os.path.dirname(__file__))
sys.path.append(BOILERPLATE_PATH)
def u8(s):
if isinstance(s, bytes):
return s.decode('utf-8')
return s
def gitconfig(entry):
return u8(subprocess.check_output('git config -z --get %s' % entry, shell=True)[:-1])
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-a', '--author',
default=gitconfig('user.name'), type=u8)
parser.add_argument(
'-e', '--email',
default=gitconfig('user.email'), type=u8)
subparsers = parser.add_subparsers()
recipes_module = import_module('recipes', package='boilerplate_data')
for k, v in recipes_module.__dict__.items():
if inspect.isclass(v) and not k.startswith('_'):
v.configure_subparser(subparsers)
args = parser.parse_args()
recipe = args.recipe(args)
recipe.generate()
if __name__ == '__main__':
main()
<%inherit file="layout.py"/>
<%inherit file="layout.pyt"/>
from weboob.browser import ${'LoginBrowser, need_login' if r.login else 'PagesBrowser'}, URL
from .pages import Page1, Page2
......
<%inherit file="layout.py"/>
<%inherit file="layout.pyt"/>
from weboob.tools.backend import Module
from .browser import ${r.classname}Browser
......
<%inherit file="layout.py"/>
<%inherit file="layout.pyt"/>
from weboob.browser.pages import HTMLPage
......
<%inherit file="layout.py"/>
<%inherit file="layout.pyt"/>
from weboob.tools.test import BackendTest
......
<%inherit file="layout.py"/>
<%inherit file="layout.pyt"/>
from weboob.tools.backend import Module${', BackendConfig' if r.login else ''}
% if login:
from weboob.tools.value import Value, ValueBackendPassword
......
<%inherit file="layout.py"/>
<%inherit file="layout.pyt"/>
from weboob.tools.capabilities.gallery.genericcomicreader import GenericComicReaderModule, DisplayPage
......
<%inherit file="layout.py"/>
<%inherit file="layout.pyt"/>
from weboob.tools.capabilities.gallery.genericcomicreadertest import GenericComicReaderTest
......
<%inherit file="layout.py"/>
<%inherit file="layout.pyt"/>
from .module import ${r.classname}Module
......
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright(C) 2013 Laurent Bachelier
# Copyright(C) 2013-2019 Laurent Bachelier, Sébastien Jean
#
# This file is part of weboob.
#
......@@ -20,82 +20,13 @@
from __future__ import print_function
import argparse
import subprocess
import datetime
import importlib
import os
import sys
import codecs
from mako.lookup import TemplateLookup
MODULE_PATH = os.getenv(
'MODULE_PATH',
os.path.realpath(os.path.join(os.path.dirname(__file__), '../modules')))
TEMPLATE_PATH = os.getenv(
'TEMPLATE_PATH',
os.path.realpath(os.path.join(os.path.dirname(__file__), 'boilerplate_data')))
VERSION = '1.5'
from recipe import _Recipe
TEMPLATES = TemplateLookup(directories=[TEMPLATE_PATH])
def u8(s):
if isinstance(s, bytes):
return s.decode('utf-8')
return s
def gitconfig(entry):
return u8(subprocess.check_output('git config -z --get %s' % entry, shell=True)[:-1])
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)
class Recipe(object):
@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(MODULE_PATH, self.name, filename), contents)
def template(self, name, **kwargs):
if '.' not in name:
name += '.py'
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()
class BaseRecipe(Recipe):
class BaseRecipe(_Recipe):
NAME = 'base'
def generate(self):
......@@ -106,7 +37,7 @@ class BaseRecipe(Recipe):
self.write('test.py', self.template('base_test'))
class CapRecipe(Recipe):
class CapRecipe(_Recipe):
NAME = 'cap'
def __init__(self, args):
......@@ -185,7 +116,7 @@ class CapRecipe(Recipe):
self.write('test.py', self.template('base_test'))
class ComicRecipe(Recipe):
class ComicRecipe(_Recipe):
NAME = 'comic'
def generate(self):
......@@ -193,7 +124,7 @@ class ComicRecipe(Recipe):
self.write('module.py', self.template('comic_module'))
class ComicTestRecipe(Recipe):
class ComicTestRecipe(_Recipe):
NAME = 'comic.test'
@classmethod
......@@ -208,26 +139,3 @@ class ComicTestRecipe(Recipe):
def generate(self):
self.write('test.py', self.template('comic_test'))
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-a', '--author',
default=gitconfig('user.name'), type=u8)
parser.add_argument(
'-e', '--email',
default=gitconfig('user.email'), type=u8)
subparsers = parser.add_subparsers()
recipes = [BaseRecipe, ComicRecipe, ComicTestRecipe, CapRecipe]
for recipe in recipes:
recipe.configure_subparser(subparsers)
args = parser.parse_args()
recipe = args.recipe(args)
recipe.generate()
if __name__ == '__main__':
main()
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright(C) 2013 Laurent Bachelier
#
# 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
import os
import datetime
import sys
import codecs
from mako.lookup import TemplateLookup
from weboob import __version__
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')))
TEMPLATES = TemplateLookup(directories=[BOILERPLATE_PATH])
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)
class _Recipe(object):
@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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment