Skip to content

Commit 44aa094

Browse files
nikromenpraiskup
authored andcommitted
frontend: unify naming convention for safe methods
Originally, safe methods were considered as methods raising ObjectNotFound for API (mainly with session.one() method). Now methods which returns None or ignores the error completely are also considered as "safe". For the sake of consistency: - *_or_none -> returns None if object was not found in the database - *_safe -> methods that ignores error completely (they basically log and/or are safe from exceptions) and renaming some of the current safe methods to normal ones that aren't safe by the new rules
1 parent adfc60b commit 44aa094

19 files changed

+240
-111
lines changed

CONTRIBUTE.md

+9
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,14 @@ normal form. Having numeric primary keys is a convention agreed among team
4040
members.
4141

4242

43+
## Safe, or_none methods
44+
45+
In sake of consistency we have two conventions when naming a method/function
46+
47+
- `*_safe`: Some methods are considered as "safe" in the sense of "safe from exceptions". They
48+
deal with the exceptions in proper way and/or logs the errors.
49+
- `*_or_none`: returns the desired output or if object was not found in the database they
50+
return None.
51+
4352

4453
[pep8]: https://www.python.org/dev/peps/pep-0008/

frontend/coprs_frontend/coprs/logic/complex_logic.py

+161-49
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def get_transitive_runtime_dependencies(cls, copr):
6565

6666
for dep in analyzed_copr.runtime_deps:
6767
try:
68-
copr_dep = cls.get_copr_by_repo_safe(dep)
68+
copr_dep = cls.get_copr_by_repo(dep)
6969
except exceptions.ObjectNotFound:
7070
non_existing.add(dep)
7171
continue
@@ -170,120 +170,232 @@ def fork_copr(cls, copr, user, dstname, dstgroup=None):
170170
return fcopr, created
171171

172172
@staticmethod
173-
def get_group_copr_safe(group_name, copr_name, **kwargs):
174-
group = ComplexLogic.get_group_by_name_safe(group_name)
173+
def get_group_copr(group_name, copr_name, **kwargs):
174+
"""
175+
Get group Copr by group and copr name.
176+
177+
Returns:
178+
Copr model
179+
180+
Raises:
181+
ObjectNotFound to API if nothing is found in database
182+
"""
183+
group = ComplexLogic.get_group_by_name(group_name)
175184
try:
176185
return CoprsLogic.get_by_group_id(
177186
group.id, copr_name, **kwargs).one()
178-
except sqlalchemy.orm.exc.NoResultFound:
187+
except sqlalchemy.orm.exc.NoResultFound as exc:
179188
raise ObjectNotFound(
180-
message="Project @{}/{} does not exist."
181-
.format(group_name, copr_name))
189+
message="Project @{}/{} does not exist.".format(
190+
group_name, copr_name
191+
)
192+
) from exc
182193

183194
@staticmethod
184-
def get_copr_safe(user_name, copr_name, **kwargs):
195+
def get_copr(user_name, copr_name, **kwargs):
185196
""" Get one project.
186197
187-
This always return personal project. For group projects see get_group_copr_safe().
198+
This always return personal project. For group projects see get_group_copr().
188199
"""
189200
try:
190201
return CoprsLogic.get(user_name, copr_name, **kwargs).filter(Copr.group_id.is_(None)).one()
191-
except sqlalchemy.orm.exc.NoResultFound:
202+
except sqlalchemy.orm.exc.NoResultFound as exc:
192203
raise ObjectNotFound(
193-
message="Project {}/{} does not exist."
194-
.format(user_name, copr_name))
204+
message="Project {}/{} does not exist.".format(
205+
user_name, copr_name
206+
)
207+
) from exc
195208

