示例#1
0
 def _to_csv(self, node, with_header=False):
     '''print the tree to csv'''
     rend = anytree.RenderTree(node, childiter=self._sort_tree)
     if with_header:
         Logger.out(self.CSV_HEADER)
     for _, _, node in rend:
         self._node_to_csv(node)
示例#2
0
 def _print_node(self, node, pre='', withpath=False,
                 withdepth=False, withstorage=False):
     '''print a node'''
     if node.type == self.TYPE_TOP:
         Logger.out('{}{}'.format(pre, node.name))
     elif node.type == self.TYPE_FILE:
         name = node.name
         if withpath:
             name = node.relpath
         if withstorage:
             storage = self._get_storage(node)
         attr = ''
         if node.md5:
             attr = ', md5:{}'.format(node.md5)
         compl = 'size:{}{}'.format(utils.human(node.size), attr)
         if withstorage:
             compl += ', storage:{}'.format(Logger.bold(storage.name))
         Logger.file(pre, name, compl)
     elif node.type == self.TYPE_DIR:
         name = node.name
         if withpath:
             name = node.relpath
         depth = ''
         if withdepth:
             depth = len(node.children)
         if withstorage:
             storage = self._get_storage(node)
         attr = []
         if node.size:
             attr.append(['totsize', utils.human(node.size)])
         if withstorage:
             attr.append(['storage', Logger.bold(storage.name)])
         Logger.dir(pre, name, depth=depth, attr=attr)
     elif node.type == self.TYPE_STORAGE:
         hf = utils.human(node.free)
         ht = utils.human(node.total)
         dt = ''
         if self._has_attr(node, 'ts'):
             dt = ', date:'
             dt += utils.epoch_to_str(node.ts)
         name = '{} (free:{}, total:{}{})'.format(node.name, hf, ht, dt)
         Logger.storage(pre, name, node.attr)
     elif node.type == self.TYPE_ARC:
         if self.arc:
             Logger.arc(pre, node.name, node.archive)
     else:
         Logger.err('Weird node encountered: {}'.format(node))
示例#3
0
    def _node_to_csv(self, node, sep=','):
        '''
        print a node to csv
        @node: the node to consider
        '''
        if not node:
            return ''
        if node.type == self.TYPE_TOP:
            return ''

        out = []
        if node.type == self.TYPE_STORAGE:
            # handle storage
            out.append(node.name)
            out.append(node.type)
            out.append('')  # full path
            # size
            sz = self._rec_size(node, store=False)
            out.append(utils.human(sz))
            out.append(utils.epoch_to_str(node.ts))
            out.append('')  # maccess
            out.append('')  # md5
        else:
            out.append(node.name)
            out.append(node.type)

            # node full path
            parents = self._get_parents(node)
            storage = self._get_storage(node)
            fullpath = os.path.join(storage.name, parents)
            out.append(fullpath)

            out.append(utils.human(node.size))
            out.append(utils.epoch_to_str(storage.ts))
            out.append(utils.epoch_to_str(node.maccess))

            # md5 if any
            if node.md5:
                out.append(node.md5)
            else:
                out.append('')

        line = sep.join(['"' + o + '"' for o in out])
        if len(line) > 0:
            Logger.out(line)
示例#4
0
文件: noder.py 项目: birt/catcli
 def _print_node(self,
                 node,
                 pre='',
                 withpath=False,
                 withdepth=False,
                 withstorage=False,
                 recalcparent=False):
     '''
     print a node
     @node: the node to print
     @pre: string to print before node
     @withpath: print the node path
     @withdepth: print the node depth info
     @withstorage: print the node storage it belongs to
     @recalcparent: get relpath from tree instead of relpath field
     '''
     if node.type == self.TYPE_TOP:
         # top node
         Logger.out('{}{}'.format(pre, node.name))
     elif node.type == self.TYPE_FILE:
         # node of type file
         name = node.name
         if withpath:
             if recalcparent:
                 name = os.sep.join([self._get_parents(node.parent), name])
             else:
                 name = node.relpath
         name = name.lstrip(os.sep)
         if withstorage:
             storage = self._get_storage(node)
         attr = ''
         if node.md5:
             attr = ', md5:{}'.format(node.md5)
         compl = 'size:{}{}'.format(utils.human(node.size), attr)
         if withstorage:
             compl += ', storage:{}'.format(Logger.bold(storage.name))
         Logger.file(pre, name, compl)
     elif node.type == self.TYPE_DIR:
         # node of type directory
         name = node.name
         if withpath:
             if recalcparent:
                 name = os.sep.join([self._get_parents(node.parent), name])
             else:
                 name = node.relpath
         name = name.lstrip(os.sep)
         depth = ''
         if withdepth:
             depth = len(node.children)
         if withstorage:
             storage = self._get_storage(node)
         attr = []
         if node.size:
             attr.append(['totsize', utils.human(node.size)])
         if withstorage:
             attr.append(['storage', Logger.bold(storage.name)])
         Logger.dir(pre, name, depth=depth, attr=attr)
     elif node.type == self.TYPE_STORAGE:
         # node of type storage
         hf = utils.human(node.free)
         ht = utils.human(node.total)
         nbchildren = len(node.children)
         # get the date
         dt = ''
         if self._has_attr(node, 'ts'):
             dt = 'date:{}'.format(utils.epoch_to_str(node.ts))
         ds = ''
         # the children size
         sz = self._rec_size(node, store=False)
         sz = utils.human(sz)
         ds = 'totsize:{}'.format(sz)
         # format the output
         name = '{}'.format(node.name)
         args = [
             'nbfiles:{}'.format(nbchildren), 'free:{}/{}'.format(hf, ht),
             dt, ds
         ]
         Logger.storage(pre, name, '({})'.format(','.join(args)), node.attr)
     elif node.type == self.TYPE_ARC:
         # archive node
         if self.arc:
             Logger.arc(pre, node.name, node.archive)
     else:
         Logger.err('bad node encountered: {}'.format(node))