Пример #1
0
def from_configparser(filepath):
    """Have an ini file that the python configparser can understand? Pass the filepath
    to this function, and a matching Configuration will magically be returned."""

    if not os.path.exists(filepath):
        logging.error(_('configuration file not found: %(filepath)s'), {'filepath':filepath})
        return None
    if not os.path.isfile(filepath):
        logging.error(_('configuration path is not a file: %(filepath)s'), {'filepath':filepath})
        return None

    try:
        from configparser import ConfigParser
    except ImportError:
        from backport.configparser import ConfigParser
    cfgp = ConfigParser()
    with open(filepath, encoding='utf-8') as fp:
        cfgp.readfp(fp)
    dic = OrderedDict()
    for section_name in cfgp.sections():
        if 'DEFAULT' == section_name:
            section_name = ''
        for name, value in cfgp.items(section_name):
            value += ''   # inner workaround for python 2.6+
                              # transforms ascii str to unicode because
                              # of unicode_literals import
            dic[Key(section_name) + name] = value
    return Configuration.from_mapping(dic)
Пример #2
0
def from_configparser(filepath):
    """Have an ini file that the python configparser can understand? Pass the filepath
    to this function, and a matching Configuration will magically be returned."""

    if not os.path.exists(filepath):
        logging.error(_('configuration file not found: %(filepath)s'),
                      {'filepath': filepath})
        return None
    if not os.path.isfile(filepath):
        logging.error(_('configuration path is not a file: %(filepath)s'),
                      {'filepath': filepath})
        return None

    try:
        from configparser import ConfigParser
    except ImportError:
        from backport.configparser import ConfigParser
    cfgp = ConfigParser()
    with open(filepath, encoding='utf-8') as fp:
        cfgp.readfp(fp)
    dic = OrderedDict()
    for section_name in cfgp.sections():
        if 'DEFAULT' == section_name:
            section_name = ''
        for name, value in cfgp.items(section_name):
            value += ''  # inner workaround for python 2.6+
            # transforms ascii str to unicode because
            # of unicode_literals import
            dic[Key(section_name) + name] = value
    return Configuration.from_mapping(dic)
Пример #3
0
 def count_subfolders_and_files(self):
     if self.dir:
         self.subdircount = 0
         self.subfilescount = 0
         fullpath = CherryModel.abspath(self.path)
         if not os.path.isdir(fullpath):
             # not a dir, or not even there: fail gracefully.
             # There are 0 subfolders and 0 files by default.
             log.error("MusicEntry does not exist: %r", self.path)
             return
         directory_listing = os.listdir(fullpath)
         for idx, filename in enumerate(directory_listing):
             if idx > MusicEntry.MAX_SUB_FILES_ITER_COUNT:
                 # estimate remaining file count
                 self.subfilescount *= len(directory_listing) / float(idx +
                                                                      1)
                 self.subfilescount = int(self.subfilescount)
                 self.subdircount *= len(directory_listing) / float(idx + 1)
                 self.subdircount = int(self.subdircount)
                 self.subfilesestimate = True
                 return
             subfilefullpath = os.path.join(fullpath, filename)
             if os.path.isfile(subfilefullpath):
                 if CherryModel.isplayable(subfilefullpath):
                     self.subfilescount += 1
             else:
                 self.subdircount += 1
Пример #4
0
 def count_subfolders_and_files(self):
     if self.dir:
         self.subdircount = 0
         self.subfilescount = 0
         fullpath = CherryModel.abspath(self.path)
         if not os.path.isdir(fullpath):
             # not a dir, or not even there: fail gracefully.
             # There are 0 subfolders and 0 files by default.
             log.error(
                 "MusicEntry does not exist: %r", self.path)
             return
         try:
             directory_listing = os.listdir(fullpath)
         except OSError as e:
             log.e(_('Error listing directory %s: %s') % (fullpath, str(e)))
             directory_listing = []
         for idx, filename in enumerate(directory_listing):
             if idx > MusicEntry.MAX_SUB_FILES_ITER_COUNT:
                 # estimate remaining file count
                 self.subfilescount *= len(directory_listing)/float(idx+1)
                 self.subfilescount = int(self.subfilescount)
                 self.subdircount *= len(directory_listing)/float(idx+1)
                 self.subdircount = int(self.subdircount)
                 self.subfilesestimate = True
                 return
             subfilefullpath = os.path.join(fullpath, filename)
             if os.path.isfile(subfilefullpath):
                 if CherryModel.isplayable(subfilefullpath):
                     self.subfilescount += 1
             else:
                 self.subdircount += 1