From cee043b6d2e229ae3171e698cb65265e0d2dfc9d Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 4 Apr 2026 14:02:06 +0200 Subject: [PATCH] refactor the project up to modern standards --- pyproject.toml | 23 ++++++++++ forthlift.py => src/forthlift/forthlift.py | 52 ++++++++++++---------- 2 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 pyproject.toml rename forthlift.py => src/forthlift/forthlift.py (87%) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5e90228 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,23 @@ +[project] +name = "forthlift" +version = "0.1.0" +description = "A command line application to post sequences of text lines on social media" +readme = "README.md" +requires-python = ">=3.12" +dependencies = [ + "argparse>=1.4.0", + "configparser>=7.2.0", + "datetime>=6.0", +] + +[project.scripts] +forthlift = "forthlift.forthlift:main" + +[build-system] +requires = ["setuptools>=68", "wheel"] +build-backend = "setuptools.build_meta" + +[tool.setuptools.packages.find] +where = ["src"] +include = ["forthlift*"] + diff --git a/forthlift.py b/src/forthlift/forthlift.py similarity index 87% rename from forthlift.py rename to src/forthlift/forthlift.py index b48ecaf..f66a736 100755 --- a/forthlift.py +++ b/src/forthlift/forthlift.py @@ -2,11 +2,12 @@ import sys import locale +import logging import argparse import datetime -import ConfigParser +from configparser import ConfigParser -import tweepy +#import tweepy class AppKeyError(Exception): @@ -160,24 +161,24 @@ def setup_twitter(configfile="twitter.conf"): app_key = config.get("App","app_key") app_secret = config.get("App","app_key_secret") - auth = tweepy.OAuthHandler(app_key, app_secret) + # auth = tweepy.OAuthHandler(app_key, app_secret) # Authenticate the user. - auth_url = auth.get_authorization_url() - print "Copy and paste this URL in your browser while your are logged in the twitter account where you want to post." - print "Authorization URL: " + auth_url + # auth_url = auth.get_authorization_url() + # print("Copy and paste this URL in your browser while your are logged in the twitter account where you want to post.") + # print("Authorization URL: " + auth_url) - print "Then paste the Personal Identification Number given by Twitter:" - verifier = raw_input('PIN: ').strip() - token = auth.get_access_token(verifier) + # print("Then paste the Personal Identification Number given by Twitter:") + # verifier = raw_input('PIN: ').strip() + # token = auth.get_access_token(verifier) # Authenticate and get the user name. - token_key, token_secret = token - auth.set_access_token(token_key, token_secret) - api = tweepy.API(auth) - username = api.me().name - print "Authentication successful, ready to post to account: " + username + # token_key, token_secret = token + # auth.set_access_token(token_key, token_secret) + # api = tweepy.API(auth) + # username = api.me().name + # print("Authentication successful, ready to post to account: " + username) # Save authentication tokens. @@ -200,8 +201,7 @@ def setup_twitter(configfile="twitter.conf"): # CLI # -if __name__=="__main__": - +def main(): errors = {"NO_ERROR":0, "UNKNOWN_ERROR":1, "NO_SETUP_NEEDED":2, "NO_APP_KEY":10} usage = "Post text read on the standard input to a website or an API." @@ -261,7 +261,7 @@ if __name__=="__main__": sys.stderr.write(e.msg) sys.exit(errors["NO_APP_KEY"]) except: - print "Unexpected error:", sys.exc_info()[0] + logging.error(f"Unexpected error:\n{sys.exc_info()[0]}") raise else: sys.exit(errors["NO_ERROR"]) @@ -294,9 +294,9 @@ if __name__=="__main__": except OSError: cannot.append(img) if cannot: - print("Cannot open the following image files, I will not continue: ") + logger.error("Cannot open the following image files, I will not continue: ") for img in cannot: - print(img) + logger.error(img) sys.exit(5) # I/O Error # APIs @@ -321,11 +321,15 @@ if __name__=="__main__": access_token = config.get("Auth","local_token") access_token_secret = config.get("Auth","local_token_secret") - auth = tweepy.OAuthHandler(app_key, app_secret, "https://api.twitter.com/1.1/") - auth.set_access_token(access_token, access_token_secret) + # auth = tweepy.OAuthHandler(app_key, app_secret, "https://api.twitter.com/1.1/") + # auth.set_access_token(access_token, access_token_secret) - api = tweepy.API(auth) + # api = tweepy.API(auth) - # Post - operate( on_twitter, api, asked ) + # # Post + # operate( on_twitter, api, asked ) + + +if __name__ == "__main__": + main()