def StructuralDbroot(): """Singleton-like empty dbroot as a cache. Returns: empty dbroot """ if not StructuralDbroot.cached_dbroot: StructuralDbroot.cached_dbroot = dbroot_utils.MakeEmptyDbroot() return StructuralDbroot.cached_dbroot
def _LoadPathValuesFromDict(snippets, log): """Loads {fieldpath: value} from dictionary. Verifies field path spelling and demangle it into valid protobuf source form. a.b.c:value -> a.b.c.value Args: snippets: snippets path:value map. log: logging obj. Returns: A dict, of value, by fieldpath. """ hardwired_values = {} for mangled_path, value in snippets.iteritems(): # Allow for value being multi-word. <path> of course is 'abstract', # ie looks like 'a.b.c', no [] or indices. log.debug("field:[%s], value:[%s]", mangled_path, str(value)) log.debug("maybe mangled: %s", mangled_path) path = path_converters.Demangled(mangled_path) log.debug("path: %s", path) if not proto_reflection.IsLegitFieldPath(path): log.warning( "The path '%s' is misspelled or otherwise does not exist." " Skipping.", path) continue if not path_utils.IsAbstract(path): log.warning( "The above path is expected to be abstract (no \'[\', \']\'" " or indices).") # Assure the end_snippet prefix path = path_utils.EnsureFull(path) field_type = proto_reflection.TypeAtFieldPath( dbroot_utils.MakeEmptyDbroot(), path) log.debug("field:[%s], value:[%s]", path, str(value)) if field_type == google.protobuf.descriptor.FieldDescriptor.TYPE_BOOL: if isinstance(value, str): value = _FlexibleBool(value) log.debug("path getting bool default: %s: %s", path, str(value)) hardwired_values[path] = value return hardwired_values
def CreateEndSnippetProto(snippets_json, search_def_list, search_tab_id, supplemental_search_label, supplemental_search_url, log): """Creates end snippets proto section from snippets json string. Precedence is: filled defaults < user values < forced values. Args: snippets_json: a set of end snippets in json format. search_def_list: a list of search definition objects (basic_types.SearchDef) describing search services: label, service_url,... search_tab_id: a search tab ID to append to a search service label. If the search tab id is not None, it is added as a suffix to a search service label (format: 'search_service_label [target_path]') to get search tab label. Otherwise (None), a search service label is used as is. supplemental_search_label: a label for the supplemental UI search. supplemental_search_url: an URL for the supplemental UI search. log: logger object. Returns: serialized to string proto dbroot content. """ log.debug("CreateEndSnippetProto...") dbroot = dbroot_utils.MakeEmptyDbroot() log.debug("made empty") # TODO a bit inelegant - should write straight to # protobuf the 'default-disarming', 'nerfing' values. defaults_plugged_tree = {} # Overwrite defaults now; the /user's/ values will be written & take # precedence, later. snippet_masker.NerfUnwantedExposedDefaults(defaults_plugged_tree, log) log.debug("masked unfriendly...") log.debug("defaults plugged:" + str(defaults_plugged_tree)) _WriteTreeToDbroot(defaults_plugged_tree, dbroot, log) # The user's values if snippets_json: log.debug("writing users snippets") users_snippets_tree = tree_utils.CompactTree(snippets_json, log) _WriteTreeToDbroot(users_snippets_tree, dbroot, log) # Finally, overwrite with any forced values we have. forced = {} log.debug("writing forced snippets") snippet_masker.ForceFields(forced, log) log.debug("forced fields") _WriteTreeToDbroot(forced, dbroot, log) # Add search servers to proto end_snippet. _AddSearchServers(dbroot, search_def_list, search_tab_id, supplemental_search_label, supplemental_search_url, log) # Note: useful for debugging. # if __debug__: # log.debug("CreateEndSnippetProto - PROTO DBROOT: %s", dbroot) content = dbroot.SerializeToString() # Note: useful for debugging. # dbroot_restored = dbroot_utils.MakeEmptyDbroot() # dbroot_restored.ParseFromString(content) # dbroot_restored.ParseFromString(content) # log.debug("CreateEndSnippetProto - PROTO DBROOT RESTORED: %s", # dbroot_restored) return content
def _MassageSpecialCases(almost_snippet_values, log): """Catchall for converting anything fiddled-with back to pure dbroot form. Right now just translates enum text to numeric values. Args: almost_snippet_values: snippet values. log: logger obj. Returns: All the snippet values, ready to write to a dbroot. """ log.debug(">massaging...") true_snippet_values = [] assert isinstance(almost_snippet_values, list) log.debug("survived assertion") dbroot_proto_for_structure = dbroot_utils.MakeEmptyDbroot() for concrete_fieldpath, snippet_value in almost_snippet_values: log.debug("concrete for no good reason " + concrete_fieldpath) abstract_fieldpath = path_utils.AsAbstract(concrete_fieldpath) log.debug("abs field:" + abstract_fieldpath) fdesc = proto_reflection.FieldDescriptorAtFieldPath( dbroot_proto_for_structure, abstract_fieldpath) log.debug("got field desc") is_enum = fdesc.enum_type is not None log.debug("enum? " + str(is_enum)) # Special case - on the client we represent /repeated enums/ as a choice of # fixed checkboxes. Here, we convert that back. if not is_enum: # plain log.debug("massaging, but plain: " + concrete_fieldpath + " " + str(snippet_value)) true_snippet_values.append((concrete_fieldpath, snippet_value)) log.debug("did not massage non-enum") else: is_repeated = fdesc.label == fdesc.LABEL_REPEATED if not is_repeated: log.debug("massaging singular enum:" + concrete_fieldpath + snippet_value) enum_val = _EnumValFromText(fdesc, snippet_value, log) log.debug("massaged singular; " + concrete_fieldpath + " " + str(enum_val)) true_snippet_values.append((concrete_fieldpath, enum_val)) else: # repeated enum log.debug("supposedly repeated enum...name: %s %s" % (concrete_fieldpath, str(snippet_value))) enum_text_vals = _ExtractWidgetEnumTextValues( snippet_value, log) for i, enum_text in enumerate(enum_text_vals): log.debug("enum text: " + enum_text) log.debug("all enum vals: " + fdesc.name) log.debug("all enum vals: " + str(fdesc.enum_type.values_by_name.keys())) enum_val = _EnumValFromText(fdesc, enum_text, log) log.debug("whew, found enum val!") # need to concretize, now! No way around it. log.debug("special enum snippetval: " + snippet_value + "->" + str(enum_val)) # thank heaven these can only be primitives! (& thus easy to get at) added_concrete_fieldpath = concrete_fieldpath + "[%d]" % i log.debug("enummed: " + added_concrete_fieldpath) true_snippet_values.append( (added_concrete_fieldpath, enum_val)) log.debug("massaged enum") log.debug("done massaging") return true_snippet_values