| 1 | """ |
|---|
| 2 | Local templates for the qplone3_theme |
|---|
| 3 | """ |
|---|
| 4 | import os, sys, re, datetime |
|---|
| 5 | from ConfigParser import SafeConfigParser |
|---|
| 6 | from paste.script import pluginlib |
|---|
| 7 | |
|---|
| 8 | from zopeskel.base import var |
|---|
| 9 | from zopeskel.localcommands import ZopeSkelLocalTemplate |
|---|
| 10 | from qthemetemplate.localcommands import QThemeSubTemplate |
|---|
| 11 | |
|---|
| 12 | RESP = re.compile("\s+") |
|---|
| 13 | REBAN = re.compile("[\\\/\,\.\+\-\*\%\~\@\#\$\^\&\|\;\:\?\(\)\=\[\]\{\}\_]+") |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | class SkinLayerSubTemplate(QThemeSubTemplate): |
|---|
| 17 | """ |
|---|
| 18 | A Plone Skin layer skeleton |
|---|
| 19 | """ |
|---|
| 20 | _template_dir = 'templates/skinlayer' |
|---|
| 21 | summary = "A Plone 3 Skin Layer" |
|---|
| 22 | |
|---|
| 23 | compo_template_markers = [ |
|---|
| 24 | ('layer4Skin', 'extra layer stuff goes here'), |
|---|
| 25 | ] |
|---|
| 26 | |
|---|
| 27 | vars = [ |
|---|
| 28 | var('layer_name', 'Skin Layer name (should not contain spaces)', |
|---|
| 29 | default="skin_layer"), |
|---|
| 30 | var('insert_type', 'Where insert the layer ("after" or "before")', default="after"), |
|---|
| 31 | var('insert_control_layer', |
|---|
| 32 | 'Layer after or before which your layer will be inserted, "*" accepted, which mean all', |
|---|
| 33 | default='custom'), |
|---|
| 34 | ] |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | class CSSSubTemplate(QThemeSubTemplate): |
|---|
| 38 | """ |
|---|
| 39 | A Plone CSS resource skeleton |
|---|
| 40 | """ |
|---|
| 41 | _template_dir = 'templates/cssresource' |
|---|
| 42 | summary = "A Plone 3 CSS resource template" |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | vars = [ |
|---|
| 46 | var('css_resource_name', 'Name of CSS resource', |
|---|
| 47 | default="main.css"), |
|---|
| 48 | var('css_file_path', 'Path to CSS file. If no path-empty file will be created.', |
|---|
| 49 | default=''), |
|---|
| 50 | var('cssreg_media', 'Optional.Possible values:screen,print,projection,handheld', |
|---|
| 51 | default="screen", ), |
|---|
| 52 | var('cssreg_rel', 'Optional', default="stylesheet"), |
|---|
| 53 | var('cssreg_rendering', 'Optional.Possible values:import,link,inline', default="inline"), |
|---|
| 54 | var('cssreg_cacheable', '', default="True"), |
|---|
| 55 | var('cssreg_compression', 'Compression type', default="safe"), |
|---|
| 56 | var('cssreg_cookable', 'Boolean, aka merging allowed', default="True"), |
|---|
| 57 | var('cssreg_enables', 'Optional.Boolean', default="1"), |
|---|
| 58 | var('cssreg_expression', 'Optional.A tal condition.', default=""), |
|---|
| 59 | ] |
|---|
| 60 | |
|---|
| 61 | def pre(self, command, output_dir, vars): |
|---|
| 62 | """ Set 'css_resource_content' value from css_file_path |
|---|
| 63 | """ |
|---|
| 64 | super(CSSSubTemplate, self).pre(command, output_dir, vars) |
|---|
| 65 | |
|---|
| 66 | vars['css_resource_content'] = '' |
|---|
| 67 | if os.path.isfile(vars['css_file_path']): |
|---|
| 68 | vars['css_resource_content'] = file(vars['css_file_path'],'rb').read() |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | class JSSubTemplate(QThemeSubTemplate): |
|---|
| 72 | """ |
|---|
| 73 | A Plone JS resource skeleton |
|---|
| 74 | """ |
|---|
| 75 | _template_dir = 'templates/jsresource' |
|---|
| 76 | summary = "A Plone 3 JS resource template" |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | vars = [ |
|---|
| 80 | var('js_resource_name', 'Name of JS resource', default="foo.js"), |
|---|
| 81 | var('js_file_path', 'Path to JS file. If no path - empty file will be created', |
|---|
| 82 | default=''), |
|---|
| 83 | var('jsreg_inline', 'Optional.Boolean', default="False"), |
|---|
| 84 | var('jsreg_cacheable', '', default="True"), |
|---|
| 85 | var('jsreg_compression', 'Compression type.Possible:none,safe,full,safe-encode,full-encode', |
|---|
| 86 | default="safe"), |
|---|
| 87 | var('jsreg_cookable', 'Boolean, aka merging allowed', default="True"), |
|---|
| 88 | var('jsreg_enables', 'Optional.Boolean', default="1"), |
|---|
| 89 | var('jsreg_expression', 'Optional.A tal condition.', default=""), |
|---|
| 90 | ] |
|---|
| 91 | |
|---|
| 92 | def pre(self, command, output_dir, vars): |
|---|
| 93 | """ Set 'js_resource_content' value from js_file_path |
|---|
| 94 | """ |
|---|
| 95 | super(JSSubTemplate, self).pre(command, output_dir, vars) |
|---|
| 96 | |
|---|
| 97 | vars['js_resource_content'] = '' |
|---|
| 98 | if os.path.isfile(vars['js_file_path']): |
|---|
| 99 | vars['js_resource_content'] = file(vars['js_file_path'],'rb').read() |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | class ViewletOrderSubTemplate(QThemeSubTemplate): |
|---|
| 103 | """ |
|---|
| 104 | A Plone Order Viewlet skeleton |
|---|
| 105 | """ |
|---|
| 106 | _template_dir = 'templates/viewlet' |
|---|
| 107 | summary = "A Plone 3 Order Viewlet template" |
|---|
| 108 | |
|---|
| 109 | # list of 2 item tuple - |
|---|
| 110 | # (compotemplate_name, compo marker), for ex.: |
|---|
| 111 | compo_template_markers = [] |
|---|
| 112 | |
|---|
| 113 | shared_vars = ['viewlet_profile_marker',] |
|---|
| 114 | |
|---|
| 115 | vars = [ |
|---|
| 116 | var('viewlet_name', "Viewlet name", default='example'), |
|---|
| 117 | var('viewlet_manager_interface', "Viewlet manager interface", |
|---|
| 118 | default="plone.app.layout.viewlets.interfaces.IPortalHeader"), |
|---|
| 119 | var('viewlet_manager_name', "Viewlet manager name", default='plone.portalheader'), |
|---|
| 120 | var('viewlet_permission', "Viewlet permission", default="zope2.View"), |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | var('insert_type', 'Where insert the viewlet ("after" or "before")', default="after"), |
|---|
| 124 | var('insert_control_viewlet', 'Viewlet after or before which your viewlet will be inserted, ' \ |
|---|
| 125 | '"*" accepted, which mean all', default='*'), |
|---|
| 126 | |
|---|
| 127 | var('layer_interface', "Layer interface for registry this viewlet on", |
|---|
| 128 | default=".interfaces.IThemeSpecific"), |
|---|
| 129 | var('layer_name', "Layer name for registry this viewlet on", default=""), |
|---|
| 130 | #var('skinname', "Skin name, for bind viewlet to, '*' - mean for all", default=""), |
|---|
| 131 | #var('skinbase', "Base skin, for get viewlets from", default=""), |
|---|
| 132 | ] |
|---|
| 133 | |
|---|
| 134 | def pre(self, command, output_dir, vars): |
|---|
| 135 | """ Set 'css_resource_content' value from css_file_path |
|---|
| 136 | """ |
|---|
| 137 | super(ViewletOrderSubTemplate, self).pre(command, output_dir, vars) |
|---|
| 138 | |
|---|
| 139 | vn_lower_nospc = RESP.sub('',vars['viewlet_name']).lower() |
|---|
| 140 | vn_lower_under = RESP.sub('_',vars['viewlet_name']).lower() |
|---|
| 141 | VnCamel = ''.join([i.capitalize() for i in REBAN.sub(' ',vars['viewlet_name']).split()]) |
|---|
| 142 | vars['viewlet_class_name'] = VnCamel |
|---|
| 143 | vars['viewlet_interface_name'] = "I"+VnCamel |
|---|
| 144 | vars['viewlet_template_name'] = vn_lower_nospc+'_viewlet.pt' |
|---|
| 145 | |
|---|
| 146 | viewlet_profile_marker = "[order_%s] viewlet stuff goes here" % \ |
|---|
| 147 | '.'.join([vars['viewlet_manager_name'], vars['qplone3_theme_skinname'], |
|---|
| 148 | vars['qplone3_theme_skinbase']]) |
|---|
| 149 | |
|---|
| 150 | vars['add_order_tag'] = self.add_order_tag(output_dir, vars, viewlet_profile_marker) |
|---|
| 151 | vars['viewlet_profile_marker'] = viewlet_profile_marker |
|---|
| 152 | self.compo_template_markers.append( |
|---|
| 153 | ('viewlet_profiles',viewlet_profile_marker)) |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | def add_order_tag(self, output_dir, vars, pmarker): |
|---|
| 157 | need_update = True |
|---|
| 158 | egg_info = pluginlib.find_egg_info_dir(output_dir) |
|---|
| 159 | theme_vars_fp = os.path.join(egg_info, 'theme_vars.txt') |
|---|
| 160 | |
|---|
| 161 | if egg_info and os.path.exists(theme_vars_fp): |
|---|
| 162 | config = SafeConfigParser() |
|---|
| 163 | config.read(theme_vars_fp) |
|---|
| 164 | |
|---|
| 165 | sec, opt = 'qplone3_theme', 'used_subtemplates' |
|---|
| 166 | used_subtemplates = filter(None,[st.strip() \ |
|---|
| 167 | for st in config.get(sec,opt).split(',')]) |
|---|
| 168 | |
|---|
| 169 | if self.name in used_subtemplates: |
|---|
| 170 | sections = [self.name,] |
|---|
| 171 | if config.has_section('multiple_templates') and \ |
|---|
| 172 | config.has_option('multiple_templates',self.name): |
|---|
| 173 | ms_sections = config.get('multiple_templates',self.name) |
|---|
| 174 | sections = [s.strip() for s in ms_sections.split(',')] |
|---|
| 175 | |
|---|
| 176 | pmarkers = [config.get(sec, 'viewlet_profile_marker') \ |
|---|
| 177 | for sec in sections] |
|---|
| 178 | if pmarker in pmarkers: |
|---|
| 179 | need_update = False |
|---|
| 180 | |
|---|
| 181 | return need_update |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | class ViewletHiddenSubTemplate(QThemeSubTemplate): |
|---|
| 185 | """ |
|---|
| 186 | A Plone Hidden Viewlet skeleton |
|---|
| 187 | """ |
|---|
| 188 | _template_dir = 'templates/viewlet_hidden' |
|---|
| 189 | summary = "A Plone 3 Hidden Viewlet template" |
|---|
| 190 | |
|---|
| 191 | shared_vars = ['viewlet_profile_marker',] |
|---|
| 192 | |
|---|
| 193 | compo_template_markers = [] |
|---|
| 194 | |
|---|
| 195 | vars = [ |
|---|
| 196 | var('viewlet_name', "Viewlet name", default='plone.global_sections'), |
|---|
| 197 | var('viewlet_manager_name', "Viewlet manager name", default='plone.portalheader'), |
|---|
| 198 | #var('skinname', "Skin name, for bind viewlet to, may be '*'", default=""), |
|---|
| 199 | ] |
|---|
| 200 | |
|---|
| 201 | def pre(self, command, output_dir, vars): |
|---|
| 202 | """ Set 'css_resource_content' value from css_file_path |
|---|
| 203 | """ |
|---|
| 204 | super(ViewletHiddenSubTemplate, self).pre(command, output_dir, vars) |
|---|
| 205 | |
|---|
| 206 | viewlet_profile_marker = "[hidden_%s] viewlet stuff goes here" % \ |
|---|
| 207 | '.'.join([vars['viewlet_manager_name'], vars['qplone3_theme_skinname']]) |
|---|
| 208 | |
|---|
| 209 | vars['add_hidden_tag'] = self.add_hidden_tag(output_dir, vars, viewlet_profile_marker) |
|---|
| 210 | vars['viewlet_profile_marker'] = viewlet_profile_marker |
|---|
| 211 | self.compo_template_markers.append( |
|---|
| 212 | ('viewlet_hidden_profiles',viewlet_profile_marker)) |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | def add_hidden_tag(self, output_dir, vars, pmarker): |
|---|
| 216 | add_hidden = True |
|---|
| 217 | egg_info = pluginlib.find_egg_info_dir(output_dir) |
|---|
| 218 | theme_vars_fp = os.path.join(egg_info, 'theme_vars.txt') |
|---|
| 219 | |
|---|
| 220 | if egg_info and os.path.exists(theme_vars_fp): |
|---|
| 221 | config = SafeConfigParser() |
|---|
| 222 | config.read(theme_vars_fp) |
|---|
| 223 | |
|---|
| 224 | sec, opt = 'qplone3_theme', 'used_subtemplates' |
|---|
| 225 | used_subtemplates = filter(None,[st.strip() \ |
|---|
| 226 | for st in config.get(sec,opt).split(',')]) |
|---|
| 227 | |
|---|
| 228 | if self.name in used_subtemplates: |
|---|
| 229 | sections = [self.name,] |
|---|
| 230 | if config.has_section('multiple_templates') and \ |
|---|
| 231 | config.has_option('multiple_templates',self.name): |
|---|
| 232 | ms_sections = config.get('multiple_templates',self.name) |
|---|
| 233 | sections = [s.strip() for s in ms_sections.split(',')] |
|---|
| 234 | |
|---|
| 235 | pmarkers = [config.get(sec, 'viewlet_profile_marker') \ |
|---|
| 236 | for sec in sections] |
|---|
| 237 | if pmarker in pmarkers: |
|---|
| 238 | add_hidden = False |
|---|
| 239 | return add_hidden |
|---|
| 240 | |
|---|
| 241 | class ImportSubTemplate(QThemeSubTemplate): |
|---|
| 242 | """ |
|---|
| 243 | A skeleton for importing zexp objects into portal on theme installation |
|---|
| 244 | """ |
|---|
| 245 | _template_dir = 'templates/importing' |
|---|
| 246 | summary = "A template for importing zexp-objects into portal on installation" |
|---|
| 247 | |
|---|
| 248 | def pre(self, command, output_dir, vars): |
|---|
| 249 | """ Set timestamp var for generate import_steps profile id |
|---|
| 250 | """ |
|---|
| 251 | super(ImportSubTemplate, self).pre(command, output_dir, vars) |
|---|
| 252 | |
|---|
| 253 | vars['timestamp'] = datetime.date.today().strftime("%Y%m%d") |
|---|
| 254 | vars['already_used'] = self.already_used(vars) |
|---|
| 255 | |
|---|
| 256 | def already_used(self, vars): |
|---|
| 257 | used = vars.get('qplone3_theme_used_subtemplates','').split(',') |
|---|
| 258 | return self.name in filter(None,[st.strip() for st in used]) |
|---|