Changeset 3609

Show
Ignore:
Timestamp:
10/10/08 22:13:10 (3 months ago)
Author:
dmeyer
Message:

refactoring

Location:
trunk/beacon/src
Files:
11 modified

Legend:

Unmodified
Added
Removed
  • trunk/beacon/src/db.py

    r3604 r3609  
    7171        # url is stored in the name (remote items in directory) 
    7272        return Item(dbid, data['name'], data, parent, parent._beacon_media) 
    73  
    7473    # generate url based on name and parent url 
    7574    url = parent.url 
     
    8887    Create a file or directory 
    8988    """ 
    90     return File(data, parent, overlay, isdir) 
     89    if isinstance(data, str): 
     90        # fake item, there is no database entry 
     91        id = None 
     92        filename = parent.filename + data 
     93        data = { 'name': data } 
     94        if parent and parent._beacon_id: 
     95            data['parent_type'], data['parent_id'] = parent._beacon_id 
     96        media = parent._beacon_media 
     97        if isdir: 
     98            filename += '/' 
     99    elif isinstance(parent, File): 
     100        # db data 
     101        id = (data['type'], data['id']) 
     102        media = parent._beacon_media 
     103        filename = parent.filename + data['name'] 
     104        if isdir: 
     105            filename += '/' 
     106    elif not data['name']: 
     107        # root directory 
     108        id = (data['type'], data['id']) 
     109        media = parent 
     110        parent = None 
     111        filename = media.mountpoint 
     112    else: 
     113        raise ValueError('unable to create File item from %s', data) 
     114    return File(id, filename, data, parent, media, overlay, isdir) 
    91115 
    92116 
     
    104128    a directory, make it an Item, not a File. 
    105129    """ 
    106     if (data.get('name').find('://') > 0) or (parent and not parent.isdir()): 
     130    if (data.get('name').find('://') > 0) or (parent and not parent.isdir): 
    107131        return create_item(data, parent) 
    108132    return create_file(data, parent, overlay, isdir) 
     
    278302                # file deleted 
    279303                i = items[pos] 
    280                 if not i.isdir() and not i.isfile(): 
     304                if not i.isdir and not i.isfile(): 
    281305                    # A remote URL in the directory 
    282306                    pos += 1 
     
    300324            # deleted files at the end 
    301325            for i in items[pos+1-len(items):]: 
    302                 if not i.isdir() and not i.isfile(): 
     326                if not i.isdir and not i.isfile(): 
    303327                    # A remote URL in the directory 
    304328                    continue 
  • trunk/beacon/src/file.py

    r3475 r3609  
    6666    """ 
    6767 
    68     def __init__(self, data, parent, overlay=False, isdir=False): 
    69         if isinstance(data, str): 
    70             # fake item, there is no database entry 
    71             id = None 
    72             filename = parent.filename + data 
    73             data = { 'name': data } 
    74             if parent and parent._beacon_id: 
    75                 data['parent_type'], data['parent_id'] = parent._beacon_id 
    76             media = parent._beacon_media 
    77             if isdir: 
    78                 filename += '/' 
    79         elif isinstance(parent, File): 
    80             # db data 
    81             id = (data['type'], data['id']) 
    82             media = parent._beacon_media 
    83             filename = parent.filename + data['name'] 
    84             if isdir: 
    85                 filename += '/' 
    86         elif not data['name']: 
    87             # root directory 
    88             id = (data['type'], data['id']) 
    89             media = parent 
    90             parent = None 
    91             filename = media.mountpoint 
    92         else: 
    93             raise ValueError('unable to create File item from %s', data) 
    94  
     68    def __init__(self, id, filename, data, parent, media, overlay=False, isdir=False): 
    9569        Item.__init__(self, id, 'file://' + filename, data, parent, media) 
    9670        if self._beacon_data.get('scheme'): 
    9771            # file uses a special scheme like dvd 
    9872            self.url = self._beacon_data.get('scheme') + '://' + filename 
    99  
    10073        self._beacon_overlay = overlay 
    10174        self._beacon_isdir = isdir 
     
    10982                self._beacon_islink = True 
    11083 
    111  
    112     # ------------------------------------------------------------------------- 
    113     # Public API 
    114     # ------------------------------------------------------------------------- 
    115  
    11684    def list(self, recursive=False): 
    11785        """ 
     
    12088        @returns: InProgress object of a kaa.beacon.Query 
    12189        """ 
    122         # This function is not used internally 
    123         return self._beacon_controller().query(parent=self, recursive=recursive) 
    124  
     90        # Note: this function is not used internally 
     91        return self._beacon_controller.query(parent=self, recursive=recursive) 
    12592 
    12693    def scan(self): 
    12794        """ 
    12895        Request the item to be scanned. 
    129         Returns either False if not connected or an InProgress object. 
    130         """ 
    131         # This function is only used by client.py used internally 
    132         result = self._beacon_controller()._beacon_parse(self) 
     96 
     97        @returns: False if not connected or an InProgress object. 
     98        """ 
     99        # Note: this function is not used by the server 
     100        result = self._beacon_controller._beacon_parse(self) 
    133101        if isinstance(result, kaa.InProgress): 
    134102            result.connect_once(self._beacon_database_update) 
    135103        return result 
    136  
    137  
    138     def __repr__(self): 
    139         """ 
    140         Convert object to string (usefull for debugging) 
    141         """ 
    142         s = '<beacon.File %s' % self.filename 
    143         if not self.url.startswith('file://'): 
    144             s = '<beacon.File %s' % self.url 
    145         if self._beacon_data.get('mtime') == None: 
    146             s += ' (new)' 
    147         else: 
    148             s += ' (type=%s)' % str(self._beacon_data.get('type')) 
    149         return s + '>' 
    150  
    151  
    152     # ------------------------------------------------------------------------- 
    153     # Internal API 
    154     # ------------------------------------------------------------------------- 
    155104 
    156105    def _beacon_listdir(self, cache=False): 
     
    165114            # three seconds ago 
    166115            return self._beacon_listdir_cache[1:] 
    167  
    168116        # FIXME: this could block for everything except media 1. So this 
    169117        # should be done in the hwmon process. But the server doesn't like 
     
    175123            # No overlay 
    176124            overlay_results = [] 
    177  
    178125        try: 
    179126            # Try to list the directory. If that fails for some reason, 
     
    184131            self._beacon_listdir_cache = time.time(), [], {} 
    185132            return [], {} 
    186  
    187133        results_file_map = {} 
    188134        timer = time.time() 
    189  
    190135        for is_overlay, prefix, results in \ 
    191136                ((False, self.filename, fs_results), 
     
    207152                    continue 
    208153                results_file_map[r] = (r, fullpath, is_overlay, statinfo) 
    209  
    210154        # We want to avoid lambda on large data sets, so we sort the keys, 
    211155        # which is just a list of files.  This is the common case that sort() 
     
    218162        return result, results_file_map 
    219163 
    220  
     164    @property 
    221165    def _beacon_mtime(self): 
    222166        """ 
     
    241185            except (OSError, IOError): 
    242186                return mtime 
    243  
    244187        # Normal file 
    245188        fullname = self._beacon_data['name'] 
     
    249192            ext = basename[pos:] 
    250193            basename = basename[:pos] 
    251  
    252194        # FIXME: move this logic to kaa.metadata. The best way would be to 
    253195        # use the info modules for that kind of information, but we may not 
     
    263205            # cover 
    264206            special_exts = ( '.png', '.jpg' ) 
    265  
    266207        listdir_file_map = self._beacon_parent._beacon_listdir(cache=True)[1] 
    267208        # calculate the new modification time 
     
    278219                mtime += fname[3][stat.ST_MTIME] 
    279220        return mtime 
     221 
     222    def __repr__(self): 
     223        """ 
     224        Convert object to string (usefull for debugging) 
     225        """ 
     226        s = '<beacon.File %s' % self.filename 
     227        if not self.url.startswith('file://'): 
     228            s = '<beacon.File %s' % self.url 
     229        if self._beacon_data.get('mtime') == None: 
     230            s += ' (new)' 
     231        else: 
     232            s += ' (type=%s)' % str(self._beacon_data.get('type')) 
     233        return s + '>' 
  • trunk/beacon/src/fusefs.py

    r3079 r3609  
    8787        self._query_update_time = int(time.time()) 
    8888        for item in self._query: 
    89             if not item.isfile() and not item.isdir(): 
     89            if not item.isfile() and not item.isdir: 
    9090                # no file or dir, we can't link to it 
    9191                continue 
  • trunk/beacon/src/item.py

    r3475 r3609  
    5050    url:         unique url of the item 
    5151    filename:    empty string 
     52    isdir:       False 
     53    isfile:      False 
     54    scanned:     True if the item is scanned 
    5255 
    5356    Functions: 
     
    5659    __setitem__: set an attribute 
    5760    keys:        return all known attributes of the item 
    58     scanned:     return True if the item is scanned 
    5961    list:        return list of subitems 
    60     isdir:       return False 
    61     isfile:      return False 
    6262 
    6363    Do not access attributes starting with _beacon outside kaa.beacon 
     
    6868        self.url = url 
    6969        self.filename = '' 
    70  
    7170        # internal data 
    7271        self._beacon_id = _beacon_id 
     
    8180        self._beacon_name = data['name'] 
    8281 
    83  
    84     # ------------------------------------------------------------------------- 
    85     # Public API 
    86     # ------------------------------------------------------------------------- 
    87  
    8882    def get(self, key, default=None): 
    8983        """ 
     
    9589        if key.startswith('tmp:'): 
    9690            return self._beacon_tmpdata.get(key[4:], default) 
    97  
    9891        if key == 'parent': 
    9992            return self._beacon_parent 
    100  
    10193        if key == 'media': 
    10294            return self._beacon_media 
    103  
    10495        if key == 'read_only': 
    10596            # FIXME: this is not correct, a directory can also be 
    10697            # read only on a rw filesystem. 
    10798            return self._beacon_media.get('volume.read_only', default) 
    108  
    10999        if key in ('image', 'thumbnail'): 
    110100            image = self._beacon_data.get('image') 
     
    122112            if key == 'image': 
    123113                return image 
    124  
    125114            if key == 'thumbnail': 
    126115                return Thumbnail(image, self._beacon_media) 
    127  
    128  
    129116        if key == 'title': 
    130117            t = self._beacon_data.get('title') 
     
    135122            self._beacon_data['title'] = t 
    136123            return t 
    137  
    138124        result = self._beacon_data.get(key, default) 
    139125        if result is None: 
     
    141127        return result 
    142128 
    143  
    144129    def __getitem__(self, key): 
    145130        return self.get(key) 
    146  
    147131 
    148132    def __setitem__(self, key, value): 
     
    157141        self._beacon_data[key] = value 
    158142        if not self._beacon_changes: 
    159             self._beacon_controller()._beacon_update(self) 
     143            self._beacon_controller._beacon_update(self) 
    160144        self._beacon_changes[key] = value 
    161145 
    162  
    163146    def keys(self): 
    164147        """ 
     
    166149        """ 
    167150        return self._beacon_data.keys() + self._beacon_tmpdata.keys() 
    168  
    169151 
    170152    def has_key(self, key): 
     
    175157               key in self._beacon_tmpdata.keys() 
    176158 
    177  
     159    @property 
    178160    def scanned(self): 
    179161        """ 
    180         Return True if the item is in the database and fully scanned. 
     162        True if the item is in the database and fully scanned. 
    181163        """ 
    182164        return self._beacon_id is not None 
    183165 
     166    @property 
     167    def isdir(self): 
     168        """ 
     169        True if the item is a directory. 
     170        """ 
     171        return self._beacon_isdir 
     172 
     173    @property 
     174    def isfile(self): 
     175        """ 
     176        True if the item is a regular file. 
     177        """ 
     178        return not self._beacon_isdir and self.filename != '' 
    184179 
    185180    def list(self): 
     
    195190            # FIXME: return empty Query object 
    196191            return result 
    197         return self._beacon_controller().query(parent=self) 
    198  
    199  
    200     def isdir(self): 
    201         """ 
    202         Return if the item is a directory. 
    203         """ 
    204         return self._beacon_isdir 
    205  
    206  
    207     def isfile(self): 
    208         """ 
    209         Return if the item is a regular file. 
    210         """ 
    211         return not self._beacon_isdir and self.filename != '' 
    212  
     192        return self._beacon_controller.query(parent=self) 
    213193 
    214194    def delete(self): 
     
    216196        Delete item from the database (does not work on files) 
    217197        """ 
    218         return self._beacon_controller().delete_item(self) 
    219  
     198        return self._beacon_controller.delete_item(self) 
    220199 
    221200    def scan(self): 
     
    225204        return False 
    226205 
    227  
    228     def get_ancestors(self): 
     206    @property 
     207    def ancestors(self): 
    229208        """ 
    230209        Return an iterator to walk through the parents. 
    231210        """ 
    232211        return ParentIterator(self) 
    233  
    234  
    235     # ------------------------------------------------------------------------- 
    236     # Internal API 
    237     # ------------------------------------------------------------------------- 
    238212 
    239213    def _beacon_database_update(self, data): 
     
    247221            self._beacon_data[key] = value 
    248222 
    249  
     223    @property 
    250224    def _beacon_controller(self): 
    251225        """ 
    252226        Get the controller (the client or the server) 
    253227        """ 
    254         return self._beacon_media.get_controller() 
    255  
    256  
     228        return self._beacon_media._beacon_controller 
     229 
     230    @property 
    257231    def _beacon_mtime(self): 
    258232        """ 
     
    261235        return None 
    262236 
    263  
    264237    def __repr__(self): 
    265238        """ 
     
    267240        """ 
    268241        return '<beacon.Item %s>' % self.url 
    269  
    270242 
    271243 
  • trunk/beacon/src/media.py

    r3071 r3609  
    5252    def __init__(self, id, controller): 
    5353        log.info('new media %s', id) 
    54         self._controller = controller 
     54        self._beacon_controller = controller 
    5555        self.id = id 
    5656        # needed by server. 
     
    5858 
    5959 
    60     def get_controller(self): 
    61         """ 
    62         Get the controller (the client or the server) 
    63         """ 
    64         return self._controller 
    65  
    66  
    6760    def eject(self): 
    6861        """ 
    6962        Eject the media. 
    7063        """ 
    71         self._controller.eject(self) 
     64        self._beacon_controller.eject(self) 
    7265 
    7366 
     
    8578            self.mountpoint += '/' 
    8679        # get basic information from database 
    87         media = self._controller._beacon_media_information(self) 
     80        media = self._beacon_controller._beacon_media_information(self) 
    8881        if isinstance(media, kaa.InProgress): 
    8982            # This will happen for the client because in the client 
     
    151144 
    152145 
    153     def _get_beacon_media(self): 
     146    @property 
     147    def _beacon_media(self): 
    154148        """ 
    155149        Get _beacon_media which is this object itself. To avoid circular 
     
    157151        """ 
    158152        return self 
    159  
    160     _beacon_media  = property(_get_beacon_media, None, None, "media reference") 
    161  
    162153 
    163154 
     
    169160        self._dict = dict() 
    170161        self._idlist = [] 
    171         self._controller = None 
     162        self._beacon_controller = None 
    172163 
    173164 
     
    178169        for media in self._dict.keys()[:]: 
    179170            self.remove(media) 
    180         self._controller = controller 
     171        self._beacon_controller = controller 
    181172 
    182173 
     
    186177        Add a media. 
    187178        """ 
    188         if not self._controller: 
     179        if not self._beacon_controller: 
    189180            raise RuntimeError('not connected to database') 
    190181        if id in self._dict: 
    191182            yield self._dict.get(id) 
    192         media = Media(id, self._controller) 
     183        media = Media(id, self._beacon_controller) 
    193184        async = media.update(prop) 
    194185        if isinstance(async, kaa.InProgress): 
  • trunk/beacon/src/server/cdrom.py

    r3608 r3609  
    3434import os 
    3535import sys 
     36import re 
    3637import array 
    3738import struct 
     
    7071import kaa.metadata 
    7172 
    72 from ..utils import fstab 
    73  
    7473# get logging object 
    7574log = logging.getLogger('beacon.hal') 
     
    8281 
    8382_rom_drives = [] 
     83 
     84def fstab(): 
     85    """ 
     86    Read /etc/fstab into a list of (device, mountpoint, type, options) 
     87    """ 
     88    if not os.path.isfile('/etc/fstab'): 
     89        return [] 
     90    result = [] 
     91    regexp = re.compile('([^ \t]*)[ \t]*([^ \t]*)[ \t]*([^ \t]*)[ \t]*([^ \t]*)') 
     92    fd = open('/etc/fstab') 
     93    for line in fd.readlines(): 
     94        if line.find('#') >= 0: 
     95            line = line[:line.find('#')] 
     96        line = line.strip() 
     97        if not line: 
     98            continue 
     99        if not regexp.match(line): 
     100            continue 
     101        device, mountpoint, type, options = regexp.match(line).groups() 
     102        device = os.path.realpath(device) 
     103        result.append((device, mountpoint, type, options)) 
     104    fd.close() 
     105    return result 
     106 
    84107 
    85108@kaa.threaded('beacon.cdrom') 
  • trunk/beacon/src/server/controller.py

    r3608 r3609  
    5151        self.hwmon = devices.HardwareMonitor(handler, db, rootfs) 
    5252 
    53     # Item callbacks 
    54  
    5553    def _beacon_parse(self, item): 
    5654        """ 
    5755        Parse an item 
     56        Called by Item objects 
    5857        """ 
    5958        return parse(self._db, item) 
    60  
    6159 
    6260    @kaa.coroutine() 
     
    6462        """ 
    6563        Timed callback to write all changes to the db. 
     64        Called by Item objects 
    6665        """ 
    6766        while self._db.read_lock.is_locked(): 
     
    7574        self._db.commit() 
    7675 
    77  
    7876    def _beacon_update(self, item): 
    7977        """ 
    8078        Mark item as changed to be updated in the db. 
     79        Called by Item objects 
    8180        """ 
    8281        if not self._changed: 
     
    8584        self._changed.append(item) 
    8685 
    87  
    8886    def query(self, **query): 
    8987        """ 
    9088        Database query. 
     89        Called by Item objects 
    9190        """ 
    9291        return self._db.query(**query)