196209
@staticmethod
197-
def get_copr_by_owner_safe(owner_name, copr_name, **kwargs):
210+
def get_copr_by_owner(owner_name, copr_name, **kwargs):
211+
"""
212+
Get Copr by owner name and copr name.
213+
214+
Returns:
215+
Copr model
216+
217+
Raises:
218+
ObjectNotFound to API if nothing is found in database
219+
"""
198220
if owner_name[0] == "@":
199-
return ComplexLogic.get_group_copr_safe(owner_name[1:], copr_name, **kwargs)
200-
return ComplexLogic.get_copr_safe(owner_name, copr_name, **kwargs)
221+
return ComplexLogic.get_group_copr(owner_name[1:], copr_name, **kwargs)
222+
return ComplexLogic.get_copr(owner_name, copr_name, **kwargs)
201223

202224
@staticmethod
203-
def get_copr_by_repo_safe(repo_url):
225+
def get_copr_by_repo(repo_url):
226+
"""
227+
Safely get copr repo by repo url.
228+
229+
Args:
230+
repo_url: str
231+
232+
Returns:
233+
Copr repo or None in case of invalid url format or different url
234+
scheme than copr.
235+
236+
Raises:
237+
ObjectNotFound to the API if no such Copr (group) result was found
238+
in database.
239+
"""
204240
copr_repo = helpers.copr_repo_fullname(repo_url)
205241
if not copr_repo:
206242
return None
207243
try:
208244
owner, copr = copr_repo.split("/")
209-
except:
245+
except ValueError:
210246
# invalid format, e.g. multiple slashes in copr_repo
211247
return None
212-
return ComplexLogic.get_copr_by_owner_safe(owner, copr)
248+
return ComplexLogic.get_copr_by_owner(owner, copr)
213249

214250
@staticmethod
215-
def get_copr_dir_safe(ownername, copr_dirname, **kwargs):
251+
def get_copr_dir(ownername, copr_dirname):
252+
"""
253+
Get CoprDir by owner name and dir name.
254+
255+
Returns:
256+
CoprDir model
257+
258+
Raises:
259+
ObjectNotFound to the API if no result was found in database.
260+
"""
216261
try:
217262
return CoprDirsLogic.get_by_ownername(ownername, copr_dirname).one()
218-
except sqlalchemy.orm.exc.NoResultFound:
219-
raise ObjectNotFound(message="copr dir {}/{} does not exist."
220-
.format(ownername, copr_dirname))
263+
except sqlalchemy.orm.exc.NoResultFound as exc:
264+
raise ObjectNotFound(
265+
message="copr dir {}/{} does not exist.".format(
266+
ownername, copr_dirname
267+
)
268+
) from exc
221269

222270
@staticmethod
223-
def get_copr_by_id_safe(copr_id):
271+
def get_copr_by_id(copr_id):
272+
"""
273+
Get Copr by its ID.
274+
275+
Returns:
276+
Copr model
277+
278+
Raises:
279+
ObjectNotFound to the API if no such project with ID exists.
280+
"""
224281
try:
225282
return CoprsLogic.get_by_id(copr_id).one()
226-
except sqlalchemy.orm.exc.NoResultFound:
283+
except sqlalchemy.orm.exc.NoResultFound as exc:
227284
raise ObjectNotFound(
228-
message="Project with id {} does not exist."
229-
.format(copr_id))
285+
message="Project with id {} does not exist.".format(copr_id)
286+
) from exc
230287

231288
@staticmethod
232-
def get_build_safe(build_id):
289+
def get_build(build_id):
290+
"""
291+
Get Build by its ID.
292+
293+
Returns:
294+
Build model
295+
296+
Raises:
297+
ObjectNotFound to the API if no such build with ID exists.
298+
"""
233299
try:
234300
return BuildsLogic.get_by_id(build_id).one()
235-
except (sqlalchemy.orm.exc.NoResultFound,
236-
sqlalchemy.exc.DataError):
301+
except (sqlalchemy.orm.exc.NoResultFound, sqlalchemy.exc.DataError) as exc:
237302
raise ObjectNotFound(
238-
message="Build {} does not exist.".format(build_id))
303+
message="Build {} does not exist.".format(build_id)
304+
) from exc
239305

