From b7d217ba41043e07cffd73024754dc4ed07908d4 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Sun, 21 Aug 2011 19:30:34 +0300 Subject: [PATCH] ppf: Move find_numeric_subfolder function outside class. --- ppf/new/storage.py | 53 +++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/ppf/new/storage.py b/ppf/new/storage.py index 997db03..4cf9e43 100644 --- a/ppf/new/storage.py +++ b/ppf/new/storage.py @@ -20,7 +20,7 @@ logger.setLevel(logging.DEBUG) # Create console handler and set level to ERROR. ch = logging.StreamHandler() -ch.setLevel(logging.ERROR) +ch.setLevel(logging.DEBUG) # Create formatter. formatter = logging.Formatter('%(filename)s:%(lineno)s - %(levelname)s: %(message)s') @@ -196,7 +196,7 @@ class VerboseMessage(object): self.length = length self.listen_port = listen_port -class SwarmDataAccess(): +class SwarmDataAccess(object): def __init__(self): pass @@ -264,33 +264,32 @@ class FileAccess(SwarmDataAccess): def __init__(self, path): self.base_path = path +def find_last_numeric_subfolder(path): + """ + Find last numeric folder in base_path folder. + The last numeric folder is the last swarm_id. + """ + dir_list = [] + pattern = re.compile("[0-9]+") + + # Browse entries in base_path folder. + listing = os.listdir(path) + for entry in listing: + # If directory name is a number (id) add it to the list. + if os.path.isdir(os.path.join(path, entry)): + logger.debug("folder entry: %s" %(entry)) + if pattern.match(entry): + dir_list.append(int(entry)) + + if not dir_list: + return None + else: + dir_list.sort() + return dir_list[len(dir_list)-1] + class TreeTextFileAccess(FileAccess): def __init__(self, path): - super(TextTreeFileAccess, self).__init(path) - # TODO: Check exception. - os.mkdir(self.base_path) - - def find_last_folder_id(self): - """ - Find last numeric folder in base_path folder. - The last numeric folder is the last swarm_id. - """ - dir_list = [] - pattern = re.compile("[0-9]+") - - # Browse entries in base_path folder. - listing = os.listdir(self.base_path) - for entry in listing: - # If directory name is a number (id) add it to the list. - if os.path.isdir(entry): - if pattern.match(entry): - dir_list.append(int(entry)) - - if dir_list.empty(): - return None - else: - dir_list.sort() - return dir_list[len(dir_list)-1] + super(TreeTextFileAccess, self).__init__(path) def add_swarm(self, swarm): f = open(os.path.join(self.base_path, "swarm.conf")) -- 2.20.1