<commit_before># -*- coding: utf-8 -*-
"""
This file contains functions that adjust matplotlib properties
of figures to give adequate output to both presentations (larger fonts) and
publications.(more information). Features include:

* automatically allowing latex math mode
* improving quality of image
* limiting number of tick labels in presentations

To get this function to work in Jupyter notebook, run `%matplotlib inline`
before `plot_tools.xxxxxx()`. Here's an example:

```
import plot_tools
%matplotlib inline
plot_tools.presentation()
import matplotlib.pyplot as plt
```

a future improvement might be to set tick labels on the upper and lower margins
instead of the middle to avoid conflicting letters

"""

import matplotlib as mpl
import matplotlib.pyplot as plt
from cycler import cycler
import re
"""
This file contains tools that can be called at the begining of a
script to modify all the matplotlib graphs. The two most useful methods
might be `presentation` and `publication`, which changes the detail on
how much information is in figures

I might want to look into compatibility with the seaborn package.

"""
def general_changes():
    mpl.rcParams['figure.figsize'] = (12.0,8.0) # default = (6.0, 4.0)
    mpl.rcParams['figure.dpi'] = 300
    mpl.rcParams['savefig.dpi'] = 300
    use_tex()

def use_tex():
    mpl.rcParams['text.usetex'] = True
    mpl.rcParams['text.latex.unicode'] = True
    mpl.rcParams['text.latex.preamble'] = [r"\usepackage[version=4]{mhchem}"]

def set_linear_tick_locator_for_mpl(self,axis):
    """Hack to limit number of labels automatically
    Written by Schulfa Schwein on stack overflow:
    http://stackoverflow.com/questions/10437689/matplotlib-globally-set-number-of-ticks-x-axis-y-axis-colorbar"
    """
    if isinstance(axis, mpl.axis.XAxis):
        axis.set_major_locator(mpl.ticker.MaxNLocator())
    elif isinstance(axis, mpl.axis.YAxis):
        axis.set_major_locator(mpl.ticker.MaxNLocator())

    axis.set_major_formatter(mpl.ticker.ScalarFormatter())
    axis.set_minor_locator(mpl.ticker.NullLocator())
    axis.set_minor_formatter(mpl.ticker.NullFormatter())

def set_log_tick_locator_for_mpl(self,axis):
    """Hack to limit number of labels automatically
    Written by Schulfa Schwein on stack overflow:
    http://stackoverflow.com/questions/10437689/matplotlib-globally-set-number-of-ticks-x-axis-y-axis-colorbar"
    """

    axis.set_major_locator(mpl.ticker.LogLocator(self.base,numticks=3))
    axis.set_major_formatter(mpl.ticker.LogFormatterMathtext(self.base))
    axis.set_minor_locator(mpl.ticker.LogLocator(self.base, self.subs))
    axis.set_minor_formatter(mpl.ticker.NullFormatter())

def reduce_labels(nlabels = 3):
    # make MaxNLocator default
    mpl.scale.LinearScale.set_default_locators_and_formatters = set_linear_tick_locator_for_mpl
    mpl.scale.LogScale.set_default_locators_and_formatters = set_log_tick_locator_for_mpl
    mpl.ticker.MaxNLocator.default_params['nbins'] = nlabels-1 #this might

def presentation():
    reduce_labels()
    mpl.rcParams['font.size'] = 33 # default = 10
    mpl.rcParams['legend.fontsize'] = 'small' # default = large
    mpl.rcParams['axes.linewidth'] = 2.0 # default = 1.0
    mpl.rcParams['lines.linewidth'] = 2.0 # default = 1.0
    mpl.rcParams['patch.linewidth'] = 1.0 # default = 1.0
    mpl.rcParams['grid.linewidth'] = 1.0 # default = 0.5
    mpl.rcParams['xtick.major.width'] = 2.0 # default = 0.5
    mpl.rcParams['xtick.major.size'] = 8.0 # default = 0.5
    mpl.rcParams['ytick.major.size'] = 8.0 # default = 0.5
    mpl.rcParams['ytick.major.width'] = 2.0 # default = 0.5
    mpl.rcParams['xtick.major.pad'] = 16 #default = 4.0 (prevents overlap labels)
    general_changes()

    # make latex font visible
    #latex_preamble = [r'\usepackage[mathbf]{euler}'] # ideal depth but odd shape
    latex_preamble = [r'\usepackage{arev}'] # pretty visible
    #latex_preamble = [r'\usepackage{cmbright}'] # too light color
    mpl.rcParams['text.latex.preamble'] = mpl.rcParams['text.latex.preamble'] + latex_preamble

def publication():
    mpl.rcParams['font.size'] = 18 # default = 10
    mpl.rcParams['axes.linewidth'] = 2.0 # default = 1.0
    mpl.rcParams['lines.linewidth'] = 2.0 # default = 1.0
    mpl.rcParams['patch.linewidth'] = 1.0 # default = 1.0
    mpl.rcParams['grid.linewidth'] = 1.0 # default = 0.5
    mpl.rcParams['xtick.major.width'] = 1.0 # default = 0.5
    mpl.rcParams['xtick.minor.width'] = 1.0 # default = 0.5
    mpl.rcParams['ytick.major.width'] = 1.0 # default = 0.5
    mpl.rcParams['ytick.minor.width'] = 1.0 # default = 0.5
    general_changes()

######################################################
# methods for usage within plotting functions
######################################################

def _get_color_list_from_colormap(name,number_colors=10):
    """
    returns list of length `number_colors` based on the default
    colormap of `name`. number must be less than 256

    this can be implemented in matplotlib by

    ```
    colors = plot_tools._get_color_list_from_colormap('viridis',number_reactions)
    from cycler import cycler
    axis.set_prop_cycle(cycler('color',colors))
    ```

    This method does not work with LinearColorMaps

    seaborn.mpl_palette(name,n_colors) seems identical to this
    """

    colormap = plt.get_cmap(name)
    color_array = colormap.colors
    color_map_colors = len(color_array)
    if number_colors == 1:
        return [color_array[0]]
    if number_colors > color_map_colors:
        raise ValueError('number colors cannot be more than %i. currently is %i' % (color_map_colors,number_colors))
    # find distance between each node, and multiply by almost one to allow proper 'floor'ing of double
    distance_between_array_elements = float(color_map_colors) / (number_colors-1) * 0.999999999999
    colors = []
    for index in range(number_colors):
        color = color_array[int(index*distance_between_array_elements)]
        colors.append(color)
    return colors

def set_colors(number_colors=10,name = 'viridis'):
    """
    sets the color cycle to the `number_colors` using the colormap `name`.

    this allows for easy comparison of N set of parameters on one graph.
    For example, a full and reduced model with the same number of variables
    can be plotted with different icons ('-', '.', etc) without specifying
    colors each time.

    deprecated, use instead:

    ```
    import seaborn as sns
    color_palette = sns.mpl_palette('jet',10)
    sns.set_palette(color_palette)
    ```

    checkout color map options [here](http://matplotlib.org/users/colormaps.html)
    """

    colors = _get_color_list_from_colormap( name, number_colors)
    mpl.rcParams['axes.prop_cycle'] = cycler('color',colors)

