def write_py_rst_collections_file(root_path, collection_name, subfolders, files): # go through subfolders and find the index.rst and add to toc jazz # go through files and list global methods (leave making nice "summary table" as another exercise) title = upper_first(collection_name) title_emphasis = "=" * len(title) # Frame <frame-/index.rst> toctree = indent("\n".join(["%s <%s/index.rst>" % (entity_type_to_class_name(subfolder.replace('-',':')), subfolder) for subfolder in subfolders])) names =[f[:-4] for f in files if f[-4:] == ".rst" and f != "index.rst"] globs = "\n\n".join([":doc:`%s<%s>`" % (name, name) for name in names]) hidden_toctree = indent("\n".join(names)) content = """ {title} {title_emphasis} **Classes** .. toctree:: {toctree} .. toctree:: :hidden: {hidden_toctree} ------- **Global Methods** {globs} """.format(title=title, title_emphasis=title_emphasis, toctree=toctree, hidden_toctree=hidden_toctree, globs=globs) file_path = os.path.join(root_path, "index.rst") write_text_to_file(file_path, content)
def create_get_object(self): get_object_name = "__get_%s" % self._term rest_target = '%ss' % self._term rest_target_with_name = '%s?name=' % rest_target module_logger = self._module_logger http = self._http term = self._term obj_class = self._class get_class = get_entity_class_from_store def get_object(identifier): module_logger.info("%s(%s)", get_object_name, identifier) if isinstance(identifier, basestring): if uri_pattern.match(identifier): uri = identifier else: uri = rest_target_with_name + identifier else: uri = '%s/%s' % (rest_target, identifier) r = http.get(uri) try: entity_type = r.json()['entity_type'] except KeyError: return obj_class(_info=r.json()) else: if not entity_type.startswith(term): raise ValueError("Object '%s' is not a %s type" % (identifier, term)) cls = get_class(entity_type) return cls(_info=r.json()) set_entity_collection(get_object, entity_type_to_collection_name( self._term)) # so meta knows where it goes get_object.__name__ = get_object_name get_object.__doc__ = """Get handle to a {obj_term} object.""".format( obj_term=self._term) # decorate the method with api and arg, which will also give a nice command def for setting the doc_stub api_decorator = get_api_decorator(module_logger, parent_class_name="_BaseGlobals") arg_decorator = arg(name="identifier", data_type="str | int", description="Name of the %s to get" % self._term) # Determining the return type is difficult, because we don't know in advance what specific type of entity # we will get (like Frame or VertexFrame or ?). We choose to go with the general entity type, like "Frame", # because it is likely the most common case and also has backing for SPA (i.e. _BaseFrame does not get defined # in the DocStubs) Btw, we know "Model" will fail SPA --there is no "general" Model class. returns_type = upper_first(self._term) # so, "frame" -> "Frame" returns_decorator = returns(returns_type, "%s object" % self._term) decorated_method = api_decorator( returns_decorator(arg_decorator(get_object))) return decorated_method
def create_get_object(self): get_object_name = "__get_%s" % self._term rest_target = '%ss' % self._term rest_target_with_name = '%s?name=' % rest_target module_logger = self._module_logger http = self._http term = self._term obj_class = self._class get_class = get_entity_class_from_store def get_object(identifier): module_logger.info("%s(%s)", get_object_name, identifier) if isinstance(identifier, basestring): if uri_pattern.match(identifier): uri = identifier else: uri = rest_target_with_name + identifier else: uri = '%s/%s' % (rest_target, identifier) r = http.get(uri) try: entity_type = r.json()['entity_type'] except KeyError: return obj_class(_info=r.json()) else: if not entity_type.startswith(term): raise ValueError("Object '%s' is not a %s type" % (identifier, term)) cls = get_class(entity_type) return cls(_info=r.json()) set_entity_collection(get_object, entity_type_to_collection_name(self._term)) # so meta knows where it goes get_object.__name__ = get_object_name get_object.__doc__ = """Get handle to a {obj_term} object.""".format(obj_term=self._term) # decorate the method with api and arg, which will also give a nice command def for setting the doc_stub api_decorator = get_api_decorator(module_logger, parent_class_name="_BaseGlobals") arg_decorator = arg(name="identifier", data_type="str | int", description="Name of the %s to get" % self._term) # Determining the return type is difficult, because we don't know in advance what specific type of entity # we will get (like Frame or VertexFrame or ?). We choose to go with the general entity type, like "Frame", # because it is likely the most common case and also has backing for SPA (i.e. _BaseFrame does not get defined # in the DocStubs) Btw, we know "Model" will fail SPA --there is no "general" Model class. returns_type = upper_first(self._term) # so, "frame" -> "Frame" returns_decorator = returns(returns_type, "%s object" % self._term) decorated_method = api_decorator(returns_decorator(arg_decorator(get_object))) return decorated_method
def test_upper_first(self): from trustedanalytics.meta.names import upper_first self.assertEqual("Apple", upper_first('apple')) self.assertEqual("Apple", upper_first('Apple')) self.assertEqual('', upper_first('')) self.assertEqual('', upper_first(None))