More efficient detection of non-empty strings

main
Bob Mottram 2025-04-12 22:08:06 +01:00
parent a895212a04
commit 7b6eb5a414
11 changed files with 88 additions and 88 deletions

View File

@ -110,8 +110,7 @@ def _create_accept_reject(federation_list: [],
'object': object_json 'object': object_json
} }
if cc_url: if cc_url:
if len(cc_url) > 0: new_accept['cc'] = [cc_url]
new_accept['cc'] = [cc_url]
return new_accept return new_accept

View File

@ -206,8 +206,7 @@ def create_announce(session, base_dir: str, federation_list: [],
'type': 'Announce' 'type': 'Announce'
} }
if cc_url: if cc_url:
if len(cc_url) > 0: new_announce['cc'] = [cc_url]
new_announce['cc'] = [cc_url]
if save_to_file: if save_to_file:
outbox_dir = create_outbox_dir(nickname, domain, base_dir) outbox_dir = create_outbox_dir(nickname, domain, base_dir)
filename = \ filename = \

View File

@ -234,7 +234,7 @@ def post_login_screen(self, calling_domain: str, cookie: str,
login_str += login_prm + '=' login_str += login_prm + '='
else: else:
len_str = login_prm.split('&')[0] len_str = login_prm.split('&')[0]
if len(len_str) > 0: if len_str:
login_str += login_prm + '*' login_str += login_prm + '*'
len_str = '' len_str = ''
if '&' in login_prm: if '&' in login_prm:

View File

@ -104,8 +104,7 @@ def _create_like(recent_posts_cache: {},
'object': object_url 'object': object_url
} }
if cc_list: if cc_list:
if len(cc_list) > 0: new_like_json['cc'] = cc_list
new_like_json['cc'] = cc_list
# Extract the domain and nickname from a statuses link # Extract the domain and nickname from a statuses link
liked_post_nickname = None liked_post_nickname = None

4
pgp.py
View File

@ -917,7 +917,7 @@ def actor_to_vcard(actor: {}, domain: str, translate: {}) -> str:
vcard_str += \ vcard_str += \
'EXPERTISE;LEVEL=' + level_str + ':' + skill_name + '\n' 'EXPERTISE;LEVEL=' + level_str + ':' + skill_name + '\n'
if actor.get('hasOccupation'): if actor.get('hasOccupation'):
if len(actor['hasOccupation']) > 0: if actor['hasOccupation']:
if actor['hasOccupation'][0].get('name'): if actor['hasOccupation'][0].get('name'):
vcard_str += \ vcard_str += \
'ROLE:' + \ 'ROLE:' + \
@ -1048,7 +1048,7 @@ def actor_to_vcard_xml(actor: {}, domain: str, translate: {}) -> str:
'</parameters>' + \ '</parameters>' + \
'<text>' + pgp_key_encoded + '</text></key>\n' '<text>' + pgp_key_encoded + '</text></key>\n'
if actor.get('hasOccupation'): if actor.get('hasOccupation'):
if len(actor['hasOccupation']) > 0: if actor['hasOccupation']:
if actor['hasOccupation'][0].get('name'): if actor['hasOccupation'][0].get('name'):
vcard_str += \ vcard_str += \
' <role><text>' + \ ' <role><text>' + \

View File

@ -5692,7 +5692,7 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
print('EX: archive_posts_for_person unable to read ' + print('EX: archive_posts_for_person unable to read ' +
index_filename + ' ' + str(ex)) index_filename + ' ' + str(ex))
# save the new index file # save the new index file
if len(new_index) > 0: if new_index:
try: try:
with open(index_filename, 'w+', with open(index_filename, 'w+',
encoding='utf-8') as fp_index: encoding='utf-8') as fp_index:

View File

@ -567,7 +567,7 @@ def prepend_base(base, iri):
# directory from base # directory from base
if rel.path != '': if rel.path != '':
path = path[0:path.rfind('/') + 1] path = path[0:path.rfind('/') + 1]
if len(path) > 0 and not path.endswith('/'): if path and not path.endswith('/'):
path += '/' path += '/'
path += rel.path path += rel.path
@ -779,7 +779,7 @@ class JsonLdProcessor(object):
tmp = ctx tmp = ctx
ctx = [] ctx = []
for v in tmp: for v in tmp:
if not _is_object(v) or len(v) > 0: if not _is_object(v) or v:
ctx.append(v) ctx.append(v)
# remove array if only one context # remove array if only one context
@ -1265,7 +1265,7 @@ class JsonLdProcessor(object):
""" """
if property in subject: if property in subject:
value = subject[property] value = subject[property]
return not _is_array(value) or len(value) > 0 return not _is_array(value) or value
return False return False
@staticmethod @staticmethod

