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


Python Path.to_relative方法代碼示例

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


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

示例1: walk_logs

# 需要導入模塊: from moments.path import Path [as 別名]
# 或者: from moments.path.Path import to_relative [as 別名]
def walk_logs(source, destination='/c/journal/', add_tags=[], subtract_tags=[],
              include_all_path_tags=False, include_some_path_tags=True):
    """
    walk the given path and
    load a journal object for each log encountered in the path
    then split it up using split_logs function

    based on moments.journal.load_journal

    """

    #ignore_items = [ 'downloads', 'index.txt' ]
    ignore_items = [ ]
    log_check = re.compile('.*\.txt$')
    if os.path.isdir(source):
        for root,dirs,files in os.walk(source):
            for f in files:
                current_file = os.path.join(root, f)
                
                #make sure it is a log file (.txt):
                if not log_check.search(f):
                    continue

                if not check_ignore(current_file, ignore_items):
                    these_tags = add_tags[:]
                    filename_tags = []
                    if include_all_path_tags:
                        filename_tags = Path(current_file).to_tags()
                    elif include_some_path_tags:
                        #rather than include all tags from path
                        #check relative path only
                        #include those tags that are not a date tag
                        #otherwise ok to skip
                        full_path = Path(current_file)
                        relative_path = full_path.to_relative(source)
                        filename_tags = Path(os.path.join('/', relative_path)).to_tags()
                        filename_tags = omit_date_tags(filename_tags)
                        #typically this is not really the appropriate tag.
                        #used more as a generic file
                        #for history in a given context.  
                        if 'journal' in filename_tags:
                            filename_tags.remove('journal')

                    these_tags.extend(filename_tags)

                    #subtract tags last:
                    for tag in subtract_tags:
                        if tag in these_tags:
                            these_tags.remove(tag)

                    print("add_tags: %s" % these_tags)
                            
                    split_log(current_file, add_tags=these_tags, destination=destination)

    else:
        print("pass in a directory")
開發者ID:charlesbrandt,項目名稱:mindstream,代碼行數:58,代碼來源:split_logs_to_days.py

示例2: copy_media

# 需要導入模塊: from moments.path import Path [as 別名]
# 或者: from moments.path.Path import to_relative [as 別名]
def copy_media(source, source_root, destination_root):
    m3u = M3U(source)

    total_size = 0
    total_items = 0
    for item in m3u:
        if re.match(source_root, item):
            p = Path(item)
            relative = p.to_relative(source_root)
            sparent = p.parent()
            destination = os.path.join(destination_root, relative)
            dpath = Path(destination)
            dparent = dpath.parent()
            print(relative)
            print(sparent)
            print(destination)

            if not os.path.exists(str(dparent)):
                os.makedirs(str(dparent))

            if not os.path.exists(destination):
                p.copy(destination)
            else:
                print("already have: %s" % destination)

            for option in os.listdir(str(sparent)):
                soption = os.path.join(str(sparent), option)
                spath = Path(soption)
                print(spath.type())
                if spath.type() != "Movie" and spath.type() != "Directory":
                    doption = os.path.join(str(dparent), option)
                    if not os.path.exists(doption):
                        print("copy here: %s, to %s" % (soption, doption))
                        shutil.copy(soption, doption)
                    
                

            print()
開發者ID:charlesbrandt,項目名稱:medley,代碼行數:40,代碼來源:copy_playlist_media.py

示例3: rescan

# 需要導入模塊: from moments.path import Path [as 別名]
# 或者: from moments.path.Path import to_relative [as 別名]
    def rescan(self, ignores=[], debug=False):
        """
        look for all json files that describe the content items
        these should have been generated externally (e.g. during scrape)
        
        json files should contain the main attributes that a Content object has
        the rest will be kept in a remainder

        parsing html and generating json summaries of content
        is beyond the scope of this application
        and should be kept outside of this code base (too specific to content)
        """

        if not self.root:
            raise ValueError("Cannot rescan. No root set on collection: %s" % self.root)
        
        #clear out anything else
        del self[:]

        if debug:
            print("walking directory for contents: %s" % self.root)

        json_check = re.compile('.*\.json$')

        #it might be inefficient to try to define these here...
        #too many different names that might work in different contexts
        #ignores = ["contents", "collection", "incompletes"]
        #can pass them in if needed...
        
        self_root_path = Path(self.root)
        parent = self_root_path.parent()
        if not os.path.isdir(self.root):
            print("Looking for path of root: %s" % self.root)
            print("(is the drive mounted???)")
            self.root = os.path.dirname(self.root)
            #if we still don't have a directory, something is wrong with root
            assert os.path.isdir(self.root)

        
        #instead of looking for ignores
        #will limit by convention
        #top level directory should only contain meta jsons
        #(that should be ignored as content data)
        #content jsons will always be in a subdirectory
        #similarly, meta jsons should never be in a subdirectory

        #for root,dirs,files in os.walk(self.root):
        subdirs = self_root_path.load().directories
        for subdir in subdirs:
            if check_ignore(str(subdir), ignores):
                print("Ignoring directory: %s" % (subdir))
            else:
                for root,dirs,files in os.walk(str(subdir)):
                    for f in files:

                        #if json_check.search(f):
                        if json_check.search(f) and not check_ignore(f, ignores):
                            json_file = os.path.join(root, f)
                            p_root = Path(root)
                            relative_root = p_root.to_relative(str(parent))
                            #get rid of leading slash
                            if re.match('/', relative_root):
                                relative_root = relative_root[1:]
                            if debug:
                                print("loading content from: %s" % json_file)
                            #c = Content(json_file, root=relative_root)
                            c = Content(json_file)
                            if debug:
                                print("setting base_dir to: %s" % relative_root)

                            #if updating one here, should update the other:
                            c.base_dir = relative_root
                            c.drive_dir = str(parent)

                            self.append(c)

        if debug:
            print("Finished loading %s contents manually" % (len(self)))
開發者ID:charlesbrandt,項目名稱:medley,代碼行數:80,代碼來源:collector.py


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