Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add restapi_serializer #493

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions bobtemplates/plone/addon.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-

from bobtemplates.plone.base import git_commit
from bobtemplates.plone.base import git_init
from bobtemplates.plone.base import make_path
from bobtemplates.plone.utils import run_black
from bobtemplates.plone.utils import run_isort

import os
import shutil

from bobtemplates.plone.base import git_commit, git_init, make_path
from bobtemplates.plone.utils import run_black, run_isort


def pre_render(configurator):
"""Some variables to make templating easier."""
Expand Down
2 changes: 1 addition & 1 deletion bobtemplates/plone/addon/constraints_plone60.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
#certifi ; platform_system == 'Windows'

isort>=5
black==22.8.0
black==22.10.0
flake8>=5.0.4
22 changes: 13 additions & 9 deletions bobtemplates/plone/base.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# -*- coding: utf-8 -*-
from colorama import Fore
from colorama import Style
from datetime import date
from lxml import etree
from mrbob import hooks
from mrbob.bobexceptions import MrBobError
from mrbob.bobexceptions import SkipQuestion
from mrbob.bobexceptions import ValidationError
from mrbob.rendering import jinja2_env
from six.moves import input

import case_conversion as cc
import codecs
import keyword
import os
import re
import six
import string
import subprocess
import sys
from datetime import date

import case_conversion as cc
import six
from colorama import Fore, Style
from lxml import etree
from mrbob import hooks
from mrbob.bobexceptions import MrBobError, SkipQuestion, ValidationError
from mrbob.rendering import jinja2_env
from six.moves import input

try:
from ConfigParser import ConfigParser
Expand Down
13 changes: 8 additions & 5 deletions bobtemplates/plone/behavior.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
import os

import case_conversion as cc
from bobtemplates.plone.base import base_prepare_renderer
from bobtemplates.plone.base import echo
from bobtemplates.plone.base import git_commit
from bobtemplates.plone.base import update_file
from bobtemplates.plone.utils import run_black
from bobtemplates.plone.utils import run_isort
from lxml import etree

from bobtemplates.plone.base import base_prepare_renderer, echo, git_commit, update_file
from bobtemplates.plone.utils import run_black, run_isort
import case_conversion as cc
import os


def _update_package_configure_zcml(configurator):
Expand Down
8 changes: 8 additions & 0 deletions bobtemplates/plone/bobregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ def plone_restapi_service():
return reg


def plone_restapi_serializer():
reg = RegEntry()
reg.template = "bobtemplates.plone:restapi_serializer"
reg.plonecli_alias = "restapi_serializer"
reg.depend_on = "plone_addon"
return reg


def plone_svelte_app():
reg = RegEntry()
reg.template = "bobtemplates.plone:svelte_app"
Expand Down
25 changes: 12 additions & 13 deletions bobtemplates/plone/content_type.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
# -*- coding: utf-8 -*-
"""Generate content type."""

from bobtemplates.plone.base import base_prepare_renderer
from bobtemplates.plone.base import get_normalized_classname
from bobtemplates.plone.base import get_normalized_dxtypename
from bobtemplates.plone.base import get_normalized_ftiname
from bobtemplates.plone.base import git_commit
from bobtemplates.plone.base import update_file
from bobtemplates.plone.utils import run_black
from bobtemplates.plone.utils import run_isort
from lxml import etree
from mrbob.bobexceptions import SkipQuestion
from mrbob.bobexceptions import ValidationError

import keyword
import os
import re

from lxml import etree
from mrbob.bobexceptions import SkipQuestion, ValidationError

from bobtemplates.plone.base import (
base_prepare_renderer,
get_normalized_classname,
get_normalized_dxtypename,
get_normalized_ftiname,
git_commit,
update_file,
)
from bobtemplates.plone.utils import run_black, run_isort


def is_container(configurator, question):
"""Test if base class is a container."""
Expand Down
18 changes: 8 additions & 10 deletions bobtemplates/plone/controlpanel.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# -*- coding: utf-8 -*-

import os

