--- a/pvl/args.py Fri Sep 20 14:45:27 2013 +0300
+++ b/pvl/args.py Fri Sep 20 14:45:50 2013 +0300
@@ -2,10 +2,13 @@
CLI argument handling; common stuff: logging
"""
-import optparse
+import codecs
+import grp
import logging
-
-import pwd, grp, os, sys
+import optparse
+import os
+import pwd
+import sys
import logging; log = logging.getLogger('pvl.args')
@@ -91,6 +94,35 @@
log.error("refusing to run as root, use --uid 0 to override")
sys.exit(2)
+def apply_file (path=None, mode='r', charset='utf-8') :
+ """
+ Open unicode-enabled file from path, with - using stdio.
+ """
+
+ if not path :
+ # use stdin/out based on mode
+ stream, func = {
+ 'r': (sys.stdin, codecs.getreader),
+ 'w': (sys.stdout, codecs.getwriter),
+ }[mode[0]]
+
+ # wrap
+ return func(charset)(stream)
+
+ else :
+ # open
+ return codecs.open(path, mode, charset)
+
+def apply_files (paths, *args, **opts) :
+ """
+ Open one or more files from given paths, defaulting to stdio.
+ """
+
+ if paths :
+ return [apply_file(path, *args, **opts) for path in paths]
+ else:
+ return [apply_file(None, *args, **opts)]
+
def apply (options, logname=None, rootok=True) :
"""
Apply the optparse options.
@@ -121,3 +153,9 @@
for logger in options.debug_module :
logging.getLogger(logger).setLevel(logging.DEBUG)
+def main (main) :
+ """
+ Run given main func.
+ """
+
+ sys.exit(main(sys.argv))