def place_legend_outside_plot(axis,legend_entry= None):
    """
    this method takes an axis object and the text of the legend, and
    it places the legend to the right of the figure, making the figure
    less wide.
    """
    if legend_entry != None:
        axis.legend(legend_entry, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    else:
        axis.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

def latexify_legend(legend_string,mode = 'math'):
    """makes the legend string interpreted able to be interpreted
    by adding a
    """
    if mode =='math':
        prefix = r'$'
        sufix = r"$"
    elif mode == 'chemistry':
        prefix = r'\ce{'
        sufix = r'}'
        #replace '=>' with '->'
        irreversible_reaction = re.compile(r'\s=>')
        for index, string in enumerate(legend_string):
            if irreversible_reaction.search(string) != None:
                legend_string[index] = re.sub(irreversible_reaction,' ->',string)
    new_legend = []
    for index,string in enumerate(legend_string):
        string = string.replace('#',r'\#')
        new_legend.append(prefix + string + sufix)
    return new_legend

######################################################
# methods that plot functions and return the figure object
# this mostly involves methods used in data analysis of various projects
######################################################

def plot_multiple_comparisons(data,dictionary_of_plots,x_data = None,
                              log_plots = [],
                              title='',
                              xlabel='time',
                              yunit=''):
    """
    This method takes in a data frame and plots multiple plots each named
    by the key in `dictionary_of_plots` with the column(s) given by the list
    in the values of `dictionary_of_plots`.

    x_data is a pd.Series object for the data to be plotted. If the x_axis is
    meant to be the index of the dataframe `data`, then it doesn't need to be
    specified.

    title = string above all plots
    xlabel = plt.xlabel
    ynit = unit appended to the ylabel

    returns the figure object,whose axes can be accessed with output.

    used in the isotope project
    """
    number_ratios = len(dictionary_of_plots.keys())
    f, axes = plt.subplots(number_ratios,1, sharex=True,figsize=(12,6*number_ratios))

    axes[0].set_title(title)
    for plot_name, axis in zip(dictionary_of_plots.keys(),axes):
        isotopomers = dictionary_of_plots[plot_name]
        sum_of_isotopomers = data.filter(isotopomers).sum('columns')
        for isotopomer in isotopomers:
            axis.plot(x_data,data[isotopomer]/sum_of_isotopomers)
        #axis.set_xscale('log')
        if plot_name in log_plots:
            axis.set_yscale('log')
        #else:
            #axis.set_ylim((-.1,1.1))
        #axis.legend(dictionary_of_plots[plot_name], loc='best')
        place_legend_outside_plot(axis,dictionary_of_plots[plot_name])
        axis.set_ylabel(plot_name + ' (' + yunit+ ')')
    axes[-1].set_xlabel(xlabel)
    return f<commit_msg>added description to plot tool function<commit_after># -*- coding: utf-8 -*-
"""
This file contains functions that adjust matplotlib properties
of figures to give adequate output to both presentations (larger fonts) and
publications.(more information). Features include:

* automatically allowing latex math mode
* improving quality of image
* limiting number of tick labels in presentations

To get this function to work in Jupyter notebook, run `%matplotlib inline`
before `plot_tools.xxxxxx()`. Here's an example:

```
import plot_tools
%matplotlib inline
plot_tools.presentation()
import matplotlib.pyplot as plt
```

a future improvement might be to set tick labels on the upper and lower margins
instead of the middle to avoid conflicting letters

"""

import matplotlib as mpl
import matplotlib.pyplot as plt
from cycler import cycler
import re
"""
This file contains tools that can be called at the begining of a
script to modify all the matplotlib graphs. The two most useful methods
might be `presentation` and `publication`, which changes the detail on
how much information is in figures

I might want to look into compatibility with the seaborn package.

"""
def general_changes():
    mpl.rcParams['figure.figsize'] = (12.0,8.0) # default = (6.0, 4.0)
    mpl.rcParams['figure.dpi'] = 300
    mpl.rcParams['savefig.dpi'] = 300
    use_tex()

def use_tex():
    mpl.rcParams['text.usetex'] = True
    mpl.rcParams['text.latex.unicode'] = True
    mpl.rcParams['text.latex.preamble'] = [r"\usepackage[version=4]{mhchem}"]

def set_linear_tick_locator_for_mpl(self,axis):
    """Hack to limit number of labels automatically
    Written by Schulfa Schwein on stack overflow:
    http://stackoverflow.com/questions/10437689/matplotlib-globally-set-number-of-ticks-x-axis-y-axis-colorbar"
    """
    if isinstance(axis, mpl.axis.XAxis):
        axis.set_major_locator(mpl.ticker.MaxNLocator())
    elif isinstance(axis, mpl.axis.YAxis):
        axis.set_major_locator(mpl.ticker.MaxNLocator())

    axis.set_major_formatter(mpl.ticker.ScalarFormatter())
    axis.set_minor_locator(mpl.ticker.NullLocator())
    axis.set_minor_formatter(mpl.ticker.NullFormatter())

def set_log_tick_locator_for_mpl(self,axis):
    """Hack to limit number of labels automatically
    Written by Schulfa Schwein on stack overflow:
    http://stackoverflow.com/questions/10437689/matplotlib-globally-set-number-of-ticks-x-axis-y-axis-colorbar"
    """

    axis.set_major_locator(mpl.ticker.LogLocator(self.base,numticks=3))
    axis.set_major_formatter(mpl.ticker.LogFormatterMathtext(self.base))
    axis.set_minor_locator(mpl.ticker.LogLocator(self.base, self.subs))
    axis.set_minor_formatter(mpl.ticker.NullFormatter())

def reduce_labels(nlabels = 3):
    # make MaxNLocator default
    mpl.scale.LinearScale.set_default_locators_and_formatters = set_linear_tick_locator_for_mpl
    mpl.scale.LogScale.set_default_locators_and_formatters = set_log_tick_locator_for_mpl
    mpl.ticker.MaxNLocator.default_params['nbins'] = nlabels-1 #this might

def presentation():
    reduce_labels()
    mpl.rcParams['font.size'] = 33 # default = 10
    mpl.rcParams['legend.fontsize'] = 'small' # default = large
    mpl.rcParams['axes.linewidth'] = 2.0 # default = 1.0
    mpl.rcParams['lines.linewidth'] = 2.0 # default = 1.0
    mpl.rcParams['patch.linewidth'] = 1.0 # default = 1.0
    mpl.rcParams['grid.linewidth'] = 1.0 # default = 0.5
    mpl.rcParams['xtick.major.width'] = 2.0 # default = 0.5
    mpl.rcParams['xtick.major.size'] = 8.0 # default = 0.5
    mpl.rcParams['ytick.major.size'] = 8.0 # default = 0.5
    mpl.rcParams['ytick.major.width'] = 2.0 # default = 0.5
    mpl.rcParams['xtick.major.pad'] = 16 #default = 4.0 (prevents overlap labels)
    general_changes()

    # make latex font visible
    #latex_preamble = [r'\usepackage[mathbf]{euler}'] # ideal depth but odd shape
    latex_preamble = [r'\usepackage{arev}'] # pretty visible
    #latex_preamble = [r'\usepackage{cmbright}'] # too light color
    mpl.rcParams['text.latex.preamble'] = mpl.rcParams['text.latex.preamble'] + latex_preamble

def publication():
    mpl.rcParams['font.size'] = 18 # default = 10
    mpl.rcParams['axes.linewidth'] = 2.0 # default = 1.0
    mpl.rcParams['lines.linewidth'] = 2.0 # default = 1.0
    mpl.rcParams['patch.linewidth'] = 1.0 # default = 1.0
    mpl.rcParams['grid.linewidth'] = 1.0 # default = 0.5
    mpl.rcParams['xtick.major.width'] = 1.0 # default = 0.5
    mpl.rcParams['xtick.minor.width'] = 1.0 # default = 0.5
    mpl.rcParams['ytick.major.width'] = 1.0 # default = 0.5
    mpl.rcParams['ytick.minor.width'] = 1.0 # default = 0.5
    general_changes()

######################################################
# methods for usage within plotting functions
######################################################

def _get_color_list_from_colormap(name,number_colors=10):
    """
    returns list of length `number_colors` based on the default
    colormap of `name`. number must be less than 256

    this can be implemented in matplotlib by

    ```
    colors = plot_tools._get_color_list_from_colormap('viridis',number_reactions)
    from cycler import cycler
    axis.set_prop_cycle(cycler('color',colors))
    ```

    This method does not work with LinearColorMaps

    seaborn.mpl_palette(name,n_colors) seems identical to this
    """

    colormap = plt.get_cmap(name)
    color_array = colormap.colors
    color_map_colors = len(color_array)
    if number_colors == 1:
        return [color_array[0]]
    if number_colors > color_map_colors:
        raise ValueError('number colors cannot be more than %i. currently is %i' % (color_map_colors,number_colors))
    # find distance between each node, and multiply by almost one to allow proper 'floor'ing of double
    distance_between_array_elements = float(color_map_colors) / (number_colors-1) * 0.999999999999
    colors = []
    for index in range(number_colors):
        color = color_array[int(index*distance_between_array_elements)]
        colors.append(color)
    return colors

def set_colors(number_colors=10,name = 'viridis'):
    """
    sets the color cycle to the `number_colors` using the colormap `name`.

    this allows for easy comparison of N set of parameters on one graph.
    For example, a full and reduced model with the same number of variables
    can be plotted with different icons ('-', '.', etc) without specifying
    colors each time.

    deprecated, use instead:

    ```
    import seaborn as sns
    color_palette = sns.mpl_palette('jet',10)
    sns.set_palette(color_palette)
    ```

    checkout color map options [here](http://matplotlib.org/users/colormaps.html)
    """

    colors = _get_color_list_from_colormap( name, number_colors)
    mpl.rcParams['axes.prop_cycle'] = cycler('color',colors)

def place_legend_outside_plot(axis,legend_entry= None):
    """
    this method takes an axis object and the text of the legend, and
    it places the legend to the right of the figure, making the figure
    less wide.

    usage in normal contexts:

    ```
    f = plt.figure()
    [run plotting functions]
    ax = f.get_axes()[0]
    ptt.place_legend_outside_plot(axis=ax,legend_entry= ['a','b','c'])
    ```
    """
    if legend_entry != None:
        axis.legend(legend_entry, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    else:
        axis.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

def latexify_legend(legend_string,mode = 'math'):
    """makes the legend string interpreted able to be interpreted
    by adding a
    """
    if mode =='math':
        prefix = r'$'
        sufix = r"$"
    elif mode == 'chemistry':
        prefix = r'\ce{'
        sufix = r'}'
        #replace '=>' with '->'
        irreversible_reaction = re.compile(r'\s=>')
        for index, string in enumerate(legend_string):
            if irreversible_reaction.search(string) != None:
                legend_string[index] = re.sub(irreversible_reaction,' ->',string)
    new_legend = []
    for index,string in enumerate(legend_string):
        string = string.replace('#',r'\#')
        new_legend.append(prefix + string + sufix)
    return new_legend

######################################################
# methods that plot functions and return the figure object
# this mostly involves methods used in data analysis of various projects
######################################################

def plot_multiple_comparisons(data,dictionary_of_plots,x_data = None,
                              log_plots = [],
                              title='',
                              xlabel='time',
                              yunit=''):
    """
    This method takes in a data frame and plots multiple plots each named
    by the key in `dictionary_of_plots` with the column(s) given by the list
    in the values of `dictionary_of_plots`.

    x_data is a pd.Series object for the data to be plotted. If the x_axis is
    meant to be the index of the dataframe `data`, then it doesn't need to be
    specified.

    title = string above all plots
    xlabel = plt.xlabel
    ynit = unit appended to the ylabel

    returns the figure object,whose axes can be accessed with output.

    used in the isotope project
    """
    number_ratios = len(dictionary_of_plots.keys())
    f, axes = plt.subplots(number_ratios,1, sharex=True,figsize=(12,6*number_ratios))

    axes[0].set_title(title)
    for plot_name, axis in zip(dictionary_of_plots.keys(),axes):
        isotopomers = dictionary_of_plots[plot_name]
        sum_of_isotopomers = data.filter(isotopomers).sum('columns')
        for isotopomer in isotopomers:
            axis.plot(x_data,data[isotopomer]/sum_of_isotopomers)
        #axis.set_xscale('log')
        if plot_name in log_plots:
            axis.set_yscale('log')
        #else:
            #axis.set_ylim((-.1,1.1))
        #axis.legend(dictionary_of_plots[plot_name], loc='best')
        place_legend_outside_plot(axis,dictionary_of_plots[plot_name])
        axis.set_ylabel(plot_name + ' (' + yunit+ ')')
    axes[-1].set_xlabel(xlabel)
    return f
<|endoftext|>


<commit_before>import datetime
import re
import ssl
import time

import django
import feedparser
import requests

import core
from core.templatetags.core import html2text
from features.associations import models as associations
from features.content import models as content
from features.content.signals import post_create
from features.gestalten import models as gestalten
from features.groups import models as groups
from features.imports import models

if hasattr(ssl, '_create_unverified_context'):
    ssl._create_default_https_context = ssl._create_unverified_context

class Command(django.core.management.base.BaseCommand):

    FEED_RE = (
            r'<link\s+[^>]*'
            r'(?:type=[\"\']application/(?:rss|atom)\+xml[\"\']\s+[^>]*'
            r'href=[\"\']([^\"\']+)[\"\']'
            r'|href=[\"\']([^\"\']+)[\"\']\s+[^>]*'
            r'type=[\"\']application/(?:rss|atom)\+xml[\"\'])'
            r'[^>]*>')

    def handle(self, *args, **options):
        author = gestalten.Gestalt.objects.get(
            id=django.conf.settings.GROUPRISE_FEEDS_IMPORTER_GESTALT_ID)
        for group in groups.Group.objects.filter(url_import_feed=True):
            try:
                r = requests.get(group.url, headers={'User-Agent': 'Stadtgestalten'})
                matches = re.findall(self.FEED_RE, r.text)
                for i, match_groups in enumerate(matches):
                    feed_url = match_groups[0] or match_groups[1]
                    if i > 0:
                        # print('{}: Ignoring feed {}'.format(group, feed_url))
                        continue
                    feed = feedparser.parse(feed_url)
                    for entry in feed.entries:
                        key = entry.get('id', entry.get('link'))
                        if key and not models.Imported.objects.filter(key=key).exists():
                            title = entry.get('title')
                            text = html2text(entry.get('summary'), preset='import')
                            if title and text:
                                c = content.Content.objects.create(title=title)
                                link = entry.get('link')
                                if link:
                                    text = '{}\n\n{}'.format(text, link)
                                v = content.Version.objects.create(
                                        author=author, content=c, text=text)
                                t = entry.get('published_parsed')
                                if t:
                                    t = datetime.datetime.fromtimestamp(time.mktime(t))
                                    tz = django.utils.timezone.get_current_timezone()
                                    v.time_created = tz.localize(t)
                                    v.save()
                                slug = core.models.get_unique_slug(
                                        associations.Association, {
                                            'entity_id': group.id,
                                            'entity_type': group.content_type,
                                            'slug': core.text.slugify(title),
                                            })
                                associations.Association.objects.create(
                                        entity_type=group.content_type, entity_id=group.id,
                                        container_type=c.content_type, container_id=c.id,
                                        public=True, slug=slug)
                                models.Imported.objects.create(key=key)
                                post_create.send(sender=self.__class__, instance=c)
            except (requests.exceptions.ChunkedEncodingError,
                    requests.exceptions.ConnectionError):
                pass
<commit_msg>Fix rss import in case of empty id<commit_after>import datetime
import re
import ssl
import time

import django
import feedparser
import requests

import core
from core.templatetags.core import html2text
from features.associations import models as associations
from features.content import models as content
from features.content.signals import post_create
from features.gestalten import models as gestalten
from features.groups import models as groups
from features.imports import models

if hasattr(ssl, '_create_unverified_context'):
    ssl._create_default_https_context = ssl._create_unverified_context

class Command(django.core.management.base.BaseCommand):

    FEED_RE = (
            r'<link\s+[^>]*'
            r'(?:type=[\"\']application/(?:rss|atom)\+xml[\"\']\s+[^>]*'
            r'href=[\"\']([^\"\']+)[\"\']'
            r'|href=[\"\']([^\"\']+)[\"\']\s+[^>]*'
            r'type=[\"\']application/(?:rss|atom)\+xml[\"\'])'
            r'[^>]*>')

    def handle(self, *args, **options):
        author = gestalten.Gestalt.objects.get(
            id=django.conf.settings.GROUPRISE_FEEDS_IMPORTER_GESTALT_ID)
        for group in groups.Group.objects.filter(url_import_feed=True):
            try:
                r = requests.get(group.url, headers={'User-Agent': 'Stadtgestalten'})
                matches = re.findall(self.FEED_RE, r.text)
                for i, match_groups in enumerate(matches):
                    feed_url = match_groups[0] or match_groups[1]
                    if i > 0:
                        # print('{}: Ignoring feed {}'.format(group, feed_url))
                        continue
                    feed = feedparser.parse(feed_url)
                    for entry in feed.entries:
                        key = entry.get('id')
                        if not key:
                            key = entry.get('link')
                        if key and not models.Imported.objects.filter(key=key).exists():
                            title = entry.get('title')
                            text = html2text(entry.get('summary'), preset='import')
                            if title and text:
                                c = content.Content.objects.create(title=title)
                                link = entry.get('link')
                                if link:
                                    text = '{}\n\n{}'.format(text, link)
                                v = content.Version.objects.create(
                                        author=author, content=c, text=text)
                                t = entry.get('published_parsed')
                                if t:
                                    t = datetime.datetime.fromtimestamp(time.mktime(t))
                                    tz = django.utils.timezone.get_current_timezone()
                                    v.time_created = tz.localize(t)
                                    v.save()
                                slug = core.models.get_unique_slug(
                                        associations.Association, {
                                            'entity_id': group.id,
                                            'entity_type': group.content_type,
                                            'slug': core.text.slugify(title),
                                            })
                                associations.Association.objects.create(
                                        entity_type=group.content_type, entity_id=group.id,
                                        container_type=c.content_type, container_id=c.id,
                                        public=True, slug=slug)
                                models.Imported.objects.create(key=key)
                                post_create.send(sender=self.__class__, instance=c)
            except (requests.exceptions.ChunkedEncodingError,
                    requests.exceptions.ConnectionError):
                pass
<|endoftext|>


<commit_before>'''
Configuration object
====================

:class:`Config` object is an instance of a modified Python ConfigParser.
See `ConfigParser documentation
<http://docs.python.org/library/configparser.html>`_ for more information.

Usage of Config object
----------------------

Read a configuration token from a particular section::

    >>> from kivy.config import Config
    >>> Config.getint('kivy', 'show_fps')
    0

Change the configuration and save it::

    >>> Config.set('kivy', 'retain_time', 50)
    >>> Config.write()

Available configuration tokens
------------------------------

:kivy:

    `log_level`: (debug, info, warning, error, critical)
        Set the minimum log level to use
    `log_dir`: string
        Path of log directory
    `log_name`: string
        Format string to use for the filename of log file
    `log_enable`: (0, 1)
        Activate file logging
    `keyboard_mode`: ('', 'system', 'dock', 'multi')
        Keyboard mode to use. If empty, Kivy will decide for you what is the
        best for your current platform. Otherwise, you can set one of 'system'
        (real keyboard), 'dock' (one virtual keyboard docked in a screen side),
        'multi' (one virtual keyboard everytime a widget ask for.)
    `keyboard_layout`: string
        Identifier of the layout to use

:postproc:

    `double_tap_time`: int
        Time allowed for the detection of double tap, in milliseconds
    `double_tap_distance`: float
        Maximum distance allowed for a double tap, normalized inside the range
        0 - 1000
    `retain_time`: int
        Time allowed for a retain touch, in milliseconds
    `retain_distance`: int
        If the touch moves more than is indicated by retain_distance, it will
        not be retained. Argument should be an int between 0 and 1000.
    `jitter_distance`: int
        Maximum distance for jitter detection, normalized inside the range 0
        - 1000
    `jitter_ignore_devices`: string, seperated with comma
        List of devices to ignore from jitter detection
    `ignore`: list of tuples
        List of regions where new touches are ignored.
        This configuration token can be used to resolve hotspot problems
        with DIY hardware. The format of the list must be::

            ignore = [(xmin, ymin, xmax, ymax), ...]

        All the values must be inside 0 - 1 range.

:graphics:

    `maxfps`: int, default to 60
        Maximum FPS allowed.
    `fullscreen`: (0, 1, fake, auto)
        Activate fullscreen. If set to `1`, a resolution of `width`
        times `height` pixels will be used.
        If set to `auto`, your current display's resolution will be
        used instead. This is most likely what you want.
        If you want to place the window in another display,
        use `fake` and adjust `width`, `height`, `top` and `left`.
    `width`: int
        Width of the :class:`~kivy.core.window.Window`, not used if in `auto
        fullscreen`
    `height`: int
        Height of the :class:`~kivy.core.window.Window`, not used if in `auto
        fullscreen`
    `fbo`: (hardware, software, force-hardware)
        Select the FBO backend to use.
    `show_cursor`: (0, 1)
        Show the cursor on the screen
    `position`: (auto, custom)
        Position of the window on your display. If `auto` is used, you have no
        control of the initial position: `top` and `left` are ignored.
    `top`: int
        Top position of the :class:`~kivy.core.window.Window`
    `left`: int
        Left position of the :class:`~kivy.core.window.Window`
    `window_icon`: string
        Path of the window icon. Use this if you want to replace the default
        pygame icon.
    `rotation`: (0, 90, 180, 270)
        Rotation of the :class:`~kivy.core.window.Window`
    `resizable`: (0, 1)
        If 0, the window will have a fixed size. If 1, the window will be
        resizable.

:input:

    Input section is particular. You can create new input device with this
    syntax::

        # example of input provider instance
        yourid = providerid,parameters

        # example for tuio provider
        default = tuio,127.0.0.1:3333
        mytable = tuio,192.168.0.1:3334

    .. seealso::

        Check all the providers in kivy.input.providers for the syntax to use
        inside the configuration file.

:widgets:

    `scroll_distance`: int
        Default value of :data:`~kivy.uix.scrollview.Scrollview.scroll_distance`
        property in :class:`~kivy.uix.scrollview.Scrollview` widget.
        Check the widget documentation for more information.

    `scroll_friction`: float
        Default value of :data:`~kivy.uix.scrollview.Scrollview.scroll_friction`
        property in :class:`~kivy.uix.scrollview.Scrollview` widget.
        Check the widget documentation for more information.

    `scroll_timeout`: int
        Default value of :data:`~kivy.uix.scrollview.Scrollview.scroll_timeout`
        property in :class:`~kivy.uix.scrollview.Scrollview` widget.
        Check the widget documentation for more information.

:modules:

    You can activate modules with this syntax::

        modulename =

    Anything after the = will be passed to the module as arguments.
    Check the specific module's documentation for a list of accepted arguments.

.. versionchanged:: 1.2.0
    `resizable` has been added to graphics section

.. versionchanged:: 1.1.0
    tuio is not listening by default anymore. windows icons are not copied to
    user directory anymore. You can still set a new window icon by using
    ``window_icon`` config setting.

.. versionchanged:: 1.0.8
    `scroll_timeout`, `scroll_distance` and `scroll_friction` have been added.
    `list_friction`, `list_trigger_distance` and `list_friction_bound` have been
    removed. `keyboard_type` and `keyboard_layout` have been removed from
    widget.  `keyboard_mode` and `keyboard_layout` have been added to kivy
    section.
'''

__all__ = ('Config', 'ConfigParser')

from ConfigParser import ConfigParser as PythonConfigParser
from sys import platform
from os import environ
from os.path import exists
from kivy import kivy_config_fn
from kivy.logger import Logger, logger_config_update
from kivy.utils import OrderedDict

# Version number of current configuration format
KIVY_CONFIG_VERSION = 6

#: Kivy configuration object
Config = None

class ConfigParser(PythonConfigParser):
    '''Enhanced ConfigParser class, that supports addition of default
    sections and default values.

    .. versionadded:: 1.0.7
    '''

    def __init__(self):
        PythonConfigParser.__init__(self)
        self._sections = OrderedDict()
        self.filename = None
        self._callbacks = []

    def add_callback(self, callback, section=None, key=None):
        '''Add a callback to be called when a specific section/key changed. If
        you don't specify a section or a key, it will call the callback for all
        section/keys.

        Callbacks will receive 3 arguments: the section, key and value.

        .. versionadded:: 1.4.1
        '''
        if section is None and key is not None:
            raise Exception('You cannot specify a key without a section')
        self._callbacks.append((callback, section, key))

    def _do_callbacks(self, section, key, value):
        for callback, csection, ckey in self._callbacks:
            if csection is not None and csection != section:
                continue
            elif ckey is not None and ckey != key:
                continue
            callback(section, key, value)

    def read(self, filename):
        '''Read only one filename. In contrast to the original ConfigParser of
        Python, this one is able to read only one file at a time. The latest
        read file will be used for the :meth:`write` method.
        '''
        if type(filename) not in (str, unicode):
            raise Exception('Only one filename is accepted (str or unicode)')
        self.filename = filename
        PythonConfigParser.read(self, filename)

    def set(self, section, option, value):
        '''Functions similarly to PythonConfigParser's set method, except that
        the value is implicitly converted to a string.
        '''
        ret = PythonConfigParser.set(self, section, option, str(value))
        self._do_callbacks(section, option, str(value))
        return ret

    def setdefaults(self, section, keyvalues):
        '''Set a lot of keys/values in one section at the same time
        '''
        self.adddefaultsection(section)
        for key, value in keyvalues.iteritems():
            self.setdefault(section, key, value)

    def setdefault(self, section, option, value):
        '''Set the default value of a particular option
        '''
        if self.has_option(section, option):
            return
        self.set(section, option, value)

    def getdefault(self, section, option, defaultvalue):
        '''Get an option. If not found, it will return the default value
        '''
        if not self.has_section(section):
            return defaultvalue
        if not self.has_option(section, option):
            return defaultvalue
        return self.getint(section, option)

    def adddefaultsection(self, section):
        '''Add a section if the section is missing.
        '''
        if self.has_section(section):
            return
        self.add_section(section)

    def write(self):
        '''Write the configuration to the latest file opened with :meth:`read`
        method.

        Return True if the write finished successfully.
        '''
        if self.filename is None:
            return False
        try:
            with open(self.filename, 'w') as fd:
                PythonConfigParser.write(self, fd)
        except IOError:
            Logger.exception('Unable to write the config <%s>' % self.filename)
            return False
        return True

if not environ.get('KIVY_DOC_INCLUDE'):

    #
    # Read, analyse configuration file
    # Support upgrade of older config file versions
    #

    # Create default configuration
    Config = ConfigParser()
    Config.add_callback(logger_config_update, 'kivy', 'log_level')

    # Read config file if exist
    if exists(kivy_config_fn) and \
        'KIVY_USE_DEFAULTCONFIG' not in environ and \
        'KIVY_NO_CONFIG' not in environ:
        try:
            Config.read(kivy_config_fn)
        except Exception, e:
            Logger.exception('Core: error while reading local'
                             'configuration')

    version = Config.getdefault('kivy', 'config_version', 0)

    # Add defaults section
    Config.adddefaultsection('kivy')
    Config.adddefaultsection('graphics')
    Config.adddefaultsection('input')
    Config.adddefaultsection('postproc')
    Config.adddefaultsection('widgets')
    Config.adddefaultsection('modules')

    # Upgrade default configuration until we have the current version
    need_save = False
    if version != KIVY_CONFIG_VERSION and 'KIVY_NO_CONFIG' not in environ:
        Logger.warning('Config: Older configuration version detected'
                       ' (%d instead of %d)' % (
                           version, KIVY_CONFIG_VERSION))
        Logger.warning('Config: Upgrading configuration in progress.')
        need_save = True

    while version < KIVY_CONFIG_VERSION:
        Logger.debug('Config: Upgrading from %d to %d' % (version, version + 1))

        if version == 0:

            # log level
            Config.setdefault('kivy', 'keyboard_repeat_delay', '300')
            Config.setdefault('kivy', 'keyboard_repeat_rate', '30')
            Config.setdefault('kivy', 'log_dir', 'logs')
            Config.setdefault('kivy', 'log_enable', '1')
            Config.setdefault('kivy', 'log_level', 'info')
            Config.setdefault('kivy', 'log_name', 'kivy_%y-%m-%d_%_.txt')
            Config.setdefault('kivy', 'window_icon', '')

            # default graphics parameters
            Config.setdefault('graphics', 'display', '-1')
            Config.setdefault('graphics', 'fullscreen', 'no')
            Config.setdefault('graphics', 'height', '600')
            Config.setdefault('graphics', 'left', '0')
            Config.setdefault('graphics', 'maxfps', '0')
            Config.setdefault('graphics', 'multisamples', '2')
            Config.setdefault('graphics', 'position', 'auto')
            Config.setdefault('graphics', 'rotation', '0')
            Config.setdefault('graphics', 'show_cursor', '1')
            Config.setdefault('graphics', 'top', '0')
            Config.setdefault('graphics', 'vsync', '1')
            Config.setdefault('graphics', 'width', '800')

            # input configuration
            Config.setdefault('input', 'mouse', 'mouse')

            # activate native input provider in configuration
            # from 1.0.9, don't activate mactouch by default, or app are
            # unusable.
            if platform == 'win32':
                Config.setdefault('input', 'wm_touch', 'wm_touch')
                Config.setdefault('input', 'wm_pen', 'wm_pen')
            elif platform == 'linux2':
                Config.setdefault('input', '%(name)s', 'probesysfs')

            # input postprocessing configuration
            Config.setdefault('postproc', 'double_tap_distance', '20')
            Config.setdefault('postproc', 'double_tap_time', '250')
            Config.setdefault('postproc', 'ignore', '[]')
            Config.setdefault('postproc', 'jitter_distance', '0')
            Config.setdefault('postproc', 'jitter_ignore_devices',
                                   'mouse,mactouch,')
            Config.setdefault('postproc', 'retain_distance', '50')
            Config.setdefault('postproc', 'retain_time', '0')

            # default configuration for keyboard repeatition
            Config.setdefault('widgets', 'keyboard_layout', 'qwerty')
            Config.setdefault('widgets', 'keyboard_type', '')
            Config.setdefault('widgets', 'list_friction', '10')
            Config.setdefault('widgets', 'list_friction_bound', '20')
            Config.setdefault('widgets', 'list_trigger_distance', '5')

        elif version == 1:
            Config.remove_option('graphics', 'vsync')
            Config.set('graphics', 'maxfps', '60')

        elif version == 2:
            # was a version to automatically copy windows icon in the user
            # directory, but it's now not used anymore. User can still change
            # the window icon by touching the config.
            pass

        elif version == 3:
            # add token for scrollview
            Config.setdefault('widgets', 'scroll_timeout', '55')
            Config.setdefault('widgets', 'scroll_distance', '20')
            Config.setdefault('widgets', 'scroll_friction', '1.')
            Config.setdefualt('widgets', 'scroll_stoptime', '300')
            Config.setdefault('widgets', 'scroll_moves', '5')

            # remove old list_* token
            Config.remove_option('widgets', 'list_friction')
            Config.remove_option('widgets', 'list_friction_bound')
            Config.remove_option('widgets', 'list_trigger_distance')

        elif version == 4:
            Config.remove_option('widgets', 'keyboard_type')
            Config.remove_option('widgets', 'keyboard_layout')

            # add keyboard token
            Config.setdefault('kivy', 'keyboard_mode', '')
            Config.setdefault('kivy', 'keyboard_layout', 'qwerty')

        elif version == 5:
            Config.setdefault('graphics', 'resizable', '1')

        #elif version == 1:
        #   # add here the command for upgrading from configuration 0 to 1
        #
        else:
            # for future.
            break

        # Pass to the next version
        version += 1

    # Indicate to the Config that we've upgrade to the latest version.
    Config.set('kivy', 'config_version', KIVY_CONFIG_VERSION)

    # Now, activate log file
    if Config.getint('kivy', 'log_enable'):
        Logger.logfile_activated = True

    # If no configuration exist, write the default one.
    if (not exists(kivy_config_fn) or need_save) and \
        'KIVY_NO_CONFIG' not in environ:
        try:
            Config.filename = kivy_config_fn
            Config.write()
        except Exception, e:
            Logger.exception('Core: Error while saving default config file')

<commit_msg>Config documentation update.<commit_after>'''
Configuration object
====================

:class:`Config` object is an instance of a modified Python ConfigParser.
See `ConfigParser documentation
<http://docs.python.org/library/configparser.html>`_ for more information.

Usage of Config object
----------------------

Read a configuration token from a particular section::

    >>> from kivy.config import Config
    >>> Config.getint('kivy', 'show_fps')
    0

Change the configuration and save it::

    >>> Config.set('kivy', 'retain_time', 50)
    >>> Config.write()

Available configuration tokens
------------------------------

:kivy:

    `log_level`: (debug, info, warning, error, critical)
        Set the minimum log level to use
    `log_dir`: string
        Path of log directory
    `log_name`: string
        Format string to use for the filename of log file
    `log_enable`: (0, 1)
        Activate file logging
    `keyboard_mode`: ('', 'system', 'dock', 'multi')
        Keyboard mode to use. If empty, Kivy will decide for you what is the
        best for your current platform. Otherwise, you can set one of 'system'
        (real keyboard), 'dock' (one virtual keyboard docked in a screen side),
        'multi' (one virtual keyboard everytime a widget ask for.)
    `keyboard_layout`: string
        Identifier of the layout to use

:postproc:

    `double_tap_time`: int
        Time allowed for the detection of double tap, in milliseconds
    `double_tap_distance`: float
        Maximum distance allowed for a double tap, normalized inside the range
        0 - 1000
    `retain_time`: int
        Time allowed for a retain touch, in milliseconds
    `retain_distance`: int
        If the touch moves more than is indicated by retain_distance, it will
        not be retained. Argument should be an int between 0 and 1000.
    `jitter_distance`: int
        Maximum distance for jitter detection, normalized inside the range 0
        - 1000
    `jitter_ignore_devices`: string, seperated with comma
        List of devices to ignore from jitter detection
    `ignore`: list of tuples
        List of regions where new touches are ignored.
        This configuration token can be used to resolve hotspot problems
        with DIY hardware. The format of the list must be::

            ignore = [(xmin, ymin, xmax, ymax), ...]

        All the values must be inside 0 - 1 range.

:graphics:

    `maxfps`: int, default to 60
        Maximum FPS allowed.
    `fullscreen`: (0, 1, fake, auto)
        Activate fullscreen. If set to `1`, a resolution of `width`
        times `height` pixels will be used.
        If set to `auto`, your current display's resolution will be
        used instead. This is most likely what you want.
        If you want to place the window in another display,
        use `fake` and adjust `width`, `height`, `top` and `left`.
    `width`: int
        Width of the :class:`~kivy.core.window.Window`, not used if in `auto
        fullscreen`
    `height`: int
        Height of the :class:`~kivy.core.window.Window`, not used if in `auto
        fullscreen`
    `fbo`: (hardware, software, force-hardware)
        Select the FBO backend to use.
    `show_cursor`: (0, 1)
        Show the cursor on the screen
    `position`: (auto, custom)
        Position of the window on your display. If `auto` is used, you have no
        control of the initial position: `top` and `left` are ignored.
    `top`: int
        Top position of the :class:`~kivy.core.window.Window`
    `left`: int
        Left position of the :class:`~kivy.core.window.Window`
    `window_icon`: string
        Path of the window icon. Use this if you want to replace the default
        pygame icon.
    `rotation`: (0, 90, 180, 270)
        Rotation of the :class:`~kivy.core.window.Window`
    `resizable`: (0, 1)
        If 0, the window will have a fixed size. If 1, the window will be
        resizable.

:input:

    Input section is particular. You can create new input device with this
    syntax::

        # example of input provider instance
        yourid = providerid,parameters

        # example for tuio provider
        default = tuio,127.0.0.1:3333
        mytable = tuio,192.168.0.1:3334

    .. seealso::

        Check all the providers in kivy.input.providers for the syntax to use
        inside the configuration file.

:widgets:

    `scroll_distance`: int
        Default value of :data:`~kivy.uix.scrollview.Scrollview.scroll_distance`
        property in :class:`~kivy.uix.scrollview.Scrollview` widget.
        Check the widget documentation for more information.

    `scroll_friction`: float
        Default value of :data:`~kivy.uix.scrollview.Scrollview.scroll_friction`
        property in :class:`~kivy.uix.scrollview.Scrollview` widget.
        Check the widget documentation for more information.

    `scroll_timeout`: int
        Default value of :data:`~kivy.uix.scrollview.Scrollview.scroll_timeout`
        property in :class:`~kivy.uix.scrollview.Scrollview` widget.
        Check the widget documentation for more information.

    `scroll_stoptime`: int
        Default value of :data:`~kivy.uix.scrollview.Scrollview.scroll_stoptime`
        property in :class:`~kivy.uix.scrollview.Scrollview` widget.
        Check the widget documentation for more information.

    `scroll_moves`: int
        Default value of :data:`~kivy.uix.scrollview.Scrollview.scroll_moves`
        property in :class:`~kivy.uix.scrollview.Scrollview` widget.
        Check the widget documentation for more information.

:modules:

    You can activate modules with this syntax::

        modulename =

    Anything after the = will be passed to the module as arguments.
    Check the specific module's documentation for a list of accepted arguments.

.. versionchanged:: 1.2.0
    `resizable` has been added to graphics section

.. versionchanged:: 1.1.0
    tuio is not listening by default anymore. windows icons are not copied to
    user directory anymore. You can still set a new window icon by using
    ``window_icon`` config setting.

.. versionchanged:: 1.0.8
    `scroll_timeout`, `scroll_distance` and `scroll_friction` have been added.
    `list_friction`, `list_trigger_distance` and `list_friction_bound` have been
    removed. `keyboard_type` and `keyboard_layout` have been removed from
    widget.  `keyboard_mode` and `keyboard_layout` have been added to kivy
    section.
'''

__all__ = ('Config', 'ConfigParser')

from ConfigParser import ConfigParser as PythonConfigParser
from sys import platform
from os import environ
from os.path import exists
from kivy import kivy_config_fn
from kivy.logger import Logger, logger_config_update
from kivy.utils import OrderedDict

# Version number of current configuration format
KIVY_CONFIG_VERSION = 6

#: Kivy configuration object
Config = None

class ConfigParser(PythonConfigParser):
    '''Enhanced ConfigParser class, that supports addition of default
    sections and default values.

    .. versionadded:: 1.0.7
    '''

    def __init__(self):
        PythonConfigParser.__init__(self)
        self._sections = OrderedDict()
        self.filename = None
        self._callbacks = []

    def add_callback(self, callback, section=None, key=None):
        '''Add a callback to be called when a specific section/key changed. If
        you don't specify a section or a key, it will call the callback for all
        section/keys.

        Callbacks will receive 3 arguments: the section, key and value.

        .. versionadded:: 1.4.1
        '''
        if section is None and key is not None:
            raise Exception('You cannot specify a key without a section')
        self._callbacks.append((callback, section, key))

    def _do_callbacks(self, section, key, value):
        for callback, csection, ckey in self._callbacks:
            if csection is not None and csection != section:
                continue
            elif ckey is not None and ckey != key:
                continue
            callback(section, key, value)

    def read(self, filename):
        '''Read only one filename. In contrast to the original ConfigParser of
        Python, this one is able to read only one file at a time. The latest
        read file will be used for the :meth:`write` method.
        '''
        if type(filename) not in (str, unicode):
            raise Exception('Only one filename is accepted (str or unicode)')
        self.filename = filename
        PythonConfigParser.read(self, filename)

    def set(self, section, option, value):
        '''Functions similarly to PythonConfigParser's set method, except that
        the value is implicitly converted to a string.
        '''
        ret = PythonConfigParser.set(self, section, option, str(value))
        self._do_callbacks(section, option, str(value))
        return ret

    def setdefaults(self, section, keyvalues):
        '''Set a lot of keys/values in one section at the same time
        '''
        self.adddefaultsection(section)
        for key, value in keyvalues.iteritems():
            self.setdefault(section, key, value)

    def setdefault(self, section, option, value):
        '''Set the default value of a particular option
        '''
        if self.has_option(section, option):
            return
        self.set(section, option, value)

    def getdefault(self, section, option, defaultvalue):
        '''Get an option. If not found, it will return the default value
        '''
        if not self.has_section(section):
            return defaultvalue
        if not self.has_option(section, option):
            return defaultvalue
        return self.getint(section, option)

    def adddefaultsection(self, section):
        '''Add a section if the section is missing.
        '''
        if self.has_section(section):
            return
        self.add_section(section)

    def write(self):
        '''Write the configuration to the latest file opened with :meth:`read`
        method.

        Return True if the write finished successfully.
        '''
        if self.filename is None:
            return False
        try:
            with open(self.filename, 'w') as fd:
                PythonConfigParser.write(self, fd)
        except IOError:
            Logger.exception('Unable to write the config <%s>' % self.filename)
            return False
        return True

if not environ.get('KIVY_DOC_INCLUDE'):

    #
    # Read, analyse configuration file
    # Support upgrade of older config file versions
    #

    # Create default configuration
    Config = ConfigParser()
    Config.add_callback(logger_config_update, 'kivy', 'log_level')

    # Read config file if exist
    if exists(kivy_config_fn) and \
        'KIVY_USE_DEFAULTCONFIG' not in environ and \
        'KIVY_NO_CONFIG' not in environ:
        try:
            Config.read(kivy_config_fn)
        except Exception, e:
            Logger.exception('Core: error while reading local'
                             'configuration')

    version = Config.getdefault('kivy', 'config_version', 0)

    # Add defaults section
    Config.adddefaultsection('kivy')
    Config.adddefaultsection('graphics')
    Config.adddefaultsection('input')
    Config.adddefaultsection('postproc')
    Config.adddefaultsection('widgets')
    Config.adddefaultsection('modules')

    # Upgrade default configuration until we have the current version
    need_save = False
    if version != KIVY_CONFIG_VERSION and 'KIVY_NO_CONFIG' not in environ:
        Logger.warning('Config: Older configuration version detected'
                       ' (%d instead of %d)' % (
                           version, KIVY_CONFIG_VERSION))
        Logger.warning('Config: Upgrading configuration in progress.')
        need_save = True

    while version < KIVY_CONFIG_VERSION:
        Logger.debug('Config: Upgrading from %d to %d' % (version, version + 1))

        if version == 0:

            # log level
            Config.setdefault('kivy', 'keyboard_repeat_delay', '300')
            Config.setdefault('kivy', 'keyboard_repeat_rate', '30')
            Config.setdefault('kivy', 'log_dir', 'logs')
            Config.setdefault('kivy', 'log_enable', '1')
            Config.setdefault('kivy', 'log_level', 'info')
            Config.setdefault('kivy', 'log_name', 'kivy_%y-%m-%d_%_.txt')
            Config.setdefault('kivy', 'window_icon', '')

            # default graphics parameters
            Config.setdefault('graphics', 'display', '-1')
            Config.setdefault('graphics', 'fullscreen', 'no')
            Config.setdefault('graphics', 'height', '600')
            Config.setdefault('graphics', 'left', '0')
            Config.setdefault('graphics', 'maxfps', '0')
            Config.setdefault('graphics', 'multisamples', '2')
            Config.setdefault('graphics', 'position', 'auto')
            Config.setdefault('graphics', 'rotation', '0')
            Config.setdefault('graphics', 'show_cursor', '1')
            Config.setdefault('graphics', 'top', '0')
            Config.setdefault('graphics', 'vsync', '1')
            Config.setdefault('graphics', 'width', '800')

            # input configuration
            Config.setdefault('input', 'mouse', 'mouse')

            # activate native input provider in configuration
            # from 1.0.9, don't activate mactouch by default, or app are
            # unusable.
            if platform == 'win32':
                Config.setdefault('input', 'wm_touch', 'wm_touch')
                Config.setdefault('input', 'wm_pen', 'wm_pen')
            elif platform == 'linux2':
                Config.setdefault('input', '%(name)s', 'probesysfs')

            # input postprocessing configuration
            Config.setdefault('postproc', 'double_tap_distance', '20')
            Config.setdefault('postproc', 'double_tap_time', '250')
            Config.setdefault('postproc', 'ignore', '[]')
            Config.setdefault('postproc', 'jitter_distance', '0')
            Config.setdefault('postproc', 'jitter_ignore_devices',
                                   'mouse,mactouch,')
            Config.setdefault('postproc', 'retain_distance', '50')
            Config.setdefault('postproc', 'retain_time', '0')

            # default configuration for keyboard repeatition
            Config.setdefault('widgets', 'keyboard_layout', 'qwerty')
            Config.setdefault('widgets', 'keyboard_type', '')
            Config.setdefault('widgets', 'list_friction', '10')
            Config.setdefault('widgets', 'list_friction_bound', '20')
            Config.setdefault('widgets', 'list_trigger_distance', '5')

        elif version == 1:
            Config.remove_option('graphics', 'vsync')
            Config.set('graphics', 'maxfps', '60')

        elif version == 2:
            # was a version to automatically copy windows icon in the user
            # directory, but it's now not used anymore. User can still change
            # the window icon by touching the config.
            pass

        elif version == 3:
            # add token for scrollview
            Config.setdefault('widgets', 'scroll_timeout', '55')
            Config.setdefault('widgets', 'scroll_distance', '20')
            Config.setdefault('widgets', 'scroll_friction', '1.')
            Config.setdefualt('widgets', 'scroll_stoptime', '300')
            Config.setdefault('widgets', 'scroll_moves', '5')

            # remove old list_* token
            Config.remove_option('widgets', 'list_friction')
            Config.remove_option('widgets', 'list_friction_bound')
            Config.remove_option('widgets', 'list_trigger_distance')

        elif version == 4:
            Config.remove_option('widgets', 'keyboard_type')
            Config.remove_option('widgets', 'keyboard_layout')

            # add keyboard token
            Config.setdefault('kivy', 'keyboard_mode', '')
            Config.setdefault('kivy', 'keyboard_layout', 'qwerty')

        elif version == 5:
            Config.setdefault('graphics', 'resizable', '1')

        #elif version == 1:
        #   # add here the command for upgrading from configuration 0 to 1
        #
        else:
            # for future.
            break

        # Pass to the next version
        version += 1

    # Indicate to the Config that we've upgrade to the latest version.
    Config.set('kivy', 'config_version', KIVY_CONFIG_VERSION)

    # Now, activate log file
    if Config.getint('kivy', 'log_enable'):
        Logger.logfile_activated = True

    # If no configuration exist, write the default one.
    if (not exists(kivy_config_fn) or need_save) and \
        'KIVY_NO_CONFIG' not in environ:
        try:
            Config.filename = kivy_config_fn
            Config.write()
        except Exception, e:
            Logger.exception('Core: Error while saving default config file')

<|endoftext|>


<commit_before># KlupuNG
# Copyright (C) 2013 Koodilehto Osk <http://koodilehto.fi>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

## Standard library imports
import datetime
import re
import unicodedata
import urllib

## 3rd party imports
import flask

## Local imports
import klupung.models

class Error(Exception):
    """Common base class for all API-related exceptions."""

    def __init__(self, code, message):
        self.code = code
        self.message = message

class InvalidArgumentError(Error):
    """Raised when a client has provided an invalid query argument."""

    def __init__(self, arg, name, expected=""):
        message = "Invalid value '%s' for argument '%s', " \
            "expected %s." % (arg, name, expected)
        Error.__init__(self, 400, message)

_SLUG_PUNCT_RE = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')

def _slugify(text, delim=u'-'):
    """Return an unicode slug of the text"""
    result = []
    for word in _SLUG_PUNCT_RE.split(text.lower()):
        word = unicodedata.normalize('NFKD', word).encode('ascii', 'ignore')
        if word:
            result.append(word)
    return unicode(delim.join(result))

def _get_uint_arg(name, default):
    """Return `int` value of argument `name` from the current request

    If the current request does not have argument `name`, `default`
    value is returned instead. Raises `InvalidArgumentError` if the
    value (or `default` value if it is used) is not a positive integer.

    """

    arg = flask.request.args.get(name, "")
    arg = arg if arg else default
    try:
        value = int(arg)
    except ValueError:
        raise InvalidArgumentError(arg, name, expected="a positive integer")
    else:
        if value < 0:
            raise InvalidArgumentError(arg, name, expected="a positive integer")
    return value

def _get_choice_arg(name, choices):
    """Return `str` value of argument `name` from the current request

    If the current request does not have argument `name`, the first item
    from `choices` sequence is returned. Raises `InvalidArgumentError`
    if the value is not listed in `choices`.

    """

    arg = flask.request.args.get(name, "")
    arg = arg if arg else choices[0]
    if arg not in choices:
        raise InvalidArgumentError(arg, name,
                                   expected=" or ".join([repr(s) for s in choices]))
    return arg

def _jsonified_resource(model_class=None, get_resource=None, model_id=None, sortable_fields=()):
    if model_id is not None:
        resource = get_resource(model_class.query.get_or_404(model_id))
        return flask.jsonify(**resource)

    limit = min(_get_uint_arg("limit", 20), 1000)
    offset = _get_uint_arg("offset", 0)

    total_count = 0
    objects = []

    if model_class is not None:
        models = model_class.query.limit(limit).offset(offset).all()
        total_count = model_class.query.count()
        objects = [get_resource(m) for m in models]

    if sortable_fields:
        choices = []
        for field in sortable_fields:
            choices.append(field)
            choices.append("-%s" % field)
        order_by_arg = _get_choice_arg("order_by", choices)
        is_descending = order_by_arg.startswith("-")
        field = order_by_arg.lstrip("-")
        objects.sort(key=lambda o: o[field], reverse=is_descending)

    next_path = None
    prev_path = None

    if limit + offset < total_count:
        next_path_args = flask.request.args.to_dict()
        next_path_args["offset"] = offset + limit
        next_path = "%s?%s" % (flask.request.path, urllib.urlencode(next_path_args))

    if offset > 0:
        prev_path_args = flask.request.args.to_dict()
        prev_path_args["offset"] = max(offset - limit, 0)
        prev_path = "%s?%s" % (flask.request.path, urllib.urlencode(prev_path_args))

    resource = {
        "meta": {
            "limit"       : limit,
            "next"        : next_path,
            "offset"      : offset,
            "previous"    : prev_path,
            "total_count" : total_count,
            },
        "objects": objects,
        }

    return flask.jsonify(**resource)

def _get_category_resource(category):
    parent_uri = None
    if category.parent_id is not None:
        parent_uri = flask.url_for("._category_route",
                                   category_id=category.parent_id)

    return {
        "id": category.id,
        "level": category.level,
        "name": category.name,
        "origin_id": category.origin_id,
        "parent": parent_uri,
        "resource_uri": flask.url_for("._category_route",
                                      category_id=category.id),
        }

def _get_policymaker_resource(policymaker):
    return {
        "id"          : policymaker.id,
        "abbreviation": policymaker.abbreviation,
        "name"        : policymaker.name,
        "origin_id"   : policymaker.abbreviation,
        "slug"        : _slugify(policymaker.abbreviation),
        "summary"     : None,
        "resource_uri": flask.url_for("._policymaker_route",
                                      policymaker_id=policymaker.id),
        }

def _get_meeting_resource(meeting):
    return {
        "id"              : meeting.id,
        "date"            : str(meeting.start_datetime.date()),
        "minutes"         : True,
        "number"          : 1,
        "policymaker"     : flask.url_for("._policymaker_route",
                                          policymaker_id=meeting.policymaker.id),
        "policymaker_name": meeting.policymaker.name,
        "year"            : meeting.start_datetime.year,
        "resource_uri"    : flask.url_for("._meeting_route",
                                          meeting_id=meeting.id),
        }

def _get_meeting_document_resource(meeting_document):
    return {
        "id"                 : meeting_document.id,
        "last_modified_time" : str(meeting_document.publish_datetime),
        "meeting"            : _get_meeting_resource(meeting_document.meeting),
        "organisation"       : None,
        "origin_id"          : meeting_document.origin_id,
        "origin_url"         : meeting_document.origin_url,
        "publish_time"       : str(meeting_document.publish_datetime),
        "type"               : "minutes",
        "xml_uri"            : None,
        "resource_uri"       : flask.url_for("._meeting_document_route",
                                             meeting_document_id=meeting_document.id)
    }

v0 = flask.Blueprint("v0", __name__, url_prefix="/api/v0")

@v0.route("/policymaker/")
@v0.route("/policymaker/<int:policymaker_id>/")
def _policymaker_route(policymaker_id=None):
    return _jsonified_resource(
        model_class=klupung.models.Policymaker,
        get_resource=_get_policymaker_resource,
        model_id=policymaker_id,
        sortable_fields=["name"])

@v0.route("/meeting/")
@v0.route("/meeting/<int:meeting_id>/")
def _meeting_route(meeting_id=None):
    return _jsonified_resource(
        model_class=klupung.models.Meeting,
        get_resource=_get_meeting_resource,
        model_id=meeting_id,
        sortable_fields=["date", "policymaker"])

@v0.route("/meeting_document/")
@v0.route("/meeting_document/<int:meeting_document_id>/")
def _meeting_document_route(meeting_document_id=None):
    return _jsonified_resource(
        model_class=klupung.models.MeetingDocument,
        get_resource=_get_meeting_document_resource,
        model_id=meeting_document_id)

@v0.route("/category/")
@v0.route("/category/<int:category_id>")
def _category_route(category_id=None):
    return _jsonified_resource(
        model_class=klupung.models.Category,
        get_resource=_get_category_resource,
        model_id=category_id)

@v0.route("/video/")
def _video_route():
    return _jsonified_resource()

@v0.route("/district/")
def _district_route():
    return _jsonified_resource()

@v0.route("/attachment/")
def _attachment_route():
    return _jsonified_resource()

@v0.errorhandler(Error)
def _errorhandler(error):
    return flask.jsonify(error=error.message), error.code
<commit_msg>Cleanup: align resource dict fields<commit_after># KlupuNG
# Copyright (C) 2013 Koodilehto Osk <http://koodilehto.fi>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

## Standard library imports
import datetime
import re
import unicodedata
import urllib

## 3rd party imports
import flask

## Local imports
import klupung.models

class Error(Exception):
    """Common base class for all API-related exceptions."""

    def __init__(self, code, message):
        self.code = code
        self.message = message

class InvalidArgumentError(Error):
    """Raised when a client has provided an invalid query argument."""

    def __init__(self, arg, name, expected=""):
        message = "Invalid value '%s' for argument '%s', " \
            "expected %s." % (arg, name, expected)
        Error.__init__(self, 400, message)

_SLUG_PUNCT_RE = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')

def _slugify(text, delim=u'-'):
    """Return an unicode slug of the text"""
    result = []
    for word in _SLUG_PUNCT_RE.split(text.lower()):
        word = unicodedata.normalize('NFKD', word).encode('ascii', 'ignore')
        if word:
            result.append(word)
    return unicode(delim.join(result))

def _get_uint_arg(name, default):
    """Return `int` value of argument `name` from the current request

    If the current request does not have argument `name`, `default`
    value is returned instead. Raises `InvalidArgumentError` if the
    value (or `default` value if it is used) is not a positive integer.

    """

    arg = flask.request.args.get(name, "")
    arg = arg if arg else default
    try:
        value = int(arg)
    except ValueError:
        raise InvalidArgumentError(arg, name, expected="a positive integer")
    else:
        if value < 0:
            raise InvalidArgumentError(arg, name, expected="a positive integer")
    return value

def _get_choice_arg(name, choices):
    """Return `str` value of argument `name` from the current request

    If the current request does not have argument `name`, the first item
    from `choices` sequence is returned. Raises `InvalidArgumentError`
    if the value is not listed in `choices`.

    """

    arg = flask.request.args.get(name, "")
    arg = arg if arg else choices[0]
    if arg not in choices:
        raise InvalidArgumentError(arg, name,
                                   expected=" or ".join([repr(s) for s in choices]))
    return arg

def _jsonified_resource(model_class=None, get_resource=None, model_id=None, sortable_fields=()):
    if model_id is not None:
        resource = get_resource(model_class.query.get_or_404(model_id))
        return flask.jsonify(**resource)

    limit = min(_get_uint_arg("limit", 20), 1000)
    offset = _get_uint_arg("offset", 0)

    total_count = 0
    objects = []

    if model_class is not None:
        models = model_class.query.limit(limit).offset(offset).all()
        total_count = model_class.query.count()
        objects = [get_resource(m) for m in models]

    if sortable_fields:
        choices = []
        for field in sortable_fields:
            choices.append(field)
            choices.append("-%s" % field)
        order_by_arg = _get_choice_arg("order_by", choices)
        is_descending = order_by_arg.startswith("-")
        field = order_by_arg.lstrip("-")
        objects.sort(key=lambda o: o[field], reverse=is_descending)

    next_path = None
    prev_path = None

    if limit + offset < total_count:
        next_path_args = flask.request.args.to_dict()
        next_path_args["offset"] = offset + limit
        next_path = "%s?%s" % (flask.request.path, urllib.urlencode(next_path_args))

    if offset > 0:
        prev_path_args = flask.request.args.to_dict()
        prev_path_args["offset"] = max(offset - limit, 0)
        prev_path = "%s?%s" % (flask.request.path, urllib.urlencode(prev_path_args))

    resource = {
        "meta": {
            "limit"       : limit,
            "next"        : next_path,
            "offset"      : offset,
            "previous"    : prev_path,
            "total_count" : total_count,
            },
        "objects": objects,
        }

    return flask.jsonify(**resource)

def _get_category_resource(category):
    parent_uri = None
    if category.parent_id is not None:
        parent_uri = flask.url_for("._category_route",
                                   category_id=category.parent_id)

    return {
        "id"          : category.id,
        "level"       : category.level,
        "name"        : category.name,
        "origin_id"   : category.origin_id,
        "parent"      : parent_uri,
        "resource_uri": flask.url_for("._category_route",
                                      category_id=category.id),
        }

def _get_policymaker_resource(policymaker):
    return {
        "id"          : policymaker.id,
        "abbreviation": policymaker.abbreviation,
        "name"        : policymaker.name,
        "origin_id"   : policymaker.abbreviation,
        "slug"        : _slugify(policymaker.abbreviation),
        "summary"     : None,
        "resource_uri": flask.url_for("._policymaker_route",
                                      policymaker_id=policymaker.id),
        }

def _get_meeting_resource(meeting):
    return {
        "id"              : meeting.id,
        "date"            : str(meeting.start_datetime.date()),
        "minutes"         : True,
        "number"          : 1,
        "policymaker"     : flask.url_for("._policymaker_route",
                                          policymaker_id=meeting.policymaker.id),
        "policymaker_name": meeting.policymaker.name,
        "year"            : meeting.start_datetime.year,
        "resource_uri"    : flask.url_for("._meeting_route",
                                          meeting_id=meeting.id),
        }

def _get_meeting_document_resource(meeting_document):
    return {
        "id"                 : meeting_document.id,
        "last_modified_time" : str(meeting_document.publish_datetime),
        "meeting"            : _get_meeting_resource(meeting_document.meeting),
        "organisation"       : None,
        "origin_id"          : meeting_document.origin_id,
        "origin_url"         : meeting_document.origin_url,
        "publish_time"       : str(meeting_document.publish_datetime),
        "type"               : "minutes",
        "xml_uri"            : None,
        "resource_uri"       : flask.url_for("._meeting_document_route",
                                             meeting_document_id=meeting_document.id)
    }

v0 = flask.Blueprint("v0", __name__, url_prefix="/api/v0")

@v0.route("/policymaker/")
@v0.route("/policymaker/<int:policymaker_id>/")
def _policymaker_route(policymaker_id=None):
    return _jsonified_resource(
        model_class=klupung.models.Policymaker,
        get_resource=_get_policymaker_resource,
        model_id=policymaker_id,
        sortable_fields=["name"])

@v0.route("/meeting/")
@v0.route("/meeting/<int:meeting_id>/")
def _meeting_route(meeting_id=None):
    return _jsonified_resource(
        model_class=klupung.models.Meeting,
        get_resource=_get_meeting_resource,
        model_id=meeting_id,
        sortable_fields=["date", "policymaker"])

@v0.route("/meeting_document/")
@v0.route("/meeting_document/<int:meeting_document_id>/")
def _meeting_document_route(meeting_document_id=None):
    return _jsonified_resource(
        model_class=klupung.models.MeetingDocument,
        get_resource=_get_meeting_document_resource,
        model_id=meeting_document_id)

@v0.route("/category/")
@v0.route("/category/<int:category_id>")
def _category_route(category_id=None):
    return _jsonified_resource(
        model_class=klupung.models.Category,
        get_resource=_get_category_resource,
        model_id=category_id)

@v0.route("/video/")
def _video_route():
    return _jsonified_resource()

@v0.route("/district/")
def _district_route():
    return _jsonified_resource()

@v0.route("/attachment/")
def _attachment_route():
    return _jsonified_resource()

@v0.errorhandler(Error)
def _errorhandler(error):
    return flask.jsonify(error=error.message), error.code
<|endoftext|>