1 | # -*- coding: utf-8 -*- |
---|
2 | """ |
---|
3 | pydal.utils |
---|
4 | ----------- |
---|
5 | |
---|
6 | Provides some utilities for pydal. |
---|
7 | |
---|
8 | :copyright: (c) 2017 by Giovanni Barillari and contributors |
---|
9 | :license: BSD, see LICENSE for more details. |
---|
10 | """ |
---|
11 | |
---|
12 | import warnings |
---|
13 | import re |
---|
14 | |
---|
15 | |
---|
16 | class RemovedInNextVersionWarning(DeprecationWarning): |
---|
17 | pass |
---|
18 | |
---|
19 | |
---|
20 | warnings.simplefilter("always", RemovedInNextVersionWarning) |
---|
21 | |
---|
22 | |
---|
23 | def warn_of_deprecation(old_name, new_name, prefix=None, stack=2): |
---|
24 | msg = "%(old)s is deprecated, use %(new)s instead." |
---|
25 | if prefix: |
---|
26 | msg = "%(prefix)s." + msg |
---|
27 | warnings.warn( |
---|
28 | msg % {"old": old_name, "new": new_name, "prefix": prefix}, |
---|
29 | RemovedInNextVersionWarning, |
---|
30 | stack, |
---|
31 | ) |
---|
32 | |
---|
33 | |
---|
34 | class deprecated(object): |
---|
35 | def __init__(self, old_method_name, new_method_name, class_name=None, s=0): |
---|
36 | self.class_name = class_name |
---|
37 | self.old_method_name = old_method_name |
---|
38 | self.new_method_name = new_method_name |
---|
39 | self.additional_stack = s |
---|
40 | |
---|
41 | def __call__(self, f): |
---|
42 | def wrapped(*args, **kwargs): |
---|
43 | warn_of_deprecation( |
---|
44 | self.old_method_name, |
---|
45 | self.new_method_name, |
---|
46 | self.class_name, |
---|
47 | 3 + self.additional_stack, |
---|
48 | ) |
---|
49 | return f(*args, **kwargs) |
---|
50 | |
---|
51 | return wrapped |
---|
52 | |
---|
53 | |
---|
54 | def split_uri_args(query, separators="&?", need_equal=False): |
---|
55 | """ |
---|
56 | Split the args in the query string of a db uri. |
---|
57 | |
---|
58 | Returns a dict with splitted args and values. |
---|
59 | """ |
---|
60 | if need_equal: |
---|
61 | regex_arg_val = "(?P<argkey>[^=]+)=(?P<argvalue>[^%s]*)[%s]?" % ( |
---|
62 | separators, |
---|
63 | separators, |
---|
64 | ) |
---|
65 | else: |
---|
66 | regex_arg_val = "(?P<argkey>[^=%s]+)(=(?P<argvalue>[^%s]*))?[%s]?" % ( |
---|
67 | separators, |
---|
68 | separators, |
---|
69 | separators, |
---|
70 | ) |
---|
71 | return dict( |
---|
72 | [m.group("argkey", "argvalue") for m in re.finditer(regex_arg_val, query)] |
---|
73 | ) |
---|