240306
@staticmethod
241307
def get_build_chroot(build_id, chrootname):
242308
"""
243-
Return a `models.BuildChroot` instance based on build ID and name of the chroot.
244-
If there is no such chroot, `ObjectNotFound` execption is raised.
309+
Get a BuildChroot instance based on build ID and name of the chroot.
310+
311+
Returns:
312+
BuildChroot model
313+
314+
Raises:
315+
If there is no such chroot, `ObjectNotFound` execption is raised.
245316
"""
246-
build = ComplexLogic.get_build_safe(build_id)
317+
build = ComplexLogic.get_build(build_id)
247318
try:
248319
return build.chroots_dict_by_name[chrootname]
249-
except KeyError:
320+
except KeyError as exc:
250321
msg = "Build {} was not submitted to {} chroot.".format(build_id, chrootname)
251322
if not MockChrootsLogic.get_from_name(chrootname).one_or_none():
252323
msg = "Chroot {} does not exist".format(chrootname)
253-
raise ObjectNotFound(message=msg)
324+
raise ObjectNotFound(message=msg) from exc
254325

255326
@staticmethod
256-
def get_package_by_id_safe(package_id):
327+
def get_package_by_id(package_id):
328+
"""
329+
Get Package instance based on its ID.
330+
331+
Returns:
332+
Package model
333+
334+
Raises:
335+
ObjectNotFound to the API if no such package with ID exists.
336+
"""
257337
try:
258338
return PackagesLogic.get_by_id(package_id).one()
259-
except sqlalchemy.orm.exc.NoResultFound:
339+
except sqlalchemy.orm.exc.NoResultFound as exc:
260340
raise ObjectNotFound(
261-
message="Package {} does not exist.".format(package_id))
341+
message="Package {} does not exist.".format(package_id)
342+
) from exc
262343

263344
@staticmethod
264-
def get_package_safe(copr, package_name):
345+
def get_package(copr, package_name):
346+
"""
347+
Get Package instance based on Copr instance and package name.
348+
349+
Returns:
350+
Package model
351+
352+
Raises:
353+
ObjectNotFound to the API if no such package with given name
354+
exists.
355+
"""
265356
try:
266357
return PackagesLogic.get(copr.id, package_name).one()
267-
except sqlalchemy.orm.exc.NoResultFound:
358+
except sqlalchemy.orm.exc.NoResultFound as exc:
268359
raise ObjectNotFound(
269-
message="Package {} in the copr {} does not exist."
270-
.format(package_name, copr))
360+
message="Package {} in the copr {} does not exist.".format(
361+
package_name, copr
362+
)
363+
) from exc
271364

272365
@staticmethod
273-
def get_group_by_name_safe(group_name):
366+
def get_group_by_name(group_name):
367+
"""
368+
Get Group instance based on a given name.
369+
370+
Returns:
371+
Group model
372+
373+
Raises:
374+
ObjectNotFound for the API if no such group name exists.
375+
"""
274376
try:
275377
group = UsersLogic.get_group_by_alias(group_name).one()
276-
except sqlalchemy.orm.exc.NoResultFound:
378+
except sqlalchemy.orm.exc.NoResultFound as exc:
277379
raise ObjectNotFound(
278-
message="Group {} does not exist.".format(group_name))
380+
message="Group {} does not exist.".format(group_name)
381+
) from exc
279382
return group
280383

281384
@staticmethod
282-
def get_copr_chroot_safe(copr, chroot_name):
385+
def get_copr_chroot(copr, chroot_name):
386+
"""
387+
Get CoprChroot by Copr model and chroot name.
388+
389+
Returns:
390+
CoprChroot model
391+
392+
Raises:
393+
ObjectNotFound for the API if no such chroot name exists in Copr.
394+
"""
283395
try:
284-
chroot = CoprChrootsLogic.get_by_name_safe(copr, chroot_name)
396+
chroot = CoprChrootsLogic.get_by_name_or_none(copr, chroot_name)
285397
except (ValueError, KeyError, RuntimeError) as e:
286-
raise ObjectNotFound(message=str(e))
398+
raise ObjectNotFound(message=str(e)) from e
287399

288400
if not chroot:
289401
raise ObjectNotFound(
@@ -552,14 +664,14 @@ def get_additional_repo_views(cls, repos_list, chroot_id):
552664
"name": "Additional repo " + helpers.generate_repo_name(repo),
553665
}
554666