import case_conversion as cc
from bobtemplates.plone.base import base_prepare_renderer
from bobtemplates.plone.base import git_commit
from bobtemplates.plone.base import update_file
from bobtemplates.plone.base import ZCML_NAMESPACES
from bobtemplates.plone.utils import run_black
from bobtemplates.plone.utils import run_isort
from lxml import etree

from bobtemplates.plone.base import (
ZCML_NAMESPACES,
base_prepare_renderer,
git_commit,
update_file,
)
from bobtemplates.plone.utils import run_black, run_isort
import case_conversion as cc
import os


def _update_package_configure_zcml(configurator):
Expand Down
15 changes: 7 additions & 8 deletions bobtemplates/plone/indexer.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# -*- coding: utf-8 -*-

import case_conversion as cc
from bobtemplates.plone.base import base_prepare_renderer
from bobtemplates.plone.base import git_commit
from bobtemplates.plone.base import remove_unwanted_files
from bobtemplates.plone.base import update_configure_zcml
from bobtemplates.plone.utils import run_black
from bobtemplates.plone.utils import run_isort

from bobtemplates.plone.base import (
base_prepare_renderer,
git_commit,
remove_unwanted_files,
update_configure_zcml,
)
from bobtemplates.plone.utils import run_black, run_isort
import case_conversion as cc


def _update_package_configure_zcml(configurator):
Expand Down
10 changes: 6 additions & 4 deletions bobtemplates/plone/mockup_pattern.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
import os
import re

from bobtemplates.plone.base import base_prepare_renderer
from bobtemplates.plone.base import echo
from bobtemplates.plone.base import git_commit
from bobtemplates.plone.base import git_init
from mrbob.bobexceptions import ValidationError

from bobtemplates.plone.base import base_prepare_renderer, echo, git_commit, git_init
import os
import re


def pre_render(configurator):
Expand Down
24 changes: 12 additions & 12 deletions bobtemplates/plone/portlet.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# -*- coding: utf-8 -*-
"""Generate portlet."""

from __future__ import absolute_import, print_function

import os

import case_conversion as cc
from __future__ import absolute_import
from __future__ import print_function

from bobtemplates.plone.base import base_prepare_renderer
from bobtemplates.plone.base import git_commit
from bobtemplates.plone.base import update_file
from bobtemplates.plone.base import ZCML_NAMESPACES
from bobtemplates.plone.utils import run_black
from bobtemplates.plone.utils import run_isort
from bobtemplates.plone.utils import slugify
from lxml import etree

from bobtemplates.plone.base import (
ZCML_NAMESPACES,
base_prepare_renderer,
git_commit,
update_file,
)
from bobtemplates.plone.utils import run_black, run_isort, slugify
import case_conversion as cc
import os


def _update_portlets_configure_zcml(configurator):
Expand Down
144 changes: 144 additions & 0 deletions bobtemplates/plone/restapi_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# -*- coding: utf-8 -*-

from bobtemplates.plone.base import base_prepare_renderer
from bobtemplates.plone.base import git_commit
from bobtemplates.plone.base import remove_unwanted_files
from bobtemplates.plone.base import update_configure_zcml
from lxml import etree

import case_conversion as cc


def _update_package_configure_zcml(configurator):
path = "{0}".format(configurator.variables["package_folder"])
file_name = "configure.zcml"
match_xpath = "include[@package='.api']"
match_str = "-*- extra stuff goes here -*-"
insert_str = """
<include package=".api" />
"""
update_configure_zcml(
configurator,
path,
file_name=file_name,
match_xpath=match_xpath,
match_str=match_str,
insert_str=insert_str,
)


def _update_api_configure_zcml(configurator):
path = "{0}/api".format(configurator.variables["package_folder"])
file_name = "configure.zcml"
example_file_name = "{0}.example".format(file_name)
match_xpath = "zope:include[@package='.serializers']"
match_str = "-*- extra stuff goes here -*-"
insert_str = """
<include package=".serializers" />
"""
update_configure_zcml(
configurator,
path,
file_name=file_name,
example_file_name=example_file_name,
match_xpath=match_xpath,
match_str=match_str,
insert_str=insert_str,
)


