Commits

Charlie Clark committed 6277c61

Separate grouping from rules.

Comments (0)

Files changed (2)

openpyxl/formatting/__init__.py

+from __future__ import absolute_import
+# Copyright (c) 2010-2014 openpyxl
 
+from openpyxl.compat import iteritems, OrderedDict
+
+from .rules import CellIsRule, ColorScaleRule, FormatRule
+
+
+class ConditionalFormatting(object):
+    """Conditional formatting rules."""
+    rule_attributes = ('aboveAverage', 'bottom', 'dxfId', 'equalAverage', 'operator', 'percent', 'priority', 'rank',
+                       'stdDev', 'stopIfTrue', 'text')
+    icon_attributes = ('iconSet', 'showValue', 'reverse')
+
+    def __init__(self):
+        self.cf_rules = OrderedDict()
+        self.max_priority = 0
+        self.parse_rules = {}
+
+    def add(self, range_string, cfRule):
+        """Add a rule.  Rule is either:
+         1. A dictionary containing a key called type, and other keys, as in `ConditionalFormatting.rule_attributes`.
+         2. A rule object, such as ColorScaleRule, FormulaRule or CellIsRule
+
+         The priority will be added automatically.
+        """
+        if isinstance(cfRule, dict):
+            rule = cfRule
+        else:
+            rule = cfRule.rule
+        rule['priority'] = self.max_priority + 1
+        self.max_priority += 1
+        if range_string not in self.cf_rules:
+            self.cf_rules[range_string] = []
+        self.cf_rules[range_string].append(rule)
+
+    def update(self, cfRules):
+        """Set the conditional formatting rules from a dictionary.  Intended for use when loading a document.
+        cfRules use the structure: {range_string: [rule1, rule2]}, eg:
+        {'A1:A4': [{'type': 'colorScale', 'priority': 13, 'colorScale': {'cfvo': [{'type': 'min'}, {'type': 'max'}],
+        'color': [Color('FFFF7128'), Color('FFFFEF9C')]}]}
+        """
+        for range_string, rules in iteritems(cfRules):
+            if range_string not in self.cf_rules:
+                self.cf_rules[range_string] = rules
+            else:
+                self.cf_rules[range_string] += rules
+
+        # Fix any gap in the priority range.
+        self.max_priority = 0
+        priorityMap = []
+        for range_string, rules in iteritems(self.cf_rules):
+            for rule in rules:
+                priorityMap.append(rule['priority'])
+        priorityMap.sort()
+        for range_string, rules in iteritems(self.cf_rules):
+            for rule in rules:
+                priority = priorityMap.index(rule['priority']) + 1
+                rule['priority'] = priority
+                if 'priority' in rule and priority > self.max_priority:
+                    self.max_priority = priority
+
+    def setDxfStyles(self, wb):
+        """Formatting for non color scale conditional formatting uses the dxf style list in styles.xml. This scans
+        the cf_rules for dxf styles which have not been added - and saves them to the workbook.
+
+        When adding a conditional formatting rule that uses a font, border or fill, this must be called at least once
+        before saving the workbook.
+
+        :param wb: the workbook
+        """
+        if not wb.style_properties:
+            wb.style_properties = {'dxf_list': []}
+        elif 'dxf_list' not in wb.style_properties:
+            wb.style_properties['dxf_list'] = []
+
+        for rules in self.cf_rules.values():
+            for rule in rules:
+                if 'dxf' in rule:
+                    dxf = {}
+                    if 'font' in rule['dxf'] and isinstance(rule['dxf']['font'], Font):
+                        # DXF font is limited to color, bold, italic, underline and strikethrough
+                        dxf['font'] = rule['dxf']['font']
+                    if 'border' in rule['dxf'] and isinstance(rule['dxf']['border'], Borders):
+                        dxf['border'] = rule['dxf']['border']
+                    if 'fill' in rule['dxf'] and isinstance(rule['dxf']['fill'], Fill):
+                        dxf['fill'] = rule['dxf']['fill']
+
+                    wb.style_properties['dxf_list'].append(dxf)
+                    rule.pop('dxf')
+                    rule['dxfId'] = len(wb.style_properties['dxf_list']) - 1
+

