Beispiel #1
0
 def test_file_paths(self):
     self.assertPathsEqual(expected_file_paths, file_paths(self.filtered_walk()))
     self.assertPathsEqual(expected_file_paths[len(depth_0_file_paths):],
                           file_paths(self.filtered_walk(min_depth=1)))
     self.assertPathsEqual(depth_0_file_paths, file_paths(self.filtered_walk(depth=0)))
     walk_iter = self.filtered_walk(included_files=['file*'],
                                    excluded_files=['*2*'],
                                    included_dirs=['sub*'],
                                    excluded_dirs=['*2'])
     self.assertPathsEqual(filtered_file_paths, file_paths(walk_iter))
Beispiel #2
0
 def test_file_paths(self):
     self.assertPathsEqual(expected_file_paths, file_paths(self.filtered_walk()))
     self.assertPathsEqual(expected_file_paths[len(depth_0_file_paths):],
                          file_paths(self.filtered_walk(min_depth=1)))
     self.assertPathsEqual(depth_0_file_paths, file_paths(self.filtered_walk(depth=0)))
     walk_iter = self.filtered_walk(included_files=['file*'],
                                excluded_files=['*2*'],
                                included_dirs=['sub*'],
                                excluded_dirs=['*2'])
     self.assertPathsEqual(filtered_file_paths, file_paths(walk_iter))
def index_podcasts():
    files = file_paths(
        filtered_walk(
            'static/podcasts',
            included_files=['*.mp3'],
        ))
    return files
def main(srcdir, destpath, img_size, thread_number):
    #Iterate csv container
    csvFiles = file_paths(
        filtered_walk(srcdir, depth=1, included_files='*.csv'))
    for csvfile in list(csvFiles):
        #Download files
        down(csvfile, destpath, img_size, thread_number)
Beispiel #5
0
def index_content(root_dir, file_types, content_type):
    """ Scan the media directory, creating an index of file properties for display and serving
    """
    logger.debug('indexing')
    hasher = sha1()
    content_dir = os.path.join(root_dir, app.config['CONTENT_DIR'])
    files = file_paths(filtered_walk(
        content_dir,
        included_files=file_types,
    ))
    for contentfile in files:
        rel_path = os.path.relpath(contentfile, root_dir)
        filepath = os.path.join(root_dir, rel_path)
        filename = os.path.split(contentfile)[1]
        local_path = os.path.relpath(filepath, root_dir)
        if os.path.exists(os.path.join(filepath, 'folder.jpg')):
            img = os.path.join(filepath, 'folder.jpg')
        else:
            img = ''
        hasher.update(local_path)
        file_key = hasher.hexdigest()
        tags = _get_tags(filepath)
        media = Media()
        media.type = content_type
        media.path = filepath
        media.filename = filename
        media.file_id = file_key
        media.tags = tags
        media.img = img
        media.type = content_type
        media.save()
Beispiel #6
0
def index_content(root_dir, file_types, content_type):
    """ Scan the media directory, creating an index of file properties for display and serving
    """
    logger.debug('indexing')
    hasher = sha1()
    content_dir = os.path.join(root_dir, app.config['CONTENT_DIR'])
    files = file_paths(filtered_walk(content_dir, included_files=file_types, ))
    for contentfile in files:
        rel_path = os.path.relpath(contentfile, root_dir)
        filepath = os.path.join(root_dir, rel_path)
        filename = os.path.split(contentfile)[1]
        local_path = os.path.relpath(filepath, root_dir)
        if os.path.exists(os.path.join(filepath, 'folder.jpg')):
            img = os.path.join(filepath, 'folder.jpg')
        else:
            img = ''
        hasher.update(local_path)
        file_key = hasher.hexdigest()
        tags = _get_tags(filepath)
        media = Media()
        media.type = content_type
        media.path = filepath
        media.filename = filename
        media.file_id = file_key
        media.tags = tags
        media.img = img
        media.type = content_type
        media.save()
