summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar √Čtienne Deparis 2020-03-14 00:10:31 +0100
committerGravatar √Čtienne Deparis 2020-03-14 13:24:14 +0100
commitd0e446e37d65d2b36454f60008bd8dc461897c7a (patch)
treecb54e5e14b1ceee091d20ba40ffc6a250d7f55c0
parentf55faf2beccf336e5e0a10647c880950febbbff1 (diff)
downloadpimtools-d0e446e37d65d2b36454f60008bd8dc461897c7a.tar.gz
pimtools-d0e446e37d65d2b36454f60008bd8dc461897c7a.tar.xz
pimtools-d0e446e37d65d2b36454f60008bd8dc461897c7a.zip
Remove weird output when run as scriptsHEADmaster
-rw-r--r--pimtools/buddies.py60
-rw-r--r--pimtools/todos.py41
-rw-r--r--setup.cfg4
3 files changed, 60 insertions, 45 deletions
diff --git a/pimtools/buddies.py b/pimtools/buddies.py
index 5131efa..44eeb1d 100644
--- a/pimtools/buddies.py
+++ b/pimtools/buddies.py
@@ -44,58 +44,64 @@ class Buddies:
parser.add_argument("query_or_file", nargs="+",
help="Query or file to parse.")
- args = parser.parse_args()
+ self.args = parser.parse_args()
- if args.version:
+ def run(self):
+ if self.args.version:
print("Buddies - v{}".format(__version__))
- sys.exit(0)
+ return 0
self.modifiers = {
"format": "{name} <{email}>",
- "prop_type": args.property_type,
- "show_uid": args.uid
+ "prop_type": self.args.property_type,
+ "show_uid": self.args.uid
}
- if args.filepath:
+ if self.args.filepath:
vcards = [
VCardParser(os.path.expanduser(f), self.modifiers)
- for f in args.query_or_file
+ for f in self.args.query_or_file
]
else:
- vcards = self.search(args.query_or_file)
+ vcards = self.search(self.args.query_or_file)
- if args.edit:
+ if self.args.edit:
# vCards will be edited one after the other
operation = self.edit
- elif args.clip == "email":
+ elif self.args.clip == "email":
operation = self.clip_email
- elif args.clip == "tel":
+ elif self.args.clip == "tel":
operation = self.clip_tel
- elif args.show == "vcard":
+ elif self.args.show == "vcard":
operation = self.show
- elif args.show == "photo":
+ elif self.args.show == "photo":
operation = self.open_photo
- elif args.show == "mutt":
+ elif self.args.show == "mutt":
# Mutt imply email, thus it's checked before email
self.modifiers["format"] = "{email}\t{name}"
print() # Mutt needs a first empty line
operation = self.print_email
- elif args.show == "mu":
+ elif self.args.show == "mu":
# Mu imply email, thus it's checked before email too
magenta_name = "\x1b[35m{}\x1b[0m".format("{name}")
green_email = "\x1b[32m{}\x1b[0m".format("{email}")
self.modifiers["format"] = magenta_name + " " + green_email
operation = self.print_email
- elif args.show == "email":
+ elif self.args.show == "email":
operation = self.print_email
- elif args.show == "org-contact":
+ elif self.args.show == "org-contact":
operation = self.convert_org
- elif args.show == "pine":
+ elif self.args.show == "pine":
operation = self.convert_pine
else:
operation = print
for card in vcards:
- operation(card)
+ try:
+ operation(card)
+ except RuntimeError as error:
+ print(error)
+ return 1
+ return 0
def search(self, query):
addressbooks = ADDRESSBOOKS
@@ -131,9 +137,7 @@ class Buddies:
)
if p.returncode == 0:
return
- print("Error while editing {}".format(card.filepath),
- file=sys.stderr)
- sys.exit(p.returncode)
+ raise RuntimeError("Error while editing {}".format(card.filepath))
def show(self, card):
with open(card.filepath, "r") as f:
@@ -194,10 +198,14 @@ class Buddies:
p = subprocess.run(["gio", "open", tmp_pic_path])
if p.returncode == 0:
return
- print("Error while opening photo from {}".format(card.filepath),
- file=sys.stderr)
- sys.exit(p.returncode)
+ raise RuntimeError(
+ "Error while opening photo from {}".format(card.filepath)
+ )
+
+
+def run_buddies():
+ return Buddies().run()
if __name__ == "__main__":
- Buddies()
+ sys.exit(run_buddies())
diff --git a/pimtools/todos.py b/pimtools/todos.py
index f81f5bb..9734854 100644
--- a/pimtools/todos.py
+++ b/pimtools/todos.py
@@ -34,34 +34,39 @@ class Todos:
"The default value is `fancy'.")
parser.add_argument("query_or_file", nargs="+",
help="Query or file to parse.")
+ self.args = parser.parse_args()
- args = parser.parse_args()
-
- if args.version:
+ def run(self):
+ if self.args.version:
print("Todos - v{}".format(__version__))
- sys.exit(0)
+ return 0
self.modifiers = {
- "show_uid": args.uid,
- "show_all": args.all
+ "show_uid": self.args.uid,
+ "show_all": self.args.all
}
- if args.filepath:
- vtodos = self.filter_vtodo_files(args.query_or_file)
+ if self.args.filepath:
+ vtodos = self.filter_vtodo_files(self.args.query_or_file)
else:
- vtodos = self.search(args.query_or_file)
+ vtodos = self.search(self.args.query_or_file)
- if args.edit:
+ if self.args.edit:
# vtodos will be edited one after the other
operation = self.edit
- elif args.show == "vtodo":
+ elif self.args.show == "vtodo":
operation = self.show
- elif args.show == "org":
+ elif self.args.show == "org":
operation = self.convert_org
else:
operation = print
for card in vtodos:
- operation(card)
+ try:
+ operation(card)
+ except RuntimeError as error:
+ print(error, file=sys.stderr)
+ return 1
+ return 0
def filter_vtodo_files(self, ics_files):
vtodos = []
@@ -105,9 +110,7 @@ class Todos:
)
if p.returncode == 0:
return
- print("Error while editing {}".format(card.filepath),
- file=sys.stderr)
- sys.exit(p.returncode)
+ raise RuntimeError("Error while editing {}".format(card.filepath))
def show(self, card):
with open(card.filepath, "r") as f:
@@ -118,5 +121,9 @@ class Todos:
print()
+def run_todos():
+ return Todos().run()
+
+
if __name__ == "__main__":
- Todos()
+ sys.exit(run_todos())
diff --git a/setup.cfg b/setup.cfg
index 16b8e0f..3a463f2 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -21,5 +21,5 @@ packages = find:
[options.entry_points]
console_scripts =
- buddies = pimtools.buddies:Buddies
- todos = pimtools.todos:Todos
+ buddies = pimtools.buddies:run_buddies
+ todos = pimtools.todos:run_todos