Example #1
0
 def test_get_doc_from_wrong_root(self) :
     self._print(f"ContextTests.test_get_doc_from_wrong_root")
     if self.off(): return
     metadata = ContextMetadata()
     context = Context(metadata)
     docpath = "/app/home/teams/todos/assignee"
     txt = context.get_doc_from_roots(["images"], docpath, False)
     self.assertIsNone(txt, msg=f"doc at {docpath} in root 'images' must be none")
Example #2
0
 def test_get_doc_from_right_root(self) :
     self._print(f"ContextTests.test_get_doc_from_right_root")
     if self.off(): return
     metadata = ContextMetadata()
     context = Context(metadata)
     docpath = "/app/home/teams/todos/assignee"
     txt = context.get_doc_from_roots(["public"], docpath)
     self.assertIsNotNone(txt, msg=f"doc at {docpath} in root 'public' must not be none")
Example #3
0
 def setup(self,
           configpath: Optional[str] = None,
           askdebug: Optional[bool] = True):
     self.ask_debug()
     configpath = self.ask_config()
     metadata = None
     self._config = SimpleConfig(configpath)
     self._metadata = ContextMetadata(self._config)
     self._context = Context(self._metadata)
Example #4
0
 def test_get_doc_just_checking(self) :
     self._print(f"ContextTests.test_get_doc_just_checking")
     if self.off(): return
     metadata = ContextMetadata()
     context = Context(metadata)
     docpath = "/app/home/teams*delete_assignee"
     txt = context.get_doc(docpath)
     # this doc could be a not-found doc. for what I need today that's fine.
     self.assertIsNotNone(txt, msg=f"doc at {docpath} must not be none")
     self._print(f"ContextTests.test_get_doc_just_checking: doing get_docs. the doc txt is: {txt}")
Example #5
0
 def test_get_known_type_doc(self):
     self._print(f"ContextTests.test_get_known_type_doc")
     if self.off(): return
     metadata = ContextMetadata()
     context = Context(metadata)
     docpath = "/app/home/teams/3-copy.png"
     f = context.get_doc(docpath)
     self._print(f"the image doc {type(f)} ")
     self.assertIsNotNone(f, msg=f"doc at {docpath} must not be none")
     self.assertEqual( type(f).__name__, 'bytes', msg=f"type must be 'bytes'")
Example #6
0
 def test_filter_root_names_for_path(self):
     self._print("ContextTests.test_filter_root_names_for_path")
     if self.off(): return
     metadata = ContextMetadata()
     context = Context(metadata)
     roots = ["images", "fish", "public"]
     rs = context.filter_root_names_for_path(roots, "/x/y/z")
     self._print(f"filtered roots: {rs}")
     self.assertIsNotNone( rs, msg="filtered roots must not be None")
     self.assertEqual(len(rs), 1, msg="must be 1 filtered root")
     self.assertEqual(rs, ["public"], msg="filtered roots must be ['public']")
Example #7
0
 def test_get_filetype(self):
     self._print("ContextTests.test_get_filetype")
     if self.off(): return
     metadata = ContextMetadata()
     context = Context(metadata)
     atype = context.get_filetype("/x/y/z.gif")
     self._print(f"type for /x/y/z.gif: {atype}")
     self.assertEqual(atype, "gif", msg="type must be gif")
     atype = context.get_filetype("/x/y/z")
     self._print(f"type for /x/y/z: {atype}")
     self.assertEqual(atype, 'cdocs', msg="type must be cdocs")
Example #8
0
 def test_get_doc(self) :
     self._print(f"ContextTests.test_get_doc")
     if self.off(): return
     metadata = ContextMetadata()
     context = Context(metadata)
     docpath = "/app/home/teams/todos/assignee"
     txt = context.get_doc(docpath)
     self.assertIsNotNone(txt, msg=f"doc at {docpath} must not be none")
     self._print(f"test_get_doc: doing get_docs. the doc txt is: {txt}")
     point = txt.find("assignee in company starstruck!")
     self.assertNotEqual(-1,point, msg="must include 'assignee in company starstruck!'")
     point = txt.find("my app name: you should see: my app's name is fruit")
     self.assertNotEqual(-1,point, msg="must include 'my app name: you should see: my app's name is fruit'")
Example #9
0
 def test_get_root_names_accepting_path(self):
     self._print("ContextTests.test_get_root_names_accepting_path")
     if self.off(): return
     metadata = ContextMetadata()
     context = Context(metadata)
     gifs = context.get_root_names_accepting_path("/x/y/z.gif")
     self._print(f"roots for /x/y/z.gif: {gifs}")
     self.assertIsNotNone( gifs, msg="gifs must not be None")
     self.assertEqual(len(gifs), 1, msg="must be 1 items in gifs")
     cdocs = context.get_root_names_accepting_path("/x/y/z")
     self._print(f"roots for /x/y/z: {cdocs}")
     self.assertIsNotNone( cdocs, msg="cdocs must not be None")
     self.assertEqual(len(cdocs), 2, msg="must be 2 items in cdocs")
