Пример #1
0
    def testMakeArchive(self):
        # Part A creates a single file, archives it, then deletes it
        # Part B extracts the file again from the archive and checks the content

        base = self.tempdir

        frel = "testfile"
        fabs = opj(base, "testfile")
        rar = opj(base, "testarchive1.rar")

        # Part A:
        file_helper.create_file(fabs, "content123test")

        a = archive.Archive(rar, base)
        a.add_file(frel)

        self.assertEqual(a.run(), 0)

        self.assertEqual(file_helper.file_exists(rar), True)

        file_helper.remove_file(fabs)
        self.assertEqual(file_helper.file_exists(fabs), False)

        # Part B:
        b = archive.Archive(rar, base)
        syscode = b.extract(target_path=base)
        self.assertEqual(syscode, 0)

        self.assertEqual(file_helper.file_head(fabs), "content123test")
Пример #2
0
  def testMakeArchiveSubdir1(self):
    # test archiving a subdir with a file and extracting relative
    
    base=self.tempdir
    rar=opj(base,"testarchive1.rar")
    a = archive.Archive(rar,base)

    drel_top = "A"
    drel=opj(drel_top,"1","a")
    dabs=opj(base,drel)

    frel=opj(drel,"testfile-1")
    fabs=opj(base,frel)


    file_helper.mkdir_p(dabs)
    file_helper.create_file(fabs,"content-1")

    a.add_dir(drel_top)

    self.assertEqual(a.run(),0) 
    self.assertEqual(file_helper.file_exists(rar),True)

    file_helper.remove_file(fabs)
    self.assertEqual(file_helper.file_exists(fabs),False)
                                                                        
    # test content of archive by extracting:                                                     
    b = archive.Archive(rar,base)    
    syscode = b.extract(target_path=self.tempdir)
    self.assertEqual(syscode,0)

    self.assertEqual(file_helper.file_head(fabs),"content-1")
Пример #3
0
def api_filter():
    query_parameters = request.args  # grabs all the query parameters provided in the URL
    id = query_parameters.get('id')

    # `SELECT <columns> FROM <table> WHERE <column=match> AND <column=match>;
    query = "SELECT * FROM books WHERE"
    to_filter = []
    print_value = "Nothing happened"

    if id:
        id = int(id)
        query += ' id=? AND'
        to_filter.append(id)
        if id == 1:  # 1-> create file
            file_name = 'file1'
            file_helper.create_file(file_name)
            template_name = 'template'
            file_helper.copy_info(file_name, template_name)
            print_value = "You choose action 1 -> copy from template to a new file named: file1."
        elif id == 2:
            file_name = 'file1'
            template_name = 'template2'
            file_helper.add_info(file_name, template_name)
            print_value = "<p>You choose action 2 -> copy from template2 to the already existed file1.</p>"
        elif id == 3:
            file_name = 'file1'
            file_helper.delete_file(file_name)
            print_value = "<p>You choose action 3 -> delete file1.</p>"
    if not id:
        return page_not_found(404)

    return print_value
Пример #4
0
    def testMakeArchiveSubdir1(self):
        # test archiving a subdir with a file and extracting relative

        base = self.tempdir
        rar = opj(base, "testarchive1.rar")
        a = archive.Archive(rar, base)

        drel_top = "A"
        drel = opj(drel_top, "1", "a")
        dabs = opj(base, drel)

        frel = opj(drel, "testfile-1")
        fabs = opj(base, frel)

        file_helper.mkdir_p(dabs)
        file_helper.create_file(fabs, "content-1")

        a.add_dir(drel_top)

        self.assertEqual(a.run(), 0)
        self.assertEqual(file_helper.file_exists(rar), True)

        file_helper.remove_file(fabs)
        self.assertEqual(file_helper.file_exists(fabs), False)

        # test content of archive by extracting:
        b = archive.Archive(rar, base)
        syscode = b.extract(target_path=self.tempdir)
        self.assertEqual(syscode, 0)

        self.assertEqual(file_helper.file_head(fabs), "content-1")
Пример #5
0
  def testMakeArchive(self):
    # Part A creates a single file, archives it, then deletes it
    # Part B extracts the file again from the archive and checks the content
    
    base = self.tempdir
    
    frel = "testfile"
    fabs = opj(base,"testfile")
    rar = opj(base,"testarchive1.rar")
    
    # Part A:
    file_helper.create_file(fabs,"content123test")
    
    a = archive.Archive(rar,base)
    a.add_file(frel)

    self.assertEqual(a.run(),0)
     
    self.assertEqual(file_helper.file_exists(rar),True)

    file_helper.remove_file(fabs)
    self.assertEqual(file_helper.file_exists(fabs),False)

    # Part B:
    b = archive.Archive(rar,base)
    syscode = b.extract(target_path=base)
    self.assertEqual(syscode,0)

    self.assertEqual(file_helper.file_head(fabs),"content123test")
