Skip to content

Commit 9477d11

Browse files
committed
For #28441: refactored to clean up duplication in flag checks
1 parent 75315e0 commit 9477d11

File tree

2 files changed

+55
-54
lines changed

2 files changed

+55
-54
lines changed

shotgun_api3/shotgun.py

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def __init__(self, host, meta):
129129
self.is_dev = False
130130

131131
self.version = tuple(self.version[:3])
132-
self._ensure_json_supported()
132+
self.ensure_json_supported()
133133

134134

135135
def _ensure_support(self, feature):
@@ -146,25 +146,28 @@ def _ensure_support(self, feature):
146146
"%s requires server version %s or higher, "\
147147
"server is %s" % (feature['label'], _version_str(feature['version']), _version_str(self.version))
148148
)
149+
return False
150+
else:
151+
return True
149152

150153

151-
def _ensure_json_supported(self):
154+
def ensure_json_supported(self):
152155
"""Wrapper for ensure_support"""
153-
self._ensure_support({
156+
return self._ensure_support({
154157
'version': (2, 4, 0),
155158
'label': 'JSON API'
156159
})
157160

158161
def ensure_include_archived_projects(self):
159162
"""Wrapper for ensure_support"""
160-
self._ensure_support({
163+
return self._ensure_support({
161164
'version': (5, 3, 14),
162165
'label': 'include_archived_projects parameter'
163166
})
164167

165168
def ensure_include_template_projects(self):
166169
"""Wrapper for ensure_support"""
167-
self._ensure_support({
170+
return self._ensure_support({
168171
'version': (6, 0, 0),
169172
'label': 'include_template_projects parameter'
170173
})
@@ -466,7 +469,7 @@ def find_one(self, entity_type, filters, fields=None, order=None,
466469
:param include_template_projects: Optional, flag to include entities
467470
belonging to template projects. Default: False
468471
469-
:returns: Result
472+
:returns: dict of requested entity's fields, or None if not found.
470473
"""
471474

472475
results = self.find(entity_type, filters, fields, order,
@@ -529,22 +532,13 @@ def find(self, entity_type, filters, fields=None, order=None,
529532
raise ShotgunError("Deprecated: Use of filter_operator for find()"
530533
" is not valid any more. See the documentation on find()")
531534

532-
if not include_archived_projects:
533-
# This defaults to True on the server (no argument is sent)
534-
# So we only need to check the server version if it is False
535-
self.server_caps.ensure_include_archived_projects()
536-
537-
if include_template_projects:
538-
# This defaults to False on the server (no argument is sent)
539-
# So we only need to check the server version if it is True
540-
self.server_caps.ensure_include_template_projects()
541-
542-
543535
params = self._construct_read_parameters(entity_type,
544536
fields,
545537
filters,
546538
retired_only,
547-
order,
539+
order)
540+
541+
params = self._construct_flag_parameters(params,
548542
include_archived_projects,
549543
include_template_projects)
550544

@@ -583,31 +577,24 @@ def find(self, entity_type, filters, fields=None, order=None,
583577
return self._parse_records(records)
584578

585579

586-
587580
def _construct_read_parameters(self,
588581
entity_type,
589582
fields,
590583
filters,
591584
retired_only,
592-
order,
593-
include_archived_projects,
594-
include_template_projects):
595-
params = {}
596-
params["type"] = entity_type
597-
params["return_fields"] = fields or ["id"]
598-
params["filters"] = filters
599-
params["return_only"] = (retired_only and 'retired') or "active"
600-
params["return_paging_info"] = True
601-
params["paging"] = { "entities_per_page": self.config.records_per_page,
602-
"current_page": 1 }
603-
604-
if include_archived_projects is False:
605-
# Defaults to True on the server, so only pass it if it's False
606-
params["include_archived_projects"] = False
585+
order):
607586

608-
if include_template_projects is True:
609-
# Defaults to False on the server, so only pass it if it's True
610-
params["include_template_projects"] = True
587+
params = {
588+
"type": entity_type,
589+
"return_fields": fields or ["id"],
590+
"filters": filters,
591+
"return_only": (retired_only and 'retired') or "active",
592+
"return_paging_info": True,
593+
"paging": {
594+
"entities_per_page": self.config.records_per_page,
595+
"current_page": 1
596+
}
597+
}
611598

612599
if order:
613600
sort_list = []
@@ -621,8 +608,32 @@ def _construct_read_parameters(self,
621608
'direction' : sort['direction']
622609
})
623610
params['sorts'] = sort_list
611+
612+
return params
613+
614+
615+
def _construct_flag_parameters(self,
616+
params,
617+
include_archived_projects,
618+
include_template_projects):
619+
620+
if not include_archived_projects:
621+
# This defaults to True on the server (no argument is sent)
622+
# So we only need to check the server version if it's False
623+
self.server_caps.ensure_include_archived_projects()
624+
# Only pass it if it's False
625+
params["include_archived_projects"] = False
626+
627+
if include_template_projects:
628+
# This defaults to False on the server (no argument is sent)
629+
# So we only need to check the server version if it's True
630+
self.server_caps.ensure_include_template_projects()
631+
# Only pass it if it's True
632+
params["include_template_projects"] = True
633+
624634
return params
625635

636+
626637
def summarize(self,
627638
entity_type,
628639
filters,
@@ -647,19 +658,9 @@ def summarize(self,
647658
"summaries": summary_fields,
648659
"filters": filters}
649660

650-
if not include_archived_projects:
651-
# This defaults to True on the server (no argument is sent)
652-
# So we only need to check the server version if it is False
653-
self.server_caps.ensure_include_archived_projects()
654-
# Only pass it if it's False
655-
params["include_archived_projects"] = False
656-
657-
if include_template_projects:
658-
# This defaults to False on the server (no argument is sent)
659-
# So we only need to check the server version if it is True
660-
self.server_caps.ensure_include_template_projects()
661-
# Only pass it if it's True
662-
params["include_template_projects"] = True
661+
params = self._construct_flag_parameters(params,
662+
include_archived_projects,
663+
include_template_projects)
663664

664665
if grouping != None:
665666
params['grouping'] = grouping

tests/test_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@ def test_server_version_json(self):
7474
sc = ServerCapabilities("foo", {"version" : (2,4,0)})
7575

7676
sc.version = (2,3,99)
77-
self.assertRaises(api.ShotgunError, sc._ensure_json_supported)
77+
self.assertRaises(api.ShotgunError, sc.ensure_json_supported)
7878
self.assertRaises(api.ShotgunError, ServerCapabilities, "foo",
7979
{"version" : (2,2,0)})
8080

8181
sc.version = (0,0,0)
82-
self.assertRaises(api.ShotgunError, sc._ensure_json_supported)
82+
self.assertRaises(api.ShotgunError, sc.ensure_json_supported)
8383

8484
sc.version = (2,4,0)
85-
sc._ensure_json_supported()
85+
sc.ensure_json_supported()
8686

8787
sc.version = (2,5,0)
88-
sc._ensure_json_supported()
88+
sc.ensure_json_supported()
8989

9090

9191
def test_session_uuid(self):

0 commit comments

Comments
 (0)