openpyxl/formatting/rules.py

 from __future__ import absolute_import
 # Copyright (c) 2010-2014 openpyxl
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-#
-# @license: http://www.opensource.org/licenses/mit-license.php
-# @author: see AUTHORS file
+
 
 from collections import Mapping
 
-from openpyxl.compat import iteritems, OrderedDict
 from openpyxl.styles import Font, Fill, Borders
 
 
         return r
 
 
-class ConditionalFormatting(object):
-    """Conditional formatting rules."""
-    rule_attributes = ('aboveAverage', 'bottom', 'dxfId', 'equalAverage', 'operator', 'percent', 'priority', 'rank',
-                       'stdDev', 'stopIfTrue', 'text')
-    icon_attributes = ('iconSet', 'showValue', 'reverse')
-
-    def __init__(self):
-        self.cf_rules = OrderedDict()
-        self.max_priority = 0
-        self.parse_rules = {}
-
-    def add(self, range_string, cfRule):
-        """Add a rule.  Rule is either:
-         1. A dictionary containing a key called type, and other keys, as in `ConditionalFormatting.rule_attributes`.
-         2. A rule object, such as ColorScaleRule, FormulaRule or CellIsRule
-
-         The priority will be added automatically.
-        """
-        if isinstance(cfRule, dict):
-            rule = cfRule
-        else:
-            rule = cfRule.rule
-        rule['priority'] = self.max_priority + 1
-        self.max_priority += 1
-        if range_string not in self.cf_rules:
-            self.cf_rules[range_string] = []
-        self.cf_rules[range_string].append(rule)
-
-    def update(self, cfRules):
-        """Set the conditional formatting rules from a dictionary.  Intended for use when loading a document.
-        cfRules use the structure: {range_string: [rule1, rule2]}, eg:
-        {'A1:A4': [{'type': 'colorScale', 'priority': 13, 'colorScale': {'cfvo': [{'type': 'min'}, {'type': 'max'}],
-        'color': [Color('FFFF7128'), Color('FFFFEF9C')]}]}
-        """
-        for range_string, rules in iteritems(cfRules):
-            if range_string not in self.cf_rules:
-                self.cf_rules[range_string] = rules
-            else:
-                self.cf_rules[range_string] += rules
-
-        # Fix any gap in the priority range.
-        self.max_priority = 0
-        priorityMap = []
-        for range_string, rules in iteritems(self.cf_rules):
-            for rule in rules:
-                priorityMap.append(rule['priority'])
-        priorityMap.sort()
-        for range_string, rules in iteritems(self.cf_rules):
-            for rule in rules:
-                priority = priorityMap.index(rule['priority']) + 1
-                rule['priority'] = priority
-                if 'priority' in rule and priority > self.max_priority:
-                    self.max_priority = priority
-
-    def setDxfStyles(self, wb):
-        """Formatting for non color scale conditional formatting uses the dxf style list in styles.xml. This scans
-        the cf_rules for dxf styles which have not been added - and saves them to the workbook.
-
-        When adding a conditional formatting rule that uses a font, border or fill, this must be called at least once
-        before saving the workbook.
-
-        :param wb: the workbook
-        """
-        if not wb.style_properties:
-            wb.style_properties = {'dxf_list': []}
-        elif 'dxf_list' not in wb.style_properties:
-            wb.style_properties['dxf_list'] = []
-
-        for rules in self.cf_rules.values():
-            for rule in rules:
-                if 'dxf' in rule:
-                    dxf = {}
-                    if 'font' in rule['dxf'] and isinstance(rule['dxf']['font'], Font):
-                        # DXF font is limited to color, bold, italic, underline and strikethrough
-                        dxf['font'] = rule['dxf']['font']
-                    if 'border' in rule['dxf'] and isinstance(rule['dxf']['border'], Borders):
-                        dxf['border'] = rule['dxf']['border']
-                    if 'fill' in rule['dxf'] and isinstance(rule['dxf']['fill'], Fill):
-                        dxf['fill'] = rule['dxf']['fill']
-
-                    wb.style_properties['dxf_list'].append(dxf)
-                    rule.pop('dxf')
-                    rule['dxfId'] = len(wb.style_properties['dxf_list']) - 1