def push(): utils.validate_auth() access_token = utils.get_access_token() user_id = utils.get_user_id() root_directory = f'{__prefix__}/resources' utils.ensure_directory(root_directory) backup = dict() for date in utils.loop_dir(root_directory): backup[date] = dict() for operation in utils.loop_dir(f'{root_directory}/{date}'): backup[date][operation] = dict() leaf_directory = f'{root_directory}/{date}/{operation}' for f in utils.loop_dir(leaf_directory): backup[date][operation][f] = utils.read_file(leaf_directory, f) encrypted_text = utils.encrypt(json.dumps(backup)) response = requests.put( f'{__host__}/{user_id}', headers={ 'BJ_REQUEST_ORIGIN': 'bj.cli', 'BJ_SERVER_ACCESS_TOKEN': access_token, }, data=encrypted_text, ) if response.status_code == 204: print("Successfully pushed bullet journal to remote server.") else: print("Something went wrong.")
def main(): utils.ensure_directory(bullet_journal.__prefix__) if len(sys.argv) == 1: return f'Welcome to Bullet Journal Toolkit Version {bullet_journal.__version__}!' try: return dispatch(sys.argv[1:]) except exceptions.BulletJournalException as exc: return '{}: {}'.format(exc.__class__.__name__, exc.args[0])
def pull(): utils.validate_auth() access_token = utils.get_access_token() user_id = utils.get_user_id() response = requests.get( f'{__host__}/{user_id}', headers={ 'BJ_REQUEST_ORIGIN': 'bj.cli', 'BJ_SERVER_ACCESS_TOKEN': access_token, }, ) if response.status_code == 200: print("Successfully pulled bullet journal from remote server.") else: print("Something went wrong.") backup = json.loads(utils.decrypt(response.text)) utils.cleanup() root_directory = f'{__prefix__}/resources' utils.ensure_directory(root_directory) for date in backup: utils.ensure_directory(f'{root_directory}/{date}') for operation in backup[date]: leaf_directory = f'{root_directory}/{date}/{operation}' utils.ensure_directory(leaf_directory) for f in backup[date][operation]: utils.write_file(leaf_directory, backup[date][operation][f], file_name=f)
def migrate(date): utils.is_valid_date(date) directory_add = f'{__prefix__}/resources/{date}/add' directory_migrate = f'{__prefix__}/resources/{date}/migrate' utils.ensure_directory(directory_add) utils.ensure_directory(directory_migrate) records = list() print(f"Active items in {date}:") for f in utils.loop_dir(directory_add): records.append(f) print(f"Item {len(records)}: {utils.read_file(directory_add, f)}") if not records: print(f"There is no active item in {date}.") exit(0) choices = f"1-{len(records)}" if len(records) > 1 else "1" index = input(f"Index of item to migrate in {date} [{choices}]: ") try: i = int(index) except ValueError: #Handle the exception print("Please enter an integer") exit(1) file_name = records[i-1] utils.move_file(directory_add, directory_migrate, file_name) dst_date = input(f"Date to move item to [yyyy_mm_dd] (default {utils.tomorrow()}):") if not utils.is_valid_date(dst_date): dst_date = utils.tomorrow() print(f"Migrated item to default date: {dst_date}") directory_dst = f'{__prefix__}/resources/{dst_date}/add' utils.ensure_directory(directory_dst) utils.write_file(directory_dst, utils.read_file(directory_migrate, file_name))
def delete(date): utils.is_valid_date(date) directory_add = f'{__prefix__}/resources/{date}/add' utils.ensure_directory(directory_add) records = list() print(f"Active items in {date}:") for f in utils.loop_dir(directory_add): records.append(f) print(f"Item {len(records)}: {utils.read_file(directory_add, f)}") if not records: print(f"There is no active item in {date}.") exit(0) choices = f"1-{len(records)}" if len(records) > 1 else "1" index = input(f"Index of item to delete in {date} [{choices}]: ") try: i = int(index) except ValueError: #Handle the exception print("Please enter an integer") exit(1) file_name = records[i - 1] utils.remove_file(directory_add, file_name)
def daily_log(date): utils.is_valid_date(date) directory_add = f'{__prefix__}/resources/{date}/add' directory_close = f'{__prefix__}/resources/{date}/close' directory_migrate = f'{__prefix__}/resources/{date}/migrate' utils.ensure_directory(directory_add) utils.ensure_directory(directory_close) utils.ensure_directory(directory_migrate) for f in utils.loop_dir(directory_add): print(f"+ {utils.read_file(directory_add, f)}") for f in utils.loop_dir(directory_close): print(f"x {utils.read_file(directory_close, f)}") for f in utils.loop_dir(directory_migrate): print(f"> {utils.read_file(directory_migrate, f)}")
def add(date): utils.is_valid_date(date) directory = f'{__prefix__}/resources/{date}/add' utils.ensure_directory(directory) content = input(f"Add item to {date}: ") utils.write_file(directory, content)