# -*- coding: utf-8 -*-

# TODO: http://docs.python.org/library/glob.html

try:
    import sys
    import os
    import yaml
except ImportError, error:
    print('Import error: %s' % error)
    sys.exit(1)


class Database:

    def __init__(self, yamldir):
        self.issues = self.generate(yamldir)

    """Reads YAML files and generates a data dictionary of the contents"""
    def gen_yamlfile_locations(self, yamldir):
        """File handle generator for YAML-files"""
        if os.path.islink(yamldir):
            sys.exit('Location for YAML-files can not be a symlink: %s' % yamldir)
        if not os.path.isdir(yamldir):
            sys.exit('Location for YAML-files is not a directory: %s' % yamldir)
        for filename in os.listdir(yamldir):
            if not filename.endswith('.yaml'):
                continue
            if os.path.islink(yamldir + filename):
                continue
            if not os.path.isfile(yamldir + filename):
                continue 
            file = open(yamldir + filename, 'r')
            yield file

    def generate(self, yamldir):
        """Generates data dictionary of definitions from YAML files"""
        data = {}
        for file in self.gen_yamlfile_locations(yamldir):
            try:
                result = yaml.safe_load(file.read())
                data = dict(data.items() + result.items())
            except AttributeError, e:  # empty file
                print('No data found inside: %s' % file)
            except yaml.scanner.ScannerError, e:  # syntax error
                print('Error while loading YAML-file: %s' % file)
        return data

    def locations(self, application, with_lists=True):
        """Returns list of locations by appname."""
        locations = []
        for (appname, issues) in self.issues.iteritems():
            if not appname == application:
                continue
            for issue in issues.iteritems():
                if with_lists is True:
                    locations.append(issue[1]['location'])
                else:
                    if type(issue[1]['location']) == str:
                        locations.append(issue[1]['location'])
                    if type(issue[1]['location']) == list:
                        i = 0
                        while i < len(issue[1]['location']):
                            locations.append(issue[1]['location'][i])
                            i += 1
        return locations

@@ -1772,25 +1772,23 @@
 for 
-(appname
+issue_id
 , issue
-s)
  in 
@@ -1802,121 +1802,21 @@
 sues
-.iteritems():%0A            if not appname == application:%0A                continue%0A            for issue in issues
+%5Bapplication%5D
 .ite
@@ -1837,20 +1837,16 @@
         
-   
- 
 if with_
@@ -1860,20 +1860,16 @@
 s True:%0A
-    
         
@@ -1891,35 +1891,32 @@
 ns.append(issue%5B
-1%5D%5B
 'location'%5D)%0A   
@@ -1928,22 +1928,14 @@
     
-   
- 
 else:%0A
-    
     
@@ -1952,35 +1952,32 @@
   if type(issue%5B
-1%5D%5B
 'location'%5D) == 
@@ -1993,36 +1993,32 @@
                 
-   
- 
 locations.append
@@ -2016,35 +2016,32 @@
 ns.append(issue%5B
-1%5D%5B
 'location'%5D)%0A   
@@ -2045,36 +2045,32 @@
                 
-  
-  
 if type(issue%5B1%5D
@@ -2063,27 +2063,24 @@
  type(issue%5B
-1%5D%5B
 'location'%5D)
@@ -2113,22 +2113,14 @@
     
-    
 i = 0%0A
-    
     
@@ -2151,27 +2151,24 @@
 %3C len(issue%5B
-1%5D%5B
 'location'%5D)
@@ -2185,36 +2185,32 @@
                 
-    
 locations.append
@@ -2220,11 +2220,8 @@
 sue%5B
-1%5D%5B
 'loc
@@ -2232,20 +2232,16 @@
 n'%5D%5Bi%5D)%0A
-    
         

Drop loop from Database.locations