diff --git a/blog.py b/blog.py index 7487f7117..e9440c1ef 100644 --- a/blog.py +++ b/blog.py @@ -704,7 +704,7 @@ def _no_of_blog_accounts(base_dir: str) -> int: """Returns the number of blog accounts """ ctr = 0 - for subdir, dirs, files in os.walk(base_dir + '/accounts'): + for _, dirs, _ in os.walk(base_dir + '/accounts'): for acct in dirs: if not is_account_dir(acct): continue @@ -719,7 +719,7 @@ def _no_of_blog_accounts(base_dir: str) -> int: def _single_blog_account_nickname(base_dir: str) -> str: """Returns the nickname of a single blog account """ - for subdir, dirs, files in os.walk(base_dir + '/accounts'): + for _, dirs, _ in os.walk(base_dir + '/accounts'): for acct in dirs: if not is_account_dir(acct): continue diff --git a/categories.py b/categories.py index acefee5e3..3a0838a61 100644 --- a/categories.py +++ b/categories.py @@ -50,7 +50,7 @@ def get_hashtag_categories(base_dir: str, days_since_epoch = (curr_time - datetime.datetime(1970, 1, 1)).days recently = days_since_epoch - 1 - for subdir, dirs, files in os.walk(base_dir + '/tags'): + for _, _, files in os.walk(base_dir + '/tags'): for catfile in files: if not catfile.endswith('.category'): continue diff --git a/content.py b/content.py index 5dfc5c03b..24533e741 100644 --- a/content.py +++ b/content.py @@ -380,7 +380,6 @@ def _update_common_emoji(base_dir: str, emoji_content: str) -> None: common_emoji = fp_emoji.readlines() except OSError: print('EX: unable to load common emoji file') - pass if common_emoji: new_common_emoji = [] emoji_found = False @@ -1579,7 +1578,7 @@ def import_emoji(base_dir: str, import_filename: str, session) -> None: def content_diff(content: str, prev_content: str) -> str: """Returns a diff for the given content """ - d = difflib.Differ() + cdiff = difflib.Differ() text1_lines = content.splitlines() text1_sentences = [] for line in text1_lines: @@ -1594,7 +1593,7 @@ def content_diff(content: str, prev_content: str) -> str: for sentence in sentences: text2_sentences.append(sentence.strip()) - diff = d.compare(text1_sentences, text2_sentences) + diff = cdiff.compare(text1_sentences, text2_sentences) diff_text = '' for line in diff: @@ -1629,7 +1628,7 @@ def create_edits_html(edits_json: {}, post_json_object: {}, if not post_json_object['object'].get('contentMap'): return '' edit_dates_list = [] - for modified, item in edits_json.items(): + for modified, _ in edits_json.items(): edit_dates_list.append(modified) edit_dates_list.sort(reverse=True) edits_str = '' @@ -1708,11 +1707,11 @@ def remove_script(content: str, log_filename: str, if log_filename and actor: # write the detected script to a log file log_str = actor + ' ' + url + ' ' + text + '\n' - writeType = 'a+' + write_type = 'a+' if os.path.isfile(log_filename): - writeType = 'w+' + write_type = 'w+' try: - with open(log_filename, writeType) as fp_log: + with open(log_filename, write_type) as fp_log: fp_log.write(log_str) except OSError: print('EX: cannot append to svg script log') diff --git a/cwtch.py b/cwtch.py index 4849ac014..7fa1df6eb 100644 --- a/cwtch.py +++ b/cwtch.py @@ -100,7 +100,7 @@ def set_cwtch_address(actor_json: {}, cwtch_address: str) -> None: continue if not property_value['type'].endswith('PropertyValue'): continue - prop_value_name, prop_value = \ + prop_value_name, _ = \ get_attachment_property_value(property_value) if not prop_value_name: continue diff --git a/daemon.py b/daemon.py index 3e2d766be..49a0f9491 100644 --- a/daemon.py +++ b/daemon.py @@ -2230,12 +2230,12 @@ class PubServer(BaseHTTPRequestHandler): moderation_text = None moderation_button = None # get the moderation text first - actStr = 'moderationAction=' + act_str = 'moderationAction=' for moderation_str in moderation_params.split('&'): - if moderation_str.startswith(actStr): - if actStr in moderation_str: + if moderation_str.startswith(act_str): + if act_str in moderation_str: moderation_text = \ - moderation_str.split(actStr)[1].strip() + moderation_str.split(act_str)[1].strip() mod_text = moderation_text.replace('+', ' ') moderation_text = \ urllib.parse.unquote_plus(mod_text.strip()) @@ -4672,7 +4672,7 @@ class PubServer(BaseHTTPRequestHandler): print('origin_path_str: ' + origin_path_str) print('remove_post_actor: ' + remove_post_actor) if origin_path_str in remove_post_actor: - toList = [ + to_list = [ 'https://www.w3.org/ns/activitystreams#Public', remove_post_actor ] @@ -4680,7 +4680,7 @@ class PubServer(BaseHTTPRequestHandler): "@context": "https://www.w3.org/ns/activitystreams", 'actor': remove_post_actor, 'object': remove_message_id, - 'to': toList, + 'to': to_list, 'cc': [remove_post_actor + '/followers'], 'type': 'Delete' } @@ -5461,7 +5461,7 @@ class PubServer(BaseHTTPRequestHandler): acct_dir(base_dir, nickname, domain) + \ '/' + m_type + '.temp' - filename, attachment_media_type = \ + filename, _ = \ save_media_in_form_post(media_bytes, debug, filename_base) if filename: @@ -14956,16 +14956,16 @@ class PubServer(BaseHTTPRequestHandler): calling_domain: str, referer_domain: str, http_prefix: str, domain: str, port: int, - followingItemsPerPage: int, - debug: bool, listName='following') -> None: + following_items_per_page: int, + debug: bool, list_name: str = 'following') -> None: """Returns json collection for following.txt """ following_json = \ get_following_feed(base_dir, domain, port, path, http_prefix, - True, followingItemsPerPage, listName) + True, following_items_per_page, list_name) if not following_json: if debug: - print(listName + ' json feed not found for ' + path) + print(list_name + ' json feed not found for ' + path) self._404() return msg_str = json.dumps(following_json, @@ -15280,7 +15280,6 @@ class PubServer(BaseHTTPRequestHandler): known_bots_str = '' for bot_name in self.server.known_bots: known_bots_str += bot_name + '\n' - # TODO msg = known_bots_str.encode('utf-8') msglen = len(msg) self._set_headers('text/plain; charset=utf-8', @@ -16395,8 +16394,7 @@ class PubServer(BaseHTTPRequestHandler): '_GET', 'show welcome screen', self.server.debug) return - else: - self.path = self.path.replace('/welcome', '') + self.path = self.path.replace('/welcome', '') # the welcome screen which allows you to set an avatar image if html_getreq and authorized and \ @@ -18556,10 +18554,11 @@ class PubServer(BaseHTTPRequestHandler): return if response_str == 'Not modified': if endpoint_type == 'put': - return self._200() - else: - return self._304() - elif response_str.startswith('ETag:') and endpoint_type == 'put': + self._200() + return + self._304() + return + if response_str.startswith('ETag:') and endpoint_type == 'put': response_etag = response_str.split('ETag:', 1)[1] self._201(response_etag) elif response_str != 'Ok': @@ -19483,7 +19482,7 @@ class PubServer(BaseHTTPRequestHandler): curr_session, proxy_type): return 1 return -1 - elif post_type == 'newshare' or post_type == 'newwanted': + elif post_type in ('newshare', 'newwanted'): if not fields.get('itemQty'): print(post_type + ' no itemQty') return -1 diff --git a/delete.py b/delete.py index 00e83d33a..6bb3ba30d 100644 --- a/delete.py +++ b/delete.py @@ -28,7 +28,7 @@ from posts import get_person_box def send_delete_via_server(base_dir: str, session, from_nickname: str, password: str, - from_domain: str, fromPort: int, + from_domain: str, from_port: int, http_prefix: str, delete_object_url: str, cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, @@ -39,7 +39,7 @@ def send_delete_via_server(base_dir: str, session, print('WARN: No session for send_delete_via_server') return 6 - from_domain_full = get_full_domain(from_domain, fromPort) + from_domain_full = get_full_domain(from_domain, from_port) actor = local_actor_url(http_prefix, from_nickname, from_domain_full) to_url = 'https://www.w3.org/ns/activitystreams#Public' @@ -178,8 +178,7 @@ def outbox_delete(base_dir: str, http_prefix: str, def remove_old_hashtags(base_dir: str, max_months: int) -> str: """Remove old hashtags """ - if max_months > 11: - max_months = 11 + max_months = min(max_months, 11) max_days_since_epoch = \ (datetime.utcnow() - datetime(1970, 1 + max_months, 1)).days remove_hashtags = [] diff --git a/enigma.py b/enigma.py index 0612548b5..1550597c4 100644 --- a/enigma.py +++ b/enigma.py @@ -65,7 +65,7 @@ def set_enigma_pub_key(actor_json: {}, enigma_pub_key: str) -> None: property_found = property_value break if property_found: - actor_json['attachment'].remove(property_value) + actor_json['attachment'].remove(property_found) if remove_key: return diff --git a/feeds.py b/feeds.py index edb40f108..6b91885c3 100644 --- a/feeds.py +++ b/feeds.py @@ -9,6 +9,8 @@ __module_group__ = "RSS Feeds" def rss2tag_header(hashtag: str, http_prefix: str, domain_full: str) -> str: + """Header for rss 2 + """ return \ "" + \ "" + \ @@ -19,4 +21,6 @@ def rss2tag_header(hashtag: str, http_prefix: str, domain_full: str) -> str: def rss2tag_footer() -> str: + """Footer for rss 2 + """ return '' diff --git a/follow.py b/follow.py index dc234e15f..503377fac 100644 --- a/follow.py +++ b/follow.py @@ -560,8 +560,7 @@ def get_following_feed(base_dir: str, domain: str, port: int, path: str, curr_page += 1 following['totalItems'] = total_ctr last_page = int(total_ctr / follows_per_page) - if last_page < 1: - last_page = 1 + last_page = max(last_page, 1) if next_page_number > last_page: following['next'] = \ local_actor_url(http_prefix, nickname, domain) + \ @@ -731,10 +730,10 @@ def followed_account_accepts(session, base_dir: str, http_prefix: str, port: int, nickname: str, domain: str, from_port: int, person_url: str, federation_list: [], - follow_json: {}, send_threads: [], postLog: [], + follow_json: {}, send_threads: [], post_log: [], cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, - removeFollowActivity: bool, + remove_follow_activity: bool, signing_priv_key_pem: str, curr_domain: str, onion_domain: str, i2p_domain: str): @@ -759,7 +758,7 @@ def followed_account_accepts(session, base_dir: str, http_prefix: str, accept_handle + ' port ' + str(from_port)) client_to_server = False - if removeFollowActivity: + if remove_follow_activity: # remove the follow request json follow_activity_filename = \ acct_dir(base_dir, nickname_to_follow, domain_to_follow) + \ @@ -783,7 +782,7 @@ def followed_account_accepts(session, base_dir: str, http_prefix: str, nickname, domain, from_port, '', http_prefix, True, client_to_server, federation_list, - send_threads, postLog, cached_webfingers, + send_threads, post_log, cached_webfingers, person_cache, debug, project_version, None, group_account, signing_priv_key_pem, 7856837, curr_domain, onion_domain, i2p_domain) @@ -796,7 +795,7 @@ def followed_account_rejects(session, session_onion, session_i2p, port: int, nickname: str, domain: str, from_port: int, federation_list: [], - send_threads: [], postLog: [], + send_threads: [], post_log: [], cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, signing_priv_key_pem: str): @@ -853,7 +852,7 @@ def followed_account_rejects(session, session_onion, session_i2p, nickname, domain, from_port, '', http_prefix, True, client_to_server, federation_list, - send_threads, postLog, cached_webfingers, + send_threads, post_log, cached_webfingers, person_cache, debug, project_version, None, group_account, signing_priv_key_pem, 6393063, @@ -865,10 +864,10 @@ def send_follow_request(session, base_dir: str, sender_domain: str, sender_port: int, http_prefix: str, follow_nickname: str, follow_domain: str, - followedActor: str, - followPort: int, followHttpPrefix: str, + followed_actor: str, + follow_port: int, follow_http_prefix: str, client_to_server: bool, federation_list: [], - send_threads: [], postLog: [], cached_webfingers: {}, + send_threads: [], post_log: [], cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, signing_priv_key_pem: str, curr_domain: str, @@ -885,22 +884,22 @@ def send_follow_request(session, base_dir: str, full_domain = get_full_domain(sender_domain, sender_port) follow_actor = local_actor_url(http_prefix, nickname, full_domain) - request_domain = get_full_domain(follow_domain, followPort) + request_domain = get_full_domain(follow_domain, follow_port) status_number, _ = get_status_number() group_account = False if follow_nickname: - followed_id = followedActor + followed_id = followed_actor follow_handle = follow_nickname + '@' + request_domain - group_account = has_group_type(base_dir, followedActor, person_cache) + group_account = has_group_type(base_dir, followed_actor, person_cache) if group_account: follow_handle = '!' + follow_handle print('Follow request being sent to group account') else: if debug: print('DEBUG: send_follow_request - assuming single user instance') - followed_id = followHttpPrefix + '://' + request_domain + followed_id = follow_http_prefix + '://' + request_domain single_user_nickname = 'dev' follow_handle = single_user_nickname + '@' + request_domain @@ -947,11 +946,11 @@ def send_follow_request(session, base_dir: str, send_signed_json(new_follow_json, session, base_dir, nickname, sender_domain, sender_port, - follow_nickname, follow_domain, followPort, + follow_nickname, follow_domain, follow_port, 'https://www.w3.org/ns/activitystreams#Public', http_prefix, True, client_to_server, federation_list, - send_threads, postLog, cached_webfingers, person_cache, + send_threads, post_log, cached_webfingers, person_cache, debug, project_version, None, group_account, signing_priv_key_pem, 8234389, curr_domain, onion_domain, i2p_domain) @@ -963,7 +962,7 @@ def send_follow_request_via_server(base_dir: str, session, from_nickname: str, password: str, from_domain: str, from_port: int, follow_nickname: str, follow_domain: str, - followPort: int, + follow_port: int, http_prefix: str, cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, @@ -976,7 +975,7 @@ def send_follow_request_via_server(base_dir: str, session, from_domain_full = get_full_domain(from_domain, from_port) - follow_domain_full = get_full_domain(follow_domain, followPort) + follow_domain_full = get_full_domain(follow_domain, follow_port) follow_actor = \ local_actor_url(http_prefix, from_nickname, from_domain_full) @@ -1055,7 +1054,7 @@ def send_unfollow_request_via_server(base_dir: str, session, from_nickname: str, password: str, from_domain: str, from_port: int, follow_nickname: str, follow_domain: str, - followPort: int, + follow_port: int, http_prefix: str, cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, @@ -1067,13 +1066,13 @@ def send_unfollow_request_via_server(base_dir: str, session, return 6 from_domain_full = get_full_domain(from_domain, from_port) - follow_domain_full = get_full_domain(follow_domain, followPort) + follow_domain_full = get_full_domain(follow_domain, follow_port) follow_actor = \ local_actor_url(http_prefix, from_nickname, from_domain_full) followed_id = \ http_prefix + '://' + follow_domain_full + '/@' + follow_nickname - status_number, published = get_status_number() + status_number, _ = get_status_number() unfollow_json = { '@context': 'https://www.w3.org/ns/activitystreams', @@ -1173,13 +1172,12 @@ def get_following_via_server(base_dir: str, session, 'Authorization': auth_header } - if page_number < 1: - page_number = 1 + page_number = max(page_number, 1) url = follow_actor + '/following?page=' + str(page_number) - followingJson = \ + following_json = \ get_json(signing_priv_key_pem, session, url, headers, {}, debug, __version__, http_prefix, domain, 10, True) - if not followingJson: + if not following_json: if debug: print('DEBUG: GET following list failed for c2s to ' + url) return 5 @@ -1187,7 +1185,7 @@ def get_following_via_server(base_dir: str, session, if debug: print('DEBUG: c2s GET following list request success') - return followingJson + return following_json def get_followers_via_server(base_dir: str, session, @@ -1255,8 +1253,7 @@ def get_follow_requests_via_server(base_dir: str, session, 'Authorization': auth_header } - if page_number < 1: - page_number = 1 + page_number = max(page_number, 1) url = follow_actor + '/followrequests?page=' + str(page_number) followers_json = \ get_json(signing_priv_key_pem, session, url, headers, {}, debug, diff --git a/httpsig.py b/httpsig.py index 45aeb4268..2d80023e8 100644 --- a/httpsig.py +++ b/httpsig.py @@ -62,7 +62,7 @@ def get_digest_algorithm_from_headers(http_headers: {}) -> str: return 'rsa-sha256' -def sign_post_headers(dateStr: str, private_key_pem: str, +def sign_post_headers(date_str: str, private_key_pem: str, nickname: str, domain: str, port: int, to_domain: str, to_port: int, path: str, http_prefix: str, @@ -76,8 +76,8 @@ def sign_post_headers(dateStr: str, private_key_pem: str, to_domain = get_full_domain(to_domain, to_port) - if not dateStr: - dateStr = strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()) + if not date_str: + date_str = strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()) if nickname != domain and nickname.lower() != 'actor': key_id = local_actor_url(http_prefix, nickname, domain) else: @@ -88,7 +88,7 @@ def sign_post_headers(dateStr: str, private_key_pem: str, headers = { '(request-target)': f'get {path}', 'host': to_domain, - 'date': dateStr, + 'date': date_str, 'accept': content_type } else: @@ -99,7 +99,7 @@ def sign_post_headers(dateStr: str, private_key_pem: str, headers = { '(request-target)': f'post {path}', 'host': to_domain, - 'date': dateStr, + 'date': date_str, 'digest': f'{digest_prefix}={body_digest}', 'content-type': 'application/activity+json', 'content-length': str(content_length) @@ -138,7 +138,7 @@ def sign_post_headers(dateStr: str, private_key_pem: str, return signature_header -def sign_post_headers_new(dateStr: str, private_key_pem: str, +def sign_post_headers_new(date_str: str, private_key_pem: str, nickname: str, domain: str, port: int, to_domain: str, to_port: int, @@ -157,11 +157,11 @@ def sign_post_headers_new(dateStr: str, private_key_pem: str, to_domain = get_full_domain(to_domain, to_port) time_format = "%a, %d %b %Y %H:%M:%S %Z" - if not dateStr: + if not date_str: curr_time = gmtime() - dateStr = strftime(time_format, curr_time) + date_str = strftime(time_format, curr_time) else: - curr_time = datetime.datetime.strptime(dateStr, time_format) + curr_time = datetime.datetime.strptime(date_str, time_format) seconds_since_epoch = \ int((curr_time - datetime.datetime(1970, 1, 1)).total_seconds()) key_id = local_actor_url(http_prefix, nickname, domain) + '#main-key' @@ -170,7 +170,7 @@ def sign_post_headers_new(dateStr: str, private_key_pem: str, '@request-target': f'get {path}', '@created': str(seconds_since_epoch), 'host': to_domain, - 'date': dateStr + 'date': date_str } else: body_digest = message_content_digest(message_body_json_str, @@ -181,7 +181,7 @@ def sign_post_headers_new(dateStr: str, private_key_pem: str, '@request-target': f'post {path}', '@created': str(seconds_since_epoch), 'host': to_domain, - 'date': dateStr, + 'date': date_str, 'digest': f'{digest_prefix}={body_digest}', 'content-type': 'application/activity+json', 'content-length': str(content_length) @@ -234,10 +234,10 @@ def sign_post_headers_new(dateStr: str, private_key_pem: str, return signature_index_header, signature_header -def create_signed_header(dateStr: str, private_key_pem: str, nickname: str, +def create_signed_header(date_str: str, private_key_pem: str, nickname: str, domain: str, port: int, to_domain: str, to_port: int, - path: str, http_prefix: str, withDigest: bool, + path: str, http_prefix: str, with_digest: bool, message_body_json_str: str, content_type: str) -> {}: """Note that the domain is the destination, not the sender @@ -247,22 +247,22 @@ def create_signed_header(dateStr: str, private_key_pem: str, nickname: str, header_domain = get_full_domain(to_domain, to_port) # if no date is given then create one - if not dateStr: - dateStr = strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()) + if not date_str: + date_str = strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()) # Content-Type or Accept header if not content_type: content_type = 'application/activity+json' - if not withDigest: + if not with_digest: headers = { '(request-target)': f'get {path}', 'host': header_domain, - 'date': dateStr, + 'date': date_str, 'accept': content_type } signature_header = \ - sign_post_headers(dateStr, private_key_pem, nickname, + sign_post_headers(date_str, private_key_pem, nickname, domain, port, to_domain, to_port, path, http_prefix, None, content_type, algorithm, None) @@ -274,13 +274,13 @@ def create_signed_header(dateStr: str, private_key_pem: str, nickname: str, headers = { '(request-target)': f'post {path}', 'host': header_domain, - 'date': dateStr, + 'date': date_str, 'digest': f'{digest_prefix}={body_digest}', 'content-length': str(content_length), 'content-type': content_type } signature_header = \ - sign_post_headers(dateStr, private_key_pem, nickname, + sign_post_headers(date_str, private_key_pem, nickname, domain, port, to_domain, to_port, path, http_prefix, message_body_json_str, @@ -310,33 +310,33 @@ def _verify_recent_signature(signed_date_str: str) -> bool: def verify_post_headers(http_prefix: str, - publicKeyPem: str, headers: dict, - path: str, GETmethod: bool, - messageBodyDigest: str, + public_key_pem: str, headers: dict, + path: str, get_method: bool, + message_body_digest: str, message_body_json_str: str, debug: bool, - noRecencyCheck: bool = False) -> bool: + no_recency_check: bool = False) -> bool: """Returns true or false depending on if the key that we plugged in here validates against the headers, method, and path. - publicKeyPem - the public key from an rsa key pair + public_key_pem - the public key from an rsa key pair headers - should be a dictionary of request headers path - the relative url that was requested from this site - GETmethod - GET or POST + get_method - GET or POST message_body_json_str - the received request body (used for digest) """ - if GETmethod: + if get_method: method = 'GET' else: method = 'POST' if debug: print('DEBUG: verify_post_headers ' + method) - print('verify_post_headers publicKeyPem: ' + str(publicKeyPem)) + print('verify_post_headers public_key_pem: ' + str(public_key_pem)) print('verify_post_headers headers: ' + str(headers)) print('verify_post_headers message_body_json_str: ' + str(message_body_json_str)) - pubkey = load_pem_public_key(publicKeyPem.encode('utf-8'), + pubkey = load_pem_public_key(public_key_pem.encode('utf-8'), backend=default_backend()) # Build a dictionary of the signature values if headers.get('Signature-Input') or headers.get('signature-input'): @@ -422,8 +422,8 @@ def verify_post_headers(http_prefix: str, if debug: print('http signature algorithm: ' + algorithm) elif signed_header == 'digest': - if messageBodyDigest: - body_digest = messageBodyDigest + if message_body_digest: + body_digest = message_body_digest else: body_digest = \ message_content_digest(message_body_json_str, @@ -446,7 +446,7 @@ def verify_post_headers(http_prefix: str, ' not found in ' + str(headers)) else: if headers.get(signed_header): - if signed_header == 'date' and not noRecencyCheck: + if signed_header == 'date' and not no_recency_check: if not _verify_recent_signature(headers[signed_header]): if debug: print('DEBUG: ' + diff --git a/languages.py b/languages.py index 16f59742c..d586f96e9 100644 --- a/languages.py +++ b/languages.py @@ -153,7 +153,7 @@ def libretranslate_languages(url: str, api_key: str = None) -> []: else: url += "languages" - params = dict() + params = {} if api_key: params["api_key"] = api_key @@ -162,9 +162,9 @@ def libretranslate_languages(url: str, api_key: str = None) -> []: req = request.Request(url, data=url_params.encode()) - response = request.urlopen(req) - - response_str = response.read().decode() + response_str = '' + with request.urlopen(req) as response: + response_str = response.read().decode() result = json.loads(response_str) if not result: @@ -270,13 +270,16 @@ def libretranslate(url: str, text: str, url_params = parse.urlencode(lt_params) req = request.Request(url, data=url_params.encode()) + response_str = None try: - response = request.urlopen(req) - except BaseException: - print('EX: Unable to translate: ' + text) + with request.urlopen(req) as response: + response_str = response.read().decode() + except BaseException as ex: + print('EX: Unable to translate: ' + text + ' ' + str(ex)) return original_text - response_str = response.read().decode() + if not response_str: + return original_text translated_text = \ '

' + json.loads(response_str)['translatedText'] + '

' diff --git a/like.py b/like.py index 425d20c6c..51a2b00cb 100644 --- a/like.py +++ b/like.py @@ -74,7 +74,7 @@ def _create_like(recent_posts_cache: {}, cc_list: [], http_prefix: str, object_url: str, actor_liked: str, client_to_server: bool, - send_threads: [], postLog: [], + send_threads: [], post_log: [], person_cache: {}, cached_webfingers: {}, debug: bool, project_version: str, signing_priv_key_pem: str, @@ -141,7 +141,7 @@ def _create_like(recent_posts_cache: {}, liked_post_port, 'https://www.w3.org/ns/activitystreams#Public', http_prefix, True, client_to_server, federation_list, - send_threads, postLog, cached_webfingers, + send_threads, post_log, cached_webfingers, person_cache, debug, project_version, None, group_account, signing_priv_key_pem, 7367374, @@ -156,7 +156,7 @@ def like_post(recent_posts_cache: {}, like_nickname: str, like_domain: str, like_port: int, cc_list: [], like_status_number: int, client_to_server: bool, - send_threads: [], postLog: [], + send_threads: [], post_log: [], person_cache: {}, cached_webfingers: {}, debug: bool, project_version: str, signing_priv_key_pem: str, @@ -173,7 +173,8 @@ def like_post(recent_posts_cache: {}, nickname, domain, port, cc_list, http_prefix, object_url, actor_liked, client_to_server, - send_threads, postLog, person_cache, cached_webfingers, + send_threads, post_log, person_cache, + cached_webfingers, debug, project_version, signing_priv_key_pem, curr_domain, onion_domain, i2p_domain) diff --git a/linked_data_sig.py b/linked_data_sig.py index a2790fc81..3c61d24c3 100644 --- a/linked_data_sig.py +++ b/linked_data_sig.py @@ -87,8 +87,8 @@ def verify_json_signature(doc: {}, public_key_pem: str) -> bool: padding.PKCS1v15(), hazutils.Prehashed(hashes.SHA256())) return True - except BaseException: - print('EX: verify_json_signature unable to verify') + except BaseException as ex: + print('EX: verify_json_signature unable to verify ' + str(ex)) return False diff --git a/maps.py b/maps.py index c4ecac990..a7f224110 100644 --- a/maps.py +++ b/maps.py @@ -202,13 +202,13 @@ def geocoords_from_map_link(url: str, """ if osm_domain in url: return _geocoords_from_osm_link(url, osm_domain) - elif '.google.co' in url: + if '.google.co' in url: return _geocoords_from_gmaps_link(url) - elif '.bing.co' in url: + if '.bing.co' in url: return _geocoords_from_bmaps_link(url) - elif '.waze.co' in url: + if '.waze.co' in url: return _geocoords_from_waze_link(url) - elif 'wego.here.co' in url: + if 'wego.here.co' in url: return _geocoords_from_wego_link(url) return None, None, None diff --git a/metadata.py b/metadata.py index 40c9fb088..d3d488a12 100644 --- a/metadata.py +++ b/metadata.py @@ -35,7 +35,7 @@ def meta_data_node_info(base_dir: str, about_url: str, terms_of_service_url: str, registration: bool, version: str, - showAccounts: bool) -> {}: + show_accounts: bool) -> {}: """ /nodeinfo/2.0 endpoint Also see https://socialhub.activitypub.rocks/t/ fep-f1d5-nodeinfo-in-fediverse-software/1190/4 @@ -46,7 +46,7 @@ def meta_data_node_info(base_dir: str, Also exposure of the version number and number of accounts could be sensitive """ - if showAccounts: + if show_accounts: active_accounts = no_of_accounts(base_dir) active_accounts_monthly = no_of_active_accounts_monthly(base_dir, 1) active_accounts_half_year = no_of_active_accounts_monthly(base_dir, 6) @@ -81,7 +81,7 @@ def meta_data_node_info(base_dir: str, return nodeinfo -def meta_data_instance(showAccounts: bool, +def meta_data_instance(show_accounts: bool, instance_title: str, instance_description_short: str, instance_description: str, @@ -131,7 +131,7 @@ def meta_data_instance(showAccounts: bool, http_prefix + '://' + domain_full + '/@' + \ admin_actor['preferredUsername'] - if showAccounts: + if show_accounts: active_accounts = no_of_accounts(base_dir) local_posts = _get_status_count(base_dir) else: diff --git a/newsdaemon.py b/newsdaemon.py index 431230ccc..0faf24a00 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -54,7 +54,7 @@ def _update_feeds_outbox_index(base_dir: str, domain: str, feeds_file.seek(0, 0) feeds_file.write(post_id + '\n' + content) print('DEBUG: feeds post added to index') - except BaseException as ex: + except OSError as ex: print('EX: Failed to write entry to feeds posts index ' + index_filename + ' ' + str(ex)) else: @@ -803,8 +803,7 @@ def run_newswire_daemon(base_dir: str, httpd, print('Newswire daemon has no session') time.sleep(60) continue - else: - print('Newswire daemon session established') + print('Newswire daemon session established') # try to update the feeds print('Updating newswire feeds') diff --git a/newswire.py b/newswire.py index db20b0956..8acd22364 100644 --- a/newswire.py +++ b/newswire.py @@ -471,7 +471,7 @@ def _valid_podcast_entry(base_dir: str, key: str, entry: {}) -> bool: https://github.com/Podcastindex-org/podcast-namespace/ blob/main/proposal-docs/social/social.md#socialinteract-element """ - if key == 'socialInteract' or key == 'discussion': + if key in ('socialInteract', 'discussion'): if not entry.get('protocol'): return False if not entry.get('uri'): @@ -678,10 +678,9 @@ def get_link_from_rss_item(rss_item: str, '.i2p/' not in link: continue return link, mime_type - else: - if '.onion/' not in link and \ - '.i2p/' not in link: - return link, mime_type + if '.onion/' not in link and \ + '.i2p/' not in link: + return link, mime_type if ' None: property_found = property_value break if property_found: - actor_json['attachment'].remove(property_value) + actor_json['attachment'].remove(property_found) if remove_key: return @@ -283,7 +283,7 @@ def set_pgp_fingerprint(actor_json: {}, fingerprint: str) -> None: property_found = property_value break if property_found: - actor_json['attachment'].remove(property_value) + actor_json['attachment'].remove(property_found) if remove_fingerprint: return diff --git a/posts.py b/posts.py index c102b29b0..2a1ed356d 100644 --- a/posts.py +++ b/posts.py @@ -1451,7 +1451,7 @@ def _create_post_base(base_dir: str, break if music_metadata: for audio_tag, audio_value in music_metadata.items(): - if audio_tag == 'title' or audio_tag == 'track': + if audio_tag in ('title', 'track'): continue # capitalize and remove any spaces audio_value = audio_value.title().replace(' ', '') @@ -1984,7 +1984,7 @@ def create_question_post(base_dir: str, client_to_server: bool, comments_enabled: bool, attach_image_filename: str, media_type: str, image_description: str, city: str, - subject: str, durationDays: int, + subject: str, duration_days: int, system_language: str, low_bandwidth: bool, content_license_url: str, languages_understood: []) -> {}: @@ -2011,7 +2011,7 @@ def create_question_post(base_dir: str, message_json['object']['votersCount'] = 0 curr_time = datetime.datetime.utcnow() days_since_epoch = \ - int((curr_time - datetime.datetime(1970, 1, 1)).days + durationDays) + int((curr_time - datetime.datetime(1970, 1, 1)).days + duration_days) end_time = datetime.datetime(1970, 1, 1) + \ datetime.timedelta(days_since_epoch) message_json['object']['endTime'] = end_time.strftime("%Y-%m-%dT%H:%M:%SZ") @@ -2394,7 +2394,7 @@ def thread_send_post(session, post_json_str: str, federation_list: [], def send_post(signing_priv_key_pem: str, project_version: str, session, base_dir: str, nickname: str, domain: str, port: int, - to_nickname: str, to_domain: str, to_port: int, cc: str, + to_nickname: str, to_domain: str, to_port: int, cc_str: str, http_prefix: str, content: str, followers_only: bool, save_to_file: bool, client_to_server: bool, comments_enabled: bool, @@ -2462,7 +2462,7 @@ def send_post(signing_priv_key_pem: str, project_version: str, post_json_object = \ _create_post_base(base_dir, nickname, domain, port, - to_person_id, cc, http_prefix, content, + to_person_id, cc_str, http_prefix, content, followers_only, save_to_file, client_to_server, comments_enabled, attach_image_filename, media_type, @@ -2559,7 +2559,7 @@ def send_post_via_server(signing_priv_key_pem: str, project_version: str, from_nickname: str, password: str, from_domain: str, from_port: int, to_nickname: str, to_domain: str, to_port: int, - cc: str, + cc_str: str, http_prefix: str, content: str, followers_only: bool, comments_enabled: bool, attach_image_filename: str, media_type: str, @@ -2631,7 +2631,8 @@ def send_post_via_server(signing_priv_key_pem: str, project_version: str, client_to_server = True if to_domain.lower().endswith('public'): to_person_id = 'https://www.w3.org/ns/activitystreams#Public' - cc = local_actor_url(http_prefix, from_nickname, from_domain_full) + \ + cc_str = \ + local_actor_url(http_prefix, from_nickname, from_domain_full) + \ '/followers' else: if to_domain.lower().endswith('followers') or \ @@ -2648,7 +2649,7 @@ def send_post_via_server(signing_priv_key_pem: str, project_version: str, post_json_object = \ _create_post_base(base_dir, from_nickname, from_domain, from_port, - to_person_id, cc, http_prefix, content, + to_person_id, cc_str, http_prefix, content, followers_only, save_to_file, client_to_server, comments_enabled, attach_image_filename, media_type, @@ -2760,7 +2761,8 @@ def _add_followers_to_public_post(post_json_object: {}) -> None: def send_signed_json(post_json_object: {}, session, base_dir: str, nickname: str, domain: str, port: int, - to_nickname: str, to_domain: str, to_port: int, cc: str, + to_nickname: str, to_domain: str, + to_port: int, cc_str: str, http_prefix: str, save_to_file: bool, client_to_server: bool, federation_list: [], send_threads: [], post_log: [], cached_webfingers: {}, diff --git a/reaction.py b/reaction.py index d511fb231..a610e198b 100644 --- a/reaction.py +++ b/reaction.py @@ -182,8 +182,8 @@ def reaction_post(recent_posts_cache: {}, def send_reaction_via_server(base_dir: str, session, from_nickname: str, password: str, - from_domain: str, fromPort: int, - http_prefix: str, reactionUrl: str, + from_domain: str, from_port: int, + http_prefix: str, reaction_url: str, emoji_content: str, cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, @@ -198,7 +198,7 @@ def send_reaction_via_server(base_dir: str, session, emoji_content + '"') return 7 - from_domain_full = get_full_domain(from_domain, fromPort) + from_domain_full = get_full_domain(from_domain, from_port) actor = local_actor_url(http_prefix, from_nickname, from_domain_full) @@ -206,7 +206,7 @@ def send_reaction_via_server(base_dir: str, session, "@context": "https://www.w3.org/ns/activitystreams", 'type': 'EmojiReact', 'actor': actor, - 'object': reactionUrl, + 'object': reaction_url, 'content': emoji_content } @@ -272,8 +272,8 @@ def send_reaction_via_server(base_dir: str, session, def send_undo_reaction_via_server(base_dir: str, session, from_nickname: str, password: str, - from_domain: str, fromPort: int, - http_prefix: str, reactionUrl: str, + from_domain: str, from_port: int, + http_prefix: str, reaction_url: str, emoji_content: str, cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, @@ -284,7 +284,7 @@ def send_undo_reaction_via_server(base_dir: str, session, print('WARN: No session for send_undo_reaction_via_server') return 6 - from_domain_full = get_full_domain(from_domain, fromPort) + from_domain_full = get_full_domain(from_domain, from_port) actor = local_actor_url(http_prefix, from_nickname, from_domain_full) @@ -295,7 +295,7 @@ def send_undo_reaction_via_server(base_dir: str, session, 'object': { 'type': 'EmojiReact', 'actor': actor, - 'object': reactionUrl, + 'object': reaction_url, 'content': emoji_content } } @@ -457,7 +457,6 @@ def _update_common_reactions(base_dir: str, emoji_content: str) -> None: common_reactions = fp_react.readlines() except OSError: print('EX: unable to load common reactions file') - pass if common_reactions: new_common_reactions = [] reaction_found = False diff --git a/session.py b/session.py index 87e1b5415..35fed4ab6 100644 --- a/session.py +++ b/session.py @@ -49,7 +49,7 @@ def create_session(proxy_type: str): session.proxies = {} session.proxies['http'] = 'socks5h://localhost:7777' session.proxies['https'] = 'socks5h://localhost:7777' - elif proxy_type == 'ipfs' or proxy_type == 'ipns': + elif proxy_type in ('ipfs', 'ipns'): session.proxies = {} session.proxies['ipfs'] = 'socks5h://localhost:4001' # print('New session created with proxy ' + str(proxy_type)) @@ -79,8 +79,7 @@ def url_exists(session, url: str, timeout_sec: int = 3, timeout=timeout_sec, allow_redirects=False) if result: - if result.status_code == 200 or \ - result.status_code == 304: + if result.status_code in (200, 304): return True print('url_exists for ' + url + ' returned ' + str(result.status_code)) @@ -775,7 +774,7 @@ def get_method(method_name: str, xml_str: str, result = session.request(method_name, url, headers=session_headers, data=xml_str, params=session_params, timeout=timeout_sec) - if result.status_code != 200 and result.status_code != 207: + if result.status_code not in (200, 207): if result.status_code == 401: print("WARN: get_method " + url + ' rejected by secure mode') elif result.status_code == 403: diff --git a/shares.py b/shares.py index d25373ed6..202573838 100644 --- a/shares.py +++ b/shares.py @@ -543,8 +543,7 @@ def get_shares_feed_for_person(base_dir: str, curr_page += 1 shares['totalItems'] = total_ctr last_page = int(total_ctr / shares_per_page) - if last_page < 1: - last_page = 1 + last_page = max(last_page, 1) if next_page_number > last_page: shares['next'] = \ local_actor_url(http_prefix, nickname, domain) + \ @@ -554,14 +553,14 @@ def get_shares_feed_for_person(base_dir: str, def send_share_via_server(base_dir, session, from_nickname: str, password: str, - from_domain: str, fromPort: int, + from_domain: str, from_port: int, http_prefix: str, display_name: str, summary: str, image_filename: str, item_qty: float, item_type: str, item_category: str, location: str, duration: str, cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, - itemPrice: str, item_currency: str, + item_price: str, item_currency: str, signing_priv_key_pem: str) -> {}: """Creates an item share via c2s """ @@ -570,14 +569,14 @@ def send_share_via_server(base_dir, session, return 6 # convert $4.23 to 4.23 USD - new_item_price, new_item_currency = get_price_from_string(itemPrice) - if new_item_price != itemPrice: - itemPrice = new_item_price + new_item_price, new_item_currency = get_price_from_string(item_price) + if new_item_price != item_price: + item_price = new_item_price if not item_currency: if new_item_currency != item_currency: item_currency = new_item_currency - from_domain_full = get_full_domain(from_domain, fromPort) + from_domain_full = get_full_domain(from_domain, from_port) actor = local_actor_url(http_prefix, from_nickname, from_domain_full) to_url = 'https://www.w3.org/ns/activitystreams#Public' @@ -597,7 +596,7 @@ def send_share_via_server(base_dir, session, "category": item_category, "location": location, "duration": duration, - "itemPrice": itemPrice, + "itemPrice": item_price, "itemCurrency": item_currency, 'to': [to_url], 'cc': [cc_url] @@ -679,7 +678,7 @@ def send_share_via_server(base_dir, session, def send_undo_share_via_server(base_dir: str, session, from_nickname: str, password: str, - from_domain: str, fromPort: int, + from_domain: str, from_port: int, http_prefix: str, display_name: str, cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, @@ -690,7 +689,7 @@ def send_undo_share_via_server(base_dir: str, session, print('WARN: No session for send_undo_share_via_server') return 6 - from_domain_full = get_full_domain(from_domain, fromPort) + from_domain_full = get_full_domain(from_domain, from_port) actor = local_actor_url(http_prefix, from_nickname, from_domain_full) to_url = 'https://www.w3.org/ns/activitystreams#Public' @@ -774,14 +773,14 @@ def send_undo_share_via_server(base_dir: str, session, def send_wanted_via_server(base_dir, session, from_nickname: str, password: str, - from_domain: str, fromPort: int, + from_domain: str, from_port: int, http_prefix: str, display_name: str, summary: str, image_filename: str, item_qty: float, item_type: str, item_category: str, location: str, duration: str, cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, - itemMaxPrice: str, item_currency: str, + item_max_price: str, item_currency: str, signing_priv_key_pem: str) -> {}: """Creates a wanted item via c2s """ @@ -790,14 +789,15 @@ def send_wanted_via_server(base_dir, session, return 6 # convert $4.23 to 4.23 USD - new_item_max_price, new_item_currency = get_price_from_string(itemMaxPrice) - if new_item_max_price != itemMaxPrice: - itemMaxPrice = new_item_max_price + new_item_max_price, new_item_currency = \ + get_price_from_string(item_max_price) + if new_item_max_price != item_max_price: + item_max_price = new_item_max_price if not item_currency: if new_item_currency != item_currency: item_currency = new_item_currency - from_domain_full = get_full_domain(from_domain, fromPort) + from_domain_full = get_full_domain(from_domain, from_port) actor = local_actor_url(http_prefix, from_nickname, from_domain_full) to_url = 'https://www.w3.org/ns/activitystreams#Public' @@ -817,7 +817,7 @@ def send_wanted_via_server(base_dir, session, "category": item_category, "location": location, "duration": duration, - "itemPrice": itemMaxPrice, + "itemPrice": item_max_price, "itemCurrency": item_currency, 'to': [to_url], 'cc': [cc_url] @@ -899,7 +899,7 @@ def send_wanted_via_server(base_dir, session, def send_undo_wanted_via_server(base_dir: str, session, from_nickname: str, password: str, - from_domain: str, fromPort: int, + from_domain: str, from_port: int, http_prefix: str, display_name: str, cached_webfingers: {}, person_cache: {}, debug: bool, project_version: str, @@ -910,7 +910,7 @@ def send_undo_wanted_via_server(base_dir: str, session, print('WARN: No session for send_undo_wanted_via_server') return 6 - from_domain_full = get_full_domain(from_domain, fromPort) + from_domain_full = get_full_domain(from_domain, from_port) actor = local_actor_url(http_prefix, from_nickname, from_domain_full) to_url = 'https://www.w3.org/ns/activitystreams#Public' @@ -1488,7 +1488,7 @@ def create_shared_item_federation_token(base_dir: str, def authorize_shared_items(shared_items_federated_domains: [], base_dir: str, origin_domain_full: str, - calling_domainFull: str, + calling_domain_full: str, auth_header: str, debug: bool, tokens_json: {} = None) -> bool: @@ -1528,16 +1528,16 @@ def authorize_shared_items(shared_items_federated_domains: [], tokens_json = load_json(tokens_filename, 1, 2) if not tokens_json: return False - if not tokens_json.get(calling_domainFull): + if not tokens_json.get(calling_domain_full): if debug: print('DEBUG: shared item federation token ' + - 'check failed for ' + calling_domainFull) + 'check failed for ' + calling_domain_full) return False - if not constant_time_string_check(tokens_json[calling_domainFull], + if not constant_time_string_check(tokens_json[calling_domain_full], provided_token): if debug: print('DEBUG: shared item federation token ' + - 'mismatch for ' + calling_domainFull) + 'mismatch for ' + calling_domain_full) return False return True diff --git a/utils.py b/utils.py index 6fc1adf7d..e9e985d13 100644 --- a/utils.py +++ b/utils.py @@ -2050,7 +2050,7 @@ def _is_valid_language(text: str) -> bool: "Phonetic Extensions": [7467, 7544], "Combining Half Marks": [65070, 65071] } - for lang_name, lang_range in natural_languages.items(): + for _, lang_range in natural_languages.items(): ok_lang = True for char in text: if char.isdigit() or char == '_': @@ -2134,7 +2134,7 @@ def no_of_accounts(base_dir: str) -> bool: """Returns the number of accounts on the system """ account_ctr = 0 - for subdir, dirs, files in os.walk(base_dir + '/accounts'): + for _, dirs, _ in os.walk(base_dir + '/accounts'): for account in dirs: if is_account_dir(account): account_ctr += 1 @@ -2148,7 +2148,7 @@ def no_of_active_accounts_monthly(base_dir: str, months: int) -> bool: account_ctr = 0 curr_time = int(time.time()) month_seconds = int(60*60*24*30*months) - for subdir, dirs, files in os.walk(base_dir + '/accounts'): + for _, dirs, _ in os.walk(base_dir + '/accounts'): for account in dirs: if not is_account_dir(account): continue @@ -2427,7 +2427,7 @@ def search_box_posts(base_dir: str, nickname: str, domain: str, search_words = [search_str] res = [] - for root, dirs, fnames in os.walk(path): + for root, _, fnames in os.walk(path): for fname in fnames: file_path = os.path.join(root, fname) with open(file_path, 'r') as post_file: @@ -3485,7 +3485,7 @@ def has_object_string_object(post_json_object: {}, debug: bool) -> bool: if post_json_object['object'].get('object'): if isinstance(post_json_object['object']['object'], str): return True - elif debug: + if debug: if post_json_object.get('type'): print('DEBUG: ' + post_json_object['type'] + ' object within dict is not a string') @@ -3566,14 +3566,14 @@ def load_account_timezones(base_dir: str) -> {}: """Returns a dictionary containing the preferred timezone for each account """ account_timezone = {} - for subdir, dirs, files in os.walk(base_dir + '/accounts'): + for _, dirs, _ in os.walk(base_dir + '/accounts'): for acct in dirs: if '@' not in acct: continue if acct.startswith('inbox@') or acct.startswith('Actor@'): continue - acct_dir = os.path.join(base_dir + '/accounts', acct) - tz_filename = acct_dir + '/timezone.txt' + acct_directory = os.path.join(base_dir + '/accounts', acct) + tz_filename = acct_directory + '/timezone.txt' if not os.path.isfile(tz_filename): continue timezone = None @@ -3590,7 +3590,7 @@ def load_bold_reading(base_dir: str) -> {}: """Returns a dictionary containing the bold reading status for each account """ bold_reading = {} - for subdir, dirs, files in os.walk(base_dir + '/accounts'): + for _, dirs, _ in os.walk(base_dir + '/accounts'): for acct in dirs: if '@' not in acct: continue @@ -3601,6 +3601,7 @@ def load_bold_reading(base_dir: str) -> {}: if os.path.isfile(bold_reading_filename): nickname = acct.split('@')[0] bold_reading[nickname] = True + break return bold_reading diff --git a/webapp_column_left.py b/webapp_column_left.py index dc88eec99..97df269e7 100644 --- a/webapp_column_left.py +++ b/webapp_column_left.py @@ -117,7 +117,7 @@ def _get_left_column_wanted(base_dir: str, def get_left_column_content(base_dir: str, nickname: str, domain_full: str, http_prefix: str, translate: {}, editor: bool, artist: bool, - show_back_button: bool, timelinePath: str, + show_back_button: bool, timeline_path: str, rss_icon_at_top: bool, show_header_image: bool, front_page: bool, theme: str, access_keys: {}, @@ -146,7 +146,7 @@ def get_left_column_content(base_dir: str, nickname: str, domain_full: str, if show_back_button: html_str += \ - '
' + \ + '
' + \ '\n' @@ -359,7 +359,7 @@ def get_left_column_content(base_dir: str, nickname: str, domain_full: str, def html_links_mobile(css_cache: {}, base_dir: str, nickname: str, domain_full: str, http_prefix: str, translate, - timelinePath: str, authorized: bool, + timeline_path: str, authorized: bool, rss_icon_at_top: bool, icons_as_buttons: bool, default_timeline: str, @@ -406,7 +406,7 @@ def html_links_mobile(css_cache: {}, base_dir: str, get_left_column_content(base_dir, nickname, domain_full, http_prefix, translate, editor, artist, - False, timelinePath, + False, timeline_path, rss_icon_at_top, False, False, theme, access_keys, shared_items_federated_domains) diff --git a/webapp_headerbuttons.py b/webapp_headerbuttons.py index 5b38b1bcc..14670319f 100644 --- a/webapp_headerbuttons.py +++ b/webapp_headerbuttons.py @@ -22,7 +22,7 @@ def header_buttons_timeline(default_timeline: str, page_number: int, translate: {}, users_path: str, - mediaButton: str, + media_button: str, blogs_button: str, features_button: str, news_button: str, @@ -62,7 +62,7 @@ def header_buttons_timeline(default_timeline: str, tl_str += ' aria-current="location"' tl_str += \ '>' elif default_timeline == 'tlblogs': tl_str += \ @@ -134,7 +134,7 @@ def header_buttons_timeline(default_timeline: str, tl_str += ' aria-current="location"' tl_str += \ '>' else: if not minimal: diff --git a/webapp_likers.py b/webapp_likers.py index 54d51507a..15d024b88 100644 --- a/webapp_likers.py +++ b/webapp_likers.py @@ -40,7 +40,7 @@ def html_likers_of_post(base_dir: str, nickname: str, system_language: str, max_like_count: int, signing_priv_key_pem: str, cw_lists: {}, lists_enabled: str, - boxName: str, default_timeline: str, + box_name: str, default_timeline: str, bold_reading: bool, dict_name: str = 'likes') -> str: """Returns html for a screen showing who liked a post @@ -96,7 +96,7 @@ def html_likers_of_post(base_dir: str, nickname: str, None, True, False, http_prefix, project_version, - boxName, + box_name, yt_replace_domain, twitter_replacement_domain, show_published_date_only, diff --git a/webapp_podcast.py b/webapp_podcast.py index 732e252c9..b570c6b2c 100644 --- a/webapp_podcast.py +++ b/webapp_podcast.py @@ -118,7 +118,7 @@ def _html_podcast_transcripts(podcast_properties: {}, translate: {}) -> str: return '' ctr = 1 html_str = '' - for transcript in podcast_properties[key]: + for _ in podcast_properties[key]: transcript_url = None if podcast_properties[key].get('url'): transcript_url = podcast_properties[key]['url'] diff --git a/webapp_post.py b/webapp_post.py index 92dd236b0..29838ad40 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -214,14 +214,14 @@ def _html_post_metadata_open_graph(domain: str, post_json_object: {}, def _log_post_timing(enable_timing_log: bool, post_start_time, - debugId: str) -> None: + debug_id: str) -> None: """Create a log of timings for performance tuning """ if not enable_timing_log: return time_diff = int((time.time() - post_start_time) * 1000) if time_diff > 100: - print('TIMING INDIV ' + debugId + ' = ' + str(time_diff)) + print('TIMING INDIV ' + debug_id + ' = ' + str(time_diff)) def prepare_html_post_nickname(nickname: str, post_html: str) -> str: @@ -374,7 +374,7 @@ def _get_post_from_recent_cache(session, return post_html -def _get_avatar_image_html(showAvatarOptions: bool, +def _get_avatar_image_html(show_avatar_options: bool, nickname: str, domain_full: str, avatar_url: str, post_actor: str, translate: {}, avatar_position: str, @@ -399,7 +399,7 @@ def _get_avatar_image_html(showAvatarOptions: bool, show_profile_str + '" alt=" "' + avatar_position + \ get_broken_link_substitute() + '/>\n' - if showAvatarOptions and \ + if show_avatar_options and \ domain_full + '/users/' + nickname not in post_actor: show_options_for_this_person_str = 'Show options for this person' if translate.get(show_options_for_this_person_str): @@ -528,7 +528,7 @@ def _get_reply_icon_html(base_dir: str, nickname: str, domain: str, def _get_edit_icon_html(base_dir: str, nickname: str, domain_full: str, post_json_object: {}, actor_nickname: str, - translate: {}, isEvent: bool) -> str: + translate: {}, is_event: bool) -> str: """Returns html for the edit icon/button """ edit_str = '' @@ -573,7 +573,7 @@ def _get_edit_icon_html(base_dir: str, nickname: str, domain_full: str, '' + edit_blog_post_str + \
                     ' |\n' - elif isEvent: + elif is_event: edit_event_str = 'Edit event' if translate.get(edit_event_str): edit_event_str = translate[edit_event_str] @@ -1488,7 +1488,7 @@ def individual_post_as_html(signing_priv_key_pem: str, session, cached_webfingers: {}, person_cache: {}, nickname: str, domain: str, port: int, post_json_object: {}, - avatar_url: str, showAvatarOptions: bool, + avatar_url: str, show_avatar_options: bool, allow_deletion: bool, http_prefix: str, project_version: str, box_name: str, @@ -1642,7 +1642,7 @@ def individual_post_as_html(signing_priv_key_pem: str, _log_post_timing(enable_timing_log, post_start_time, '7') avatar_link = \ - _get_avatar_image_html(showAvatarOptions, + _get_avatar_image_html(show_avatar_options, nickname, domain_full, avatar_url, post_actor, translate, avatar_position, diff --git a/webapp_profile.py b/webapp_profile.py index f49198761..bfa7daca4 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -613,7 +613,7 @@ def html_profile(signing_priv_key_pem: str, debug: bool, access_keys: {}, city: str, system_language: str, max_like_count: int, shared_items_federated_domains: [], - extraJson: {}, page_number: int, + extra_json: {}, page_number: int, max_items_per_page: int, cw_lists: {}, lists_enabled: str, content_license_url: str, @@ -636,7 +636,7 @@ def html_profile(signing_priv_key_pem: str, yt_replace_domain, twitter_replacement_domain, show_published_date_only, - newswire, theme, extraJson, + newswire, theme, extra_json, allow_local_network_access, access_keys, system_language, max_like_count, shared_items_federated_domains, None, @@ -1042,7 +1042,7 @@ def html_profile(signing_priv_key_pem: str, authorized, nickname, domain, port, session, cached_webfingers, - person_cache, extraJson, + person_cache, extra_json, project_version, ["unfollow"], selected, users_path, page_number, @@ -1055,7 +1055,7 @@ def html_profile(signing_priv_key_pem: str, authorized, nickname, domain, port, session, cached_webfingers, - person_cache, extraJson, + person_cache, extra_json, project_version, ["block"], selected, users_path, page_number, max_items_per_page, dormant_months, debug, @@ -1064,21 +1064,21 @@ def html_profile(signing_priv_key_pem: str, if selected == 'roles': profile_str += \ _html_profile_roles(translate, nickname, domain_full, - extraJson) + extra_json) elif selected == 'skills': profile_str += \ _html_profile_skills(translate, nickname, domain_full, - extraJson) + extra_json) # elif selected == 'shares': # profile_str += \ # _html_profile_shares(actor, translate, # nickname, domain_full, -# extraJson, 'shares') + license_str +# extra_json, 'shares') + license_str # elif selected == 'wanted': # profile_str += \ # _html_profile_shares(actor, translate, # nickname, domain_full, -# extraJson, 'wanted') + license_str +# extra_json, 'wanted') + license_str # end of #timeline profile_str += '
' @@ -1174,7 +1174,7 @@ def _html_profile_following(translate: {}, base_dir: str, http_prefix: str, session, cached_webfingers: {}, person_cache: {}, following_json: {}, project_version: str, buttons: [], - feedName: str, actor: str, + feed_name: str, actor: str, page_number: int, max_items_per_page: int, dormant_months: int, debug: bool, @@ -1188,7 +1188,7 @@ def _html_profile_following(translate: {}, base_dir: str, http_prefix: str, # page up arrow profile_str += \ '
\n' + \ - ' \n
\n' - for role in rolesList: + for role in roles_list: if translate.get(role): profile_str += '

' + translate[role] + '

\n' else: @@ -2462,7 +2462,7 @@ def _individual_follow_as_html(signing_priv_key_pem: str, base_dir: str, session, cached_webfingers: {}, person_cache: {}, domain: str, - followUrl: str, + follow_url: str, authorized: bool, actor_nickname: str, http_prefix: str, @@ -2472,21 +2472,22 @@ def _individual_follow_as_html(signing_priv_key_pem: str, buttons=[]) -> str: """An individual follow entry on the profile screen """ - follow_url_nickname = get_nickname_from_actor(followUrl) + follow_url_nickname = get_nickname_from_actor(follow_url) if not follow_url_nickname: return '' - follow_url_domain, follow_url_port = get_domain_from_actor(followUrl) + follow_url_domain, follow_url_port = get_domain_from_actor(follow_url) follow_url_domain_full = \ get_full_domain(follow_url_domain, follow_url_port) title_str = '@' + follow_url_nickname + '@' + follow_url_domain_full - avatar_url = get_person_avatar_url(base_dir, followUrl, person_cache, True) + avatar_url = \ + get_person_avatar_url(base_dir, follow_url, person_cache, True) if not avatar_url: - avatar_url = followUrl + '/avatar.png' + avatar_url = follow_url + '/avatar.png' - display_name = get_display_name(base_dir, followUrl, person_cache) + display_name = get_display_name(base_dir, follow_url, person_cache) is_group = False if not display_name: - # lookup the correct webfinger for the followUrl + # lookup the correct webfinger for the follow_url follow_url_handle = follow_url_nickname + '@' + follow_url_domain_full follow_url_wf = \ webfinger_handle(session, follow_url_handle, http_prefix, @@ -2524,7 +2525,7 @@ def _individual_follow_as_html(signing_priv_key_pem: str, if btn == 'block': buttons_str += \ '
\n' @@ -2536,7 +2537,7 @@ def _individual_follow_as_html(signing_priv_key_pem: str, unfollow_str = 'Leave' buttons_str += \ '\n' @@ -2544,7 +2545,7 @@ def _individual_follow_as_html(signing_priv_key_pem: str, result_str = '
\n' result_str += \ '\n' + follow_url + ';1;' + avatar_url + '">\n' result_str += '

 ' result_str += title_str + '' + buttons_str + '

\n' diff --git a/webapp_question.py b/webapp_question.py index fe2881d4f..b6b3cfcaa 100644 --- a/webapp_question.py +++ b/webapp_question.py @@ -16,37 +16,37 @@ from utils import acct_dir def insert_question(base_dir: str, translate: {}, nickname: str, domain: str, port: int, content: str, - post_json_object: {}, pageNumber: int) -> str: + post_json_object: {}, page_number: int) -> str: """ Inserts question selection into a post """ if not is_question(post_json_object): return content if len(post_json_object['object']['oneOf']) == 0: return content - messageId = remove_id_ending(post_json_object['id']) - if '#' in messageId: - messageId = messageId.split('#', 1)[0] - pageNumberStr = '' - if pageNumber: - pageNumberStr = '?page=' + str(pageNumber) + message_id = remove_id_ending(post_json_object['id']) + if '#' in message_id: + message_id = message_id.split('#', 1)[0] + page_number_str = '' + if page_number: + page_number_str = '?page=' + str(page_number) - votesFilename = \ + votes_filename = \ acct_dir(base_dir, nickname, domain) + '/questions.txt' - showQuestionResults = False - if os.path.isfile(votesFilename): - if messageId in open(votesFilename).read(): - showQuestionResults = True + show_question_results = False + if os.path.isfile(votes_filename): + if message_id in open(votes_filename).read(): + show_question_results = True - if not showQuestionResults: + if not show_question_results: # show the question options content += '
' content += \ '
\n' + nickname + '/question' + page_number_str + '">\n' content += \ '\n
\n' + message_id + '">\n
\n' for choice in post_json_object['object']['oneOf']: if not choice.get('type'): continue @@ -64,45 +64,43 @@ def insert_question(base_dir: str, translate: {}, content += '
\n' # get the maximum number of votes - maxVotes = 1 - for questionOption in post_json_object['object']['oneOf']: - if not questionOption.get('name'): + max_votes = 1 + for question_option in post_json_object['object']['oneOf']: + if not question_option.get('name'): continue - if not questionOption.get('replies'): + if not question_option.get('replies'): continue votes = 0 try: - votes = int(questionOption['replies']['totalItems']) + votes = int(question_option['replies']['totalItems']) except BaseException: print('EX: insert_question unable to convert to int') - if votes > maxVotes: - maxVotes = int(votes+1) + if votes > max_votes: + max_votes = int(votes+1) # show the votes as sliders - questionCtr = 1 - for questionOption in post_json_object['object']['oneOf']: - if not questionOption.get('name'): + for question_option in post_json_object['object']['oneOf']: + if not question_option.get('name'): continue - if not questionOption.get('replies'): + if not question_option.get('replies'): continue votes = 0 try: - votes = int(questionOption['replies']['totalItems']) + votes = int(question_option['replies']['totalItems']) except BaseException: print('EX: insert_question unable to convert to int 2') - votesPercent = str(int(votes * 100 / maxVotes)) + votes_percent = str(int(votes * 100 / max_votes)) content += \ '

\n' + \ '
\n' + \ + question_option['name'] + '
\n' + \ ' \n' + \ - ' \n' + \ ' ' + \ - ' \n' + \ + ' \n' + \ '

\n' - questionCtr += 1 content += '
\n' return content diff --git a/webapp_search.py b/webapp_search.py index d7888044f..dfea4f615 100644 --- a/webapp_search.py +++ b/webapp_search.py @@ -490,30 +490,31 @@ def html_skills_search(actor: str, continue actor_filename = os.path.join(subdir, fname) actor_json = load_json(actor_filename) - if actor_json: - if actor_json.get('id') and \ - no_of_actor_skills(actor_json) > 0 and \ - actor_json.get('name') and \ - actor_json.get('icon'): - actor = actor_json['id'] - actor_skills_list = actor_json['hasOccupation']['skills'] - skills = get_skills_from_list(actor_skills_list) - for skill_name, skill_level in skills.items(): - skill_name = skill_name.lower() - if not (skill_name in skillsearch or - skillsearch in skill_name): - continue - skill_level_str = str(skill_level) - if skill_level < 100: - skill_level_str = '0' + skill_level_str - if skill_level < 10: - skill_level_str = '0' + skill_level_str - index_str = \ - skill_level_str + ';' + actor + ';' + \ - actor_json['name'] + \ - ';' + actor_json['icon']['url'] - if index_str not in results: - results.append(index_str) + if not actor_json: + continue + if actor_json.get('id') and \ + no_of_actor_skills(actor_json) > 0 and \ + actor_json.get('name') and \ + actor_json.get('icon'): + actor = actor_json['id'] + actor_skills_list = actor_json['hasOccupation']['skills'] + skills = get_skills_from_list(actor_skills_list) + for skill_name, skill_level in skills.items(): + skill_name = skill_name.lower() + if not (skill_name in skillsearch or + skillsearch in skill_name): + continue + skill_level_str = str(skill_level) + if skill_level < 100: + skill_level_str = '0' + skill_level_str + if skill_level < 10: + skill_level_str = '0' + skill_level_str + index_str = \ + skill_level_str + ';' + actor + ';' + \ + actor_json['name'] + \ + ';' + actor_json['icon']['url'] + if index_str not in results: + results.append(index_str) break if not instance_only: # search actor cache @@ -525,33 +526,34 @@ def html_skills_search(actor: str, continue actor_filename = os.path.join(subdir, fname) cached_actor_json = load_json(actor_filename) - if cached_actor_json: - if cached_actor_json.get('actor'): - actor_json = cached_actor_json['actor'] - if actor_json.get('id') and \ - no_of_actor_skills(actor_json) > 0 and \ - actor_json.get('name') and \ - actor_json.get('icon'): - actor = actor_json['id'] - actor_skills_list = \ - actor_json['hasOccupation']['skills'] - skills = get_skills_from_list(actor_skills_list) - for skill_name, skill_level in skills.items(): - skill_name = skill_name.lower() - if not (skill_name in skillsearch or - skillsearch in skill_name): - continue - skill_level_str = str(skill_level) - if skill_level < 100: - skill_level_str = '0' + skill_level_str - if skill_level < 10: - skill_level_str = '0' + skill_level_str - index_str = \ - skill_level_str + ';' + actor + ';' + \ - actor_json['name'] + \ - ';' + actor_json['icon']['url'] - if index_str not in results: - results.append(index_str) + if not cached_actor_json: + continue + if cached_actor_json.get('actor'): + actor_json = cached_actor_json['actor'] + if actor_json.get('id') and \ + no_of_actor_skills(actor_json) > 0 and \ + actor_json.get('name') and \ + actor_json.get('icon'): + actor = actor_json['id'] + actor_skills_list = \ + actor_json['hasOccupation']['skills'] + skills = get_skills_from_list(actor_skills_list) + for skill_name, skill_level in skills.items(): + skill_name = skill_name.lower() + if not (skill_name in skillsearch or + skillsearch in skill_name): + continue + skill_level_str = str(skill_level) + if skill_level < 100: + skill_level_str = '0' + skill_level_str + if skill_level < 10: + skill_level_str = '0' + skill_level_str + index_str = \ + skill_level_str + ';' + actor + ';' + \ + actor_json['name'] + \ + ';' + actor_json['icon']['url'] + if index_str not in results: + results.append(index_str) break results.sort(reverse=True) @@ -616,7 +618,7 @@ def html_history_search(css_cache: {}, translate: {}, base_dir: str, show_published_date_only: bool, peertube_instances: [], allow_local_network_access: bool, - theme_name: str, boxName: str, + theme_name: str, box_name: str, system_language: str, max_like_count: int, signing_priv_key_pem: str, @@ -632,7 +634,7 @@ def html_history_search(css_cache: {}, translate: {}, base_dir: str, box_filenames = \ search_box_posts(base_dir, nickname, domain, - historysearch, posts_per_page, boxName) + historysearch, posts_per_page, box_name) css_filename = base_dir + '/epicyon-profile.css' if os.path.isfile(base_dir + '/epicyon.css'): @@ -647,7 +649,7 @@ def html_history_search(css_cache: {}, translate: {}, base_dir: str, domain_full = get_full_domain(domain, port) actor = local_actor_url(http_prefix, nickname, domain_full) history_search_title = '🔍 ' + translate['Your Posts'] - if boxName == 'bookmarks': + if box_name == 'bookmarks': history_search_title = '🔍 ' + translate['Bookmarks'] history_search_form += \ diff --git a/webapp_theme_designer.py b/webapp_theme_designer.py index f3e8d5e3f..8ba236699 100644 --- a/webapp_theme_designer.py +++ b/webapp_theme_designer.py @@ -255,8 +255,7 @@ def html_theme_designer(css_cache: {}, base_dir: str, variable_name_str = variable_name_str.title() variable_name_label = variable_name_str if contrast_warning: - if variable_name == 'main-bg-color' or \ - variable_name == 'main-fg-color': + if variable_name in ('main-bg-color', 'main-fg-color'): variable_name_label = contrast_warning + variable_name_str font_str += \ '