Beispiel #7
0
def file(path: str, exts: list) -> list:
    ''' 在{path}中搜索所有扩展名符合{exts}的文件,并转换成绝对路径。

    Args:
        path: 文件或目录
        exts: 扩展名列表

    Returns:
        list: 文件绝对路径列表
    '''

    # 将path转换为绝对路径
    if not os.path.isabs(path):
        abs_path = os.path.join(os.getcwd(), path)
    else:
        abs_path = path
    # 遍历path,并返回指定扩展名的文件
    if os.path.isfile(path) and os.path.splitext(path)[1].lower() in exts:
        return [abs_path]
    if os.path.isdir(abs_path):
        return filter(
            None,
            list(
                file_paths(
                    filtered_walk(abs_path,
                                  included_files=['*' + e for e in exts]))))
    return []
Beispiel #8
0
def directory_to_csv(dicom_path, csv_file_path, tags_in_files,
                     tags_to_exclude):
    """
    directory_to_csv iterates over a directory, finds dicom files with
    a .dcm extension and then creates a spreadsheet containing all of 
    the tag values for the tags in the csv for every dicom file

    Args:
        dicom_path (str): Path to scan for dicom files.
        csv_file_path (str): Path and file name for the output csv file.
        tags_in_files (dict): Dictionary containing tags to include in the csv
        tags_to_exclude (dict): Dictionary containing tags to exclude in the csv

    Returns:
        None
    """
    tags_in_files = tags_in_files.copy()  # copy because we're going to modify
    for tag_to_exclude in tags_to_exclude:
        if tag_to_exclude in tags_in_files:
            del tags_in_files[tag_to_exclude]

    # sort by group and then element number
    tags_in_files = OrderedDict(
        sorted(tags_in_files.items(), key=(lambda k: (k[1][0], k[1][1]))))
    dicom_file_paths = file_paths(
        filtered_walk(dicom_path, included_files=["*.dcm"]))

    with open(csv_file_path, "w") as f:
        writer = csv.writer(f)

        # write the headers
        header_row = list(tags_in_files.keys())
        header_row.append("FilePath")
        writer.writerow(header_row)

        # write the rows
        for dicom_file_path in dicom_file_paths:
            dicom_file = pydicom.read_file(dicom_file_path)

            row_vals = []
            for keyword in tags_in_files:
                tag_val = dicom_file.get(keyword)

                if tag_val is None:
                    tag_val = ""
                else:
                    if isinstance(tag_val, Sequence) and not isinstance(
                            tag_val, (str, bytes, bytearray)):
                        tag_val = "^".join([str(x) for x in tag_val])
                    elif not isinstance(tag_val, str):
                        tag_val = str(tag_val)

                    tag_val = (tag_val.replace(",", "^").replace("\n",
                                                                 "").replace(
                                                                     "\r", ""))
                row_vals.append(tag_val)

            row_vals.append(dicom_file_path)
            writer.writerow(row_vals)
Beispiel #9
0
 def _display_files_not_restored(self, restore_to):
     print
     count = 0
     files = file_paths(filtered_walk(restore_to))
     for item in files:
         count = count + 1
         print('{}. {}'.format(count, item))
     if count:
         print(yellow(
             "The {} files listed above were not restored "
             "(just so you know).".format(count)
         ))
         print
Beispiel #10
0
 def sequence(self, formats):
     '''return final list'''
     total = []
     print 'iQuence Searching Path: %s' % self.topdir
     fileset = file_paths(filtered_walk(self.topdir, depth=self.depth ,included_files=formats))
     #~ print files
     #for i in fileset: print i
     #~ for pattern in formats:
         #~ fileset = self.find_files(pattern)
     seqDict = self.extractDict(fileset)
     print seqDict
     seqlist = self.parseDict(seqDict)
     #~ print seqlist
         #~ total+=seqlist
         
     cmds = self.genViewerCmds(seqlist)
     #~ print cmds
     return cmds
