1. spirit
  2. guess_language

Commits

spirit  committed d4f9152

Use locale encoding when piping stdin.

  • Participants
  • Parent commits 367de04
  • Branches default

Comments (0)

Files changed (1)

File guess_language/__main__.py

View file
 """
 
 import argparse
+import locale
 import os
 import sys
 
 import guess_language
 
-DEFAULT_ENCODING = "utf-8"
-
 
 def parse_args():
     parser = argparse.ArgumentParser(
         prog="{} -m {}".format(os.path.basename(sys.executable),
                                "guess_language")
     )
-    parser.add_argument("file", nargs="?", help="plain text file")
-    parser.add_argument("--encoding", dest="encoding", help="input encoding")
+    parser.add_argument("file",
+                        help='plain text file or "-" for stdin')
+    parser.add_argument("--encoding",
+                        help="input encoding")
     parser.add_argument("--disable-enchant", dest="use_enchant",
-                        action="store_false", help="disable enchant")
+                        action="store_false",
+                        help="disable enchant")
     return parser.parse_args()
 
 
 def main():
     args = parse_args()
-    if args.file:
+
+    if args.file == "-":
+        file = sys.stdin.fileno()
+        encoding = args.encoding or (
+            sys.stdin.encoding if sys.stdin.isatty()
+            else locale.getpreferredencoding()
+        )
+    else:
         file = args.file
-        encoding = args.encoding if args.encoding else DEFAULT_ENCODING
-    else:
-        file = sys.stdin.fileno()
-        encoding = args.encoding if args.encoding else sys.stdin.encoding
+        encoding = args.encoding or "utf-8"
+
     with open(file, encoding=encoding) as f:
         text = "\n".join(f.readlines())
+
     guess_language.USE_ENCHANT = args.use_enchant
     tag = guess_language.guess_language(text)
     print(tag)
+
     return 0 if tag else 1