| OLD | NEW |
| 1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
| 2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH |
| 3 # | 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
| 7 # | 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
| 12 # | 12 # |
| 13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 | 15 |
| 16 """ | 16 """ |
| 17 Functions for integrating with rPython. | 17 Functions for integrating with rPython. |
| 18 | 18 |
| 19 see: https://cran.r-project.org/web/packages/rPython/index.html | 19 see: https://cran.r-project.org/web/packages/rPython/index.html |
| 20 """ | 20 """ |
| 21 | 21 |
| 22 from __future__ import unicode_literals | |
| 23 | |
| 24 from abp.filters import parse_line | 22 from abp.filters import parse_line |
| 25 | 23 |
| 26 __all__ = ['line2dict'] | 24 __all__ = ['line2dict'] |
| 27 | 25 |
| 28 | 26 |
| 29 def option_list_to_dict(options): | 27 def option_list_to_dict(options): |
| 30 """Recursively parse filter options into dicts. | 28 """Recursively parse filter options into dicts. |
| 31 | 29 |
| 32 Parameters | 30 Parameters |
| 33 ---------- | 31 ---------- |
| (...skipping 29 matching lines...) Expand all Loading... |
| 63 """ | 61 """ |
| 64 result = dict(data._asdict()) | 62 result = dict(data._asdict()) |
| 65 if 'options' in result: | 63 if 'options' in result: |
| 66 result['options'] = option_list_to_dict(result['options']) | 64 result['options'] = option_list_to_dict(result['options']) |
| 67 | 65 |
| 68 result['type'] = data.__class__.__name__ | 66 result['type'] = data.__class__.__name__ |
| 69 | 67 |
| 70 return result | 68 return result |
| 71 | 69 |
| 72 | 70 |
| 73 def strings2utf8(data): | |
| 74 """Convert strings in a data structure to utf8 byte strings. | |
| 75 | |
| 76 Parameters | |
| 77 ---------- | |
| 78 data: dict | |
| 79 The data to convert. Can include nested dicts, lists and tuples. | |
| 80 | |
| 81 Returns | |
| 82 ------- | |
| 83 dict | |
| 84 With all strings encoded as unicode. | |
| 85 | |
| 86 """ | |
| 87 if isinstance(data, dict): | |
| 88 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()} | |
| 89 if isinstance(data, list): | |
| 90 return [strings2utf8(v) for v in data] | |
| 91 if isinstance(data, type('')): | |
| 92 # The condition is a Python 2/3 way of saying "unicode string". | |
| 93 return data.encode('utf-8') | |
| 94 return data | |
| 95 | |
| 96 | |
| 97 def line2dict(text, mode='body'): | 71 def line2dict(text, mode='body'): |
| 98 """Convert a filterlist line to a dictionary. | 72 """Convert a filterlist line to a dictionary. |
| 99 | 73 |
| 100 All strings in the output dictionary will be UTF8 byte strings. This is | 74 All strings in the output dictionary will be UTF8 byte strings. This is |
| 101 necessary to prevent unicode encoding errors in rPython conversion layer. | 75 necessary to prevent unicode encoding errors in rPython conversion layer. |
| 102 | 76 |
| 103 Parameters | 77 Parameters |
| 104 ---------- | 78 ---------- |
| 105 text: str | 79 text: str |
| 106 The filter text we want to parse | 80 The filter text we want to parse |
| 107 mode: str | 81 mode: str |
| 108 Parsing mode (see `abp.filters.parser.parse_line`). | 82 Parsing mode (see `abp.filters.parser.parse_line`). |
| 109 | 83 |
| 110 Returns | 84 Returns |
| 111 ------- | 85 ------- |
| 112 dict | 86 dict |
| 113 With the parsing results and all strings converted to utf8 byte | 87 With the parsing results and all strings converted to utf8 byte |
| 114 strings. | 88 strings. |
| 115 | 89 |
| 116 """ | 90 """ |
| 117 return strings2utf8(tuple2dict(parse_line(text, mode))) | 91 return tuple2dict(parse_line(text, mode)) |
| 118 | 92 |
| 119 | 93 |
| 120 def lines2dicts(string_list, mode='body'): | 94 def lines2dicts(string_list, mode='body'): |
| 121 """Convert a list of filterlist strings to a dictionary. | 95 """Convert a list of filterlist strings to a dictionary. |
| 122 | 96 |
| 123 All strings in the output dictionary will be UTF8 byte strings. This is | 97 All strings in the output dictionary will be UTF8 byte strings. This is |
| 124 necessary to prevent unicode encoding errors in rPython conversion layer. | 98 necessary to prevent unicode encoding errors in rPython conversion layer. |
| 125 | 99 |
| 126 Parameters | 100 Parameters |
| 127 ---------- | 101 ---------- |
| 128 string_list: iterable of str | 102 string_list: iterable of str |
| 129 Each string in the list can be an empty line, include instruction, or | 103 Each string in the list can be an empty line, include instruction, or |
| 130 filter. If the mode is 'start', headers and metadata can also be | 104 filter. If the mode is 'start', headers and metadata can also be |
| 131 parsed. | 105 parsed. |
| 132 mode: str | 106 mode: str |
| 133 Parsing mode (see `abp.filters.parser.parse_line`). | 107 Parsing mode (see `abp.filters.parser.parse_line`). |
| 134 | 108 |
| 135 Returns | 109 Returns |
| 136 ------- | 110 ------- |
| 137 list of dict | 111 list of dict |
| 138 With the parsing results and all strings converted to utf8 byte | 112 With the parsing results and all strings converted to utf8 byte |
| 139 strings. | 113 strings. |
| 140 | 114 |
| 141 """ | 115 """ |
| 142 result = [] | 116 result = [] |
| 143 for string in string_list: | 117 for string in string_list: |
| 144 result.append(line2dict(string, mode)) | 118 result.append(line2dict(string, mode)) |
| 145 return result | 119 return result |
| OLD | NEW |