Beispiel #11
0
def get_tags_in_files(dicom_path, tag_file_path):
    """
    get_tags_in_files iterates over a directory, finds dicom files with
    a .dcm extension, and finds all unique dicom tag instances. it then
    writes the tags out as a csv file.

    Args:
        dicom_path (str): Path to scan for dicom files.
        tag_file_path (str): Path and file name for the output csv file.

    Returns:
        dict: A dictionary containing the tags loaded.
    """
    # create the output directory
    if not os.path.exists(os.path.dirname(tag_file_path)):
        os.makedirs(os.path.dirname(tag_file_path))

    # get the tags
    tags_in_files = {}
    dicom_file_paths = file_paths(
        filtered_walk(dicom_path, included_files=["*.dcm"]))
    for dicom_file_path in dicom_file_paths:
        dicom_file = pydicom.read_file(dicom_file_path)
        for item in dicom_file:
            if item.keyword not in tags_in_files:
                group = "0x%04x" % item.tag.group
                element = "0x%04x" % item.tag.element
                tags_in_files[
                    item.keyword] = group, element, item.keyword, item.name

    # sort the tags
    tags_in_files = OrderedDict(
        sorted(tags_in_files.items(), key=(lambda k: (k[1][0], k[1][1]))))

    # write out the file
    with open(tag_file_path, "w") as f:
        writer = csv.writer(f)
        writer.writerow(["group", "element", "keyword", "name"])
        for item in tags_in_files:
            writer.writerow(tags_in_files[item])

    return tags_in_files
Beispiel #12
0
    def sequence(self, formats):
        '''return final list'''
        total = []
        print 'iQuence Searching Path: %s' % self.topdir
        fileset = file_paths(
            filtered_walk(self.topdir,
                          depth=self.depth,
                          included_files=formats))
        #~ print files
        #for i in fileset: print i
        #~ for pattern in formats:
        #~ fileset = self.find_files(pattern)
        seqDict = self.extractDict(fileset)
        print seqDict
        seqlist = self.parseDict(seqDict)
        #~ print seqlist
        #~ total+=seqlist

        cmds = self.genViewerCmds(seqlist)
        #~ print cmds
        return cmds
Beispiel #13
0
def search(path):
    """
    This method takes path to a directory as an argument and returns list of
    files under it in the format:
        filename: absolute path
    """
    EXCLUDED_DIRS = ['env',
                     'venv',
                     '.git'
                     ]
    EXCLUDED_FILES = ['.gitignore',
                      '*.pyc',
                      '*.out'
                      ]

    files = {}

    for i in file_paths(filtered_walk(path, excluded_dirs=EXCLUDED_DIRS,
                                      excluded_files=EXCLUDED_FILES)):
        files[os.path.basename(i)] = os.path.realpath(i)
    return files
Beispiel #14
0
nbog + etnam


# In[21]:

#Write the blog post
#Ask to write content or publish 
writecont = input('Write content? y/N ')
if 'y' in writecont:
    contenmak = input('content: ')

else:
    #search or manually locate fil.
    pear = input('path to search: y/N')
    if 'y' in pear:
        files = file_paths(filtered_walk(pear, depth=100, included_files=[nbog + etnam]))
        for fil in files:
            print (fil)
            jsve.update({'filefound' : fil})
    else:
        patlim = input('path of file: ')
        jsve.update({'filefound' : patlim + nbog + etnam})


# In[22]:

#fil


# In[23]:
Beispiel #15
0
def get_file_paths(root_paths, extension='flac'):
    pattern = '*.%s' % extension
    for root in root_paths:
        paths = file_paths(filtered_walk(root, included_files=[pattern]))
        for file_path in paths:
            yield file_path
Beispiel #16
0
runpy.run_module('requests')


# In[ ]:




# In[29]:

nbog = raw_input('Name of notebook to tag: ')


# In[30]:

files = file_paths(filtered_walk('/home/wcmckee/github/', depth=100, included_files=[nbog + '.ipynb']))


# In[35]:

#Easier to access ipynb and access the code import cell.
#Parse the notebook for import tags
#IPython module wrapper for returning list of imported modules
#from a ipynb file. 
#Get the list of modules for tags from this.


# In[32]:

for fil in files:
    #print fil
def index_podcasts():
    files = file_paths(filtered_walk('static/podcasts', included_files=['*.mp3'], ))
    return files
Beispiel #18
0
import os.path

from walkdir import filtered_walk, dir_paths, all_paths, file_paths
files = file_paths(filtered_walk('.', included_files=['*.go']))

packages = {}

for f in files:
    dirn = os.path.dirname(f)

    if dirn not in packages:
        packages[dirn] = {}

    packages[dirn][f] = 1

for n in packages:
    print "{0}.o: {1}".format(n, " ".join(packages[n].keys()))
    print "\t$(GCCGO) -L . -I . -c -g -o $@ $^"

print "all_pkgs:" + " ".join(["{}.o".format(n) for n in packages])
print "\teho"