def _load_clr_def(self): path = self.CORE['path']['color-def'] clr_def = File(self, path, 'csv', True) clr_def = clr_def.read() clr_def = {int(row[0]):tuple([ int(el) for el in row[1:4] ])\ for row in clr_def[1:]} return clr_def
def _fromMemoryOrCache(channelId=None, username=None, mine=None): if username is None and channelId is None and mine is None: raise ValueError( 'Channel loader must get either username, channelId or mine. Got neither.' ) if mine: if mineLoaded: return mineLoaded cacheFile = File.fromNameAndDir(MY_CHANNEL_FILE, CHANNEL_CACHE_DIR) if cacheFile.exists(): channel = fromCacheFile(cacheFile, mine=True) return channel return None if username and not channelId: if not usernameDic.has(username): return None channelId = usernameDic.get(username) if channelId in channelsLoaded: return channelsLoaded[channelId] cacheFileName = channelId + CACHE_FILE_EXTENSION cacheFile = File.fromNameAndDir(cacheFileName, CHANNEL_CACHE_DIR) if cacheFile.exists(): channel = fromCacheFile(cacheFile) return channel return None
async def get_blacklist(ctx): if ctx.guild.id == 633673794468446270: blacklist = File("blacklist") if len(blacklist.read_data()) > 5: await ctx.send(f"``{blacklist.read_data()}``") else: await ctx.send("В чёрном списке никого нет.")
def load(collectionFile): dicFile = File.fromFullpath(COLLECTION_DIC_FILE) if dicFile.exists(): collectionDic = dicFile.loadObject() xmlFP = collectionFile.fullpath if xmlFP in collectionDic: #check if this collection was ever cached dumpFileFP = collectionDic[xmlFP] dumpFile = File.fromFullpath(dumpFileFP) collection = dumpFile.loadObject() if collection.cachedXml == collectionFile.contents(): #xml is same, we can use the data from before collection.fetchInfoIfTime() #unless a long time passed. if so, refetch it return collection else: #xml changed so we reload and redump the collection collection = _load(collectionFile, dumpFile) #dic doesn't need to be touched return collection else: collectionDic = {} collection = _load(collectionFile) #either there was no dic file at all, or collection #was never cached. we load the collection from collectionDic[collectionFile.fullpath] = collection.dumpFile.fullpath #scratch, find a new available dump file and dump it. dicFile.dumpObject(collectionDic) #we also add it to the dic and dump/redump the dic return collection
def _load_prov_map(self): path = self.CORE['path']['prov-map'] prov_map = File(self, path, 'img', True) image = prov_map.read() id_pos = self._map_pixels_clr(image) self.SRC_IMG = image.copy() image = self._prov_map_filter(image) return id_pos, image
def _load_assignment(self): path_area = self.CORE['path']['area-assign'] path_regn = self.CORE['path']['regn-assign'] path_segn = self.CORE['path']['segn-assign'] areas = File(self, path_area, 'asgn', True).read() regns = File(self, path_regn, 'asgn', True).read() segns = File(self, path_segn, 'asgn', True).read() return areas, regns, segns
def emptyFromDirPath(title, dirPath): from src.file import File collectionFile = File.fromInvalidNameAndDir(title + '.xml', dirPath) while collectionFile.exists(): collectionFile = File.fromNameAndDir(collectionFile.soleName + '_.xml', dirPath) return empty(title, collectionFile)
async def add_user_blacklist(ctx, user_id): if ctx.guild.id == 633673794468446270: blacklist = File("blacklist") if user_id in blacklist.read_data(): await ctx.send("Пользователь уже есть в чёрном списке!") return blacklist.write_data(f"{user_id} ({ctx.guild.get_member(int(user_id)).name})\n") await ctx.send("Пользователь был добавлен в чёрный список.")
def _load_map_def(self): path = self.CORE['path']['map-def'] map_def = File(self, path, 'game', True) map_def = map_def.read() map_size = int(map_def['width'][0]), int(map_def['height'][0]) sea_provs = [int(el) for el in map_def['sea_starts'][0]] rnw_provs = [int(el) for el in map_def['only_used_for_random'][0]] lake_provs = [int(el) for el in map_def['lakes'][0]] return map_size, sea_provs, rnw_provs, lake_provs
def setUp(self): parser = RegexParser( regex= '(?P<match_left>"version"\s*:\s*(?:"))(?P<version>(?:(?:\d+)+.?)+)(?P<match_right>")' ) self.file = File( "test", "test_file", os.path.join(dirname(dirname(__file__)), 'test_resources', 'test_file'), parser) self.file.update_version('0.2.0')
def __init__(self, *args, **kw): fuse.Fuse.__init__(self, *args, **kw) self.root = raw_input("Root inode: ") self.key = raw_input("Encryption key: ") self.prefix = raw_input("Prefix: ") inode = Inode(self.root, self.key, prefix=self.prefix) self.root_dir = Directory(inode, prefix=self.prefix) self.freefile = File(self.root_dir.dirs[".freefile"]) self.free = self.freefile.read(self.freefile.inode.file_size, 0).split("$") self.root_dir.free = self.free self.last_objs = dict() print """
def test_no_version_definition_is_found(self): out = StringIO() sys.stdout = out path = os.path.join(dirname(dirname(__file__)), 'test_resources', 'test_file') parser = RegexParser( regex= '(?P<match_left>"version"\s*=\s*(?:"))(?P<version>(?:(?:\d+)+.?)+)(?P<match_right>")' ) self.file = File("test", "test_file", path, parser) self.assertIsNone(self.file.current_version) self.assertEqual( "%sNo version definition is found in file %s" % (Fore.WHITE, path), out.getvalue().strip())
def load_history(self): history = {} namelist = [] subdir = self.CORE['path']['prov-hist-dir'] try: namelist = listdir(self.MPATH+subdir) except FileNotFoundError: pass try: namelist += listdir(self.GPATH+subdir) except FileNotFoundError: pass namelist = list(set(namelist)) for fn in namelist: file = File(self, subdir+fn, 'game', True) contents = file.read() id = self._get_id(fn) self.provs[id].set_history(contents, fn)
def browse(relativePath): lastFolderFile = File.fromNameAndDir(LAST_FOLDER_FILE, GENERAL_CACHE_DIR) if relativePath.endswith('/'): relativePath = relativePath[:-1] if relativePath == USE_LAST_FOLDER_STR: relativePath = lastFolderFile.loadObject() path = MY_COLLECTIONS_DIR + relativePath folder = Folder.fromFullpath(path) items = ItemList() files, subfolders = folder.listFolder() for subfolder in subfolders: if subfolder.name != "_images": subpath = relativePath + '/' + subfolder.name items.addSelectableFolder(subfolder, foldersVisual, subpath) items.addCustomFile(NEW_COLLECTION_STR + relativePath, createNewCollectionVisual) items.addCustomFile(NEW_FOLDER_STR + relativePath, createNewFolderVisual) for collectionFile in files: collection = Collection.fromFile(collectionFile, loadSources=False) items.addSelectableCollection(collection, collectionsVisual) lastFolderFile.dumpObject(relativePath) items.present(viewStyle)
def get_obj(self, path): if path in self.last_objs.keys(): return self.last_objs[path] pe = path.split('/')[1:] step = 0 current = self.root_dir whole_path = "" for part in pe: in_cache = False whole_path += "/" + part if whole_path in self.last_objs.keys(): in_cache = True current = self.last_objs[whole_path] else: if current.dirs[part].is_dir(): current = Directory(current.dirs[part], self.free, prefix=self.prefix) elif part != pe[-1]: return -errno.ENOENT else: current = File(current.dirs[part], self.free, prefix=self.prefix) if not in_cache: self.last_objs[whole_path] = current return current
def gc(): global _globalCollection if _globalCollection: return _globalCollection from src.collection import Collection gcFile = File.fromNameAndDir(GLOBAL_COLLECTION_FILE, VIEWS_DATA_DIR) if gcFile.exists(): gc = Collection.fromFile(gcFile, loadSources=False, isGlobal=True) gc.setLoadedSources() gc.feedSettings.useLimits = True #temp cause of people changing from version 2.0.1 where #this attrib didn't exist. remove this in future else: gc = Collection.empty('Global Collection', gcFile) gc.setOnClick(Collection.D_ONCLICK) gc.feedSettings.use = True gc.feedSettings.useLimits = True gc.feedSettings.TS.use = True gc.sourcesSettings.use = True gc.sourcesSettings.TS.use = True gc.writeCollectionFile() _globalCollection = gc return gc
async def on_raw_reaction_remove(preyload): react_channel = await self.bot.fetch_channel(preyload.channel_id) if self.read_config()[str(react_channel.guild.id)]["message_id"] is None: return blacklist = File("blacklist") if str(preyload.user_id) in blacklist.read_data(): return reaction_list = (await react_channel.fetch_message(preyload.message_id)).reactions user = react_channel.guild.get_member(preyload.user_id) if preyload.message_id == self.read_config()[str(react_channel.guild.id)]["message_id"]: for key, value in (self.read_config()[str(react_channel.guild.id)]["reactions"]).items(): if key == preyload.emoji.name: await user.remove_roles(react_channel.guild.get_role(int(value))) return
def __init__(self, query, searchType): self.query = query self.searchType = searchType self.results = Pages(self._serviceRequest, self._responseProcess, ItemType.VSOURCE, '%s search results' %searchType, s.searchesCacheTime, TimeUnit.MINUTES, self) cacheFileName = str(uuid.uuid4()) + '.srch' self.cacheFile = File.fromNameAndDir(cacheFileName, SEARCH_CACHE_DIR)
def test_no_version_definition_is_found(self): out = StringIO() sys.stdout = out path = os.path.join(dirname(dirname(__file__)), 'test_resources', 'test_file') parser = RegexParser(regex='(?P<match_left>"version"\s*=\s*(?:"))(?P<version>(?:(?:\d+)+.?)+)(?P<match_right>")') self.file = File("test", "test_file", path, parser) self.assertIsNone(self.file.current_version) self.assertEqual("%sNo version definition is found in file %s" % (Fore.WHITE, path), out.getvalue().strip())
def load(fileQuery): collectionFile = File.fromQuery(fileQuery) #get collection settings and set defaults if not specified xmlRoot, = root(collectionFile) rootAttribs = xmlRoot.attrs title = getAttrib(rootAttribs, 'title', DEFAULT_TITLE) thumb = getAttrib(rootAttribs, 'thumb', DEFAULT_THUMB) sort = getAttrib(rootAttribs, 'sort', DEFAULT_SORT) collectionLimit = getAttrib(rootAttribs, 'collectionLimit', DEFAULT_COLLECTION_LIMIT, AttribType.INT) unwatched = getAttrib(rootAttribs, 'unwatched', DEFAULT_UNWATCHED, AttribType.BOOL) globalLimit = getAttrib(rootAttribs, 'limit', DEFAULT_GLOBAL_LIMIT, AttribType.INT) globalRepeat = getAttrib(rootAttribs, 'repeat', DEFAULT_GLOBAL_REPEAT, AttribType.INT) globalPush = getAttrib(rootAttribs, 'push', DEFAULT_GLOBAL_PUSH, AttribType.INT) if globalLimit > MAXLIMIT: globalLimit = MAXLIMIT sources = [] #channelSources = _processChannels(xmlRoot.channels) #playlistSources = _processPlaylists(xmlRoot.playlists) for node in xmlRoot.children: print node types = { 'channels': _processChannels, 'playlists': _processPlaylists } #maybe move to bottom for node in xmlRoot.children: print node #get sources and their settings and set the global setting if not specified #(which is possibly the default) nodeAttribs = node.attrib limit = getAttrib(nodeAttribs, 'limit', globalLimit, AttribType.INT) repeat = getAttrib(nodeAttribs, 'repeat', globalRepeat, AttribType.INT) push = getAttrib(nodeAttribs, 'push', globalPush, AttribType.INT) if limit > MAXLIMIT: limit = MAXLIMIT #process according to the type of source sourceType = node.tag nodeSources = types[sourceType](nodeAttribs, limit, repeat, push) sources.extend(nodeSources) collection = Collection(title, thumb, sort, collectionLimit, unwatched, collectionFile, sources) return collection
def load(fileName, fileDir): fullpath = fileDir + '/' + fileName watchedDic = dd._loadFromMemory(fullpath) if watchedDic: return watchedDic dicFile = File.fromFullpath(fullpath) return WatchedDic(dicFile)
def test_parser_none(self): try: File( "test", "test_file", os.path.join(dirname(dirname(__file__)), 'test_resources', 'test_file'), None) self.assertFalse(True) except Exception as e: self.assertEqual("Parser must be given.", str(e))
def load(fileName, fileDir): fullpath = fileDir + '/' + fileName dataDic = _loadFromMemory(fullpath) if dataDic: return dataDic dicFile = File.fromFullpath(fullpath) return DataDictionary(dicFile)
def test_NodeAsObject(): test_data = Folder("E:/Course/Design Pattern/DP_inclass/test_data") folder_1 = Folder("E:/Course/Design Pattern/DP_inclass/test_data/folder_1") hello_cpp = File("E:/Course/Design Pattern/DP_inclass/test_data/hello.cpp") test_data.add(folder_1) test_data.add(hello_cpp) assert test_data.numberOfNodes() == 2
def __init__(self, parent=None): super().__init__(parent) # 클래스 기본 설정 self.file = File('/PlayList.txt') NotificationCenter.subscribe(NotificationName.end_video, self.next_video) NotificationCenter.subscribe(NotificationName.add_video, self.add_video) # UI 설정 self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.clicked.connect(self.did_clicked_item) self.model = QStandardItemModel() self.setModel(self.model) self.refresh() # 첫 번째 인덱스로 영상 시작 self.play_video(0)
def _load(collectionFile, dumpFile=None): root = rootAndChildren(collectionFile) #get collection settings and set defaults if not specified title = root.text if title == '': title = d.TITLE settings = root.settings collectionLimit = settings.get( 'climit', d.COLLECTION_LIMIT ) unwatched = settings.get( 'unwatched', d.UNWATCHED ) globalLimit = settings.get( 'limit', d.GLOBAL_LIMIT ) if globalLimit > d.MAX_LIMIT: globalLimit = d.MAX_LIMIT sources = [] types = {'channels':_processChannel, 'playlists':_processPlaylist} #maybe move to bottom for node in root.children: sourceType = node.name for textRow in node.textRows: sourceSettings = _processSourceSettings(textRow.settings, globalLimit) source = types[sourceType](textRow, sourceSettings) sources.append(source) if dumpFile is None: dumpFile = File.fromFullpath(CACHE_DIR + collectionFile.soleName + '.col') while dumpFile.exists(): dumpFile = File.fromFullpath (dumpFile.path + '/' + dumpFile.soleName + '_' + '.col') collection = Collection(title, collectionLimit, unwatched, collectionFile, sources, dumpFile) return collection
def checkVersionChange(): addonVer = addonSettings.version() currentVerFile = File.fromNameAndDir(CURRENT_VERSION_FILE, GENERAL_DATA_DIR) if currentVerFile.exists() and (currentVerFile.loadObject() == addonVer): return import delete_cache delete_cache.delete(successDialog=False) currentVerFile.dumpObject(addonVer)
def tryFetchingCredentials(): global credentials try: credentials = flow.step2_exchange(device_flow_info=deviceFlowInfo) except FlowExchangeError as e: if e.message != 'authorization_pending': raise ValueError ('Unknown FlowExchange error: %s' %e.message) return False if credentials.invalid: credentials = None return False credentialsFile = File.fromNameAndDir(CREDENTIALS_FILE, YOUTUBE_DATA_DIR) storage = Storage(credentialsFile.fullpathTranslated()) #store on hard drive storage.put(credentials) return True #READ_ONLY_SCOPE = 'https://www.googleapis.com/auth/youtube.readonly' #REDIERCT_URI = 'urn:ietf:wg:oauth:2.0:oob' #directLink = flow.step1_get_authorize_url() #from oauth2client.client import OAuth2WebServerFlow #CLIENT_ID = '1074802623980-dpf5kf1o0e14hkjb9al8st51r2fqk71l.apps.googleusercontent.com' #CLIENT_SECRET = 'xCvTxLTqi0TbkWijXxYBS8_7' # flow = OAuth2WebServerFlow(client_id=CLIENT_ID, # client_secret=CLIENT_SECRET, # scope=READ_WRITE_SCOPE, # redirect_uri=REDIERCT_URI)
def _fromMemoryOrCache(path): global foldersLoaded if path in foldersLoaded: return foldersLoaded[path] if pathDic.has(path): cacheFilePath = pathDic.get(path) cacheFile = File.fromFullpath(cacheFilePath) return fromCacheFile(cacheFile) return None
def fetchSignInInfo(): global flow global deviceFlowInfo clientSecret = File.fromFullpath(CLIENT_SECRET_FILE) flow = flow_from_clientsecrets(clientSecret.fullpathTranslated(), scope=READ_WRITE_SCOPE) deviceFlowInfo = flow.step1_get_device_and_user_codes() code = deviceFlowInfo[1] return code
def endEstimations(self): if self.lastUpdate is None: if ( self.numEstimated == 0 ): #this folder was never saved before and didn't add any records either return #so no need to save it. if it was saved before and added no records #we still need to save it just to mark the new time of lastUpdate fileName = str(uuid.uuid4()) + '.fde' self.fileName = fileName recordFile = File.fromNameAndDir(fileName, KODI_ESTIMATION_DIR) pathDic.setIfNonExistent(self.folderPath, recordFile.fullpath) else: recordFile = File.fromNameAndDir(self.fileName, KODI_ESTIMATION_DIR) self.lastUpdate = datetime.now(pytz.utc) del self.numEstimated recordFile.dumpObject(self)
class Test(unittest.TestCase): def setUp(self): self.f = File("test_file") def tearDown(self): pass def test_set_password(self): self.assertIsNone(self.f._password) self.f.set_password("password") self.assertIsNotNone(self.f._password) self.assertIsInstance(self.f._password, long) def test_check_password(self): self.f.set_password("password") self.assertTrue(self.f.check_password("password")) self.assertFalse(self.f.check_password("not_password")) self.f.set_password(12345) self.assertTrue(self.f.check_password(12345)) self.assertFalse(self.f.check_password(54321))
def _fromMemoryOrCache(channelId=None, username=None, mine=None): if username is None and channelId is None and mine is None: raise ValueError('Channel loader must get either username, channelId or mine. Got neither.') if mine: if mineLoaded: return mineLoaded cacheFile = File.fromNameAndDir(MY_CHANNEL_FILE, CHANNEL_CACHE_DIR) if cacheFile.exists(): channel = fromCacheFile(cacheFile, mine=True) return channel return None if username and not channelId: if not usernameDic.has(username): return None channelId = usernameDic.get(username) if channelId in channelsLoaded: return channelsLoaded[channelId] cacheFileName = channelId + CACHE_FILE_EXTENSION cacheFile = File.fromNameAndDir(cacheFileName, CHANNEL_CACHE_DIR) if cacheFile.exists(): channel = fromCacheFile(cacheFile) return channel return None
def _fromMemoryOrCache(playlistId): global playlistsLoaded if playlistId in playlistsLoaded: return playlistsLoaded[playlistId], None cacheFileName = playlistId + CACHE_FILE_EXTENSION cacheFile = File.fromNameAndDir(cacheFileName, PLAYLIST_CACHE_DIR) if cacheFile.exists(): playlist = fromCacheFile(cacheFile) return playlist, None return None, cacheFile
async def remove_user_blacklist(ctx, user_id): if ctx.guild.id == 633673794468446270: blacklist = File("blacklist") if user_id not in blacklist.read_data(): await ctx.send("Пользователя нет в чёрном списке!") return data = blacklist.read_data().replace(f"{user_id} ({ctx.guild.get_member(int(user_id)).name})\n", "") await ctx.send("Пользователь был убран из чёрного списка.") blacklist.re_write(data)
class TestFile(unittest.TestCase): def setUp(self): self.tmp = File() def test_File_read(self): open = mock_open(read_data="test data") with patch('builtins.open', open): self.assertEqual("test data", self.tmp.read('/path/')) def test_File_write(self): open = mock_open(read_data="test data") with patch('builtins.open', open): self.tmp.write('/path/', "test data 2") open.assert_called_once_with('/path/', 'w+') def test_File_delete_success(self): mock = MagicMock() mock2 = MagicMock() with patch('os.path.exists', mock): with patch('os.remove', mock2): result = self.tmp.delete('/path/') self.assertEqual(result, True) def test_File_delete_failure(self): mock = MagicMock() mock.return_value = False mock2 = MagicMock() with patch('os.path.exists', mock): with patch('os.remove', mock2): self.assertRaises(Exception, self.tmp.delete, '/path/')
def endEstimations(self): if self.lastUpdate is None: if (self.numEstimated == 0): #this folder was never saved before and didn't add any records either return #so no need to save it. if it was saved before and added no records #we still need to save it just to mark the new time of lastUpdate fileName = str(uuid.uuid4()) + '.fde' self.fileName = fileName recordFile = File.fromNameAndDir(fileName, KODI_ESTIMATION_DIR) pathDic.setIfNonExistent(self.folderPath, recordFile.fullpath) else: recordFile = File.fromNameAndDir(self.fileName, KODI_ESTIMATION_DIR) self.lastUpdate = datetime.now(pytz.utc) del self.numEstimated recordFile.dumpObject(self)
def loadFromFile(): playlistFile = File.fromInvalidNameAndDir(PLAYLIST_FILE, GENERAL_CACHE_DIR) cvls = playlistFile.loadObject() playlist = ItemPlaylist() for cvl in cvls: li = xbmcgui.ListItem(cvl.title, thumbnailImage=cvl.thumb) li.setInfo('general', cvl.generalInfoLabels) li.setInfo('video', cvl.videoInfoLabels) #li.setProperty('IsPlayable', 'True') pi = (cvl.url, li) playlist.addPi(pi) return playlist
def load(): global loaded if loaded: loaded.checkSettingsChange() return loaded cacheFile = File.fromNameAndDir(CACHE_FILE, YOUTUBE_CACHE_DIR) if cacheFile.exists(): subscriptions = cacheFile.loadObject() subscriptions.channels.loadFromCachedMode() subscriptions.checkSettingsChange() loaded = subscriptions return subscriptions return Subscriptions(cacheFile)
def fromCategoryList(item): global categoriesLoaded categoryId = item['id'] snippet = item['snippet'] if categoryId in categoriesLoaded: return categoriesLoaded[categoryId] cacheFileName = categoryId + '.cat' cacheFile = File.fromNameAndDir(cacheFileName, CATEGORY_CACHE_DIR) if cacheFile.exists(): category = fromCacheFile(cacheFile) return category return Category(categoryId, cacheFile, snippet)
def init(): global credentials global ranInit if ranInit: return credentialsFile = File.fromNameAndDir(CREDENTIALS_FILE, YOUTUBE_DATA_DIR) if credentialsFile.exists(): storage = Storage(credentialsFile.fullpathTranslated()) credentials = storage.get() if credentials and credentials.invalid: credentials = None else: credentials = None ranInit = True
def fromPath(folderPath): global loaded if folderPath in loaded: #from memory return loaded[folderPath] if pathDic.has(folderPath): recordFilePath = pathDic.get(folderPath) recordFile = File.fromFullpath(recordFilePath) folderRecord = recordFile.loadObject() if folderRecord.folderPath in loaded: raise ValueError("Tried loading folder record from file when it's already in memory") else: #create new folderRecord = FolderRecord(folderPath) loaded[folderRecord.folderPath] = folderRecord return folderRecord
def __init__(self, path, title, thumb, description=None, date=None, dateType=None, duration=None, rating=None): studioTitle = title #maybe original's plugin name? tvShowTitle = title sourceType = SourceType.FOLDER sourceId = path super(KodiFolder, self).__init__(title, studioTitle, tvShowTitle, description, thumb, sourceType, sourceId) self.path = path self.description = description self.date = date self.dateType = dateType self.duration = duration self.rating = rating #self.path = self.path.replace('\\', '/') #HIGHLY UNSURE IF THIS WORKS FOR ALL CASES cacheFileName = str(uuid.uuid4()) + '.kfd' cacheFile = File.fromNameAndDir(cacheFileName, KODI_FOLDER_CACHE_DIR) self.cacheFile = cacheFile pathDic.setIfNonExistent(path, cacheFile.fullpath) self._contentsUpdatedAt = None self._cacheMode = False self.cache() global foldersLoaded foldersLoaded[path] = self
def cache(self): playlistFile = File.fromInvalidNameAndDir(PLAYLIST_FILE, GENERAL_CACHE_DIR) playlistFile.dumpObject(self.cvls)
def setUp(self): self.f = File("test_file")
def updateInfo(self, snippet, channelId, contentDetails=None, statistics=None, videoCount=None, subNewItemCount=None, updatedInfoAt=datetime.now()): title = snippet['title'] studioTitle = title tvShowTitle = '%s Uploads' %title description = snippet['description'] youtubeThumb = Thumb(snippet['thumbnails']) thumb = youtubeThumb.get(s.sourceThumbres) #self.youtubeThumb = youtubeThumb sourceType = SourceType.CHANNEL sourceId = channelId super(Channel, self).__init__(title, studioTitle, tvShowTitle, description, thumb, sourceType, sourceId) self.channelId = channelId cacheFileName = self.channelId + CACHE_FILE_EXTENSION self.cacheFile = File.fromNameAndDir(cacheFileName, CHANNEL_CACHE_DIR) if self.username: usernameDic.setIfNonExistent(self.username, channelId) if contentDetails: relatedPlaylists = contentDetails['relatedPlaylists'] uploadsPID = relatedPlaylists['uploads'] if (self._uploadPlaylist is None) or (self._uploadPlaylist and self._uploadPlaylist.playlistId != uploadsPID): self._uploadPlaylist = Playlist.Playlist(uploadsPID, None, channelSource=self) self.videos = self._uploadPlaylist.videos #self._uploadPlaylist.fetchInfo() if self.mine: likesPID = relatedPlaylists['likes'] favoritesPID = relatedPlaylists['favorites'] watchHistoryPID = relatedPlaylists['watchHistory'] watchLaterPID = relatedPlaylists['watchLater'] self.likesPL = Playlist.Playlist(likesPID, None, channelSource=self) self.favoritesPL = Playlist.Playlist(favoritesPID, None, channelSource=self) self.watchHistoryPL= Playlist.Playlist(watchHistoryPID,None, channelSource=self) self.watchLaterPL = Playlist.Playlist(watchLaterPID, None, channelSource=self) if statistics: self.viewCount = int(statistics['viewCount']) self.commentCount = int(statistics['commentCount']) self.subscriberCount = int(statistics['subscriberCount']) if statistics['videoCount'] != 0: self.videoCount = int(statistics['videoCount']) if videoCount and videoCount!=0: self.videoCount = videoCount if subNewItemCount: self.subNewItemCount = subNewItemCount #this is for subscription channels if self.playlists is None: self.playlists = Pages(self._playlistsRequest, self. _playlistsResponseProcesse, ItemType.VSOURCE, 'channel playlists', s.channelPlaylistsCacheTime, TimeUnit.DAYS, self) if self.mine: global mineLoaded mineLoaded = self else: global channelsLoaded if self.channelId in channelsLoaded: if channelsLoaded[self.channelId] != self: raise ValueError('Channel is in memory but with a different instance. This should never happen.') else: channelsLoaded[self.channelId] = self self._gotInfo = True self._updatedInfoAt = updatedInfoAt self.cache()
def add(vSourceId, vSourceFile, sourceType, relStartPath=''): # import add_to_collection_browse2 # from src.file import Folder # # myCollectionFolder = Folder.fromFullpath(MY_COLLECTIONS_DIR) # add_to_collection_browse2.browse(myCollectionFolder) # return if sourceType == SourceType.FOLDER: parseMethod = ParseMethod.NORMAL useInFeed = True kodiFolder = KodiFolder.fromPath(vSourceId) if not kodiFolder.videos(): result = noVideosDialog(kodiFolder) if result == -1: return if result == 0: useInFeed = False if result == 1: parseMethod = ParseMethod.FIRST_IN_FOLDER kfTitle = customTitleDialog(kodiFolder).strip() if not kfTitle: return vSource = kodiFolder path = router.addToCollectionBrowseUrl(relStartPath + '/') selection = dialog.browse(BrowseType.SHOW_AND_GET_FILE, BROWSE_DIALOG_HEADING, 'files', useThumbs=True, default=path) if selection == path: #this means the user clicked cancel, for whatever reason (xbmc shit...) return if selection.startswith(NEW_COLLECTION_STR): import create_new_collection relativePath = selection[len(NEW_COLLECTION_STR):] path = MY_COLLECTIONS_DIR + relativePath create_new_collection.create(path, showSuccessDialog=False) add(vSourceId, vSourceFile, sourceType, USE_LAST_FOLDER_STR) return if selection.startswith(NEW_FOLDER_STR): import create_new_folder relativePath = selection[len(NEW_FOLDER_STR):] path = MY_COLLECTIONS_DIR + relativePath create_new_folder.create(path, showSuccessDialog=False) add(vSourceId, vSourceFile, sourceType, USE_LAST_FOLDER_STR) return collectionFile = File.fromFullpath(selection) collection = Collection.fromFile(collectionFile) if collection.hasSource(vSourceId): cSource = collection.getCSource(vSourceId) alreadyInCollectionDialog(cSource.videoSource, collection) return if sourceType == SourceType.FOLDER: cSource = collection.addCollectionSource(kodiFolder, useInFeed=useInFeed, kodiParseMethod=parseMethod, customTitle=kfTitle) else: if sourceType == SourceType.CHANNEL: vSource = Channel.fromCacheFile(vSourceFile) else: vSource, needsInfoUpdate = Playlist.fromPlaylistId(vSourceId) cSource = collection.addCollectionSource(vSource) collection.writeCollectionFile() successDialog(cSource, collection)
class test_File(TestCase): def setUp(self): parser = RegexParser(regex='(?P<match_left>"version"\s*:\s*(?:"))(?P<version>(?:(?:\d+)+.?)+)(?P<match_right>")') self.file = File("test", "test_file", os.path.join(dirname(dirname(__file__)), 'test_resources', 'test_file'), parser) self.file.update_version('0.2.0') def test_parser_none(self): try: File("test", "test_file", os.path.join(dirname(dirname(__file__)), 'test_resources', 'test_file'), None) self.assertFalse(True) except Exception as e: self.assertEqual("Parser must be given.", str(e)) def test_no_version_definition_is_found(self): out = StringIO() sys.stdout = out path = os.path.join(dirname(dirname(__file__)), 'test_resources', 'test_file') parser = RegexParser(regex='(?P<match_left>"version"\s*=\s*(?:"))(?P<version>(?:(?:\d+)+.?)+)(?P<match_right>")') self.file = File("test", "test_file", path, parser) self.assertIsNone(self.file.current_version) self.assertEqual("%sNo version definition is found in file %s" % (Fore.WHITE, path), out.getvalue().strip()) def test_current_version(self): # current_version test self.assertEquals('0.2.0', self.file.current_version) def test_update_version(self): # update_version test self.file.update_version('0.2.1') self.assertEqual('0.2.1', self.file.current_version) self.file.update_version('0.2.0') def test_update_with_invalid_version(self): try: self.file.update_version('a.b.c') self.assertFalse(True) except Exception as e: self.assertTrue("Invalid version" in str(e)) self.assertEqual('0.2.0', self.file.current_version) def test_bump_version(self): # update_version test self.assertEqual('0.2.0', self.file.current_version) self.file.bump_version('patch') self.assertEqual('0.2.1', self.file.current_version) self.file.bump_version('minor') self.assertEqual('0.3.0', self.file.current_version) self.file.bump_version('patch') self.assertEqual('0.3.1', self.file.current_version) self.file.bump_version('major') self.assertEqual('1.0.0', self.file.current_version) try: self.file.bump_version('wrongbump') self.assertFalse(True) except Exception as e: self.assertEqual("wrongbump is not one among ['major', 'minor', 'patch']", str(e))
def setUp(self): parser = RegexParser(regex='(?P<match_left>"version"\s*:\s*(?:"))(?P<version>(?:(?:\d+)+.?)+)(?P<match_right>")') self.file = File("test", "test_file", os.path.join(dirname(dirname(__file__)), 'test_resources', 'test_file'), parser) self.file.update_version('0.2.0')
def signOut(): global credentials credentialsFile = File.fromNameAndDir(CREDENTIALS_FILE, YOUTUBE_DATA_DIR) credentialsFile.deleteIfExists() credentials = None