lauromoura pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=bb3f741990dee47292c23936c05b80326ea49e45

commit bb3f741990dee47292c23936c05b80326ea49e45
Author: Lauro Moura <[email protected]>
Date:   Tue Nov 26 11:56:41 2019 -0300

    pyolian: Add helper functions for simple scripts
    
    Summary:
    These two functions encapsulate some code that commonly is repeated
    through pyolian scripts.
    
    With these, a minimal example (provided `src/scripts/pyolian` is in
    the PYTHONPATH) could be done with:
    
    ```
    import os
    
    from pyolian import eolian
    
    SCAN_FOLDER = os.path.join(eolian.in_tree_src_dir(), 'src', 'lib')
    eolian_db = eolian.parse_folders(SCAN_FOLDER)
    
    for cls in eolian_db.classes:
        print(cls)
    ```
    
    Reviewers: segfaultxavi, DaveMDS, felipealmeida
    
    Reviewed By: felipealmeida
    
    Subscribers: cedric, #reviewers, #committers, felipealmeida, brunobelo
    
    Tags: #efl
    
    Differential Revision: https://phab.enlightenment.org/D10589
---
 src/scripts/pyolian/eolian.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/src/scripts/pyolian/eolian.py b/src/scripts/pyolian/eolian.py
index eababfadba..e7681208c7 100644
--- a/src/scripts/pyolian/eolian.py
+++ b/src/scripts/pyolian/eolian.py
@@ -23,6 +23,7 @@ a way that this folder will be available on PYTHON_PATH, fe:
   from pyolian.generator import Template
 
 """
+import os
 from enum import IntEnum
 import atexit
 from ctypes import cast, byref, c_char_p, c_void_p, c_int
@@ -520,6 +521,44 @@ class Eolian_State(Eolian_Unit):
         return bool(lib.eolian_state_check(self))
 
 
+#  Helper functions  ###################################################
+
+def parse_folders(folders):
+    """Loads a new database scanning the given folders.
+
+    For example:
+
+    SCAN_FOLDER = os.path.join(eolian.in_tree_src_dir(), 'src', 'lib')
+    eolian_db = eolian.parse_folders(SCAN_FOLDER)
+    """
+
+    db = Eolian_State()
+    if not isinstance(db, Eolian_State):
+        raise (RuntimeError('Eolian, failed to create Eolian state'))
+
+    if isinstance(folders, str):
+        folders = [folders]
+
+    for folder in folders:
+        # eolian source tree scan
+        if not db.directory_add(folder):
+            raise (RuntimeError('Eolian, failed to scan source dirsectory'))
+
+    # Parse all known eo files
+    if not db.all_eot_files_parse():
+        raise (RuntimeError('Eolian, failed to parse all EOT files'))
+
+    if not db.all_eo_files_parse():
+        raise (RuntimeError('Eolian, failed to parse all EO files'))
+
+    return db
+
+def in_tree_src_dir():
+    """Returns the root folder of this script's source tree"""
+    script_path = os.path.dirname(os.path.realpath(__file__))
+    return os.path.abspath(os.path.join(script_path, '..', '..', '..'))
+
+
 #  Namespace Utility Class  ###################################################
 
 class Namespace(object):

-- 


Reply via email to