mv all filemethdos to file_ethods.py
This commit is contained in:
@@ -1,21 +1,20 @@
|
||||
__author__ = 'dev'
|
||||
|
||||
from utilities.jsonfile import JsonFile
|
||||
from utilities.feedfile import FeedFile
|
||||
from config.config import Config
|
||||
from utilities.podcastserializer import PodcastSerializer
|
||||
import os
|
||||
import feedparser
|
||||
import os
|
||||
import utilities.file_methods as fm
|
||||
|
||||
|
||||
class Feed:
|
||||
|
||||
def __init__(self, feed_name=None, feed_type=None, link_type=None, feed_url=None):
|
||||
self.config_path = None
|
||||
self.feed_folder_path = None
|
||||
self.feed_config = None
|
||||
self.feed_file_path = None
|
||||
self.serializer = None
|
||||
self.feed_folder = None
|
||||
self.setup_feed(feed_name, feed_type, link_type, feed_url)
|
||||
|
||||
def load_feed(self):
|
||||
@@ -25,22 +24,20 @@ class Feed:
|
||||
def load_new_feed(self):
|
||||
assert (not os.path.exists(self.feed_file_path))
|
||||
assert (self.serializer is not None)
|
||||
feed_file = FeedFile(self.feed_file_path)
|
||||
feed_file.write_feed_json_file(self.load_feed(), self.serializer)
|
||||
fm.write_feed_json_file(self.feed_file_path, self.load_feed(), self.serializer)
|
||||
|
||||
def load_existing_feed(self):
|
||||
|
||||
assert (os.path.exists(self.feed_file_path))
|
||||
assert (self.serializer is not None)
|
||||
|
||||
tmp_feed_file = FeedFile(self.feed_file_path + "tmp")
|
||||
tmp_feed_file.write_feed_json_file(self.load_feed(), self.serializer)
|
||||
tmp_feed_file_path = self.feed_file_path + "tmp"
|
||||
fm.write_feed_json_file(tmp_feed_file_path, self.load_feed(), self.serializer)
|
||||
tmp_free_json = fm.read_json_file(tmp_feed_file_path)
|
||||
|
||||
feed_file = FeedFile(self.feed_file_path)
|
||||
|
||||
if tmp_feed_file.read_feed_file() == feed_file.read_feed_file():
|
||||
feed_file.write_json_file(self.feed_file_path)
|
||||
tmp_feed_file.check_and_remove_file()
|
||||
if tmp_free_json != fm.read_json_file(self.feed_file_path):
|
||||
fm.write_json_file(self.feed_file_path, tmp_free_json)
|
||||
fm.check_and_remove_file(tmp_feed_file_path)
|
||||
|
||||
def get_serializer(self):
|
||||
return {
|
||||
@@ -53,20 +50,20 @@ class Feed:
|
||||
assert link_type is not None
|
||||
assert feed_url is not None
|
||||
|
||||
self.feed_config = self.create_feed_config_file(feed_name, feed_type, link_type, feed_url)
|
||||
self.feed_config = Feed.create_feed_config_file(feed_name, feed_type, link_type, feed_url)
|
||||
self.config_path = self.create_feed_config_path()
|
||||
if os.path.exists(self.config_path) is False:
|
||||
self.write_feed_config_file()
|
||||
|
||||
self.feed_file_path = self.feed_folder_path+"/"+self.feed_config["FeedName"]+".json"
|
||||
self.feed_file_path = self.feed_folder_path + "/" + self.feed_config["FeedName"] + ".json"
|
||||
self.serializer = self.get_serializer()
|
||||
|
||||
def new_feed_folder(self):
|
||||
|
||||
if self.feed_folder_path is None:
|
||||
assert self.feed_config is not None
|
||||
feed_folder = Config.get("FeedStorageFolder")
|
||||
assert feed_folder is not None
|
||||
self.feed_folder = Config.get("FeedStorageFolder")
|
||||
assert self.feed_folder is not None
|
||||
feed_folder_prefix = Config.get("FeedStoragePrefix")
|
||||
assert feed_folder_prefix is not None
|
||||
|
||||
@@ -76,17 +73,27 @@ class Feed:
|
||||
tmp_value = self.feed_config.get(value)
|
||||
if tmp_value is not None:
|
||||
value = tmp_value
|
||||
feed_folder += "/" + value
|
||||
self.feed_folder += "/" + value
|
||||
else:
|
||||
break
|
||||
|
||||
self.feed_folder_path = feed_folder
|
||||
self.feed_folder_path = self.feed_folder
|
||||
if not os.path.exists(self.feed_folder_path):
|
||||
os.mkdir(feed_folder)
|
||||
os.mkdir(self.feed_folder)
|
||||
|
||||
return self.feed_folder_path
|
||||
|
||||
def create_feed_config_file(self, feed_name, feed_type, link_type, feed_url):
|
||||
def create_feed_config_path(self):
|
||||
if self.config_path is None:
|
||||
self.config_path = self.new_feed_folder() + "/" + self.feed_config["FeedName"] + Config[
|
||||
"ConfigFileSuffix"] + Config["ConfigFileExtension"]
|
||||
return self.config_path
|
||||
|
||||
def write_feed_config_file(self):
|
||||
fm.write_json_file(self.create_feed_config_path(), self.feed_config)
|
||||
|
||||
@staticmethod
|
||||
def create_feed_config_file(feed_name, feed_type, link_type, feed_url):
|
||||
assert feed_name is not None
|
||||
assert feed_type is not None
|
||||
assert link_type is not None
|
||||
@@ -99,13 +106,4 @@ class Feed:
|
||||
"FeedUrl": feed_url,
|
||||
"LinkType": link_type
|
||||
}
|
||||
return feed_config
|
||||
|
||||
def create_feed_config_path(self):
|
||||
if self.config_path is None:
|
||||
self.config_path = self.new_feed_folder() + "/" + self.feed_config["FeedName"] + Config["ConfigFileSuffix"] + Config["ConfigFileExtension"]
|
||||
return self.config_path
|
||||
|
||||
def write_feed_config_file(self):
|
||||
file = JsonFile(self.create_feed_config_path())
|
||||
file.write_json_file(self.feed_config)
|
||||
return feed_config
|
||||
@@ -1,36 +0,0 @@
|
||||
__author__ = 'stubbfel'
|
||||
|
||||
import feedparser
|
||||
from utilities.jsonfile import JsonFile
|
||||
from utilities.serializer import Serializer
|
||||
|
||||
class FeedFile(JsonFile):
|
||||
|
||||
def __init__(self, filename):
|
||||
"""
|
||||
init class
|
||||
:param filename: path to the file
|
||||
"""
|
||||
super(FeedFile, self).__init__(filename)
|
||||
|
||||
def read_feed_file(self):
|
||||
"""
|
||||
method read and convert the content of the file to a FeedParserDict
|
||||
:return: dict - feed object
|
||||
"""
|
||||
feed_str = self.read_text_file()
|
||||
return feedparser.parse(feed_str)
|
||||
|
||||
def write_feed_json_file(self, feed, serializer=None):
|
||||
"""
|
||||
method write a feedparpser object to a text file. It use a certain serializer or the default serializer
|
||||
if type of the serializer is none
|
||||
:param feed: expetc FeedparserDict
|
||||
:param serializer: None or a subclass of Serializer
|
||||
"""
|
||||
assert isinstance(feed, feedparser.FeedParserDict)
|
||||
|
||||
if serializer is None:
|
||||
self.write_json_file(feed)
|
||||
else:
|
||||
self.write_json_file(serializer.serialize(feed))
|
||||
90
src/utilities/file_methods.py
Normal file
90
src/utilities/file_methods.py
Normal file
@@ -0,0 +1,90 @@
|
||||
__author__ = 'dev'
|
||||
|
||||
import feedparser
|
||||
import os.path
|
||||
import json
|
||||
|
||||
|
||||
def exists_file(file_path):
|
||||
"""
|
||||
check if this file exists or not
|
||||
:return: true if the file exists, otherwise false
|
||||
"""
|
||||
return os.path.isfile(file_path)
|
||||
|
||||
|
||||
def check_and_remove_file(file_path):
|
||||
"""
|
||||
Method remove this file, if the file don't exists the method don't call remove
|
||||
"""
|
||||
if exists_file(file_path):
|
||||
os.remove(file_path)
|
||||
|
||||
|
||||
def read_text_file(file_path):
|
||||
"""
|
||||
method return and read the content of a text file
|
||||
:return: str - file content
|
||||
"""
|
||||
with open(file_path, newline='\n') as file:
|
||||
return file.read()
|
||||
|
||||
|
||||
def read_json_file(file_path):
|
||||
"""
|
||||
method read and convert the content of the file to a json object
|
||||
:return: dict - json object
|
||||
"""
|
||||
return json.loads(read_text_file(file_path))
|
||||
|
||||
|
||||
def read_feed_file(file_path):
|
||||
"""
|
||||
method read and convert the content of the file to a FeedParserDict
|
||||
:return: dict - feed object
|
||||
"""
|
||||
feed_str = read_text_file(file_path)
|
||||
return feedparser.parse(feed_str)
|
||||
|
||||
|
||||
def write_text_file(file_path, file_content):
|
||||
"""
|
||||
write strings to a text file
|
||||
:param file_content: strings which has to been write i a text file
|
||||
:raise ValueError: will be raised, when the content is not a string
|
||||
"""
|
||||
assert isinstance(file_path, str)
|
||||
with open(file_path, 'w', newline='\n') as file:
|
||||
file.write(file_content)
|
||||
file.close()
|
||||
|
||||
|
||||
def write_json_file(file_path, json_object):
|
||||
"""
|
||||
write a json object to a text file
|
||||
:param json_object: json object, which has to be saved. If the object is a string,
|
||||
the method will be check if its a valid json object
|
||||
:raise ValueError: will be raised if json_object is not a dictionary or string or the json object is not valid
|
||||
"""
|
||||
valid_types = (dict, str)
|
||||
assert isinstance(json_object, valid_types)
|
||||
if isinstance(json_object, str):
|
||||
json_object = json.loads(json_object)
|
||||
|
||||
json_string = json.dumps(json_object, indent=4)
|
||||
write_text_file(file_path, json_string)
|
||||
|
||||
|
||||
def write_feed_json_file(file_path, feed, serializer=None):
|
||||
"""
|
||||
method write a feedparpser object to a text file. It use a certain serializer or the default serializer
|
||||
if type of the serializer is none
|
||||
:param feed: expetc FeedparserDict
|
||||
:param serializer: None or a subclass of Serializer
|
||||
"""
|
||||
assert isinstance(feed, feedparser.FeedParserDict)
|
||||
|
||||
if serializer is None:
|
||||
write_json_file(file_path, feed)
|
||||
else:
|
||||
write_json_file(file_path, serializer.serialize(feed))
|
||||
@@ -1,36 +0,0 @@
|
||||
__author__ = 'stubbfel'
|
||||
import json
|
||||
from utilities.textfile import TextFile
|
||||
|
||||
|
||||
class JsonFile(TextFile):
|
||||
|
||||
def __init__(self, filename):
|
||||
"""
|
||||
init class
|
||||
:param filename: path to the file
|
||||
"""
|
||||
super(JsonFile, self).__init__(filename)
|
||||
|
||||
def write_json_file(self, json_object):
|
||||
"""
|
||||
write a json object to a text file
|
||||
:param json_object: json object, which has to be saved. If the object is a string,
|
||||
the method will be check if its a valid json object
|
||||
:raise ValueError: will be raised if json_object is not a dictionary or string or the json object is not valid
|
||||
"""
|
||||
valid_types = (dict, str)
|
||||
assert isinstance(json_object, valid_types)
|
||||
if isinstance(json_object, str):
|
||||
json_object = json.loads(json_object)
|
||||
|
||||
json_string = json.dumps(json_object, indent=4)
|
||||
self.write_text_file(json_string)
|
||||
|
||||
def read_json_file(self):
|
||||
"""
|
||||
method read and convert the content of the file to a json object
|
||||
:return: dict - json object
|
||||
"""
|
||||
return json.loads(self.read_text_file())
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
__author__ = 'stubbfel'
|
||||
import os.path
|
||||
|
||||
|
||||
class TextFile:
|
||||
|
||||
def __init__(self, filename):
|
||||
"""
|
||||
init the class and set the filename
|
||||
:param filename:
|
||||
"""
|
||||
assert isinstance(filename, str)
|
||||
self._filename = filename
|
||||
|
||||
def write_text_file(self, file_content):
|
||||
"""
|
||||
write strings to a text file
|
||||
:param file_content: strings which has to been write i a text file
|
||||
:raise ValueError: will be raised, when the content is not a string
|
||||
"""
|
||||
assert isinstance(self._filename, str)
|
||||
with open(self._filename, 'w', newline='\n') as file:
|
||||
file.write(file_content)
|
||||
file.close()
|
||||
|
||||
def check_and_remove_file(self):
|
||||
"""
|
||||
Method remove this file, if the file don't exists the method don't call remove
|
||||
"""
|
||||
if self.exists_file:
|
||||
os.remove(self._filename)
|
||||
|
||||
def read_text_file(self):
|
||||
"""
|
||||
method return and read the content of a text file
|
||||
|
||||
:return: str - file content
|
||||
"""
|
||||
with open(self._filename, newline='\n') as file:
|
||||
return file.read()
|
||||
|
||||
@property
|
||||
def exists_file(self):
|
||||
"""
|
||||
check if this file exists or not
|
||||
:return: true if the file exists, otherwise false
|
||||
"""
|
||||
return os.path.isfile(self._filename)
|
||||
|
||||
@property
|
||||
def get_filename(self):
|
||||
"""
|
||||
get the file Name
|
||||
:return: str
|
||||
"""
|
||||
assert isinstance(self._filename, str)
|
||||
return self._filename
|
||||
Reference in New Issue
Block a user