555-
# We ask get_copr_by_repo_safe() here only to resolve the
667+
# We ask get_copr_by_repo() here only to resolve the
556668
# module_hotfixes attribute. If the asked project doesn't exist, we
557669
# still adjust the 'repos' variable -- the build will eventually
558670
# fail on repo downloading, but at least the copr maintainer will be
559671
# notified about the misconfiguration. Better than just skip the
560672
# repo.
561673
try:
562-
copr = ComplexLogic.get_copr_by_repo_safe(repo)
674+
copr = ComplexLogic.get_copr_by_repo(repo)
563675
except ObjectNotFound:
564676
copr = None
565677
if copr and copr.module_hotfixes:

frontend/coprs_frontend/coprs/logic/coprs_logic.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def request_permission(cls, copr, user, permission, req_bool):
622622

623623
class CoprDirsLogic(object):
624624
@classmethod
625-
def get_by_copr_safe(cls, copr, dirname):
625+
def get_by_copr_or_none(cls, copr, dirname):
626626
"""
627627
Return _query_ for getting CoprDir by Copr and dirname
628628
"""
@@ -637,7 +637,7 @@ def get_by_copr(cls, copr, dirname):
637637
Return CoprDir instance per given Copr instance and dirname. Raise
638638
ObjectNotFound if it doesn't exist.
639639
"""
640-
coprdir = cls.get_by_copr_safe(copr, dirname)
640+
coprdir = cls.get_by_copr_or_none(copr, dirname)
641641
if not coprdir:
642642
raise exceptions.ObjectNotFound(
643643
"Dirname '{}' doesn't exist in '{}' copr".format(
@@ -669,7 +669,7 @@ def get_or_create(cls, copr, dirname, trusted_caller=False):
669669
submitted. We don't create the "main" CoprDirs here (those are created
670670
when a new project is created.
671671
"""
672-
copr_dir = cls.get_by_copr_safe(copr, dirname)
672+
copr_dir = cls.get_by_copr_or_none(copr, dirname)
673673
if copr_dir:
674674
return copr_dir
675675

@@ -948,9 +948,17 @@ def get_by_name(cls, copr, chroot_name, active_only=True):
948948
return cls.get_by_mock_chroot_id(copr, mc.id)
949949

950950
@classmethod
951-
def get_by_name_safe(cls, copr, chroot_name):
951+
def get_by_name_or_none(cls, copr, chroot_name):
952952
"""
953-
:rtype: models.CoprChroot
953+
Get CoprChroot in Copr model by chroot name.
954+
955+
Args:
956+
copr: Copr model
957+
chroot_name: str
958+
959+
Returns:
960+
CoprChroot model or None if no such chroot name in given Copr
961+
model exists.
954962
"""
955963
try:
956964
return cls.get_by_name(copr, chroot_name).one()

frontend/coprs_frontend/coprs/views/apiv3_ns/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def get_copr(ownername=None, projectname=None):
123123
request = flask.request
124124
ownername = ownername or request.form.get("ownername") or request.json["ownername"]
125125
projectname = projectname or request.form.get("projectname") or request.json["projectname"]
126-
return ComplexLogic.get_copr_by_owner_safe(ownername, projectname)
126+
return ComplexLogic.get_copr_by_owner(ownername, projectname)
127127

128128

129129
class Paginator(object):

frontend/coprs_frontend/coprs/views/apiv3_ns/apiv3_build_chroots.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def to_dict(build_chroot):
1919

2020
def build_config(build_chroot):
2121
config = BuildConfigLogic.generate_build_config(build_chroot.build.copr, build_chroot.name)
22-
copr_chroot = CoprChrootsLogic.get_by_name_safe(build_chroot.build.copr, build_chroot.name)
22+
copr_chroot = CoprChrootsLogic.get_by_name_or_none(build_chroot.build.copr, build_chroot.name)
2323
dict_data = {
2424
"repos": config.get("repos"),
2525
"additional_repos": BuildConfigLogic.generate_additional_repos(copr_chroot),

0 commit comments

Comments
 (0)