def check_full_segment_data(segment, data, context, echoerr): if "name" not in segment: return True, False, False ext = data["ext"] theme_segment_data = context[0][1].get("segment_data", {}) top_theme_name = data["main_config"].get("ext", {}).get(ext, {}).get("theme", None) if not top_theme_name or data["theme"] == top_theme_name: top_segment_data = {} else: top_segment_data = data["ext_theme_configs"].get(top_theme_name, {}).get("segment_data", {}) names = [segment["name"]] if segment.get("type", "function") == "function": module = segment.get("module", context[0][1].get("default_module", "powerline.segments." + ext)) names.insert(0, unicode(module) + "." + unicode(names[0])) segment_copy = segment.copy() for key in ("before", "after", "args", "contents"): if key not in segment_copy: for segment_data in [theme_segment_data, top_segment_data]: for name in names: try: val = segment_data[name][key] # HACK to keep marks l = list(segment_data[name]) k = l[l.index(key)] segment_copy[k] = val except KeyError: pass return check_key_compatibility(segment_copy, data, context, echoerr)
def safe_unicode(s): '''Return unicode instance without raising an exception. Order of assumptions: * ASCII string or unicode object * UTF-8 string * Object with __str__() or __repr__() method that returns UTF-8 string or unicode object (depending on python version) * String in powerline.lib.encoding.get_preferred_output_encoding() encoding * If everything failed use safe_unicode on last exception with which everything failed ''' try: try: if type(s) is bytes: return unicode(s, 'ascii') else: return unicode(s) except UnicodeDecodeError: try: return unicode(s, 'utf-8') except TypeError: return unicode(str(s), 'utf-8') except UnicodeDecodeError: return unicode(s, get_preferred_output_encoding()) except Exception as e: return safe_unicode(e)
def check_segment_data_key(key, data, context, echoerr): ext = data["ext"] top_theme_name = data["main_config"].get("ext", {}).get(ext, {}).get("theme", None) is_top_theme = data["theme"] == top_theme_name if is_top_theme: themes = data["ext_theme_configs"].values() else: themes = [context[0][1]] for theme in themes: for segments in theme.get("segments", {}).values(): found = False for segment in segments: if "name" in segment: if key == segment["name"]: found = True module = segment.get("module", theme.get("default_module", "powerline.segments." + ext)) if key == unicode(module) + "." + unicode(segment["name"]): found = True if found: break if found: break else: echoerr( context="Error while checking segment data", problem="found key {0} that cannot be associated with any segment".format(key), problem_mark=key.mark, ) return True, False, True return True, False, False
def check_key_compatibility(segment, data, context, echoerr): segment_type = segment.get('type', 'function') if segment_type not in type_keys: echoerr(context='Error while checking segments (key {key})'.format(key=context_key(context)), problem='found segment with unknown type {0}'.format(segment_type), problem_mark=segment_type.mark) return False, False, True keys = set(segment) if not ((keys - generic_keys) < type_keys[segment_type]): unknown_keys = keys - generic_keys - type_keys[segment_type] echoerr(context='Error while checking segments (key {key})'.format(key=context_key(context)), context_mark=context[-1][1].mark, problem='found keys not used with the current segment type: {0}'.format( ', '.join((unicode(key) for key in unknown_keys))), problem_mark=list(unknown_keys)[0].mark) return True, False, True if not (keys > required_keys[segment_type]): missing_keys = required_keys[segment_type] - keys echoerr(context='Error while checking segments (key {key})'.format(key=context_key(context)), context_mark=context[-1][1].mark, problem='found missing required keys: {0}'.format( ', '.join((unicode(key) for key in missing_keys)))) return True, False, True return True, False, False
def import_segment(name, data, context, echoerr, module=None): if not module: module = context[-2][1].get('module', context[0][1].get('default_module', 'powerline.segments.' + data['ext'])) with WithPath(data['import_paths']): try: func = getattr(__import__(unicode(module), fromlist=[unicode(name)]), unicode(name)) except ImportError: echoerr(context='Error while checking segments (key {key})'.format(key=context_key(context)), problem='failed to import module {0}'.format(module), problem_mark=module.mark) return None except AttributeError: echoerr(context='Error while loading segment function (key {key})'.format(key=context_key(context)), problem='failed to load function {0} from module {1}'.format(name, module), problem_mark=name.mark) return None if not callable(func): echoerr(context='Error while checking segments (key {key})'.format(key=context_key(context)), problem='imported "function" {0} from module {1} is not callable'.format(name, module), problem_mark=module.mark) return None return func
def check_full_segment_data(segment, data, context, echoerr): if 'name' not in segment: return True, False, False ext = data['ext'] theme_segment_data = context[0][1].get('segment_data', {}) top_theme_name = data['main_config'].get('ext', {}).get(ext, {}).get('theme', None) if not top_theme_name or data['theme'] == top_theme_name: top_segment_data = {} else: top_segment_data = data['ext_theme_configs'].get(top_theme_name, {}).get('segment_data', {}) names = [segment['name']] if segment.get('type', 'function') == 'function': module = segment.get('module', context[0][1].get('default_module', 'powerline.segments.' + ext)) names.insert(0, unicode(module) + '.' + unicode(names[0])) segment_copy = segment.copy() for key in ('before', 'after', 'args', 'contents'): if key not in segment_copy: for segment_data in [theme_segment_data, top_segment_data]: for name in names: try: val = segment_data[name][key] # HACK to keep marks l = list(segment_data[name]) k = l[l.index(key)] segment_copy[k] = val except KeyError: pass return check_key_compatibility(segment_copy, data, context, echoerr)
def convert_to_unicode(object): ''' Converts given object to its unicode string representation like \ python's native "builtins.str()" method. ''' if builtins.isinstance(object, builtins.Exception): if builtins.hasattr(object, '__unicode__'): return object.__unicode__() if builtins.hasattr(object, 'message'): object = object.message if builtins.isinstance(object, builtins.unicode): ''' NOTE: To force specified encoding we should encode and than \ decode here. ''' return object if builtins.isinstance(object, builtins.str): return builtins.unicode(object, ENCODING) if not builtins.isinstance(object, builtins.type): return convert_type_to_unicode(object) ''' NOTE: We have to avoid using an explicit encoding to mimic python \ native "builtins.str()" method behavior for getting a string \ representation. ''' return builtins.unicode(object)
def check_segment_data_key(key, data, context, echoerr): ext = data['ext'] top_theme_name = data['main_config'].get('ext', {}).get(ext, {}).get('theme', None) is_top_theme = (data['theme'] == top_theme_name) if is_top_theme: themes = data['ext_theme_configs'].values() else: themes = [context[0][1]] for theme in themes: for segments in theme.get('segments', {}).values(): found = False for segment in segments: if 'name' in segment: if key == segment['name']: found = True module = segment.get('module', theme.get('default_module', 'powerline.segments.' + ext)) if key == unicode(module) + '.' + unicode(segment['name']): found = True if found: break if found: break else: echoerr(context='Error while checking segment data', problem='found key {0} that cannot be associated with any segment'.format(key), problem_mark=key.mark) return True, False, True return True, False, False
def unicode3(value, *args, **kwargs): """ Rewire of the unicode function for python 3 support """ try: import __builtin__ __builtin__.unicode(value, *args **kwargs) except: return value
def template_to_path(self, track): path = self.template path = path.replace("%{artist}", slugify(unicode(track['artist']))) path = path.replace("%{album}", slugify(unicode(track['album']))) path = path.replace("%{track}", str(track['track']).zfill(2)) path = path.replace("%{title}", slugify(track['title'])) path = u"{0}/{1}.{2}".format(self.directory, path, "mp3") return path
def safe_unicode(s): '''Return unicode instance without raising an exception. ''' try: try: return unicode(s) except UnicodeDecodeError: try: return unicode(s, 'utf-8') except TypeError: return unicode(str(s), 'utf-8') except Exception as e: return safe_unicode(e)
def unicode(text): if sys.version_info < (3, 0): if isinstance(text, str): text = text.decode('utf-8') import __builtin__ return __builtin__.unicode(text) return str(text)
def check_matcher_func(ext, match_name, data, context, echoerr): import_paths = [os.path.expanduser(path) for path in context[0][1].get('common', {}).get('paths', [])] match_module, separator, match_function = match_name.rpartition('.') if not separator: match_module = 'powerline.matchers.{0}'.format(ext) match_function = match_name with WithPath(import_paths): try: func = getattr(__import__(match_module, fromlist=[match_function]), unicode(match_function)) except ImportError: echoerr(context='Error while loading matcher functions', problem='failed to load module {0}'.format(match_module), problem_mark=match_name.mark) return True, True except AttributeError: echoerr(context='Error while loading matcher functions', problem='failed to load matcher function {0}'.format(match_function), problem_mark=match_name.mark) return True, True if not callable(func): echoerr(context='Error while loading matcher functions', problem='loaded "function" {0} is not callable'.format(match_function), problem_mark=match_name.mark) return True, True if hasattr(func, 'func_code') and hasattr(func.func_code, 'co_argcount'): if func.func_code.co_argcount != 1: echoerr(context='Error while loading matcher functions', problem='function {0} accepts {1} arguments instead of 1. Are you sure it is the proper function?'.format(match_function, func.func_code.co_argcount), problem_mark=match_name.mark) return True, False
def message_viewer_has_following_fields_set(context): subject_row = filter(lambda x: x['Field'] == 'Subject:', context.table.rows) assert subject_row is not None, "Subject should be specified to correctly detect message viewer" # Save text and table here, as behave rewrites it in execute_steps text = context.text table = context.table cells = context.app.viewer.findChildren(GenericPredicate(roleName='table cell')) cell_text = map(lambda x: x.text.strip('\xc2\xa0'), cells)[2:] message_body = context.app.viewer.findChildren( GenericPredicate(roleName='document frame'))[-1].\ child(roleName='section').text.strip() cell_text.append('Body:') cell_text.append(message_body) # Convert the list to dict import itertools cell_values = dict(itertools.izip_longest(*[iter(cell_text)] * 2, fillvalue="")) for row in table: expected = cell_values[row['Field']] actual = row['Value'] context.assertion.assertEquals(actual, expected) if text: expected = unicode(cell_values['Body:'], 'utf-8') actual = text context.assertion.assertMultiLineEqual(actual, expected)
def u(s): '''Return unicode instance assuming UTF-8 encoded string. ''' if type(s) is unicode: return s else: return unicode(s, 'utf-8')
def uniCharToKeySym(uniChar): # OK, if it's not actually unicode we can fix that, right? if not isinstance(uniChar, unicode): uniChar = unicode(uniChar) i = ord(uniChar) keySym = Gdk.unicode_to_keyval(i) return keySym
def str(txt): try: ret = __builtin__.str( txt ) except: sys.exc_clear( ) ret = __builtin__.unicode( txt ) return ret
def emails_are_set_to(context, section): (textboxes, comboboxes) = get_combobox_textbox_object( context.app.contact_editor, section) actual = [] for index, textbox in enumerate(textboxes): combo_value = textbox.text if combo_value.strip() != '': type_value = comboboxes[index].combovalue actual.append({'Field': unicode(type_value), 'Value': unicode(combo_value)}) actual = sorted(actual) expected = [] for row in context.table: expected.append({'Field': row['Field'], 'Value': row['Value']}) expected = sorted(expected) context.assertion.assertEqual(actual, expected)
def convert_type_to_unicode(object): '''Converts a generic string representable object to unicode.''' if builtins.hasattr(object, '__unicode__'): return object.__unicode__() elif builtins.hasattr(object, '__str__'): try: object = object.__str__() except builtins.UnicodeEncodeError: if builtins.isinstance(object, Iterable): for index, item in builtins.enumerate(object): object[index] = convert_to_unicode(item) object = object.__str__() else: raise if builtins.isinstance(object, builtins.unicode): return object return builtins.unicode(object, ENCODING) return builtins.unicode(object)
def unicode(s, encoding): """Force conversion of s to unicode.""" if PY3: return s else: if isinstance(s, __builtin__.unicode): return s return __builtin__.unicode(s, encoding)
def main(configurationDirectory): import sys if len(sys.argv)>1: configFile=unicode(sys.argv[1]) runtime=ExperimentRuntime(configurationDirectory, configFile) else: runtime=ExperimentRuntime(configurationDirectory, "experiment_config.yaml") runtime.start()
def subopen(args, stdin=None, shell=False, cwd=None, env=None): temp = None pid = None close_fds = True rstrip = True try: if stdin is not None and isstring(stdin): temp = tempfile() write(temp, stdin) stdin = open(temp) args = tolist(args) for i, arg in enumerate(args): if not isinstance(arg, int): if PY3: args[i] = str(arg) # python3 else: args[i] = __builtin__.unicode(arg) # python2 if not env: env = os.environ.copy() process = subprocess.Popen(args, stdin=stdin, stderr=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=close_fds, shell=shell, cwd=cwd, env=env, universal_newlines=False # , # startupinfo=startupinfo#, # creationflags=creationflags ) pid = process.pid stdoutdata, stderrdata = process.communicate() if PY3: # python3. process.communicate returns bytes stdoutdata = str(stdoutdata, "utf-8") # stdoutdata stderrdata = str(stderrdata, "utf-8") # stderrdata # rstrip if rstrip and stdoutdata: stdoutdata = stdoutdata.rstrip() if rstrip and stderrdata: stderrdata = stderrdata.rstrip() returncode = process.returncode return (returncode, stdoutdata, stderrdata) # raise CalledProcessError(returncode,args,err) # if returncode!=0: # raise CalledProcessError(returncode,args,stderrdata) # return stdoutdata finally: try: if pid: os.killpg(pid, signal.SIGTERM) except Exception: pass # print(format_exc()) rm(temp)
def check_segment_module(module, data, context, echoerr): with WithPath(data['import_paths']): try: __import__(unicode(module)) except ImportError: echoerr(context='Error while checking segments (key {key})'.format(key=context_key(context)), problem='failed to import module {0}'.format(module), problem_mark=module.mark) return True, False, True return True, False, False
def property_in_section_is_set_to(context, field, section, expected): element = get_element_by_name( context.app.contact_editor, field, section=section) if element.roleName == "text": actual = element.text elif element.roleName == "combo box": actual = element.combovalue if actual == '': actual = element.textentry('').text context.assertion.assertEquals(unicode(actual), expected)
def __init__(self, **attrs): res = super(string,self).__init__(**attrs) # ensure that self._object_ is using a fixed-width encoding _object_ = self._object_ # encode 3 types of strings and ensure that their lengths scale up with their string sizes res,single,double = ( __builtin__.unicode(n, 'ascii').encode(_object_.encoding.name) for n in ('\x00', 'A', 'AA') ) if len(res) * 2 == len(single) * 2 == len(double): return raise ValueError(self.classname(), 'string.__init__', 'User tried to specify a variable-width character encoding : {:s}'.format(_object_.encoding.name))
def __setvalue__(self, value): '''Set the _char_t to the str ``value``.''' if isinstance(value, __builtin__.str): try: value = __builtin__.unicode(value, 'ascii') except UnicodeDecodeError: return super(pint.integer_t,self).__setvalue__(str(value)) elif isinstance(value, __builtin__.unicode): value = value else: raise ValueError(self, '_char_t.set', 'User tried to set a value of an incorrect type : {:s}'.format(value.__class__)) res = value.encode(self.encoding.name) return super(pint.integer_t,self).__setvalue__(res)
def unicode(text): if sys.version_info < (3, 0): if isinstance(text, str): text = text.decode("utf-8") import __builtin__ return __builtin__.unicode(text) elif not isinstance(text, str): return text.decode("utf-8") else: return text
def check_tuple(self, value, context_mark, data, context, echoerr, start, end): hadproblem = False for (i, item, spec) in zip(itertools.count(), value, self.specs[start:end]): proceed, ihadproblem = spec.match( item, value.mark, data, context + (("tuple item " + unicode(i), item),), echoerr ) if ihadproblem: hadproblem = True if not proceed: return False, hadproblem return True, hadproblem
def getTokenErrorDisplay(self, t): if t is None: return u"<no token>" s = t.text if s is None: if t.type == Token.EOF: s = u"<EOF>" else: s = u"<" + unicode(t.type) + u">" s = s.replace(u"\n", u"\\n") s = s.replace(u"\r", u"\\r") s = s.replace(u"\t", u"\\t") return u"'" + s + u"'"
def out_u(s): '''Return unicode string suitable for displaying Unlike other functions assumes get_preferred_output_encoding() first. Unlike u() does not throw exceptions for invalid unicode strings. Unlike safe_unicode() does throw an exception if object is not a string. ''' if isinstance(s, unicode): return s elif isinstance(s, bytes): return unicode(s, get_preferred_output_encoding(), 'powerline_decode_error') else: raise TypeError('Expected unicode or bytes instance, got {0}'.format(repr(type(s))))
def __unicode__(self): return u"pred_" + unicode(self.ruleIndex) + u":" + unicode( self.predIndex)
def __str__(self): return unicode(self)
def __unicode__(self): return unicode(self.label_)
def __unicode__(self): return u"action_" + unicode(self.ruleIndex) + u":" + unicode( self.actionIndex)