#!/usr/bin/env python3

import os.path

from pathlib import PurePath as Path
from string import Template

import bs4
import requests


def read_template():
    import os.path
    template = os.path.join(os.path.dirname(__file__), 'jdt-language-server.template')
    with open(template, 'r') as f:
        return Template(f.read())

def url(filename):
    return '/'.join(['http://download.eclipse.org/jdtls/snapshots', filename])

def find_latest():
    r = requests.get(url('latest.txt'))
    r.raise_for_status()
    return r.text.strip()

def fetch_sha256(filename, hash_function='sha256'):
    sha256_file = '.'.join([filename, hash_function])
    r = requests.get(url(sha256_file))
    r.raise_for_status()
    return r.text.strip()

def extract_version_string(filename):
    a = '-'.join(filename.rsplit('-')[-2:])
    return '.'.join(a.split('.')[:-2])

def file_exists(filename):
    r = requests.head(url(filename))
    return r.status_code < 400

if __name__ == '__main__':
    latest_snapshot_file = find_latest()
    if file_exists(latest_snapshot_file):
        urrrl = url(latest_snapshot_file)
        sha256 = fetch_sha256(latest_snapshot_file)
        version = extract_version_string(latest_snapshot_file)
        template = read_template()
        print(template.substitute({
            'url': urrrl,
            'sha256': sha256,
            'version': version,
        }), end='')

@@ -168,16 +168,17 @@
 os.path%0A
+%0A
     temp
@@ -224,17 +224,17 @@
 ile__), 
-'
+%22
 jdt-lang
@@ -253,17 +253,17 @@
 template
-'
+%22
 )%0A    wi
@@ -284,11 +284,11 @@
 te, 
-'r'
+%22r%22
 ) as
@@ -326,16 +326,17 @@
 ead())%0A%0A
+%0A
 def url(
@@ -361,19 +361,19 @@
 urn 
-'/'
+%22/%22
 .join(%5B
-'
+%22
 http
@@ -411,17 +411,17 @@
 napshots
-'
+%22
 , filena
@@ -422,24 +422,25 @@
 filename%5D)%0A%0A
+%0A
 def find_lat
@@ -471,17 +471,17 @@
 get(url(
-'
+%22
 latest.t
@@ -482,17 +482,17 @@
 test.txt
-'
+%22
 ))%0A    r
@@ -534,24 +534,25 @@
 xt.strip()%0A%0A
+%0A
 def fetch_sh
@@ -580,24 +580,24 @@
 unction=
-'
+%22
 sha256
-'
+%22
 ):%0A    s
@@ -609,19 +609,19 @@
 _file = 
-'.'
+%22.%22
 .join(%5Bf
@@ -736,16 +736,17 @@
 trip()%0A%0A
+%0A
 def extr
@@ -783,19 +783,19 @@
     a = 
-'-'
+%22-%22
 .join(fi
@@ -812,11 +812,11 @@
 lit(
-'-'
+%22-%22
 )%5B-2
@@ -830,19 +830,19 @@
  return 
-'.'
+%22.%22
 .join(a.
@@ -851,11 +851,11 @@
 lit(
-'.'
+%22.%22
 )%5B:-
@@ -859,16 +859,17 @@
 %5B:-2%5D)%0A%0A
+%0A
 def file
@@ -956,16 +956,17 @@
  %3C 400%0A%0A
+%0A
 if __nam
@@ -976,17 +976,17 @@
  == 
-'
+%22
 __main__
 ':%0A 
@@ -981,17 +981,17 @@
 __main__
-'
+%22
 :%0A    la
@@ -1273,16 +1273,29 @@
   print(
+%0A            
 template
@@ -1311,34 +1311,21 @@
 te(%7B
-%0A            'url'
+%22url%22
 : urrrl,
 %0A   
@@ -1324,29 +1324,17 @@
 rrl,
-%0A            '
+ %22
 sha256
-'
+%22
 : sh
@@ -1342,30 +1342,18 @@
 256,
-%0A            '
+ %22
 version
-'
+%22
 : ve
@@ -1357,16 +1357,18 @@
  version
+%7D)
 ,%0A      
@@ -1373,16 +1373,26 @@
     
-%7D),
+   
  end=
-''
+%22%22,%0A        
 )%0A

Reformat updater with Black