當前位置: 首頁>>代碼示例>>Python>>正文


Python path.Path類代碼示例

本文整理匯總了Python中moments.path.Path的典型用法代碼示例。如果您正苦於以下問題:Python Path類的具體用法?Python Path怎麽用?Python Path使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Path類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: path

def path(relative=''):
    """
    serve a static file

    this also allows pose to function as a customizable file system browser

    be careful with what you set path_root to
    if the machine you run this on has sensitive information
    and is connected to a public network
    """
    global path_root

    if re.match('~', relative):
        relative = os.path.expanduser(relative)
    ## else:
    ##     relative = os.path.join('/', relative)
    ##     full = os.path.abspath(relative)
    ## print full

    full_path = os.path.join(path_root, relative)
 
    path = Path(full_path, relative_prefix=path_root)
    if path.type() == "Directory":
        node = path.load()
        #will depend what we want to sort by here:
        node.sort_by_path()
        #node.sort_by_date()
        return template('directory', path=path, contents=node.contents)
    else:
        #this is equivalent to a view...
        #indicate it in the log:
        #path.log_action()
        return static_file(relative, root=path_root)
開發者ID:charlesbrandt,項目名稱:mindstream,代碼行數:33,代碼來源:application.py

示例2: test_load_journal

 def test_load_journal(self):
     dest = 'zoobar/todo.txt'
     p = Path(dest)
     p.load_journal(create=True)
     assert os.path.exists(dest)
     p.remove()
     assert not os.path.exists(dest)
開發者ID:charlesbrandt,項目名稱:moments,代碼行數:7,代碼來源:test_path.py

示例3: find_photos

def find_photos(cur_summary, cur_year, photo_roots, ignores=[]):
    #look for pictures
    #may require customization for each root
    #but starting with one known one for now
    for photo_root in photo_roots:
        #now look for content matching the day:
        year_path = os.path.join(photo_root, cur_year)
        options = os.listdir(year_path)
        for option in options:
            ignore = False
            for i in ignores:
                if re.search(i, option):
                    ignore = True

            if re.match(d_compact, option) and not ignore:
                #find all photos in dir:
                option_path = os.path.join(year_path, option)
                print("looking in: %s" % option_path)
                #very similar to content.Content.find_media()
                #but this is only checking one level down
                #I don't think we want to walk here:
                kind = "Image"
                image_options = os.listdir(option_path)
                for io in image_options:
                    media = os.path.join(option_path, io)
                    mpath = Path(media)
                    #if debug:
                    #    print "looking at: %s" % media
                    if mpath.type() == kind:
                        print("matched!", media)
                        if not media in cur_summary.photos:
                            cur_summary.photos.append(media)
開發者ID:charlesbrandt,項目名稱:mindstream,代碼行數:32,代碼來源:make_summary.py

示例4: main

def main():
    if len(sys.argv) > 1:
        helps = ['--help', 'help', '-h']
        for i in helps:
            if i in sys.argv:
                usage()
                exit()

        f1 = sys.argv[1]
        if len(sys.argv) > 2:
            f2 = sys.argv[2]
        else:
            f1_path = Path(f1)
            f1_dir = f1_path.parent()
            
            f2 = os.path.join(str(f1_dir), "summary.txt")
            f2_path = Path(f2)
            if not f2_path.exists():
                print("Saving output to: %s" % f2)
            else:
                print("Warning: %s exists!" % f2)
                exit()

        create_summary(f1, f2)
    else:
        usage()
        exit()
開發者ID:charlesbrandt,項目名稱:mindstream,代碼行數:27,代碼來源:time_report.py

示例5: path

def path(relative=''):
    """
    serve a static file

    this also allows pose to function as a customizable file system browser

    be careful with what you set path_root to
    if the machine you run this on has sensitive information
    and is connected to a public network
    """
    global path_root

    if re.match('~', relative):
        relative = os.path.expanduser(relative)

    full_path = os.path.join(path_root, relative)
 
    path = Path(full_path, relative_prefix=path_root)
    if path.type() == "Directory":
        #we shouldn't be returning directory listing here
        pass    
    else:
        #this is equivalent to a view...
        #indicate it in the log:
        #path.log_action()
        return static_file(relative, root=path_root)
開發者ID:charlesbrandt,項目名稱:medley,代碼行數:26,代碼來源:application.py

示例6: add_new

def add_new(source_list, source_dir, destination=None):
    #ignores = ["classics", "misc", "other", "youtube-dl", "playlists"]
    ignores = ["playlists"]

    m3u = M3U(source_list)
    if os.path.isdir(source_dir):
        source_dir_path = Path(source_dir)
        subdirs = source_dir_path.load().directories
        #subdirs = os.listdir(source_dir)
        for subdir in subdirs:
            print("")
            if check_ignore(str(subdir), ignores):
                print("SKIP (IGNORES): %s" % subdir)
            else:
                print("SUBDIR: %s" % subdir)
                scan_dir(m3u, subdir)

        scan_dir(m3u, source_dir)


    else:
        print("NOT A DIRECTORY: %s" % source_dir)

    print("")
    print("")
    #for item in m3u:
    #    print item
    if destination is None:
        source_list_path = Path(source_list)
        dest_name = Timestamp().compact(accuracy="day") + "-videos.m3u"
        destination = os.path.join(str(source_list_path.parent()), dest_name)
        
    print("Saving to: %s" % destination)
    m3u.save(destination)
