
import glob
import os

import ogre_parse.reader

# collect all files with an extension matching on of 'extensions'
# returns (materials, programs, compositors)
def script_search(aFolder):
    mats = []
    progs = []
    comps = []

    for root, dirs, files in os.walk(aFolder):
        for file in files:
            if file.endswith('.material'):
                mats.append( os.path.join(root, file))
            elif file.endswith('.program'):
                progs.append( os.path.join(root, file))
            elif file.endswith('.compositor'):
                comps.append( os.path.join(root, file))

    return (mats, progs, comps)


def parse_script(aPath):

    res = None
    len_elems = 0
    scriptReader = ogre_parse.reader.ReadScript()
    with open(aPath, 'r') as f:
        script = f.read()
        try:
            res = scriptReader.parseString(script)
        except Exception as e:
            print('--an error occurred reading, with message:\n%s' % (aPath, str(e)))

    return res


def show_stats(aFolder):
    (mats, progs, comps) = script_search(aFolder)
    print('ogre statistics, under folder, %s, \nreading files: [%s] material, [%s] program, [%s] compositor' % (search_folder, len(mats), len(progs), len(comps)))

    len_mats = 0
    for m in mats:
        parsedres = parse_script(m)
        script = parsedres[0]
        len_mats += len(script.materials)

        with open(m, 'r') as f:
            ftext = f.read()
            len_elems = ftext.count('material ')
            if len_elems != len(script.materials):
                print('!!! script file likely contains [%s] materials, but parser found [%s], in script: %s' % (len_elems, len(script.materials), m))
            else:
                print('parsed [%s] materials from file: %s' % (len(script.materials), m))

    len_progs = 0
    for p in progs:
        parsedres = parse_script(p)
        len_progs += len(parsedres[0].shaders)

    len_comps = 0
    for c in comps:
        parsedres = parse_script(c)
        len_comps += len(parsedres[0].compositors)

    print(20*'-' + '\n[%s] materials, [%s] shader definitions, [%s] compositors' % (len_mats, len_progs, len_comps))


if __name__ == '__main__':
    # search_folder = 'D:\\Documents\\STI\\code\\projects\\SystemsTech\\SDK_various'
    # # search_folder = r'C:\STISIM3'
    # show_stats(search_folder)

    fullpath = r'D:\Documents\STI\code\projects\SystemsTech\SDK_various\data\Examples\Clouds\clouds.material'
    parsedres = parse_script(fullpath)
    print(parsedres.dump())
    # script = parsedres[0]
    # print(str(script))


@@ -946,16 +946,20 @@
 reading,
+%5Cn%25s
  with me
@@ -1318,16 +1318,64 @@
 ript(m)%0A
+        if not parsedres:%0A            continue%0A%0A
         
@@ -2256,18 +2256,16 @@
 __':%0A   
- #
  search_
@@ -2341,18 +2341,16 @@
 s'%0A    #
- #
  search_
@@ -2375,18 +2375,16 @@
 IM3'%0A   
- #
  show_st
@@ -2406,16 +2406,18 @@
 er)%0A%0A   
+ #
  fullpat
@@ -2481,34 +2481,72 @@
 ous%5C
-data%5CExamples%5CClouds%5Ccloud
+install%5CSystemsTech_SDK_rev1445%5Cdata%5CExamples%5CParticles%5Cparticle
 s.ma
@@ -2556,16 +2556,18 @@
 ial'%0A   
+ #
  parsedr
@@ -2593,24 +2593,28 @@
 ullpath)%0A   
+ # #
  print(parse
@@ -2631,16 +2631,59 @@
 ))%0A    #
+ print(len(parsedres%5B0%5D.materials))%0A    # #
  script 
@@ -2698,24 +2698,26 @@
 res%5B0%5D%0A    #
+ #
  print(str(s

fix for no objects found in file.