class TestStorageKeyring(unittest.TestCase): data = ( "https://python-reference.readthedocs.io/en/latest/docs/statements/assert.html" ) def setUp(self): """Surpress RessourceWarnings. Link: https://github.com/jaraco/keyring/issues/380&https://github.com/jaraco/keyring/pull/381 """ with warnings.catch_warnings(): warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning) self.storage = Storage(backend=Storage.STORAGE_BACKEND_KEYRING) self.storage.del_data() self.storage.set_data(self.data) def tearDown(self): self.storage.del_data() def test_get_data_keyring(self): rdata = self.storage.get_data() self.assertEqual(self.data, rdata) def test_del_data_keyring(self): self.storage.del_data() self.assertIsNone(self.storage.get_data())
def __init__(self): while True: self.config = Configuration() self.storage = Storage() self.page = PageLoader() self.page_text = self.page.get_page().text # получаем ссылки на все моты на всех страницах bikes = [] # первая страница bikes_page = self.get_bikes(self.page_text) for bike in bikes_page: bikes.append(bike) pages = self.get_pages() for page in pages: new_page = PageLoader() page_text = new_page.get_page_by_url(page).text bikes_page = self.get_bikes(page_text) for bike in bikes_page: bikes.append(bike) bike_infos = [] for bike in bikes: new_page = PageLoader() page_text = new_page.get_page_by_url(bike).text bike_infos.append(self.get_bike_info(page_text)) # чистим хранилище перед инсертом self.storage.clear() self.storage.insert_many(bike_infos) time.sleep(int(self.config.interval))
def action_read(args): encoded_file = os.path.expanduser(args.read) if not os.path.isfile(encoded_file): print("{0} file not found.".format(encoded_file)) sys.exit(1) # check permissions 0600 st = os.stat(encoded_file) if oct(st.st_mode) > OSEnvController.FILE_PERMISSION: print("File {0} has the wrong permissions. \"0600\" are required.". format(encoded_file)) sys.exit(1) # read from file userkey = None try: userkey = getpass.getpass( prompt="Input master key and press [ENTER]: ") if userkey is not None and len(userkey) != 44: print("Wrong key length {length}, encryption process aborted!". format(length=len(userkey))) sys.exit(1) except Exception as error: print("ERROR key", error) return None crypto = FileEncryption() json_data = crypto.decrypt_configfile(input_file=encoded_file, key=userkey) # save to keyring storage = Storage() storage.set_data(json_data.decode("utf-8"))
def __init__(self): self._directory = get_library_path() if not os.path.isdir(self._directory): raise ValueError(translate(30135)) self._add_unaired_episodes = add_unaired_episodes() self._add_specials = add_special_episodes() self._update_kodi_library = update_kodi_library() self._movies_directory = os.path.join(self._directory, "Movies") self._shows_directory = os.path.join(self._directory, "TV Shows") if not os.path.exists(self._movies_directory): os.makedirs(self._movies_directory) if not os.path.exists(self._shows_directory): os.makedirs(self._shows_directory) self._storage = Storage(os.path.join(self._directory, "library.sqlite")) self._table_name = "library" self._storage.execute_and_commit( "CREATE TABLE IF NOT EXISTS `{}` (" "id INTEGER NOT NULL, " "type TEXT NOT NULL, " "path TEXT CHECK(path <> '') NOT NULL, " "PRIMARY KEY (id, type)" ");".format(self._table_name))
def setUp(self): """Surpress RessourceWarnings. Link: https://github.com/jaraco/keyring/issues/380&https://github.com/jaraco/keyring/pull/381 """ with warnings.catch_warnings(): warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning) self.storage = Storage(backend=Storage.STORAGE_BACKEND_KEYRING) self.storage.del_data() self.storage.set_data(self.data)
def compare_shelf_current(self): db = Storage() stored = db.book_paths_list() closed = db.close() try: self.books except Exception: self.filter_books() on_disk, in_storage = [], [] for _x in self.books: on_disk.append(_x) for _y in stored: in_storage.append(_y[0]) a, b, = set(on_disk), set(in_storage) c = set.difference(a, b) return c
def action_environment(args): """Load ENV from keyrings to active SESSION.""" storage = Storage() json_data = storage.get_data() data = json.loads(json_data) # print("List of available environments:\n") if data: for osenv in data: env = next(iter(osenv)).split("_")[0] if env == args.environment: tenant = Tenant(env) tenant.import_data(data=osenv, prefix=env) print(tenant.export_ostackrc()) else: print("No environments loaded...")
class TestStorageSession(unittest.TestCase): data = ( "https://python-reference.readthedocs.io/en/latest/docs/statements/assert.html" ) def setUp(self): self.storage = Storage(backend=Storage.STORAGE_BACKEND_SESSION) self.storage.del_data() self.storage.set_data(self.data) def tearDown(self): self.storage.del_data() def test_get_data_session(self): rdata = self.storage.get_data() self.assertEqual(self.data, rdata) def test_del_data_session(self): with captured_output() as (del_out, del_err): self.storage.del_data self.assertIn(del_out.getvalue().strip(), "unset") try: rdata = os.environ[Storage.SESSION_STORAGE_VARIABLE] self.assertIsNot(self.data, rdata) except KeyError: self.assertIsNone(None)
def action_list(): """Show available ENV's from keyring.""" storage = Storage() if (storage.active_storage_backend == storage.STORAGE_BACKEND_SESSION and os.environ.get(Storage.SESSION_STORAGE_VARIABLE) is None): print("Active session not found. Please load with \"{0} -r\".". format(_program)) sys.exit(1) storage = Storage() json_data = storage.get_data() data = json.loads(json_data) # print("List of available environments:\n") if data: for osenv in data: env = next(iter(osenv)).split("_")[0] print("{0}".format(env)) else: print("No environments loaded...")
def import_books(self, list=None): book_list = self.compare_shelf_current() db = Storage() for book in book_list: book = self.process_book(book) extracted = self.extract_metadata(book) db.insert_book(extracted) inserted = db.commit() if inserted is not True: print(inserted) if input('Continue ? y/n') == 'y': pass db.close()
def test_self_check_backend(self): """Surpress RessourceWarnings. Link: https://github.com/jaraco/keyring/issues/380&https://github.com/jaraco/keyring/pull/381 """ with warnings.catch_warnings(): warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning) status = False self.storage = Storage() if (self.storage.active_storage_backend == self.storage.STORAGE_BACKEND_KEYRING or self.storage.active_storage_backend == self.storage.STORAGE_BACKEND_SESSION): status = True self.assertTrue(status)
def main(): app = App() try: # exec mysqldump filepath = Mysqldump(app).run() # exec sendmail (parallel) if app.config.use_sendmail: Sendmail(app, filepath).run() # exec webdav (parallel) if app.config.use_storage: Storage(app, filepath).run() except SystemExit: app.logger.critical('error found, process terminated') except Exception as e: app.logger.error(e) finally: sys.exit()
from lib.storage import Storage import cgi print "Content-type: text/plain" form = cgi.FieldStorage() entry = form.getvalue('entry') prompt = form.getvalue('prompt') answer = form.getvalue('answer') if not entry.isdigit(): print """ Incorrect entry id. """ else: try: s = Storage() s.save(entry, prompt, answer) print """ Sucessfully added. """ except Exception as e: print """ %s %s %s %s """ % (e, entry, prompt, answer)
def load(): try: with open('data/storage.pkl', 'rb') as f: return pickle.load(f) except FileNotFoundError: return Storage()
# -*- coding: utf-8 -*- from lib.storage import Storage import re # s = Storage() # c = s.count_meaning('eat') # print c s = Storage() result = s.search_writing('食べる', 10)
from lib.storage import Storage import cgi import re form = cgi.FieldStorage() subj = form.getvalue('subj') s = Storage() def is_alpha(s): p = '^[a-zA-Z\s]*$' if re.match(p, s): return True return False if subj.isdigit(): result = s.get_id(subj) elif not is_alpha(subj): result = s.search_writing(subj, 10) else: result = s.search_meaning(subj, 10) html = '' html += '<table class="result">' if result: for item in result: html += '<tr>' html += '<td class="main">'
#!/usr/bin/python from lib.logger import Logger from lib.storage import Storage import logging log = logging.getLogger() log.setLevel(logging.INFO) if __name__ == "__main__": store = Storage() logger = Logger(store) logger.run()
import os from lib.arp import get_arp_table from lib.storage import Storage print('[start]') current_dir = os.path.dirname(os.path.abspath(__file__)) database_file = '{}/db.json'.format(current_dir) storage = Storage(database_file) arp_table = get_arp_table() for arp_record in arp_table: storage.store_by_mac_address(arp_record['mac_address'], arp_record) print('-'*130) print('{:<24}{:<24}{:<30}{:<30}{:<30}'.format( 'mac_address', 'ip_address', 'first_seen', 'last_seen', 'last_ip_change', )) print('-'*130) for row in storage.db.all(): print('{:<24}{:<24}{:<30}{:<30}{:<30}'.format( row['mac_address'], row['ip_address'], row['first_seen'].format('YYYY-MM-DD HH:mm:ss') if row['first_seen'] else None, row['last_seen'].format('YYYY-MM-DD HH:mm:ss') if row['last_seen'] else None, row['last_ip_change'].format('YYYY-MM-DD HH:mm:ss') if row['last_ip_change'] else None, ))
import boto3 from lib.storage import Storage import json source_name = getenv("source_name") bucket = getenv("bucket") region = getenv("AWS_REGION") output_format = getenv("output_format") text_type = getenv("text_type") voice_id = getenv("voice_id") language_code = getenv("language_code") sns_topic = getenv("sns_topic") dynamodb_table = getenv("dynamodb_table") domain_name = getenv("domain_name") storage = Storage(bucket) dynamodb = boto3.Session(region_name=region).client("dynamodb") polly = boto3.Session(region_name=region).client("polly") def load(): try: mod = import_module(source_name) podcast_class = getattr(mod, source_name.title()) if not issubclass(podcast_class, getattr(mod, "BasePodcast")): raise Exception("Not a podcast") podcast = podcast_class( source_name=source_name, bucket=bucket, polly=polly, output_format=output_format, text_type=text_type,
def setUp(self): self.storage = Storage(backend=Storage.STORAGE_BACKEND_SESSION) self.storage.del_data() self.storage.set_data(self.data)
from lib.storage import Storage import cgi form = cgi.FieldStorage() id = form.getvalue('id') s = Storage() result = s.get_id(id) result = result[0] html = "" html += "Writing:" html += "<ul>" for w in result.writings: html += "<li>%s</li>" % w.content html += "</ul>" html += "Reading:" html += "<ul>" for r in result.readings: html += "<li>%s</li>" % r.content html += "</ul>" html += "Meaning:" html += "<ul>" for m in result.meanings: #if m.parts: # html += "<li>%s</li>" % '; '.join([p.content for p in m.parts])
from lib.storage import Storage import cgi print "Content-type: text/html" form = cgi.FieldStorage() box = form.getvalue('box') s = Storage() entries = s.retrieve(box, 1) if entries: sample = entries[0] else: sample = 'No entries found.' print """ <html> <head> <title>Kanji Flash</title> <link rel="stylesheet" type="text/css" href="css/main.css" /> </head> <body> %s </body> </html> """ % (sample,)
class Library(object): MOVIE_TYPE = "movie" SHOW_TYPE = "show" def __init__(self): self._directory = get_library_path() if not os.path.isdir(self._directory): raise ValueError(translate(30135)) self._add_unaired_episodes = add_unaired_episodes() self._add_specials = add_special_episodes() self._update_kodi_library = update_kodi_library() self._movies_directory = os.path.join(self._directory, "Movies") self._shows_directory = os.path.join(self._directory, "TV Shows") if not os.path.exists(self._movies_directory): os.makedirs(self._movies_directory) if not os.path.exists(self._shows_directory): os.makedirs(self._shows_directory) self._storage = Storage(os.path.join(self._directory, "library.sqlite")) self._table_name = "library" self._storage.execute_and_commit( "CREATE TABLE IF NOT EXISTS `{}` (" "id INTEGER NOT NULL, " "type TEXT NOT NULL, " "path TEXT CHECK(path <> '') NOT NULL, " "PRIMARY KEY (id, type)" ");".format(self._table_name)) def _storage_has_item(self, item_id, item_type): return self._storage_get_path(item_id, item_type) is not None def _storage_get_path(self, item_id, item_type): row = self._storage.execute( "SELECT path FROM `{}` WHERE id = ? AND type = ?;".format( self._table_name), (item_id, item_type)).fetchone() return row and row[0] def _storage_count_entries(self): return self._storage.count(self._table_name) def _storage_get_entries(self): return self._storage.fetch_items("SELECT * FROM `{}`;".format( self._table_name)) def _storage_count_entries_by_type(self, item_type): return self._storage.execute( "SELECT COUNT(*) FROM `{}` WHERE type = ?;".format( self._table_name), (item_type, )).fetchone()[0] def _storage_get_entries_by_type(self, item_type): return self._storage.fetch_items( "SELECT id, path FROM `{}` WHERE type = ?;".format( self._table_name), (item_type, )) def _storage_add_item(self, item_id, item_type, path): self._storage.execute_and_commit( "INSERT INTO `{}` (id, type, path) VALUES(?, ?, ?);".format( self._table_name), (item_id, item_type, path)) def _add_movie(self, item, name, override_if_exists=True): movie_dir = os.path.join(self._movies_directory, name) if not os.path.isdir(movie_dir): os.makedirs(movie_dir) movie_path = os.path.join(movie_dir, name + ".strm") if override_if_exists or not os.path.exists(movie_path): with open(movie_path, "w") as f: f.write("plugin://{}/providers/play_movie/{}".format( ADDON_ID, item.movie_id)) def add_movie(self, item): if self._storage_has_item(item.movie_id, self.MOVIE_TYPE): logging.debug("Movie %s was previously added", item.movie_id) return False name = item.get_info("originaltitle") year = item.get_info("year") if year: name += " ({})".format(year) name = make_legal_name(name) self._add_movie(item, name) self._storage_add_item(item.movie_id, self.MOVIE_TYPE, name) if self._update_kodi_library: self.update_movies() return True def _add_show(self, item, name, override_if_exists=True): show_dir = os.path.join(self._shows_directory, name) if not os.path.isdir(show_dir): os.makedirs(show_dir) for season in item.seasons(get_unaired=self._add_unaired_episodes): if not self._add_specials and season.season_number == 0: continue for episode in Season(item.show_id, season.season_number).episodes( get_unaired=self._add_unaired_episodes): episode_name = u"{} S{:02d}E{:02d}".format( name, episode.season_number, episode.episode_number) episode_path = os.path.join(show_dir, episode_name + ".strm") if override_if_exists or not os.path.exists(episode_path): with open(episode_path, "w") as f: f.write("plugin://{}/providers/play_episode/{}/{}/{}". format(ADDON_ID, episode.show_id, episode.season_number, episode.episode_number)) def add_show(self, item): if self._storage_has_item(item.show_id, self.SHOW_TYPE): logging.debug("Show %s was previously added", item.show_id) return False name = item.get_info("originaltitle") year = item.get_info("year") if year: name += " ({})".format(year) name = make_legal_name(name) self._add_show(item, name) self._storage_add_item(item.show_id, self.SHOW_TYPE, name) if self._update_kodi_library: self.update_shows() return True def rebuild(self): items_iter = self._storage_get_entries() if is_library_progress_enabled(): items_iter = Progress(items_iter, self._storage_count_entries(), heading=ADDON_NAME, message=translate(30142)) for item_id, item_type, path in items_iter: if item_type == self.MOVIE_TYPE: self._add_movie(Movie(item_id), path) elif item_type == self.SHOW_TYPE: self._add_show(Show(item_id), path) else: logging.error( "Unknown item type '%s' for id '%s' and path '%s'", item_type, item_id, path) if self._update_kodi_library: self.update_movies(wait=True) self.update_shows(wait=True) def update_library(self): items_iter = self._storage_get_entries_by_type(self.SHOW_TYPE) if is_library_progress_enabled(): items_iter = Progress(items_iter, self._storage_count_entries_by_type( self.SHOW_TYPE), heading=ADDON_NAME, message=translate(30141)) for item_id, path in items_iter: logging.debug("Updating show %s on %s", item_id, path) self._add_show(Show(item_id), path, override_if_exists=False) if self._update_kodi_library: self.update_movies(wait=True) self.update_shows(wait=True) def discover_contents(self, pages): include_adult = include_adult_content() api = Discover() pages_iter = range(1, pages + 1) if is_library_progress_enabled(): pages_iter = Progress(pages_iter, pages, heading=ADDON_NAME, message=translate(30140)) for page in pages_iter: for movie in get_movies( api.movie(page=page, include_adult=include_adult))[0]: logging.debug("Adding movie %s to library", movie.movie_id) self.add_movie(movie) for show in get_shows( api.tv(page=page, include_adult=include_adult))[0]: logging.debug("Adding show %s to library", show.show_id) self.add_show(show) if self._update_kodi_library: self.update_movies(wait=True) self.update_shows(wait=True) def update_shows(self, wait=False): LibraryMonitor().start_scan(self._shows_directory, wait) def update_movies(self, wait=False): LibraryMonitor().start_scan(self._movies_directory, wait) def close(self): self._storage.close() def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.close()
#!/usr/bin/python import mimetypes import os import zipfile from http.server import BaseHTTPRequestHandler, HTTPServer from config import Config from lib.library import Catalogue from lib.storage import Storage config = Config() Storage = Storage() class InitFiles: """First run file creation operations""" def __init__(self, file_array): print("Begining creation of file structure") for _pointer in file_array: if not os.path.isfile(_pointer): self.CreateFile(_pointer) print("Concluded file creation") def CreateFile(self, _pointer): """Create the file""" if not os.path.isdir(os.path.split(_pointer)[0]): os.mkdir(os.path.split(_pointer)[0]) f = open(_pointer, "w+") f.close()