開發者ID:charlesbrandt,項目名稱:medley,代碼行數:34,代碼來源:update_playlist.py

示例7: copy_up

def copy_up(relative=''):
    """
    find the item at the supplied path
    and copy it up to the parent directory
    this is useful for images that should show up as the default image
    """
    global path_root

    if re.match('~', relative):
        relative = os.path.expanduser(relative)

    full_path = os.path.join(path_root, relative)
    path = Path(full_path, relative_prefix=path_root)

    if path.type() == "Image":
        cur_dir = path.parent()
        parent = cur_dir.parent()

        path.copy(parent)

        #this should be sufficient
        return "Success!"

    else:
        return "Failed"
開發者ID:charlesbrandt,項目名稱:sortable_list,代碼行數:25,代碼來源:application.py

示例8: test_create

 def test_create(self):
     p = "create_me.txt"
     path = Path(p)
     assert not os.path.exists(p)
     path.create()
     assert os.path.exists(p)
     path.remove()
     assert not os.path.exists(p)
開發者ID:charlesbrandt,項目名稱:moments,代碼行數:8,代碼來源:test_path.py

示例9: test_files_to_journal

 def test_files_to_journal(self):
     output = "temp_files_to_journal_test_output.txt"
     self.d.files_to_journal(journal_file=output)
     dest = os.path.join('./zoobar', output)
     assert os.path.exists(dest)
     path = Path(dest)
     path.remove()
     assert not os.path.exists(dest)
開發者ID:charlesbrandt,項目名稱:moments,代碼行數:8,代碼來源:test_path.py

示例10: find_json

def find_json(item, limit_by_name=False, debug=False):
    """
    take any string
    see if it is a path for a json file
    or a path to a directory that contains a json file
    or look in the same directory as the item

    if more than one json file found, print a warning
    return the last json file

    if limit_by_name is true, and if item is a (non-json) file,
    use its filename to limit jsons to match that filename by default
    also [2016.04.10 14:21:49]
    switching this behavior
    now if limit_by_name is set to true,
    it will only return a result if the name matches
    otherwise the default behavior will be to try to match the name
    if there is more than one json in the directory
    otherwise it won't be strict
    
    """
    matches = find_jsons(item, limit_by_name, debug)

    if not matches:
        return None

    elif len(matches) == 1:
        logging.debug("find_json: found match: %s" % matches[0])
        return matches[0]

    else:
        #found more than one
        logging.debug("find_json: found many: %s" % matches)

        #even if limit by name was not specified as true,
        #in the case of multiple matches,
        #it may still make sense to try to match by name
        #if no match, then it's still possible to return the last one
        found = False
        name = ''
        p = Path(item)
        #maybe this should be checked for directories too???
        if p.type() != "Directory":
            name = to_tag(p.name)
            
        if name: 
            for match in matches:
                if re.search(name, str(match)):
                    found = match

        if found:
            logging.debug("find_json: matched name: %s" % found)
            return found
        else:
            print("WARNING: find_json: more than one match found: %s" % matches)
            logging.debug("find_json: returning last: %s" % matches[-1])
            return matches[-1]
開發者ID:charlesbrandt,項目名稱:medley,代碼行數:57,代碼來源:helpers.py

示例11: process_files

def process_files(source_list, translate=None, action="copy", m3u_dest="temp.txt"):
    """
    copy *only* the files referenced in a source_list to a new loacation
    """
    result = ''
    
    #j = Journal()
    #j.from_file(journal)
    #j = load_journal(journal)
    #m = MediaList()
    #m.from_journal(j, local_path='/c')
    #sources = Sources()

    sl = Path(source_list)
    assert sl.exists()
    converter = Converter()

    if sl.extension == ".m3u":
        print("M3U!")
        sources = converter.from_m3u(source_list)
    elif sl.extension == ".txt":
        sources = converter.from_journal(source_list)
    else:
        print("UNKNOWN EXTENSION: %s" % sl.extension)

    
    new_sources = Sources()
    counter = 0
    for i in sources:
        #print i
        #if re.search('\.mp3', i.path):
        if os.path.exists(str(i.path)):
            destination = make_destination(i.path, translate)
            #print "SOURCE: %s" % i
            print("DEST: %s" % destination)

            if action == "copy":
                print("Copy %03d: %s" % (counter, i.path))
                copy_file(i.path, destination)
            if action == "m3u":
                new_sources.append(Source(destination))
        else:
            print("COULD NOT FIND FILE: %s" % i.path)
        counter += 1

    if action == "m3u":
        #print len(new_sources)
        m3u = converter.to_m3u(new_sources, verify=False)
        #print m3u
        print("SAVING M3U TO: %s" % m3u_dest)
        f = open(m3u_dest, 'w')
        f.write(m3u)
        f.close()