View File

@ -190,7 +190,7 @@ def get_instance_software(base_dir: str, session,
nodeinfo_url = None nodeinfo_url = None
if nodeinfo1_json.get('links'): if nodeinfo1_json.get('links'):
if isinstance(nodeinfo1_json['links'], list): if isinstance(nodeinfo1_json['links'], list):
if len(nodeinfo1_json['links']) > 0: if nodeinfo1_json['links']:
if nodeinfo1_json['links'][0].get('href'): if nodeinfo1_json['links'][0].get('href'):
href = nodeinfo1_json['links'][0]['href'] href = nodeinfo1_json['links'][0]['href']
if isinstance(href, str): if isinstance(href, str):

View File

@ -1336,12 +1336,13 @@ def html_profile(signing_priv_key_pem: str,
with open(follow_requests_filename, 'r', with open(follow_requests_filename, 'r',
encoding='utf-8') as fp_foll: encoding='utf-8') as fp_foll:
for line in fp_foll: for line in fp_foll:
if len(line) > 0: if not line:
follow_approvals = True continue
followers_button = 'buttonhighlighted' follow_approvals = True
if selected == 'followers': followers_button = 'buttonhighlighted'
followers_button = 'buttonselectedhighlighted' if selected == 'followers':
break followers_button = 'buttonselectedhighlighted'
break
except OSError as exc: except OSError as exc:
print('EX: html_profile unable to read ' + print('EX: html_profile unable to read ' +
follow_requests_filename + ' ' + str(exc)) follow_requests_filename + ' ' + str(exc))
@ -1352,59 +1353,60 @@ def html_profile(signing_priv_key_pem: str,
with open(follow_requests_filename, 'r', with open(follow_requests_filename, 'r',
encoding='utf-8') as fp_req: encoding='utf-8') as fp_req:
for follower_handle in fp_req: for follower_handle in fp_req:
if len(follower_handle) > 0: if not follower_handle:
follower_handle = \ continue
remove_eol(follower_handle) follower_handle = \
if '://' in follower_handle: remove_eol(follower_handle)
follower_actor = follower_handle if '://' in follower_handle:
else: follower_actor = follower_handle
nick = follower_handle.split('@')[0] else:
dom = follower_handle.split('@')[1] nick = follower_handle.split('@')[0]
follower_actor = \ dom = follower_handle.split('@')[1]
local_actor_url(http_prefix, nick, dom) follower_actor = \
local_actor_url(http_prefix, nick, dom)
# is this a new domain? # is this a new domain?
# if so then append a new instance indicator # if so then append a new instance indicator
follower_domain, _ = \ follower_domain, _ = \
get_domain_from_actor(follower_actor) get_domain_from_actor(follower_actor)
new_follower_domain = '' new_follower_domain = ''
if follower_domain not in curr_follower_domains: if follower_domain not in curr_follower_domains:
new_follower_domain = '' new_follower_domain = ''
# Show the handle of the potential follower # Show the handle of the potential follower
# being approved, linking to search on that handle # being approved, linking to search on that handle
base_path = '/users/' + nickname base_path = '/users/' + nickname
follow_approvals_section += \ follow_approvals_section += \
'<div class="container">\n' + \ '<div class="container">\n' + \
' <form method="POST" action="' + \ ' <form method="POST" action="' + \
base_path + '/searchhandle?page=1">\n' + \ base_path + '/searchhandle?page=1">\n' + \
' <input type="hidden" ' + \ ' <input type="hidden" ' + \
'name="actor" value="' + actor + '">\n' + \ 'name="actor" value="' + actor + '">\n' + \
' <input type="hidden" ' + \ ' <input type="hidden" ' + \
'name="searchtext" value="' + \ 'name="searchtext" value="' + \
follower_actor + \ follower_actor + \
'">\n <button type="submit" ' + \ '">\n <button type="submit" ' + \
'class="followApproveHandle" ' + \ 'class="followApproveHandle" ' + \
'name="submitSearch" tabindex="2">' + \ 'name="submitSearch" tabindex="2">' + \
follower_handle + new_follower_domain + \ follower_handle + new_follower_domain + \
'</button>\n </form>\n' '</button>\n </form>\n'
# show Approve and Deny buttons # show Approve and Deny buttons
follow_approvals_section += \ follow_approvals_section += \
'<a href="' + base_path + \ '<a href="' + base_path + \
'/followapprove=' + follower_handle + \ '/followapprove=' + follower_handle + \
'" tabindex="2">' '" tabindex="2">'
follow_approvals_section += \ follow_approvals_section += \
'<button class="followApprove">' + \ '<button class="followApprove">' + \
translate['Approve'] + '</button></a><br><br>' translate['Approve'] + '</button></a><br><br>'
follow_approvals_section += \ follow_approvals_section += \
'<a href="' + base_path + \ '<a href="' + base_path + \
'/followdeny=' + follower_handle + \ '/followdeny=' + follower_handle + \
'" tabindex="3">' '" tabindex="3">'
follow_approvals_section += \ follow_approvals_section += \
'<button class="followDeny">' + \ '<button class="followDeny">' + \
translate['Deny'] + '</button></a>' translate['Deny'] + '</button></a>'
follow_approvals_section += '</div>' follow_approvals_section += '</div>'
profile_description_short = \ profile_description_short = \
_get_profile_short_description(profile_description) _get_profile_short_description(profile_description)
@ -1950,7 +1952,7 @@ def _html_profile_roles(translate: {}, nickname: str, domain: str,
else: else:
profile_str += '<h3>' + role + '</h3>\n' profile_str += '<h3>' + role + '</h3>\n'
profile_str += '</div></div>\n' profile_str += '</div></div>\n'
if len(profile_str) == 0: if not profile_str:
profile_str += \ profile_str += \
'<p>@' + nickname + '@' + domain + ' has no roles assigned</p>\n' '<p>@' + nickname + '@' + domain + ' has no roles assigned</p>\n'
else: else:
@ -1967,7 +1969,7 @@ def _html_profile_skills(skills_json: {}) -> str:
'<div>' + skill + \ '<div>' + skill + \
'<br><div id="myProgress"><div id="myBar" style="width:' + \ '<br><div id="myProgress"><div id="myBar" style="width:' + \
str(level) + '%"></div></div></div>\n<br>\n' str(level) + '%"></div></div></div>\n<br>\n'
if len(profile_str) > 0: if profile_str:
profile_str = '<center><div class="skill-title">' + \ profile_str = '<center><div class="skill-title">' + \
profile_str + '</div></center>\n' profile_str + '</div></center>\n'
return profile_str return profile_str
@ -1984,7 +1986,7 @@ def _html_profile_shares(actor: str, translate: {},
actor, item, translate, actor, item, translate,
False, False, False, False,
shares_file_type) shares_file_type)
if len(profile_str) > 0: if profile_str:
profile_str = '<div class="share-title">' + profile_str + '</div>\n' profile_str = '<div class="share-title">' + profile_str + '</div>\n'
return profile_str return profile_str

View File

@ -707,20 +707,21 @@ def html_timeline(default_timeline: str,
with open(follow_requests_filename, 'r', with open(follow_requests_filename, 'r',
encoding='utf-8') as fp_foll: encoding='utf-8') as fp_foll:
for line in fp_foll: for line in fp_foll:
if len(line) > 0: if not line:
# show follow approvals icon continue
follow_approvals = \ # show follow approvals icon
'<a href="' + users_path + \ follow_approvals = \
'/followers#buttonheader" ' + \ '<a href="' + users_path + \
'accesskey="' + \ '/followers#buttonheader" ' + \
access_keys['followButton'] + '">' + \ 'accesskey="' + \
'<img loading="lazy" decoding="async" ' + \ access_keys['followButton'] + '">' + \
'class="timelineicon" alt="' + \ '<img loading="lazy" decoding="async" ' + \
translate['Approve follow requests'] + \ 'class="timelineicon" alt="' + \
'" title="' + \ translate['Approve follow requests'] + \
translate['Approve follow requests'] + \ '" title="' + \
'" src="/icons/person.png"/></a>\n' translate['Approve follow requests'] + \
break '" src="/icons/person.png"/></a>\n'
break
except OSError: except OSError:
print('EX: html_timeline unable to read ' + print('EX: html_timeline unable to read ' +
follow_requests_filename) follow_requests_filename)

View File

@ -1457,7 +1457,7 @@ def get_post_attachments_as_html(base_dir: str,
media_creator = attach['schema:creator'] media_creator = attach['schema:creator']
elif attach.get('attribution'): elif attach.get('attribution'):
if isinstance(attach['attribution'], list): if isinstance(attach['attribution'], list):
if len(attach['attribution']) > 0: if attach['attribution']:
attrib_str = attach['attribution'][0] attrib_str = attach['attribution'][0]
if not dangerous_markup(attrib_str, False, []): if not dangerous_markup(attrib_str, False, []):
if not is_filtered(base_dir, nickname, domain, if not is_filtered(base_dir, nickname, domain,