Example #10
0
 def test_get_labels(self):
     self._print(f"ContextTests.test_get_labels")
     if self.off(): return
     docpath = "/app/home/teams/todos/assignee"
     #docpath = "/v1/test/fish"
     metadata = ContextMetadata()
     context = Context(metadata)
     labels = context.get_labels(docpath)
     print(f"test_get_labels: the labels are: {labels}")
     self.assertIn("app", labels, msg=f"labels at {docpath} must include key 'app'")
     self.assertIn("team", labels, msg=f"labels at {docpath} must include key 'team'")
     self.assertIn("my_app_name", labels, msg=f"labels at {docpath} must include key 'my_app_name'")
     self.assertEqual("my app's name is fruit", labels["my_app_name"], msg=f"label my app name must == my app's name is fruit")
     self.assertEqual("starstruck", labels["company"], msg=f"label company must == starstruck")
Example #11
0
 def test_get_compose_doc_with_roots(self):
     self._print(f"ContextTests.test_get_compose_doc_with_roots")
     if self.off(): return
     docpath = "/app/home/teams"
     metadata = ContextMetadata()
     context = Context(metadata)
     doc = context.get_doc_from_roots(["internal"], docpath, False)
     self._print(f"test_get_compose_doc_with_roots: doc from 'internal': {doc}")
     assignee = doc.find('assignee in company starstruck!')
     self.assertNotEqual(-1, assignee, msg=f'{docpath} must include "assignee in company starstruck!" in {doc}')
     edit = doc.find('edit assignee')
     self.assertNotEqual(-1, edit, msg=f'{docpath} must include "edit assignee" in {doc}')
     doc = context.get_doc_from_roots(["public"], docpath)
     self._print(f"test_get_compose_doc_with_roots: doc from 'public': {doc}")
     notfound = doc.find("Not found!")
     self.assertNotEqual(notfound, -1, msg=f'doc at {docpath} must include "Not found!"')
Example #12
0
 def test_create_context(self):
     self._print("ContextTests.test_context")
     if self.off(): return
     metadata = ContextMetadata()
     context = Context(metadata)
     cdocs = context.cdocs
     self.assertEqual( len(cdocs), 5, msg=f"must be 4 cdocs, not {len(cdocs)} from {cdocs}" )
Example #13
0
class Repl(object):
    def __init__(self):
        self._config = None
        self._metadata = None
        self._context = None
        self._continue = True
        self._debug = False
        self._commands = {}
        self._logger = logging.getLogger('')
        self._add_commands()

    def _add_commands(self):
        self.commands["quit"] = self.quit
        self.commands["read"] = self.read
        self.commands["list"] = self.list
        self.commands["roots"] = self.roots
        self.commands["labels"] = self.labels
        self.commands["tokens"] = self.tokens
        self.commands["debug"] = self.debug
        self.commands["help"] = self.help
        self.commands["?"] = self.help

    @property
    def commands(self):
        return self._commands

    @commands.setter
    def commands(self, adict):
        self._commands = adict

    def ask_debug(self):
        d = input("want to debug during load? (y/n) ")
        if (d == 'y'):
            self.debug()

    def ask_config(self):
        d = input("want to use your own config? (y/n) ")
        if (d == 'y'):
            return input("what path? ")
        return None

    def setup(self,
              configpath: Optional[str] = None,
              askdebug: Optional[bool] = True):
        self.ask_debug()
        configpath = self.ask_config()
        metadata = None
        self._config = SimpleConfig(configpath)
        self._metadata = ContextMetadata(self._config)
        self._context = Context(self._metadata)

    def loop(self):
        print("\n")
        while self._continue:
            self._one_loop()

    def _one_loop(self) -> bool:
        cmd = input("cmd: ")
        self.do_cmd(cmd)

    def do_cmd(self, cmd):
        print(f"do_cmd: {cmd}")
        callme = self.commands.get(cmd)
        if callme is None:
            print(f"not sure what {cmd} means")
        else:
            callme()

    def debug(self):
        if self._debug:
            self._logger.setLevel(level=logging.WARN)
            self._logger.warning("Set level to WARN")
            self._debug = False
        else:
            self._logger.setLevel(level=logging.DEBUG)
            self._logger.debug("Set level to DEBUG")
            self._debug = True

    def help(self):
        print("\nHelp:")
        for k, v in self.commands.items():
            print(f"   {k}")
        return True

    def read(self):
        roots = self._get_roots()
        docpath = input("docpath: ")
        doc = ""
        try:
            if len(roots) >= 1:
                doc = self._context.get_doc_from_roots(roots, docpath)
            else:
                doc = self._context.get_doc(docpath)
            print("\ndoc: ")
            print(f"{doc}")
        except BadDocPath as e:
            print(f"Error: {e}")
        return True

    def labels(self):
        roots = self._get_roots()
        docpath = input("docpath: ")
        labels = ""
        try:
            if len(roots) >= 1:
                labels = self._context.get_labels_from_roots(roots, docpath)
            else:
                labels = self._context.get_labels(docpath)
            print("\nlabels: ")
            print(f"{labels}")
        except BadDocPath as e:
            print(f"Error: {e}")
        return True

    def tokens(self):
        roots = self._get_roots()
        docpath = input("docpath: ")
        labels = ""
        try:
            if len(roots) >= 1:
                labels = self._context.get_tokens_from_roots(roots, docpath)
            else:
                tokens = self._context.get_tokens(docpath)
            print("\ntokens: ")
            print(f"{tokens}")
        except BadDocPath as e:
            print(f"Error: {e}")
        return True

    def _get_roots(self):
        roots = input("which roots (Csv or return for all): ")
        print("roots are: " + roots)
        roots = [] if roots == "" else roots.split(",")
        print(f"roots are: {roots}")
        return roots

    def list(self):
        roots = self._get_roots()
        docpath = input("docpath: ")
        docs = []
        if len(roots) >= 1:
            docs = self._context.list_docs_from_roots(roots, docpath)
        else:
            docs = self._context.list_docs(docpath)
        print("\ndocs: ")
        for doc in docs:
            print(f"   {doc}")
        return True

    def roots(self):
        roots = self._config.get_items("docs")
        print("\nroots:")
        for root in roots:
            print(f"   {root}")
        return True

    def quit(self):
        self._continue = False
        return True