Пример #6
0
 def testMakeArchiveFailSubdirs(self):
   # create an archive at not existing directory
   base=self.tempdir
   aname = opj(self.tempdir,"dir/doesnt/exist","testarchive1.rar")
   a = archive.Archive(aname,base)
   file_helper.create_file(opj(self.tempdir,"testfile")) # empty file is ok
   a.add_file(opj("testfile"))
   syscode = a.run()
   # should fail, because the target dir does not exist:
   self.assertNotEqual(syscode,0)
Пример #7
0
 def testMakeArchiveFailSubdirs(self):
     # create an archive at not existing directory
     base = self.tempdir
     aname = opj(self.tempdir, "dir/doesnt/exist", "testarchive1.rar")
     a = archive.Archive(aname, base)
     file_helper.create_file(opj(self.tempdir,
                                 "testfile"))  # empty file is ok
     a.add_file(opj("testfile"))
     syscode = a.run()
     # should fail, because the target dir does not exist:
     self.assertNotEqual(syscode, 0)
Пример #8
0
  def testFileHelperFile(self):
    # create file:
    file_helper.create_file(opj(self.tempdir,"testfile"),"content123test")
    
    # test if it exists:
    self.assertEqual(file_helper.file_exists(opj(self.tempdir,"testfile")),True)

    # test its content:
    self.assertEqual(file_helper.file_head(opj(self.tempdir,"testfile")),"content123test")

    # test if it can be removed:
    file_helper.remove_file(opj(self.tempdir,"testfile"))
    self.assertEqual(file_helper.file_exists(opj(self.tempdir,"testfile")),False)
Пример #9
0
    def testFileHelperFile(self):
        # create file:
        file_helper.create_file(opj(self.tempdir, "testfile"),
                                "content123test")

        # test if it exists:
        self.assertEqual(
            file_helper.file_exists(opj(self.tempdir, "testfile")), True)

        # test its content:
        self.assertEqual(file_helper.file_head(opj(self.tempdir, "testfile")),
                         "content123test")

        # test if it can be removed:
        file_helper.remove_file(opj(self.tempdir, "testfile"))
        self.assertEqual(
            file_helper.file_exists(opj(self.tempdir, "testfile")), False)
Пример #10
0
    def testExclude(self):
        # test archiving a subdir with a file and extracting relative

        base = self.tempdir
        rar = opj(base, "testarchive1.rar")
        a = archive.Archive(rar, base)

        drel_top = "A"
        drel = opj(drel_top, "1", "a")
        dabs = opj(base, drel)
        file_helper.mkdir_p(dabs)

        frel = opj(drel, "testfile-1")
        fabs = opj(base, frel)
        file_helper.create_file(fabs, "content-2")

        frel2 = opj(drel, "testfile-2")
        fabs2 = opj(base, frel2)
        file_helper.create_file(fabs2, "content-2")

        a.add_dir(drel_top)
        a.exclude(frel)  # exclude the first file!

        self.assertEqual(a.run(), 0)
        self.assertEqual(file_helper.file_exists(rar), True)

        file_helper.remove_file(fabs)
        self.assertEqual(file_helper.file_exists(fabs), False)

        file_helper.remove_file(fabs2)
        self.assertEqual(file_helper.file_exists(fabs2), False)

        # test content of archive by extracting:
        b = archive.Archive(rar, base)
        syscode = b.extract(target_path=self.tempdir)
        self.assertEqual(syscode, 0)

        # the first file was excluded, may not exist:
        self.assertEqual(file_helper.file_exists(fabs), False)
        # the second file must be there:
        self.assertEqual(file_helper.file_head(fabs2), "content-2")
