Repository: coala/coala

Issue:
glob/collecting: Accept strings too
the methods there want a list but `glob("string")` makes sense too (and behaves very strangely), we should support that.



Discussion:
When I run `glob("coalib")` it accurately shows - `['coalib']`

What's weird about it ?

at least I tried `collect_dirs("/some/absolute/path")` and got pretty weird resuilts, I may try reproducing later in a more organized way...

so here's the issue:

```
>>> glob("*/pars*")
['coalib/parsing']
>>> collect_dirs("*/pars*")
['.misc', 'bears', 'docs', 'locale', 'coalib', '.git', '/']
```

@sils1297 well, yes, this kinda is by design. Glob always works on one file path and therefore takes a string. the collectors accept a list (iterable) of glob strings so that they only need to be called once.

Passing a string (iterable of chars) leads to glob being called on every char, in your example `glob('*')` yields the results you describe.

Anyway, we can change the design, so I will do that very quick.


Resolving patch:
diff --git a/coalib/collecting/Collectors.py b/coalib/collecting/Collectors.py
--- a/coalib/collecting/Collectors.py
+++ b/coalib/collecting/Collectors.py
@@ -35,9 +35,12 @@ def icollect(file_paths):
     """
     Evaluate globs in file paths and return all matching files.
 
-    :param file_paths:  list of file paths that can include globs
+    :param file_paths:  file path or list of such that can include globs
     :return:            iterator that yields paths of all matching files
     """
+    if isinstance(file_paths, str):
+        file_paths = [file_paths]
+
     for file_path in file_paths:
         for match in iglob(file_path):
             yield match
@@ -47,7 +50,7 @@ def collect_files(file_paths):
     """
     Evaluate globs in file paths and return all matching files
 
-    :param file_paths: list of file paths that can include globs
+    :param file_paths: file path or list of such that can include globs
     :return:           list of paths of all matching files
     """
     return list(filter(os.path.isfile, icollect(file_paths)))
@@ -57,7 +60,7 @@ def collect_dirs(dir_paths):
     """
     Evaluate globs in directory paths and return all matching directories
 
-    :param dir_paths: list of file paths that can include globs
+    :param dir_paths: file path or list of such that can include globs
     :return:          list of paths of all matching directories
     """
     return list(filter(os.path.isdir, icollect(dir_paths)))
@@ -68,7 +71,7 @@ def icollect_bears(bear_dirs, bear_names, kinds, log_printer):
     """
     Collect all bears from bear directories that have a matching kind.
 
-    :param bear_dirs:   directories that can contain bears
+    :param bear_dirs:   directory name or list of such that can contain bears
     :param bear_names:  names of bears
     :param kinds:       list of bear kinds to be collected
     :param log_printer: log_printer to handle logging
@@ -93,7 +96,7 @@ def collect_bears(bear_dirs, bear_names, kinds, log_printer):
     """
     Collect all bears from bear directories that have a matching kind.
 
-    :param bear_dirs:   directories that can contain bears
+    :param bear_dirs:   directory name or list of such that can contain bears
     :param bear_names:  names of bears
     :param kinds: list  of bear kinds to be collected
     :param log_printer: log_printer to handle logging


Test patch:
diff --git a/coalib/tests/collecting/CollectorsTest.py b/coalib/tests/collecting/CollectorsTest.py
--- a/coalib/tests/collecting/CollectorsTest.py
+++ b/coalib/tests/collecting/CollectorsTest.py
@@ -32,6 +32,17 @@ def test_file_collection(self):
                              "py_files",
                              "file2.py"))])
 
+    def test_file_string_collection(self):
+        self.assertEqual(collect_files(os.path.join(self.collectors_test_dir,
+                                                    "others",
+                                                    "*",
+                                                    "*2.py")),
+                         [os.path.normcase(os.path.join(
+                             self.collectors_test_dir,
+                             "others",
+                             "py_files",
+                             "file2.py"))])
+
 
 class CollectDirsTest(unittest.TestCase):
     def setUp(self):
@@ -64,6 +75,25 @@ def test_dir_collection(self):
                                               "py_files")),
                 os.path.normcase(self.collectors_test_dir+os.sep)]))
 
+    def test_dir_string_collection(self):
+        self.assertEqual(
+            sorted(collect_dirs(os.path.join(self.collectors_test_dir,
+                                             "**"))),
+            sorted([os.path.normcase(os.path.join(
+                self.collectors_test_dir, "bears")),
+                os.path.normcase(os.path.join(self.collectors_test_dir,
+                                              "bears",
+                                              "__pycache__")),
+                os.path.normcase(os.path.join(self.collectors_test_dir,
+                                              "others")),
+                os.path.normcase(os.path.join(self.collectors_test_dir,
+                                              "others",
+                                              "c_files")),
+                os.path.normcase(os.path.join(self.collectors_test_dir,
+                                              "others",
+                                              "py_files")),
+                os.path.normcase(self.collectors_test_dir+os.sep)]))
+
 
 class CollectBearsTest(unittest.TestCase):
     def setUp(self):
@@ -89,6 +119,13 @@ def test_simple_single(self):
             ["kind"],
             self.log_printer)), 1)
 
+    def test_string_single(self):
+        self.assertEqual(len(collect_bears(
+            os.path.join(self.collectors_test_dir, "bears"),
+            ["bear1"],
+            ["kind"],
+            self.log_printer)), 1)
+
     def test_reference_single(self):
         self.assertEqual(len(collect_bears(
             [os.path.join(self.collectors_test_dir, "bears")],