1313
1414@contextmanager
1515def conanfile_remove_attr (conanfile , names , method ):
16- """ remove some self.xxxx attribute from the class, so it raises an exception if used
16+ """remove some self.xxxx attribute from the class, so it raises an exception if used
1717 within a given conanfile method
1818 """
1919 original_class = type (conanfile )
2020
2121 def _prop (attr_name ):
2222 def _m (_ ):
2323 raise ConanException (f"'self.{ attr_name } ' access in '{ method } ()' method is forbidden" )
24+
2425 return property (_m )
2526
2627 try :
27- new_class = type (original_class .__name__ , (original_class , ), {})
28+ new_class = type (original_class .__name__ , (original_class ,), {})
2829 conanfile .__class__ = new_class
2930 for name in names :
3031 setattr (new_class , name , _prop (name ))
@@ -40,9 +41,12 @@ def conanfile_exception_formatter(conanfile_name, func_name):
4041 """
4142
4243 def _raise_conanfile_exc (e ):
43- from conan .api .output import LEVEL_DEBUG , conan_output_level
44+ from conan .api .output import LEVEL_DEBUG
45+ from conan .api .output import conan_output_level
46+
4447 if conan_output_level <= LEVEL_DEBUG :
4548 import traceback
49+
4650 raise ConanExceptionInUserConanfileMethod (traceback .format_exc ())
4751 m = _format_conanfile_exception (conanfile_name , func_name , e )
4852 raise ConanExceptionInUserConanfileMethod (m )
@@ -54,12 +58,17 @@ def _raise_conanfile_exc(e):
5458 msg = "{}: Invalid configuration: {}" .format (str (conanfile_name ), exc )
5559 raise ConanInvalidConfiguration (msg )
5660 except AttributeError as exc :
57- list_methods = [m for m in dir (list ) if not m .startswith ('__' )]
58- if "NoneType" in str (exc ) and func_name in ['layout' , 'package_info' ] and \
59- any (method in str (exc ) for method in list_methods ):
60- raise ConanException ("{}: {}. No default values are set for components. You are probably "
61- "trying to manipulate a component attribute in the '{}' method "
62- "without defining it previously" .format (str (conanfile_name ), exc , func_name ))
61+ list_methods = [m for m in dir (list ) if not m .startswith ("__" )]
62+ if (
63+ "NoneType" in str (exc )
64+ and func_name in ["layout" , "package_info" ]
65+ and any (method in str (exc ) for method in list_methods )
66+ ):
67+ raise ConanException (
68+ "{}: {}. No default values are set for components. You are probably "
69+ "trying to manipulate a component attribute in the '{}' method "
70+ "without defining it previously" .format (str (conanfile_name ), exc , func_name )
71+ )
6372 else :
6473 _raise_conanfile_exc (exc )
6574 except Exception as exc :
@@ -74,6 +83,7 @@ def _format_conanfile_exception(scope, method, exception):
7483 """
7584 import sys
7685 import traceback
86+
7787 try :
7888 conanfile_reached = False
7989 tb = sys .exc_info ()[2 ]
@@ -91,8 +101,11 @@ def _format_conanfile_exception(scope, method, exception):
91101 msg = "%s: Error in %s() method" % (scope , method )
92102 msg += ", line %d\n \t %s" % (line , contents )
93103 else :
94- msg = ("while calling '%s', line %d\n \t %s" % (name , line , contents )
95- if line else "\n \t %s" % contents )
104+ msg = (
105+ "while calling '%s', line %d\n \t %s" % (name , line , contents )
106+ if line
107+ else "\n \t %s" % contents
108+ )
96109 content_lines .append (msg )
97110 conanfile_reached = True
98111 index += 1
@@ -105,8 +118,9 @@ def _format_conanfile_exception(scope, method, exception):
105118
106119class ConanException (Exception ):
107120 """
108- Generic conans exception
121+ Generic conans exception
109122 """
123+
110124 def __init__ (self , * args , ** kwargs ):
111125 self .info = None
112126 self .remote = kwargs .pop ("remote" , None )
@@ -119,6 +133,7 @@ def remote_message(self):
119133
120134 def __str__ (self ):
121135 from conans .util .files import exception_message_safe
136+
122137 msg = super (ConanException , self ).__str__ ()
123138 if self .remote :
124139 return "{}.{}" .format (exception_message_safe (msg ), self .remote_message ())
@@ -127,18 +142,20 @@ def __str__(self):
127142
128143
129144class ConanReferenceDoesNotExistInDB (ConanException ):
130- """ Reference does not exist in cache db """
145+ """Reference does not exist in cache db"""
146+
131147 pass
132148
133149
134150class ConanReferenceAlreadyExistsInDB (ConanException ):
135- """ Reference already exists in cache db """
151+ """Reference already exists in cache db"""
152+
136153 pass
137154
138155
139156class NoRemoteAvailable (ConanException ):
140- """ No default remote configured or the specified remote do not exists
141- """
157+ """No default remote configured or the specified remote do not exists"""
158+
142159 pass
143160
144161
@@ -162,6 +179,7 @@ class ConanInvalidConfiguration(ConanExceptionInUserConanfileMethod):
162179 """
163180 This binary, for the requested configuration and package-id cannot be built
164181 """
182+
165183 pass
166184
167185
@@ -172,35 +190,39 @@ class ConanMigrationError(ConanException):
172190# Remote exceptions #
173191class InternalErrorException (ConanException ):
174192 """
175- Generic 500 error
193+ Generic 500 error
176194 """
195+
177196 pass
178197
179198
180199class RequestErrorException (ConanException ):
181200 """
182- Generic 400 error
201+ Generic 400 error
183202 """
203+
184204 pass
185205
186206
187207class AuthenticationException (ConanException ): # 401
188208 """
189- 401 error
209+ 401 error
190210 """
211+
191212 pass
192213
193214
194215class ForbiddenException (ConanException ): # 403
195216 """
196- 403 error
217+ 403 error
197218 """
219+
198220 pass
199221
200222
201223class NotFoundException (ConanException ): # 404
202224 """
203- 404 error
225+ 404 error
204226 """
205227
206228 def __init__ (self , * args , ** kwargs ):
@@ -209,10 +231,12 @@ def __init__(self, *args, **kwargs):
209231
210232
211233class RecipeNotFoundException (NotFoundException ):
212-
213234 def __init__ (self , ref , remote = None ):
214235 from conans .model .recipe_ref import RecipeReference
215- assert isinstance (ref , RecipeReference ), "RecipeNotFoundException requires a RecipeReference"
236+
237+ assert isinstance (
238+ ref , RecipeReference
239+ ), "RecipeNotFoundException requires a RecipeReference"
216240 self .ref = ref
217241 super (RecipeNotFoundException , self ).__init__ (remote = remote )
218242
@@ -222,31 +246,35 @@ def __str__(self):
222246
223247
224248class PackageNotFoundException (NotFoundException ):
225-
226249 def __init__ (self , pref , remote = None ):
227250 from conans .model .package_ref import PkgReference
251+
228252 assert isinstance (pref , PkgReference ), "PackageNotFoundException requires a PkgReference"
229253 self .pref = pref
230254
231255 super (PackageNotFoundException , self ).__init__ (remote = remote )
232256
233257 def __str__ (self ):
234- return "Binary package not found: '{}'{}" .format (self .pref .repr_notime (),
235- self .remote_message ())
258+ return "Binary package not found: '{}'{}" .format (
259+ self .pref .repr_notime (), self .remote_message ()
260+ )
236261
237262
238263class UserInterfaceErrorException (RequestErrorException ):
239264 """
240- 420 error
265+ 420 error
241266 """
267+
242268 pass
243269
244270
245- EXCEPTION_CODE_MAPPING = {InternalErrorException : 500 ,
246- RequestErrorException : 400 ,
247- AuthenticationException : 401 ,
248- ForbiddenException : 403 ,
249- NotFoundException : 404 ,
250- RecipeNotFoundException : 404 ,
251- PackageNotFoundException : 404 ,
252- UserInterfaceErrorException : 420 }
271+ EXCEPTION_CODE_MAPPING = {
272+ InternalErrorException : 500 ,
273+ RequestErrorException : 400 ,
274+ AuthenticationException : 401 ,
275+ ForbiddenException : 403 ,
276+ NotFoundException : 404 ,
277+ RecipeNotFoundException : 404 ,
278+ PackageNotFoundException : 404 ,
279+ UserInterfaceErrorException : 420 ,
280+ }
0 commit comments