def setDescr(p_filename, p_setDescrToThis): """ :param p_filename: name/path of the file :type p_filename: string :param p_setDescrToThis: description metadata will be set to this :type p_setDescrToThis: string :return: True if operation was successful :rtype: bool """ if p_setDescrToThis == "": try: MetadataManagerL0.wipeDescr(p_filename) except Exception as e: print("MetadataManager2.setDescr() error: ", e) return False else: try: MetadataManagerL0.setDescr(p_filename, p_setDescrToThis) except Exception as e: print("MetadataManager2.setDescr() error: ", e) return False try: MetadataManagerL1.placeMark(p_filename) return True except Exception as e: print("MetadataManager2.setDescr() Mark error: ", e) return False
def setTitle(p_filename, p_setTitleToThis): """ :param p_filename: name/path of the file :type p_filename: string :param p_setTitleToThis: title we will store as title metadata :type p_setTitleToThis: string :return: True if operation was successful :rtype: bool """ if p_setTitleToThis == "": try: MetadataManagerL0.wipeTitle(p_filename) except Exception as e: print("MetadataManager2.setTitle() error: ", e) return False else: try: MetadataManagerL0.setTitle(p_filename, p_setTitleToThis) except Exception as e: print("MetadataManager2.setTitle() error: ", e) return False try: MetadataManagerL1.placeMark(p_filename) return True except Exception as e: print("MetadataManager2.setTitle() Mark error: ", e) return False
def tagUseageCheck(p_filename): f_metadata = pyexiv2.ImageMetadata(p_filename) f_metadata.read() print(f_metadata.exif_keys) f_keywords = f_metadata['Exif.Image.XPKeywords'] f_dirtyTagString = pyexiv2.utils.undefined_to_string(f_keywords.value) print("tagUseageCheck() f_dirtyTagString\t\t", f_dirtyTagString) f_cleanTagList = MetadataManagerL0.dirtyStr2cleanList(f_dirtyTagString) print("tagUseageCheck() f_cleanTagList\t\t", f_cleanTagList) f_dirtyTagString2 = MetadataManagerL0.cleanList2dirtyStr(f_cleanTagList) print("tagUseageCheck() f_dirtyTagString2\t\t", f_dirtyTagString2) return
def test_removeArtistResults(self): releaseAllClones(g_clonenames) f_value = "publisher: twitter" f_expected = ["stockphotographer"] f_filename = singleClone(g_files["fixingComputer.jpg"].fullname) MetadataManagerL1.removeArtist(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getArtists(f_filename)) os.remove(f_filename) f_value = "photographer: idunno" f_expected = [] f_filename = singleClone(g_files["catScreamPizza.jpg"].fullname) MetadataManagerL1.removeArtist(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getArtists(f_filename)) os.remove(f_filename)
def hiddenMarkTest(self): f_mark = configManagement.getSoftwareName() f_version = configManagement.currentVersion() f_filename = singleClone(g_files2['pokefile0'].fullname) MetadataManagerL1.placeMark(f_filename) self.assertEqual(True, MetadataManagerL0.containsMetadataDate(f_filename)) self.assertEqual(True, MetadataManagerL0.containsTaggerMark(f_filename)) self.assertEqual(f_mark, MetadataManagerL0.getTaggerMark(f_filename)) self.assertEqual(True, MetadataManagerL0.containsVersionNum(f_filename)) self.assertEqual(f_version, MetadataManagerL0.getVersionNum(f_filename)) os.remove(f_filename)
def singleClone(p_filename, p_stop=False): """ creates a copy of a file and gives the copy a "clone name". Used to create fresh files for metadata editting tests example: given a file home/user/pictures/birds.jpg exists, singleClone('home/user/pictures/birds.jpg') will return "home/user/pictures/birdsCopy.jpg" and 'home/user/pictures/birdsCopy.jpg' will be created Note: according to the shutil documentation, this might not copy all metadata :param p_filename: full filename including the path :type p_filename: string :param p_stop: True if we want to prevent existing clones from being overwritten (default: False) :type p_stop: Boolean :raise OSError: if no file with p_filename is found :raise ValueError: if p_stop is True and a file with a clone's name already exists :return: name of the cloned file including the path :rtype: string """ ext = MetadataManagerL0.getExtension(p_filename) f_name = p_filename[:-len(ext)] f_newfile = f_name + "Copy" + ext if os.path.isfile(f_newfile) == True: if p_stop: raise ValueError('File \'{}\' already exists'.format(f_newfile)) else: singleRelease(f_newfile) shutil.copy2(p_filename, f_newfile) shutil.copy2(p_filename, f_newfile) return f_newfile
def getSeries(p_filename): """! :param p_filename: name/path of the file :type p_filename: string :return: series if it exists. Else, ("", -1) :rtype: tuple (string, int) """ try: f_seriesName = MetadataManagerL0.getSeriesName(p_filename) f_seriesInstallment = MetadataManagerL0.getSeriesInstallment( p_filename) return (f_seriesName, f_seriesInstallment) except Exception as e: print("MetadataManager2.getSeries() error: ", e) return ("", -1)
def test_removeTagResults(self): releaseAllClones(g_clonenames) f_value = "funny" f_expected = [ "stock photo", "bad stock photos of my job", "technology" ] f_filename = singleClone(g_files["fixingComputer.jpg"].fullname) MetadataManagerL1.removeTag(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getTags(f_filename)) os.remove(f_filename) f_value = "cat" f_expected = [] f_filename = singleClone(g_files["catScreamPizza.jpg"].fullname) MetadataManagerL1.removeTag(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getTags(f_filename)) os.remove(f_filename)
def containsSeries(p_filename): """! This will tell us if the file has Series metadata. It must contain a series name and a series installent :param p_filename: name/path of the file :type p_filename: string :return: True if file has series name and series installment metadata :rtype: bool """ try: return MetadataManagerL0.containsSeriesName(p_filename) \ and MetadataManagerL0.containsSeriesInstallment(p_filename) except Exception as e: print("MetadataManager2.containsSeries() error: ", e) return False
def test_addDescrResults(self): releaseAllClones(g_clonenames) f_value = "\nThis is basically me building my gaming pc" f_expected = "Bad stock photo of my job found on twitter.\nThis is basically me building my gaming pc" f_filename = singleClone(g_files["fixingComputer.jpg"].fullname) MetadataManagerL1.addDescr(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getDescr(f_filename)) os.remove(f_filename) f_value = "\nCrazy cat picture" f_expected = "a cat screaming at the camera in front of a dog wearing a pizza box\nCrazy cat picture" f_filename = singleClone(g_files["catScreamPizza.jpg"].fullname) MetadataManagerL1.addDescr(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getDescr(f_filename)) os.remove(f_filename) f_value = "The game is about a penguin" f_expected = "The game is about a penguin" f_filename = singleClone(g_files["rippledotzero.jpg"].fullname) MetadataManagerL1.addDescr(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getDescr(f_filename)) os.remove(f_filename)
def wipeRating(p_filename): """ removes rating metadata from a file completely :param p_filename: name/path of the file :type p_filename: string :return: True if operation was successful :rtype: bool """ try: MetadataManagerL0.wipeRating(p_filename) except Exception as e: print("MetadataManager2.wipeRating() error: ", e) return False try: MetadataManagerL1.placeMark(p_filename) return True except Exception as e: print("MetadataManager2.wipeRating() Mark error: ", e) return True
def setOrgDate(p_filename, p_date): """:param p_filename: name/path of the file :type p_filename: string :param p_date: original date metadata will be set to this :type p_date: datetime :return: True if operation was successful :rtype: bool """ try: MetadataManagerL0.setOrgDate(p_filename, p_date) except Exception as e: print("MetadataManager2.setOrgDate() error: ", e) return False try: MetadataManagerL1.placeMark(p_filename) return True except Exception as e: print("MetadataManager2.setOrgDate() Mark error: ", e) return True
def checkAllKeysPresent(p_file, p_metatype): #takes a filename and a metadata type (Title, Description, Tags, etc) #and returns true if all keys associated with that metadata type are present in the file #used for testing the metadata editing functions f_metadata = pyexiv2.ImageMetadata(p_file) f_metadata.read() f_keys = MetadataManagerL0.appropriateKeys(p_file, p_metatype) for key in f_keys: if key not in (f_metadata.exif_keys + f_metadata.xmp_keys + f_metadata.iptc_keys): return False return True
def setRating(p_filename, p_setRatingToThis): """ :param p_filename: name/path of the file :type p_filename: string :param p_setRatingToThis: rating metadata will be set to this :type p_setRatingToThis: int :return: True if operation was successful :rtype: bool """ try: MetadataManagerL0.setRating(p_filename, p_setRatingToThis) except Exception as e: print("MetadataManager2.setRating() error: ", e) return False try: MetadataManagerL1.placeMark(p_filename) return True except Exception as e: print("MetadataManager2.setRating() Mark error: ", e) return True
def getRating(p_filename): """! :param p_filename: name/path of the file :type p_filename: string :return: rating if it exists. Else, -1 :rtype: int """ try: return MetadataManagerL0.getRating(p_filename) except Exception as e: print("MetadataManager2.getRating() error: ", e) return -1
def getArtists(p_filename): """! :param p_filename: name/path of the file :type p_filename: string :return: list of artists if it exists. Else, [] :rtype: list<string> """ try: return MetadataManagerL0.getArtists(p_filename) except Exception as e: print("MetadataManager2.getArtists() error: ", e) return []
def getSource(p_filename): """! :param p_filename: name/path of the file :type p_filename: string :return: source url if it exists. Else, "" :rtype: string """ try: return MetadataManagerL0.getSource(p_filename) except Exception as e: print("MetadataManager2.getSource() error: ", e) return ""
def test_addTagResults(self): releaseAllClones(g_clonenames) f_value = "computer" f_expected = [ "computer", "stock photo", "funny", "bad stock photos of my job", "technology" ] f_filename = singleClone(g_files["fixingComputer.jpg"].fullname) MetadataManagerL1.addTag(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getTags(f_filename)) os.remove(f_filename) f_value = "dramatic" f_expected = ["dramatic", "cat"] f_filename = singleClone(g_files["catScreamPizza.jpg"].fullname) MetadataManagerL1.addTag(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getTags(f_filename)) os.remove(f_filename) f_value = "video games" f_expected = ["video games"] f_filename = singleClone(g_files["rippledotzero.jpg"].fullname) MetadataManagerL1.addTag(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getTags(f_filename)) os.remove(f_filename)
def test_addArtistResults(self): releaseAllClones(g_clonenames) f_value = "model: crazyguy" f_expected = [ "model: crazyguy", "stockphotographer", "publisher: twitter" ] f_filename = singleClone(g_files["fixingComputer.jpg"].fullname) MetadataManagerL1.addArtist(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getArtists(f_filename)) os.remove(f_filename) f_value = "model: pizzadog" f_expected = ["model: pizzadog", "photographer: idunno"] f_filename = singleClone(g_files["catScreamPizza.jpg"].fullname) MetadataManagerL1.addArtist(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getArtists(f_filename)) os.remove(f_filename) f_value = "Artist: Simon Stalenhag" f_expected = ["Artist: Simon Stalenhag"] f_filename = singleClone(g_files["rippledotzero.jpg"].fullname) MetadataManagerL1.addArtist(f_filename, f_value) self.assertEqual(f_expected, MetadataManagerL0.getArtists(f_filename)) os.remove(f_filename)
def containsArtists(p_filename): """! This will tell us if the file has any artist metadata. :param p_filename: name/path of the file :type p_filename: string :return: True if file has artist metadata :rtype: bool """ try: return MetadataManagerL0.containsArtists(p_filename) except Exception as e: print("MetadataManager2.containsArtists() error: ", e) return False
def getOrgDate(p_filename): """ if none exists, returns datetime.datetime(1, 1, 1) please don't use this as a magic number. It's just to keep consistent types since set and get should have the same requirements to work. :param p_filename: name/path of the file :type p_filename: string :return: original data if it exists. Else, datetime.datetime(1,1,1) :rtype: datetime """ try: return MetadataManagerL0.getOrgDate(p_filename) except Exception as e: print("MetadataManager2.getOrgDate() error: ", e) return datetime.datetime(1, 1, 1)
def checkAnyKeysPresent(p_file, p_metatype, p_verbose=False): #takes a filename and a metadata type (Title, Description, Tags, etc) #and returns true if any keys associated with that metadata type are present in the file #used for testing the metadata editing functions f_metadata = pyexiv2.ImageMetadata(p_file) f_metadata.read() f_keys = MetadataManagerL0.appropriateKeys(p_file, p_metatype) f_present = [] for key in f_keys: if key in (f_metadata.exif_keys + f_metadata.xmp_keys + f_metadata.iptc_keys): f_present.append(key) if p_verbose: print("In", p_file, "the following", p_metatype, "keys still exist:", f_present) if len(f_present) == 0: return False return True
def tryAddData(p_filename): #this will try to add every kind of metadata possible to an image. #-------------Title-------------------------------------- MetadataManagerL0.setTitle(p_filename, "testFile") # -------------Tags-------------------------------------- MetadataManagerL1.addArtist(p_filename, "creator: weirdo") # -------------Artist------------------------------------ MetadataManagerL1.addTag(p_filename, "test") # -------------Description------------------------------- MetadataManagerL0.setDescr(p_filename, "this is a sample description") # -------------Rating------------------------------------ MetadataManagerL0.setRating(p_filename, 3) return
g_desc8 = 'Some fast drawing of a weird cat. It\'s staring at a pizza like it wants to sit on it. I found this online a long time ago.' g_rating8 = 1 g_tags8 = ['drawing', 'cat', 'mspaint'] g_artist8 = ['9566215387126'] g_date8 = datetime.datetime(2019, 1, 18, 18, 32) g_source8 = 'tinyu.rl/1jv345' g_seriesname8 = 'cat pizza meme' g_seriesins8 = 2 g_mark8 = 'taggerMark' g_vers8 = "1.03" g_mdate9 = datetime.datetime(2019, 1, 9, 5, 20) g_mark9 = 'taggerMark' g_vers9 = "0.50" g_mdate8 = datetime.datetime(2019, 1, 10, 3, 41) """ MetadataManagerL0.allMeta(g_testfile0) MetadataManagerL0.allMeta(g_testfile1) MetadataManagerL0.allMeta(g_testfile2) MetadataManagerL0.allMeta(g_testfile3) MetadataManagerL0.allMeta(g_testfile4) MetadataManagerL0.allMeta(g_testfile5) MetadataManagerL0.allMeta(g_testfile6) MetadataManagerL0.allMeta(g_testfile7) MetadataManagerL0.allMeta(g_testfile8) MetadataManagerL0.allMeta(g_testfile9) """ sampleData = TestData(p_title="Sample Title", p_desc="Sample description of our\nfile", p_rating=2,
def EverythingUseageCheck(p_fileEntry, p_testfilelist, f_outpath=g_outpath): #the file you load better have all this metadata in it. f_picID = p_testfilelist[p_fileEntry] f_filename = getGoogleDrivePicture(f_picID, f_outpath) f_metadata = pyexiv2.ImageMetadata(f_filename) f_metadata.read() print(f_metadata.exif_keys) #-------------Title-------------------------------------- key1 = 'Exif.Image.XPTitle' f_keywords1 = f_metadata[key1] f_dirtyString1 = pyexiv2.utils.undefined_to_string(f_keywords1.value) print("titleUseageCheck() f_dirtyString1: \t\t", f_dirtyString1) f_cleanThing1 = MetadataManagerL0.dirtyStr2cleanStr(f_dirtyString1) print("titleUseageCheck() Title\t\t", f_cleanThing1) # -------------Tags-------------------------------------- key2 = 'Exif.Image.XPTitle' f_keywords2 = f_metadata[key2] f_dirtyString2 = pyexiv2.utils.undefined_to_string(f_keywords2.value) print("titleUseageCheck() f_dirtyString2: \t\t", f_dirtyString2) f_cleanThing2 = MetadataManagerL0.dirtyStr2cleanList(f_dirtyString2) print("titleUseageCheck() Tags\t\t", f_cleanThing2) # -------------Artist-------------------------------------- key3 = 'Exif.Image.XPAuthor' f_keywords3 = f_metadata[key3] f_dirtyString3 = pyexiv2.utils.undefined_to_string(f_keywords3.value) print("titleUseageCheck() f_dirtyString3: \t\t", f_dirtyString3) f_cleanThing3 = MetadataManagerL0.dirtyStr2cleanList(f_dirtyString3) print("titleUseageCheck() Artist\t\t", f_cleanThing3) # -------------Description-------------------------------------- key4 = 'Exif.Image.XPComment' f_keywords4 = f_metadata[key4] f_dirtyString4 = pyexiv2.utils.undefined_to_string(f_keywords4.value) print("titleUseageCheck() f_dirtyString4: \t\t", f_dirtyString4) f_cleanThing4 = MetadataManagerL0.dirtyStr2cleanStr(f_dirtyString4) print("titleUseageCheck() Description\t\t", f_cleanThing4) # -------------Rating-------------------------------------- key5 = 'Exif.Image.Rating' f_keywords5 = f_metadata[key5] print("titleUseageCheck() f_keywords5: \t\t", f_keywords5) print("titleUseageCheck() f_keywords5.value: \t\t", f_keywords5.value) #f_metadata.__delitem__('Exif.Image.Rating') #f_metadata.__delitem__('Exif.Image.RatingPercent') #f_metadata.write() #f_metadata.read() #print(f_metadata.exif_keys) # ------------Source URL----------------------------------- key6 = 'Exif.Image.ImageHistory' value6 = "modified by file tagger" f_metadata[key6] = pyexiv2.ExifTag(key6, value6) f_metadata.write() f_metadata.read() print(f_metadata.exif_keys) f_keywords6 = f_metadata[key6] print("titleUseageCheck() f_keywords6: \t\t", f_keywords6) print("titleUseageCheck() f_keywords6.value: \t\t", f_keywords6.value) print("titleUseageCheck() type(f_keywords6.value): \t\t", type(f_keywords6.value)) # ------------Original Date-------------------------------- key7 = 'Exif.Photo.DateTimeOriginal' f_keywords7 = f_metadata[key7] print("titleUseageCheck() f_keywords7: \t\t", f_keywords7) print("titleUseageCheck() f_keywords7.value: \t\t", f_keywords7.value) print("titleUseageCheck() type(f_keywords7.value): \t\t", type(f_keywords7.value)) #f_dirtyTagString = pyexiv2.utils.undefined_to_string(f_keywords.value) #print("titleUseageCheck() f_dirtyTagString\t\t", f_dirtyTagString) #f_cleanTagList = MetadataManager.dirtyStr2cleanList(f_dirtyTagString) #print("titleUseageCheck() f_cleanTagList\t\t", f_cleanTagList) #f_dirtyTagString2 = MetadataManager.cleanList2dirtyStr(f_cleanTagList) #print("titleUseageCheck() f_dirtyTagString2\t\t", f_dirtyTagString2) os.remove(f_filename) return