def _update_serializers_configure_zcml(configurator):
path = "{0}/api/serializers".format(configurator.variables["package_folder"])
file_name = "configure.zcml"
example_file_name = "{0}.example".format(file_name)
match_xpath = "zope:include[@package='.{0}']".format(
configurator.variables["serializer_class_name_normalized"]
)
match_str = "-*- extra stuff goes here -*-"
insert_str = '<include package=".{0}" />\n'.format(
configurator.variables["serializer_class_name_normalized"]
)
update_configure_zcml(
configurator,
path,
file_name=file_name,
example_file_name=example_file_name,
match_xpath=match_xpath,
match_str=match_str,
insert_str=insert_str,
)


def _update_metadata_xml(configurator):
"""Add plone.restapi dependency metadata.xml in
Generic Setup profiles.
"""
metadata_file_name = "metadata.xml"
metadata_file_dir = "profiles/default"
metadata_file_path = (
configurator.variables["package_folder"]
+ "/"
+ metadata_file_dir
+ "/"
+ metadata_file_name
)

with open(metadata_file_path, "r") as xml_file:
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(xml_file, parser)
dependencies = tree.xpath("/metadata/dependencies")[0]
dep = "profile-plone.restapi:default"
dep_exists = False
for e in dependencies.iter("dependency"):
dep_name = e.text
if dep_name == dep:
dep_exists = True

if dep_exists:
print("{dep} already in metadata.xml, skip adding!".format(dep=dep))
return
dep_element = etree.Element("dependency")
dep_element.text = dep
dependencies.append(dep_element)

with open(metadata_file_path, "wb") as xml_file:
tree.write(xml_file, pretty_print=True, xml_declaration=True, encoding="utf-8")


def _remove_unwanted_files(configurator):
file_paths = []
rel_file_paths = [
"/api/configure.zcml.example",
"/api/serializers/configure.zcml.example",
]
base_path = configurator.variables["package_folder"]
for rel_file_path in rel_file_paths:
file_paths.append("{0}{1}".format(base_path, rel_file_path))
remove_unwanted_files(file_paths)


def pre_renderer(configurator):
"""Pre rendering."""
configurator = base_prepare_renderer(configurator)
configurator.variables["template_id"] = "restapi_serializer"
class_name = configurator.variables["serializer_class_name"].strip("_")
configurator.variables["serializer_class_name"] = cc.pascalcase(class_name)
configurator.variables["serializer_class_name_normalized"] = cc.snakecase(
class_name
)
configurator.target_directory = configurator.variables["package_folder"]


def post_renderer(configurator):
"""Post rendering."""
_update_package_configure_zcml(configurator)
_update_api_configure_zcml(configurator)
_update_serializers_configure_zcml(configurator)
_update_metadata_xml(configurator)
_remove_unwanted_files(configurator)
git_commit(
configurator,
"Add restapi_serializer: {0}".format(
configurator.variables["serializer_class_name"]
),
)
19 changes: 19 additions & 0 deletions bobtemplates/plone/restapi_serializer/.mrbob.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[questions]
subtemplate_warning.question = Please commit your changes, before using a sub-template! Continue anyway? (y/n)
subtemplate_warning.required = True
subtemplate_warning.default = n
subtemplate_warning.pre_ask_question = bobtemplates.plone.base:git_clean_state_check
subtemplate_warning.post_ask_question = mrbob.hooks:validate_choices bobtemplates.plone.base:subtemplate_warning_post_question
subtemplate_warning.choices = y|n
subtemplate_warning.choices_delimiter = |

serializer_class_name.question = Serializer class name
serializer_class_name.help = Should be something like 'SpecialFieldSerializer' (PascalCase)
serializer_class_name.required = True
serializer_class_name.default =
serializer_class_name.post_ask_question = bobtemplates.plone.base:check_klass_name

[template]
pre_render = bobtemplates.plone.restapi_serializer:pre_renderer
post_render = bobtemplates.plone.restapi_serializer:post_renderer
post_ask = bobtemplates.plone.base:set_global_vars
Empty file.
Loading