Changeset 348

Show
Ignore:
Timestamp:
Mon Oct 22 18:43:05 2007
Author:
Brian
Message:

Set eol-style for object factory scripts.

Files:

Legend:

Unmodified
Added
Removed
Modified
  • scripts/trunk/Object API/Test Object Factory.py

    r324 r348  
    1   # Test Object Factory  
    2   # Copyright 2007 by Brian C. Christensen  
    3    
    4   """  
    5   define object model for manipulating ganttpv data  
    6   """  
    7    
    8   #    This file is part of GanttPV.  
    9   #  
    10   #    GanttPV is free software; you can redistribute it and/or modify  
    11   #    it under the terms of the GNU General Public License as published by  
    12   #    the Free Software Foundation; either version 2 of the License, or  
    13   #    (at your option) any later version.  
    14   #  
    15   #    GanttPV is distributed in the hope that it will be useful,  
    16   #    but WITHOUT ANY WARRANTY; without even the implied warranty of  
    17   #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
    18   #    GNU General Public License for more details.  
    19   #  
    20   #    You should have received a copy of the GNU General Public License  
    21   #    along with GanttPV; if not, write to the Free Software  
    22   #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    23    
    24   # 070820 - first tests  
    25    
    26   def test1():  
    27       db = Data._dbDatabase(Data.Database)  # will get objects from this database  
    28       project1 = db.GetObject('Project', 1)  
    29       print project1.Name  
    30       reports = project1.GetList('Report')  
    31       for x in reports:  
    32           print x.Name, x.Get('Project')  
    33    
    34       project2 = db.GetObject('Project', 2)  
    35       print project2.name  
    36       tasks = project2.GetList('Task')  
    37       for x in tasks:  
    38           print x.Name  
    39           assignments = x.GetList('Assignment')  
    40           for y in assignments:  
    41               print y.Get('Resource').Name  
    42           print 'end of task'  
    43    
    44   test1()  
    45    
    46    
      1 # Test Object Factory  
      2 # Copyright 2007 by Brian C. Christensen  
      3  
      4 """  
      5 define object model for manipulating ganttpv data  
      6 """  
      7  
      8 #    This file is part of GanttPV.  
      9 #  
      10 #    GanttPV is free software; you can redistribute it and/or modify  
      11 #    it under the terms of the GNU General Public License as published by  
      12 #    the Free Software Foundation; either version 2 of the License, or  
      13 #    (at your option) any later version.  
      14 #  
      15 #    GanttPV is distributed in the hope that it will be useful,  
      16 #    but WITHOUT ANY WARRANTY; without even the implied warranty of  
      17 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
      18 #    GNU General Public License for more details.  
      19 #  
      20 #    You should have received a copy of the GNU General Public License  
      21 #    along with GanttPV; if not, write to the Free Software  
      22 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
      23  
      24 # 070820 - first tests  
      25  
      26 def test1():  
      27     db = Data._dbDatabase(Data.Database)  # will get objects from this database  
      28     project1 = db.GetObject('Project', 1)  
      29     print project1.Name  
      30     reports = project1.GetList('Report')  
      31     for x in reports:  
      32         print x.Name, x.Get('Project')  
      33  
      34     project2 = db.GetObject('Project', 2)  
      35     print project2.name  
      36     tasks = project2.GetList('Task')  
      37     for x in tasks:  
      38         print x.Name  
      39         assignments = x.GetList('Assignment')  
      40         for y in assignments:  
      41             print y.Get('Resource').Name  
      42         print 'end of task'  
      43  
      44 test1()  
      45  
      46  
  • scripts/trunk/Object API/Install Object Factory.py

    r343 r348  
    1   # Install Object Factory  
    2   # Copyright 2007 by Brian C. Christensen  
    3    
    4   """  
    5   define object model for manipulating ganttpv data  
    6   """  
    7    
    8   #    This file is part of GanttPV.  
    9   #  
    10   #    GanttPV is free software; you can redistribute it and/or modify  
    11   #    it under the terms of the GNU General Public License as published by  
    12   #    the Free Software Foundation; either version 2 of the License, or  
    13   #    (at your option) any later version.  
    14   #  
    15   #    GanttPV is distributed in the hope that it will be useful,  
    16   #    but WITHOUT ANY WARRANTY; without even the implied warranty of  
    17   #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
    18   #    GNU General Public License for more details.  
    19   #  
    20   #    You should have received a copy of the GNU General Public License  
    21   #    along with GanttPV; if not, write to the Free Software  
    22   #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    23    
    24   # 070820 - first draft based on prior version  
    25    
    26   ##"""  
    27   ##Each object contains only the table name and id of the corresponding database record.  
    28   ##Accessing attributes retrieves data from the database.  
    29   ##  
    30   ##Using a factory to return objects  
    31   ##  
    32   ## Limitations  
    33   ## - update will not work on non-current databases  
    34   ## - creation of new objects will not work on non-current databases  
    35   ## - GetList will not work on non-current databases  
    36   ##  
    37   ## - will not follow non-standards foreign keys (for example, Report's FirstColumn  
    38   ##  
    39   ##"""  
    40    
    41   debug = 1  
    42    
    43   class dbObject:  
    44       def __init__(self, db, object_type, object_id):  
    45           ''' object_type = table name; object_id = row id '''  
    46           self.__dict__['db'] = db  
    47           self.__dict__['Table'] = object_type  
    48           self.__dict__['ID'] = object_id  
    49   ##        self.Table = object_type  # avoiding use of __setattr__  
    50   ##        self.ID = object_id  
    51    
    52       def __str__(self):  
    53           return 'Object Type %s with ID %d' % (self.Table, self.ID)  
    54            
    55       def __repr__(self):  
    56           return 'Object Type %s with ID %d' % (self.Table, self.ID)  
    57            
    58       def __getattr__(self, name):  
    59   # it is impossible to return values for 'Get', 'GetList', or 'Valid'  
    60   # the program only comes here if it can't find the real attribute  
    61   #     this applies to db, Table, and ID  
    62    
    63           # edit on every access or on creation?  
    64           if not self.Table in self.db.Database:  
    65               return None  
    66           rec = self.db.Database[self.Table].get(self.ID)  
    67           if not rec:  
    68               return None  
    69    
    70           return rec.get(name)  # any column name  
    71    
    72       def __setattr__(self, name, value):  
    73           # edit on every access or on creation?  
    74           if not self.Table in self.db.Database:  
    75               return  
    76           rec = self.db.Database[self.Table].get(self.ID)  
    77           if not rec:  
    78               return  
    79    
    80           # apply update  
    81           if name in ('Table', 'ID'):  
    82               return  # silently ignore attempts to change these?  
    83           change = {'Table': self.Table, 'ID': self.ID, name: value }  
    84           Data.Update(change)  # can only update the active database -- IMPORTANT  
    85           return  
    86    
    87       # ---- these are experimental ----  
    88       def _SetInShell(self, tag, value):  
    89           '''Don't update the database; save value in shell object'''  
    90           self.__dict__[tag] = value  
    91    
    92       def _SetToSubclass(self, subclass):  
    93           '''Change the class of the object to a subtype of the class'''  
    94           # self.__dict__['__class__'] = subclass  # how to make this work?  
    95           self.__class__ = subclass  # or this??  
    96           print self.__class__  
    97    
    98       # ----- end -------  
    99        
    100       def Valid(self):  
    101           try:  
    102               rec = self.db.Database[self.Table].get(self.ID)  
    103               return (rec and rec.get('zzStatus') != 'deleted')  
    104           except:  
    105               return None  
    106    
    107       def Get(self, name=None):  
    108           '''  
    109   returns an object that this one points to  
    110   returns object pointed to by name + ID  
    111           '''  
    112           if self.Table == 'ReportRow' and name == 'Target':  
    113               return self.db.GetObject(self.TableName, self.TableID)  
    114    
    115           # edit on every access or on creation?  
    116           if not self.Table in self.db.Database:  
    117               return None  
    118           rec = self.db.Database[self.Table].get(self.ID)  
    119           if not rec:  
    120               return None  
    121    
    122           target_id = rec.get(name + 'ID')  # FK value  
    123           if not name:  
    124               return None  
    125           return self.db.GetObject(name, target_id)  # object  
    126    
    127    
    128       def GetList(self, table_name, attribute=None):  
    129           '''  
    130   returns a list of "table_name type" objects that point to this one  
    131   list_of_assignment_objects = ThisTask.Find('Assignment', 'Prerequisite')  
    132   Returns a list of all assignment objects that point to the original task object  
    133           '''  
    134           if attribute:  
    135               fk = attribute  
    136           else:  
    137               fk = self.Table  
    138           table_rows = self.db.Database.get(table_name) or {}  
    139           search = { fk + 'ID': self.ID }  
    140           result = Data.SearchByColumn(table_rows, search)  # only works w/ current db -- IMPORTANT  
    141           # if debug: print result  
    142           return [ self.db.GetObject(table_name, x['ID']) for x in result.itervalues() if x.get('zzStatus') != 'deleted']  
    143    
    144    
    145   ##class db:  
    146   ##    def __init__(self, id=0):  
    147   ##        self.id = id  
    148    
    149   ##    def Find(self, table_name, attribute=None):  
    150   ##        '''  
    151   ##list_of_assignment_objects = ThisTask.Find('Assignment', 'Prerequisite')  
    152   ##Returns a list of all assignment objects that point to the original task object  
    153   ##        '''  
    154   ##        if attribute:  
    155   ##            fk = attribute  
    156   ##        else:  
    157   ##            fk = self.Table  
    158   ##        table_rows = Data.Database.get(table_name) or {}  
    159   ##        search = { fk + 'ID': self.ID }  
    160   ##        result = Data.SearchByColumn(table_rows, search)  
    161   ##        return [ Data.GetObject(table_name, x['ID']) for x in result if x.get('zzStatus') != 'deleted']  
    162    
    163   class dbDatabase:  
    164       def __init__(self, database):  
    165           ''' database '''  
    166           self.Database = database  
    167           self._ObjectXref = {}  # key = (table, id)  
    168    
    169       def GetObject(self, object_type, object_id=None):  
    170           if object_type in ('Table', 'ID'):  
    171               return None  
    172           # convert known aliases  
    173           da = self.Database['TableAlias'][1]  
    174           if object_type in da:  
    175               object_type = da[object_type]  
    176           # add new objects to database  
    177           if not object_id:  
    178               update = {'Table': object_type}  
    179               object_id = Data.Update(update)['ID']  # only works w/ current db -- IMPORTANT  
    180           # create object, save a copy for possible future use  
    181           key = (object_type, object_id)  
    182           if key in self._ObjectXref:  
    183               return self._ObjectXref[key]  
    184           else:  
    185               ob = Data._dbObject(self, object_type, object_id)  
    186               self._ObjectXref[key] = ob  
    187               return ob  
    188    
    189       def GetList(self, table_name):  
    190           '''  
    191   returns a list of "table_name type"  
    192           '''  
    193           table = self.Database.get(table_name) or {}  
    194           return [ self.GetObject(table_name, x['ID']) for x in table.itervalues() if x.get('zzStatus') != 'deleted']  
    195    
    196               # value = object  
    197   Data._dbObject = dbObject  
    198   Data._dbDatabase = dbDatabase  
    199    
    200   ##class Task(dbObject):  
    201   ##    def Prerequisites(self):  
    202   ##        tlist = []  
    203   ##        for k, v in Data.Database['Prerequisite']:  
    204   ##            if v.get('zzStatus') == 'deleted': continue  
    205   ##            if v.get('TaskID') == self.id:  
    206   ##                tlist.append(Task(v.get('PrerequisiteID')))  
    207   ##        return tlist  
    208   ##  
    209   ##    def Successors(self):  
    210   ##        tlist = []  
    211   ##        for k, v in Data.Database['Prerequisite']:  
    212   ##            if v.get('zzStatus') == 'deleted': continue  
    213   ##            if v.get('PrerequisiteID') == self.id:  
    214   ##                tlist.append(Task(v.get('TaskID')))  
    215   ##        return tlist  
    216   ##  
    217   ##    def Resources(self):  
    218   ##        rlist = []  
    219   ##        for k, v in Data.Database['Assignment']:  
    220   ##            if v.get('zzStatus') == 'deleted': continue  
    221   ##            if v.get('TaskID') == self.id:  
    222   ##                rlist.append(Resource(v.get('ResourceID')))  
    223   ##        return rlist  
    224   ##  
    225   ##    def __getattr__(self, name):  
    226   ##        rec = Data.Database['Task'].get(self.id)  
    227   ##        if name == "Project":  
    228   ##            return Project(rec.get('ProjectID'))  
    229   ##        else:  
    230   ##            return rec.get(name)  
    231   ##  
    232    
    233   #    def hint(s):  
    234   #        dlg = wx.MessageDialog(None, s, 'Hint')  
    235   #        if dlg.ShowModal() == wx.ID_OK:  
    236   #            pass  
    237   #        dlg.Destroy()  
    238    
    239   #Do()  
    240    
    241    
      1 # Install Object Factory  
      2 # Copyright 2007 by Brian C. Christensen  
      3  
      4 """  
      5 define object model for manipulating ganttpv data  
      6 """  
      7  
      8 #    This file is part of GanttPV.  
      9 #  
      10 #    GanttPV is free software; you can redistribute it and/or modify  
      11 #    it under the terms of the GNU General Public License as published by  
      12 #    the Free Software Foundation; either version 2 of the License, or  
      13 #    (at your option) any later version.  
      14 #  
      15 #    GanttPV is distributed in the hope that it will be useful,  
      16 #    but WITHOUT ANY WARRANTY; without even the implied warranty of  
      17 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
      18 #    GNU General Public License for more details.  
      19 #  
      20 #    You should have received a copy of the GNU General Public License  
      21 #    along with GanttPV; if not, write to the Free Software  
      22 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
      23  
      24 # 070820 - first draft based on prior version  
      25  
      26 ##"""  
      27 ##Each object contains only the table name and id of the corresponding database record.  
      28 ##Accessing attributes retrieves data from the database.  
      29 ##  
      30 ##Using a factory to return objects  
      31 ##  
      32 ## Limitations  
      33 ## - update will not work on non-current databases  
      34 ## - creation of new objects will not work on non-current databases  
      35 ## - GetList will not work on non-current databases  
      36 ##  
      37 ## - will not follow non-standards foreign keys (for example, Report's FirstColumn  
      38 ##  
      39 ##"""  
      40  
      41 debug = 1  
      42  
      43 class dbObject:  
      44     def __init__(self, db, object_type, object_id):  
      45         ''' object_type = table name; object_id = row id '''  
      46         self.__dict__['db'] = db  
      47         self.__dict__['Table'] = object_type  
      48         self.__dict__['ID'] = object_id  
      49 ##        self.Table = object_type  # avoiding use of __setattr__  
      50 ##        self.ID = object_id  
      51  
      52     def __str__(self):  
      53         return 'Object Type %s with ID %d' % (self.Table, self.ID)  
      54          
      55     def __repr__(self):  
      56         return 'Object Type %s with ID %d' % (self.Table, self.ID)  
      57          
      58     def __getattr__(self, name):  
      59 # it is impossible to return values for 'Get', 'GetList', or 'Valid'  
      60 # the program only comes here if it can't find the real attribute  
      61 #     this applies to db, Table, and ID  
      62  
      63         # edit on every access or on creation?  
      64         if not self.Table in self.db.Database:  
      65             return None  
      66         rec = self.db.Database[self.Table].get(self.ID)  
      67         if not rec:  
      68             return None  
      69  
      70         return rec.get(name)  # any column name  
      71  
      72     def __setattr__(self, name, value):  
      73         # edit on every access or on creation?  
      74         if not self.Table in self.db.Database:  
      75             return  
      76         rec = self.db.Database[self.Table].get(self.ID)  
      77         if not rec:  
      78             return  
      79  
      80         # apply update  
      81         if name in ('Table', 'ID'):  
      82             return  # silently ignore attempts to change these?  
      83         change = {'Table': self.Table, 'ID': self.ID, name: value }  
      84         Data.Update(change)  # can only update the active database -- IMPORTANT  
      85         return  
      86  
      87     # ---- these are experimental ----  
      88     def _SetInShell(self, tag, value):  
      89         '''Don't update the database; save value in shell object'''  
      90         self.__dict__[tag] = value  
      91  
      92     def _SetToSubclass(self, subclass):  
      93         '''Change the class of the object to a subtype of the class'''  
      94         # self.__dict__['__class__'] = subclass  # how to make this work?  
      95         self.__class__ = subclass  # or this??  
      96         print self.__class__  
      97  
      98     # ----- end -------  
      99      
      100     def Valid(self):  
      101         try:  
      102             rec = self.db.Database[self.Table].get(self.ID)  
      103             return (rec and rec.get('zzStatus') != 'deleted')  
      104         except:  
      105             return None  
      106  
      107     def Get(self, name=None):  
      108         '''  
      109 returns an object that this one points to  
      110 returns object pointed to by name + ID  
      111         '''  
      112         if self.Table == 'ReportRow' and name == 'Target':  
      113             return self.db.GetObject(self.TableName, self.TableID)  
      114  
      115         # edit on every access or on creation?  
      116         if not self.Table in self.db.Database:  
      117             return None  
      118         rec = self.db.Database[self.Table].get(self.ID)  
      119         if not rec:  
      120             return None  
      121  
      122         target_id = rec.get(name + 'ID')  # FK value  
      123         if not name:  
      124             return None  
      125         return self.db.GetObject(name, target_id)  # object  
      126  
      127  
      128     def GetList(self, table_name, attribute=None):  
      129         '''  
      130 returns a list of "table_name type" objects that point to this one  
      131 list_of_assignment_objects = ThisTask.Find('Assignment', 'Prerequisite')  
      132 Returns a list of all assignment objects that point to the original task object  
      133         '''  
      134         if attribute:  
      135             fk = attribute  
      136         else:  
      137             fk = self.Table  
      138         table_rows = self.db.Database.get(table_name) or {}  
      139         search = { fk + 'ID': self.ID }  
      140         result = Data.SearchByColumn(table_rows, search)  # only works w/ current db -- IMPORTANT  
      141         # if debug: print result  
      142         return [ self.db.GetObject(table_name, x['ID']) for x in result.itervalues() if x.get('zzStatus') != 'deleted']  
      143  
      144  
      145 ##class db:  
      146 ##    def __init__(self, id=0):  
      147 ##        self.id = id  
      148  
      149 ##    def Find(self, table_name, attribute=None):  
      150 ##        '''  
      151 ##list_of_assignment_objects = ThisTask.Find('Assignment', 'Prerequisite')  
      152 ##Returns a list of all assignment objects that point to the original task object  
      153 ##        '''  
      154 ##        if attribute:  
      155 ##            fk = attribute  
      156 ##        else:  
      157 ##            fk = self.Table  
      158 ##        table_rows = Data.Database.get(table_name) or {}  
      159 ##        search = { fk + 'ID': self.ID }  
      160 ##        result = Data.SearchByColumn(table_rows, search)  
      161 ##        return [ Data.GetObject(table_name, x['ID']) for x in result if x.get('zzStatus') != 'deleted']  
      162  
      163 class dbDatabase:  
      164     def __init__(self, database):  
      165         ''' database '''  
      166         self.Database = database  
      167         self._ObjectXref = {}  # key = (table, id)  
      168  
      169     def GetObject(self, object_type, object_id=None):  
      170         if object_type in ('Table', 'ID'):  
      171             return None  
      172         # convert known aliases  
      173         da = self.Database['TableAlias'][1]  
      174         if object_type in da:  
      175             object_type = da[object_type]  
      176         # add new objects to database  
      177         if not object_id:  
      178             update = {'Table': object_type}  
      179             object_id = Data.Update(update)['ID']  # only works w/ current db -- IMPORTANT  
      180         # create object, save a copy for possible future use  
      181         key = (object_type, object_id)  
      182         if key in self._ObjectXref:  
      183             return self._ObjectXref[key]  
      184         else:  
      185             ob = Data._dbObject(self, object_type, object_id)  
      186             self._ObjectXref[key] = ob  
      187             return ob  
      188  
      189     def GetList(self, table_name):  
      190         '''  
      191 returns a list of "table_name type"  
      192         '''  
      193         table = self.Database.get(table_name) or {}  
      194         return [ self.GetObject(table_name, x['ID']) for x in table.itervalues() if x.get('zzStatus') != 'deleted']  
      195  
      196             # value = object  
      197 Data._dbObject = dbObject  
      198 Data._dbDatabase = dbDatabase  
      199  
      200 ##class Task(dbObject):  
      201 ##    def Prerequisites(self):  
      202 ##        tlist = []  
      203 ##        for k, v in Data.Database['Prerequisite']:  
      204 ##            if v.get('zzStatus') == 'deleted': continue  
      205 ##            if v.get('TaskID') == self.id:  
      206 ##                tlist.append(Task(v.get('PrerequisiteID')))  
      207 ##        return tlist  
      208 ##  
      209 ##    def Successors(self):  
      210 ##        tlist = []  
      211 ##        for k, v in Data.Database['Prerequisite']:  
      212 ##            if v.get('zzStatus') == 'deleted': continue  
      213 ##            if v.get('PrerequisiteID') == self.id:  
      214 ##                tlist.append(Task(v.get('TaskID')))  
      215 ##        return tlist  
      216 ##  
      217 ##    def Resources(self):  
      218 ##        rlist = []  
      219 ##        for k, v in Data.Database['Assignment']:  
      220 ##            if v.get('zzStatus') == 'deleted': continue  
      221 ##            if v.get('TaskID') == self.id:  
      222 ##                rlist.append(Resource(v.get('ResourceID')))  
      223 ##        return rlist  
      224 ##  
      225 ##    def __getattr__(self, name):  
      226 ##        rec = Data.Database['Task'].get(self.id)  
      227 ##        if name == "Project":  
      228 ##            return Project(rec.get('ProjectID'))  
      229 ##        else:  
      230 ##            return rec.get(name)  
      231 ##  
      232  
      233 #    def hint(s):  
      234 #        dlg = wx.MessageDialog(None, s, 'Hint')  
      235 #        if dlg.ShowModal() == wx.ID_OK:  
      236 #            pass  
      237 #        dlg.Destroy()  
      238  
      239 #Do()  
      240  
      241