当前位置: 首页>>代码示例>>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;未经允许,请勿转载。