[PYTHON] I thought I could make a nice gitignore editor, so I tried to make something like MVP for the time being

Ki: A little trouble with .gitignore

The beginning of that

One of the main tasks to do after ʻInitial commit of a project is to "create a nice .gitignore` from gitignore.io, right? [^ 1] [^ 2]

** Is there? ** **

.Gitignore changes over time ...

There is no problem if it can be operated as it is at the beginning,

  1. Oh, I also want to add ignore for this environment
  2. It seems that a little special ignore will be needed

That may come out.

** It will come out. ** **

In case (1), you may have to go to gitignore.io again and regenerate it.

In the case of (2), I have to write it somewhere in the template-generated .gitignore. If this happens, you may feel some trouble each time you respond to (1). Perhaps

Acceptance: Rough ideas and MVP-like

Idea

So I wondered if I could make something like "an editor that simply writes" language name "and" language ignore "in a nice way when editing .gitignore. " Isn't the UI like lazygit cool?

こんなの

If you can do it, it would be convenient to throw in all the patterns you want to ignore in the derivation.

However, I don't currently have the ability to create editors in this format.

Try to make something like that (= MVP?) Without any hassle

I tried a super-simple implementation that looks like MVP. Here => vigi = vi gitignore

When you run the vigi command,


# ~~~~~~

Open a file like this with vim and

.idea

# ~~~~~~
python
virtualenv

If you edit like this and exit vim,

.idea

# ~~~~~~

# https://www.gitignore.io/api/python,Contents of virtualenv

It will create a .gitignore in a state like this.

By the way, if you execute the vigi command in this state, the gitignore.io part will be information only in the language. Easy to edit ... I think.

Tran: The contents look like this

A style that creates a temporary file with the name .gi, edits it, and converts it to .gitignore when it is saved.

import subprocess
from pathlib import Path
from subprocess import CalledProcessError, PIPE

def main():
    gi_path = Path.cwd() / '.gi'
    gitignore_path = Path.cwd() / '.gitignore'
    try:
        # Convert from ``.gitignore`` to ``.gi``
        if gitignore_path.exists():
            encode_gitignore(gitignore_path, gi_path)
        else:
            with gi_path.open('w') as fp:
                fp.write(dedent(gi_template).strip())
        # Spawn vim
        vim_args = ['/usr/bin/vim', str(gi_path)]
        vim_proc = subprocess.run(vim_args, check=True)
        # Convert from ``.gi`` to ``.gitignore``
        decode_gitignore(gi_path, gitignore_path)
    except CalledProcessError:
        pass
    finally:
        # Remove ``.gi``
        gi_path.unlink()

call vim

A rough style that calls vim with subprocess.run and is OK if there are no CalledProcessError exceptions. It's easy and good.

Do not leave .gi

I'm using try ~ finally and finally throwing away the .gi file. If there is an error, .gitignore is not edited, so it is environmentally friendly.

Make .gitignore based on the material

The .gi file is normally on the filesystem at this point, so it feels like opening it as a normal file. Then, put together the information on the language side and pass it to gitignore.io. Only when the HTTP status is 404, I save the response as usual, but This is because all cases that "contain information that does not exist in the master" seem to be 404. It's the only thing that can be said to be a little ingenuity.

def decode_gitignore(src, dest):
    custom = []
    online = []
    with src.open() as fp:
        # .Open the gi file and separate the front and back of the delimiter, etc.
    if online:
        url = f'https://www.gitignore.io/api/{",".join(online)}'
        req = Request(url, None, headers={'User-Agent': 'vigi'})
        try:
            resp = urlopen(req)
            custom.append(resp.read().decode())
        except HTTPError as err:
            if err.code == 404:
                custom.append(resp.read().decode())
            raise err
    with dest.open('w') as fp:
        fp.write('\n'.join(custom))

Conclusion: Usability

For the time being, .gitignore in the vigi project was created based on this command.

I think it is a merit that the number of lines displayed at the time of editing is significantly reduced. In that sense, he did his job as an MVP properly.

However, I don't know what kind of pattern is included in the language master until I finish the conversion to .gitignore. If I wanted to develop it a little more, I had to pull out the language master from the pattern candidates properly. I don't know if it's really good. ~~ Maybe it's suitable for training in some language? ~~

[^ 1]: There is a project template, but please forget it. [^ 2]: You can also manage it with dotfiles, but forget about this too

Recommended Posts

I thought I could make a nice gitignore editor, so I tried to make something like MVP for the time being
[Hi Py (Part 1)] I want to make something for the time being, so first set a goal.
I want to create a Dockerfile for the time being.
Make a histogram for the time being (matplotlib)
I want to move selenium for the time being [for mac]
I tried running PIFuHD on Windows for the time being
I tried to make a simple text editor using PyQt
I want to see something beautiful, so I tried to visualize the function used for benchmarking the optimization function.
Java programmer tried to touch Go language (for the time being)
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
For the time being, I want to convert files with ffmpeg !!
I tried to make a strange quote for Jojo with LSTM
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
I made a function to check if the webhook is received in Lambda for the time being
I tried tensorflow for the first time
I tried to make a face diagnosis AI for a female professional golfer ①
I tried to make a face diagnosis AI for a female professional golfer ②
I tried to make a calculator with Tkinter so I will write it
I tried to make a Web API
For the time being using FastAPI, I want to display how to use API like that on swagger
I wanted to use the find module of Ansible2, but it took some time, so make a note
I tried to move the 3D model by doing something like motion capture with just a laptop + webcam
I want to make a music player and file music at the same time
I bought Sipeed Lichee Zero so I set it up for the time being
I tried using scrapy for the first time
I played with Floydhub for the time being
I tried python programming for the first time.
I tried to make AI for Smash Bros.
I tried Mind Meld for the first time
I tried to make a ○ ✕ game using TensorFlow
I tried to make OneHotEncoder, which is often used for data analysis, so that it can reach the itch.
I will try to summarize the links that seem to be useful for the time being
I want to create a lunch database [EP1] Django study for the first time
A Python beginner made a chat bot, so I tried to summarize how to make it.
It's Cat Day, so I tried to make something that translates into cat-like words.
I tried to make a "fucking big literary converter"
I tried Python on Mac for the first time.
Flow memo to move LOCUST for the time being
Molecular dynamics simulation to try for the time being
I tried python on heroku for the first time
I will install Arch Linux for the time being.
Next to Excel, for the time being, jupyter notebook
AI Gaming I tried it for the first time
I tried to make a function to judge whether the major stock exchanges in the world are daylight saving time with python
"Stop committing Japanese files to git on Mac> <" For the time being, I wrote a script to search for incompatible Japanese files on Mac and Linux.
I tried to make the political broadcast video like IPPON Grand Prix (OpenCV: Python version)
I want to use Ubuntu's desktop environment on Android for the time being (UserLAnd version)
I tried porting the code written for TensorFlow to Theano
I want to make a blog editor with django admin
I tried to illustrate the time and time in C language
I tried to display the time and today's weather w
I stumbled when I tried to install Basemap, so a memorandum
I tried the Google Cloud Vision API for the first time
I tried to create a bot for PES event notification
I tried to make a stopwatch using tkinter in python
[Introduction to Reinforcement Learning] Reinforcement learning to try moving for the time being
I want to make input () a nice complement in python
I tried to make a site that makes it easy to see the update information of Azure
I tried to create serverless batch processing for the first time with DynamoDB and Step Functions
[Python] I tried to make a simple program that works on the command line using argparse.