Sfoglia il codice sorgente

Added support for datetime formatting in output naming

master
Roxie Gibson 4 anni fa
parent
commit
ed41fdc7ac
Non sono state trovate chiavi note per questa firma nel database
2 ha cambiato i file con 42 aggiunte e 5 eliminazioni
  1. +19
    -1
      readme.md
  2. +23
    -4
      seatingplan/__main__.py

+ 19
- 1
readme.md Vedi File

@@ -75,7 +75,25 @@ Replace `CLIENT_ID`, `CLIENT_SECRET`, and `TENANT_ID` with the values from the A

### Output (Optional but recommended)

***ADD ADVICE HERE ABOUT DATETIME FORMATTING AND HOW TO NAME THE OUTPUT CSV HERE WHEN YOU FINISH ALL THAT CODE***
You can configure the output path too. Normally, the script will output a file called `Seating Plan.csv` in the directory you ran the script in. This can be edited with the `-o` flag. We can put the file in a different folder and have a different name like this:

```sh
seatingplan -c ~/.config/seatingplan.yaml -o "/path/to/file"
```

For example, we can generate a csv in our users Documents folder and name it "Who's in office?"

```sh
seatingplan -c ~/.config/seatingplan.yaml -o "~/Documents/Who's in office" # If you don't provide a .csv file extension, it will be added for you.
```

This also supports datetime formatting. This can be done using Python's formatting codes for datetime [which you can find the docs for here.](https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior) The datetime provided to this function will be the datetime when the script it run.

```sh
seatingplan -c ~/.config/seatingplan.yaml -o "Seating Plan {:%Y-%m-%d}"
```

This will output a file called `Seating Plan 2019-09-12.csv`

## Running the program


+ 23
- 4
seatingplan/__main__.py Vedi File

@@ -33,6 +33,23 @@ def read_config_file(config_path):
return config


def format_output_path(output_path):
"""
Checks the string for datetime formatting and formats it if possible.
:param output_path: str of the output path
:return: str of the new output path
"""
try:
new_path = output_path.format(datetime.now())
if new_path.split(".")[-1] != "csv":
new_path += ".csv"
return new_path
except SyntaxError:
print("Invalid formatting pattern given for output_path. Cannot name output_path. Exiting...")
exit(1)

def parse_args():
"""
Parses arguments from the commandline.
@@ -40,18 +57,20 @@ def parse_args():
:return: config yaml file as a dict
"""
args = parser.parse_args()
output_path = format_output_path(args.output_path)

if args.config_path:
# Read the file provided and return the required config
config = read_config_file(args.config_path)
config["config_path"] = args.config_path
config["output_path"] = args.output_path
config["output_path"] = output_path
config["users"] = sorted([x.lower() for x in config["users"]]) # make all names lowercase and sort alphabetically
return config

else:
# If the code has gotten here, then a config can't be parsed so we must close the program
print("Cannot login. No config file was provided.")
print("Cannot login. No config file was provided. Exiting...")
exit(1)


@@ -180,9 +199,9 @@ def get_ooo_list(email: str, connection: Connection):
attendees = [event["Organizer"]]
if (end - start) <= timedelta(days=1):
# Event is for one day only, check if it starts
# Event is for one day only, check if it starts within the week
if monday <= start <= friday:
# Event is within the week we are looking at, add all attendees +
# Event is within the week we are looking at, add all attendees
weekday = outofoffice[start.weekday()]
weekday = add_attendees_to_ooo_list(attendees, weekday)
else:

Loading…
Annulla
Salva