def save_file(fname, content, dt, dn): import filecmp from webnotes.model.code import load_doctype_module files_path = get_path("public", "files") module = load_doctype_module(dt, webnotes.conn.get_value("DocType", dt, "module")) if hasattr(module, "attachments_folder"): files_path = os.path.join(files_path, module.attachments_folder) file_size = check_max_file_size(content) temp_fname = write_file(content, files_path) fname = scrub_file_name(fname) fpath = os.path.join(files_path, fname) fname_parts = fname.split(".", -1) main = ".".join(fname_parts[:-1]) extn = fname_parts[-1] versions = get_file_versions(files_path, main, extn) if versions: found_match = False for version in versions: if filecmp.cmp(os.path.join(files_path, version), temp_fname): # remove new file, already exists! os.remove(temp_fname) fname = version found_match = True break if not found_match: # get_new_version name fname = get_new_fname_based_on_version(files_path, main, extn, versions) fpath = os.path.join(files_path, fname) # rename if os.path.exists(fpath.encode("utf-8")): webnotes.throw("File already exists: " + fname) os.rename(temp_fname, fpath.encode("utf-8")) else: # rename new file if os.path.exists(fpath.encode("utf-8")): webnotes.throw("File already exists: " + fname) os.rename(temp_fname, fpath.encode("utf-8")) f = webnotes.bean({ "doctype": "File Data", "file_name": os.path.relpath(fpath, get_path("public")), "attached_to_doctype": dt, "attached_to_name": dn, "file_size": file_size }) f.ignore_permissions = True f.insert(); return f.doc
def load_translations(bootinfo): webnotes.set_user_lang(webnotes.session.user) if webnotes.lang != 'en': from webnotes.translate import get_lang_data from webnotes.utils import get_path # framework bootinfo["__messages"] = get_lang_data(get_path("lib","public", "js", "wn"), None, "js") # doctype and module names bootinfo["__messages"].update(get_lang_data(get_path("app","public", "js"), None, "js")) bootinfo["lang"] = webnotes.lang
def prepare_docs(): os.chdir(get_path("public")) if not os.path.exists("docs"): os.mkdir("docs") if not os.path.exists("docs/css"): os.mkdir("docs/css") os.mkdir("docs/css/font") os.system("cp ../lib/public/css/bootstrap.css docs/css") os.system("cp ../lib/public/css/font-awesome.css docs/css") os.system("cp ../lib/public/css/font/* docs/css/font") os.system("cp ../lib/public/css/prism.css docs/css") # clean links in font-awesome with open("docs/css/font-awesome.css", "r") as fontawesome: t = fontawesome.read() t = t.replace("../lib/css/", "") with open("docs/css/font-awesome.css", "w") as fontawesome: fontawesome.write(t) # copy latest docs.css os.system("cp ../lib/core/doctype/documentation_tool/docs.css docs/css") if not os.path.exists("docs/js"): os.mkdir("docs/js") os.system("cp ../lib/public/js/lib/bootstrap.min.js docs/js") os.system("cp ../lib/public/js/lib/jquery/jquery.min.js docs/js") os.system("cp ../lib/public/js/lib/prism.js docs/js") if not os.path.exists("docs/img/splash.svg"): if not os.path.exists("docs/img"): os.mkdir("docs/img") os.system("cp ../app/public/images/splash.svg docs/img")
def make(site=None): """make public folder symlinks if missing""" from webnotes.utils import get_site_base_path, get_base_path, get_path webnotes.init(site=site) site_path = get_site_base_path() if site else get_base_path() # setup standard folders for param in (("public_path", "public"), ("backup_path", "public/backups"), ("files_path", "public/files")): path = os.path.join(site_path, webnotes.conf.get(param[0], param[1])) if not os.path.exists(path): os.mkdir(path) # setup js and css folders if not site: for folder in ("js", "css"): path = get_path(webnotes.conf.get("public_path", "public"), folder) if not os.path.exists(path): os.mkdir(path) os.chdir(webnotes.conf.get("public_path", "public")) symlinks = [ ["app", "../app/public"], ["lib", "../lib/public"], ] for link in symlinks: if not os.path.exists(link[0]) and os.path.exists(link[1]): os.symlink(link[1], link[0]) os.chdir("..")
def write_docs(data, build_sitemap=None, domain=None): from webnotes.utils import global_date_format if webnotes.session.user != "Administrator": raise webnotes.PermissionError with open(os.path.join(os.path.dirname(__file__), "docs.html"), "r") as docshtml: docs_template = docshtml.read() if isinstance(data, basestring): data = json.loads(data) template = Template(docs_template) data["index"] = data["docs"] data["docs"] = None for name, d in data.items(): if d: if not d.get("title"): d["title"] = d["_label"] if d.get("_parent_page") == "docs.html": d["_parent_page"] = "index.html" if not d.get("_icon"): d["_icon"] = "icon-file-alt" if not d["_icon"].startswith("icon-"): d["_icon"] = "icon-" + d["_icon"] if d.get("_modified"): d["_modified"] = global_date_format(d["_modified"]) with open(get_path("public", "docs", name + ".html"), "w") as docfile: if not d.get("description"): d["description"] = "Help pages for " + d["title"] html = template.render(d) docfile.write(html.encode("utf-8", errors="ignore")) if build_sitemap and domain: if not domain.endswith("/"): domain = domain + "/" content = "" for fname in os.listdir(get_path("public", "docs")): if fname.endswith(".html"): content += sitemap_link_xml % ( domain + fname, get_timestamp(get_path("public", "docs", fname))) with open(get_path("public", "docs", "sitemap.xml"), "w") as sitemap: sitemap.write(sitemap_frame_xml % content)
def loader(path): import conf fail = True if hasattr(conf, 'sites_dir'): site = get_site_name(self.environ.get('HTTP_HOST')) possible_site_path = get_path(directory, path, base=os.path.join(conf.sites_dir, site)) if os.path.isfile(possible_site_path): path = possible_site_path fail = False if fail and os.path.isfile(get_path(directory, path)): path = get_path(directory, path) fail = False if fail: return None, None return os.path.basename(path), self._opener(path)
def write_docs(data, build_sitemap=None, domain=None): from webnotes.utils import global_date_format if webnotes.session.user != "Administrator": raise webnotes.PermissionError if isinstance(data, basestring): data = json.loads(data) jenv = Environment(loader = FileSystemLoader(webnotes.utils.get_base_path())) template = jenv.get_template("app/docs/templates/docs.html") data["index"] = data["docs"] data["docs"] = None for name, d in data.items(): if d: if not d.get("title"): d["title"] = d["_label"] if d.get("_parent_page")=="docs.html": d["_parent_page"] = "index.html" if not d.get("_icon"): d["_icon"] = "icon-file-alt" if not d["_icon"].startswith("icon-"): d["_icon"] = "icon-" + d["_icon"] if d.get("_modified"): d["_modified"] = global_date_format(d["_modified"]) with open(get_path("public", "docs", name + ".html"), "w") as docfile: if not d.get("description"): d["description"] = "Help pages for " + d["title"] html = template.render(d) docfile.write(html.encode("utf-8", errors="ignore")) if build_sitemap and domain: if not domain.endswith("/"): domain = domain + "/" content = "" for fname in os.listdir(get_path("public", "docs")): fname = cstr(fname) if fname.endswith(".html"): content += sitemap_link_xml % (domain + fname, get_timestamp(get_path("public", "docs", fname))) with open(get_path("public", "docs", "sitemap.xml"), "w") as sitemap: sitemap.write(sitemap_frame_xml % content)
def get_static_pages(): mydocs = {} for repo in ("lib", "app"): for fname in os.listdir(get_path(repo, "docs")): if fname.endswith(".md"): fpath = get_path(repo, "docs", fname) with open(fpath, "r") as docfile: src = unicode(docfile.read(), "utf-8") try: temp, headers, body = src.split("---", 2) d = json.loads(headers) except Exception, e: webnotes.msgprint("Bad Headers in: " + fname) continue d["_intro"] = body d["_gh_source"] = get_gh_url(fpath) d["_modified"] = get_timestamp(fpath) mydocs[fname[:-3]] = d
def loader(path): import conf path = cstr(path) fail = True if hasattr(conf, 'sites_dir'): site = get_site_name(self.environ.get('HTTP_HOST')) possible_site_path = get_path(directory, path, base=os.path.join(conf.sites_dir, site)) if os.path.isfile(possible_site_path): path = possible_site_path fail = False if fail and os.path.isfile(get_path(directory, path)): path = get_path(directory, path) fail = False if fail: return None, None return os.path.basename(path), self._opener(path)
def get_city(): p = pygeoip.GeoIP(get_path("app", "data", "GeoLiteCity.dat")) try: r = p.record_by_addr(webnotes.get_request_header("REMOTE_ADDR")) except Exception, e: r = { "city": "Mumbai", "latitude": "18.974999999999994", "longitude": "72.82579999999999" }
def get_context(self): from data.utils import get_file_data from webnotes.utils import get_path headers, self.doc.data = get_file_data(get_path("app", "downloads", "data.gov.in", self.doc.raw_filename)) self.doc.max_cols = max([len(r) for r in self.doc.data]) self.doc.comment_list = webnotes.conn.sql("""\ select comment, comment_by_fullname, creation from `tabComment` where comment_doctype="Data Set" and comment_docname=%s order by creation""", self.doc.name, as_dict=1)
def make_word_count(): for ds in webnotes.conn.sql( """select name, raw_filename from `tabData Set`""", as_dict=1): from webnotes.utils import get_path if ds.raw_filename: headers, data = utils.get_file_data( get_path("app", "downloads", "data.gov.in", ds.raw_filename)) webnotes.conn.set_value("Data Set", ds.name, "row_count", len(data)) webnotes.conn.commit()
def get_file(fname): f = webnotes.conn.sql("""select file_name from `tabFile Data` where name=%s or file_name=%s""", (fname, fname)) if f: file_name = f[0][0] else: file_name = fname if not "/" in file_name: file_name = "files/" + file_name # read the file with open(get_path("public", file_name), 'r') as f: content = f.read() return [file_name, content]
def make(): from webnotes.webutils import get_home_page from webnotes.utils import get_path if not webnotes.conn: webnotes.connect() home_page = get_home_page() if not os.path.exists(get_path("public", "js")): os.makedirs(get_path("public", "js")) fname = os.path.join(get_path("public", "js", "wn-web.js")) with open(fname, 'w') as f: f.write(get_web_script()) if not os.path.exists(get_path("public", "css")): os.makedirs(get_path("public", "css")) fname = os.path.join(get_path("public", "css", "wn-web.css")) with open(fname, 'w') as f: f.write(get_web_style())
def sync_for(folder, force=0, sync_everything = False, verbose=False): return walk_and_sync(get_path(folder), force, sync_everything, verbose=verbose)
def get_files_path(): global files_path if not files_path: files_path = get_path("public", "files") return files_path