Пример #11
0
  def testExclude(self):
    # test archiving a subdir with a file and extracting relative
    
    base=self.tempdir
    rar=opj(base,"testarchive1.rar")
    a = archive.Archive(rar,base)

    drel_top = "A"
    drel=opj(drel_top,"1","a")
    dabs=opj(base,drel)
    file_helper.mkdir_p(dabs)

    frel=opj(drel,"testfile-1")
    fabs=opj(base,frel)
    file_helper.create_file(fabs,"content-2")

    frel2=opj(drel,"testfile-2")
    fabs2=opj(base,frel2)
    file_helper.create_file(fabs2,"content-2")

    a.add_dir(drel_top)
    a.exclude(frel) # exclude the first file!

    self.assertEqual(a.run(),0) 
    self.assertEqual(file_helper.file_exists(rar),True)

    file_helper.remove_file(fabs)
    self.assertEqual(file_helper.file_exists(fabs),False)

    file_helper.remove_file(fabs2)
    self.assertEqual(file_helper.file_exists(fabs2),False)
                                                                        
    # test content of archive by extracting:                                                     
    b = archive.Archive(rar,base)    
    syscode = b.extract(target_path=self.tempdir)
    self.assertEqual(syscode,0)

    # the first file was excluded, may not exist:
    self.assertEqual(file_helper.file_exists(fabs),False)
    # the second file must be there:
    self.assertEqual(file_helper.file_head(fabs2),"content-2")
Пример #12
0
  def testMakeArchiveFileInSubdir(self):
    # test archiving a file in a subdir and extracting relative

    base = self.tempdir
    rar = opj(base,"testarchive1.rar")

    # Part A:
    a = archive.Archive(rar,base)

    
    drel = opj("A","1","a") # relative path to directory
    dabs = opj(base,drel) # absolute
    frel = opj("A","1","a","testfile-1")
    fabs = opj(base,frel)

    file_helper.mkdir_p(dabs)
    file_helper.create_file(fabs,"content-1")
    self.assertEqual(file_helper.file_exists(fabs),True)

    a.add_file(frel) # must add relative paths

    # run and check if rar archive exists:
    
    self.assertEqual(a.run(),0)
    self.assertEqual(file_helper.file_exists(rar),True)

    # remove original file and check:
    file_helper.remove_file(fabs)
    self.assertEqual(file_helper.file_exists(fabs),False)
                                                                        
    # Part B:                                                     
    b = archive.Archive(rar,base)          
    syscode = b.extract(target_path=base)
    self.assertEqual(syscode,0)

    self.assertEqual(file_helper.file_exists(fabs),True)
    
    # TODO: check content:
    self.assertEqual(file_helper.file_head(fabs),"content-1")
Пример #13
0
    def testMakeArchiveFileInSubdir(self):
        # test archiving a file in a subdir and extracting relative

        base = self.tempdir
        rar = opj(base, "testarchive1.rar")

        # Part A:
        a = archive.Archive(rar, base)

        drel = opj("A", "1", "a")  # relative path to directory
        dabs = opj(base, drel)  # absolute
        frel = opj("A", "1", "a", "testfile-1")
        fabs = opj(base, frel)

        file_helper.mkdir_p(dabs)
        file_helper.create_file(fabs, "content-1")
        self.assertEqual(file_helper.file_exists(fabs), True)

        a.add_file(frel)  # must add relative paths

        # run and check if rar archive exists:

        self.assertEqual(a.run(), 0)
        self.assertEqual(file_helper.file_exists(rar), True)

        # remove original file and check:
        file_helper.remove_file(fabs)
        self.assertEqual(file_helper.file_exists(fabs), False)

        # Part B:
        b = archive.Archive(rar, base)
        syscode = b.extract(target_path=base)
        self.assertEqual(syscode, 0)

        self.assertEqual(file_helper.file_exists(fabs), True)

        # TODO: check content:
        self.assertEqual(file_helper.file_head(fabs), "content-1")
Пример #14
0
  def testMakeArchiveTreeAndFile(self):
    # test archiving files in a tree and extracting relative
    # method to communcate tree: add 1 file and 1 base dir of a tree
    
    # Part A creates a single file, archives it, then deletes it
    # Part B extracts the file again from the archive and checks the content
    
    base = self.tempdir 
    
    rar = opj(base,"testarchive1.rar")



    drel_top = "A"
    drel=opj(drel_top,"1","a")
    dabs = opj(base,drel)
    frel=opj(drel_top,"1","a","testfile-1")
    fabs=opj(base,frel)
    
    drel_topB = "B"

    drel2=opj(drel_topB,"1","a")
    dabs2 = opj(base,drel2)
    frel2=opj(drel_topB,"1","a","testfile-")
    fabs2=opj(base,frel2)
    
    drel3=opj(drel_topB,"2","a")
    dabs3 = opj(base,drel3)
    frel3=opj(drel_topB,"2","a","testfile-")
    fabs3=opj(base,frel3)

    # Part A:
    
    a = archive.Archive(rar,base)

    file_helper.mkdir_p(dabs)
    file_helper.create_file(fabs,"content-1")

    a.add_file(frel)

    file_helper.mkdir_p(dabs2)
    file_helper.create_file(fabs2,"content-2")

    file_helper.mkdir_p(dabs3)
    file_helper.create_file(fabs3,"content-3")

    a.add_dir(drel_topB)

    self.assertEqual(a.run(),0) 
    self.assertEqual(file_helper.file_exists(rar),True)

    file_helper.remove_file(fabs)
    file_helper.remove_file(fabs2)
    file_helper.remove_file(fabs3)

    self.assertEqual(file_helper.file_exists(fabs),False)
    self.assertEqual(file_helper.file_exists(fabs2),False)
    self.assertEqual(file_helper.file_exists(fabs3),False)
                                                                        
    # Part B:                                                     
    b = archive.Archive(rar,base)          
    syscode = b.extract(target_path=base)
    self.assertEqual(syscode,0)

    self.assertEqual(file_helper.file_head(fabs),"content-1")
    self.assertEqual(file_helper.file_head(fabs2),"content-2")
    self.assertEqual(file_helper.file_head(fabs3),"content-3")
