Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I make it so my code works so my command line can look like python3 edfs.py -create /user/file instead of python3 edfs.py -create

How can I make it so my code works so my command line can look like "python3 edfs.py -create /user/file" instead of "python3 edfs.py -create /user file" as it only works with spaces between. I need the path to work without putting space between each one. This should mean the very last argument in the path is the file that is created or used while the rest is the path.

image text in transcribed

here is the code transcript which is should be pictured above

import sys

import json

import requests

import base64

# Firebase URL

firebase_url = "https://test-80e8c-default-rtdb.firebaseio.com/"

# Function to get the data from Firebase

def get_firebase_data(url):

response = requests.get(url)

data = json.loads(response.text)

return data

# Function to list all files and directories under a given directory

def list_directory(dir_path):

data = get_firebase_data(firebase_url + dir_path + ".json")

if isinstance(data, dict):

for key in data.keys():

if key == "content":

print(dir_path + ": " + data[key])

else:

list_directory(dir_path + "/" + key)

# Function to create a new file with the given content

def create_file(file_path, content):

data = {"content": content}

response = requests.put(firebase_url + file_path + ".json", data=json.dumps(data))

if response.status_code == 200:

print("File created successfully")

elif response.status_code == 400:

print("File already exists")

# Function to create a new directory

def create_directory(dir_path):

data = {}

response = requests.put(firebase_url + dir_path + ".json", data=json.dumps(data))

if response.status_code == 200:

print("Directory created successfully")

elif response.status_code == 400:

print("Directory already exists")

# Function to remove a file or directory

def remove(path):

response = requests.delete(firebase_url + path + ".json")

if response.status_code == 200:

print("File/directory removed successfully")

elif response.status_code == 404:

print("File/directory not found")

# Function to export the file system structure in XML format

def export_xml():

root_data = get_firebase_data(firebase_url + ".json")

print("")

for key in root_data.keys():

print("")

export_directory(key)

print("" + key + ">")

print("")

# Function to export a directory and its contents in XML format

def export_directory(dir_path):

data = get_firebase_data(firebase_url + dir_path + ".json")

if isinstance(data, dict):

for key in data.keys():

if key == "content":

content = data[key]

print("" + base64.b64encode(content.encode('utf-8')).decode('utf-8') + "")

else:

print("")

export_directory(dir_path + "/" + key)

print("")

# Parse the command-line arguments

args = sys.argv

if len(args)

print("Usage: python3 edfs.py [content]")

exit()

command = args[1]

path = args[2]

if command == "-create":

if len(args)

print("Usage: python3 edfs.py -create ")

exit()

content = args[3]

create_file(path, content)

elif command == "-mkdir":

create_directory(path)

elif command == "-rmdir" or command == "-rm":

remove(path)

elif command == "-ls":

list_directory(path)

elif command == "-export":

export_xml()

else:

print("Invalid command: " + command)

print("Usage: python3 edfs.py [content]")

import sys impont json import requests import base 64 \# Firebase URL firebase_ur1 = "YoUA_FIREBASE_URL" 4 Function to get the data from Firebase def get_firebase_data(url) response = requests, get(ur1) data = json. loads (response . text) return data \# Function to list a11 files and directories under a given directory def list_directory (dir_path) if isinstance(data, dict): for key in data.keys () if key = "content" print (dir_path +":7+ data[key ]) else: list_directory (dir_path +/+ key) \# Function to create a new file with the given content def create_file(file_path, content): data = \{"content" : content } if response.status_code =200 : print ("File created successfully") elif response:status_code =49 : print("File already exists") * Function to create a new directory def create_directory(dir_path): data ={} response = requests. put (fi rebase_ur 1+dir_path+n. json", data=json, dumps ( data ) ) if response.status_code =20 : print ("Directory created successfully") elif response, status_code ==49 : print("Directory already exists") * Function to remove a file ar directory def remove(path): response = requests. delete (firebase_url + path + ".j5on") if response. status_code ==20e : print ("File/directory removed successfully n ) print ("File/directory not found") \# Function to export the file systen structure in XML format def export_xm1(): root_data = get_firebase_data(firebase_url + ".json") print ( sroot)" for key in root_data.keys (): print ("e" + key + ">") export_directory (key) print ("n) print (" root ) \# Function to export a directory and its contents in XML fonmat def export_directory (dir_path) : data = get_firebase_data(firebase_ur 1+ dir_path ++ json ) if isinstance(data, dict) for key in data.keys(): if key = "content" : content = data[key] print("sfile name-" dir_path +n+>+ base64, b64encode (content. encode('ut f - 81)). decode('utf- else: print("edirectory name=" + key ++ ) export_directory (dir_path +" " key) print ("s/directory)") \# Parse the command-line arguments args = sys. argv if len(args)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions

Question

a. What is the purpose of the team?

Answered: 1 week ago

Question

a. How are members selected to join the team?

Answered: 1 week ago

Question

b. What are its goals and objectives?

Answered: 1 week ago