def download(working_directory, config): download_spec = get_download_spec(config) fs = filesystem.Filesystem(working_directory) try: for album in download_spec: dirname = os.path.join(working_directory, album.name) if not os.path.exists(dirname): print ("Making directory {}".format(dirname)) os.mkdir(dirname) num_photos = len(album.photos) for idx, photo in enumerate(album.photos, 1): filename = get_photo_filename(photo.name, photo.filetype, idx, num_photos, album.name) file_identifier = get_file_id(album.identifier, photo.identifier) @retry(NETWORK_EXCEPTIONS) def download(url, path): urllib.request.urlretrieve(url, path) def creator(path, try_num=0): print(" -- Downloading {}".format(path)) download(photo.get_url(), path) fs.add(file_identifier, filename, creator) fs.finish_sync() except KeyboardInterrupt: pass finally: print('Saving filesystem state') fs.save()
def test_second_sync_with_new_name(self): mock_fsops = MockFilesystemOperations() mock_creator1 = MockCreator() mock_creator2 = MockCreator() mock_creator3 = MockCreator() mock_creator4 = MockCreator() fs = filesystem.Filesystem('dummy dir', fsops=mock_fsops) # First sync fs.add('a', 'name1', mock_creator1.creator) fs.add('b', 'name2', mock_creator2.creator) fs.finish_sync() # Second sync, b has new name fs.add('a', 'name1', mock_creator3.creator) fs.add('b', 'name3', mock_creator4.creator) fs.finish_sync() self.assertTrue(mock_creator1.creator_called) self.assertTrue(mock_creator2.creator_called) self.assertFalse(mock_creator3.creator_called) self.assertFalse(mock_creator4.creator_called) self.assertTrue(fs.state.has_identifier('a')) self.assertTrue(fs.state.has_identifier('b')) self.assertEqual('name1', fs.state.get_filename('a')) self.assertEqual('name3', fs.state.get_filename('b')) self.assertFalse(mock_fsops.delete_called)
def test_second_sync_adds_file_with_same_name(self): mock_fsops = MockFilesystemOperations() mock_creator1 = MockCreator() mock_creator2 = MockCreator() mock_creator3 = MockCreator() mock_creator4 = MockCreator() mock_creator5 = MockCreator() fs = filesystem.Filesystem('dummy dir', fsops=mock_fsops) # First sync fs.add('a', 'name1', mock_creator1.creator) fs.add('b', 'name2', mock_creator2.creator) fs.finish_sync() # Second sync, c takes name of b fs.add('a', 'name1', mock_creator3.creator) fs.add('c', 'name2', mock_creator4.creator) fs.add('b', 'name3', mock_creator5.creator) self.assertTrue(mock_creator1.creator_called) self.assertTrue(mock_creator2.creator_called) self.assertFalse(mock_creator3.creator_called) self.assertTrue(mock_creator4.creator_called) self.assertFalse(mock_creator5.creator_called) self.assertTrue(fs.state.has_identifier('a')) self.assertTrue(fs.state.has_identifier('b')) self.assertTrue(fs.state.has_identifier('c')) self.assertEqual('name1', fs.state.get_filename('a')) self.assertEqual('name3', fs.state.get_filename('b')) self.assertEqual('name2', fs.state.get_filename('c')) self.assertTrue('name1' in fs.state.touched_filenames) self.assertTrue('name2' in fs.state.touched_filenames) self.assertTrue('name3' in fs.state.touched_filenames) self.assertFalse(mock_fsops.delete_called)
def setUp(self): self.__mediator = mediator.Mediator() self.__redis_boundary = redis_boundary.RedisBoundary(self.__mediator) self.__filesystem = filesystem.Filesystem() self.__raspberry_pi = RespberryPiBoundaryMock() self.__mediator.initialize(self.__redis_boundary, self.__filesystem, self.__raspberry_pi) # サービスの開始 self.__mediator.start()
def test_add_file(self): mock_fsops = MockFilesystemOperations() mock_creator = MockCreator() fs = filesystem.Filesystem('dummy dir', fsops=mock_fsops) fs.add('a', 'name1', mock_creator.creator) self.assertTrue(mock_creator.creator_called) self.assertTrue(fs.state.has_identifier('a')) self.assertEqual('name1', fs.state.get_filename('a'))
def test_complex_case(self): mock_fsops = MockFilesystemOperations() def mock_delete(x): print("Delete {}".format(x)) mock_fsops.delete = mock_delete mock_creator = MockCreator() mock_creator_a = MockCreator() mock_creator_c = MockCreator() mock_creator_d = MockCreator() mock_creator_e = MockCreator() mock_creator_f = MockCreator() fs = filesystem.Filesystem('dummy dir', fsops=mock_fsops) # First sync print('start') fs.add('a', 'name1', mock_creator.creator) fs.add('b', 'name2', mock_creator.creator) fs.add('c', 'name3', mock_creator.creator) fs.add('d', 'name4', mock_creator.creator) fs.finish_sync() # Second sync: # - b is removed # - c and d switches name # - e is added with name of b # - f is added fs.add('a', 'name1', mock_creator_a.creator) fs.add('c', 'name4', mock_creator_c.creator) fs.add('d', 'name3', mock_creator_d.creator) fs.add('e', 'name2', mock_creator_e.creator) fs.add('f', 'name6', mock_creator_f.creator) fs.finish_sync() print('done') self.assertFalse(mock_creator_a.creator_called) self.assertFalse(mock_creator_c.creator_called) self.assertFalse(mock_creator_d.creator_called) self.assertTrue(mock_creator_e.creator_called) self.assertTrue(mock_creator_f.creator_called) self.assertTrue(fs.state.has_identifier('a')) self.assertFalse(fs.state.has_identifier('b')) self.assertTrue(fs.state.has_identifier('c')) self.assertTrue(fs.state.has_identifier('d')) self.assertTrue(fs.state.has_identifier('e')) self.assertTrue(fs.state.has_identifier('f')) self.assertEqual('name1', fs.state.get_filename('a')) self.assertEqual('name4', fs.state.get_filename('c')) self.assertEqual('name3', fs.state.get_filename('d')) self.assertEqual('name2', fs.state.get_filename('e')) self.assertEqual('name6', fs.state.get_filename('f'))
def test_add_file_when_filename_exists(self): mock_fsops = MockFilesystemOperations() mock_creator1 = MockCreator() mock_creator2 = MockCreator() fs = filesystem.Filesystem('dummy dir', fsops=mock_fsops) fs.add('a', 'name1', mock_creator1.creator) fs.add('b', 'name1', mock_creator2.creator) fs.finish_sync() self.assertTrue(mock_creator1.creator_called) self.assertTrue(mock_creator2.creator_called) self.assertTrue(fs.state.has_identifier('b')) self.assertFalse(fs.state.has_identifier('a')) self.assertTrue(mock_fsops.delete_called)
import filesystem import raspberry_pi_boundary import mediator import time if __name__ == '__main__': logging.basicConfig(filename='logs/ir_receiver.log', level=logging.DEBUG) logger = logging.getLogger(__name__) sh = logging.StreamHandler() logger.addHandler(sh) #各種コンポーネントの初期化 logger.debug('Received ir_receiver service starting') __mediator = mediator.Mediator() __redis_boundary = redis_boundary.RedisBoundary(__mediator) __filesystem = filesystem.Filesystem() __raspberry_pi = raspberry_pi_boundary.RespberryPiBoundary() __mediator.initialize(__redis_boundary, __filesystem, __raspberry_pi) try: # サービスの開始 __mediator.start() logger.debug('Received ir_receiver service started') while True: time.sleep(0.1) except KeyboardInterrupt: logger.debug('KeyboardInterrupt') __mediator.stop()
def __init__(self): self.fs = filesystem.Filesystem() self.cfg = config.Config() self.cmds = commands.Commands() self.board = self.fs.load() self.reader = reader.Reader()
import filesystem import config import helpers import mailing fs = filesystem.Filesystem() cfg = config.Config() hlp = helpers.Helpers() mail = mailing.Mailing() def main(): print_hello() def print_hello(): print('\n Nano Shopping List') print(' ------------------\n') main()
file_update = 2 with open('parameter.json') as f: data = json.load(f) CONNECTION = data['CONNECTION'] HEADER_LENGTH = data['HEADER_LENGTH'] IP = data["IP"] PORT = data['PORT'] CONNECTION = data['CONNECTION'] #need the root of the directory root = data["ROOT"] vice_file = FUSE(filesystem.Filesystem(root), "/", nothreads=True, foreground=True, **{'allow_other': True}) # Initialize and bind socket for stream and resuse same socket for # reconnection if it goes down venus_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) venus_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) venus_socket.bind((IP, PORT)) #All sockets including venus sockets_list = [venus_socket] #client socket is key and data is value vice_data = {}