Example #14
0
    def test_split_plus_concat(self):
        # tests the use of:
        #    split-plus searches
        #    non-split-plus searches
        #    split-plus searches with roots that deny split-plus
        #    implicitly, the order of multi-root searches
        # see config.ini re: order of roots under [doc] in the
        # context of split-plus searches
        docpath = "app/home/teams#delete_assignee+todo"
        metadata = ContextMetadata()
        context = Context(metadata)

        # split plus: true
        #    both roots allow split plus
        doc = context.get_doc(docpath)
        print(f"\nContextTests.test_split_plus_concat: doc 1:\n{doc}")
        self.assertIsNotNone(doc, msg=f"doc at {docpath} must not be none")
        found = doc.find("edit assignee") > -1
        found = found and doc.find("my app name: you should see") > -1
        self.assertEqual(found, True, msg=f"doc: {doc} doesn't have expected content")

        # split plus: false
        #    both roots allow split plus
        doc = context.get_doc(docpath, False, False)
        print(f"\nContextTests.test_split_plus_concat: doc 2:\n{doc}")
        found = doc.find("edit assignee") > -1
        found = found and doc.find("my app name: you should see") > -1
        self.assertEqual(found, False, msg=f"doc: {doc} doesn't have expected content")

        context._nosplitplus = ['internal','public']

        # split plus: true
        #    both roots deny split plus
        doc = context.get_doc(docpath)
        print(f"\nContextTests.test_split_plus_concat: doc 3:\n{doc}")
        self.assertIsNotNone(doc, msg=f"doc at {docpath} must not be none")
        found = doc.find("edit assignee") == -1
        found = found and doc.find("my app name: you should see") == -1
        self.assertEqual(found, True, msg=f"doc: {doc} doesn't have expected content")
        # split plus: false
        #    both roots deny split plus
        doc = context.get_doc(docpath, False, False)
        print(f"\nContextTests.test_split_plus_concat: doc 4:\n{doc}")
        found = doc.find("edit assignee") == -1
        found = found and doc.find("my app name: you should see") == -1
        self.assertEqual(found, False, msg=f"doc: {doc} doesn't have expected content")

        context._nosplitplus = ['public']

        # split plus: true
        #    public denies split plus
        doc = context.get_doc(docpath)
        print(f"\nContextTests.test_split_plus_concat: doc 5:\n{doc}")
        self.assertIsNotNone(doc, msg=f"doc at {docpath} must not be none")
        found = doc.find("edit assignee") > -1
        found = found and doc.find("my app name: you should see") == -1
        self.assertEqual(found, True, msg=f"doc: {doc} doesn't have expected content")
        # split plus: false
        #    public denies split plus
        doc = context.get_doc(docpath, False, False)
        print(f"\nContextTests.test_split_plus_concat: doc 6:\n{doc}")
        found = doc.find("edit assignee") == -1
        found = found and doc.find("my app name: you should see") > -1
        self.assertEqual(found, True, msg=f"doc: {doc} doesn't have expected content")

        context._nosplitplus = ['internal']

        # split plus: true
        #    internal denies split plus
        doc = context.get_doc(docpath)
        print(f"\nContextTests.test_split_plus_concat: doc 7:\n{doc}")
        self.assertIsNotNone(doc, msg=f"doc at {docpath} must not be none")
        found = doc.find("edit assignee") == -1
        found = found and doc.find("my app name: you should see") > -1
        self.assertEqual(found, True, msg=f"doc: {doc} doesn't have expected content")

        # split plus: false
        #    internal denies split plus
        doc = context.get_doc(docpath, False, False)
        print(f"\nContextTests.test_split_plus_concat: doc 8:\n{doc}")
        found = doc.find("edit assignee") == -1
        found = found and doc.find("my app name: you should see") > -1
        self.assertEqual(found, True, msg=f"doc: {doc} doesn't have expected content")