Пример #15
0
 def test_empty_folder(self):
     file_helper.create_file(os.path.join(self.test_path, 'to_be_removed.txt'))
     file_helper.empty_folder(self.test_path)
     assert(len(os.listdir(self.test_path)) == 0)
Пример #16
0
    def testMakeArchiveTreeAndFile(self):
        # test archiving files in a tree and extracting relative
        # method to communcate tree: add 1 file and 1 base dir of a tree

        # Part A creates a single file, archives it, then deletes it
        # Part B extracts the file again from the archive and checks the content

        base = self.tempdir

        rar = opj(base, "testarchive1.rar")

        drel_top = "A"
        drel = opj(drel_top, "1", "a")
        dabs = opj(base, drel)
        frel = opj(drel_top, "1", "a", "testfile-1")
        fabs = opj(base, frel)

        drel_topB = "B"

        drel2 = opj(drel_topB, "1", "a")
        dabs2 = opj(base, drel2)
        frel2 = opj(drel_topB, "1", "a", "testfile-")
        fabs2 = opj(base, frel2)

        drel3 = opj(drel_topB, "2", "a")
        dabs3 = opj(base, drel3)
        frel3 = opj(drel_topB, "2", "a", "testfile-")
        fabs3 = opj(base, frel3)

        # Part A:

        a = archive.Archive(rar, base)

        file_helper.mkdir_p(dabs)
        file_helper.create_file(fabs, "content-1")

        a.add_file(frel)

        file_helper.mkdir_p(dabs2)
        file_helper.create_file(fabs2, "content-2")

        file_helper.mkdir_p(dabs3)
        file_helper.create_file(fabs3, "content-3")

        a.add_dir(drel_topB)

        self.assertEqual(a.run(), 0)
        self.assertEqual(file_helper.file_exists(rar), True)

        file_helper.remove_file(fabs)
        file_helper.remove_file(fabs2)
        file_helper.remove_file(fabs3)

        self.assertEqual(file_helper.file_exists(fabs), False)
        self.assertEqual(file_helper.file_exists(fabs2), False)
        self.assertEqual(file_helper.file_exists(fabs3), False)

        # Part B:
        b = archive.Archive(rar, base)
        syscode = b.extract(target_path=base)
        self.assertEqual(syscode, 0)

        self.assertEqual(file_helper.file_head(fabs), "content-1")
        self.assertEqual(file_helper.file_head(fabs2), "content-2")
        self.assertEqual(file_helper.file_head(fabs3), "content-3")
Пример #17
0
import file_helper

# ..................MAIN.......................
choice = int(input(
    "Please choose 1 - 2 - 3: "))  # int()->because input() returns a string
print(f'You choose {choice} !')
# 1-> create file
if choice == 1:
    file_name = input("Please add file name: ")
    file_helper.create_file(file_name)
    template_name = input("Please add template name: ")
    file_helper.copy_info(file_name, template_name)
# 2-> modify file
elif choice == 2:
    file_name = input("Please add file name: ")
    template_name = input("Please add template name: ")
    file_helper.add_info(file_name, template_name)
# 3->delete file
elif choice == 3:
    file_name = input("Please add file name: ")
    file_helper.delete_file(file_name)
else:
    print("Not a valid value")
Пример #18
0
 def test_create_file(self):
     file_helper.create_file(os.path.join(self.test_path, 'newly_created.txt'))
     assert(len(os.listdir(self.test_path)) == 1)