開發者ID:charlesbrandt,項目名稱:medley,代碼行數:53,代碼來源:copy_media.py

示例12: rotate_image

def rotate_image(source, degrees):
    if os.path.exists(source):
        p = Path(source)
        assert p.type() == "Image"
        i = p.load()
        print("Rotating %s by %s degrees" % (source, degrees))
        i.rotate(degrees)
        #i.auto_rotate()
        
    else:
        print("Couldn't find path: %s" % source)
        exit()
開發者ID:charlesbrandt,項目名稱:templates,代碼行數:12,代碼來源:rotate_image.py

示例13: diff_files

def diff_files(fname, path1, path2, indent, diff_system=False):

    #until we prove otherwise, we'll assume they're different
    is_difference = True
    p1 = Path(path1)
    n1 = p1.load()
    #n1 = make_node(path1)

    p2 = Path(path2)
    n2 = p2.load()
    #n2 = make_node(path2)

    if n1.size == n2.size:
        #probably pretty safe to assume that they are equal
        #print " %s - BOTH, SAME SIZE" % phraseUnicode2ASCII(fname)
        #print "EQUAL sizes: %s %s" % (n1.size, n2.size)
        is_difference = False

        #could do additional checks if desired
        #enabling another diff level will take longer, but will be more accurate:
        f_a = file(path1)
        f_b = file(path2)
        a = f_a.readlines()
        b = f_b.readlines()
        diff = unified_diff(a, b)
        for d in diff:
            is_difference = True
            #print d

        #this will signal which files have differences:
        if is_difference:
            print(" %s - BOTH, DIFFERENT CONTENT" % fname.translate(unaccented_map()))
            
        #could move it somewhere else:
        #os.rename(path1, os.path.join(d1, "dupes", fname))
        #os.rename(path2, os.path.join(d2, "merged", fname))

    else:
        #print " %s - BOTH, DIFFERENT SIZE" % phraseUnicode2ASCII(fname)
        print(" %s - BOTH, DIFFERENT SIZE" % fname.translate(unaccented_map()))
        if diff_system:
            print("diffing: %s %s\n" % (path1, path2))
            try:
                #diff_system( phraseUnicode2ASCII(path1),
                #             phraseUnicode2ASCII(path2) )
                #diff_playlists(n1, n2)
                diff_system( path1.translate(unaccented_map()),
                             path2.translate(unaccented_map()) )
            except:
                print("Unable to diff.")

    return is_difference
開發者ID:charlesbrandt,項目名稱:templates,代碼行數:52,代碼來源:diff_json_and_merge.py

示例14: find_jsons

def find_jsons(item, limit_by_name=False, debug=False):
    """
    foundation for find_json
    but this returns all matches (based on parameters)
    """

    if re.search('.*\.json$', item):
        if debug:
            #print "find_and_load_json: item is a json string: %s" % item
            logging.debug("find_json: item is a json string: %s" % item)
        return [item]

    else:
        parent = ''
        name = ''
        p = Path(item)
        if p.type() == "Directory":
            #item must be a directory... just look here
            parent = p
            d = p.load()
        else:
            name = to_tag(p.name)
            #must be some other file type... load the parent directory:
            parent = p.parent()
            d = parent.load()
            if debug:
                print("%s not a directory, using: %s" % (item, parent))
            
        matches = []
        for j in d.files:
            #if debug:
            #    print "Checking: %s" % j
            if re.search('\.json$', str(j)):
                if debug:
                    print("matched json: %s" % j)

                match = os.path.join(str(parent), str(j))
                #this should allow us to hone in on one
                #if there is more than one media file in a directory
                if name and limit_by_name:
                    if re.search(name, str(j)):
                        matches.append(match)
                    else:
                        if debug:
                            print("could not find %s in %s" % (name, str(j)))
                else:
                    matches.append(match)

        if debug:
            print("Found the following: %s" % matches)

        return matches
開發者ID:charlesbrandt,項目名稱:medley,代碼行數:52,代碼來源:helpers.py

示例15: image

def image(relative=''):
    global path_root

    # if not re.match('/', relative):
    #    relative = os.path.join(path_root, relative)

    # print()"SHOWING IMAGE: %s" % relative)
    path = Path(relative, relative_prefix=path_root)
    if path.type() == "Image":
        return bottle.static_file(relative, root=path_root)
    else:
        # TODO: raise 404
        pass
開發者ID:charlesbrandt,項目名稱:sortable_list,代碼行數:13,代碼來源:application.py


注:本文中的moments.path.Path類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。