Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions | Variables
gn_helpers Namespace Reference

Classes

class  GNException
 

Functions

 _TranslateToGnChars (s)
 
 ToGNString (value, allow_dicts=True)
 

Variables

_Ord = ord if sys.version_info.major < 3 else lambda c
 

Detailed Description

Helper functions useful when writing scripts that are run from GN's
exec_script function.

Function Documentation

◆ _TranslateToGnChars()

gn_helpers._TranslateToGnChars (   s)
protected

Definition at line 19 of file gn_helpers.py.

19def _TranslateToGnChars(s):
20 for decoded_ch in s.encode('utf-8'): # str in Python 2, bytes in Python 3.
21 code = _Ord(decoded_ch) # int
22 if code in (34, 36, 92): # For '"', '$', or '\\'.
23 yield '\\' + chr(code)
24 elif 32 <= code < 127:
25 yield chr(code)
26 else:
27 yield '$0x%02X' % code
28
29

◆ ToGNString()

gn_helpers.ToGNString (   value,
  allow_dicts = True 
)
Returns a stringified GN equivalent of a Python value.

allow_dicts indicates if this function will allow converting dictionaries
to GN scopes. This is only possible at the top level, you can't nest a
GN scope in a list, so this should be set to False for recursive calls.
Prints the given value to stdout.

allow_dicts indicates if this function will allow converting dictionaries
to GN scopes. This is only possible at the top level, you can't nest a
GN scope in a list, so this should be set to False for recursive calls.

Definition at line 30 of file gn_helpers.py.

30def ToGNString(value, allow_dicts=True):
31 """Returns a stringified GN equivalent of a Python value.
32
33 allow_dicts indicates if this function will allow converting dictionaries
34 to GN scopes. This is only possible at the top level, you can't nest a
35 GN scope in a list, so this should be set to False for recursive calls."""
36 if isinstance(value, str) or isinstance(value, unicode):
37 if value.find('\n') >= 0:
38 raise GNException("Trying to print a string with a newline in it.")
39 return '"' + ''.join(_TranslateToGnChars(value)) + '"'
40
41 if isinstance(value, list):
42 return '[ %s ]' % ', '.join(ToGNString(v, False) for v in value)
43
44 if isinstance(value, dict):
45 if not allow_dicts:
46 raise GNException("Attempting to recursively print a dictionary.")
47 result = ""
48 for key in value:
49 if not isinstance(key, str):
50 raise GNException("Dictionary key is not a string.")
51 result += "%s = %s\n" % (key, ToGNString(value[key], False))
52 return result
53
54 if isinstance(value, int):
55 return str(value)
56
57 raise GNException("Unsupported type %s (value %s) when printing to GN." %
58 (type(value), value))

Variable Documentation

◆ _Ord

c gn_helpers._Ord = ord if sys.version_info.major < 3 else lambda c
protected

Definition at line 16 of file gn_helpers.py.