mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-12-23 01:42:16 +03:00
file management is updated
This commit is contained in:
parent
de0b98cbac
commit
f9bbda0582
2 changed files with 25 additions and 8 deletions
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
from base.plugin.abstract_plugin import AbstractPlugin
|
from base.plugin.abstract_plugin import AbstractPlugin
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class GetFileContent(AbstractPlugin):
|
class GetFileContent(AbstractPlugin):
|
||||||
|
@ -20,6 +21,12 @@ class GetFileContent(AbstractPlugin):
|
||||||
file_content = ""
|
file_content = ""
|
||||||
is_file_exists = False
|
is_file_exists = False
|
||||||
|
|
||||||
|
if os.path.isdir(file_path):
|
||||||
|
self.context.create_response(code=self.message_code.TASK_ERROR.value,
|
||||||
|
message='Dosya yolu bir dizin olamaz.',
|
||||||
|
content_type=self.get_content_type().APPLICATION_JSON.value)
|
||||||
|
return
|
||||||
|
|
||||||
if self.is_exist(file_path):
|
if self.is_exist(file_path):
|
||||||
self.logger.info("File exists: " + file_path)
|
self.logger.info("File exists: " + file_path)
|
||||||
is_file_exists = True
|
is_file_exists = True
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Author: Hasan Kara <hasan.kara@pardus.org.tr>
|
# Author: Hasan Kara <hasan.kara@pardus.org.tr>
|
||||||
|
|
||||||
|
import os
|
||||||
from base.plugin.abstract_plugin import AbstractPlugin
|
from base.plugin.abstract_plugin import AbstractPlugin
|
||||||
|
|
||||||
class WriteToFile(AbstractPlugin):
|
class WriteToFile(AbstractPlugin):
|
||||||
|
@ -17,29 +18,38 @@ class WriteToFile(AbstractPlugin):
|
||||||
file_path = self.data['file-path']
|
file_path = self.data['file-path']
|
||||||
file_content = self.data['file-content']
|
file_content = self.data['file-content']
|
||||||
|
|
||||||
|
if os.path.isdir(file_path):
|
||||||
|
self.context.create_response(code=self.message_code.TASK_ERROR.value,
|
||||||
|
message='Belirtilen yol bir dizin, dosya yolu olmalı.',
|
||||||
|
content_type=self.get_content_type().APPLICATION_JSON.value)
|
||||||
|
return
|
||||||
|
|
||||||
if self.is_exist(file_path):
|
if self.is_exist(file_path):
|
||||||
self.write_file(file_path, file_content)
|
self.write_file(file_path, file_content)
|
||||||
else:
|
else:
|
||||||
path_str = ""
|
path_str = ""
|
||||||
for idx, folder in enumerate(file_path.split("/")):
|
for idx, folder in enumerate(file_path.split("/")[:-1]):
|
||||||
if idx != len(file_path.split("/")) - 1:
|
path_str += folder + "/"
|
||||||
path_str += folder + "/"
|
|
||||||
(result_code, p_out, p_err) = self.execute("mkdir -p /" + path_str)
|
(result_code, p_out, p_err) = self.execute("mkdir -p /" + path_str)
|
||||||
|
|
||||||
if result_code == 0:
|
if result_code == 0:
|
||||||
self.logger.error('Folders are created')
|
self.logger.info('Folders are created')
|
||||||
else:
|
else:
|
||||||
self.logger.error('Error occured while creating folders.')
|
self.logger.error('Error occurred while creating folders.')
|
||||||
|
self.context.create_response(code=self.message_code.TASK_ERROR.value,
|
||||||
|
message='Klasörler oluşturulurken hata oluştu.',
|
||||||
|
content_type=self.get_content_type().APPLICATION_JSON.value)
|
||||||
|
return
|
||||||
self.write_file(file_path, file_content)
|
self.write_file(file_path, file_content)
|
||||||
|
|
||||||
self.context.create_response(code=self.message_code.TASK_PROCESSED.value,
|
self.context.create_response(code=self.message_code.TASK_PROCESSED.value,
|
||||||
message='İçerik dosyaya başarıyla yazıldı..',
|
message='İçerik dosyaya başarıyla yazıldı.',
|
||||||
content_type=self.get_content_type().APPLICATION_JSON.value)
|
content_type=self.get_content_type().APPLICATION_JSON.value)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(str(e))
|
self.logger.error(str(e))
|
||||||
self.context.create_response(code=self.message_code.TASK_ERROR.value,
|
self.context.create_response(code=self.message_code.TASK_ERROR.value,
|
||||||
message='İçerik dosyaya yazılırken hata oluştu: {0}'.format(str(e)))
|
message='İçerik dosyaya yazılırken hata oluştu: {0}'.format(str(e)),
|
||||||
|
content_type=self.get_content_type().APPLICATION_JSON.value)
|
||||||
|
|
||||||
def handle_task(task, context):
|
def handle_task(task, context):
|
||||||
plugin = WriteToFile(task, context)
|
plugin = WriteToFile(task, context)
|
||||||
|
|
Loading…
Reference in a new issue