Gentoo Archives: gentoo-portage-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-portage-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-portage-dev] [PATCH 1/2] portage.util.configparser: Provide common code to handle cp imports
Date: Sun, 22 May 2016 08:41:59
Message-Id: 20160522084148.2658-1-mgorny@gentoo.org
1 Provide a common code unit to handle portable *ConfigParser imports
2 for all supported Python versions.
3 ---
4 pym/portage/_sets/__init__.py | 10 ++--------
5 pym/portage/repository/config.py | 9 +--------
6 pym/portage/util/_desktop_entry.py | 7 ++-----
7 pym/portage/util/configparser.py | 22 ++++++++++++++++++++++
8 4 files changed, 27 insertions(+), 21 deletions(-)
9 create mode 100644 pym/portage/util/configparser.py
10
11 diff --git a/pym/portage/_sets/__init__.py b/pym/portage/_sets/__init__.py
12 index 3203521..ec42f7c 100644
13 --- a/pym/portage/_sets/__init__.py
14 +++ b/pym/portage/_sets/__init__.py
15 @@ -9,14 +9,6 @@ __all__ = ["SETPREFIX", "get_boolean", "SetConfigError",
16 import io
17 import logging
18 import sys
19 -try:
20 - from configparser import NoOptionError, ParsingError
21 - if sys.hexversion >= 0x3020000:
22 - from configparser import ConfigParser as SafeConfigParser
23 - else:
24 - from configparser import SafeConfigParser
25 -except ImportError:
26 - from ConfigParser import SafeConfigParser, NoOptionError, ParsingError
27 import portage
28 from portage import os
29 from portage import load_mod
30 @@ -29,6 +21,8 @@ from portage.const import _ENABLE_SET_CONFIG
31 from portage.exception import PackageSetNotFound
32 from portage.localization import _
33 from portage.util import writemsg_level
34 +from portage.util.configparser import (SafeConfigParser,
35 + NoOptionError, ParsingError)
36
37 SETPREFIX = "@"
38
39 diff --git a/pym/portage/repository/config.py b/pym/portage/repository/config.py
40 index 00319fe..9039886 100644
41 --- a/pym/portage/repository/config.py
42 +++ b/pym/portage/repository/config.py
43 @@ -9,14 +9,6 @@ import warnings
44 import sys
45 import re
46
47 -try:
48 - from configparser import Error as ConfigParserError
49 - if sys.hexversion >= 0x3020000:
50 - from configparser import ConfigParser as SafeConfigParser
51 - else:
52 - from configparser import SafeConfigParser
53 -except ImportError:
54 - from ConfigParser import SafeConfigParser, Error as ConfigParserError
55 import portage
56 from portage import eclass_cache, os
57 from portage.const import (MANIFEST2_HASH_FUNCTIONS, MANIFEST2_REQUIRED_HASH,
58 @@ -25,6 +17,7 @@ from portage.eapi import eapi_allows_directories_on_profile_level_and_repository
59 from portage.env.loaders import KeyValuePairFileLoader
60 from portage.util import (normalize_path, read_corresponding_eapi_file, shlex_split,
61 stack_lists, writemsg, writemsg_level, _recursive_file_list)
62 +from portage.util.configparser import SafeConfigParser, ConfigParserError
63 from portage.util._path import isdir_raise_eaccess
64 from portage.util.path import first_existing
65 from portage.localization import _
66 diff --git a/pym/portage/util/_desktop_entry.py b/pym/portage/util/_desktop_entry.py
67 index 0b49547..95a015e 100644
68 --- a/pym/portage/util/_desktop_entry.py
69 +++ b/pym/portage/util/_desktop_entry.py
70 @@ -6,14 +6,11 @@ import re
71 import subprocess
72 import sys
73
74 -try:
75 - from configparser import Error as ConfigParserError, RawConfigParser
76 -except ImportError:
77 - from ConfigParser import Error as ConfigParserError, RawConfigParser
78 -
79 import portage
80 from portage import _encodings, _unicode_encode, _unicode_decode
81 from portage.util import writemsg
82 +from portage.util.configparser import ConfigParserError, RawConfigParser
83 +
84
85 def parse_desktop_entry(path):
86 """
87 diff --git a/pym/portage/util/configparser.py b/pym/portage/util/configparser.py
88 new file mode 100644
89 index 0000000..d305052
90 --- /dev/null
91 +++ b/pym/portage/util/configparser.py
92 @@ -0,0 +1,22 @@
93 +# Copyright 2016 Gentoo Foundation
94 +# Distributed under the terms of the GNU General Public License v2
95 +
96 +__all__ = ['ConfigParserError', 'NoOptionError', 'ParsingError',
97 + 'RawConfigParser', 'SafeConfigParser']
98 +
99 +# the following scary compatibility thing provides two classes:
100 +# - SafeConfigParser that provides safe interpolation for values,
101 +# - RawConfigParser that provides no interpolation for values.
102 +
103 +import sys
104 +
105 +try:
106 + from configparser import (Error as ConfigParserError,
107 + NoOptionError, ParsingError, RawConfigParser)
108 + if sys.hexversion >= 0x3020000:
109 + from configparser import ConfigParser as SafeConfigParser
110 + else:
111 + from configparser import SafeConfigParser
112 +except ImportError:
113 + from ConfigParser import (Error as ConfigParserError,
114 + NoOptionError, ParsingError, RawConfigParser, SafeConfigParser)
115 --
116 2.8.3

Replies