Created
December 15, 2014 22:15
-
-
Save hogjonny/a8c512ce23098f2dfd75 to your computer and use it in GitHub Desktop.
Pyside --> Filtered (pattern), directory file list (via: QtCore.QAbstractListModel)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding:utf-8 | |
# -- This line is 75 characters ------------------------------------------- | |
# ------------------------------------------------------------------------- | |
# ------------------------------------------------------------------------- | |
# version: 0.1 | |
# date: 12/15/2014 | |
# author: jGalloway | |
# ------------------------------------------------------------------------- | |
# ------------------------------------------------------------------------- | |
import os, sys | |
#site-library imports | |
from PySide import QtCore, QtGui | |
# project imports | |
from bp_helpers import synthesize # storage/variable gettr/settr convenience class | |
from bp_packages.bp_files.bp_path import Path # path convenience class | |
#################################################################### | |
class dirFileListModel(QtCore.QAbstractListModel): | |
def __init__(self, inRootPath=None, pattern='*.txt', parent=None, *args): | |
""" | |
takes a path | |
gens a list of files matching pattern | |
data = each file is a item | |
""" | |
super(dirFileListModel, self).__init__() | |
#synthesize(self, '_fileCount', 0) | |
if inRootPath == None: # in case we got nothing, just make it empty | |
inRootPath = '' | |
if not isinstance(inRootPath, Path): | |
inRootPath = Path(inRootPath) | |
synthesize(self, '_inRootPath', inRootPath) | |
synthesize(self, '_pattern', pattern) | |
if self._inRootPath != '' or len(self._inRootPath) >0: | |
self.genList(self._pattern) | |
# -------------------------------------------------------------------- | |
def genList(self, pattern='*.txt'): | |
try: | |
self._listData = self._inRootPath.files(pattern) | |
except: | |
synthesize(self, '_listData', self._inRootPath.files(pattern)) | |
return self._listData | |
# -------------------------------------------------------------------- | |
def rowCount(self, parent=QtCore.QModelIndex()): | |
return len(self._listData) | |
# -------------------------------------------------------------------- | |
def data(self, index, role=QtCore.Qt.DisplayRole): | |
if not index.isValid(): | |
return None | |
if index.row() >= len(self._listData) or index.row() < 0: | |
return None | |
if role == QtCore.Qt.DisplayRole: | |
return self._listData[index.row()] | |
########################################################################### | |
# --call block------------------------------------------------------------- | |
if __name__ == "__main__": | |
'''Allows the script to be run as main()''' | |
testPath = Path(r'c:\\project\\dir') | |
testModel = dirFileListModel(testPath) | |
#should print the number of files in the dir matching the pattern | |
print testModel.rowCount() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment