def execute(macro, args): request = macro.request # Handle GET arguments level = '4' level_text = '7' if not args: args = '' arglist = [x.strip() for x in args.split(',') if x] if not have_cairo(): return "Cairo not found." key = cache_key(request, (macro.name, arglist)) if not cache_exists(request, key): law = None if len(arglist) == 1: level = args[0] elif len(arglist): level = args[0] if arglist[1].isdigit(): level_text = ','.join(arglist[1:]) else: level_text = '' law = ','.join(arglist[1:]) data = plot_tll(level, level_text, law) cache.put(request, key, data, content_type='image/png') f = macro.formatter divfmt = {"class": "ST"} result = f.div(1, **divfmt) result += f.image(src=cache.url(request, key), alt=LAW.format(level_text)) result += f.div(0) return result
def execute(macro, args): request = macro.request if not have_cairo(): return "Cairo not found." if not args: key = DEFAULT elif not hasattr(request.cfg, 'gwiki_markings'): return "No gwiki_markings in configuration." else: try: val = request.cfg['gwiki_markings'][args] key = list() for line in val: if not isinstance(line, unicode): return ("Marking misconfiguration " + "(not a tuple of unicode strings)") key.append((line, CAIRO_BOLD)) except KeyError: return "Marking not in gwiki_markings." level_text = ' '.join(x[0] for x in key) ckey = cache_key(request, (macro.name, key)) if not cache_exists(request, ckey): data = plot_box(key) cache.put(request, ckey, data, content_type='image/png') f = macro.formatter divfmt = {"class": "CM"} result = f.div(1, **divfmt) result += f.image(src=cache.url(request, ckey), alt=level_text) result += f.div(0) return result
def execute(macro, args): formatter = macro.formatter macro.request.page.formatter = formatter request = macro.request _ = request.getText if not have_cairo(): return formatter.text(_(\ "ERROR: Cairo Python extensions not installed. " +\ "Not performing layout.")) + formatter.linebreak() urlargs, macro_args = radarchart_args(args) pagename = request.page.page_name # The first page mentioned will be the page of the chart for arg in macro_args.split(','): page = Page(request, arg) if page.exists(): pagename = arg break return u'<div class="metaradarchart">' + \ u'<img src="%s">' % url_construct(request, urlargs, pagename) + \ u'</div>'
def execute(macro, args): formatter = macro.formatter macro.request.page.formatter = formatter request = macro.request _ = request.getText if not have_cairo(): return _sysmsg % ('error', _(\ "ERROR: Cairo Python extensions not installed. " +\ "Not performing layout.")) url_args, args = radarchart_args(args) # For multiple radar charts per table row try: height = ''.join(url_args.get('height', list())) width = ''.join(url_args.get('width', list())) if not height: height = MAX_WIDTH if not width: width = MAX_WIDTH height, width = int(height), int(width) except ValueError: pass # MAX_WIDTH is the assumed max_width here amount = MAX_WIDTH / min(height, width) if amount < 1: amount = 1 # Note, metatable_parseargs deals with permissions pagelist, metakeys, _ = metatable_parseargs(request, args, get_all_keys=True) values = set() for page in pagelist: metas = get_metas(request, page, metakeys) for key in metas: # Get the maximum value of each key on a page if metas[key]: numberedvals = dict() for i, val in enumerate(map(ordervalue, metas[key])): numberedvals[val] = i maxval = max(numberedvals.keys()) i = numberedvals[maxval] # This contraption is here because we need to deliver # unparsed (textual) values in urls values.add(metas[key][i]) for val in values: if val.startswith('attachment'): # A bit ugly fix for a weird corner case val = "attachment:%s" % (val[11:]) url_args.setdefault('value', list()).append(val) out = StringIO.StringIO() out.write(macro.formatter.linebreak() + u'<div class="metaradartable">' + macro.formatter.table(1)) rowcount = (len(pagelist) / amount) if len(pagelist) % amount: rowcount += 1 # Iterate over the number of rows for i in range(rowcount): out.write(macro.formatter.table_row(1)) pages = pagelist[i * amount:(i + 1) * amount] # First enter page names to first row for page in pages: out.write(macro.formatter.table_cell(1, {'class': 'meta_page'})) out.write(macro.formatter.pagelink(1, page)) out.write(macro.formatter.text(page)) out.write(macro.formatter.pagelink(0)) out.write(macro.formatter.linebreak()) # Don't make extra squares for the first row if i: for j in range(amount - len(pages)): out.write(macro.formatter.table_cell(1)) out.write(macro.formatter.table_row(1)) # Chart images to the other row for page in pages: out.write(macro.formatter.table_cell(1, {'class': 'meta_radar'})) out.write(u'<img src="%s">' % (url_construct(request, url_args, page))) out.write(macro.formatter.linebreak()) if i: for j in range(amount - len(pages)): out.write(macro.formatter.table_cell(1)) out.write(macro.formatter.table(0) + u'</div>') return out.getvalue()
def execute(pagename, request): if not have_cairo(): error = request.getText( "ERROR: Cairo Python extensions not installed. " + "Not performing layout." ) request.content_type = 'text/plain' request.write(error) return request.content_type = "image/png" # Grab arguments args = ', '.join(x for x in request.values.getlist('arg')) params = {'height': 0, 'width': 0} # Height and Width for attr in ['height', 'width']: if request.values.has_key(attr): val = ''.join(request.values.getlist(attr)) try: params[attr] = int(val) except ValueError: pass # Aggregate set of included values of a page values = set() if request.values.has_key('value'): values.update(map(ordervalue, request.values.getlist('value'))) # Values that need to be included to form a complete scale, say 1-10 scale = list() if request.values.has_key('scale'): scale = request.values['scale'] if not params['height'] and params['width']: params['height'] = params['width'] elif params['height'] and not params['width']: params['width'] = params['height'] elif not params['height'] and not params['width']: params['width'] = params['height'] = 1000 # calculate center and radius, leave some room for text and stuff center = (params['height'] / 2, params['width'] / 2) radius = min(center) if radius > 50: radius -= 50 # Setup Cairo surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, params['height'], params["width"]) ctx = cairo.Context(surface) ctx.select_font_face("Times-Roman", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) ctx.set_font_size(12) ctx.set_source_rgb(1.0, 1.0, 1.0) ctx.rectangle(0, 0, params['width'], params['height']) ctx.fill() ctx.set_source_rgb(0.0, 0.0, 0.0) # Note, metatable_parseargs deals with permissions pagelist, metakeys, _ = metatable_parseargs(request, args, get_all_keys=True) metakeys = filter(lambda x: x not in SPECIAL_ATTRS, metakeys) # If no keys, print nothing if not pagelist: request.write(plot_error(request)) return # Populate data to the radar chart data = dict() for page in pagelist: # On links, we want a list of page names, not their markup metas = get_metas(request, page, metakeys, checkAccess=False) # Get the maximum value of each key on a page for key in metakeys: data.setdefault(key, list()) if metas[key]: scale.extend(map(ordervalue, metas[key])) if page == pagename: data[key].append(max(map(ordervalue, metas[key]))) # Get values for the chart axes data_per_axis = dict() for axis, key in enumerate(metakeys): data_per_axis[axis] = key if not values: for x in data.values(): values.update(x) if scale: values.update(scale) values = sorted(values) # Refuse to draw if no values for any key if not len(values): request.write(plot_error(request)) return no_values = len(values) + 1 per_value = radius / no_values sectors = len(data) angle = 2*math.pi/sectors cur_radius = per_value # Make the base grid for x in range(1, no_values): ctx, gridpoints = spider_radius(ctx, center, cur_radius, sectors) cur_radius += per_value # Apply ink from strokes so far ctx.stroke() # Now start to make chart on top of the base ctx.set_source_rgb(50/255.0,137/255.0,37/255.0) ctx.set_line_width(5) endpoints = list() # Find coords for each value for i in range(sectors): val = data[data_per_axis[i]] if val: val = val.pop() radius = values.index(val) + 1 else: # If no values exist, it's in the bottom radius = 0 x, y = spider_coords(radius * per_value, i*angle) endpoints.append(add_to_center(center, (x, y))) draw_path(ctx, endpoints) # Draw path filling the contents ctx.stroke_preserve() ctx.set_source_rgba(150/255.0,190/255.0,13/255.0, 0.2) ctx.fill() # Write axis names on top of it all for point, axis in zip(gridpoints, data_per_axis.keys()): text = data_per_axis[axis] ctx.set_source_rgba(1, 1, 1, 0.9) width, height = ctx.text_extents(text)[2:4] # Move texts on the left side a bit left if point[0] < center[0]: point = (point[0] - width, point[1]) width, height = width * 1.2, -height * 1.2 x, y = point[0] - 0.1 * width, point[1] + 0.1 * height ctx.rectangle(x, y, width, height) ctx.fill() ctx.set_source_rgb(0, 0, 0) ctx.move_to(*point) ctx.show_text(text) request.write(cairo_surface_to_png(surface))
def execute(pagename, request): if not have_cairo(): error = request.getText( "ERROR: Cairo Python extensions not installed. " + "Not performing layout.") request.content_type = 'text/plain' request.write(error) return request.content_type = "image/png" # Grab arguments args = ', '.join(x for x in request.values.getlist('arg')) params = {'height': 0, 'width': 0} # Height and Width for attr in ['height', 'width']: if request.values.has_key(attr): val = ''.join(request.values.getlist(attr)) try: params[attr] = int(val) except ValueError: pass # Aggregate set of included values of a page values = set() if request.values.has_key('value'): values.update(map(ordervalue, request.values.getlist('value'))) # Values that need to be included to form a complete scale, say 1-10 scale = list() if request.values.has_key('scale'): scale = request.values['scale'] if not params['height'] and params['width']: params['height'] = params['width'] elif params['height'] and not params['width']: params['width'] = params['height'] elif not params['height'] and not params['width']: params['width'] = params['height'] = 1000 # calculate center and radius, leave some room for text and stuff center = (params['height'] / 2, params['width'] / 2) radius = min(center) if radius > 50: radius -= 50 # Setup Cairo surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, params['height'], params["width"]) ctx = cairo.Context(surface) ctx.select_font_face("Times-Roman", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) ctx.set_font_size(12) ctx.set_source_rgb(1.0, 1.0, 1.0) ctx.rectangle(0, 0, params['width'], params['height']) ctx.fill() ctx.set_source_rgb(0.0, 0.0, 0.0) # Note, metatable_parseargs deals with permissions pagelist, metakeys, _ = metatable_parseargs(request, args, get_all_keys=True) metakeys = filter(lambda x: x not in SPECIAL_ATTRS, metakeys) # If no keys, print nothing if not pagelist: request.write(plot_error(request)) return # Populate data to the radar chart data = dict() for page in pagelist: # On links, we want a list of page names, not their markup metas = get_metas(request, page, metakeys, checkAccess=False) # Get the maximum value of each key on a page for key in metakeys: data.setdefault(key, list()) if metas[key]: scale.extend(map(ordervalue, metas[key])) if page == pagename: data[key].append(max(map(ordervalue, metas[key]))) # Get values for the chart axes data_per_axis = dict() for axis, key in enumerate(metakeys): data_per_axis[axis] = key if not values: for x in data.values(): values.update(x) if scale: values.update(scale) values = sorted(values) # Refuse to draw if no values for any key if not len(values): request.write(plot_error(request)) return no_values = len(values) + 1 per_value = radius / no_values sectors = len(data) angle = 2 * math.pi / sectors cur_radius = per_value # Make the base grid for x in range(1, no_values): ctx, gridpoints = spider_radius(ctx, center, cur_radius, sectors) cur_radius += per_value # Apply ink from strokes so far ctx.stroke() # Now start to make chart on top of the base ctx.set_source_rgb(50 / 255.0, 137 / 255.0, 37 / 255.0) ctx.set_line_width(5) endpoints = list() # Find coords for each value for i in range(sectors): val = data[data_per_axis[i]] if val: val = val.pop() radius = values.index(val) + 1 else: # If no values exist, it's in the bottom radius = 0 x, y = spider_coords(radius * per_value, i * angle) endpoints.append(add_to_center(center, (x, y))) draw_path(ctx, endpoints) # Draw path filling the contents ctx.stroke_preserve() ctx.set_source_rgba(150 / 255.0, 190 / 255.0, 13 / 255.0, 0.2) ctx.fill() # Write axis names on top of it all for point, axis in zip(gridpoints, data_per_axis.keys()): text = data_per_axis[axis] ctx.set_source_rgba(1, 1, 1, 0.9) width, height = ctx.text_extents(text)[2:4] # Move texts on the left side a bit left if point[0] < center[0]: point = (point[0] - width, point[1]) width, height = width * 1.2, -height * 1.2 x, y = point[0] - 0.1 * width, point[1] + 0.1 * height ctx.rectangle(x, y, width, height) ctx.fill() ctx.set_source_rgb(0, 0, 0) ctx.move_to(*point) ctx.show_text(text) request.write(cairo_surface_to_png(surface))
@license: MIT <http://www.opensource.org/licenses/mit-license.php> """ from MoinMoin.action import cache from graphingwiki import cairo, have_cairo, cairo_surface_to_png from graphingwiki.util import cache_key, cache_exists LEVELS = {'2': 'SALAINEN', '3': 'LUOTTAMUKSELLINEN', '4': 'KÄYTTÖ RAJOITETTU'} LEVELSROMAN = {'4': 'IV', '3': 'III', '2': 'II'} LAW = u"JulkL (621/1999) 24.1 \xa7:n {0} k" if have_cairo(): CAIRO_BOLD = ("sans-serif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) CAIRO_NORMAL = ("sans-serif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) def plot_box(texts): # Make a context to calculate font sizes with # There must be a better way to do this, I just don't know it! surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) ctx = cairo.Context(surface) ctx.select_font_face("sans-serif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) ctx.set_font_size(15)
@copyright: 2009, 2011 by Juhani Eronen <*****@*****.**> @license: MIT <http://www.opensource.org/licenses/mit-license.php> """ from MoinMoin.action import cache from graphingwiki import cairo, have_cairo, cairo_surface_to_png from graphingwiki.util import cache_key, cache_exists LEVELS = {'2': 'SALAINEN', '3': 'LUOTTAMUKSELLINEN', '4': 'KÄYTTÖ RAJOITETTU'} LEVELSROMAN = {'4': 'IV', '3': 'III', '2': 'II'} LAW = u"JulkL (621/1999) 24.1 \xa7:n {0} k" if have_cairo(): CAIRO_BOLD = ("sans-serif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) CAIRO_NORMAL = ("sans-serif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) def plot_box(texts): # Make a context to calculate font sizes with # There must be a better way to do this, I just don't know it! surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0) ctx = cairo.Context(surface) ctx.select_font_face("sans-serif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) ctx.set_font_size(15)
def execute(macro, args): formatter = macro.formatter macro.request.page.formatter = formatter request = macro.request _ = request.getText if not have_cairo(): return _sysmsg % ('error', _(\ "ERROR: Cairo Python extensions not installed. " +\ "Not performing layout.")) url_args, args = radarchart_args(args) # For multiple radar charts per table row try: height = ''.join(url_args.get('height', list())) width = ''.join(url_args.get('width', list())) if not height: height = MAX_WIDTH if not width: width = MAX_WIDTH height, width = int(height), int(width) except ValueError: pass # MAX_WIDTH is the assumed max_width here amount = MAX_WIDTH / min(height, width) if amount < 1: amount = 1 # Note, metatable_parseargs deals with permissions pagelist, metakeys, _ = metatable_parseargs(request, args, get_all_keys=True) values = set() for page in pagelist: metas = get_metas(request, page, metakeys) for key in metas: # Get the maximum value of each key on a page if metas[key]: numberedvals = dict() for i, val in enumerate(map(ordervalue, metas[key])): numberedvals[val] = i maxval = max(numberedvals.keys()) i = numberedvals[maxval] # This contraption is here because we need to deliver # unparsed (textual) values in urls values.add(metas[key][i]) for val in values: if val.startswith('attachment'): # A bit ugly fix for a weird corner case val = "attachment:%s" % (val[11:]) url_args.setdefault('value', list()).append(val) out = StringIO.StringIO() out.write(macro.formatter.linebreak() + u'<div class="metaradartable">' + macro.formatter.table(1)) rowcount = (len(pagelist) / amount) if len(pagelist) % amount: rowcount += 1 # Iterate over the number of rows for i in range(rowcount): out.write(macro.formatter.table_row(1)) pages = pagelist[i*amount:(i+1)*amount] # First enter page names to first row for page in pages: out.write(macro.formatter.table_cell(1, {'class': 'meta_page'})) out.write(macro.formatter.pagelink(1, page)) out.write(macro.formatter.text(page)) out.write(macro.formatter.pagelink(0)) out.write(macro.formatter.linebreak()) # Don't make extra squares for the first row if i: for j in range(amount - len(pages)): out.write(macro.formatter.table_cell(1)) out.write(macro.formatter.table_row(1)) # Chart images to the other row for page in pages: out.write(macro.formatter.table_cell(1, {'class': 'meta_radar'})) out.write(u'<img src="%s">' % (url_construct(request, url_args, page))) out.write(macro.formatter.linebreak()) if i: for j in range(amount - len(pages)): out.write(macro.formatter.table_cell(1)) out.write(macro.formatter.table(0) + u'</div>') return out.getvalue()