Skip to content

Commit

Permalink
Added kattis config to initialize and update.
Browse files Browse the repository at this point in the history
Pass nothing and it will make sure that .kattisrc has all available settings.
Pass a section, an option and a value to mutate.
If section is for file associations and commands, they are added even when option is not present.
  • Loading branch information
Duckapple committed Nov 19, 2019
1 parent dd08c63 commit 84842ca
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\Users\\simon\\AppData\\Local\\Programs\\Python\\Python38-32\\python.exe"
}
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ This is a command line program for getting, testing and submitting Kattis attemp

Credits to Kattis and their own python script for submitting files [(found here)](https://open.kattis.com/help/submit). Our submission script is heavily inspired by theirs.

## Installing:
## Installing

1. First of all download this repository (either by zip or git) and make sure to extract it to a known location.
2. Install dependencies by running `pip3 install -r requirements.txt` inside the directory.
2. Install dependencies by running `python -m pip install -r requirements.txt` inside the directory.
3. Add the directory to %PATH or `ln -s /path/to/kat/kattis.py /usr/bin/kat` (latter only works on unix systems)
4. To use most functionalities, add your personal configuration to this repository. Go to open.kattis.com > Help > How to submit > Download your personal configuaration file and download the resulting file to this folder.
## Usage:
4. To use most functionalities, add your personal configuration to this repository. Go to open.kattis.com > Help > How to submit > Download your personal configuaration file and download the resulting file to this folder as `.kattisrc`.
5. Run `kattis config` to copy Kat-internal configurations to the `.kattisrc` file for you to configure (might we suggest changing `language` under `[kat]`?).

## Usage

### Get

```
```txt
kattis get <problem-name>
```

This downloads the sample input-output files for the problem and creates a directory for the problem. Inside, the test files are put in the subfolder `test`, and the `boilerplate.py` is copied into the folder as `<problem-name>.py`. Happy hacking!

### Test

```
```txt
kattis test <problem-name> [path-to-file]
```

Expand All @@ -31,11 +34,17 @@ If multiple supported source files are present within the problem directory, you

### Submit

```
```txt
kattis submit <problem-name> [path-to-file]
```

This submits a given script to kattis for final judgement.
Remember to get your own configuration file from [here](https://open.kattis.com/help/submit).

If multiple supported source files are present within the problem directory, you will be prompted to choose one. Alternativly you can supply the path to your chosen script as a second argument
If multiple supported source files are present within the problem directory, you will be prompted to choose one. Alternativly you can supply the path to your chosen script as a second argument.

### Other commands

We also provide commands such as `archive`, `unarchive`, `list`, `help`, `read`, `watch`, `web` and `work`.

`@TODO: write information about these commands`
36 changes: 36 additions & 0 deletions commands/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from helpers.config import getConfig, commandConvert

def configCommand(args, options):
if len(args) == 0:
print("Adding/updating configuration to your .kattisrc...")
if getConfig() == -1:
print("""\
Something went wrong in locating the configuration file
for kattis. Have you fetched the .kattisrc? Consult the
README.md for more details.""")
else:
print("Successfully added default configuration to your .kattisrc!")

elif len(args) == 3:
cfg, location = getConfig(shouldReturnLocation=True)
if type(cfg) is int:
print("""\
Something went wrong in locating the configuration file
for kattis. Have you fetched the .kattisrc? Consult the
README.md for more details.""")
elif cfg.has_section(args[0]):
if not cfg.has_option(args[0], args[1]) and args[0].lower() in ["kat", "kattis", "user"]:
print("Setting", args[1], "was not recognized for section [" + args[0] + "]")
else:
cfg.set(args[0], args[1], args[2])
with open(location, "w") as configFile:
cfg.write(configFile)
print("The setting", args[1], "from section [" + args[0] + "]", "was set to", args[2])
else:
print("Section [" + args[0] + "]", "was not found.")

else:
print("""\
Invalid number of arguments, expected
'config <section> <option> <value>'.
Remember to put arguments with spaces in quotes.""")
112 changes: 66 additions & 46 deletions helpers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
submissionurl: https://<kattis>/submit"""


def getConfig():
def getConfig(shouldReturnLocation = False):
cfg = configparser.ConfigParser(converters={"array": strToArr, "command": toCommandArray})

alternativeLocations = [
Expand All @@ -35,10 +35,14 @@ def getConfig():

if not found:
print(_CONFIG_NOT_FOUND_MSG)
if shouldReturnLocation:
return -1, ""
return -1

cfg = preconfigure(cfg, found)

if shouldReturnLocation:
return cfg, found
return cfg

def getUrl(cfg, option, default):
Expand All @@ -51,68 +55,84 @@ def formatUrl(hostname, path):
return "https://%s/%s" % (hostname, path)

def preconfigure(cfg, location):
if "kat" in cfg.sections():
return cfg
cfg["kat"] = {
"language": "python",
"openFileCommand": "",
"workCommand": "",
defaults = {
"kat": {
"language": "python",
"openFileCommand": "",
"workCommand": "",
},
"File associations": {
".c": "C",
".c#": "C#",
".c++": "C++",
".cc": "C++",
".cpp": "C++",
".cs": "C#",
".cxx": "C++",
".go": "Go",
".h": "C++",
".hs": "Haskell",
".java": "Java",
".js": "JavaScript",
".m": "Objective-C",
".pas": "Pascal",
".php": "PHP",
".pl": "Prolog",
".py": "Python",
".rb": "Ruby",
".fs": "F#",
".fsx": "F#",
".fsscript": "F#",
},
"Initialize commands": {
"F#": "dotnet new console -lang F#",
"C#": "dotnet new console",
},
"Run commands": {
"Python": "python @f",
"PHP": "php @f",
"Java": "java @c",
"C#": "dotnet run",
"F#": "dotnet run",
# TODO: Support rest of the languages that kattis supports
},
"Compile commands": {
"Java": "javac @f"
}
}
cfg["File associations"] = {
".c": "C",
".c#": "C#",
".c++": "C++",
".cc": "C++",
".cpp": "C++",
".cs": "C#",
".cxx": "C++",
".go": "Go",
".h": "C++",
".hs": "Haskell",
".java": "Java",
".js": "JavaScript",
".m": "Objective-C",
".pas": "Pascal",
".php": "PHP",
".pl": "Prolog",
".py": "Python",
".rb": "Ruby",
".fs": "F#",
".fsx": "F#",
".fsscript": "F#",
}
cfg["Initialize commands"] = {
"F#": "dotnet new console -lang F#",
"C#": "dotnet new console",
}
cfg["Run commands"] = {
"Python": "python @f",
"PHP": "php @f",
"Java": "java @c",
"C#": "dotnet run",
"F#": "dotnet run",
# TODO: Support rest of the languages that kattis supports
}
cfg["Compile commands"] = {"Java": "javac @f"}

for (section, settings) in defaults.items():
if section not in cfg.sections():
cfg.add_section(section)
for (key, value) in settings.items():
_set(cfg[section], key, value)

with open(location, "w") as configFile:
cfg.write(configFile)
return cfg

def _set(cfgForSection, key, value):
if not cfgForSection.get(key, False):
cfgForSection[key] = value

def strToArr(string):
string = string.replace("[","").replace("]","")
string = string.replace("'","").replace('"',"")
return string.split(", ")

def toCommandArray(string):
def toCommandArray(string: str):
splitString = string.split(" ")
return commandConvert(splitString)

def commandConvert(array: list):
result = []
cumulator = None
for item in splitString:
for item in array:
print(item, cumulator)
if item[0] == '"' or item[0] == "'":
cumulator = item
elif cumulator:
cumulator += item
cumulator += " " + item
if cumulator[-1] == cumulator[0]:
result.append(cumulator)
cumulator = None
Expand Down
2 changes: 1 addition & 1 deletion helpers/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def divideArgs(args):
options = {}
for i in range(1, len(args)):
word = args[i]
if "-" in word:
if word.startswith("-"):
otherWord = args[i+1] if i+1 < len(args) else None
option = makeOption(word, otherWord)
if option:
Expand Down
3 changes: 3 additions & 0 deletions kattis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
from helpers.exceptions import RedundantCommandException, InvalidProblemException
from helpers.flags import divideArgs
from commands.unarchive import unarchiveCommand
from commands.config import configCommand
from helpers.config import commandConvert

execCommand = {
"archive": archiveCommand,
"unarchive": unarchiveCommand,
"config": configCommand,
"get": getCommand,
"submit": submitCommand,
"test": testCommand,
Expand Down

0 comments on commit 84842ca

Please sign in to comment.