Here are the examples of the python api sys.stdin.encoding taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
84 Examples
5
Example 1
Project: win-unicode-console Source File: readline_hook.py
def check_encodings():
if sys.stdin.encoding != sys.stdout.encoding:
# raise RuntimeError("sys.stdin.encoding != sys.stdout.encoding, readline hook doesn't know, which one to use to decode prompt")
warnings.warn("sys.stdin.encoding == {!r}, whereas sys.stdout.encoding == {!r}, readline hook consumer may assume they are the same".format(sys.stdin.encoding, sys.stdout.encoding),
RuntimeWarning, stacklevel=3)
4
Example 2
def _encode_utf8(self, msg):
"""The MusicBrainz server also accepts UTF-8 encoded passwords."""
encoding = sys.stdin.encoding or locale.getpreferredencoding()
try:
# This works on Python 2 (msg in bytes)
msg = msg.decode(encoding)
except AttributeError:
# on Python 3 (msg is already in unicode)
pass
return msg.encode("utf-8")
4
Example 3
def encode(value, encoding='utf-8', errors='backslashreplace'):
"""
Unicode string -> encoded string (default to UTF-8)
"""
if type(value) is unicode:
res = value.encode(encoding, errors)
elif type(value) is str:
decoding = get_console_encoding(sys.stdin.encoding)
if encoding == decoding:
res = value
else:
res = transcode(value, decoding, encoding)
else:
res = str(value)
return res
3
Example 4
def get_new_password():
"""
Gets new password from user and verify it.
"""
while True:
password = getpass.getpass('New password: ').decode(
sys.stdin.encoding)
password2 = getpass.getpass('Verify password: ').decode(
sys.stdin.encoding)
if password == password2:
return password
print(' ** Passwords do not match! **')
3
Example 5
Project: win-unicode-console Source File: raw_input.py
def stdin_decode(b):
if isinstance(b, unicode):
return b
encoding = sys.stdin.encoding
errors = sys.stdin.errors
if errors is not None:
return b.decode(encoding, errors)
else:
return b.decode(encoding)
3
Example 6
Project: win-unicode-console Source File: raw_input.py
def stdin_encode(s):
if isinstance(s, bytes):
return s
encoding = sys.stdin.encoding
errors = sys.stdin.errors
if errors is not None:
return s.encode(encoding, errors)
else:
return s.encode(encoding)
3
Example 7
def __init__(self, hook):
threading.Thread.__init__(self)
self.daemon = True
self.encoding = sys.stdin.encoding
self.key_hook = lambda x: True
self.mouse_button_hook = lambda x: True
self.mouse_move_hook = lambda x: True
self.screen_hook = lambda x: True
self.remap = {
248: u"\xf8",
216: u"\xd8",
230: u"\xe6",
198: u"\xc6",
229: u"\xe5",
197: u"\xc5"
}
self.hm = hook
3
Example 8
def __init__(self):
self.encoding = sys.stdin.encoding
self.key_hook = lambda x: True
self.mouse_button_hook = lambda x: True
self.mouse_move_hook = lambda x: True
self.screen_hook = lambda x: True
self.remap = {
248: u"\xf8",
216: u"\xd8",
230: u"\xe6",
198: u"\xc6",
229: u"\xe5",
197: u"\xc5"
}
3
Example 9
def emit(self, record):
if sys.stdin.encoding != "utf-8":
record = record
stream = self.stream
try:
msg = self.formatter.format(record, stream.isatty())
stream.write(msg.encode(stream.encoding, errors="replace").decode(stream.encoding))
stream.write(self.terminator)
self.flush()
except Exception:
self.handleError(record)
3
Example 10
Project: LocalNote Source File: oauth2.py
@retry(3)
def _login(self, preloadContent):
data = {
'username': raw_input('Username: ').decode(sys.stdin.encoding),
'password': getpass.getpass(),
'login': '登陆',
'showSwitchService': 'true',
'targetUrl': "/api/DeveloperToken.action",
'_sourcePage': "MbulSL0GgBDiMUD9T65RG_YvRLZ-1eYO3fqfqRu0fynRL_1nukNa4gH1t86pc1SP",
'__fp': "ZtDCgDFj-IY3yWPvuidLz-TPR6I9Jhx8",
}
for eid in ('hpts', 'hptsh'):
data[eid] = re.compile('getElementById\("%s"\).value = "(.*?)";'%eid).search(preloadContent).groups()[0]
return self.s.post(self.host + '/Login.action', data).content
3
Example 11
def check_output(*args, **kwargs):
"""subprocess.check_output wrapper to not leak file descriptors
"""
kwargs = __update_kwargs(kwargs)
LOGGER.debug("Checking output with: %s %s" % (args, kwargs))
return unicode(subprocess.check_output(*args, **kwargs),
encoding=sys.stdin.encoding)
3
Example 12
Project: loso Source File: scripts.py
def run(self):
logging.basicConfig(level=logging.DEBUG)
cfg = _loadConfig()
seg_service = service.SegumentService(cfg)
while True:
text = raw_input('Text:').decode(sys.stdin.encoding)
terms = seg_service.splitTerms(text, self.category)
print ' '.join(terms)
3
Example 13
def decode(msg):
"""This will replace unsuitable characters and use stdin encoding
"""
if isinstance(msg, bytes):
return msg.decode(sys.stdin.encoding, "replace")
else:
return unicode_string(msg)
3
Example 14
def _unicode(string, encoding=None):
"""Try to decode byte strings to unicode.
This can only be a guess, but this might be better than failing.
It is safe to use this on numbers or strings that are already unicode.
"""
if isinstance(string, compat.unicode):
unicode_string = string
elif isinstance(string, compat.bytes):
# use given encoding, stdin, preferred until something != None is found
if encoding is None:
encoding = sys.stdin.encoding
if encoding is None:
encoding = locale.getpreferredencoding()
unicode_string = string.decode(encoding, "ignore")
else:
unicode_string = compat.unicode(string)
return unicode_string.replace('\x00', '').strip()
3
Example 15
def enable(use_pyreadline=True):
check_encodings()
if use_pyreadline and pyreadline:
pyreadline_manager.set_codepage(sys.stdin.encoding)
# pyreadline assumes that encoding of all sys.stdio objects is the same
if not pyreadline_is_active():
manager.install_hook(stdio_readline)
else:
manager.install_hook(stdio_readline)
3
Example 16
def raw_input(prompt):
"""Input one line from the console and converts it to unicode as
appropriate.
"""
printout(prompt, newline=False)
return to_unicode(__builtin__.raw_input(), sys.stdin.encoding)
3
Example 17
def __init__(self, *args, **kwargs):
try:
self.encoding = sys.stdin.encoding
except:
#Not sure if it's available in all Python versions...
pass
try:
self.errors = sys.stdin.errors # Who knew? sys streams have an errors attribute!
except:
#Not sure if it's available in all Python versions...
pass
3
Example 18
Project: madcow Source File: __init__.py
def run(self):
"""Runs madcow loop"""
while self.running:
self.check_response_queue()
line = decode(raw_input('>>> '), sys.stdin.encoding).rstrip()
req = Request(message=line)
req.nick = os.environ['USER']
req.channel = u'none'
req.addressed = True
req.private = True
self.check_addressing(req)
self.process_message(req)
3
Example 19
def dump_json(self, entry):
encodings = encodings = ["ascii", "utf-8", "latin1", "utf-16le", sys.stdin.encoding]
to_write = {}
for k, v in entry.items():
if v:
encoding, value = find_encoding(v)
if encoding:
try:
to_write[k] = value.decode(encoding).encode('utf-8', 'ignore')
except UnicodeEncodeError:
to_write[k] = binascii.b2a_hex(v).encode('UTF-8','ignore')
else:
to_write[k] = ''.join([a for a in v]).encode('utf-8', 'ignore')
self.output.write(json.dumps(to_write))
pass
3
Example 20
def __init__(self, *args, **kwargs):
try:
self.encoding = sys.stdin.encoding
except:
#Not sure if it's available in all Python versions...
pass
3
Example 21
def read_line():
from sys import stdin
if version_info >= (3,):
return input()
return raw_input().decode(stdin.encoding or 'utf-8')
3
Example 22
Project: shellpy Source File: core.py
def __init__(self, process, params):
self._process = process
self._params = params
self.stdin = Stream(process.stdin, sys.stdin.encoding)
print_stdout = _is_param_set(params, _PARAM_PRINT_STDOUT) or config.PRINT_STDOUT_ALWAYS
self.stdout = Stream(process.stdout, sys.stdout.encoding, print_stdout)
print_stderr = _is_param_set(params, _PARAM_PRINT_STDERR) or config.PRINT_STDERR_ALWAYS
color = None if not _is_colorama_enabled() else Fore.RED
self.stderr = Stream(process.stderr, sys.stderr.encoding, print_stderr, color)
3
Example 23
def ask_password(profile, no_interactive):
"""
Prompt for profile password
"""
utf8 = "UTF-8"
input_encoding = utf8 if sys.stdin.encoding in (None, 'ascii') else sys.stdin.encoding
passmsg = "\nMaster Password for profile {}: ".format(profile)
if sys.stdin.isatty() and not no_interactive:
passwd = getpass(passmsg)
else:
# Ability to read the password from stdin (echo "pass" | ./firefox_...)
passwd = sys.stdin.readline().rstrip("\n")
if PY3:
return passwd
else:
return passwd.decode(input_encoding)
3
Example 24
Project: caterpillar Source File: caterpillar.py
def unicode_arg(arg):
"""Converts a bytestring command-line argument into a Unicode string."""
if sys.stdin.encoding:
return arg.decode(sys.stdin.encoding)
return arg.decode(sys.getfilesystemencoding())
3
Example 25
def find_encoding(value):
encodings = ["ascii", "utf-8", "latin1", "utf-16le", sys.stdin.encoding]
for encoding in encodings:
try:
if type(value) != "str" and type(value) != "unicode":
value = str(value).decode(encoding)
elif type(s) == "str":
value = value.decode(encoding)
return encoding, value
except UnicodeEncodeError:
pass
except UnicodeDecodeError:
pass
return None, None
3
Example 26
def prompt(prompt_):
"""Wrapper around the raw_input builtin function
Will return unicode in Python 2 like in Python 3
"""
result = raw_input(prompt_)
try:
return result.decode(sys.stdin.encoding)
except AttributeError:
return result
2
Example 27
def check_output(*popenargs, **kwargs):
"""
Adapted version of the builtin subprocess.check_output which sets a
"stderr" field on the resulting exception (in addition to "output")
if the subprocess fails. (If the command succeeds, the contents of
stderr are discarded.)
:param also_return_stderr: if True, return stderr along with the output of the command as such (output, stderr)
:type also_return_stderr: bool
Unlike subprocess.check_output, unconditionally decodes the contents of the subprocess stdout and stderr using
sys.stdin.encoding.
"""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
if 'stderr' in kwargs:
raise ValueError('stderr argument not allowed, it will be overridden.')
return_stderr = False
if 'also_return_stderr' in kwargs:
if kwargs['also_return_stderr']:
return_stderr = True
del kwargs['also_return_stderr']
# Unplug stdin (if not already overridden) so that dx doesn't prompt
# user for input at the tty
process = subprocess.Popen(stdin=kwargs.get('stdin', subprocess.PIPE),
stdout=subprocess.PIPE, stderr=subprocess.PIPE, *popenargs, **kwargs)
output, err = process.communicate()
retcode = process.poll()
if not isinstance(output, str):
output = output.decode(sys.stdin.encoding)
if not isinstance(err, str):
err = err.decode(sys.stdin.encoding)
if retcode:
print(err)
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
exc = DXCalledProcessError(retcode, cmd, output=output, stderr=err)
raise exc
if return_stderr:
return (output, err)
else:
return output
2
Example 28
Project: aeneas Source File: globalfunctions.py
def safe_unicode_stdin(string):
"""
Safely convert the given string to a Unicode string,
decoding using ``sys.stdin.encoding`` if needed.
If running from a frozen binary, ``utf-8`` encoding is assumed.
:param variant string: the byte string or Unicode string to convert
:rtype: string
"""
if string is None:
return None
if is_bytes(string):
if FROZEN:
return string.decode("utf-8")
try:
return string.decode(sys.stdin.encoding)
except UnicodeDecodeError:
return string.decode(sys.stdin.encoding, "replace")
except:
return string.decode("utf-8")
return string
2
Example 29
Project: nikola Source File: new_post.py
def _execute(self, options, args):
"""Create a new post or page."""
global LOGGER
compiler_names = [p.name for p in
self.site.plugin_manager.getPluginsOfCategory(
"PageCompiler")]
if len(args) > 1:
print(self.help())
return False
elif args:
path = args[0]
else:
path = None
# Even though stuff was split into `new_page`, it’s easier to do it
# here not to duplicate the code.
is_page = options.get('is_page', False)
is_post = not is_page
content_type = 'page' if is_page else 'post'
title = options['title'] or None
author = options['author'] or ''
tags = options['tags']
onefile = options['onefile']
twofile = options['twofile']
import_file = options['import']
wants_available = options['available-formats']
if wants_available:
self.print_compilers()
return
if is_page:
LOGGER = PAGELOGGER
else:
LOGGER = POSTLOGGER
if twofile:
onefile = False
if not onefile and not twofile:
onefile = self.site.config.get('ONE_FILE_POSTS', True)
content_format = options['content_format']
content_subformat = None
if "@" in content_format:
content_format, content_subformat = content_format.split("@")
if not content_format: # Issue #400
content_format = get_default_compiler(
is_post,
self.site.config['COMPILERS'],
self.site.config['post_pages'])
if content_format not in compiler_names:
LOGGER.error("Unknown {0} format {1}, maybe you need to install a plugin or enable an existing one?".format(content_type, content_format))
self.print_compilers()
return
compiler_plugin = self.site.plugin_manager.getPluginByName(
content_format, "PageCompiler").plugin_object
# Guess where we should put this
entry = self.filter_post_pages(content_format, is_post)
if entry is False:
return 1
if import_file:
print("Importing Existing {xx}".format(xx=content_type.title()))
print("-----------------------\n")
else:
print("Creating New {xx}".format(xx=content_type.title()))
print("-----------------\n")
if title is not None:
print("Title:", title)
else:
while not title:
title = utils.ask('Title')
if isinstance(title, utils.bytes_str):
try:
title = title.decode(sys.stdin.encoding)
except (AttributeError, TypeError): # for tests
title = title.decode('utf-8')
title = title.strip()
if not path:
slug = utils.slugify(title, lang=self.site.default_lang)
else:
if isinstance(path, utils.bytes_str):
try:
path = path.decode(sys.stdin.encoding)
except (AttributeError, TypeError): # for tests
path = path.decode('utf-8')
slug = utils.slugify(os.path.splitext(os.path.basename(path))[0], lang=self.site.default_lang)
if isinstance(author, utils.bytes_str):
try:
author = author.decode(sys.stdin.encoding)
except (AttributeError, TypeError): # for tests
author = author.decode('utf-8')
# Calculate the date to use for the content
schedule = options['schedule'] or self.site.config['SCHEDULE_ALL']
rule = self.site.config['SCHEDULE_RULE']
self.site.scan_posts()
timeline = self.site.timeline
last_date = None if not timeline else timeline[0].date
date = get_date(schedule, rule, last_date, self.site.tzinfo, self.site.config['FORCE_ISO8601'])
data = {
'title': title,
'slug': slug,
'date': date,
'tags': tags,
'link': '',
'description': '',
'type': 'text',
}
if not path:
pattern = os.path.basename(entry[0])
suffix = pattern[1:]
output_path = os.path.dirname(entry[0])
txt_path = os.path.join(output_path, slug + suffix)
meta_path = os.path.join(output_path, slug + ".meta")
else:
txt_path = os.path.join(self.site.original_cwd, path)
meta_path = os.path.splitext(txt_path)[0] + ".meta"
if (not onefile and os.path.isfile(meta_path)) or \
os.path.isfile(txt_path):
# Emit an event when a post exists
event = dict(path=txt_path)
if not onefile: # write metadata file
event['meta_path'] = meta_path
signal('existing_' + content_type).send(self, **event)
LOGGER.error("The title already exists!")
LOGGER.info("Existing {0}'s text is at: {1}".format(content_type, txt_path))
if not onefile:
LOGGER.info("Existing {0}'s metadata is at: {1}".format(content_type, meta_path))
return 8
d_name = os.path.dirname(txt_path)
utils.makedirs(d_name)
metadata = {}
if author:
metadata['author'] = author
metadata.update(self.site.config['ADDITIONAL_METADATA'])
data.update(metadata)
# ipynb plugin needs the ipython kernel info. We get the kernel name
# from the content_subformat and pass it to the compiler in the metadata
if content_format == "ipynb" and content_subformat is not None:
metadata["ipython_kernel"] = content_subformat
# Override onefile if not really supported.
if not compiler_plugin.supports_onefile and onefile:
onefile = False
LOGGER.warn('This compiler does not support one-file posts.')
if onefile and import_file:
with io.open(import_file, 'r', encoding='utf-8') as fh:
content = fh.read()
elif not import_file:
if is_page:
content = self.site.MESSAGES[self.site.default_lang]["Write your page here."]
else:
content = self.site.MESSAGES[self.site.default_lang]["Write your post here."]
if (not onefile) and import_file:
# Two-file posts are copied on import (Issue #2380)
shutil.copy(import_file, txt_path)
else:
compiler_plugin.create_post(
txt_path, content=content, onefile=onefile, title=title,
slug=slug, date=date, tags=tags, is_page=is_page, **metadata)
event = dict(path=txt_path)
if not onefile: # write metadata file
with io.open(meta_path, "w+", encoding="utf8") as fd:
fd.write(utils.write_metadata(data))
LOGGER.info("Your {0}'s metadata is at: {1}".format(content_type, meta_path))
event['meta_path'] = meta_path
LOGGER.info("Your {0}'s text is at: {1}".format(content_type, txt_path))
signal('new_' + content_type).send(self, **event)
if options['edit']:
editor = os.getenv('EDITOR', '').split()
to_run = editor + [txt_path]
if not onefile:
to_run.append(meta_path)
if editor:
subprocess.call(to_run)
else:
LOGGER.error('$EDITOR not set, cannot edit the post. Please do it manually.')
2
Example 30
Project: polymaze Source File: cli.py
def commandline():
parser = _parser()
kwargs = vars(parser.parse_args())
# setup the base grid with a supershape if provided
grid = PolyGrid(supershape=ss_dict.get(kwargs.pop('shape'), None))
# pull off non-common parameters
text = kwargs.pop('text')
if text is not None:
text = text.decode(sys.stdin.encoding)
image_path = kwargs.pop('image')
if image_path is not None:
image_path = image_path.decode(sys.stdin.encoding)
font_path = kwargs.pop('font')
if font_path is not None:
font_path = font_path.decode(sys.stdin.encoding)
filename = kwargs.pop('output')
if filename is not None:
filename = filename.decode(sys.stdin.encoding)
# fill the grid and create maze based on the remaining arguments provided
if text:
grid.create_string(text, font_path=font_path, **kwargs)
maze_type = 'Text'
elif image_path:
image = PIL.Image.open(image_path).convert('L')
if not image:
print('Unable to open the provided image path: {}'
''.format(image_path))
grid.create_from_image(image, **kwargs)
maze_type = 'Image'
else:
grid.create_rectangle(**kwargs)
maze_type = 'Rectangle'
maze = Maze(grid)
save_maze(maze, maze_type, filename)
2
Example 31
def _percent_encode(self, request, encoding=None):
encoding = encoding or sys.stdin.encoding or DEFAULT_ENCODING
try:
s = unicode(request, encoding)
except TypeError:
if not isinstance(request, unicode):
# We accept int etc. types as well
s = unicode(request)
else:
s = request
res = urllib.quote(
s.encode('utf8'),
safe='~')
return res
0
Example 32
Project: aeneas Source File: diagnostics.py
@classmethod
def check_shell_encoding(cls):
"""
Check whether ``sys.stdin`` and ``sys.stdout`` are UTF-8 encoded.
Return ``True`` on failure and ``False`` on success.
:rtype: bool
"""
is_in_utf8 = True
is_out_utf8 = True
if sys.stdin.encoding not in ["UTF-8", "UTF8"]:
is_in_utf8 = False
if sys.stdout.encoding not in ["UTF-8", "UTF8"]:
is_out_utf8 = False
if (is_in_utf8) and (is_out_utf8):
gf.print_success(u"shell encoding OK")
else:
gf.print_warning(u"shell encoding WARNING")
if not is_in_utf8:
gf.print_warning(u" The default input encoding of your shell is not UTF-8")
if not is_out_utf8:
gf.print_warning(u" The default output encoding of your shell is not UTF-8")
gf.print_info(u" If you plan to use aeneas on the command line,")
if gf.is_posix():
gf.print_info(u" you might want to 'export PYTHONIOENCODING=UTF-8' in your shell")
else:
gf.print_info(u" you might want to 'set PYTHONIOENCODING=UTF-8' in your shell")
return True
return False
0
Example 33
Project: the-listserve-archive Source File: bootstrap.py
def get_post_from_user():
ok = False
sentinel = '<stop>'
post_kwargs = {
'subject': None,
'author': None,
'body': None,
'date': None,
}
print "enter %r on a line by itself to end input" % sentinel
while not ok:
for param in post_kwargs.keys():
print
print "%s:" % param
entered = '\n'.join(iter(raw_input, sentinel))
post_kwargs[param] = entered.decode(sys.stdin.encoding)
date_list = [int(i) for i in post_kwargs['date'].split('-')]
post_kwargs['date'] = date_list
print
pprint.pprint(post_kwargs)
ok = raw_input('confirm (y/n) :') == 'y'
return Post(**post_kwargs)
0
Example 34
Project: django Source File: questioner.py
def _ask_default(self, default=''):
"""
Prompt for a default value.
The ``default`` argument allows providing a custom default value (as a
string) which will be shown to the user and used as the return value
if the user doesn't provide any other input.
"""
print("Please enter the default value now, as valid Python")
if default:
print(
"You can accept the default '{}' by pressing 'Enter' or you "
"can provide another value.".format(default)
)
print("The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now")
print("Type 'exit' to exit this prompt")
while True:
if default:
prompt = "[default: {}] >>> ".format(default)
else:
prompt = ">>> "
if six.PY3:
# Six does not correctly abstract over the fact that
# py3 input returns a unicode string, while py2 raw_input
# returns a bytestring.
code = input(prompt)
else:
code = input(prompt).decode(sys.stdin.encoding)
if not code and default:
code = default
if not code:
print("Please enter some code, or 'exit' (with no quotes) to exit.")
elif code == "exit":
sys.exit(1)
else:
try:
return eval(code, {}, {"datetime": datetime_safe, "timezone": timezone})
except (SyntaxError, NameError) as e:
print("Invalid input: %s" % e)
0
Example 35
Project: googlecl Source File: __init__.py
def determine_terminal_encoding(config=None):
import sys
in_enc = ''
out_enc = ''
if sys.stdin.encoding:
in_enc = sys.stdin.encoding
if sys.stdout.encoding:
out_enc = sys.stdout.encoding
# Sometimes these are both defined, and hopefully they are both equal.
# I'm not sure if they are guaranteed to be equal.
if in_enc.lower() == out_enc.lower():
# If they're not defined, return the encoding specific in the config file,
# or the python system-wide default encoding (if the above is not defined)
# Apparently the above values aren't set when run as a cron job or
# piped?
if not in_enc:
return_enc = None
if config is not None:
return_enc = config.safe_get('GENERAL', 'default_encoding')
if return_enc is None:
return_enc = sys.getdefaultencoding()
else:
return_enc = in_enc
# If they are not equal, at least one of them must be defined.
else:
# Both defined, but are not the same
if in_enc and out_enc:
LOG.warning(
'HEY! You have a different encoding for input and output')
LOG.warning('Input: ' + in_enc)
LOG.warning('Output: ' + in_enc)
return_enc = out_enc or in_enc
LOG.debug('determine_terminal_encoding(): ' + return_enc)
return return_enc
0
Example 36
Project: python-u2flib-host Source File: authenticate.py
def main():
args = parse_args()
facet = text_type(args.facet, sys.stdin.encoding or sys.getdefaultencoding())
if args.infile:
with open(args.infile, 'r') as f:
data = f.read()
else:
if sys.stdin.isatty():
sys.stderr.write('Enter AuthenticateRequest JSON data...\n')
data = sys.stdin.read()
params = json.loads(data, object_hook=u2str)
if args.soft:
from u2flib_host.soft import SoftU2FDevice
devices = [SoftU2FDevice(args.soft)]
else:
devices = u2f.list_devices()
result = authenticate(devices, params, facet, args.check_only)
if args.outfile:
with open(args.outfile, 'w') as f:
json.dump(result, f)
sys.stderr.write('Output written to %s\n' % args.outfile)
else:
sys.stderr.write('\n---Result---\n')
print(json.dumps(result))
0
Example 37
Project: python-u2flib-host Source File: register.py
def main():
args = parse_args()
facet = text_type(args.facet, sys.stdin.encoding or sys.getdefaultencoding())
if args.infile:
with open(args.infile, 'r') as f:
data = f.read()
else:
if sys.stdin.isatty():
sys.stderr.write('Enter RegistrationRequest JSON data...\n')
data = sys.stdin.read()
params = json.loads(data, object_hook=u2str)
if args.soft:
from u2flib_host.soft import SoftU2FDevice
devices = [SoftU2FDevice(args.soft)]
else:
devices = u2f.list_devices()
result = register(devices, params, facet)
if args.outfile:
with open(args.outfile, 'w') as f:
json.dump(result, f)
sys.stderr.write('Output written to %s\n' % args.outfile)
else:
sys.stderr.write('\n---Result---\n')
print(json.dumps(result))
0
Example 38
def safe_encode(text, incoming=None,
encoding='utf-8', errors='strict'):
"""Encodes incoming text/bytes string using `encoding`.
If incoming is not specified, text is expected to be encoded with
current python's default encoding. (`sys.getdefaultencoding`)
:param incoming: Text's current encoding
:param encoding: Expected encoding for text (Default UTF-8)
:param errors: Errors handling policy. See here for valid
values http://docs.python.org/2/library/codecs.html
:returns: text or a bytestring `encoding` encoded
representation of it.
:raises TypeError: If text is not an instance of str
"""
if not isinstance(text, (six.string_types, six.binary_type)):
raise TypeError("%s can't be encoded" % type(text))
if not incoming:
incoming = (sys.stdin.encoding or
sys.getdefaultencoding())
if isinstance(text, six.text_type):
return text.encode(encoding, errors)
elif text and encoding != incoming:
# Decode text before encoding it with `encoding`
text = safe_decode(text, incoming, errors)
return text.encode(encoding, errors)
else:
return text
0
Example 39
def __init__(self, path, config):
self.path = path
self.config = config
self.stdin_encoding = get_console_encoding(sys.stdin.encoding)
0
Example 40
def input_(prompt=None):
"""Like `raw_input`, but decodes the result to a Unicode string.
Raises a UserError if stdin is not available. The prompt is sent to
stdout rather than stderr. A printed between the prompt and the
input cursor.
"""
# raw_input incorrectly sends prompts to stderr, not stdout, so we
# use print() explicitly to display prompts.
# http://bugs.python.org/issue1927
if prompt:
if isinstance(prompt, unicode):
prompt = prompt.encode(_encoding(), 'replace')
print(prompt, end=' ')
try:
resp = raw_input()
except EOFError:
raise UserError('stdin stream ended while input required')
return resp.decode(sys.stdin.encoding or 'utf8', 'ignore')
0
Example 41
Project: jiffy Source File: gnupg.py
def __init__(self, gpgbinary='gpg', gnupghome=None, verbose=False,
use_agent=False, keyring=None, options=None,
secret_keyring=None):
"""Initialize a GPG process wrapper. Options are:
gpgbinary -- full pathname for GPG binary.
gnupghome -- full pathname to where we can find the public and
private keyrings. Default is whatever gpg defaults to.
keyring -- name of alternative keyring file to use, or list of such
keyrings. If specified, the default keyring is not used.
options =-- a list of additional options to pass to the GPG binary.
secret_keyring -- name of alternative secret keyring file to use, or
list of such keyrings.
"""
self.gpgbinary = gpgbinary
self.gnupghome = gnupghome
if keyring:
# Allow passing a string or another iterable. Make it uniformly
# a list of keyring filenames
if isinstance(keyring, string_types):
keyring = [keyring]
self.keyring = keyring
if secret_keyring:
# Allow passing a string or another iterable. Make it uniformly
# a list of keyring filenames
if isinstance(secret_keyring, string_types):
secret_keyring = [secret_keyring]
self.secret_keyring = secret_keyring
self.verbose = verbose
self.use_agent = use_agent
if isinstance(options, str):
options = [options]
self.options = options
self.encoding = locale.getpreferredencoding()
if self.encoding is None: # This happens on Jython!
self.encoding = sys.stdin.encoding
if self.encoding is None:
logger.warning('No encoding found via locale.getpreferredencoding '
'or sys.stdin.encoding, defaulting to utf-8.')
self.encoding = 'utf-8'
if gnupghome and not os.path.isdir(self.gnupghome):
os.makedirs(self.gnupghome,0x1C0)
p = self._open_subprocess(["--version"])
result = self.result_map['verify'](self) # any result will do for this
self._collect_output(p, result, stdin=p.stdin)
if p.returncode != 0:
raise ValueError("Error invoking gpg: %s: %s" % (p.returncode,
result.stderr))
m = VERSION_RE.match(result.data)
if not m:
self.version = None
else:
dot = '.'.encode('utf-8')
self.version = tuple([int(s) for s in m.groups()[0].split(dot)])
0
Example 42
Project: cctrl Source File: oshelpers.py
def recode_input(input):
return input.decode(sys.stdin.encoding or 'UTF-8').encode('UTF-8')
0
Example 43
def safe_decode(text, incoming=None, errors='strict'):
"""Decodes incoming str using `incoming` if they're not already unicode.
:param incoming: Text's current encoding
:param errors: Errors handling policy. See here for valid
values http://docs.python.org/2/library/codecs.html
:returns: text or a unicode `incoming` encoded
representation of it.
:raises TypeError: If text is not an instance of str
"""
if not isinstance(text, six.string_types):
raise TypeError("%s can't be decoded" % type(text))
if isinstance(text, six.text_type):
return text
if not incoming:
incoming = (sys.stdin.encoding or
sys.getdefaultencoding())
try:
return text.decode(incoming, errors)
except UnicodeDecodeError:
# Note(flaper87) If we get here, it means that
# sys.stdin.encoding / sys.getdefaultencoding
# didn't return a suitable encoding to decode
# text. This happens mostly when global LANG
# var is not set correctly and there's no
# default encoding. In this case, most likely
# python will use ASCII or ANSI encoders as
# default encodings but they won't be capable
# of decoding non-ASCII characters.
#
# Also, UTF-8 is being used since it's an ASCII
# extension.
return text.decode('utf-8', errors)
0
Example 44
def safe_encode(text, incoming=None,
encoding='utf-8', errors='strict'):
"""Encodes incoming str/unicode using `encoding`.
If incoming is not specified, text is expected to be encoded with
current python's default encoding. (`sys.getdefaultencoding`)
:param incoming: Text's current encoding
:param encoding: Expected encoding for text (Default UTF-8)
:param errors: Errors handling policy. See here for valid
values http://docs.python.org/2/library/codecs.html
:returns: text or a bytestring `encoding` encoded
representation of it.
:raises TypeError: If text is not an instance of str
"""
if not isinstance(text, six.string_types):
raise TypeError("%s can't be encoded" % type(text))
if not incoming:
incoming = (sys.stdin.encoding or
sys.getdefaultencoding())
if isinstance(text, six.text_type):
if six.PY3:
return text.encode(encoding, errors).decode(incoming)
else:
return text.encode(encoding, errors)
elif text and encoding != incoming:
# Decode text before encoding it with `encoding`
text = safe_decode(text, incoming, errors)
if six.PY3:
return text.encode(encoding, errors).decode(incoming)
else:
return text.encode(encoding, errors)
return text
0
Example 45
Project: Biligrab Source File: danmaku2ass2.py
def turn_List_Unicode(List):
Files = []
for _file in List:
Files.append(_file.decode(sys.stdin.encoding))
return Files
0
Example 46
Project: ck-autotuning Source File: module.py
def extract_opts(i):
"""
Input: {
(record) - if 'yes, record to repo
(repo_uoa) - repo UOA to record compiler description
(data_uoa) - data UOA to record compiler description (if empty, autogenerate)
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
import os
import sys
import datetime
o=i.get('out','')
p=os.getcwd()
f1a=cfg['gcc_src_opt_file1']
f1b=cfg['gcc_src_opt_file2']
f2=cfg['gcc_src_params']
results={}
# Checking if GCC
try:
dirList=os.listdir(p)
except Exception as e:
None
else:
for fn in dirList:
pp=os.path.join(p, fn)
if os.path.isdir(pp) and fn.startswith('gcc-'):
found=False
p1a=os.path.join(pp,f1a)
p1b=os.path.join(pp,f1b)
p2=os.path.join(pp,f2)
if os.path.isfile(p1a) or os.path.isfile(p1b) or os.path.isfile(p2):
# Found GCC source directory with needed files
ck.out('cuem*********************************')
ck.out('Processing GCC directory: '+fn)
ck.out('')
ver=fn[4:]
lver=ver.split('.')
dd={}
iopt=0
iparam=0
p1=''
if os.path.isfile(p1a):
p1=p1a
elif os.path.isfile(p1b):
p1=p1b
if p1!='':
# Load opt file
rx=ck.load_text_file({'text_file':p1,
'split_to_list':'yes',
'encoding':sys.stdin.encoding})
if rx['return']>0: return rx
lopts=rx['lst']
found=False
for q in range(0, len(lopts)):
qq=lopts[q].strip()
if not found:
if qq=='@item Optimization Options':
ck.out('Found optimization flags')
found=True
else:
if qq.startswith('@end ') or qq=='':
break
if qq.startswith('@gccoptlist{'):
qq=qq[11:]
elif qq.startswith('@'):
continue
if qq.endswith('@gol'):
qq=qq[:-5]
jj=qq.split(' ')
# Check if base flag
if len(jj)>0 and jj[0].startswith('-O'):
choice=[]
if '-O3' in jj: choice.append('-O3')
for j in jj:
if j!='' and j!='-O3' and j!='-O':
if j.endswith('}'): j=j[:-1].strip()
choice.append(j)
dd["##base_opt"]={
"choice": choice,
"default": "",
"desc": "base compiler flag",
"sort": 10000,
"tags": [
"base",
"basic",
"optimization"
],
"type": "text"
}
else:
for j in jj:
if j!='' and not j.startswith('--param') and not j.startswith('@') and j.startswith('-f') and \
j.find('profile')==-1 and j.find('coverage')==-1:
if '@' in j:
iparam+=1
ij=j.find('@')
opt=j[:ij]
if opt.endswith('@var{n}'): opt=opt[:-7]
ck.out('Adding param '+str(iparam)+' '+opt)
dd['##param-'+opt]={
"can_omit": "yes",
"default": "",
"desc": "compiler flag: "+j,
"sort": iparam*10+30000,
"explore_prefix": j,
"explore_start": 0,
"explore_step": 1,
"explore_stop": 0,
"tags": [
"basic",
"parametric",
"optimization"
],
"type":"integer"
}
else:
iopt+=1
opt=j[2:]
if opt.startswith('no-'):
opt=opt[3:]
ck.out('Adding opt '+str(iopt)+' '+j)
dd['##bool-'+opt]={
"can_omit": "yes",
"choice": [
"-f"+opt,
"-fno-"+opt
],
"default": "",
"desc": "compiler flag: "+j,
"sort": iopt*10+10000,
"tags": [
"basic",
"boolean",
"optimization"
],
"type":"text"
}
# Checking params
if os.path.isfile(p2):
# Load opt file
rx=ck.load_text_file({'text_file':p2,
'split_to_list':'yes',
'encoding':sys.stdin.encoding})
if rx['return']>0: return rx
lparams=rx['lst']
# Parsing params
opt=''
opt1=''
desc=''
desc1=''
add=False
finish=False
for q in range(0, len(lparams)):
qq=lparams[q].strip()
if qq.startswith('DEFPARAM'):
iparam+=1
q+=1
opt=lparams[q].strip()[1:-2]
if opt.find('profile')==-1 and opt.find('coverage')==-1:
q+=1
desc=lparams[q].strip()[1:-2]
line='x'
while True:
q+=1
line=lparams[q].strip()
if line[-1]==')': break
desc+=line[1:-2]
e1=0
e2=0
exp=line[:-1].split(',')
skip=False
for j in range(0, len(exp)):
jj=exp[j].strip()
if jj.find('*')>0 or jj.find('_')>0:
skip=True
else:
jj=int(exp[j].strip())
exp[j]=jj
if not skip:
if len(exp)>1 and exp[2]>exp[1]:
e1=exp[1]
e2=exp[2]
else:
e1=0
e2=exp[0]*2
ck.out('Adding param '+str(iparam)+' "'+opt+'" - '+desc)
dd['##param-'+opt]={
"can_omit": "yes",
"default": "",
"desc": "compiler flag: --param "+opt+"= ("+desc+")",
"sort": iparam*10+30000,
"explore_prefix": "--param "+opt+"=",
"explore_start": e1,
"explore_step": 1,
"explore_stop": e2,
"tags": [
"basic",
"parametric",
"optimization"
],
"type":"integer"
}
# Prepare CK entry
if i.get('record','')=='yes':
duoa=i.get('data_uoa','')
if duoa=='':
duoa=fn+'-auto'
if o=='con':
ck.out('')
ck.out('Recording to '+duoa+' ...')
ii={'action':'add',
'module_uoa':work['self_module_uid'],
'repo_uoa':i.get('repo_uoa',''),
'data_uoa':duoa,
'desc':{'all_compiler_flags_desc':dd},
'dict':{
"tags": [
"compiler",
"gcc",
"v"+lver[0],
"v"+lver[0]+"."+lver[1],
"auto"
]
}
}
r=ck.access(ii)
if r['return']>0: return r
p9=os.path.join(pp, 'ChangeLog')
year=''
if os.path.isfile(p9):
t = os.path.getmtime(p9)
t1=str(datetime.datetime.fromtimestamp(t))
year=t1[:4]
results[fn]={'boolean_opts':iopt, 'parametric_opts':iparam, 'year':year}
# Final info
if o=='con':
ck.out('')
ck.out('Compiler: '+str(fn))
ck.out(' Year: '+year)
ck.out(' Number of boolean opts: '+str(iopt))
ck.out(' Number of parameteric opts: '+str(iparam))
# Summary
if len(results)>0:
ck.out('*********************************************************')
for q in sorted(list(results.keys())):
qq=results[q]
x=qq['year']+', '
ib=str(qq['boolean_opts'])
x+=' '*(6-len(ib))+ib+', '
ip=str(qq['parametric_opts'])
x+=' '*(6-len(ip))+ip+', '
x+=q+' '*(10-len(q))
ck.out(x)
return {'return':0}
0
Example 47
def safe_decode(text, incoming=None, errors='strict'):
"""Decodes incoming text/bytes string using `incoming` if they're not
already unicode.
:param incoming: Text's current encoding
:param errors: Errors handling policy. See here for valid
values http://docs.python.org/2/library/codecs.html
:returns: text or a unicode `incoming` encoded
representation of it.
:raises TypeError: If text is not an instance of str
"""
if not isinstance(text, (six.string_types, six.binary_type)):
raise TypeError("%s can't be decoded" % type(text))
if isinstance(text, six.text_type):
return text
if not incoming:
incoming = (sys.stdin.encoding or
sys.getdefaultencoding())
try:
return text.decode(incoming, errors)
except UnicodeDecodeError:
# Note(flaper87) If we get here, it means that
# sys.stdin.encoding / sys.getdefaultencoding
# didn't return a suitable encoding to decode
# text. This happens mostly when global LANG
# var is not set correctly and there's no
# default encoding. In this case, most likely
# python will use ASCII or ANSI encoders as
# default encodings but they won't be capable
# of decoding non-ASCII characters.
#
# Also, UTF-8 is being used since it's an ASCII
# extension.
return text.decode('utf-8', errors)
0
Example 48
Project: PyClassLessons Source File: questioner.py
def _ask_default(self):
print("Please enter the default value now, as valid Python")
print("The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()")
while True:
if six.PY3:
# Six does not correctly abstract over the fact that
# py3 input returns a unicode string, while py2 raw_input
# returns a bytestring.
code = input(">>> ")
else:
code = input(">>> ").decode(sys.stdin.encoding)
if not code:
print("Please enter some code, or 'exit' (with no quotes) to exit.")
elif code == "exit":
sys.exit(1)
else:
try:
return eval(code, {}, {"datetime": datetime_safe, "timezone": timezone})
except (SyntaxError, NameError) as e:
print("Invalid input: %s" % e)
0
Example 49
def safe_decode(text, incoming=None, errors='strict'):
"""Decodes incoming text/bytes string using `incoming` if they're not
already unicode.
This function was copied from novaclient.openstack.strutils
:param incoming: Text's current encoding
:param errors: Errors handling policy. See here for valid
values http://docs.python.org/2/library/codecs.html
:returns: text or a unicode `incoming` encoded
representation of it.
:raises TypeError: If text is not an instance of str
"""
if not isinstance(text, (six.string_types, six.binary_type)):
raise TypeError("%s can't be decoded" % type(text))
if isinstance(text, six.text_type):
return text
if not incoming:
incoming = (sys.stdin.encoding or
sys.getdefaultencoding())
try:
return text.decode(incoming, errors)
except UnicodeDecodeError:
# Note(flaper87) If we get here, it means that
# sys.stdin.encoding / sys.getdefaultencoding
# didn't return a suitable encoding to decode
# text. This happens mostly when global LANG
# var is not set correctly and there's no
# default encoding. In this case, most likely
# python will use ASCII or ANSI encoders as
# default encodings but they won't be capable
# of decoding non-ASCII characters.
#
# Also, UTF-8 is being used since it's an ASCII
# extension.
return text.decode('utf-8', errors)
0
Example 50
def version_cmd(i):
ck=i['ck_kernel']
fp=i['full_path']
ver=''
p1=os.path.dirname(fp)
p2=os.path.dirname(p1)
p3=os.path.dirname(p2)
fv=os.path.join(p2,'Readme.txt')
if not os.path.isfile(fv):
fv=os.path.join(p3,'Readme.txt')
if os.path.isfile(fv):
rx=ck.load_text_file({'text_file':fv, 'split_to_list':'yes', 'encoding':sys.stdin.encoding})
if rx['return']==0:
lst=rx['lst']
for q in lst:
if q.lower().startswith('freeglut '):
ver=q[9:]
j=ver.find(' ')
if j>=0:
ver=ver[:j]
break
return {'return':0, 'cmd':'', 'version':ver}